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