xref: /freebsd/lib/libc/gen/setmode.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1989, 1993, 1994
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes  * Dave Borman at Cray Research, Inc.
958f0484fSRodney W. Grimes  *
1058f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes  * are met:
1358f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes  *    without specific prior written permission.
2158f0484fSRodney W. Grimes  *
2258f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes  * SUCH DAMAGE.
3358f0484fSRodney W. Grimes  */
3458f0484fSRodney W. Grimes 
35d201fe46SDaniel Eischen #include "namespace.h"
3658f0484fSRodney W. Grimes #include <sys/types.h>
3758f0484fSRodney W. Grimes #include <sys/stat.h>
3873cc817eSJilles Tjoelker #include <sys/sysctl.h>
3958f0484fSRodney W. Grimes 
4058f0484fSRodney W. Grimes #include <ctype.h>
41e382e86bSPedro F. Giffuni #include <errno.h>
42e382e86bSPedro F. Giffuni #include <limits.h>
4358f0484fSRodney W. Grimes #include <signal.h>
4458f0484fSRodney W. Grimes #include <stddef.h>
4558f0484fSRodney W. Grimes #include <stdlib.h>
46215d1a9eSMike Heffner #include <unistd.h>
4758f0484fSRodney W. Grimes 
4858f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG
4958f0484fSRodney W. Grimes #include <stdio.h>
5058f0484fSRodney W. Grimes #endif
51d201fe46SDaniel Eischen #include "un-namespace.h"
52bd6060a1SKonstantin Belousov #include "libc_private.h"
5358f0484fSRodney W. Grimes 
5458f0484fSRodney W. Grimes #define	SET_LEN	6		/* initial # of bitcmd struct to malloc */
5558f0484fSRodney W. Grimes #define	SET_LEN_INCR 4		/* # of bitcmd structs to add as needed */
5658f0484fSRodney W. Grimes 
5758f0484fSRodney W. Grimes typedef struct bitcmd {
5858f0484fSRodney W. Grimes 	char	cmd;
5958f0484fSRodney W. Grimes 	char	cmd2;
6058f0484fSRodney W. Grimes 	mode_t	bits;
6158f0484fSRodney W. Grimes } BITCMD;
6258f0484fSRodney W. Grimes 
6358f0484fSRodney W. Grimes #define	CMD2_CLR	0x01
6458f0484fSRodney W. Grimes #define	CMD2_SET	0x02
6558f0484fSRodney W. Grimes #define	CMD2_GBITS	0x04
6658f0484fSRodney W. Grimes #define	CMD2_OBITS	0x08
6758f0484fSRodney W. Grimes #define	CMD2_UBITS	0x10
6858f0484fSRodney W. Grimes 
69*9053c1a4SAlex Richardson static mode_t	 get_current_umask(void);
70e382e86bSPedro F. Giffuni static BITCMD	*addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t);
71b231cb39SDavid E. O'Brien static void	 compress_mode(BITCMD *);
7258f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG
73b231cb39SDavid E. O'Brien static void	 dumpmode(BITCMD *);
7458f0484fSRodney W. Grimes #endif
7558f0484fSRodney W. Grimes 
7658f0484fSRodney W. Grimes /*
7758f0484fSRodney W. Grimes  * Given the old mode and an array of bitcmd structures, apply the operations
7858f0484fSRodney W. Grimes  * described in the bitcmd structures to the old mode, and return the new mode.
7958f0484fSRodney W. Grimes  * Note that there is no '=' command; a strict assignment is just a '-' (clear
8058f0484fSRodney W. Grimes  * bits) followed by a '+' (set bits).
8158f0484fSRodney W. Grimes  */
8258f0484fSRodney W. Grimes mode_t
getmode(const void * bbox,mode_t omode)83fd42c4d8SStefan Farfeleder getmode(const void *bbox, mode_t omode)
8458f0484fSRodney W. Grimes {
85215d1a9eSMike Heffner 	const BITCMD *set;
86b231cb39SDavid E. O'Brien 	mode_t clrval, newmode, value;
8758f0484fSRodney W. Grimes 
88215d1a9eSMike Heffner 	set = (const BITCMD *)bbox;
8958f0484fSRodney W. Grimes 	newmode = omode;
9058f0484fSRodney W. Grimes 	for (value = 0;; set++)
9158f0484fSRodney W. Grimes 		switch(set->cmd) {
9258f0484fSRodney W. Grimes 		/*
9358f0484fSRodney W. Grimes 		 * When copying the user, group or other bits around, we "know"
9458f0484fSRodney W. Grimes 		 * where the bits are in the mode so that we can do shifts to
9558f0484fSRodney W. Grimes 		 * copy them around.  If we don't use shifts, it gets real
9658f0484fSRodney W. Grimes 		 * grundgy with lots of single bit checks and bit sets.
9758f0484fSRodney W. Grimes 		 */
9858f0484fSRodney W. Grimes 		case 'u':
9958f0484fSRodney W. Grimes 			value = (newmode & S_IRWXU) >> 6;
10058f0484fSRodney W. Grimes 			goto common;
10158f0484fSRodney W. Grimes 
10258f0484fSRodney W. Grimes 		case 'g':
10358f0484fSRodney W. Grimes 			value = (newmode & S_IRWXG) >> 3;
10458f0484fSRodney W. Grimes 			goto common;
10558f0484fSRodney W. Grimes 
10658f0484fSRodney W. Grimes 		case 'o':
10758f0484fSRodney W. Grimes 			value = newmode & S_IRWXO;
10858f0484fSRodney W. Grimes common:			if (set->cmd2 & CMD2_CLR) {
10958f0484fSRodney W. Grimes 				clrval =
11058f0484fSRodney W. Grimes 				    (set->cmd2 & CMD2_SET) ?  S_IRWXO : value;
11158f0484fSRodney W. Grimes 				if (set->cmd2 & CMD2_UBITS)
11258f0484fSRodney W. Grimes 					newmode &= ~((clrval<<6) & set->bits);
11358f0484fSRodney W. Grimes 				if (set->cmd2 & CMD2_GBITS)
11458f0484fSRodney W. Grimes 					newmode &= ~((clrval<<3) & set->bits);
11558f0484fSRodney W. Grimes 				if (set->cmd2 & CMD2_OBITS)
11658f0484fSRodney W. Grimes 					newmode &= ~(clrval & set->bits);
11758f0484fSRodney W. Grimes 			}
11858f0484fSRodney W. Grimes 			if (set->cmd2 & CMD2_SET) {
11958f0484fSRodney W. Grimes 				if (set->cmd2 & CMD2_UBITS)
12058f0484fSRodney W. Grimes 					newmode |= (value<<6) & set->bits;
12158f0484fSRodney W. Grimes 				if (set->cmd2 & CMD2_GBITS)
12258f0484fSRodney W. Grimes 					newmode |= (value<<3) & set->bits;
12358f0484fSRodney W. Grimes 				if (set->cmd2 & CMD2_OBITS)
12458f0484fSRodney W. Grimes 					newmode |= value & set->bits;
12558f0484fSRodney W. Grimes 			}
12658f0484fSRodney W. Grimes 			break;
12758f0484fSRodney W. Grimes 
12858f0484fSRodney W. Grimes 		case '+':
12958f0484fSRodney W. Grimes 			newmode |= set->bits;
13058f0484fSRodney W. Grimes 			break;
13158f0484fSRodney W. Grimes 
13258f0484fSRodney W. Grimes 		case '-':
13358f0484fSRodney W. Grimes 			newmode &= ~set->bits;
13458f0484fSRodney W. Grimes 			break;
13558f0484fSRodney W. Grimes 
13658f0484fSRodney W. Grimes 		case 'X':
13758f0484fSRodney W. Grimes 			if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
13858f0484fSRodney W. Grimes 				newmode |= set->bits;
13958f0484fSRodney W. Grimes 			break;
14058f0484fSRodney W. Grimes 
14158f0484fSRodney W. Grimes 		case '\0':
14258f0484fSRodney W. Grimes 		default:
14358f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG
14458f0484fSRodney W. Grimes 			(void)printf("getmode:%04o -> %04o\n", omode, newmode);
14558f0484fSRodney W. Grimes #endif
14658f0484fSRodney W. Grimes 			return (newmode);
14758f0484fSRodney W. Grimes 		}
14858f0484fSRodney W. Grimes }
14958f0484fSRodney W. Grimes 
15058f0484fSRodney W. Grimes #define	ADDCMD(a, b, c, d)						\
15158f0484fSRodney W. Grimes 	if (set >= endset) {						\
152b231cb39SDavid E. O'Brien 		BITCMD *newset;						\
15358f0484fSRodney W. Grimes 		setlen += SET_LEN_INCR;					\
1549f36610fSPedro F. Giffuni 		newset = reallocarray(saveset, setlen, sizeof(BITCMD));	\
155e382e86bSPedro F. Giffuni 		if (newset == NULL)					\
156e382e86bSPedro F. Giffuni 			goto out;					\
15758f0484fSRodney W. Grimes 		set = newset + (set - saveset);				\
15858f0484fSRodney W. Grimes 		saveset = newset;					\
15958f0484fSRodney W. Grimes 		endset = newset + (setlen - 2);				\
16058f0484fSRodney W. Grimes 	}								\
161e382e86bSPedro F. Giffuni 	set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d))
16258f0484fSRodney W. Grimes 
16358f0484fSRodney W. Grimes #define	STANDARD_BITS	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
16458f0484fSRodney W. Grimes 
16558f0484fSRodney W. Grimes void *
setmode(const char * p)166fd42c4d8SStefan Farfeleder setmode(const char *p)
16758f0484fSRodney W. Grimes {
168e382e86bSPedro F. Giffuni 	int serrno;
169ad4f1706SMike Heffner 	char op, *ep;
17058f0484fSRodney W. Grimes 	BITCMD *set, *saveset, *endset;
171e382e86bSPedro F. Giffuni 	mode_t mask, perm, permXbits, who;
172ad4f1706SMike Heffner 	long perml;
173e382e86bSPedro F. Giffuni 	int equalopdone;
174ada46906SPedro F. Giffuni 	u_int setlen;
17558f0484fSRodney W. Grimes 
176e382e86bSPedro F. Giffuni 	if (!*p) {
177e382e86bSPedro F. Giffuni 		errno = EINVAL;
17858f0484fSRodney W. Grimes 		return (NULL);
179e382e86bSPedro F. Giffuni 	}
18058f0484fSRodney W. Grimes 
18158f0484fSRodney W. Grimes 	/*
18258f0484fSRodney W. Grimes 	 * Get a copy of the mask for the permissions that are mask relative.
18373cc817eSJilles Tjoelker 	 * Flip the bits, we want what's not set.
18458f0484fSRodney W. Grimes 	 */
185*9053c1a4SAlex Richardson 	mask = ~get_current_umask();
18658f0484fSRodney W. Grimes 
18758f0484fSRodney W. Grimes 	setlen = SET_LEN + 2;
18858f0484fSRodney W. Grimes 
189ada46906SPedro F. Giffuni 	if ((set = malloc(setlen * sizeof(BITCMD))) == NULL)
19058f0484fSRodney W. Grimes 		return (NULL);
19158f0484fSRodney W. Grimes 	saveset = set;
19258f0484fSRodney W. Grimes 	endset = set + (setlen - 2);
19358f0484fSRodney W. Grimes 
19458f0484fSRodney W. Grimes 	/*
19558f0484fSRodney W. Grimes 	 * If an absolute number, get it and return; disallow non-octal digits
19658f0484fSRodney W. Grimes 	 * or illegal bits.
19758f0484fSRodney W. Grimes 	 */
198d67f4d91SAndrey A. Chernov 	if (isdigit((unsigned char)*p)) {
199e382e86bSPedro F. Giffuni 		errno = 0;
200ad4f1706SMike Heffner 		perml = strtol(p, &ep, 8);
201e382e86bSPedro F. Giffuni 		if (*ep) {
202e382e86bSPedro F. Giffuni 			errno = EINVAL;
203e382e86bSPedro F. Giffuni 			goto out;
204e382e86bSPedro F. Giffuni 		}
205e382e86bSPedro F. Giffuni 		if (errno == ERANGE && (perml == LONG_MAX || perml == LONG_MIN))
206e382e86bSPedro F. Giffuni 			goto out;
207e382e86bSPedro F. Giffuni 		if (perml & ~(STANDARD_BITS|S_ISTXT)) {
208e382e86bSPedro F. Giffuni 			errno = EINVAL;
209e382e86bSPedro F. Giffuni 			goto out;
21058f0484fSRodney W. Grimes 		}
211ad4f1706SMike Heffner 		perm = (mode_t)perml;
21258f0484fSRodney W. Grimes 		ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
213ad4f1706SMike Heffner 		set->cmd = 0;
21458f0484fSRodney W. Grimes 		return (saveset);
21558f0484fSRodney W. Grimes 	}
21658f0484fSRodney W. Grimes 
21758f0484fSRodney W. Grimes 	/*
21858f0484fSRodney W. Grimes 	 * Build list of structures to set/clear/copy bits as described by
21958f0484fSRodney W. Grimes 	 * each clause of the symbolic mode.
22058f0484fSRodney W. Grimes 	 */
221e382e86bSPedro F. Giffuni 	equalopdone = 0;
22258f0484fSRodney W. Grimes 	for (;;) {
22358f0484fSRodney W. Grimes 		/* First, find out which bits might be modified. */
22458f0484fSRodney W. Grimes 		for (who = 0;; ++p) {
22558f0484fSRodney W. Grimes 			switch (*p) {
22658f0484fSRodney W. Grimes 			case 'a':
22758f0484fSRodney W. Grimes 				who |= STANDARD_BITS;
22858f0484fSRodney W. Grimes 				break;
22958f0484fSRodney W. Grimes 			case 'u':
23058f0484fSRodney W. Grimes 				who |= S_ISUID|S_IRWXU;
23158f0484fSRodney W. Grimes 				break;
23258f0484fSRodney W. Grimes 			case 'g':
23358f0484fSRodney W. Grimes 				who |= S_ISGID|S_IRWXG;
23458f0484fSRodney W. Grimes 				break;
23558f0484fSRodney W. Grimes 			case 'o':
23658f0484fSRodney W. Grimes 				who |= S_IRWXO;
23758f0484fSRodney W. Grimes 				break;
23858f0484fSRodney W. Grimes 			default:
23958f0484fSRodney W. Grimes 				goto getop;
24058f0484fSRodney W. Grimes 			}
24158f0484fSRodney W. Grimes 		}
24258f0484fSRodney W. Grimes 
24358f0484fSRodney W. Grimes getop:		if ((op = *p++) != '+' && op != '-' && op != '=') {
244e382e86bSPedro F. Giffuni 			errno = EINVAL;
245e382e86bSPedro F. Giffuni 			goto out;
24658f0484fSRodney W. Grimes 		}
24758f0484fSRodney W. Grimes 		if (op == '=')
24858f0484fSRodney W. Grimes 			equalopdone = 0;
24958f0484fSRodney W. Grimes 
25058f0484fSRodney W. Grimes 		who &= ~S_ISTXT;
25158f0484fSRodney W. Grimes 		for (perm = 0, permXbits = 0;; ++p) {
25258f0484fSRodney W. Grimes 			switch (*p) {
25358f0484fSRodney W. Grimes 			case 'r':
25458f0484fSRodney W. Grimes 				perm |= S_IRUSR|S_IRGRP|S_IROTH;
25558f0484fSRodney W. Grimes 				break;
25658f0484fSRodney W. Grimes 			case 's':
25758f0484fSRodney W. Grimes 				/* If only "other" bits ignore set-id. */
258bd8beb9bSRuslan Ermilov 				if (!who || who & ~S_IRWXO)
25958f0484fSRodney W. Grimes 					perm |= S_ISUID|S_ISGID;
26058f0484fSRodney W. Grimes 				break;
26158f0484fSRodney W. Grimes 			case 't':
26258f0484fSRodney W. Grimes 				/* If only "other" bits ignore sticky. */
263bd8beb9bSRuslan Ermilov 				if (!who || who & ~S_IRWXO) {
26458f0484fSRodney W. Grimes 					who |= S_ISTXT;
26558f0484fSRodney W. Grimes 					perm |= S_ISTXT;
26658f0484fSRodney W. Grimes 				}
26758f0484fSRodney W. Grimes 				break;
26858f0484fSRodney W. Grimes 			case 'w':
26958f0484fSRodney W. Grimes 				perm |= S_IWUSR|S_IWGRP|S_IWOTH;
27058f0484fSRodney W. Grimes 				break;
27158f0484fSRodney W. Grimes 			case 'X':
27258f0484fSRodney W. Grimes 				permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
27358f0484fSRodney W. Grimes 				break;
27458f0484fSRodney W. Grimes 			case 'x':
27558f0484fSRodney W. Grimes 				perm |= S_IXUSR|S_IXGRP|S_IXOTH;
27658f0484fSRodney W. Grimes 				break;
27758f0484fSRodney W. Grimes 			case 'u':
27858f0484fSRodney W. Grimes 			case 'g':
27958f0484fSRodney W. Grimes 			case 'o':
28058f0484fSRodney W. Grimes 				/*
28158f0484fSRodney W. Grimes 				 * When ever we hit 'u', 'g', or 'o', we have
28258f0484fSRodney W. Grimes 				 * to flush out any partial mode that we have,
28358f0484fSRodney W. Grimes 				 * and then do the copying of the mode bits.
28458f0484fSRodney W. Grimes 				 */
28558f0484fSRodney W. Grimes 				if (perm) {
28658f0484fSRodney W. Grimes 					ADDCMD(op, who, perm, mask);
28758f0484fSRodney W. Grimes 					perm = 0;
28858f0484fSRodney W. Grimes 				}
28958f0484fSRodney W. Grimes 				if (op == '=')
29058f0484fSRodney W. Grimes 					equalopdone = 1;
29158f0484fSRodney W. Grimes 				if (op == '+' && permXbits) {
29258f0484fSRodney W. Grimes 					ADDCMD('X', who, permXbits, mask);
29358f0484fSRodney W. Grimes 					permXbits = 0;
29458f0484fSRodney W. Grimes 				}
29558f0484fSRodney W. Grimes 				ADDCMD(*p, who, op, mask);
29658f0484fSRodney W. Grimes 				break;
29758f0484fSRodney W. Grimes 
29858f0484fSRodney W. Grimes 			default:
29958f0484fSRodney W. Grimes 				/*
30058f0484fSRodney W. Grimes 				 * Add any permissions that we haven't already
30158f0484fSRodney W. Grimes 				 * done.
30258f0484fSRodney W. Grimes 				 */
30358f0484fSRodney W. Grimes 				if (perm || (op == '=' && !equalopdone)) {
30458f0484fSRodney W. Grimes 					if (op == '=')
30558f0484fSRodney W. Grimes 						equalopdone = 1;
30658f0484fSRodney W. Grimes 					ADDCMD(op, who, perm, mask);
30758f0484fSRodney W. Grimes 					perm = 0;
30858f0484fSRodney W. Grimes 				}
30958f0484fSRodney W. Grimes 				if (permXbits) {
31058f0484fSRodney W. Grimes 					ADDCMD('X', who, permXbits, mask);
31158f0484fSRodney W. Grimes 					permXbits = 0;
31258f0484fSRodney W. Grimes 				}
31358f0484fSRodney W. Grimes 				goto apply;
31458f0484fSRodney W. Grimes 			}
31558f0484fSRodney W. Grimes 		}
31658f0484fSRodney W. Grimes 
31758f0484fSRodney W. Grimes apply:		if (!*p)
31858f0484fSRodney W. Grimes 			break;
31958f0484fSRodney W. Grimes 		if (*p != ',')
32058f0484fSRodney W. Grimes 			goto getop;
32158f0484fSRodney W. Grimes 		++p;
32258f0484fSRodney W. Grimes 	}
32358f0484fSRodney W. Grimes 	set->cmd = 0;
32458f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG
32558f0484fSRodney W. Grimes 	(void)printf("Before compress_mode()\n");
32658f0484fSRodney W. Grimes 	dumpmode(saveset);
32758f0484fSRodney W. Grimes #endif
32858f0484fSRodney W. Grimes 	compress_mode(saveset);
32958f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG
33058f0484fSRodney W. Grimes 	(void)printf("After compress_mode()\n");
33158f0484fSRodney W. Grimes 	dumpmode(saveset);
33258f0484fSRodney W. Grimes #endif
33358f0484fSRodney W. Grimes 	return (saveset);
334e382e86bSPedro F. Giffuni out:
335e382e86bSPedro F. Giffuni 	serrno = errno;
336e382e86bSPedro F. Giffuni 	free(saveset);
337e382e86bSPedro F. Giffuni 	errno = serrno;
338e382e86bSPedro F. Giffuni 	return NULL;
33958f0484fSRodney W. Grimes }
34058f0484fSRodney W. Grimes 
34173cc817eSJilles Tjoelker static mode_t
get_current_umask(void)342*9053c1a4SAlex Richardson get_current_umask(void)
34373cc817eSJilles Tjoelker {
34473cc817eSJilles Tjoelker 	sigset_t sigset, sigoset;
34573cc817eSJilles Tjoelker 	size_t len;
34673cc817eSJilles Tjoelker 	mode_t mask;
34773cc817eSJilles Tjoelker 	u_short smask;
34873cc817eSJilles Tjoelker 
349*9053c1a4SAlex Richardson #ifdef KERN_PROC_UMASK
35073cc817eSJilles Tjoelker 	/*
35173cc817eSJilles Tjoelker 	 * First try requesting the umask without temporarily modifying it.
35273cc817eSJilles Tjoelker 	 * Note that this does not work if the sysctl
35373cc817eSJilles Tjoelker 	 * security.bsd.unprivileged_proc_debug is set to 0.
35473cc817eSJilles Tjoelker 	 */
35573cc817eSJilles Tjoelker 	len = sizeof(smask);
356cee09850SBartek Rutkowski 	if (sysctl((int[4]){ CTL_KERN, KERN_PROC, KERN_PROC_UMASK, 0 },
35773cc817eSJilles Tjoelker 	    4, &smask, &len, NULL, 0) == 0)
35873cc817eSJilles Tjoelker 		return (smask);
359*9053c1a4SAlex Richardson #endif
36073cc817eSJilles Tjoelker 	/*
36173cc817eSJilles Tjoelker 	 * Since it's possible that the caller is opening files inside a signal
36273cc817eSJilles Tjoelker 	 * handler, protect them as best we can.
36373cc817eSJilles Tjoelker 	 */
36473cc817eSJilles Tjoelker 	sigfillset(&sigset);
365bd6060a1SKonstantin Belousov 	(void)__libc_sigprocmask(SIG_BLOCK, &sigset, &sigoset);
36673cc817eSJilles Tjoelker 	(void)umask(mask = umask(0));
367bd6060a1SKonstantin Belousov 	(void)__libc_sigprocmask(SIG_SETMASK, &sigoset, NULL);
36873cc817eSJilles Tjoelker 	return (mask);
36973cc817eSJilles Tjoelker }
37073cc817eSJilles Tjoelker 
37158f0484fSRodney W. Grimes static BITCMD *
addcmd(BITCMD * set,mode_t op,mode_t who,mode_t oparg,mode_t mask)372e382e86bSPedro F. Giffuni addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask)
37358f0484fSRodney W. Grimes {
37458f0484fSRodney W. Grimes 	switch (op) {
37558f0484fSRodney W. Grimes 	case '=':
37658f0484fSRodney W. Grimes 		set->cmd = '-';
37758f0484fSRodney W. Grimes 		set->bits = who ? who : STANDARD_BITS;
37858f0484fSRodney W. Grimes 		set++;
37958f0484fSRodney W. Grimes 
38058f0484fSRodney W. Grimes 		op = '+';
38158f0484fSRodney W. Grimes 		/* FALLTHROUGH */
38258f0484fSRodney W. Grimes 	case '+':
38358f0484fSRodney W. Grimes 	case '-':
38458f0484fSRodney W. Grimes 	case 'X':
38558f0484fSRodney W. Grimes 		set->cmd = op;
38658f0484fSRodney W. Grimes 		set->bits = (who ? who : mask) & oparg;
38758f0484fSRodney W. Grimes 		break;
38858f0484fSRodney W. Grimes 
38958f0484fSRodney W. Grimes 	case 'u':
39058f0484fSRodney W. Grimes 	case 'g':
39158f0484fSRodney W. Grimes 	case 'o':
39258f0484fSRodney W. Grimes 		set->cmd = op;
39358f0484fSRodney W. Grimes 		if (who) {
39458f0484fSRodney W. Grimes 			set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
39558f0484fSRodney W. Grimes 				    ((who & S_IRGRP) ? CMD2_GBITS : 0) |
39658f0484fSRodney W. Grimes 				    ((who & S_IROTH) ? CMD2_OBITS : 0);
397215d1a9eSMike Heffner 			set->bits = (mode_t)~0;
39858f0484fSRodney W. Grimes 		} else {
39958f0484fSRodney W. Grimes 			set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
40058f0484fSRodney W. Grimes 			set->bits = mask;
40158f0484fSRodney W. Grimes 		}
40258f0484fSRodney W. Grimes 
40358f0484fSRodney W. Grimes 		if (oparg == '+')
40458f0484fSRodney W. Grimes 			set->cmd2 |= CMD2_SET;
40558f0484fSRodney W. Grimes 		else if (oparg == '-')
40658f0484fSRodney W. Grimes 			set->cmd2 |= CMD2_CLR;
40758f0484fSRodney W. Grimes 		else if (oparg == '=')
40858f0484fSRodney W. Grimes 			set->cmd2 |= CMD2_SET|CMD2_CLR;
40958f0484fSRodney W. Grimes 		break;
41058f0484fSRodney W. Grimes 	}
41158f0484fSRodney W. Grimes 	return (set + 1);
41258f0484fSRodney W. Grimes }
41358f0484fSRodney W. Grimes 
41458f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG
41558f0484fSRodney W. Grimes static void
dumpmode(BITCMD * set)416fd42c4d8SStefan Farfeleder dumpmode(BITCMD *set)
41758f0484fSRodney W. Grimes {
41858f0484fSRodney W. Grimes 	for (; set->cmd; ++set)
41958f0484fSRodney W. Grimes 		(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
42058f0484fSRodney W. Grimes 		    set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
42158f0484fSRodney W. Grimes 		    set->cmd2 & CMD2_CLR ? " CLR" : "",
42258f0484fSRodney W. Grimes 		    set->cmd2 & CMD2_SET ? " SET" : "",
42358f0484fSRodney W. Grimes 		    set->cmd2 & CMD2_UBITS ? " UBITS" : "",
42458f0484fSRodney W. Grimes 		    set->cmd2 & CMD2_GBITS ? " GBITS" : "",
42558f0484fSRodney W. Grimes 		    set->cmd2 & CMD2_OBITS ? " OBITS" : "");
42658f0484fSRodney W. Grimes }
42758f0484fSRodney W. Grimes #endif
42858f0484fSRodney W. Grimes 
42958f0484fSRodney W. Grimes /*
43058f0484fSRodney W. Grimes  * Given an array of bitcmd structures, compress by compacting consecutive
43158f0484fSRodney W. Grimes  * '+', '-' and 'X' commands into at most 3 commands, one of each.  The 'u',
43258f0484fSRodney W. Grimes  * 'g' and 'o' commands continue to be separate.  They could probably be
43358f0484fSRodney W. Grimes  * compacted, but it's not worth the effort.
43458f0484fSRodney W. Grimes  */
4358b102407SPoul-Henning Kamp static void
compress_mode(BITCMD * set)436fd42c4d8SStefan Farfeleder compress_mode(BITCMD *set)
43758f0484fSRodney W. Grimes {
438b231cb39SDavid E. O'Brien 	BITCMD *nset;
439b231cb39SDavid E. O'Brien 	int setbits, clrbits, Xbits, op;
44058f0484fSRodney W. Grimes 
44158f0484fSRodney W. Grimes 	for (nset = set;;) {
44258f0484fSRodney W. Grimes 		/* Copy over any 'u', 'g' and 'o' commands. */
44358f0484fSRodney W. Grimes 		while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
44458f0484fSRodney W. Grimes 			*set++ = *nset++;
44558f0484fSRodney W. Grimes 			if (!op)
44658f0484fSRodney W. Grimes 				return;
44758f0484fSRodney W. Grimes 		}
44858f0484fSRodney W. Grimes 
44958f0484fSRodney W. Grimes 		for (setbits = clrbits = Xbits = 0;; nset++) {
45058f0484fSRodney W. Grimes 			if ((op = nset->cmd) == '-') {
45158f0484fSRodney W. Grimes 				clrbits |= nset->bits;
45258f0484fSRodney W. Grimes 				setbits &= ~nset->bits;
45358f0484fSRodney W. Grimes 				Xbits &= ~nset->bits;
45458f0484fSRodney W. Grimes 			} else if (op == '+') {
45558f0484fSRodney W. Grimes 				setbits |= nset->bits;
45658f0484fSRodney W. Grimes 				clrbits &= ~nset->bits;
45758f0484fSRodney W. Grimes 				Xbits &= ~nset->bits;
45858f0484fSRodney W. Grimes 			} else if (op == 'X')
45958f0484fSRodney W. Grimes 				Xbits |= nset->bits & ~setbits;
46058f0484fSRodney W. Grimes 			else
46158f0484fSRodney W. Grimes 				break;
46258f0484fSRodney W. Grimes 		}
46358f0484fSRodney W. Grimes 		if (clrbits) {
46458f0484fSRodney W. Grimes 			set->cmd = '-';
46558f0484fSRodney W. Grimes 			set->cmd2 = 0;
46658f0484fSRodney W. Grimes 			set->bits = clrbits;
46758f0484fSRodney W. Grimes 			set++;
46858f0484fSRodney W. Grimes 		}
46958f0484fSRodney W. Grimes 		if (setbits) {
47058f0484fSRodney W. Grimes 			set->cmd = '+';
47158f0484fSRodney W. Grimes 			set->cmd2 = 0;
47258f0484fSRodney W. Grimes 			set->bits = setbits;
47358f0484fSRodney W. Grimes 			set++;
47458f0484fSRodney W. Grimes 		}
47558f0484fSRodney W. Grimes 		if (Xbits) {
47658f0484fSRodney W. Grimes 			set->cmd = 'X';
47758f0484fSRodney W. Grimes 			set->cmd2 = 0;
47858f0484fSRodney W. Grimes 			set->bits = Xbits;
47958f0484fSRodney W. Grimes 			set++;
48058f0484fSRodney W. Grimes 		}
48158f0484fSRodney W. Grimes 	}
48258f0484fSRodney W. Grimes }
483