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 * 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.
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
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$");
39 #include "namespace.h"
40 #include <sys/types.h>
54 #include "un-namespace.h"
56 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
57 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
59 typedef struct bitcmd {
67 #define CMD2_GBITS 0x04
68 #define CMD2_OBITS 0x08
69 #define CMD2_UBITS 0x10
71 static BITCMD *addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t);
72 static void compress_mode(BITCMD *);
74 static void dumpmode(BITCMD *);
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).
84 getmode(const void *bbox, mode_t omode)
87 mode_t clrval, newmode, value;
89 set = (const BITCMD *)bbox;
91 for (value = 0;; set++)
94 * When copying the user, group or other bits around, we "know"
95 * where the bits are in the mode so that we can do shifts to
96 * copy them around. If we don't use shifts, it gets real
97 * grundgy with lots of single bit checks and bit sets.
100 value = (newmode & S_IRWXU) >> 6;
104 value = (newmode & S_IRWXG) >> 3;
108 value = newmode & S_IRWXO;
109 common: if (set->cmd2 & CMD2_CLR) {
111 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
112 if (set->cmd2 & CMD2_UBITS)
113 newmode &= ~((clrval<<6) & set->bits);
114 if (set->cmd2 & CMD2_GBITS)
115 newmode &= ~((clrval<<3) & set->bits);
116 if (set->cmd2 & CMD2_OBITS)
117 newmode &= ~(clrval & set->bits);
119 if (set->cmd2 & CMD2_SET) {
120 if (set->cmd2 & CMD2_UBITS)
121 newmode |= (value<<6) & set->bits;
122 if (set->cmd2 & CMD2_GBITS)
123 newmode |= (value<<3) & set->bits;
124 if (set->cmd2 & CMD2_OBITS)
125 newmode |= value & set->bits;
130 newmode |= set->bits;
134 newmode &= ~set->bits;
138 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
139 newmode |= set->bits;
145 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
151 #define ADDCMD(a, b, c, d) \
152 if (set >= endset) { \
154 setlen += SET_LEN_INCR; \
155 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
156 if (newset == NULL) \
158 set = newset + (set - saveset); \
160 endset = newset + (setlen - 2); \
162 set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d))
164 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
167 setmode(const char *p)
171 BITCMD *set, *saveset, *endset;
172 sigset_t sigset, sigoset;
173 mode_t mask, perm, permXbits, who;
184 * Get a copy of the mask for the permissions that are mask relative.
185 * Flip the bits, we want what's not set. Since it's possible that
186 * the caller is opening files inside a signal handler, protect them
190 (void)_sigprocmask(SIG_BLOCK, &sigset, &sigoset);
191 (void)umask(mask = umask(0));
193 (void)_sigprocmask(SIG_SETMASK, &sigoset, NULL);
195 setlen = SET_LEN + 2;
197 if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
200 endset = set + (setlen - 2);
203 * If an absolute number, get it and return; disallow non-octal digits
206 if (isdigit((unsigned char)*p)) {
208 perml = strtol(p, &ep, 8);
213 if (errno == ERANGE && (perml == LONG_MAX || perml == LONG_MIN))
215 if (perml & ~(STANDARD_BITS|S_ISTXT)) {
219 perm = (mode_t)perml;
220 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
226 * Build list of structures to set/clear/copy bits as described by
227 * each clause of the symbolic mode.
231 /* First, find out which bits might be modified. */
232 for (who = 0;; ++p) {
235 who |= STANDARD_BITS;
238 who |= S_ISUID|S_IRWXU;
241 who |= S_ISGID|S_IRWXG;
251 getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
259 for (perm = 0, permXbits = 0;; ++p) {
262 perm |= S_IRUSR|S_IRGRP|S_IROTH;
265 /* If only "other" bits ignore set-id. */
266 if (!who || who & ~S_IRWXO)
267 perm |= S_ISUID|S_ISGID;
270 /* If only "other" bits ignore sticky. */
271 if (!who || who & ~S_IRWXO) {
277 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
280 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
283 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
289 * When ever we hit 'u', 'g', or 'o', we have
290 * to flush out any partial mode that we have,
291 * and then do the copying of the mode bits.
294 ADDCMD(op, who, perm, mask);
299 if (op == '+' && permXbits) {
300 ADDCMD('X', who, permXbits, mask);
303 ADDCMD(*p, who, op, mask);
308 * Add any permissions that we haven't already
311 if (perm || (op == '=' && !equalopdone)) {
314 ADDCMD(op, who, perm, mask);
318 ADDCMD('X', who, permXbits, mask);
333 (void)printf("Before compress_mode()\n");
336 compress_mode(saveset);
338 (void)printf("After compress_mode()\n");
350 addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask)
355 set->bits = who ? who : STANDARD_BITS;
364 set->bits = (who ? who : mask) & oparg;
372 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
373 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
374 ((who & S_IROTH) ? CMD2_OBITS : 0);
375 set->bits = (mode_t)~0;
377 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
382 set->cmd2 |= CMD2_SET;
383 else if (oparg == '-')
384 set->cmd2 |= CMD2_CLR;
385 else if (oparg == '=')
386 set->cmd2 |= CMD2_SET|CMD2_CLR;
394 dumpmode(BITCMD *set)
396 for (; set->cmd; ++set)
397 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
398 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
399 set->cmd2 & CMD2_CLR ? " CLR" : "",
400 set->cmd2 & CMD2_SET ? " SET" : "",
401 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
402 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
403 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
408 * Given an array of bitcmd structures, compress by compacting consecutive
409 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
410 * 'g' and 'o' commands continue to be separate. They could probably be
411 * compacted, but it's not worth the effort.
414 compress_mode(BITCMD *set)
417 int setbits, clrbits, Xbits, op;
420 /* Copy over any 'u', 'g' and 'o' commands. */
421 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
427 for (setbits = clrbits = Xbits = 0;; nset++) {
428 if ((op = nset->cmd) == '-') {
429 clrbits |= nset->bits;
430 setbits &= ~nset->bits;
431 Xbits &= ~nset->bits;
432 } else if (op == '+') {
433 setbits |= nset->bits;
434 clrbits &= ~nset->bits;
435 Xbits &= ~nset->bits;
436 } else if (op == 'X')
437 Xbits |= nset->bits & ~setbits;