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)
34static 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: src/lib/libc/gen/setmode.c,v 1.11 2007/01/09 00:27:55 imp Exp $");
38
39#include "namespace.h"
40#include <sys/types.h>
41#include <sys/stat.h>
42
43#include <ctype.h>
44#include <signal.h>
45#include <stddef.h>
46#include <stdlib.h>
47#include <unistd.h>
48
49#ifdef SETMODE_DEBUG
50#include <stdio.h>
51#endif
52#include "un-namespace.h"
53
54#define	SET_LEN	6		/* initial # of bitcmd struct to malloc */
55#define	SET_LEN_INCR 4		/* # of bitcmd structs to add as needed */
56
57typedef struct bitcmd {
58	char	cmd;
59	char	cmd2;
60	mode_t	bits;
61} BITCMD;
62
63#define	CMD2_CLR	0x01
64#define	CMD2_SET	0x02
65#define	CMD2_GBITS	0x04
66#define	CMD2_OBITS	0x08
67#define	CMD2_UBITS	0x10
68
69#define	compress_mode	_sm_compress_mode
70
71static BITCMD	*addcmd(BITCMD *, int, int, int, u_int);
72__private_extern__ void		compress_mode(BITCMD *);
73#ifdef SETMODE_DEBUG
74static void	 dumpmode(BITCMD *);
75#endif
76
77#ifndef BUILDING_VARIANT
78/*
79 * Given the old mode and an array of bitcmd structures, apply the operations
80 * described in the bitcmd structures to the old mode, and return the new mode.
81 * Note that there is no '=' command; a strict assignment is just a '-' (clear
82 * bits) followed by a '+' (set bits).
83 */
84mode_t
85getmode(const void *bbox, mode_t omode)
86{
87	const BITCMD *set;
88	mode_t clrval, newmode, value;
89
90	set = (const BITCMD *)bbox;
91	newmode = omode;
92	for (value = 0;; set++)
93		switch(set->cmd) {
94		/*
95		 * When copying the user, group or other bits around, we "know"
96		 * where the bits are in the mode so that we can do shifts to
97		 * copy them around.  If we don't use shifts, it gets real
98		 * grundgy with lots of single bit checks and bit sets.
99		 */
100		case 'u':
101			value = (newmode & S_IRWXU) >> 6;
102			goto common;
103
104		case 'g':
105			value = (newmode & S_IRWXG) >> 3;
106			goto common;
107
108		case 'o':
109			value = newmode & S_IRWXO;
110common:			if (set->cmd2 & CMD2_CLR) {
111				clrval =
112				    (set->cmd2 & CMD2_SET) ?  S_IRWXO : value;
113				if (set->cmd2 & CMD2_UBITS)
114					newmode &= ~((clrval<<6) & set->bits);
115				if (set->cmd2 & CMD2_GBITS)
116					newmode &= ~((clrval<<3) & set->bits);
117				if (set->cmd2 & CMD2_OBITS)
118					newmode &= ~(clrval & set->bits);
119			}
120			if (set->cmd2 & CMD2_SET) {
121				if (set->cmd2 & CMD2_UBITS)
122					newmode |= (value<<6) & set->bits;
123				if (set->cmd2 & CMD2_GBITS)
124					newmode |= (value<<3) & set->bits;
125				if (set->cmd2 & CMD2_OBITS)
126					newmode |= value & set->bits;
127			}
128			break;
129
130		case '+':
131			newmode |= set->bits;
132			break;
133
134		case '-':
135			newmode &= ~set->bits;
136			break;
137
138		case 'X':
139			if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
140				newmode |= set->bits;
141			break;
142
143		case '\0':
144		default:
145#ifdef SETMODE_DEBUG
146			(void)printf("getmode:%04o -> %04o\n", omode, newmode);
147#endif
148			return (newmode);
149		}
150}
151#endif /* BUILDING_VARIANT */
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) {						\
159			if (saveset)					\
160				free(saveset);				\
161			saveset = NULL;					\
162			return (NULL);					\
163		}							\
164		set = newset + (set - saveset);				\
165		saveset = newset;					\
166		endset = newset + (setlen - 2);				\
167	}								\
168	set = addcmd(set, (a), (b), (c), (d))
169
170#ifndef VARIANT_LEGACY
171#define	STANDARD_BITS	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO|S_ISTXT)
172#else /* VARIANT_LEGACY */
173#define	STANDARD_BITS	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
174#endif /* !VARIANT_LEGACY */
175
176void *
177setmode(const char *p)
178{
179	int perm, who;
180	char op, *ep;
181	BITCMD *set, *saveset, *endset;
182	sigset_t sigset, sigoset;
183	mode_t mask;
184	int equalopdone=0, permXbits, setlen;
185	long perml;
186
187	if (!*p)
188		return (NULL);
189
190	/*
191	 * Get a copy of the mask for the permissions that are mask relative.
192	 * Flip the bits, we want what's not set.  Since it's possible that
193	 * the caller is opening files inside a signal handler, protect them
194	 * as best we can.
195	 */
196	sigfillset(&sigset);
197        (void)_sigprocmask(SIG_BLOCK, &sigset, &sigoset);
198	(void)umask(mask = umask(0));
199	mask = ~mask;
200        (void)_sigprocmask(SIG_SETMASK, &sigoset, NULL);
201
202	setlen = SET_LEN + 2;
203
204	if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
205		return (NULL);
206	saveset = set;
207	endset = set + (setlen - 2);
208
209	/*
210	 * If an absolute number, get it and return; disallow non-octal digits
211	 * or illegal bits.
212	 */
213	if (isdigit((unsigned char)*p)) {
214		perml = strtol(p, &ep, 8);
215#ifndef VARIANT_LEGACY
216		if (*ep || perml < 0 || perml & ~STANDARD_BITS)
217#else /* VARIANT_LEGACY */
218		if (*ep || perml < 0 || perml & ~(STANDARD_BITS|S_ISTXT))
219#endif /* !VARIANT_LEGACY */
220		{
221			free(saveset);
222			return (NULL);
223		}
224		perm = (mode_t)perml;
225#ifndef VARIANT_LEGACY
226		ADDCMD('=', STANDARD_BITS, perm, mask);
227#else /* VARIANT_LEGACY */
228		ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
229#endif /* !VARIANT_LEGACY */
230		set->cmd = 0;
231		return (saveset);
232	}
233
234	/*
235	 * Build list of structures to set/clear/copy bits as described by
236	 * each clause of the symbolic mode.
237	 */
238	for (;;) {
239		/* First, find out which bits might be modified. */
240		for (who = 0;; ++p) {
241			switch (*p) {
242			case 'a':
243				who |= STANDARD_BITS;
244				break;
245			case 'u':
246				who |= S_ISUID|S_IRWXU;
247				break;
248			case 'g':
249				who |= S_ISGID|S_IRWXG;
250				break;
251			case 'o':
252				who |= S_IRWXO;
253				break;
254			default:
255				goto getop;
256			}
257		}
258
259getop:		if ((op = *p++) != '+' && op != '-' && op != '=') {
260			free(saveset);
261			return (NULL);
262		}
263		if (op == '=')
264			equalopdone = 0;
265
266#ifdef VARIANT_LEGACY
267		who &= ~S_ISTXT;
268#endif /* VARIANT_LEGACY */
269		for (perm = 0, permXbits = 0;; ++p) {
270			switch (*p) {
271			case 'r':
272				perm |= S_IRUSR|S_IRGRP|S_IROTH;
273				break;
274			case 's':
275				/* If only "other" bits ignore set-id. */
276				if (!who || who & ~S_IRWXO)
277					perm |= S_ISUID|S_ISGID;
278				break;
279			case 't':
280				/* If only "other" bits ignore sticky. */
281				if (!who || who & ~S_IRWXO) {
282#ifdef VARIANT_LEGACY
283					who |= S_ISTXT;
284#endif /* VARIANT_LEGACY */
285					perm |= S_ISTXT;
286				}
287				break;
288			case 'w':
289				perm |= S_IWUSR|S_IWGRP|S_IWOTH;
290				break;
291			case 'X':
292				permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
293				break;
294			case 'x':
295				perm |= S_IXUSR|S_IXGRP|S_IXOTH;
296				break;
297			case 'u':
298			case 'g':
299			case 'o':
300				/*
301				 * When ever we hit 'u', 'g', or 'o', we have
302				 * to flush out any partial mode that we have,
303				 * and then do the copying of the mode bits.
304				 */
305				if (perm) {
306					ADDCMD(op, who, perm, mask);
307					perm = 0;
308				}
309				if (op == '=')
310					equalopdone = 1;
311				if (op == '+' && permXbits) {
312					ADDCMD('X', who, permXbits, mask);
313					permXbits = 0;
314				}
315				ADDCMD(*p, who, op, mask);
316				break;
317
318			default:
319				/*
320				 * Add any permissions that we haven't already
321				 * done.
322				 */
323				if (perm || (op == '=' && !equalopdone)) {
324					if (op == '=')
325						equalopdone = 1;
326					ADDCMD(op, who, perm, mask);
327					perm = 0;
328				}
329				if (permXbits) {
330					ADDCMD('X', who, permXbits, mask);
331					permXbits = 0;
332				}
333				goto apply;
334			}
335		}
336
337apply:		if (!*p)
338			break;
339		if (*p != ',')
340			goto getop;
341		++p;
342	}
343	set->cmd = 0;
344#ifdef SETMODE_DEBUG
345	(void)printf("Before compress_mode()\n");
346	dumpmode(saveset);
347#endif
348	compress_mode(saveset);
349#ifdef SETMODE_DEBUG
350	(void)printf("After compress_mode()\n");
351	dumpmode(saveset);
352#endif
353	return (saveset);
354}
355
356static BITCMD *
357addcmd(BITCMD *set, int op, int who, int oparg, u_int mask)
358{
359	switch (op) {
360	case '=':
361		set->cmd = '-';
362		set->bits = who ? who : STANDARD_BITS;
363		set++;
364
365		op = '+';
366		/* FALLTHROUGH */
367	case '+':
368	case '-':
369	case 'X':
370		set->cmd = op;
371		set->bits = (who ? who : mask) & oparg;
372		break;
373
374	case 'u':
375	case 'g':
376	case 'o':
377		set->cmd = op;
378		if (who) {
379			set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
380				    ((who & S_IRGRP) ? CMD2_GBITS : 0) |
381				    ((who & S_IROTH) ? CMD2_OBITS : 0);
382			set->bits = (mode_t)~0;
383		} else {
384			set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
385			set->bits = mask;
386		}
387
388		if (oparg == '+')
389			set->cmd2 |= CMD2_SET;
390		else if (oparg == '-')
391			set->cmd2 |= CMD2_CLR;
392		else if (oparg == '=')
393			set->cmd2 |= CMD2_SET|CMD2_CLR;
394		break;
395	}
396	return (set + 1);
397}
398
399#ifdef SETMODE_DEBUG
400static void
401dumpmode(BITCMD *set)
402{
403	for (; set->cmd; ++set)
404		(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
405		    set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
406		    set->cmd2 & CMD2_CLR ? " CLR" : "",
407		    set->cmd2 & CMD2_SET ? " SET" : "",
408		    set->cmd2 & CMD2_UBITS ? " UBITS" : "",
409		    set->cmd2 & CMD2_GBITS ? " GBITS" : "",
410		    set->cmd2 & CMD2_OBITS ? " OBITS" : "");
411}
412#endif
413
414#ifndef BUILDING_VARIANT
415/*
416 * Given an array of bitcmd structures, compress by compacting consecutive
417 * '+', '-' and 'X' commands into at most 3 commands, one of each.  The 'u',
418 * 'g' and 'o' commands continue to be separate.  They could probably be
419 * compacted, but it's not worth the effort.
420 */
421__private_extern__ void
422compress_mode(BITCMD *set)
423{
424	BITCMD *nset;
425	int setbits, clrbits, Xbits, op;
426
427	for (nset = set;;) {
428		/* Copy over any 'u', 'g' and 'o' commands. */
429		while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
430			*set++ = *nset++;
431			if (!op)
432				return;
433		}
434
435		for (setbits = clrbits = Xbits = 0;; nset++) {
436			if ((op = nset->cmd) == '-') {
437				clrbits |= nset->bits;
438				setbits &= ~nset->bits;
439				Xbits &= ~nset->bits;
440			} else if (op == '+') {
441				setbits |= nset->bits;
442				clrbits &= ~nset->bits;
443				Xbits &= ~nset->bits;
444			} else if (op == 'X')
445				Xbits |= nset->bits & ~setbits;
446			else
447				break;
448		}
449		if (clrbits) {
450			set->cmd = '-';
451			set->cmd2 = 0;
452			set->bits = clrbits;
453			set++;
454		}
455		if (setbits) {
456			set->cmd = '+';
457			set->cmd2 = 0;
458			set->bits = setbits;
459			set++;
460		}
461		if (Xbits) {
462			set->cmd = 'X';
463			set->cmd2 = 0;
464			set->bits = Xbits;
465			set++;
466		}
467	}
468}
469#endif /* BUILDING_VARIANT */
470