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