setmode.c revision 1.9.4.1 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 /* from: static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94"; */
39 static char *rcsid = "$Id: setmode.c,v 1.9.4.1 1995/05/02 19:35:08 jtc Exp $";
40 #endif /* LIBC_SCCS and not lint */
41
42 #include "namespace.h"
43 #include <sys/types.h>
44 #include <sys/stat.h>
45
46 #include <ctype.h>
47 #include <errno.h>
48 #include <signal.h>
49 #include <stddef.h>
50 #include <stdlib.h>
51
52 #ifdef SETMODE_DEBUG
53 #include <stdio.h>
54 #endif
55
56 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
57 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
58
59 typedef struct bitcmd {
60 char cmd;
61 char cmd2;
62 mode_t bits;
63 } BITCMD;
64
65 #define CMD2_CLR 0x01
66 #define CMD2_SET 0x02
67 #define CMD2_GBITS 0x04
68 #define CMD2_OBITS 0x08
69 #define CMD2_UBITS 0x10
70
71 static BITCMD *addcmd __P((BITCMD *, int, int, int, u_int));
72 static int compress_mode __P((BITCMD *));
73 #ifdef SETMODE_DEBUG
74 static void dumpmode __P((BITCMD *));
75 #endif
76
77 /*
78 * Given the old mode and an array of bitcmd structures, apply the operations
79 * described in the bitcmd structures to the old mode, and return the new mode.
80 * Note that there is no '=' command; a strict assignment is just a '-' (clear
81 * bits) followed by a '+' (set bits).
82 */
83 mode_t
84 getmode(bbox, omode)
85 void *bbox;
86 mode_t omode;
87 {
88 register BITCMD *set;
89 register mode_t clrval, newmode, value;
90
91 set = (BITCMD *)bbox;
92 newmode = omode;
93 for (value = 0;; set++)
94 switch(set->cmd) {
95 /*
96 * When copying the user, group or other bits around, we "know"
97 * where the bits are in the mode so that we can do shifts to
98 * copy them around. If we don't use shifts, it gets real
99 * grundgy with lots of single bit checks and bit sets.
100 */
101 case 'u':
102 value = (newmode & S_IRWXU) >> 6;
103 goto common;
104
105 case 'g':
106 value = (newmode & S_IRWXG) >> 3;
107 goto common;
108
109 case 'o':
110 value = newmode & S_IRWXO;
111 common: if (set->cmd2 & CMD2_CLR) {
112 clrval =
113 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
114 if (set->cmd2 & CMD2_UBITS)
115 newmode &= ~((clrval<<6) & set->bits);
116 if (set->cmd2 & CMD2_GBITS)
117 newmode &= ~((clrval<<3) & set->bits);
118 if (set->cmd2 & CMD2_OBITS)
119 newmode &= ~(clrval & set->bits);
120 }
121 if (set->cmd2 & CMD2_SET) {
122 if (set->cmd2 & CMD2_UBITS)
123 newmode |= (value<<6) & set->bits;
124 if (set->cmd2 & CMD2_GBITS)
125 newmode |= (value<<3) & set->bits;
126 if (set->cmd2 & CMD2_OBITS)
127 newmode |= value & set->bits;
128 }
129 break;
130
131 case '+':
132 newmode |= set->bits;
133 break;
134
135 case '-':
136 newmode &= ~set->bits;
137 break;
138
139 case 'X':
140 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
141 newmode |= set->bits;
142 break;
143
144 case '\0':
145 default:
146 #ifdef SETMODE_DEBUG
147 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
148 #endif
149 return (newmode);
150 }
151 }
152
153 #define ADDCMD(a, b, c, d) \
154 if (set >= endset) { \
155 register BITCMD *newset; \
156 setlen += SET_LEN_INCR; \
157 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
158 if (!saveset) \
159 return (NULL); \
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(p)
170 register char *p;
171 {
172 register int perm, who;
173 register char op;
174 BITCMD *set, *saveset, *endset;
175 sigset_t sigset, sigoset;
176 mode_t mask;
177 int equalopdone, permXbits, setlen;
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(*p)) {
206 perm = (mode_t)strtol(p, NULL, 8);
207 if (perm & ~(STANDARD_BITS|S_ISTXT)) {
208 free(saveset);
209 return (NULL);
210 }
211 while (*++p)
212 if (*p < '0' || *p > '7') {
213 free(saveset);
214 return (NULL);
215 }
216 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
217 return (saveset);
218 }
219
220 /*
221 * Build list of structures to set/clear/copy bits as described by
222 * each clause of the symbolic mode.
223 */
224 for (;;) {
225 /* First, find out which bits might be modified. */
226 for (who = 0;; ++p) {
227 switch (*p) {
228 case 'a':
229 who |= STANDARD_BITS;
230 break;
231 case 'u':
232 who |= S_ISUID|S_IRWXU;
233 break;
234 case 'g':
235 who |= S_ISGID|S_IRWXG;
236 break;
237 case 'o':
238 who |= S_IRWXO;
239 break;
240 default:
241 goto getop;
242 }
243 }
244
245 getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
246 free(saveset);
247 return (NULL);
248 }
249 if (op == '=')
250 equalopdone = 0;
251
252 who &= ~S_ISTXT;
253 for (perm = 0, permXbits = 0;; ++p) {
254 switch (*p) {
255 case 'r':
256 perm |= S_IRUSR|S_IRGRP|S_IROTH;
257 break;
258 case 's':
259 /* If only "other" bits ignore set-id. */
260 if (who & ~S_IRWXO)
261 perm |= S_ISUID|S_ISGID;
262 break;
263 case 't':
264 /* If only "other" bits ignore sticky. */
265 if (who & ~S_IRWXO) {
266 who |= S_ISTXT;
267 perm |= S_ISTXT;
268 }
269 break;
270 case 'w':
271 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
272 break;
273 case 'X':
274 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
275 break;
276 case 'x':
277 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
278 break;
279 case 'u':
280 case 'g':
281 case 'o':
282 /*
283 * When ever we hit 'u', 'g', or 'o', we have
284 * to flush out any partial mode that we have,
285 * and then do the copying of the mode bits.
286 */
287 if (perm) {
288 ADDCMD(op, who, perm, mask);
289 perm = 0;
290 }
291 if (op == '=')
292 equalopdone = 1;
293 if (op == '+' && permXbits) {
294 ADDCMD('X', who, permXbits, mask);
295 permXbits = 0;
296 }
297 ADDCMD(*p, who, op, mask);
298 break;
299
300 default:
301 /*
302 * Add any permissions that we haven't already
303 * done.
304 */
305 if (perm || (op == '=' && !equalopdone)) {
306 if (op == '=')
307 equalopdone = 1;
308 ADDCMD(op, who, perm, mask);
309 perm = 0;
310 }
311 if (permXbits) {
312 ADDCMD('X', who, permXbits, mask);
313 permXbits = 0;
314 }
315 goto apply;
316 }
317 }
318
319 apply: if (!*p)
320 break;
321 if (*p != ',')
322 goto getop;
323 ++p;
324 }
325 set->cmd = 0;
326 #ifdef SETMODE_DEBUG
327 (void)printf("Before compress_mode()\n");
328 dumpmode(saveset);
329 #endif
330 compress_mode(saveset);
331 #ifdef SETMODE_DEBUG
332 (void)printf("After compress_mode()\n");
333 dumpmode(saveset);
334 #endif
335 return (saveset);
336 }
337
338 static BITCMD *
339 addcmd(set, op, who, oparg, mask)
340 BITCMD *set;
341 register int oparg, who;
342 register int op;
343 u_int mask;
344 {
345 switch (op) {
346 case '=':
347 set->cmd = '-';
348 set->bits = who ? who : STANDARD_BITS;
349 set++;
350
351 op = '+';
352 /* FALLTHROUGH */
353 case '+':
354 case '-':
355 case 'X':
356 set->cmd = op;
357 set->bits = (who ? who : mask) & oparg;
358 break;
359
360 case 'u':
361 case 'g':
362 case 'o':
363 set->cmd = op;
364 if (who) {
365 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
366 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
367 ((who & S_IROTH) ? CMD2_OBITS : 0);
368 set->bits = ~0;
369 } else {
370 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
371 set->bits = mask;
372 }
373
374 if (oparg == '+')
375 set->cmd2 |= CMD2_SET;
376 else if (oparg == '-')
377 set->cmd2 |= CMD2_CLR;
378 else if (oparg == '=')
379 set->cmd2 |= CMD2_SET|CMD2_CLR;
380 break;
381 }
382 return (set + 1);
383 }
384
385 #ifdef SETMODE_DEBUG
386 static void
387 dumpmode(set)
388 register BITCMD *set;
389 {
390 for (; set->cmd; ++set)
391 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
392 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
393 set->cmd2 & CMD2_CLR ? " CLR" : "",
394 set->cmd2 & CMD2_SET ? " SET" : "",
395 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
396 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
397 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
398 }
399 #endif
400
401 /*
402 * Given an array of bitcmd structures, compress by compacting consecutive
403 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
404 * 'g' and 'o' commands continue to be separate. They could probably be
405 * compacted, but it's not worth the effort.
406 */
407 static int
408 compress_mode(set)
409 register BITCMD *set;
410 {
411 register BITCMD *nset;
412 register int setbits, clrbits, Xbits, op;
413
414 for (nset = set;;) {
415 /* Copy over any 'u', 'g' and 'o' commands. */
416 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
417 *set++ = *nset++;
418 if (!op)
419 return;
420 }
421
422 for (setbits = clrbits = Xbits = 0;; nset++) {
423 if ((op = nset->cmd) == '-') {
424 clrbits |= nset->bits;
425 setbits &= ~nset->bits;
426 Xbits &= ~nset->bits;
427 } else if (op == '+') {
428 setbits |= nset->bits;
429 clrbits &= ~nset->bits;
430 Xbits &= ~nset->bits;
431 } else if (op == 'X')
432 Xbits |= nset->bits & ~setbits;
433 else
434 break;
435 }
436 if (clrbits) {
437 set->cmd = '-';
438 set->cmd2 = 0;
439 set->bits = clrbits;
440 set++;
441 }
442 if (setbits) {
443 set->cmd = '+';
444 set->cmd2 = 0;
445 set->bits = setbits;
446 set++;
447 }
448 if (Xbits) {
449 set->cmd = 'X';
450 set->cmd2 = 0;
451 set->bits = Xbits;
452 set++;
453 }
454 }
455 }
456