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