1 /* $NetBSD: setmode.c,v 1.30 2003/08/07 16:42:56 agc Exp $ */
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Dave Borman at Cray Research, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
40 __RCSID("$NetBSD: setmode.c,v 1.30 2003/08/07 16:42:56 agc Exp $");
42 #endif /* LIBC_SCCS and not lint */
44 #include "namespace.h"
45 #include <sys/types.h>
61 __weak_alias(getmode
,_getmode
)
62 __weak_alias(setmode
,_setmode
)
65 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
66 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
68 typedef struct bitcmd
{
76 #define CMD2_GBITS 0x04
77 #define CMD2_OBITS 0x08
78 #define CMD2_UBITS 0x10
80 static BITCMD
*addcmd
__P((BITCMD
*, mode_t
, mode_t
, mode_t
, mode_t
));
81 static void compress_mode
__P((BITCMD
*));
83 static void dumpmode
__P((BITCMD
*));
87 * Given the old mode and an array of bitcmd structures, apply the operations
88 * described in the bitcmd structures to the old mode, and return the new mode.
89 * Note that there is no '=' command; a strict assignment is just a '-' (clear
90 * bits) followed by a '+' (set bits).
98 mode_t clrval
, newmode
, value
;
100 _DIAGASSERT(bbox
!= NULL
);
102 set
= (const BITCMD
*)bbox
;
104 for (value
= 0;; set
++)
107 * When copying the user, group or other bits around, we "know"
108 * where the bits are in the mode so that we can do shifts to
109 * copy them around. If we don't use shifts, it gets real
110 * grundgy with lots of single bit checks and bit sets.
113 value
= (newmode
& S_IRWXU
) >> 6;
117 value
= (newmode
& S_IRWXG
) >> 3;
121 value
= newmode
& S_IRWXO
;
122 common
: if (set
->cmd2
& CMD2_CLR
) {
124 (set
->cmd2
& CMD2_SET
) ? S_IRWXO
: value
;
125 if (set
->cmd2
& CMD2_UBITS
)
126 newmode
&= ~((clrval
<<6) & set
->bits
);
127 if (set
->cmd2
& CMD2_GBITS
)
128 newmode
&= ~((clrval
<<3) & set
->bits
);
129 if (set
->cmd2
& CMD2_OBITS
)
130 newmode
&= ~(clrval
& set
->bits
);
132 if (set
->cmd2
& CMD2_SET
) {
133 if (set
->cmd2
& CMD2_UBITS
)
134 newmode
|= (value
<<6) & set
->bits
;
135 if (set
->cmd2
& CMD2_GBITS
)
136 newmode
|= (value
<<3) & set
->bits
;
137 if (set
->cmd2
& CMD2_OBITS
)
138 newmode
|= value
& set
->bits
;
143 newmode
|= set
->bits
;
147 newmode
&= ~set
->bits
;
151 if (omode
& (S_IFDIR
|S_IXUSR
|S_IXGRP
|S_IXOTH
))
152 newmode
|= set
->bits
;
158 (void)printf("getmode:%04o -> %04o\n", omode
, newmode
);
164 #define ADDCMD(a, b, c, d) do { \
165 if (set >= endset) { \
167 setlen += SET_LEN_INCR; \
168 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
169 if (newset == NULL) \
171 set = newset + (set - saveset); \
173 endset = newset + (setlen - 2); \
175 set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d)); \
176 } while (/*CONSTCOND*/0)
178 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
186 BITCMD
*set
, *saveset
, *endset
;
187 sigset_t signset
, sigoset
;
188 mode_t mask
, perm
, permXbits
, who
;
190 int equalopdone
= 0; /* pacify gcc */
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
204 sigfillset(&signset
);
205 (void)sigprocmask(SIG_BLOCK
, &signset
, &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
221 if (isdigit((unsigned char)*p
)) {
223 lval
= strtol(p
, &ep
, 8);
228 if (errno
== ERANGE
&& (lval
== LONG_MAX
|| lval
== LONG_MIN
))
230 if (lval
& ~(STANDARD_BITS
|S_ISTXT
)) {
235 ADDCMD('=', (STANDARD_BITS
|S_ISTXT
), perm
, mask
);
241 * Build list of structures to set/clear/copy bits as described by
242 * each clause of the symbolic mode.
245 /* First, find out which bits might be modified. */
246 for (who
= 0;; ++p
) {
249 who
|= STANDARD_BITS
;
252 who
|= S_ISUID
|S_IRWXU
;
255 who
|= S_ISGID
|S_IRWXG
;
265 getop
: if ((op
= *p
++) != '+' && op
!= '-' && op
!= '=') {
273 for (perm
= 0, permXbits
= 0;; ++p
) {
276 perm
|= S_IRUSR
|S_IRGRP
|S_IROTH
;
280 * If specific bits where requested and
281 * only "other" bits ignore set-id.
283 if (who
== 0 || (who
& ~S_IRWXO
))
284 perm
|= S_ISUID
|S_ISGID
;
288 * If specific bits where requested and
289 * only "other" bits ignore set-id.
291 if (who
== 0 || (who
& ~S_IRWXO
)) {
297 perm
|= S_IWUSR
|S_IWGRP
|S_IWOTH
;
300 permXbits
= S_IXUSR
|S_IXGRP
|S_IXOTH
;
303 perm
|= S_IXUSR
|S_IXGRP
|S_IXOTH
;
309 * When ever we hit 'u', 'g', or 'o', we have
310 * to flush out any partial mode that we have,
311 * and then do the copying of the mode bits.
314 ADDCMD(op
, who
, perm
, mask
);
319 if (op
== '+' && permXbits
) {
320 ADDCMD('X', who
, permXbits
, mask
);
323 ADDCMD(*p
, who
, op
, mask
);
328 * Add any permissions that we haven't already
331 if (perm
|| (op
== '=' && !equalopdone
)) {
334 ADDCMD(op
, who
, perm
, mask
);
338 ADDCMD('X', who
, permXbits
, mask
);
353 (void)printf("Before compress_mode()\n");
356 compress_mode(saveset
);
358 (void)printf("After compress_mode()\n");
370 addcmd(set
, op
, who
, oparg
, mask
)
372 mode_t oparg
, who
, op
, mask
;
375 _DIAGASSERT(set
!= NULL
);
380 set
->bits
= who
? who
: STANDARD_BITS
;
389 set
->bits
= (who
? who
: mask
) & oparg
;
397 set
->cmd2
= ((who
& S_IRUSR
) ? CMD2_UBITS
: 0) |
398 ((who
& S_IRGRP
) ? CMD2_GBITS
: 0) |
399 ((who
& S_IROTH
) ? CMD2_OBITS
: 0);
400 set
->bits
= (mode_t
)~0;
402 set
->cmd2
= CMD2_UBITS
| CMD2_GBITS
| CMD2_OBITS
;
407 set
->cmd2
|= CMD2_SET
;
408 else if (oparg
== '-')
409 set
->cmd2
|= CMD2_CLR
;
410 else if (oparg
== '=')
411 set
->cmd2
|= CMD2_SET
|CMD2_CLR
;
423 _DIAGASSERT(set
!= NULL
);
425 for (; set
->cmd
; ++set
)
426 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
427 set
->cmd
, set
->bits
, set
->cmd2
? " cmd2:" : "",
428 set
->cmd2
& CMD2_CLR
? " CLR" : "",
429 set
->cmd2
& CMD2_SET
? " SET" : "",
430 set
->cmd2
& CMD2_UBITS
? " UBITS" : "",
431 set
->cmd2
& CMD2_GBITS
? " GBITS" : "",
432 set
->cmd2
& CMD2_OBITS
? " OBITS" : "");
437 * Given an array of bitcmd structures, compress by compacting consecutive
438 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
439 * 'g' and 'o' commands continue to be separate. They could probably be
440 * compacted, but it's not worth the effort.
447 int setbits
, clrbits
, Xbits
, op
;
449 _DIAGASSERT(set
!= NULL
);
452 /* Copy over any 'u', 'g' and 'o' commands. */
453 while ((op
= nset
->cmd
) != '+' && op
!= '-' && op
!= 'X') {
459 for (setbits
= clrbits
= Xbits
= 0;; nset
++) {
460 if ((op
= nset
->cmd
) == '-') {
461 clrbits
|= nset
->bits
;
462 setbits
&= ~nset
->bits
;
463 Xbits
&= ~nset
->bits
;
464 } else if (op
== '+') {
465 setbits
|= nset
->bits
;
466 clrbits
&= ~nset
->bits
;
467 Xbits
&= ~nset
->bits
;
468 } else if (op
== 'X')
469 Xbits
|= nset
->bits
& ~setbits
;