VirtualBox

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

Last change on this file since 285 was 227, checked in by bird, 20 years ago

builtin stuff.

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

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