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