Home | History | Annotate | Line # | Download | only in gen
setmode.c revision 1.13.2.1
      1 /*	$NetBSD: setmode.c,v 1.13.2.1 1996/09/19 20:03:39 jtc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Dave Borman at Cray Research, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #if defined(LIBC_SCCS) && !defined(lint)
     40 #if 0
     41 static char sccsid[] = "@(#)setmode.c	8.2 (Berkeley) 3/25/94";
     42 #else
     43 static char rcsid[] = "$NetBSD: setmode.c,v 1.13.2.1 1996/09/19 20:03:39 jtc Exp $";
     44 #endif
     45 #endif /* LIBC_SCCS and not lint */
     46 
     47 #include "namespace.h"
     48 #include <sys/types.h>
     49 #include <sys/stat.h>
     50 
     51 #include <ctype.h>
     52 #include <errno.h>
     53 #include <signal.h>
     54 #include <stdlib.h>
     55 
     56 #ifdef SETMODE_DEBUG
     57 #include <stdio.h>
     58 #endif
     59 
     60 #ifdef __weak_alias
     61 __weak_alias(getmode,_getmode);
     62 __weak_alias(setmode,_setmode);
     63 #endif
     64 
     65 #define	SET_LEN	6		/* initial # of bitcmd struct to malloc */
     66 #define	SET_LEN_INCR 4		/* # of bitcmd structs to add as needed */
     67 
     68 typedef struct bitcmd {
     69 	char	cmd;
     70 	char	cmd2;
     71 	mode_t	bits;
     72 } BITCMD;
     73 
     74 #define	CMD2_CLR	0x01
     75 #define	CMD2_SET	0x02
     76 #define	CMD2_GBITS	0x04
     77 #define	CMD2_OBITS	0x08
     78 #define	CMD2_UBITS	0x10
     79 
     80 static BITCMD	*addcmd __P((BITCMD *, int, int, int, u_int));
     81 static void	 compress_mode __P((BITCMD *));
     82 #ifdef SETMODE_DEBUG
     83 static void	 dumpmode __P((BITCMD *));
     84 #endif
     85 
     86 /*
     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).
     91  */
     92 mode_t
     93 getmode(bbox, omode)
     94 	void *bbox;
     95 	mode_t omode;
     96 {
     97 	register BITCMD *set;
     98 	register mode_t clrval, newmode, value;
     99 
    100 	set = (BITCMD *)bbox;
    101 	newmode = omode;
    102 	for (value = 0;; set++)
    103 		switch(set->cmd) {
    104 		/*
    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.
    109 		 */
    110 		case 'u':
    111 			value = (newmode & S_IRWXU) >> 6;
    112 			goto common;
    113 
    114 		case 'g':
    115 			value = (newmode & S_IRWXG) >> 3;
    116 			goto common;
    117 
    118 		case 'o':
    119 			value = newmode & S_IRWXO;
    120 common:			if (set->cmd2 & CMD2_CLR) {
    121 				clrval =
    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);
    129 			}
    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;
    137 			}
    138 			break;
    139 
    140 		case '+':
    141 			newmode |= set->bits;
    142 			break;
    143 
    144 		case '-':
    145 			newmode &= ~set->bits;
    146 			break;
    147 
    148 		case 'X':
    149 			if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
    150 				newmode |= set->bits;
    151 			break;
    152 
    153 		case '\0':
    154 		default:
    155 #ifdef SETMODE_DEBUG
    156 			(void)printf("getmode:%04o -> %04o\n", omode, newmode);
    157 #endif
    158 			return (newmode);
    159 		}
    160 }
    161 
    162 #define	ADDCMD(a, b, c, d)						\
    163 	if (set >= endset) {						\
    164 		register BITCMD *newset;				\
    165 		setlen += SET_LEN_INCR;					\
    166 		newset = realloc(saveset, sizeof(BITCMD) * setlen);	\
    167 		if (!saveset)						\
    168 			return (NULL);					\
    169 		set = newset + (set - saveset);				\
    170 		saveset = newset;					\
    171 		endset = newset + (setlen - 2);				\
    172 	}								\
    173 	set = addcmd(set, (a), (b), (c), (d))
    174 
    175 #define	STANDARD_BITS	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
    176 
    177 void *
    178 setmode(p)
    179 	register char *p;
    180 {
    181 	register int perm, who;
    182 	register char op;
    183 	BITCMD *set, *saveset, *endset;
    184 	sigset_t sigset, sigoset;
    185 	mode_t mask;
    186 	int equalopdone, permXbits, setlen;
    187 
    188 	if (!*p)
    189 		return (NULL);
    190 
    191 	/*
    192 	 * Get a copy of the mask for the permissions that are mask relative.
    193 	 * Flip the bits, we want what's not set.  Since it's possible that
    194 	 * the caller is opening files inside a signal handler, protect them
    195 	 * as best we can.
    196 	 */
    197 	sigfillset(&sigset);
    198         (void)sigprocmask(SIG_BLOCK, &sigset, &sigoset);
    199 	(void)umask(mask = umask(0));
    200 	mask = ~mask;
    201         (void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
    202 
    203 	setlen = SET_LEN + 2;
    204 
    205 	if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
    206 		return (NULL);
    207 	saveset = set;
    208 	endset = set + (setlen - 2);
    209 
    210 	/*
    211 	 * If an absolute number, get it and return; disallow non-octal digits
    212 	 * or illegal bits.
    213 	 */
    214 	if (isdigit(*p)) {
    215 		perm = (mode_t)strtol(p, NULL, 8);
    216 		if (perm & ~(STANDARD_BITS|S_ISTXT)) {
    217 			free(saveset);
    218 			return (NULL);
    219 		}
    220 		while (*++p)
    221 			if (*p < '0' || *p > '7') {
    222 				free(saveset);
    223 				return (NULL);
    224 			}
    225 		ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
    226 		return (saveset);
    227 	}
    228 
    229 	/*
    230 	 * Build list of structures to set/clear/copy bits as described by
    231 	 * each clause of the symbolic mode.
    232 	 */
    233 	for (;;) {
    234 		/* First, find out which bits might be modified. */
    235 		for (who = 0;; ++p) {
    236 			switch (*p) {
    237 			case 'a':
    238 				who |= STANDARD_BITS;
    239 				break;
    240 			case 'u':
    241 				who |= S_ISUID|S_IRWXU;
    242 				break;
    243 			case 'g':
    244 				who |= S_ISGID|S_IRWXG;
    245 				break;
    246 			case 'o':
    247 				who |= S_IRWXO;
    248 				break;
    249 			default:
    250 				goto getop;
    251 			}
    252 		}
    253 
    254 getop:		if ((op = *p++) != '+' && op != '-' && op != '=') {
    255 			free(saveset);
    256 			return (NULL);
    257 		}
    258 		if (op == '=')
    259 			equalopdone = 0;
    260 
    261 		who &= ~S_ISTXT;
    262 		for (perm = 0, permXbits = 0;; ++p) {
    263 			switch (*p) {
    264 			case 'r':
    265 				perm |= S_IRUSR|S_IRGRP|S_IROTH;
    266 				break;
    267 			case 's':
    268 				/* If only "other" bits ignore set-id. */
    269 				if (who & ~S_IRWXO)
    270 					perm |= S_ISUID|S_ISGID;
    271 				break;
    272 			case 't':
    273 				/* If only "other" bits ignore sticky. */
    274 				if (who & ~S_IRWXO) {
    275 					who |= S_ISTXT;
    276 					perm |= S_ISTXT;
    277 				}
    278 				break;
    279 			case 'w':
    280 				perm |= S_IWUSR|S_IWGRP|S_IWOTH;
    281 				break;
    282 			case 'X':
    283 				permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
    284 				break;
    285 			case 'x':
    286 				perm |= S_IXUSR|S_IXGRP|S_IXOTH;
    287 				break;
    288 			case 'u':
    289 			case 'g':
    290 			case 'o':
    291 				/*
    292 				 * When ever we hit 'u', 'g', or 'o', we have
    293 				 * to flush out any partial mode that we have,
    294 				 * and then do the copying of the mode bits.
    295 				 */
    296 				if (perm) {
    297 					ADDCMD(op, who, perm, mask);
    298 					perm = 0;
    299 				}
    300 				if (op == '=')
    301 					equalopdone = 1;
    302 				if (op == '+' && permXbits) {
    303 					ADDCMD('X', who, permXbits, mask);
    304 					permXbits = 0;
    305 				}
    306 				ADDCMD(*p, who, op, mask);
    307 				break;
    308 
    309 			default:
    310 				/*
    311 				 * Add any permissions that we haven't already
    312 				 * done.
    313 				 */
    314 				if (perm || (op == '=' && !equalopdone)) {
    315 					if (op == '=')
    316 						equalopdone = 1;
    317 					ADDCMD(op, who, perm, mask);
    318 					perm = 0;
    319 				}
    320 				if (permXbits) {
    321 					ADDCMD('X', who, permXbits, mask);
    322 					permXbits = 0;
    323 				}
    324 				goto apply;
    325 			}
    326 		}
    327 
    328 apply:		if (!*p)
    329 			break;
    330 		if (*p != ',')
    331 			goto getop;
    332 		++p;
    333 	}
    334 	set->cmd = 0;
    335 #ifdef SETMODE_DEBUG
    336 	(void)printf("Before compress_mode()\n");
    337 	dumpmode(saveset);
    338 #endif
    339 	compress_mode(saveset);
    340 #ifdef SETMODE_DEBUG
    341 	(void)printf("After compress_mode()\n");
    342 	dumpmode(saveset);
    343 #endif
    344 	return (saveset);
    345 }
    346 
    347 static BITCMD *
    348 addcmd(set, op, who, oparg, mask)
    349 	BITCMD *set;
    350 	register int oparg, who;
    351 	register int op;
    352 	u_int mask;
    353 {
    354 	switch (op) {
    355 	case '=':
    356 		set->cmd = '-';
    357 		set->bits = who ? who : STANDARD_BITS;
    358 		set++;
    359 
    360 		op = '+';
    361 		/* FALLTHROUGH */
    362 	case '+':
    363 	case '-':
    364 	case 'X':
    365 		set->cmd = op;
    366 		set->bits = (who ? who : mask) & oparg;
    367 		break;
    368 
    369 	case 'u':
    370 	case 'g':
    371 	case 'o':
    372 		set->cmd = op;
    373 		if (who) {
    374 			set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
    375 				    ((who & S_IRGRP) ? CMD2_GBITS : 0) |
    376 				    ((who & S_IROTH) ? CMD2_OBITS : 0);
    377 			set->bits = ~0;
    378 		} else {
    379 			set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
    380 			set->bits = mask;
    381 		}
    382 
    383 		if (oparg == '+')
    384 			set->cmd2 |= CMD2_SET;
    385 		else if (oparg == '-')
    386 			set->cmd2 |= CMD2_CLR;
    387 		else if (oparg == '=')
    388 			set->cmd2 |= CMD2_SET|CMD2_CLR;
    389 		break;
    390 	}
    391 	return (set + 1);
    392 }
    393 
    394 #ifdef SETMODE_DEBUG
    395 static void
    396 dumpmode(set)
    397 	register BITCMD *set;
    398 {
    399 	for (; set->cmd; ++set)
    400 		(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
    401 		    set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
    402 		    set->cmd2 & CMD2_CLR ? " CLR" : "",
    403 		    set->cmd2 & CMD2_SET ? " SET" : "",
    404 		    set->cmd2 & CMD2_UBITS ? " UBITS" : "",
    405 		    set->cmd2 & CMD2_GBITS ? " GBITS" : "",
    406 		    set->cmd2 & CMD2_OBITS ? " OBITS" : "");
    407 }
    408 #endif
    409 
    410 /*
    411  * Given an array of bitcmd structures, compress by compacting consecutive
    412  * '+', '-' and 'X' commands into at most 3 commands, one of each.  The 'u',
    413  * 'g' and 'o' commands continue to be separate.  They could probably be
    414  * compacted, but it's not worth the effort.
    415  */
    416 static void
    417 compress_mode(set)
    418 	register BITCMD *set;
    419 {
    420 	register BITCMD *nset;
    421 	register int setbits, clrbits, Xbits, op;
    422 
    423 	for (nset = set;;) {
    424 		/* Copy over any 'u', 'g' and 'o' commands. */
    425 		while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
    426 			*set++ = *nset++;
    427 			if (!op)
    428 				return;
    429 		}
    430 
    431 		for (setbits = clrbits = Xbits = 0;; nset++) {
    432 			if ((op = nset->cmd) == '-') {
    433 				clrbits |= nset->bits;
    434 				setbits &= ~nset->bits;
    435 				Xbits &= ~nset->bits;
    436 			} else if (op == '+') {
    437 				setbits |= nset->bits;
    438 				clrbits &= ~nset->bits;
    439 				Xbits &= ~nset->bits;
    440 			} else if (op == 'X')
    441 				Xbits |= nset->bits & ~setbits;
    442 			else
    443 				break;
    444 		}
    445 		if (clrbits) {
    446 			set->cmd = '-';
    447 			set->cmd2 = 0;
    448 			set->bits = clrbits;
    449 			set++;
    450 		}
    451 		if (setbits) {
    452 			set->cmd = '+';
    453 			set->cmd2 = 0;
    454 			set->bits = setbits;
    455 			set++;
    456 		}
    457 		if (Xbits) {
    458 			set->cmd = 'X';
    459 			set->cmd2 = 0;
    460 			set->bits = Xbits;
    461 			set++;
    462 		}
    463 	}
    464 }
    465