setmode.c revision 1.8 1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #if defined(LIBC_SCCS) && !defined(lint)
35 /*static char *sccsid = "from: @(#)setmode.c 5.6 (Berkeley) 5/27/91";*/
36 static char *rcsid = "$Id: setmode.c,v 1.8 1994/02/11 05:55:45 cgd Exp $";
37 #endif /* LIBC_SCCS and not lint */
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <signal.h>
42 #include <errno.h>
43 #include <stddef.h>
44 #ifdef SETMODE_DEBUG
45 #include <stdio.h>
46 #endif
47 #include <stdlib.h>
48 #include <ctype.h>
49
50 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
51 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
52
53 typedef struct bitcmd {
54 char cmd;
55 char cmd2;
56 mode_t bits;
57 } BITCMD;
58
59 #define CMD2_CLR 0x01
60 #define CMD2_SET 0x02
61 #define CMD2_GBITS 0x04
62 #define CMD2_OBITS 0x08
63 #define CMD2_UBITS 0x10
64
65 static BITCMD *addcmd __P((BITCMD *, int, int, int, u_int));
66 static int compress_mode __P((BITCMD *));
67 #ifdef SETMODE_DEBUG
68 static void dumpmode __P((BITCMD *));
69 #endif
70
71 /*
72 * Given the old mode and an array of bitcmd structures, apply the operations
73 * described in the bitcmd structures to the old mode, and return the new mode.
74 * Note that there is no '=' command; a strict assignment is just a '-' (clear
75 * bits) followed by a '+' (set bits).
76 */
77 mode_t
78 getmode(bbox, omode)
79 void *bbox;
80 mode_t omode;
81 {
82 register BITCMD *set;
83 register mode_t newmode, value;
84
85 set = (BITCMD *)bbox;
86 newmode = omode;
87 for (value = 0;; set++)
88 switch(set->cmd) {
89 /*
90 * When copying the user, group or other bits around, we "know"
91 * where the bits are in the mode so that we can do shifts to
92 * copy them around. If we don't use shifts, it gets real
93 * grundgy with lots of single bit checks and bit sets.
94 */
95 case 'u':
96 value = (newmode & S_IRWXU) >> 6;
97 goto common;
98
99 case 'g':
100 value = (newmode & S_IRWXG) >> 3;
101 goto common;
102
103 case 'o':
104 value = newmode & S_IRWXO;
105 common: if (set->cmd2 & CMD2_CLR) {
106 mode_t clrval;
107
108 clrval =
109 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
110
111 if (set->cmd2 & CMD2_UBITS)
112 newmode &= ~((clrval<<6) & set->bits);
113 if (set->cmd2 & CMD2_GBITS)
114 newmode &= ~((clrval<<3) & set->bits);
115 if (set->cmd2 & CMD2_OBITS)
116 newmode &= ~(clrval & set->bits);
117 }
118 if (set->cmd2 & CMD2_SET) {
119 if (set->cmd2 & CMD2_UBITS)
120 newmode |= (value<<6) & set->bits;
121 if (set->cmd2 & CMD2_GBITS)
122 newmode |= (value<<3) & set->bits;
123 if (set->cmd2 & CMD2_OBITS)
124 newmode |= value & set->bits;
125 }
126 break;
127
128 case '+':
129 newmode |= set->bits;
130 break;
131
132 case '-':
133 newmode &= ~set->bits;
134 break;
135
136 case 'X':
137 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
138 newmode |= set->bits;
139 break;
140
141 case '\0':
142 default:
143 #ifdef SETMODE_DEBUG
144 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
145 #endif
146 return (newmode);
147 }
148 }
149
150 #define ADDCMD(a, b, c, d) \
151 if (set >= endset) { \
152 register BITCMD *newset; \
153 setlen += SET_LEN_INCR; \
154 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
155 if (!saveset) \
156 return (NULL); \
157 set = newset + (set - saveset); \
158 saveset = newset; \
159 endset = newset + (setlen - 2); \
160 } \
161 set = addcmd(set, (a), (b), (c), (d))
162
163 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
164
165 void *
166 setmode(p)
167 register char *p;
168 {
169 register int perm, who;
170 register char op;
171 BITCMD *set, *saveset, *endset;
172 sigset_t sigset, sigoset;
173 mode_t mask;
174 int equalopdone, permXbits, setlen;
175
176 if (!*p)
177 return (NULL);
178
179 /*
180 * Get a copy of the mask for the permissions that are mask relative.
181 * Flip the bits, we want what's not set. Since it's possible that
182 * the caller is opening files inside a signal handler, protect them
183 * as best we can.
184 */
185 sigfillset(&sigset);
186 (void)sigprocmask(SIG_BLOCK, &sigset, &sigoset);
187 (void)umask(mask = umask(0));
188 mask = ~mask;
189 (void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
190
191 setlen = SET_LEN + 2;
192
193 if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
194 return (NULL);
195 saveset = set;
196 endset = set + (setlen - 2);
197
198 /*
199 * If an absolute number, get it and return; disallow non-octal digits
200 * or illegal bits.
201 */
202 if (isdigit(*p)) {
203 perm = (mode_t)strtol(p, (char **)0, 8);
204 if (perm & ~(STANDARD_BITS|S_ISTXT)) {
205 free(saveset);
206 return (NULL);
207 }
208 while (*++p)
209 if (*p < '0' || *p > '7') {
210 free(saveset);
211 return (NULL);
212 }
213 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
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 & ~S_IRWXO)
258 perm |= S_ISUID|S_ISGID;
259 break;
260 case 't':
261 /* If only "other" bits ignore sticky. */
262 if (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(set, op, who, oparg, mask)
337 BITCMD *set;
338 register int oparg, who;
339 register int op;
340 u_int mask;
341 {
342 switch (op) {
343 case '+':
344 case 'X':
345 set->cmd = op;
346 set->bits = (who ? who : mask) & oparg;
347 break;
348
349 case '-':
350 set->cmd = '-';
351 set->bits = (who ? who : (S_IRWXU|S_IRWXG|S_IRWXO)) & oparg;
352 break;
353
354 case '=':
355 set->cmd = '-';
356 if (!who) {
357 set->bits = STANDARD_BITS;
358 who = mask;
359 } else
360 set->bits = who;
361 set++;
362
363 set->cmd = '+';
364 set->bits = who & oparg;
365 break;
366 case 'u':
367 case 'g':
368 case 'o':
369 set->cmd = op;
370 if (who) {
371 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
372 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
373 ((who & S_IROTH) ? CMD2_OBITS : 0);
374 set->bits = ~0;
375 } else {
376 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
377 if (oparg == '-')
378 set->bits = (S_IRWXU|S_IRWXG|S_IRWXO);
379 else
380 set->bits = mask;
381 }
382
383 if (oparg == '+')
384 set->cmd2 |= CMD2_SET;
385 else if (oparg == '-')
386 set->cmd2 |= CMD2_CLR;
387 else if (oparg == '=')
388 set->cmd2 |= CMD2_SET|CMD2_CLR;
389 break;
390 }
391 return (set + 1);
392 }
393
394 #ifdef SETMODE_DEBUG
395 static void
396 dumpmode(set)
397 register BITCMD *set;
398 {
399 for (; set->cmd; ++set)
400 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
401 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
402 set->cmd2 & CMD2_CLR ? " CLR" : "",
403 set->cmd2 & CMD2_SET ? " SET" : "",
404 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
405 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
406 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
407 }
408 #endif
409
410 /*
411 * Given an array of bitcmd structures, compress by compacting consecutive
412 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
413 * 'g' and 'o' commands continue to be separate. They could probably be
414 * compacted, but it's not worth the effort.
415 */
416 static int
417 compress_mode(set)
418 register BITCMD *set;
419 {
420 register BITCMD *nset;
421 register int setbits, clrbits, Xbits, op;
422
423 for (nset = set;;) {
424 /* Copy over any 'u', 'g' and 'o' commands. */
425 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
426 *set++ = *nset++;
427 if (!op)
428 return;
429 }
430
431 for (setbits = clrbits = Xbits = 0;; nset++) {
432 if ((op = nset->cmd) == '-') {
433 clrbits |= nset->bits;
434 setbits &= ~nset->bits;
435 Xbits &= ~nset->bits;
436 } else if (op == '+') {
437 setbits |= nset->bits;
438 clrbits &= ~nset->bits;
439 Xbits &= ~nset->bits;
440 } else if (op == 'X')
441 Xbits |= nset->bits & ~setbits;
442 else
443 break;
444 }
445 if (clrbits) {
446 set->cmd = '-';
447 set->cmd2 = 0;
448 set->bits = clrbits;
449 set++;
450 }
451 if (setbits) {
452 set->cmd = '+';
453 set->cmd2 = 0;
454 set->bits = setbits;
455 set++;
456 }
457 if (Xbits) {
458 set->cmd = 'X';
459 set->cmd2 = 0;
460 set->bits = Xbits;
461 set++;
462 }
463 }
464 }
465