VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/setmode.c@ 616

Last change on this file since 616 was 616, checked in by bird, 18 years ago

Ported the new setmode to MSC.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/* $NetBSD: setmode.c,v 1.30 2003/08/07 16:42:56 agc Exp $ */
2
3/*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Dave Borman at Cray Research, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35/*#include <sys/cdefs.h>*/
36#if defined(LIBC_SCCS) && !defined(lint)
37#if 0
38static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
39#else
40__RCSID("$NetBSD: setmode.c,v 1.30 2003/08/07 16:42:56 agc Exp $");
41#endif
42#endif /* LIBC_SCCS and not lint */
43
44/*#include "namespace.h"*/
45#include <sys/types.h>
46#include <sys/stat.h>
47
48#include <assert.h>
49#include <ctype.h>
50#include <errno.h>
51#include <signal.h>
52#include <stdlib.h>
53#ifndef _MSC_VER
54#include <unistd.h>
55#else
56#include "mscfakes.h"
57#endif
58
59#ifdef SETMODE_DEBUG
60#include <stdio.h>
61#endif
62
63/*#ifdef __weak_alias
64__weak_alias(getmode,_getmode)
65__weak_alias(setmode,_setmode)
66#endif*/
67
68#define SET_LEN 6 /* initial # of bitcmd struct to malloc */
69#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
70
71typedef struct bitcmd {
72 char cmd;
73 char cmd2;
74 mode_t bits;
75} BITCMD;
76
77#define CMD2_CLR 0x01
78#define CMD2_SET 0x02
79#define CMD2_GBITS 0x04
80#define CMD2_OBITS 0x08
81#define CMD2_UBITS 0x10
82
83static BITCMD *addcmd(BITCMD *, int, int, int, u_int);
84static void compress_mode(BITCMD *);
85#ifdef SETMODE_DEBUG
86static void dumpmode(BITCMD *);
87#endif
88
89#ifndef _DIAGASSERT
90# define _DIAGASSERT assert
91#endif
92
93#ifndef S_ISTXT
94# ifdef S_ISVTX
95# define S_ISTXT S_ISVTX
96# else
97# define S_ISTXT 0
98# endif
99#endif /* !S_ISTXT */
100
101/*
102 * Given the old mode and an array of bitcmd structures, apply the operations
103 * described in the bitcmd structures to the old mode, and return the new mode.
104 * Note that there is no '=' command; a strict assignment is just a '-' (clear
105 * bits) followed by a '+' (set bits).
106 */
107mode_t
108getmode(bbox, omode)
109 const void *bbox;
110 mode_t omode;
111{
112 const BITCMD *set;
113 mode_t clrval, newmode, value;
114
115 _DIAGASSERT(bbox != NULL);
116
117 set = (const BITCMD *)bbox;
118 newmode = omode;
119 for (value = 0;; set++)
120 switch(set->cmd) {
121 /*
122 * When copying the user, group or other bits around, we "know"
123 * where the bits are in the mode so that we can do shifts to
124 * copy them around. If we don't use shifts, it gets real
125 * grundgy with lots of single bit checks and bit sets.
126 */
127 case 'u':
128 value = (newmode & S_IRWXU) >> 6;
129 goto common;
130
131 case 'g':
132 value = (newmode & S_IRWXG) >> 3;
133 goto common;
134
135 case 'o':
136 value = newmode & S_IRWXO;
137common: if (set->cmd2 & CMD2_CLR) {
138 clrval =
139 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
140 if (set->cmd2 & CMD2_UBITS)
141 newmode &= ~((clrval<<6) & set->bits);
142 if (set->cmd2 & CMD2_GBITS)
143 newmode &= ~((clrval<<3) & set->bits);
144 if (set->cmd2 & CMD2_OBITS)
145 newmode &= ~(clrval & set->bits);
146 }
147 if (set->cmd2 & CMD2_SET) {
148 if (set->cmd2 & CMD2_UBITS)
149 newmode |= (value<<6) & set->bits;
150 if (set->cmd2 & CMD2_GBITS)
151 newmode |= (value<<3) & set->bits;
152 if (set->cmd2 & CMD2_OBITS)
153 newmode |= value & set->bits;
154 }
155 break;
156
157 case '+':
158 newmode |= set->bits;
159 break;
160
161 case '-':
162 newmode &= ~set->bits;
163 break;
164
165 case 'X':
166 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
167 newmode |= set->bits;
168 break;
169
170 case '\0':
171 default:
172#ifdef SETMODE_DEBUG
173 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
174#endif
175 return (newmode);
176 }
177}
178
179#define ADDCMD(a, b, c, d) do { \
180 if (set >= endset) { \
181 BITCMD *newset; \
182 setlen += SET_LEN_INCR; \
183 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
184 if (newset == NULL) { \
185 free(saveset); \
186 return (NULL); \
187 } \
188 set = newset + (set - saveset); \
189 saveset = newset; \
190 endset = newset + (setlen - 2); \
191 } \
192 set = addcmd(set, (a), (b), (c), (d)); \
193} while (/*CONSTCOND*/0)
194
195#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
196
197void *
198setmode(p)
199 const char *p;
200{
201 int perm, who;
202 char op, *ep;
203 BITCMD *set, *saveset, *endset;
204#ifndef _MSC_VER
205 sigset_t signset, sigoset;
206#endif
207 mode_t mask;
208 int equalopdone = 0; /* pacify gcc */
209 int permXbits, setlen;
210
211 if (!*p)
212 return (NULL);
213
214 /*
215 * Get a copy of the mask for the permissions that are mask relative.
216 * Flip the bits, we want what's not set. Since it's possible that
217 * the caller is opening files inside a signal handler, protect them
218 * as best we can.
219 */
220#ifndef _MSC_VER
221 sigfillset(&signset);
222 (void)sigprocmask(SIG_BLOCK, &signset, &sigoset);
223#endif
224 (void)umask(mask = umask(0));
225 mask = ~mask;
226#ifndef _MSC_VER
227 (void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
228#endif
229
230 setlen = SET_LEN + 2;
231
232 if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
233 return (NULL);
234 saveset = set;
235 endset = set + (setlen - 2);
236
237 /*
238 * If an absolute number, get it and return; disallow non-octal digits
239 * or illegal bits.
240 */
241 if (isdigit((unsigned char)*p)) {
242 perm = (mode_t)strtol(p, &ep, 8);
243 if (*ep || perm & ~(STANDARD_BITS|S_ISTXT)) {
244 free(saveset);
245 return (NULL);
246 }
247 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
248 set->cmd = 0;
249 return (saveset);
250 }
251
252 /*
253 * Build list of structures to set/clear/copy bits as described by
254 * each clause of the symbolic mode.
255 */
256 for (;;) {
257 /* First, find out which bits might be modified. */
258 for (who = 0;; ++p) {
259 switch (*p) {
260 case 'a':
261 who |= STANDARD_BITS;
262 break;
263 case 'u':
264 who |= S_ISUID|S_IRWXU;
265 break;
266 case 'g':
267 who |= S_ISGID|S_IRWXG;
268 break;
269 case 'o':
270 who |= S_IRWXO;
271 break;
272 default:
273 goto getop;
274 }
275 }
276
277getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
278 free(saveset);
279 return (NULL);
280 }
281 if (op == '=')
282 equalopdone = 0;
283
284 who &= ~S_ISTXT;
285 for (perm = 0, permXbits = 0;; ++p) {
286 switch (*p) {
287 case 'r':
288 perm |= S_IRUSR|S_IRGRP|S_IROTH;
289 break;
290 case 's':
291 /*
292 * If specific bits where requested and
293 * only "other" bits ignore set-id.
294 */
295 if (who == 0 || (who & ~S_IRWXO))
296 perm |= S_ISUID|S_ISGID;
297 break;
298 case 't':
299 /*
300 * If specific bits where requested and
301 * only "other" bits ignore set-id.
302 */
303 if (who == 0 || (who & ~S_IRWXO)) {
304 who |= S_ISTXT;
305 perm |= S_ISTXT;
306 }
307 break;
308 case 'w':
309 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
310 break;
311 case 'X':
312 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
313 break;
314 case 'x':
315 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
316 break;
317 case 'u':
318 case 'g':
319 case 'o':
320 /*
321 * When ever we hit 'u', 'g', or 'o', we have
322 * to flush out any partial mode that we have,
323 * and then do the copying of the mode bits.
324 */
325 if (perm) {
326 ADDCMD(op, who, perm, mask);
327 perm = 0;
328 }
329 if (op == '=')
330 equalopdone = 1;
331 if (op == '+' && permXbits) {
332 ADDCMD('X', who, permXbits, mask);
333 permXbits = 0;
334 }
335 ADDCMD(*p, who, op, mask);
336 break;
337
338 default:
339 /*
340 * Add any permissions that we haven't already
341 * done.
342 */
343 if (perm || (op == '=' && !equalopdone)) {
344 if (op == '=')
345 equalopdone = 1;
346 ADDCMD(op, who, perm, mask);
347 perm = 0;
348 }
349 if (permXbits) {
350 ADDCMD('X', who, permXbits, mask);
351 permXbits = 0;
352 }
353 goto apply;
354 }
355 }
356
357apply: if (!*p)
358 break;
359 if (*p != ',')
360 goto getop;
361 ++p;
362 }
363 set->cmd = 0;
364#ifdef SETMODE_DEBUG
365 (void)printf("Before compress_mode()\n");
366 dumpmode(saveset);
367#endif
368 compress_mode(saveset);
369#ifdef SETMODE_DEBUG
370 (void)printf("After compress_mode()\n");
371 dumpmode(saveset);
372#endif
373 return (saveset);
374}
375
376static BITCMD *
377addcmd(set, op, who, oparg, mask)
378 BITCMD *set;
379 int oparg, who;
380 int op;
381 u_int mask;
382{
383
384 _DIAGASSERT(set != NULL);
385
386 switch (op) {
387 case '=':
388 set->cmd = '-';
389 set->bits = who ? who : STANDARD_BITS;
390 set++;
391
392 op = '+';
393 /* FALLTHROUGH */
394 case '+':
395 case '-':
396 case 'X':
397 set->cmd = op;
398 set->bits = (who ? who : mask) & oparg;
399 break;
400
401 case 'u':
402 case 'g':
403 case 'o':
404 set->cmd = op;
405 if (who) {
406 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
407 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
408 ((who & S_IROTH) ? CMD2_OBITS : 0);
409 set->bits = (mode_t)~0;
410 } else {
411 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
412 set->bits = mask;
413 }
414
415 if (oparg == '+')
416 set->cmd2 |= CMD2_SET;
417 else if (oparg == '-')
418 set->cmd2 |= CMD2_CLR;
419 else if (oparg == '=')
420 set->cmd2 |= CMD2_SET|CMD2_CLR;
421 break;
422 }
423 return (set + 1);
424}
425
426#ifdef SETMODE_DEBUG
427static void
428dumpmode(set)
429 BITCMD *set;
430{
431
432 _DIAGASSERT(set != NULL);
433
434 for (; set->cmd; ++set)
435 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
436 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
437 set->cmd2 & CMD2_CLR ? " CLR" : "",
438 set->cmd2 & CMD2_SET ? " SET" : "",
439 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
440 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
441 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
442}
443#endif
444
445/*
446 * Given an array of bitcmd structures, compress by compacting consecutive
447 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
448 * 'g' and 'o' commands continue to be separate. They could probably be
449 * compacted, but it's not worth the effort.
450 */
451static void
452compress_mode(set)
453 BITCMD *set;
454{
455 BITCMD *nset;
456 int setbits, clrbits, Xbits, op;
457
458 _DIAGASSERT(set != NULL);
459
460 for (nset = set;;) {
461 /* Copy over any 'u', 'g' and 'o' commands. */
462 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
463 *set++ = *nset++;
464 if (!op)
465 return;
466 }
467
468 for (setbits = clrbits = Xbits = 0;; nset++) {
469 if ((op = nset->cmd) == '-') {
470 clrbits |= nset->bits;
471 setbits &= ~nset->bits;
472 Xbits &= ~nset->bits;
473 } else if (op == '+') {
474 setbits |= nset->bits;
475 clrbits &= ~nset->bits;
476 Xbits &= ~nset->bits;
477 } else if (op == 'X')
478 Xbits |= nset->bits & ~setbits;
479 else
480 break;
481 }
482 if (clrbits) {
483 set->cmd = '-';
484 set->cmd2 = 0;
485 set->bits = clrbits;
486 set++;
487 }
488 if (setbits) {
489 set->cmd = '+';
490 set->cmd2 = 0;
491 set->bits = setbits;
492 set++;
493 }
494 if (Xbits) {
495 set->cmd = 'X';
496 set->cmd2 = 0;
497 set->bits = Xbits;
498 set++;
499 }
500 }
501}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette