xref: /openbsd-src/lib/libc/gen/setmode.c (revision b725ae7711052a2233e31a66fefb8a752c388d7a)
1 /*	$OpenBSD: setmode.c,v 1.13 2004/05/18 02:05:52 jfb 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.13 2004/05/18 02:05:52 jfb 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(const char *p)
173 {
174 	register int perm, who;
175 	register char op;
176 	BITCMD *set, *saveset, *endset;
177 	sigset_t sigset, sigoset;
178 	mode_t mask;
179 	int equalopdone, permXbits, setlen;
180 	long perml;
181 
182 	if (!*p)
183 		return (NULL);
184 
185 	/*
186 	 * Get a copy of the mask for the permissions that are mask relative.
187 	 * Flip the bits, we want what's not set.  Since it's possible that
188 	 * the caller is opening files inside a signal handler, protect them
189 	 * as best we can.
190 	 */
191 	sigfillset(&sigset);
192 	(void)sigprocmask(SIG_BLOCK, &sigset, &sigoset);
193 	(void)umask(mask = umask(0));
194 	mask = ~mask;
195 	(void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
196 
197 	setlen = SET_LEN + 2;
198 
199 	if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
200 		return (NULL);
201 	saveset = set;
202 	endset = set + (setlen - 2);
203 
204 	/*
205 	 * If an absolute number, get it and return; disallow non-octal digits
206 	 * or illegal bits.
207 	 */
208 	if (isdigit(*p)) {
209 		perml = strtol(p, NULL, 8);
210 		if (perml < 0 || (perml & ~(STANDARD_BITS|S_ISTXT))) {
211 			free(saveset);
212 			return (NULL);
213 		}
214 		perm = (mode_t)perml;
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 sticky.
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(BITCMD *set, int op, int who, int oparg, u_int mask)
350 {
351 	switch (op) {
352 	case '=':
353 		set->cmd = '-';
354 		set->bits = who ? who : STANDARD_BITS;
355 		set++;
356 
357 		op = '+';
358 		/* FALLTHROUGH */
359 	case '+':
360 	case '-':
361 	case 'X':
362 		set->cmd = op;
363 		set->bits = (who ? who : mask) & oparg;
364 		break;
365 
366 	case 'u':
367 	case 'g':
368 	case 'o':
369 		set->cmd = op;
370 		if (who) {
371 			set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
372 				    ((who & S_IRGRP) ? CMD2_GBITS : 0) |
373 				    ((who & S_IROTH) ? CMD2_OBITS : 0);
374 			set->bits = (mode_t)~0;
375 		} else {
376 			set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
377 			set->bits = mask;
378 		}
379 
380 		if (oparg == '+')
381 			set->cmd2 |= CMD2_SET;
382 		else if (oparg == '-')
383 			set->cmd2 |= CMD2_CLR;
384 		else if (oparg == '=')
385 			set->cmd2 |= CMD2_SET|CMD2_CLR;
386 		break;
387 	}
388 	return (set + 1);
389 }
390 
391 #ifdef SETMODE_DEBUG
392 static void
393 dumpmode(BITCMD *set)
394 {
395 	for (; set->cmd; ++set)
396 		(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
397 		    set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
398 		    set->cmd2 & CMD2_CLR ? " CLR" : "",
399 		    set->cmd2 & CMD2_SET ? " SET" : "",
400 		    set->cmd2 & CMD2_UBITS ? " UBITS" : "",
401 		    set->cmd2 & CMD2_GBITS ? " GBITS" : "",
402 		    set->cmd2 & CMD2_OBITS ? " OBITS" : "");
403 }
404 #endif
405 
406 /*
407  * Given an array of bitcmd structures, compress by compacting consecutive
408  * '+', '-' and 'X' commands into at most 3 commands, one of each.  The 'u',
409  * 'g' and 'o' commands continue to be separate.  They could probably be
410  * compacted, but it's not worth the effort.
411  */
412 static void
413 compress_mode(BITCMD *set)
414 {
415 	register BITCMD *nset;
416 	register int setbits, clrbits, Xbits, op;
417 
418 	for (nset = set;;) {
419 		/* Copy over any 'u', 'g' and 'o' commands. */
420 		while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
421 			*set++ = *nset++;
422 			if (!op)
423 				return;
424 		}
425 
426 		for (setbits = clrbits = Xbits = 0;; nset++) {
427 			if ((op = nset->cmd) == '-') {
428 				clrbits |= nset->bits;
429 				setbits &= ~nset->bits;
430 				Xbits &= ~nset->bits;
431 			} else if (op == '+') {
432 				setbits |= nset->bits;
433 				clrbits &= ~nset->bits;
434 				Xbits &= ~nset->bits;
435 			} else if (op == 'X')
436 				Xbits |= nset->bits & ~setbits;
437 			else
438 				break;
439 		}
440 		if (clrbits) {
441 			set->cmd = '-';
442 			set->cmd2 = 0;
443 			set->bits = clrbits;
444 			set++;
445 		}
446 		if (setbits) {
447 			set->cmd = '+';
448 			set->cmd2 = 0;
449 			set->bits = setbits;
450 			set++;
451 		}
452 		if (Xbits) {
453 			set->cmd = 'X';
454 			set->cmd2 = 0;
455 			set->bits = Xbits;
456 			set++;
457 		}
458 	}
459 }
460