xref: /netbsd-src/lib/libc/gen/setmode.c (revision 50c92675a9b228020b938b264e23f13a0c48710c)
1 /*	$NetBSD: setmode.c,v 1.15 1997/02/07 22:21:06 christos 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.15 1997/02/07 22:21:06 christos Exp $";
44 #endif
45 #endif /* LIBC_SCCS and not lint */
46 
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 
50 #include <ctype.h>
51 #include <errno.h>
52 #include <signal.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 
56 #ifdef SETMODE_DEBUG
57 #include <stdio.h>
58 #endif
59 
60 #define	SET_LEN	6		/* initial # of bitcmd struct to malloc */
61 #define	SET_LEN_INCR 4		/* # of bitcmd structs to add as needed */
62 
63 typedef struct bitcmd {
64 	char	cmd;
65 	char	cmd2;
66 	mode_t	bits;
67 } BITCMD;
68 
69 #define	CMD2_CLR	0x01
70 #define	CMD2_SET	0x02
71 #define	CMD2_GBITS	0x04
72 #define	CMD2_OBITS	0x08
73 #define	CMD2_UBITS	0x10
74 
75 static BITCMD	*addcmd __P((BITCMD *, int, int, int, u_int));
76 static void	 compress_mode __P((BITCMD *));
77 #ifdef SETMODE_DEBUG
78 static void	 dumpmode __P((BITCMD *));
79 #endif
80 
81 /*
82  * Given the old mode and an array of bitcmd structures, apply the operations
83  * described in the bitcmd structures to the old mode, and return the new mode.
84  * Note that there is no '=' command; a strict assignment is just a '-' (clear
85  * bits) followed by a '+' (set bits).
86  */
87 mode_t
88 getmode(bbox, omode)
89 	const void *bbox;
90 	mode_t omode;
91 {
92 	register const BITCMD *set;
93 	register mode_t clrval, newmode, value;
94 
95 	set = (const BITCMD *)bbox;
96 	newmode = omode;
97 	for (value = 0;; set++)
98 		switch(set->cmd) {
99 		/*
100 		 * When copying the user, group or other bits around, we "know"
101 		 * where the bits are in the mode so that we can do shifts to
102 		 * copy them around.  If we don't use shifts, it gets real
103 		 * grundgy with lots of single bit checks and bit sets.
104 		 */
105 		case 'u':
106 			value = (newmode & S_IRWXU) >> 6;
107 			goto common;
108 
109 		case 'g':
110 			value = (newmode & S_IRWXG) >> 3;
111 			goto common;
112 
113 		case 'o':
114 			value = newmode & S_IRWXO;
115 common:			if (set->cmd2 & CMD2_CLR) {
116 				clrval =
117 				    (set->cmd2 & CMD2_SET) ?  S_IRWXO : value;
118 				if (set->cmd2 & CMD2_UBITS)
119 					newmode &= ~((clrval<<6) & set->bits);
120 				if (set->cmd2 & CMD2_GBITS)
121 					newmode &= ~((clrval<<3) & set->bits);
122 				if (set->cmd2 & CMD2_OBITS)
123 					newmode &= ~(clrval & set->bits);
124 			}
125 			if (set->cmd2 & CMD2_SET) {
126 				if (set->cmd2 & CMD2_UBITS)
127 					newmode |= (value<<6) & set->bits;
128 				if (set->cmd2 & CMD2_GBITS)
129 					newmode |= (value<<3) & set->bits;
130 				if (set->cmd2 & CMD2_OBITS)
131 					newmode |= value & set->bits;
132 			}
133 			break;
134 
135 		case '+':
136 			newmode |= set->bits;
137 			break;
138 
139 		case '-':
140 			newmode &= ~set->bits;
141 			break;
142 
143 		case 'X':
144 			if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
145 				newmode |= set->bits;
146 			break;
147 
148 		case '\0':
149 		default:
150 #ifdef SETMODE_DEBUG
151 			(void)printf("getmode:%04o -> %04o\n", omode, newmode);
152 #endif
153 			return (newmode);
154 		}
155 }
156 
157 #define	ADDCMD(a, b, c, d)						\
158 	if (set >= endset) {						\
159 		register BITCMD *newset;				\
160 		setlen += SET_LEN_INCR;					\
161 		newset = realloc(saveset, sizeof(BITCMD) * setlen);	\
162 		if (!saveset)						\
163 			return (NULL);					\
164 		set = newset + (set - saveset);				\
165 		saveset = newset;					\
166 		endset = newset + (setlen - 2);				\
167 	}								\
168 	set = addcmd(set, (a), (b), (c), (d))
169 
170 #define	STANDARD_BITS	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
171 
172 void *
173 setmode(p)
174 	register const char *p;
175 {
176 	register int perm, who;
177 	register char op;
178 	BITCMD *set, *saveset, *endset;
179 	sigset_t sigset, sigoset;
180 	mode_t mask;
181 	int equalopdone, permXbits, setlen;
182 
183 	if (!*p)
184 		return (NULL);
185 
186 	/*
187 	 * Get a copy of the mask for the permissions that are mask relative.
188 	 * Flip the bits, we want what's not set.  Since it's possible that
189 	 * the caller is opening files inside a signal handler, protect them
190 	 * as best we can.
191 	 */
192 	sigfillset(&sigset);
193         (void)sigprocmask(SIG_BLOCK, &sigset, &sigoset);
194 	(void)umask(mask = umask(0));
195 	mask = ~mask;
196         (void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
197 
198 	setlen = SET_LEN + 2;
199 
200 	if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
201 		return (NULL);
202 	saveset = set;
203 	endset = set + (setlen - 2);
204 
205 	/*
206 	 * If an absolute number, get it and return; disallow non-octal digits
207 	 * or illegal bits.
208 	 */
209 	if (isdigit(*p)) {
210 		perm = (mode_t)strtol(p, NULL, 8);
211 		if (perm & ~(STANDARD_BITS|S_ISTXT)) {
212 			free(saveset);
213 			return (NULL);
214 		}
215 		while (*++p)
216 			if (*p < '0' || *p > '7') {
217 				free(saveset);
218 				return (NULL);
219 			}
220 		ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
221 		return (saveset);
222 	}
223 
224 	/*
225 	 * Build list of structures to set/clear/copy bits as described by
226 	 * each clause of the symbolic mode.
227 	 */
228 	for (;;) {
229 		/* First, find out which bits might be modified. */
230 		for (who = 0;; ++p) {
231 			switch (*p) {
232 			case 'a':
233 				who |= STANDARD_BITS;
234 				break;
235 			case 'u':
236 				who |= S_ISUID|S_IRWXU;
237 				break;
238 			case 'g':
239 				who |= S_ISGID|S_IRWXG;
240 				break;
241 			case 'o':
242 				who |= S_IRWXO;
243 				break;
244 			default:
245 				goto getop;
246 			}
247 		}
248 
249 getop:		if ((op = *p++) != '+' && op != '-' && op != '=') {
250 			free(saveset);
251 			return (NULL);
252 		}
253 		if (op == '=')
254 			equalopdone = 0;
255 
256 		who &= ~S_ISTXT;
257 		for (perm = 0, permXbits = 0;; ++p) {
258 			switch (*p) {
259 			case 'r':
260 				perm |= S_IRUSR|S_IRGRP|S_IROTH;
261 				break;
262 			case 's':
263 				/*
264 				 * If specific bits where requested and
265 				 * only "other" bits ignore set-id.
266 				 */
267 				if (who == 0 || (who & ~S_IRWXO))
268 					perm |= S_ISUID|S_ISGID;
269 				break;
270 			case 't':
271 				/*
272 				 * If specific bits where requested and
273 				 * only "other" bits ignore set-id.
274 				 */
275 				if (who == 0 || (who & ~S_IRWXO)) {
276 					who |= S_ISTXT;
277 					perm |= S_ISTXT;
278 				}
279 				break;
280 			case 'w':
281 				perm |= S_IWUSR|S_IWGRP|S_IWOTH;
282 				break;
283 			case 'X':
284 				permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
285 				break;
286 			case 'x':
287 				perm |= S_IXUSR|S_IXGRP|S_IXOTH;
288 				break;
289 			case 'u':
290 			case 'g':
291 			case 'o':
292 				/*
293 				 * When ever we hit 'u', 'g', or 'o', we have
294 				 * to flush out any partial mode that we have,
295 				 * and then do the copying of the mode bits.
296 				 */
297 				if (perm) {
298 					ADDCMD(op, who, perm, mask);
299 					perm = 0;
300 				}
301 				if (op == '=')
302 					equalopdone = 1;
303 				if (op == '+' && permXbits) {
304 					ADDCMD('X', who, permXbits, mask);
305 					permXbits = 0;
306 				}
307 				ADDCMD(*p, who, op, mask);
308 				break;
309 
310 			default:
311 				/*
312 				 * Add any permissions that we haven't already
313 				 * done.
314 				 */
315 				if (perm || (op == '=' && !equalopdone)) {
316 					if (op == '=')
317 						equalopdone = 1;
318 					ADDCMD(op, who, perm, mask);
319 					perm = 0;
320 				}
321 				if (permXbits) {
322 					ADDCMD('X', who, permXbits, mask);
323 					permXbits = 0;
324 				}
325 				goto apply;
326 			}
327 		}
328 
329 apply:		if (!*p)
330 			break;
331 		if (*p != ',')
332 			goto getop;
333 		++p;
334 	}
335 	set->cmd = 0;
336 #ifdef SETMODE_DEBUG
337 	(void)printf("Before compress_mode()\n");
338 	dumpmode(saveset);
339 #endif
340 	compress_mode(saveset);
341 #ifdef SETMODE_DEBUG
342 	(void)printf("After compress_mode()\n");
343 	dumpmode(saveset);
344 #endif
345 	return (saveset);
346 }
347 
348 static BITCMD *
349 addcmd(set, op, who, oparg, mask)
350 	BITCMD *set;
351 	register int oparg, who;
352 	register int op;
353 	u_int mask;
354 {
355 	switch (op) {
356 	case '=':
357 		set->cmd = '-';
358 		set->bits = who ? who : STANDARD_BITS;
359 		set++;
360 
361 		op = '+';
362 		/* FALLTHROUGH */
363 	case '+':
364 	case '-':
365 	case 'X':
366 		set->cmd = op;
367 		set->bits = (who ? who : mask) & oparg;
368 		break;
369 
370 	case 'u':
371 	case 'g':
372 	case 'o':
373 		set->cmd = op;
374 		if (who) {
375 			set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
376 				    ((who & S_IRGRP) ? CMD2_GBITS : 0) |
377 				    ((who & S_IROTH) ? CMD2_OBITS : 0);
378 			set->bits = ~0;
379 		} else {
380 			set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
381 			set->bits = mask;
382 		}
383 
384 		if (oparg == '+')
385 			set->cmd2 |= CMD2_SET;
386 		else if (oparg == '-')
387 			set->cmd2 |= CMD2_CLR;
388 		else if (oparg == '=')
389 			set->cmd2 |= CMD2_SET|CMD2_CLR;
390 		break;
391 	}
392 	return (set + 1);
393 }
394 
395 #ifdef SETMODE_DEBUG
396 static void
397 dumpmode(set)
398 	register BITCMD *set;
399 {
400 	for (; set->cmd; ++set)
401 		(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
402 		    set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
403 		    set->cmd2 & CMD2_CLR ? " CLR" : "",
404 		    set->cmd2 & CMD2_SET ? " SET" : "",
405 		    set->cmd2 & CMD2_UBITS ? " UBITS" : "",
406 		    set->cmd2 & CMD2_GBITS ? " GBITS" : "",
407 		    set->cmd2 & CMD2_OBITS ? " OBITS" : "");
408 }
409 #endif
410 
411 /*
412  * Given an array of bitcmd structures, compress by compacting consecutive
413  * '+', '-' and 'X' commands into at most 3 commands, one of each.  The 'u',
414  * 'g' and 'o' commands continue to be separate.  They could probably be
415  * compacted, but it's not worth the effort.
416  */
417 static void
418 compress_mode(set)
419 	register BITCMD *set;
420 {
421 	register BITCMD *nset;
422 	register int setbits, clrbits, Xbits, op;
423 
424 	for (nset = set;;) {
425 		/* Copy over any 'u', 'g' and 'o' commands. */
426 		while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
427 			*set++ = *nset++;
428 			if (!op)
429 				return;
430 		}
431 
432 		for (setbits = clrbits = Xbits = 0;; nset++) {
433 			if ((op = nset->cmd) == '-') {
434 				clrbits |= nset->bits;
435 				setbits &= ~nset->bits;
436 				Xbits &= ~nset->bits;
437 			} else if (op == '+') {
438 				setbits |= nset->bits;
439 				clrbits &= ~nset->bits;
440 				Xbits &= ~nset->bits;
441 			} else if (op == 'X')
442 				Xbits |= nset->bits & ~setbits;
443 			else
444 				break;
445 		}
446 		if (clrbits) {
447 			set->cmd = '-';
448 			set->cmd2 = 0;
449 			set->bits = clrbits;
450 			set++;
451 		}
452 		if (setbits) {
453 			set->cmd = '+';
454 			set->cmd2 = 0;
455 			set->bits = setbits;
456 			set++;
457 		}
458 		if (Xbits) {
459 			set->cmd = 'X';
460 			set->cmd2 = 0;
461 			set->bits = Xbits;
462 			set++;
463 		}
464 	}
465 }
466