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