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
36 * @(#)setmode.c 8.2 (Berkeley) 3/25/94
37 * $FreeBSD: src/lib/libc/gen/setmode.c,v 1.5.2.1 2001/03/05 09:34:10 obrien Exp $
38 * $DragonFly: src/lib/libc/gen/setmode.c,v 1.7 2005/11/19 22:32:53 swildner Exp $
41 #include "namespace.h"
42 #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
*, int, int, int, u_int
);
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
;
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 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); \
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
;
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
186 _sigprocmask(SIG_BLOCK
, &sigset
, &sigoset
);
187 umask(mask
= umask(0));
189 _sigprocmask(SIG_SETMASK
, &sigoset
, NULL
);
191 setlen
= SET_LEN
+ 2;
193 if ((set
= malloc((u_int
)(sizeof(BITCMD
) * setlen
))) == NULL
)
196 endset
= set
+ (setlen
- 2);
199 * If an absolute number, get it and return; disallow non-octal digits
202 if (isdigit((unsigned char)*p
)) {
203 perm
= (mode_t
)strtol(p
, NULL
, 8);
204 if (perm
& ~(STANDARD_BITS
|S_ISTXT
)) {
209 if (*p
< '0' || *p
> '7') {
213 ADDCMD('=', (STANDARD_BITS
|S_ISTXT
), perm
, mask
);
218 * Build list of structures to set/clear/copy bits as described by
219 * each clause of the symbolic mode.
222 /* First, find out which bits might be modified. */
223 for (who
= 0;; ++p
) {
226 who
|= STANDARD_BITS
;
229 who
|= S_ISUID
|S_IRWXU
;
232 who
|= S_ISGID
|S_IRWXG
;
242 getop
: if ((op
= *p
++) != '+' && op
!= '-' && op
!= '=') {
250 for (perm
= 0, permXbits
= 0;; ++p
) {
253 perm
|= S_IRUSR
|S_IRGRP
|S_IROTH
;
256 /* If only "other" bits ignore set-id. */
257 if (!who
|| who
& ~S_IRWXO
)
258 perm
|= S_ISUID
|S_ISGID
;
261 /* If only "other" bits ignore sticky. */
262 if (!who
|| who
& ~S_IRWXO
) {
268 perm
|= S_IWUSR
|S_IWGRP
|S_IWOTH
;
271 permXbits
= S_IXUSR
|S_IXGRP
|S_IXOTH
;
274 perm
|= S_IXUSR
|S_IXGRP
|S_IXOTH
;
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.
285 ADDCMD(op
, who
, perm
, mask
);
290 if (op
== '+' && permXbits
) {
291 ADDCMD('X', who
, permXbits
, mask
);
294 ADDCMD(*p
, who
, op
, mask
);
299 * Add any permissions that we haven't already
302 if (perm
|| (op
== '=' && !equalopdone
)) {
305 ADDCMD(op
, who
, perm
, mask
);
309 ADDCMD('X', who
, permXbits
, mask
);
324 printf("Before compress_mode()\n");
327 compress_mode(saveset
);
329 printf("After compress_mode()\n");
336 addcmd(BITCMD
*set
, int op
, int who
, int oparg
, u_int mask
)
341 set
->bits
= who
? who
: STANDARD_BITS
;
350 set
->bits
= (who
? who
: mask
) & oparg
;
358 set
->cmd2
= ((who
& S_IRUSR
) ? CMD2_UBITS
: 0) |
359 ((who
& S_IRGRP
) ? CMD2_GBITS
: 0) |
360 ((who
& S_IROTH
) ? CMD2_OBITS
: 0);
363 set
->cmd2
= CMD2_UBITS
| CMD2_GBITS
| CMD2_OBITS
;
368 set
->cmd2
|= CMD2_SET
;
369 else if (oparg
== '-')
370 set
->cmd2
|= CMD2_CLR
;
371 else if (oparg
== '=')
372 set
->cmd2
|= CMD2_SET
|CMD2_CLR
;
380 dumpmode(BITCMD
*set
)
382 for (; set
->cmd
; ++set
)
383 printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
384 set
->cmd
, set
->bits
, set
->cmd2
? " cmd2:" : "",
385 set
->cmd2
& CMD2_CLR
? " CLR" : "",
386 set
->cmd2
& CMD2_SET
? " SET" : "",
387 set
->cmd2
& CMD2_UBITS
? " UBITS" : "",
388 set
->cmd2
& CMD2_GBITS
? " GBITS" : "",
389 set
->cmd2
& CMD2_OBITS
? " OBITS" : "");
394 * Given an array of bitcmd structures, compress by compacting consecutive
395 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
396 * 'g' and 'o' commands continue to be separate. They could probably be
397 * compacted, but it's not worth the effort.
400 compress_mode(BITCMD
*set
)
403 int setbits
, clrbits
, Xbits
, op
;
406 /* Copy over any 'u', 'g' and 'o' commands. */
407 while ((op
= nset
->cmd
) != '+' && op
!= '-' && op
!= 'X') {
413 for (setbits
= clrbits
= Xbits
= 0;; nset
++) {
414 if ((op
= nset
->cmd
) == '-') {
415 clrbits
|= nset
->bits
;
416 setbits
&= ~nset
->bits
;
417 Xbits
&= ~nset
->bits
;
418 } else if (op
== '+') {
419 setbits
|= nset
->bits
;
420 clrbits
&= ~nset
->bits
;
421 Xbits
&= ~nset
->bits
;
422 } else if (op
== 'X')
423 Xbits
|= nset
->bits
& ~setbits
;