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
32 * @(#)setmode.c 8.2 (Berkeley) 3/25/94
33 * $FreeBSD: src/lib/libc/gen/setmode.c,v 1.11 2007/01/09 00:27:55 imp Exp $
34 * $DragonFly: src/lib/libc/gen/setmode.c,v 1.7 2005/11/19 22:32:53 swildner Exp $
37 #include "namespace.h"
38 #include <sys/types.h>
50 #include "un-namespace.h"
52 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
53 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
55 typedef struct bitcmd {
63 #define CMD2_GBITS 0x04
64 #define CMD2_OBITS 0x08
65 #define CMD2_UBITS 0x10
67 static BITCMD *addcmd(BITCMD *, int, int, int, u_int);
68 static void compress_mode(BITCMD *);
70 static void dumpmode(BITCMD *);
74 * Given the old mode and an array of bitcmd structures, apply the operations
75 * described in the bitcmd structures to the old mode, and return the new mode.
76 * Note that there is no '=' command; a strict assignment is just a '-' (clear
77 * bits) followed by a '+' (set bits).
80 getmode(const void *bbox, mode_t omode)
83 mode_t clrval, newmode, value;
85 set = (const BITCMD *)bbox;
87 for (value = 0;; set++)
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.
96 value = (newmode & S_IRWXU) >> 6;
100 value = (newmode & S_IRWXG) >> 3;
104 value = newmode & S_IRWXO;
105 common: if (set->cmd2 & CMD2_CLR) {
107 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
108 if (set->cmd2 & CMD2_UBITS)
109 newmode &= ~((clrval<<6) & set->bits);
110 if (set->cmd2 & CMD2_GBITS)
111 newmode &= ~((clrval<<3) & set->bits);
112 if (set->cmd2 & CMD2_OBITS)
113 newmode &= ~(clrval & set->bits);
115 if (set->cmd2 & CMD2_SET) {
116 if (set->cmd2 & CMD2_UBITS)
117 newmode |= (value<<6) & set->bits;
118 if (set->cmd2 & CMD2_GBITS)
119 newmode |= (value<<3) & set->bits;
120 if (set->cmd2 & CMD2_OBITS)
121 newmode |= value & set->bits;
126 newmode |= set->bits;
130 newmode &= ~set->bits;
134 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
135 newmode |= set->bits;
141 printf("getmode:%04o -> %04o\n", omode, newmode);
147 #define ADDCMD(a, b, c, d) \
148 if (set >= endset) { \
150 setlen += SET_LEN_INCR; \
151 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
158 set = newset + (set - saveset); \
160 endset = newset + (setlen - 2); \
162 set = addcmd(set, (a), (b), (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;
174 int equalopdone=0, permXbits, setlen;
181 * Get a copy of the mask for the permissions that are mask relative.
182 * Flip the bits, we want what's not set. Since it's possible that
183 * the caller is opening files inside a signal handler, protect them
187 _sigprocmask(SIG_BLOCK, &sigset, &sigoset);
188 umask(mask = umask(0));
190 _sigprocmask(SIG_SETMASK, &sigoset, NULL);
192 setlen = SET_LEN + 2;
194 if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
197 endset = set + (setlen - 2);
200 * If an absolute number, get it and return; disallow non-octal digits
203 if (isdigit((unsigned char)*p)) {
204 perml = strtol(p, &ep, 8);
205 if (*ep || perml < 0 || perml & ~(STANDARD_BITS|S_ISTXT)) {
209 perm = (mode_t)perml;
210 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
216 * Build list of structures to set/clear/copy bits as described by
217 * each clause of the symbolic mode.
220 /* First, find out which bits might be modified. */
221 for (who = 0;; ++p) {
224 who |= STANDARD_BITS;
227 who |= S_ISUID|S_IRWXU;
230 who |= S_ISGID|S_IRWXG;
240 getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
248 for (perm = 0, permXbits = 0;; ++p) {
251 perm |= S_IRUSR|S_IRGRP|S_IROTH;
254 /* If only "other" bits ignore set-id. */
255 if (!who || who & ~S_IRWXO)
256 perm |= S_ISUID|S_ISGID;
259 /* If only "other" bits ignore sticky. */
260 if (!who || who & ~S_IRWXO) {
266 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
269 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
272 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
278 * When ever we hit 'u', 'g', or 'o', we have
279 * to flush out any partial mode that we have,
280 * and then do the copying of the mode bits.
283 ADDCMD(op, who, perm, mask);
288 if (op == '+' && permXbits) {
289 ADDCMD('X', who, permXbits, mask);
292 ADDCMD(*p, who, op, mask);
297 * Add any permissions that we haven't already
300 if (perm || (op == '=' && !equalopdone)) {
303 ADDCMD(op, who, perm, mask);
307 ADDCMD('X', who, permXbits, mask);
322 printf("Before compress_mode()\n");
325 compress_mode(saveset);
327 printf("After compress_mode()\n");
334 addcmd(BITCMD *set, int op, int who, int oparg, u_int mask)
339 set->bits = who ? who : STANDARD_BITS;
348 set->bits = (who ? who : mask) & oparg;
356 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
357 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
358 ((who & S_IROTH) ? CMD2_OBITS : 0);
359 set->bits = (mode_t)~0;
361 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
366 set->cmd2 |= CMD2_SET;
367 else if (oparg == '-')
368 set->cmd2 |= CMD2_CLR;
369 else if (oparg == '=')
370 set->cmd2 |= CMD2_SET|CMD2_CLR;
378 dumpmode(BITCMD *set)
380 for (; set->cmd; ++set)
381 printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
382 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
383 set->cmd2 & CMD2_CLR ? " CLR" : "",
384 set->cmd2 & CMD2_SET ? " SET" : "",
385 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
386 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
387 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
392 * Given an array of bitcmd structures, compress by compacting consecutive
393 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
394 * 'g' and 'o' commands continue to be separate. They could probably be
395 * compacted, but it's not worth the effort.
398 compress_mode(BITCMD *set)
401 int setbits, clrbits, Xbits, op;
404 /* Copy over any 'u', 'g' and 'o' commands. */
405 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
411 for (setbits = clrbits = Xbits = 0;; nset++) {
412 if ((op = nset->cmd) == '-') {
413 clrbits |= nset->bits;
414 setbits &= ~nset->bits;
415 Xbits &= ~nset->bits;
416 } else if (op == '+') {
417 setbits |= nset->bits;
418 clrbits &= ~nset->bits;
419 Xbits &= ~nset->bits;
420 } else if (op == 'X')
421 Xbits |= nset->bits & ~setbits;