]> git.saurik.com Git - apple/libc.git/blob - gen/setmode-fbsd.c
Libc-594.9.1.tar.gz
[apple/libc.git] / gen / setmode-fbsd.c
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)
38 static 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
61 typedef 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
73 #define compress_mode _sm_compress_mode
74
75 static BITCMD *addcmd(BITCMD *, int, int, int, u_int);
76 __private_extern__ void compress_mode(BITCMD *);
77 #ifdef SETMODE_DEBUG
78 static void dumpmode(BITCMD *);
79 #endif
80
81 #ifndef BUILDING_VARIANT
82 /*
83 * Given the old mode and an array of bitcmd structures, apply the operations
84 * described in the bitcmd structures to the old mode, and return the new mode.
85 * Note that there is no '=' command; a strict assignment is just a '-' (clear
86 * bits) followed by a '+' (set bits).
87 */
88 mode_t
89 getmode(bbox, omode)
90 const void *bbox;
91 mode_t omode;
92 {
93 const BITCMD *set;
94 mode_t clrval, newmode, value;
95
96 set = (const BITCMD *)bbox;
97 newmode = omode;
98 for (value = 0;; set++)
99 switch(set->cmd) {
100 /*
101 * When copying the user, group or other bits around, we "know"
102 * where the bits are in the mode so that we can do shifts to
103 * copy them around. If we don't use shifts, it gets real
104 * grundgy with lots of single bit checks and bit sets.
105 */
106 case 'u':
107 value = (newmode & S_IRWXU) >> 6;
108 goto common;
109
110 case 'g':
111 value = (newmode & S_IRWXG) >> 3;
112 goto common;
113
114 case 'o':
115 value = newmode & S_IRWXO;
116 common: if (set->cmd2 & CMD2_CLR) {
117 clrval =
118 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
119 if (set->cmd2 & CMD2_UBITS)
120 newmode &= ~((clrval<<6) & set->bits);
121 if (set->cmd2 & CMD2_GBITS)
122 newmode &= ~((clrval<<3) & set->bits);
123 if (set->cmd2 & CMD2_OBITS)
124 newmode &= ~(clrval & set->bits);
125 }
126 if (set->cmd2 & CMD2_SET) {
127 if (set->cmd2 & CMD2_UBITS)
128 newmode |= (value<<6) & set->bits;
129 if (set->cmd2 & CMD2_GBITS)
130 newmode |= (value<<3) & set->bits;
131 if (set->cmd2 & CMD2_OBITS)
132 newmode |= value & set->bits;
133 }
134 break;
135
136 case '+':
137 newmode |= set->bits;
138 break;
139
140 case '-':
141 newmode &= ~set->bits;
142 break;
143
144 case 'X':
145 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
146 newmode |= set->bits;
147 break;
148
149 case '\0':
150 default:
151 #ifdef SETMODE_DEBUG
152 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
153 #endif
154 return (newmode);
155 }
156 }
157 #endif /* BUILDING_VARIANT */
158
159 #define ADDCMD(a, b, c, d) \
160 if (set >= endset) { \
161 BITCMD *newset; \
162 setlen += SET_LEN_INCR; \
163 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
164 if (!newset) { \
165 if (saveset) \
166 free(saveset); \
167 saveset = NULL; \
168 return (NULL); \
169 } \
170 set = newset + (set - saveset); \
171 saveset = newset; \
172 endset = newset + (setlen - 2); \
173 } \
174 set = addcmd(set, (a), (b), (c), (d))
175
176 #ifndef VARIANT_LEGACY
177 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO|S_ISTXT)
178 #else /* VARIANT_LEGACY */
179 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
180 #endif /* !VARIANT_LEGACY */
181
182 void *
183 setmode(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 #ifndef VARIANT_LEGACY
223 if (*ep || perml < 0 || perml & ~STANDARD_BITS)
224 #else /* VARIANT_LEGACY */
225 if (*ep || perml < 0 || perml & ~(STANDARD_BITS|S_ISTXT))
226 #endif /* !VARIANT_LEGACY */
227 {
228 free(saveset);
229 return (NULL);
230 }
231 perm = (mode_t)perml;
232 #ifndef VARIANT_LEGACY
233 ADDCMD('=', STANDARD_BITS, perm, mask);
234 #else /* VARIANT_LEGACY */
235 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
236 #endif /* !VARIANT_LEGACY */
237 set->cmd = 0;
238 return (saveset);
239 }
240
241 /*
242 * Build list of structures to set/clear/copy bits as described by
243 * each clause of the symbolic mode.
244 */
245 for (;;) {
246 /* First, find out which bits might be modified. */
247 for (who = 0;; ++p) {
248 switch (*p) {
249 case 'a':
250 who |= STANDARD_BITS;
251 break;
252 case 'u':
253 who |= S_ISUID|S_IRWXU;
254 break;
255 case 'g':
256 who |= S_ISGID|S_IRWXG;
257 break;
258 case 'o':
259 who |= S_IRWXO;
260 break;
261 default:
262 goto getop;
263 }
264 }
265
266 getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
267 free(saveset);
268 return (NULL);
269 }
270 if (op == '=')
271 equalopdone = 0;
272
273 #ifdef VARIANT_LEGACY
274 who &= ~S_ISTXT;
275 #endif /* VARIANT_LEGACY */
276 for (perm = 0, permXbits = 0;; ++p) {
277 switch (*p) {
278 case 'r':
279 perm |= S_IRUSR|S_IRGRP|S_IROTH;
280 break;
281 case 's':
282 /* If only "other" bits ignore set-id. */
283 if (!who || who & ~S_IRWXO)
284 perm |= S_ISUID|S_ISGID;
285 break;
286 case 't':
287 /* If only "other" bits ignore sticky. */
288 if (!who || who & ~S_IRWXO) {
289 #ifdef VARIANT_LEGACY
290 who |= S_ISTXT;
291 #endif /* VARIANT_LEGACY */
292 perm |= S_ISTXT;
293 }
294 break;
295 case 'w':
296 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
297 break;
298 case 'X':
299 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
300 break;
301 case 'x':
302 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
303 break;
304 case 'u':
305 case 'g':
306 case 'o':
307 /*
308 * When ever we hit 'u', 'g', or 'o', we have
309 * to flush out any partial mode that we have,
310 * and then do the copying of the mode bits.
311 */
312 if (perm) {
313 ADDCMD(op, who, perm, mask);
314 perm = 0;
315 }
316 if (op == '=')
317 equalopdone = 1;
318 if (op == '+' && permXbits) {
319 ADDCMD('X', who, permXbits, mask);
320 permXbits = 0;
321 }
322 ADDCMD(*p, who, op, mask);
323 break;
324
325 default:
326 /*
327 * Add any permissions that we haven't already
328 * done.
329 */
330 if (perm || (op == '=' && !equalopdone)) {
331 if (op == '=')
332 equalopdone = 1;
333 ADDCMD(op, who, perm, mask);
334 perm = 0;
335 }
336 if (permXbits) {
337 ADDCMD('X', who, permXbits, mask);
338 permXbits = 0;
339 }
340 goto apply;
341 }
342 }
343
344 apply: if (!*p)
345 break;
346 if (*p != ',')
347 goto getop;
348 ++p;
349 }
350 set->cmd = 0;
351 #ifdef SETMODE_DEBUG
352 (void)printf("Before compress_mode()\n");
353 dumpmode(saveset);
354 #endif
355 compress_mode(saveset);
356 #ifdef SETMODE_DEBUG
357 (void)printf("After compress_mode()\n");
358 dumpmode(saveset);
359 #endif
360 return (saveset);
361 }
362
363 static BITCMD *
364 addcmd(set, op, who, oparg, mask)
365 BITCMD *set;
366 int oparg, who;
367 int op;
368 u_int mask;
369 {
370 switch (op) {
371 case '=':
372 set->cmd = '-';
373 set->bits = who ? who : STANDARD_BITS;
374 set++;
375
376 op = '+';
377 /* FALLTHROUGH */
378 case '+':
379 case '-':
380 case 'X':
381 set->cmd = op;
382 set->bits = (who ? who : mask) & oparg;
383 break;
384
385 case 'u':
386 case 'g':
387 case 'o':
388 set->cmd = op;
389 if (who) {
390 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
391 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
392 ((who & S_IROTH) ? CMD2_OBITS : 0);
393 set->bits = (mode_t)~0;
394 } else {
395 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
396 set->bits = mask;
397 }
398
399 if (oparg == '+')
400 set->cmd2 |= CMD2_SET;
401 else if (oparg == '-')
402 set->cmd2 |= CMD2_CLR;
403 else if (oparg == '=')
404 set->cmd2 |= CMD2_SET|CMD2_CLR;
405 break;
406 }
407 return (set + 1);
408 }
409
410 #ifdef SETMODE_DEBUG
411 static void
412 dumpmode(set)
413 BITCMD *set;
414 {
415 for (; set->cmd; ++set)
416 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
417 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
418 set->cmd2 & CMD2_CLR ? " CLR" : "",
419 set->cmd2 & CMD2_SET ? " SET" : "",
420 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
421 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
422 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
423 }
424 #endif
425
426 #ifndef BUILDING_VARIANT
427 /*
428 * Given an array of bitcmd structures, compress by compacting consecutive
429 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
430 * 'g' and 'o' commands continue to be separate. They could probably be
431 * compacted, but it's not worth the effort.
432 */
433 __private_extern__ void
434 compress_mode(set)
435 BITCMD *set;
436 {
437 BITCMD *nset;
438 int setbits, clrbits, Xbits, op;
439
440 for (nset = set;;) {
441 /* Copy over any 'u', 'g' and 'o' commands. */
442 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
443 *set++ = *nset++;
444 if (!op)
445 return;
446 }
447
448 for (setbits = clrbits = Xbits = 0;; nset++) {
449 if ((op = nset->cmd) == '-') {
450 clrbits |= nset->bits;
451 setbits &= ~nset->bits;
452 Xbits &= ~nset->bits;
453 } else if (op == '+') {
454 setbits |= nset->bits;
455 clrbits &= ~nset->bits;
456 Xbits &= ~nset->bits;
457 } else if (op == 'X')
458 Xbits |= nset->bits & ~setbits;
459 else
460 break;
461 }
462 if (clrbits) {
463 set->cmd = '-';
464 set->cmd2 = 0;
465 set->bits = clrbits;
466 set++;
467 }
468 if (setbits) {
469 set->cmd = '+';
470 set->cmd2 = 0;
471 set->bits = setbits;
472 set++;
473 }
474 if (Xbits) {
475 set->cmd = 'X';
476 set->cmd2 = 0;
477 set->bits = Xbits;
478 set++;
479 }
480 }
481 }
482 #endif /* BUILDING_VARIANT */