xref: /freebsd/lib/libc/gen/setmode.c (revision 26a222dc0c048fc071b548eadad7b80405a1b126)
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 
43 #include <ctype.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <signal.h>
47 #include <stddef.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 
51 #ifdef SETMODE_DEBUG
52 #include <stdio.h>
53 #endif
54 #include "un-namespace.h"
55 
56 #define	SET_LEN	6		/* initial # of bitcmd struct to malloc */
57 #define	SET_LEN_INCR 4		/* # of bitcmd structs to add as needed */
58 
59 typedef struct bitcmd {
60 	char	cmd;
61 	char	cmd2;
62 	mode_t	bits;
63 } BITCMD;
64 
65 #define	CMD2_CLR	0x01
66 #define	CMD2_SET	0x02
67 #define	CMD2_GBITS	0x04
68 #define	CMD2_OBITS	0x08
69 #define	CMD2_UBITS	0x10
70 
71 static BITCMD	*addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t);
72 static void	 compress_mode(BITCMD *);
73 #ifdef SETMODE_DEBUG
74 static void	 dumpmode(BITCMD *);
75 #endif
76 
77 /*
78  * Given the old mode and an array of bitcmd structures, apply the operations
79  * described in the bitcmd structures to the old mode, and return the new mode.
80  * Note that there is no '=' command; a strict assignment is just a '-' (clear
81  * bits) followed by a '+' (set bits).
82  */
83 mode_t
84 getmode(const void *bbox, mode_t omode)
85 {
86 	const BITCMD *set;
87 	mode_t clrval, newmode, value;
88 
89 	set = (const BITCMD *)bbox;
90 	newmode = omode;
91 	for (value = 0;; set++)
92 		switch(set->cmd) {
93 		/*
94 		 * When copying the user, group or other bits around, we "know"
95 		 * where the bits are in the mode so that we can do shifts to
96 		 * copy them around.  If we don't use shifts, it gets real
97 		 * grundgy with lots of single bit checks and bit sets.
98 		 */
99 		case 'u':
100 			value = (newmode & S_IRWXU) >> 6;
101 			goto common;
102 
103 		case 'g':
104 			value = (newmode & S_IRWXG) >> 3;
105 			goto common;
106 
107 		case 'o':
108 			value = newmode & S_IRWXO;
109 common:			if (set->cmd2 & CMD2_CLR) {
110 				clrval =
111 				    (set->cmd2 & CMD2_SET) ?  S_IRWXO : value;
112 				if (set->cmd2 & CMD2_UBITS)
113 					newmode &= ~((clrval<<6) & set->bits);
114 				if (set->cmd2 & CMD2_GBITS)
115 					newmode &= ~((clrval<<3) & set->bits);
116 				if (set->cmd2 & CMD2_OBITS)
117 					newmode &= ~(clrval & set->bits);
118 			}
119 			if (set->cmd2 & CMD2_SET) {
120 				if (set->cmd2 & CMD2_UBITS)
121 					newmode |= (value<<6) & set->bits;
122 				if (set->cmd2 & CMD2_GBITS)
123 					newmode |= (value<<3) & set->bits;
124 				if (set->cmd2 & CMD2_OBITS)
125 					newmode |= value & set->bits;
126 			}
127 			break;
128 
129 		case '+':
130 			newmode |= set->bits;
131 			break;
132 
133 		case '-':
134 			newmode &= ~set->bits;
135 			break;
136 
137 		case 'X':
138 			if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
139 				newmode |= set->bits;
140 			break;
141 
142 		case '\0':
143 		default:
144 #ifdef SETMODE_DEBUG
145 			(void)printf("getmode:%04o -> %04o\n", omode, newmode);
146 #endif
147 			return (newmode);
148 		}
149 }
150 
151 #define	ADDCMD(a, b, c, d)						\
152 	if (set >= endset) {						\
153 		BITCMD *newset;						\
154 		setlen += SET_LEN_INCR;					\
155 		newset = realloc(saveset, sizeof(BITCMD) * setlen);	\
156 		if (newset == NULL)					\
157 			goto out;					\
158 		set = newset + (set - saveset);				\
159 		saveset = newset;					\
160 		endset = newset + (setlen - 2);				\
161 	}								\
162 	set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d))
163 
164 #define	STANDARD_BITS	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
165 
166 void *
167 setmode(const char *p)
168 {
169 	int serrno;
170 	char op, *ep;
171 	BITCMD *set, *saveset, *endset;
172 	sigset_t sigset, sigoset;
173 	mode_t mask, perm, permXbits, who;
174 	long perml;
175 	int equalopdone;
176 	int setlen;
177 
178 	if (!*p) {
179 		errno = EINVAL;
180 		return (NULL);
181 	}
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((unsigned char)*p)) {
207 		errno = 0;
208 		perml = strtol(p, &ep, 8);
209 		if (*ep) {
210 			errno = EINVAL;
211 			goto out;
212 		}
213 		if (errno == ERANGE && (perml == LONG_MAX || perml == LONG_MIN))
214 			goto out;
215 		if (perml & ~(STANDARD_BITS|S_ISTXT)) {
216 			errno = EINVAL;
217 			goto out;
218 		}
219 		perm = (mode_t)perml;
220 		ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
221 		set->cmd = 0;
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 	equalopdone = 0;
230 	for (;;) {
231 		/* First, find out which bits might be modified. */
232 		for (who = 0;; ++p) {
233 			switch (*p) {
234 			case 'a':
235 				who |= STANDARD_BITS;
236 				break;
237 			case 'u':
238 				who |= S_ISUID|S_IRWXU;
239 				break;
240 			case 'g':
241 				who |= S_ISGID|S_IRWXG;
242 				break;
243 			case 'o':
244 				who |= S_IRWXO;
245 				break;
246 			default:
247 				goto getop;
248 			}
249 		}
250 
251 getop:		if ((op = *p++) != '+' && op != '-' && op != '=') {
252 			errno = EINVAL;
253 			goto out;
254 		}
255 		if (op == '=')
256 			equalopdone = 0;
257 
258 		who &= ~S_ISTXT;
259 		for (perm = 0, permXbits = 0;; ++p) {
260 			switch (*p) {
261 			case 'r':
262 				perm |= S_IRUSR|S_IRGRP|S_IROTH;
263 				break;
264 			case 's':
265 				/* If only "other" bits ignore set-id. */
266 				if (!who || who & ~S_IRWXO)
267 					perm |= S_ISUID|S_ISGID;
268 				break;
269 			case 't':
270 				/* If only "other" bits ignore sticky. */
271 				if (!who || 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 out:
343 	serrno = errno;
344 	free(saveset);
345 	errno = serrno;
346 	return NULL;
347 }
348 
349 static BITCMD *
350 addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask)
351 {
352 	switch (op) {
353 	case '=':
354 		set->cmd = '-';
355 		set->bits = who ? who : STANDARD_BITS;
356 		set++;
357 
358 		op = '+';
359 		/* FALLTHROUGH */
360 	case '+':
361 	case '-':
362 	case 'X':
363 		set->cmd = op;
364 		set->bits = (who ? who : mask) & oparg;
365 		break;
366 
367 	case 'u':
368 	case 'g':
369 	case 'o':
370 		set->cmd = op;
371 		if (who) {
372 			set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
373 				    ((who & S_IRGRP) ? CMD2_GBITS : 0) |
374 				    ((who & S_IROTH) ? CMD2_OBITS : 0);
375 			set->bits = (mode_t)~0;
376 		} else {
377 			set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
378 			set->bits = mask;
379 		}
380 
381 		if (oparg == '+')
382 			set->cmd2 |= CMD2_SET;
383 		else if (oparg == '-')
384 			set->cmd2 |= CMD2_CLR;
385 		else if (oparg == '=')
386 			set->cmd2 |= CMD2_SET|CMD2_CLR;
387 		break;
388 	}
389 	return (set + 1);
390 }
391 
392 #ifdef SETMODE_DEBUG
393 static void
394 dumpmode(BITCMD *set)
395 {
396 	for (; set->cmd; ++set)
397 		(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
398 		    set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
399 		    set->cmd2 & CMD2_CLR ? " CLR" : "",
400 		    set->cmd2 & CMD2_SET ? " SET" : "",
401 		    set->cmd2 & CMD2_UBITS ? " UBITS" : "",
402 		    set->cmd2 & CMD2_GBITS ? " GBITS" : "",
403 		    set->cmd2 & CMD2_OBITS ? " OBITS" : "");
404 }
405 #endif
406 
407 /*
408  * Given an array of bitcmd structures, compress by compacting consecutive
409  * '+', '-' and 'X' commands into at most 3 commands, one of each.  The 'u',
410  * 'g' and 'o' commands continue to be separate.  They could probably be
411  * compacted, but it's not worth the effort.
412  */
413 static void
414 compress_mode(BITCMD *set)
415 {
416 	BITCMD *nset;
417 	int setbits, clrbits, Xbits, op;
418 
419 	for (nset = set;;) {
420 		/* Copy over any 'u', 'g' and 'o' commands. */
421 		while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
422 			*set++ = *nset++;
423 			if (!op)
424 				return;
425 		}
426 
427 		for (setbits = clrbits = Xbits = 0;; nset++) {
428 			if ((op = nset->cmd) == '-') {
429 				clrbits |= nset->bits;
430 				setbits &= ~nset->bits;
431 				Xbits &= ~nset->bits;
432 			} else if (op == '+') {
433 				setbits |= nset->bits;
434 				clrbits &= ~nset->bits;
435 				Xbits &= ~nset->bits;
436 			} else if (op == 'X')
437 				Xbits |= nset->bits & ~setbits;
438 			else
439 				break;
440 		}
441 		if (clrbits) {
442 			set->cmd = '-';
443 			set->cmd2 = 0;
444 			set->bits = clrbits;
445 			set++;
446 		}
447 		if (setbits) {
448 			set->cmd = '+';
449 			set->cmd2 = 0;
450 			set->bits = setbits;
451 			set++;
452 		}
453 		if (Xbits) {
454 			set->cmd = 'X';
455 			set->cmd2 = 0;
456 			set->bits = Xbits;
457 			set++;
458 		}
459 	}
460 }
461