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