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 */
41 #include <sys/types.h>
57 #define S_ISTXT S_ISVTX
60 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
61 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
63 typedef struct bitcmd
{
71 #define CMD2_GBITS 0x04
72 #define CMD2_OBITS 0x08
73 #define CMD2_UBITS 0x10
75 static BITCMD
*addcmd (BITCMD
*, int, int, int, unsigned int);
76 static void compress_mode (BITCMD
*);
78 static void dumpmode
__P((BITCMD
*));
82 * Given the old mode and an array of bitcmd structures, apply the operations
83 * described in the bitcmd structures to the old mode, and return the new mode.
84 * Note that there is no '=' command; a strict assignment is just a '-' (clear
85 * bits) followed by a '+' (set bits).
93 register mode_t clrval
, newmode
, value
;
97 for (value
= 0;; set
++)
100 * When copying the user, group or other bits around, we "know"
101 * where the bits are in the mode so that we can do shifts to
102 * copy them around. If we don't use shifts, it gets real
103 * grundgy with lots of single bit checks and bit sets.
106 value
= (newmode
& S_IRWXU
) >> 6;
110 value
= (newmode
& S_IRWXG
) >> 3;
114 value
= newmode
& S_IRWXO
;
115 common
: if (set
->cmd2
& CMD2_CLR
) {
117 (set
->cmd2
& CMD2_SET
) ? S_IRWXO
: value
;
118 if (set
->cmd2
& CMD2_UBITS
)
119 newmode
&= ~((clrval
<<6) & set
->bits
);
120 if (set
->cmd2
& CMD2_GBITS
)
121 newmode
&= ~((clrval
<<3) & set
->bits
);
122 if (set
->cmd2
& CMD2_OBITS
)
123 newmode
&= ~(clrval
& set
->bits
);
125 if (set
->cmd2
& CMD2_SET
) {
126 if (set
->cmd2
& CMD2_UBITS
)
127 newmode
|= (value
<<6) & set
->bits
;
128 if (set
->cmd2
& CMD2_GBITS
)
129 newmode
|= (value
<<3) & set
->bits
;
130 if (set
->cmd2
& CMD2_OBITS
)
131 newmode
|= value
& set
->bits
;
136 newmode
|= set
->bits
;
140 newmode
&= ~set
->bits
;
144 if (omode
& (S_IFDIR
|S_IXUSR
|S_IXGRP
|S_IXOTH
))
145 newmode
|= set
->bits
;
151 (void)printf("getmode:%04o -> %04o\n", omode
, newmode
);
157 #define ADDCMD(a, b, c, d) \
158 if (set >= endset) { \
159 register BITCMD *newset; \
160 setlen += SET_LEN_INCR; \
161 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)
176 register int perm
, who
;
178 BITCMD
*set
, *saveset
, *endset
;
179 sigset_t sigset
, sigoset
;
181 int equalopdone
, 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((unsigned int)(sizeof(BITCMD
) * setlen
))) == NULL
)
203 endset
= set
+ (setlen
- 2);
206 * If an absolute number, get it and return; disallow non-octal digits
210 perm
= (mode_t
)strtol(p
, NULL
, 8);
211 if (perm
& ~(STANDARD_BITS
|S_ISTXT
)) {
216 if (*p
< '0' || *p
> '7') {
220 ADDCMD('=', (STANDARD_BITS
|S_ISTXT
), perm
, mask
);
225 * Build list of structures to set/clear/copy bits as described by
226 * each clause of the symbolic mode.
229 /* First, find out which bits might be modified. */
230 for (who
= 0;; ++p
) {
233 who
|= STANDARD_BITS
;
236 who
|= S_ISUID
|S_IRWXU
;
239 who
|= S_ISGID
|S_IRWXG
;
249 getop
: if ((op
= *p
++) != '+' && op
!= '-' && op
!= '=') {
257 for (perm
= 0, permXbits
= 0;; ++p
) {
260 perm
|= S_IRUSR
|S_IRGRP
|S_IROTH
;
263 /* If only "other" bits ignore set-id. */
265 perm
|= S_ISUID
|S_ISGID
;
268 /* If only "other" bits ignore sticky. */
269 if (who
& ~S_IRWXO
) {
275 perm
|= S_IWUSR
|S_IWGRP
|S_IWOTH
;
278 permXbits
= S_IXUSR
|S_IXGRP
|S_IXOTH
;
281 perm
|= S_IXUSR
|S_IXGRP
|S_IXOTH
;
287 * When ever we hit 'u', 'g', or 'o', we have
288 * to flush out any partial mode that we have,
289 * and then do the copying of the mode bits.
292 ADDCMD(op
, who
, perm
, mask
);
297 if (op
== '+' && permXbits
) {
298 ADDCMD('X', who
, permXbits
, mask
);
301 ADDCMD(*p
, who
, op
, mask
);
306 * Add any permissions that we haven't already
309 if (perm
|| (op
== '=' && !equalopdone
)) {
312 ADDCMD(op
, who
, perm
, mask
);
316 ADDCMD('X', who
, permXbits
, mask
);
331 (void)printf("Before compress_mode()\n");
334 compress_mode(saveset
);
336 (void)printf("After compress_mode()\n");
343 addcmd(set
, op
, who
, oparg
, mask
)
345 register int oparg
, who
;
352 set
->bits
= who
? who
: STANDARD_BITS
;
361 set
->bits
= (who
? who
: mask
) & oparg
;
369 set
->cmd2
= ((who
& S_IRUSR
) ? CMD2_UBITS
: 0) |
370 ((who
& S_IRGRP
) ? CMD2_GBITS
: 0) |
371 ((who
& S_IROTH
) ? CMD2_OBITS
: 0);
374 set
->cmd2
= CMD2_UBITS
| CMD2_GBITS
| CMD2_OBITS
;
379 set
->cmd2
|= CMD2_SET
;
380 else if (oparg
== '-')
381 set
->cmd2
|= CMD2_CLR
;
382 else if (oparg
== '=')
383 set
->cmd2
|= CMD2_SET
|CMD2_CLR
;
392 register BITCMD
*set
;
394 for (; set
->cmd
; ++set
)
395 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
396 set
->cmd
, set
->bits
, set
->cmd2
? " cmd2:" : "",
397 set
->cmd2
& CMD2_CLR
? " CLR" : "",
398 set
->cmd2
& CMD2_SET
? " SET" : "",
399 set
->cmd2
& CMD2_UBITS
? " UBITS" : "",
400 set
->cmd2
& CMD2_GBITS
? " GBITS" : "",
401 set
->cmd2
& CMD2_OBITS
? " OBITS" : "");
406 * Given an array of bitcmd structures, compress by compacting consecutive
407 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
408 * 'g' and 'o' commands continue to be separate. They could probably be
409 * compacted, but it's not worth the effort.
413 register BITCMD
*set
;
415 register BITCMD
*nset
;
416 register int setbits
, clrbits
, Xbits
, op
;
419 /* Copy over any 'u', 'g' and 'o' commands. */
420 while ((op
= nset
->cmd
) != '+' && op
!= '-' && op
!= 'X') {
426 for (setbits
= clrbits
= Xbits
= 0;; nset
++) {
427 if ((op
= nset
->cmd
) == '-') {
428 clrbits
|= nset
->bits
;
429 setbits
&= ~nset
->bits
;
430 Xbits
&= ~nset
->bits
;
431 } else if (op
== '+') {
432 setbits
|= nset
->bits
;
433 clrbits
&= ~nset
->bits
;
434 Xbits
&= ~nset
->bits
;
435 } else if (op
== 'X')
436 Xbits
|= nset
->bits
& ~setbits
;
462 * $PchId: setmode.c,v 1.3 2006/05/23 11:57:34 philip Exp $