]>
git.saurik.com Git - apple/libc.git/blob - gen.subproj/setmode.c
dc2cc3a7c48599f7b6f1daa10d90136bff8adaef
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1989, 1993, 1994
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
27 * Dave Borman at Cray Research, Inc.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/types.h>
72 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
73 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
75 typedef struct bitcmd
{
83 #define CMD2_GBITS 0x04
84 #define CMD2_OBITS 0x08
85 #define CMD2_UBITS 0x10
87 static BITCMD
*addcmd
__P((BITCMD
*, int, int, int, u_int
));
88 static int compress_mode
__P((BITCMD
*));
90 static void dumpmode
__P((BITCMD
*));
94 * Given the old mode and an array of bitcmd structures, apply the operations
95 * described in the bitcmd structures to the old mode, and return the new mode.
96 * Note that there is no '=' command; a strict assignment is just a '-' (clear
97 * bits) followed by a '+' (set bits).
104 register BITCMD
*set
;
105 register mode_t clrval
, newmode
, value
;
107 set
= (BITCMD
*)bbox
;
109 for (value
= 0;; set
++)
112 * When copying the user, group or other bits around, we "know"
113 * where the bits are in the mode so that we can do shifts to
114 * copy them around. If we don't use shifts, it gets real
115 * grundgy with lots of single bit checks and bit sets.
118 value
= (newmode
& S_IRWXU
) >> 6;
122 value
= (newmode
& S_IRWXG
) >> 3;
126 value
= newmode
& S_IRWXO
;
127 common
: if (set
->cmd2
& CMD2_CLR
) {
129 (set
->cmd2
& CMD2_SET
) ? S_IRWXO
: value
;
130 if (set
->cmd2
& CMD2_UBITS
)
131 newmode
&= ~((clrval
<<6) & set
->bits
);
132 if (set
->cmd2
& CMD2_GBITS
)
133 newmode
&= ~((clrval
<<3) & set
->bits
);
134 if (set
->cmd2
& CMD2_OBITS
)
135 newmode
&= ~(clrval
& set
->bits
);
137 if (set
->cmd2
& CMD2_SET
) {
138 if (set
->cmd2
& CMD2_UBITS
)
139 newmode
|= (value
<<6) & set
->bits
;
140 if (set
->cmd2
& CMD2_GBITS
)
141 newmode
|= (value
<<3) & set
->bits
;
142 if (set
->cmd2
& CMD2_OBITS
)
143 newmode
|= value
& set
->bits
;
148 newmode
|= set
->bits
;
152 newmode
&= ~set
->bits
;
156 if (omode
& (S_IFDIR
|S_IXUSR
|S_IXGRP
|S_IXOTH
))
157 newmode
|= set
->bits
;
163 (void)printf("getmode:%04o -> %04o\n", omode
, newmode
);
169 #define ADDCMD(a, b, c, d) \
170 if (set >= endset) { \
171 register BITCMD *newset; \
172 setlen += SET_LEN_INCR; \
173 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
176 set = newset + (set - saveset); \
178 endset = newset + (setlen - 2); \
180 set = addcmd(set, (a), (b), (c), (d))
182 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
188 register int perm
, who
;
190 BITCMD
*set
, *saveset
, *endset
;
191 sigset_t sigset
, sigoset
;
193 int equalopdone
, permXbits
, setlen
;
199 * Get a copy of the mask for the permissions that are mask relative.
200 * Flip the bits, we want what's not set. Since it's possible that
201 * the caller is opening files inside a signal handler, protect them
205 (void)sigprocmask(SIG_BLOCK
, &sigset
, &sigoset
);
206 (void)umask(mask
= umask(0));
208 (void)sigprocmask(SIG_SETMASK
, &sigoset
, NULL
);
210 setlen
= SET_LEN
+ 2;
212 if ((set
= malloc((u_int
)(sizeof(BITCMD
) * setlen
))) == NULL
)
215 endset
= set
+ (setlen
- 2);
218 * If an absolute number, get it and return; disallow non-octal digits
222 perm
= (mode_t
)strtol(p
, NULL
, 8);
223 if (perm
& ~(STANDARD_BITS
|S_ISTXT
)) {
228 if (*p
< '0' || *p
> '7') {
232 ADDCMD('=', (STANDARD_BITS
|S_ISTXT
), perm
, mask
);
237 * Build list of structures to set/clear/copy bits as described by
238 * each clause of the symbolic mode.
241 /* First, find out which bits might be modified. */
242 for (who
= 0;; ++p
) {
245 who
|= STANDARD_BITS
;
248 who
|= S_ISUID
|S_IRWXU
;
251 who
|= S_ISGID
|S_IRWXG
;
261 getop
: if ((op
= *p
++) != '+' && op
!= '-' && op
!= '=') {
269 for (perm
= 0, permXbits
= 0;; ++p
) {
272 perm
|= S_IRUSR
|S_IRGRP
|S_IROTH
;
275 /* If only "other" bits ignore set-id. */
277 perm
|= S_ISUID
|S_ISGID
;
280 /* If only "other" bits ignore sticky. */
281 if (who
& ~S_IRWXO
) {
287 perm
|= S_IWUSR
|S_IWGRP
|S_IWOTH
;
290 permXbits
= S_IXUSR
|S_IXGRP
|S_IXOTH
;
293 perm
|= S_IXUSR
|S_IXGRP
|S_IXOTH
;
299 * When ever we hit 'u', 'g', or 'o', we have
300 * to flush out any partial mode that we have,
301 * and then do the copying of the mode bits.
304 ADDCMD(op
, who
, perm
, mask
);
309 if (op
== '+' && permXbits
) {
310 ADDCMD('X', who
, permXbits
, mask
);
313 ADDCMD(*p
, who
, op
, mask
);
318 * Add any permissions that we haven't already
321 if (perm
|| (op
== '=' && !equalopdone
)) {
324 ADDCMD(op
, who
, perm
, mask
);
328 ADDCMD('X', who
, permXbits
, mask
);
343 (void)printf("Before compress_mode()\n");
346 compress_mode(saveset
);
348 (void)printf("After compress_mode()\n");
355 addcmd(set
, op
, who
, oparg
, mask
)
357 register int oparg
, who
;
364 set
->bits
= who
? who
: STANDARD_BITS
;
373 set
->bits
= (who
? who
: mask
) & oparg
;
381 set
->cmd2
= ((who
& S_IRUSR
) ? CMD2_UBITS
: 0) |
382 ((who
& S_IRGRP
) ? CMD2_GBITS
: 0) |
383 ((who
& S_IROTH
) ? CMD2_OBITS
: 0);
386 set
->cmd2
= CMD2_UBITS
| CMD2_GBITS
| CMD2_OBITS
;
391 set
->cmd2
|= CMD2_SET
;
392 else if (oparg
== '-')
393 set
->cmd2
|= CMD2_CLR
;
394 else if (oparg
== '=')
395 set
->cmd2
|= CMD2_SET
|CMD2_CLR
;
404 register BITCMD
*set
;
406 for (; set
->cmd
; ++set
)
407 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
408 set
->cmd
, set
->bits
, set
->cmd2
? " cmd2:" : "",
409 set
->cmd2
& CMD2_CLR
? " CLR" : "",
410 set
->cmd2
& CMD2_SET
? " SET" : "",
411 set
->cmd2
& CMD2_UBITS
? " UBITS" : "",
412 set
->cmd2
& CMD2_GBITS
? " GBITS" : "",
413 set
->cmd2
& CMD2_OBITS
? " OBITS" : "");
418 * Given an array of bitcmd structures, compress by compacting consecutive
419 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
420 * 'g' and 'o' commands continue to be separate. They could probably be
421 * compacted, but it's not worth the effort.
425 register BITCMD
*set
;
427 register BITCMD
*nset
;
428 register int setbits
, clrbits
, Xbits
, op
;
431 /* Copy over any 'u', 'g' and 'o' commands. */
432 while ((op
= nset
->cmd
) != '+' && op
!= '-' && op
!= 'X') {
438 for (setbits
= clrbits
= Xbits
= 0;; nset
++) {
439 if ((op
= nset
->cmd
) == '-') {
440 clrbits
|= nset
->bits
;
441 setbits
&= ~nset
->bits
;
442 Xbits
&= ~nset
->bits
;
443 } else if (op
== '+') {
444 setbits
|= nset
->bits
;
445 clrbits
&= ~nset
->bits
;
446 Xbits
&= ~nset
->bits
;
447 } else if (op
== 'X')
448 Xbits
|= nset
->bits
& ~setbits
;