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