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