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