1 /* $NetBSD: setmode.c,v 1.34 2012/06/25 22:32:43 abs 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.34 2012/06/25 22:32:43 abs 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(BITCMD
*, mode_t
, mode_t
, mode_t
, mode_t
);
81 static void compress_mode(BITCMD
*);
83 static void dumpmode(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).
93 getmode(const void *bbox
, mode_t omode
)
96 mode_t clrval
, newmode
, value
;
98 _DIAGASSERT(bbox
!= NULL
);
100 set
= (const BITCMD
*)bbox
;
102 for (value
= 0;; set
++)
105 * When copying the user, group or other bits around, we "know"
106 * where the bits are in the mode so that we can do shifts to
107 * copy them around. If we don't use shifts, it gets real
108 * grundgy with lots of single bit checks and bit sets.
111 value
= (newmode
& S_IRWXU
) >> 6;
115 value
= (newmode
& S_IRWXG
) >> 3;
119 value
= newmode
& S_IRWXO
;
120 common
: if (set
->cmd2
& CMD2_CLR
) {
122 (set
->cmd2
& CMD2_SET
) ? S_IRWXO
: value
;
123 if (set
->cmd2
& CMD2_UBITS
)
124 newmode
&= ~((clrval
<<6) & set
->bits
);
125 if (set
->cmd2
& CMD2_GBITS
)
126 newmode
&= ~((clrval
<<3) & set
->bits
);
127 if (set
->cmd2
& CMD2_OBITS
)
128 newmode
&= ~(clrval
& set
->bits
);
130 if (set
->cmd2
& CMD2_SET
) {
131 if (set
->cmd2
& CMD2_UBITS
)
132 newmode
|= (value
<<6) & set
->bits
;
133 if (set
->cmd2
& CMD2_GBITS
)
134 newmode
|= (value
<<3) & set
->bits
;
135 if (set
->cmd2
& CMD2_OBITS
)
136 newmode
|= value
& set
->bits
;
141 newmode
|= set
->bits
;
145 newmode
&= ~set
->bits
;
149 if (omode
& (S_IFDIR
|S_IXUSR
|S_IXGRP
|S_IXOTH
))
150 newmode
|= set
->bits
;
156 (void)printf("getmode:%04o -> %04o\n", omode
, newmode
);
162 #define ADDCMD(a, b, c, d) do { \
163 if (set >= endset) { \
165 setlen += SET_LEN_INCR; \
166 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
167 if (newset == NULL) \
169 set = newset + (set - saveset); \
171 endset = newset + (setlen - 2); \
173 set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d)); \
174 } while (/*CONSTCOND*/0)
176 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
179 setmode(const char *p
)
183 BITCMD
*set
, *saveset
, *endset
;
184 sigset_t signset
, sigoset
;
185 mode_t mask
, perm
, permXbits
, who
;
187 int equalopdone
= 0; /* pacify gcc */
196 * Get a copy of the mask for the permissions that are mask relative.
197 * Flip the bits, we want what's not set. Since it's possible that
198 * the caller is opening files inside a signal handler, protect them
201 sigfillset(&signset
);
202 (void)sigprocmask(SIG_BLOCK
, &signset
, &sigoset
);
203 (void)umask(mask
= umask(0));
205 (void)sigprocmask(SIG_SETMASK
, &sigoset
, NULL
);
207 setlen
= SET_LEN
+ 2;
209 if ((set
= malloc((u_int
)(sizeof(BITCMD
) * setlen
))) == NULL
)
212 endset
= set
+ (setlen
- 2);
215 * If an absolute number, get it and return; disallow non-octal digits
218 if (isdigit((unsigned char)*p
)) {
220 lval
= strtol(p
, &ep
, 8);
225 if (errno
== ERANGE
&& (lval
== LONG_MAX
|| lval
== LONG_MIN
))
227 if (lval
& ~(STANDARD_BITS
|S_ISTXT
)) {
232 ADDCMD('=', (STANDARD_BITS
|S_ISTXT
), perm
, mask
);
238 * Build list of structures to set/clear/copy bits as described by
239 * each clause of the symbolic mode.
242 /* First, find out which bits might be modified. */
243 for (who
= 0;; ++p
) {
246 who
|= STANDARD_BITS
;
249 who
|= S_ISUID
|S_IRWXU
;
252 who
|= S_ISGID
|S_IRWXG
;
262 getop
: if ((op
= *p
++) != '+' && op
!= '-' && op
!= '=') {
270 for (perm
= 0, permXbits
= 0;; ++p
) {
273 perm
|= S_IRUSR
|S_IRGRP
|S_IROTH
;
277 * If specific bits where requested and
278 * only "other" bits ignore set-id.
280 if (who
== 0 || (who
& ~S_IRWXO
))
281 perm
|= S_ISUID
|S_ISGID
;
285 * If specific bits where requested and
286 * only "other" bits ignore set-id.
288 if (who
== 0 || (who
& ~S_IRWXO
)) {
294 perm
|= S_IWUSR
|S_IWGRP
|S_IWOTH
;
297 permXbits
= S_IXUSR
|S_IXGRP
|S_IXOTH
;
300 perm
|= S_IXUSR
|S_IXGRP
|S_IXOTH
;
306 * When ever we hit 'u', 'g', or 'o', we have
307 * to flush out any partial mode that we have,
308 * and then do the copying of the mode bits.
311 ADDCMD(op
, who
, perm
, mask
);
316 if (op
== '+' && permXbits
) {
317 ADDCMD('X', who
, permXbits
, mask
);
320 ADDCMD(*p
, who
, op
, mask
);
325 * Add any permissions that we haven't already
328 if (perm
|| (op
== '=' && !equalopdone
)) {
331 ADDCMD(op
, who
, perm
, mask
);
335 ADDCMD('X', who
, permXbits
, mask
);
350 (void)printf("Before compress_mode()\n");
353 compress_mode(saveset
);
355 (void)printf("After compress_mode()\n");
367 addcmd(BITCMD
*set
, mode_t op
, mode_t who
, mode_t oparg
, mode_t mask
)
370 _DIAGASSERT(set
!= NULL
);
375 set
->bits
= who
? who
: STANDARD_BITS
;
384 set
->bits
= (who
? who
: mask
) & oparg
;
392 set
->cmd2
= ((who
& S_IRUSR
) ? CMD2_UBITS
: 0) |
393 ((who
& S_IRGRP
) ? CMD2_GBITS
: 0) |
394 ((who
& S_IROTH
) ? CMD2_OBITS
: 0);
395 set
->bits
= (mode_t
)~0;
397 set
->cmd2
= CMD2_UBITS
| CMD2_GBITS
| CMD2_OBITS
;
402 set
->cmd2
|= CMD2_SET
;
403 else if (oparg
== '-')
404 set
->cmd2
|= CMD2_CLR
;
405 else if (oparg
== '=')
406 set
->cmd2
|= CMD2_SET
|CMD2_CLR
;
414 dumpmode(BITCMD
*set
)
417 _DIAGASSERT(set
!= NULL
);
419 for (; set
->cmd
; ++set
)
420 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
421 set
->cmd
, set
->bits
, set
->cmd2
? " cmd2:" : "",
422 set
->cmd2
& CMD2_CLR
? " CLR" : "",
423 set
->cmd2
& CMD2_SET
? " SET" : "",
424 set
->cmd2
& CMD2_UBITS
? " UBITS" : "",
425 set
->cmd2
& CMD2_GBITS
? " GBITS" : "",
426 set
->cmd2
& CMD2_OBITS
? " OBITS" : "");
431 * Given an array of bitcmd structures, compress by compacting consecutive
432 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
433 * 'g' and 'o' commands continue to be separate. They could probably be
434 * compacted, but it's not worth the effort.
437 compress_mode(BITCMD
*set
)
440 int setbits
, clrbits
, Xbits
, op
;
442 _DIAGASSERT(set
!= NULL
);
445 /* Copy over any 'u', 'g' and 'o' commands. */
446 while ((op
= nset
->cmd
) != '+' && op
!= '-' && op
!= 'X') {
452 for (setbits
= clrbits
= Xbits
= 0;; nset
++) {
453 if ((op
= nset
->cmd
) == '-') {
454 clrbits
|= nset
->bits
;
455 setbits
&= ~nset
->bits
;
456 Xbits
&= ~nset
->bits
;
457 } else if (op
== '+') {
458 setbits
|= nset
->bits
;
459 clrbits
&= ~nset
->bits
;
460 Xbits
&= ~nset
->bits
;
461 } else if (op
== 'X')
462 Xbits
|= nset
->bits
& ~setbits
;