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