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