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