xref: /csrg-svn/lib/libc/gen/setmode.c (revision 52791)
139978Sbostic /*
239978Sbostic  * Copyright (c) 1989 The Regents of the University of California.
339978Sbostic  * All rights reserved.
439978Sbostic  *
542626Sbostic  * %sccs.include.redist.c%
639978Sbostic  */
739978Sbostic 
839978Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*52791Sbostic static char sccsid[] = "@(#)setmode.c	5.7 (Berkeley) 03/02/92";
1039978Sbostic #endif /* LIBC_SCCS and not lint */
1139978Sbostic 
12*52791Sbostic #include <sys/types.h>
1339978Sbostic #include <sys/stat.h>
14*52791Sbostic #include <errno.h>
15*52791Sbostic #define SETMODE_DEBUG
1645901Sbostic #ifdef SETMODE_DEBUG
1741579Sbostic #include <stdio.h>
1845901Sbostic #endif
1945901Sbostic #include <stdlib.h>
2049896Sbostic #include <ctype.h>
2139978Sbostic 
2245901Sbostic #define	SET_LEN	6		/* initial # of bitcmd struct to malloc */
2345901Sbostic #define	SET_LEN_INCR 4		/* # of bitcmd structs to add as needed */
2439978Sbostic 
25*52791Sbostic typedef struct bitcmd {
2645901Sbostic 	char	cmd;
2745901Sbostic 	char	cmd2;
2845901Sbostic 	mode_t	bits;
29*52791Sbostic } BITCMD;
3045901Sbostic 
3145901Sbostic #define	CMD2_CLR	0x01
3245901Sbostic #define	CMD2_SET	0x02
3345901Sbostic #define	CMD2_GBITS	0x04
3445901Sbostic #define	CMD2_OBITS	0x08
3545901Sbostic #define	CMD2_UBITS	0x10
3645901Sbostic 
37*52791Sbostic static BITCMD	*addcmd __P((BITCMD *, int, int, int, u_int));
38*52791Sbostic static int	 compress_mode __P((BITCMD *));
39*52791Sbostic #ifdef SETMODE_DEBUG
40*52791Sbostic static void	 dumpmode __P((BITCMD *));
41*52791Sbostic #endif
42*52791Sbostic 
4345901Sbostic /*
4445901Sbostic  * Given the old mode and an array of bitcmd structures, apply the operations
4545901Sbostic  * described in the bitcmd structures to the old mode, and return the new mode.
4645901Sbostic  * Note that there is no '=' command; a strict assignment is just a '-' (clear
4745901Sbostic  * bits) followed by a '+' (set bits).
4845901Sbostic  */
4939978Sbostic mode_t
5046576Sbostic getmode(bbox, omode)
5146576Sbostic 	void *bbox;
5245901Sbostic 	mode_t omode;
5339978Sbostic {
54*52791Sbostic 	register BITCMD *set;
5545901Sbostic 	register mode_t newmode, value;
5639978Sbostic 
57*52791Sbostic 	set = (BITCMD *)bbox;
5845901Sbostic 	newmode = omode;
5945901Sbostic 	for (value = 0;; set++)
6045901Sbostic 		switch(set->cmd) {
6145901Sbostic 		/*
6245901Sbostic 		 * When copying the user, group or other bits around, we "know"
63*52791Sbostic 		 * where the bits are in the mode so that we can do shifts to
6445901Sbostic 		 * copy them around.  If we don't use shifts, it gets real
6545901Sbostic 		 * grundgy with lots of single bit checks and bit sets.
6645901Sbostic 		 */
6745901Sbostic 		case 'u':
6845901Sbostic 			value = (newmode & S_IRWXU) >> 6;
6945901Sbostic 			goto common;
7045901Sbostic 
7145901Sbostic 		case 'g':
7245901Sbostic 			value = (newmode & S_IRWXG) >> 3;
7345901Sbostic 			goto common;
7445901Sbostic 
7545901Sbostic 		case 'o':
7645901Sbostic 			value = newmode & S_IRWXO;
77*52791Sbostic common:			if (set->cmd2 & CMD2_CLR) {
7845901Sbostic 				if (set->cmd2 & CMD2_UBITS)
7945901Sbostic 					newmode &= ~(S_IRWXU & set->bits);
8045901Sbostic 				if (set->cmd2 & CMD2_GBITS)
8145901Sbostic 					newmode &= ~(S_IRWXG & set->bits);
8245901Sbostic 				if (set->cmd2 & CMD2_OBITS)
8345901Sbostic 					newmode &= ~(S_IRWXO & set->bits);
8445901Sbostic 			}
8545901Sbostic 			if (set->cmd2 & CMD2_SET) {
8645901Sbostic 				if (set->cmd2 & CMD2_UBITS)
8745901Sbostic 					newmode |= (value<<6) & set->bits;
8845901Sbostic 				if (set->cmd2 & CMD2_GBITS)
8945901Sbostic 					newmode |= (value<<3) & set->bits;
9045901Sbostic 				if (set->cmd2 & CMD2_OBITS)
9145901Sbostic 					newmode |= value & set->bits;
9245901Sbostic 			}
9345901Sbostic 			break;
9445901Sbostic 
9545901Sbostic 		case '+':
9645901Sbostic 			newmode |= set->bits;
9745901Sbostic 			break;
9845901Sbostic 
9945901Sbostic 		case '-':
10045901Sbostic 			newmode &= ~set->bits;
10145901Sbostic 			break;
10245901Sbostic 
10345901Sbostic 		case 'X':
10445901Sbostic 			if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
10545901Sbostic 				newmode |= set->bits;
10645901Sbostic 			break;
10745901Sbostic 
10845901Sbostic 		case '\0':
10945901Sbostic 		default:
11045901Sbostic #ifdef SETMODE_DEBUG
111*52791Sbostic 			(void)printf("getmode:%04o -> %04o\n", omode, newmode);
11245901Sbostic #endif
113*52791Sbostic 			return (newmode);
11445901Sbostic 		}
11539978Sbostic }
11639978Sbostic 
11745901Sbostic #define	ADDCMD(a, b, c, d) \
11845901Sbostic 	if (set >= endset) { \
119*52791Sbostic 		register BITCMD *newset; \
12045901Sbostic 		setlen += SET_LEN_INCR; \
121*52791Sbostic 		newset = realloc(saveset, sizeof(BITCMD) * setlen); \
12245901Sbostic 		if (!saveset) \
123*52791Sbostic 			return (NULL); \
12445901Sbostic 		set = newset + (set - saveset); \
12545901Sbostic 		saveset = newset; \
12645901Sbostic 		endset = newset + (setlen - 2); \
12745901Sbostic 	} \
12845901Sbostic 	set = addcmd(set, (a), (b), (c), (d))
12945901Sbostic 
130*52791Sbostic #define	STANDARD_BITS	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
131*52791Sbostic 
13246576Sbostic void *
13339978Sbostic setmode(p)
13439978Sbostic 	register char *p;
13539978Sbostic {
13639978Sbostic 	register int perm, who;
13739978Sbostic 	register char op;
138*52791Sbostic 	BITCMD *set, *saveset, *endset;
13945901Sbostic 	mode_t mask;
14045901Sbostic 	int permXbits, setlen;
14139978Sbostic 
142*52791Sbostic 	if (!*p)
143*52791Sbostic 		return (NULL);
144*52791Sbostic 
14539978Sbostic 	/*
14645901Sbostic 	 * Get a copy of the mask for the permissions that are mask relative.
14745901Sbostic 	 * Flip the bits, we want what's not set.
14839978Sbostic 	 */
14939978Sbostic 	(void)umask(mask = umask(0));
15039978Sbostic 	mask = ~mask;
15139978Sbostic 
15245901Sbostic 	setlen = SET_LEN + 2;
15345901Sbostic 
154*52791Sbostic 	if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
155*52791Sbostic 		return (NULL);
15645901Sbostic 	saveset = set;
15745901Sbostic 	endset = set + (setlen - 2);
15841579Sbostic 
15939978Sbostic 	/*
16045901Sbostic 	 * If an absolute number, get it and return; disallow non-octal digits
16145901Sbostic 	 * or illegal bits.
16239978Sbostic 	 */
16339978Sbostic 	if (isdigit(*p)) {
16445901Sbostic 		perm = (mode_t)strtol(p, (char **)0, 8);
16545901Sbostic 		if (perm & ~(STANDARD_BITS|S_ISTXT)) {
16645901Sbostic 			free(saveset);
167*52791Sbostic 			return (NULL);
16845901Sbostic 		}
16939978Sbostic 		while (*++p)
17045901Sbostic 			if (*p < '0' || *p > '7') {
17145901Sbostic 				free(saveset);
172*52791Sbostic 				return (NULL);
17345901Sbostic 			}
17445901Sbostic 		ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
175*52791Sbostic 		return (saveset);
17639978Sbostic 	}
17739978Sbostic 
17839978Sbostic 	/*
17945901Sbostic 	 * Build list of structures to set/clear/copy bits as described by
18045901Sbostic 	 * each clause of the symbolic mode.
18139978Sbostic 	 */
18239978Sbostic 	for (;;) {
18345901Sbostic 		/* First, find out which bits might be modified. */
18445901Sbostic 		for (who = 0;; ++p) {
18539978Sbostic 			switch (*p) {
18639978Sbostic 			case 'a':
18739978Sbostic 				who |= STANDARD_BITS;
18839978Sbostic 				break;
18939978Sbostic 			case 'u':
19039978Sbostic 				who |= S_ISUID|S_IRWXU;
19139978Sbostic 				break;
19239978Sbostic 			case 'g':
19339978Sbostic 				who |= S_ISGID|S_IRWXG;
19439978Sbostic 				break;
19539978Sbostic 			case 'o':
19639978Sbostic 				who |= S_IRWXO;
19739978Sbostic 				break;
19839978Sbostic 			default:
19939978Sbostic 				goto getop;
20039978Sbostic 			}
20145901Sbostic 		}
20239978Sbostic 
203*52791Sbostic getop:		if ((op = *p++) != '+' && op != '-' && op != '=') {
20445901Sbostic 			free(saveset);
205*52791Sbostic 			return (NULL);
20645901Sbostic 		}
20739978Sbostic 
20839978Sbostic 		who &= ~S_ISTXT;
20945901Sbostic 		for (perm = 0, permXbits = 0;; ++p) {
21039978Sbostic 			switch (*p) {
21139978Sbostic 			case 'r':
21239978Sbostic 				perm |= S_IRUSR|S_IRGRP|S_IROTH;
21339978Sbostic 				break;
21439978Sbostic 			case 's':
21545901Sbostic 				/* If only "other" bits ignore set-id. */
21639978Sbostic 				if (who & ~S_IRWXO)
21739978Sbostic 					perm |= S_ISUID|S_ISGID;
21839978Sbostic 				break;
21939978Sbostic 			case 't':
22045901Sbostic 				/* If only "other" bits ignore sticky. */
22139978Sbostic 				if (who & ~S_IRWXO) {
22239978Sbostic 					who |= S_ISTXT;
22339978Sbostic 					perm |= S_ISTXT;
22439978Sbostic 				}
22539978Sbostic 				break;
22639978Sbostic 			case 'w':
22739978Sbostic 				perm |= S_IWUSR|S_IWGRP|S_IWOTH;
22839978Sbostic 				break;
22939978Sbostic 			case 'X':
23039978Sbostic 				permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
23139978Sbostic 				break;
23239978Sbostic 			case 'x':
23339978Sbostic 				perm |= S_IXUSR|S_IXGRP|S_IXOTH;
23439978Sbostic 				break;
23545901Sbostic 			case 'u':
23645901Sbostic 			case 'g':
23745901Sbostic 			case 'o':
23845901Sbostic 				/*
23945901Sbostic 				 * When ever we hit 'u', 'g', or 'o', we have
24045901Sbostic 				 * to flush out any partial mode that we have,
24145901Sbostic 				 * and then do the copying of the mode bits.
24245901Sbostic 				 */
24345901Sbostic 				if (perm) {
24445901Sbostic 					ADDCMD(op, who, perm, mask);
24545901Sbostic 					perm = 0;
24645901Sbostic 				}
24745901Sbostic 				if (op == '+' && permXbits) {
24845901Sbostic 					ADDCMD('X', who, permXbits, mask);
24945901Sbostic 					permXbits = 0;
25045901Sbostic 				}
25145901Sbostic 				ADDCMD(*p, who, op, mask);
25245901Sbostic 				break;
25345901Sbostic 
25439978Sbostic 			default:
25545901Sbostic 				/*
25645901Sbostic 				 * Add any permissions that we haven't already
25745901Sbostic 				 * done.
25845901Sbostic 				 */
25945901Sbostic 				if (perm) {
26045901Sbostic 					ADDCMD(op, who, perm, mask);
26145901Sbostic 					perm = 0;
26245901Sbostic 				}
26345901Sbostic 				if (permXbits) {
26445901Sbostic 					ADDCMD('X', who, permXbits, mask);
26545901Sbostic 					permXbits = 0;
26645901Sbostic 				}
26739978Sbostic 				goto apply;
26839978Sbostic 			}
26939978Sbostic 		}
27039978Sbostic 
27145901Sbostic apply:		if (!*p)
27239978Sbostic 			break;
27339978Sbostic 		if (*p != ',')
27439978Sbostic 			goto getop;
27539978Sbostic 		++p;
27639978Sbostic 	}
27745901Sbostic 	set->cmd = 0;
27845901Sbostic #ifdef SETMODE_DEBUG
27945901Sbostic 	(void)printf("Before compress_mode()\n");
28045901Sbostic 	dumpmode(saveset);
28145901Sbostic #endif
28245901Sbostic 	compress_mode(saveset);
28345901Sbostic #ifdef SETMODE_DEBUG
28445901Sbostic 	(void)printf("After compress_mode()\n");
28545901Sbostic 	dumpmode(saveset);
28645901Sbostic #endif
287*52791Sbostic 	return (saveset);
28839978Sbostic }
28945901Sbostic 
290*52791Sbostic static BITCMD *
291*52791Sbostic addcmd(set, op, who, oparg, mask)
292*52791Sbostic 	BITCMD *set;
293*52791Sbostic 	register int oparg, who;
294*52791Sbostic 	register int op;
295*52791Sbostic 	u_int mask;
296*52791Sbostic {
297*52791Sbostic 	switch (op) {
298*52791Sbostic 	case '+':
299*52791Sbostic 	case 'X':
300*52791Sbostic 		set->cmd = op;
301*52791Sbostic 		set->bits = (who ? who : mask) & oparg;
302*52791Sbostic 		break;
303*52791Sbostic 
304*52791Sbostic 	case '-':
305*52791Sbostic 		set->cmd = '-';
306*52791Sbostic 		set->bits = (who ? who : (S_IRWXU|S_IRWXG|S_IRWXO)) & oparg;
307*52791Sbostic 		break;
308*52791Sbostic 
309*52791Sbostic 	case '=':
310*52791Sbostic 		set->cmd = '-';
311*52791Sbostic 		if (!who) {
312*52791Sbostic 			set->bits = STANDARD_BITS;
313*52791Sbostic 			who = mask;
314*52791Sbostic 		} else
315*52791Sbostic 			set->bits = who;
316*52791Sbostic 		set++;
317*52791Sbostic 
318*52791Sbostic 		set->cmd = '+';
319*52791Sbostic 		set->bits = who & oparg;
320*52791Sbostic 		break;
321*52791Sbostic 	case 'u':
322*52791Sbostic 	case 'g':
323*52791Sbostic 	case 'o':
324*52791Sbostic 		set->cmd = op;
325*52791Sbostic 		if (who) {
326*52791Sbostic 			set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
327*52791Sbostic 				    ((who & S_IRGRP) ? CMD2_GBITS : 0) |
328*52791Sbostic 				    ((who & S_IROTH) ? CMD2_OBITS : 0);
329*52791Sbostic 			set->bits = ~0;
330*52791Sbostic 		} else {
331*52791Sbostic 			set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
332*52791Sbostic 			set->bits = mask;
333*52791Sbostic 		}
334*52791Sbostic 
335*52791Sbostic 		if (oparg == '+')
336*52791Sbostic 			set->cmd2 |= CMD2_SET;
337*52791Sbostic 		else if (oparg == '-')
338*52791Sbostic 			set->cmd2 |= CMD2_CLR;
339*52791Sbostic 		else if (oparg == '=')
340*52791Sbostic 			set->cmd2 |= CMD2_SET|CMD2_CLR;
341*52791Sbostic 		break;
342*52791Sbostic 	}
343*52791Sbostic 	return (set + 1);
344*52791Sbostic }
345*52791Sbostic 
34645901Sbostic #ifdef SETMODE_DEBUG
347*52791Sbostic static void
34845901Sbostic dumpmode(set)
349*52791Sbostic 	register BITCMD *set;
35045901Sbostic {
35145901Sbostic 	for (; set->cmd; ++set)
35245901Sbostic 		(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
35345901Sbostic 		    set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
35445901Sbostic 		    set->cmd2 & CMD2_CLR ? " CLR" : "",
35545901Sbostic 		    set->cmd2 & CMD2_SET ? " SET" : "",
35645901Sbostic 		    set->cmd2 & CMD2_UBITS ? " UBITS" : "",
35745901Sbostic 		    set->cmd2 & CMD2_GBITS ? " GBITS" : "",
35845901Sbostic 		    set->cmd2 & CMD2_OBITS ? " OBITS" : "");
35945901Sbostic }
36045901Sbostic #endif
36145901Sbostic 
36245901Sbostic /*
36345901Sbostic  * Given an array of bitcmd structures, compress by compacting consecutive
36445901Sbostic  * '+', '-' and 'X' commands into at most 3 commands, one of each.  The 'u',
36545901Sbostic  * 'g' and 'o' commands continue to be separate.  They could probably be
36645901Sbostic  * compacted, but it's not worth the effort.
36745901Sbostic  */
368*52791Sbostic static int
36945901Sbostic compress_mode(set)
370*52791Sbostic 	register BITCMD *set;
37145901Sbostic {
372*52791Sbostic 	register BITCMD *nset;
37345901Sbostic 	register int setbits, clrbits, Xbits, op;
37445901Sbostic 
37545901Sbostic 	for (nset = set;;) {
37645901Sbostic 		/* Copy over any 'u', 'g' and 'o' commands. */
37745901Sbostic 		while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
37845901Sbostic 			*set++ = *nset++;
37945901Sbostic 			if (!op)
38045901Sbostic 				return;
38145901Sbostic 		}
38245901Sbostic 
38345901Sbostic 		for (setbits = clrbits = Xbits = 0;; nset++) {
38445901Sbostic 			if ((op = nset->cmd) == '-') {
38545901Sbostic 				clrbits |= nset->bits;
38645901Sbostic 				setbits &= ~nset->bits;
38745901Sbostic 				Xbits &= ~nset->bits;
38845901Sbostic 			} else if (op == '+') {
38945901Sbostic 				setbits |= nset->bits;
39045901Sbostic 				clrbits &= ~nset->bits;
39145901Sbostic 				Xbits &= ~nset->bits;
39245901Sbostic 			} else if (op == 'X')
39345901Sbostic 				Xbits |= nset->bits & ~setbits;
39445901Sbostic 			else
39545901Sbostic 				break;
39645901Sbostic 		}
39745901Sbostic 		if (clrbits) {
39845901Sbostic 			set->cmd = '-';
39945901Sbostic 			set->cmd2 = 0;
40045901Sbostic 			set->bits = clrbits;
40145901Sbostic 			set++;
40245901Sbostic 		}
40345901Sbostic 		if (setbits) {
40445901Sbostic 			set->cmd = '+';
40545901Sbostic 			set->cmd2 = 0;
40645901Sbostic 			set->bits = setbits;
40745901Sbostic 			set++;
40845901Sbostic 		}
40945901Sbostic 		if (Xbits) {
41045901Sbostic 			set->cmd = 'X';
41145901Sbostic 			set->cmd2 = 0;
41245901Sbostic 			set->bits = Xbits;
41345901Sbostic 			set++;
41445901Sbostic 		}
41545901Sbostic 	}
41645901Sbostic }
417