xref: /openbsd-src/lib/libc/gen/setmode.c (revision 11efff7f3ac2b3cfeff0c0cddc14294d9b3aca4f)
1 /*	$OpenBSD: setmode.c,v 1.15 2004/07/02 13:58:06 otto 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.15 2004/07/02 13:58:06 otto 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 	const BITCMD *set;
88 	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 		BITCMD *newset;						\
155 		setlen += SET_LEN_INCR;					\
156 		newset = realloc(saveset, sizeof(BITCMD) * setlen);	\
157 		if (newset == NULL) {					\
158 			free(saveset);					\
159 			return (NULL);					\
160 		}							\
161 		set = newset + (set - saveset);				\
162 		saveset = newset;					\
163 		endset = newset + (setlen - 2);				\
164 	}								\
165 	set = addcmd(set, (a), (b), (c), (d))
166 
167 #define	STANDARD_BITS	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
168 
169 void *
170 setmode(const char *p)
171 {
172 	int perm, who;
173 	char op, *ep;
174 	BITCMD *set, *saveset, *endset;
175 	sigset_t sigset, sigoset;
176 	mode_t mask;
177 	int equalopdone, permXbits, setlen;
178 	u_long perml;
179 
180 	if (!*p)
181 		return (NULL);
182 
183 	/*
184 	 * Get a copy of the mask for the permissions that are mask relative.
185 	 * Flip the bits, we want what's not set.  Since it's possible that
186 	 * the caller is opening files inside a signal handler, protect them
187 	 * as best we can.
188 	 */
189 	sigfillset(&sigset);
190 	(void)sigprocmask(SIG_BLOCK, &sigset, &sigoset);
191 	(void)umask(mask = umask(0));
192 	mask = ~mask;
193 	(void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
194 
195 	setlen = SET_LEN + 2;
196 
197 	if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
198 		return (NULL);
199 	saveset = set;
200 	endset = set + (setlen - 2);
201 
202 	/*
203 	 * If an absolute number, get it and return; disallow non-octal digits
204 	 * or illegal bits.
205 	 */
206 	if (isdigit(*p)) {
207 		perml = strtoul(p, &ep, 8);
208 		/* The test on perml will also catch overflow. */
209 		if (*ep != '\0' || (perml & ~(STANDARD_BITS|S_ISTXT))) {
210 			errno = ERANGE;
211 			free(saveset);
212 			return (NULL);
213 		}
214 		perm = (mode_t)perml;
215 		ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
216 		set->cmd = 0;
217 		return (saveset);
218 	}
219 
220 	/*
221 	 * Build list of structures to set/clear/copy bits as described by
222 	 * each clause of the symbolic mode.
223 	 */
224 	for (;;) {
225 		/* First, find out which bits might be modified. */
226 		for (who = 0;; ++p) {
227 			switch (*p) {
228 			case 'a':
229 				who |= STANDARD_BITS;
230 				break;
231 			case 'u':
232 				who |= S_ISUID|S_IRWXU;
233 				break;
234 			case 'g':
235 				who |= S_ISGID|S_IRWXG;
236 				break;
237 			case 'o':
238 				who |= S_IRWXO;
239 				break;
240 			default:
241 				goto getop;
242 			}
243 		}
244 
245 getop:		if ((op = *p++) != '+' && op != '-' && op != '=') {
246 			free(saveset);
247 			return (NULL);
248 		}
249 		if (op == '=')
250 			equalopdone = 0;
251 
252 		who &= ~S_ISTXT;
253 		for (perm = 0, permXbits = 0;; ++p) {
254 			switch (*p) {
255 			case 'r':
256 				perm |= S_IRUSR|S_IRGRP|S_IROTH;
257 				break;
258 			case 's':
259 				/*
260 				 * If specific bits where requested and
261 				 * only "other" bits ignore set-id.
262 				 */
263 				if (who == 0 || (who & ~S_IRWXO))
264 					perm |= S_ISUID|S_ISGID;
265 				break;
266 			case 't':
267 				/*
268 				 * If specific bits where requested and
269 				 * only "other" bits ignore sticky.
270 				 */
271 				if (who == 0 || (who & ~S_IRWXO)) {
272 					who |= S_ISTXT;
273 					perm |= S_ISTXT;
274 				}
275 				break;
276 			case 'w':
277 				perm |= S_IWUSR|S_IWGRP|S_IWOTH;
278 				break;
279 			case 'X':
280 				permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
281 				break;
282 			case 'x':
283 				perm |= S_IXUSR|S_IXGRP|S_IXOTH;
284 				break;
285 			case 'u':
286 			case 'g':
287 			case 'o':
288 				/*
289 				 * When ever we hit 'u', 'g', or 'o', we have
290 				 * to flush out any partial mode that we have,
291 				 * and then do the copying of the mode bits.
292 				 */
293 				if (perm) {
294 					ADDCMD(op, who, perm, mask);
295 					perm = 0;
296 				}
297 				if (op == '=')
298 					equalopdone = 1;
299 				if (op == '+' && permXbits) {
300 					ADDCMD('X', who, permXbits, mask);
301 					permXbits = 0;
302 				}
303 				ADDCMD(*p, who, op, mask);
304 				break;
305 
306 			default:
307 				/*
308 				 * Add any permissions that we haven't already
309 				 * done.
310 				 */
311 				if (perm || (op == '=' && !equalopdone)) {
312 					if (op == '=')
313 						equalopdone = 1;
314 					ADDCMD(op, who, perm, mask);
315 					perm = 0;
316 				}
317 				if (permXbits) {
318 					ADDCMD('X', who, permXbits, mask);
319 					permXbits = 0;
320 				}
321 				goto apply;
322 			}
323 		}
324 
325 apply:		if (!*p)
326 			break;
327 		if (*p != ',')
328 			goto getop;
329 		++p;
330 	}
331 	set->cmd = 0;
332 #ifdef SETMODE_DEBUG
333 	(void)printf("Before compress_mode()\n");
334 	dumpmode(saveset);
335 #endif
336 	compress_mode(saveset);
337 #ifdef SETMODE_DEBUG
338 	(void)printf("After compress_mode()\n");
339 	dumpmode(saveset);
340 #endif
341 	return (saveset);
342 }
343 
344 static BITCMD *
345 addcmd(BITCMD *set, int op, int who, int oparg, u_int mask)
346 {
347 	switch (op) {
348 	case '=':
349 		set->cmd = '-';
350 		set->bits = who ? who : STANDARD_BITS;
351 		set++;
352 
353 		op = '+';
354 		/* FALLTHROUGH */
355 	case '+':
356 	case '-':
357 	case 'X':
358 		set->cmd = op;
359 		set->bits = (who ? who : mask) & oparg;
360 		break;
361 
362 	case 'u':
363 	case 'g':
364 	case 'o':
365 		set->cmd = op;
366 		if (who) {
367 			set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
368 				    ((who & S_IRGRP) ? CMD2_GBITS : 0) |
369 				    ((who & S_IROTH) ? CMD2_OBITS : 0);
370 			set->bits = (mode_t)~0;
371 		} else {
372 			set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
373 			set->bits = mask;
374 		}
375 
376 		if (oparg == '+')
377 			set->cmd2 |= CMD2_SET;
378 		else if (oparg == '-')
379 			set->cmd2 |= CMD2_CLR;
380 		else if (oparg == '=')
381 			set->cmd2 |= CMD2_SET|CMD2_CLR;
382 		break;
383 	}
384 	return (set + 1);
385 }
386 
387 #ifdef SETMODE_DEBUG
388 static void
389 dumpmode(BITCMD *set)
390 {
391 	for (; set->cmd; ++set)
392 		(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
393 		    set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
394 		    set->cmd2 & CMD2_CLR ? " CLR" : "",
395 		    set->cmd2 & CMD2_SET ? " SET" : "",
396 		    set->cmd2 & CMD2_UBITS ? " UBITS" : "",
397 		    set->cmd2 & CMD2_GBITS ? " GBITS" : "",
398 		    set->cmd2 & CMD2_OBITS ? " OBITS" : "");
399 }
400 #endif
401 
402 /*
403  * Given an array of bitcmd structures, compress by compacting consecutive
404  * '+', '-' and 'X' commands into at most 3 commands, one of each.  The 'u',
405  * 'g' and 'o' commands continue to be separate.  They could probably be
406  * compacted, but it's not worth the effort.
407  */
408 static void
409 compress_mode(BITCMD *set)
410 {
411 	BITCMD *nset;
412 	int setbits, clrbits, Xbits, op;
413 
414 	for (nset = set;;) {
415 		/* Copy over any 'u', 'g' and 'o' commands. */
416 		while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
417 			*set++ = *nset++;
418 			if (!op)
419 				return;
420 		}
421 
422 		for (setbits = clrbits = Xbits = 0;; nset++) {
423 			if ((op = nset->cmd) == '-') {
424 				clrbits |= nset->bits;
425 				setbits &= ~nset->bits;
426 				Xbits &= ~nset->bits;
427 			} else if (op == '+') {
428 				setbits |= nset->bits;
429 				clrbits &= ~nset->bits;
430 				Xbits &= ~nset->bits;
431 			} else if (op == 'X')
432 				Xbits |= nset->bits & ~setbits;
433 			else
434 				break;
435 		}
436 		if (clrbits) {
437 			set->cmd = '-';
438 			set->cmd2 = 0;
439 			set->bits = clrbits;
440 			set++;
441 		}
442 		if (setbits) {
443 			set->cmd = '+';
444 			set->cmd2 = 0;
445 			set->bits = setbits;
446 			set++;
447 		}
448 		if (Xbits) {
449 			set->cmd = 'X';
450 			set->cmd2 = 0;
451 			set->bits = Xbits;
452 			set++;
453 		}
454 	}
455 }
456