2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Dave Borman at Cray Research, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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$");
43 #include "namespace.h"
44 #include <sys/types.h>
56 #include "un-namespace.h"
58 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
59 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
61 typedef struct bitcmd {
69 #define CMD2_GBITS 0x04
70 #define CMD2_OBITS 0x08
71 #define CMD2_UBITS 0x10
73 static BITCMD *addcmd(BITCMD *, int, int, int, u_int);
74 static void compress_mode(BITCMD *);
76 static void dumpmode(BITCMD *);
80 * Given the old mode and an array of bitcmd structures, apply the operations
81 * described in the bitcmd structures to the old mode, and return the new mode.
82 * Note that there is no '=' command; a strict assignment is just a '-' (clear
83 * bits) followed by a '+' (set bits).
86 getmode(const void *bbox, mode_t omode)
89 mode_t clrval, newmode, value;
91 set = (const BITCMD *)bbox;
93 for (value = 0;; set++)
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.
102 value = (newmode & S_IRWXU) >> 6;
106 value = (newmode & S_IRWXG) >> 3;
110 value = newmode & S_IRWXO;
111 common: if (set->cmd2 & CMD2_CLR) {
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);
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;
132 newmode |= set->bits;
136 newmode &= ~set->bits;
140 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
141 newmode |= set->bits;
147 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
153 #define ADDCMD(a, b, c, d) \
154 if (set >= endset) { \
156 setlen += SET_LEN_INCR; \
157 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
164 set = newset + (set - saveset); \
166 endset = newset + (setlen - 2); \
168 set = addcmd(set, (a), (b), (c), (d))
170 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
173 setmode(const char *p)
177 BITCMD *set, *saveset, *endset;
178 sigset_t sigset, sigoset;
180 int equalopdone=0, permXbits, setlen;
187 * Get a copy of the mask for the permissions that are mask relative.
188 * Flip the bits, we want what's not set. Since it's possible that
189 * the caller is opening files inside a signal handler, protect them
193 (void)_sigprocmask(SIG_BLOCK, &sigset, &sigoset);
194 (void)umask(mask = umask(0));
196 (void)_sigprocmask(SIG_SETMASK, &sigoset, NULL);
198 setlen = SET_LEN + 2;
200 if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
203 endset = set + (setlen - 2);
206 * If an absolute number, get it and return; disallow non-octal digits
209 if (isdigit((unsigned char)*p)) {
210 perml = strtol(p, &ep, 8);
211 if (*ep || perml < 0 || perml & ~(STANDARD_BITS|S_ISTXT)) {
215 perm = (mode_t)perml;
216 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
222 * Build list of structures to set/clear/copy bits as described by
223 * each clause of the symbolic mode.
226 /* First, find out which bits might be modified. */
227 for (who = 0;; ++p) {
230 who |= STANDARD_BITS;
233 who |= S_ISUID|S_IRWXU;
236 who |= S_ISGID|S_IRWXG;
246 getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
254 for (perm = 0, permXbits = 0;; ++p) {
257 perm |= S_IRUSR|S_IRGRP|S_IROTH;
260 /* If only "other" bits ignore set-id. */
261 if (!who || who & ~S_IRWXO)
262 perm |= S_ISUID|S_ISGID;
265 /* If only "other" bits ignore sticky. */
266 if (!who || who & ~S_IRWXO) {
272 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
275 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
278 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
284 * When ever we hit 'u', 'g', or 'o', we have
285 * to flush out any partial mode that we have,
286 * and then do the copying of the mode bits.
289 ADDCMD(op, who, perm, mask);
294 if (op == '+' && permXbits) {
295 ADDCMD('X', who, permXbits, mask);
298 ADDCMD(*p, who, op, mask);
303 * Add any permissions that we haven't already
306 if (perm || (op == '=' && !equalopdone)) {
309 ADDCMD(op, who, perm, mask);
313 ADDCMD('X', who, permXbits, mask);
328 (void)printf("Before compress_mode()\n");
331 compress_mode(saveset);
333 (void)printf("After compress_mode()\n");
340 addcmd(BITCMD *set, int op, int who, int oparg, u_int mask)
345 set->bits = who ? who : STANDARD_BITS;
354 set->bits = (who ? who : mask) & oparg;
362 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
363 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
364 ((who & S_IROTH) ? CMD2_OBITS : 0);
365 set->bits = (mode_t)~0;
367 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
372 set->cmd2 |= CMD2_SET;
373 else if (oparg == '-')
374 set->cmd2 |= CMD2_CLR;
375 else if (oparg == '=')
376 set->cmd2 |= CMD2_SET|CMD2_CLR;
384 dumpmode(BITCMD *set)
386 for (; set->cmd; ++set)
387 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
388 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
389 set->cmd2 & CMD2_CLR ? " CLR" : "",
390 set->cmd2 & CMD2_SET ? " SET" : "",
391 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
392 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
393 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
398 * Given an array of bitcmd structures, compress by compacting consecutive
399 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
400 * 'g' and 'o' commands continue to be separate. They could probably be
401 * compacted, but it's not worth the effort.
404 compress_mode(BITCMD *set)
407 int setbits, clrbits, Xbits, op;
410 /* Copy over any 'u', 'g' and 'o' commands. */
411 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
417 for (setbits = clrbits = Xbits = 0;; nset++) {
418 if ((op = nset->cmd) == '-') {
419 clrbits |= nset->bits;
420 setbits &= ~nset->bits;
421 Xbits &= ~nset->bits;
422 } else if (op == '+') {
423 setbits |= nset->bits;
424 clrbits &= ~nset->bits;
425 Xbits &= ~nset->bits;
426 } else if (op == 'X')
427 Xbits |= nset->bits & ~setbits;