Debian Codegraph @

src/bsd/setmode.c

raw symbols SCIP
1 /* clang-format off */
2 /* $NetBSD: setmode.c,v 1.34 2012/06/25 22:32:43 abs Exp $ */
3
4 /*
5 * Copyright (c) 1989, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Dave Borman at Cray Research, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include "bsd/cdefs.h"
37 #include <sys/stat.h>
38 #include <sys/types.h>
39
40 #include <ctype.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <signal.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46
47 #ifdef SETMODE_DEBUG
48 #include <stdio.h>
49 #endif
50
51 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */
52 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
53
54 void *reallocarray(void *ptr, size_t nmemb, size_t size);
55
56 #ifndef S_ISTXT
57 #define S_ISTXT S_ISVTX
58 #endif
59
60 typedef struct bitcmd {
61 char cmd;
62 char cmd2;
63 mode_t bits;
64 } BITCMD;
65
66 #define CMD2_CLR 0x01
67 #define CMD2_SET 0x02
68 #define CMD2_GBITS 0x04
69 #define CMD2_OBITS 0x08
70 #define CMD2_UBITS 0x10
71
72 static BITCMD *addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t);
73 static void compress_mode(BITCMD *);
74 #ifdef SETMODE_DEBUG
75 static void dumpmode(BITCMD *);
76 #endif
77
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 */
84 mode_t
85 getmode(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;
110 common: 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
152 #define ADDCMD(a, b, c, d) do { \
153 if (set >= endset) { \
154 BITCMD *newset; \
155 setlen += SET_LEN_INCR; \
156 newset = (BITCMD*) reallocarray(saveset, setlen, sizeof(BITCMD)); \
157 if (newset == NULL) \
158 goto out; \
159 set = newset + (set - saveset); \
160 saveset = newset; \
161 endset = newset + (setlen - 2); \
162 } \
163 set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d)); \
164 } while (/*CONSTCOND*/0)
165
166 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
167
168 void *
169 setmode(const char *p)
170 {
171 int serrno;
172 char op, *ep;
173 BITCMD *set, *saveset, *endset;
174 sigset_t signset, sigoset;
175 mode_t mask, perm, permXbits, who;
176 long lval;
177 int equalopdone = 0; /* pacify gcc */
178 int setlen;
179
180 if (!*p) {
181 errno = EINVAL;
182 return NULL;
183 }
184
185 /*
186 * Get a copy of the mask for the permissions that are mask relative.
187 * Flip the bits, we want what's not set. Since it's possible that
188 * the caller is opening files inside a signal handler, protect them
189 * as best we can.
190 */
191 #ifndef __MINGW32__
192 sigfillset(&signset);
193 (void)sigprocmask(SIG_BLOCK, &signset, &sigoset);
194 #endif
195 (void)umask(mask = umask(0));
196 mask = ~mask;
197 #ifndef __MINGW32__
198 (void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
199 #endif
200
201 setlen = SET_LEN + 2;
202
203 set = (BITCMD*) reallocarray(NULL, setlen, sizeof(BITCMD));
204 if (set == 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 errno = 0;
215 lval = strtol(p, &ep, 8);
216 if (*ep) {
217 errno = EINVAL;
218 goto out;
219 }
220 if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
221 goto out;
222 if (lval & ~(STANDARD_BITS|S_ISTXT)) {
223 errno = EINVAL;
224 goto out;
225 }
226 perm = (mode_t)lval;
227 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
228 set->cmd = 0;
229 return (saveset);
230 }
231
232 /*
233 * Build list of structures to set/clear/copy bits as described by
234 * each clause of the symbolic mode.
235 */
236 for (;;) {
237 /* First, find out which bits might be modified. */
238 for (who = 0;; ++p) {
239 switch (*p) {
240 case 'a':
241 who |= STANDARD_BITS;
242 break;
243 case 'u':
244 who |= S_ISUID|S_IRWXU;
245 break;
246 case 'g':
247 who |= S_ISGID|S_IRWXG;
248 break;
249 case 'o':
250 who |= S_IRWXO;
251 break;
252 default:
253 goto getop;
254 }
255 }
256
257 getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
258 errno = EINVAL;
259 goto out;
260 }
261 if (op == '=')
262 equalopdone = 0;
263
264 who &= ~S_ISTXT;
265 for (perm = 0, permXbits = 0;; ++p) {
266 switch (*p) {
267 case 'r':
268 perm |= S_IRUSR|S_IRGRP|S_IROTH;
269 break;
270 case 's':
271 /*
272 * If specific bits where requested and
273 * only "other" bits ignore set-id.
274 */
275 if (who == 0 || (who & ~S_IRWXO))
276 perm |= S_ISUID|S_ISGID;
277 break;
278 case 't':
279 /*
280 * If specific bits where requested and
281 * only "other" bits ignore set-id.
282 */
283 if (who == 0 || (who & ~S_IRWXO)) {
284 who |= S_ISTXT;
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
337 apply: 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 out:
355 serrno = errno;
356 free(saveset);
357 errno = serrno;
358 return NULL;
359 }
360
361 static BITCMD *
362 addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask)
363 {
364 switch (op) {
365 case '=':
366 set->cmd = '-';
367 set->bits = who ? who : STANDARD_BITS;
368 set++;
369
370 op = '+';
371 /* FALLTHROUGH */
372 case '+':
373 case '-':
374 case 'X':
375 set->cmd = op;
376 set->bits = (who ? who : mask) & oparg;
377 break;
378
379 case 'u':
380 case 'g':
381 case 'o':
382 set->cmd = op;
383 if (who) {
384 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
385 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
386 ((who & S_IROTH) ? CMD2_OBITS : 0);
387 set->bits = (mode_t)~0;
388 } else {
389 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
390 set->bits = mask;
391 }
392
393 if (oparg == '+')
394 set->cmd2 |= CMD2_SET;
395 else if (oparg == '-')
396 set->cmd2 |= CMD2_CLR;
397 else if (oparg == '=')
398 set->cmd2 |= CMD2_SET|CMD2_CLR;
399 break;
400 }
401 return (set + 1);
402 }
403
404 #ifdef SETMODE_DEBUG
405 static void
406 dumpmode(BITCMD *set)
407 {
408 for (; set->cmd; ++set)
409 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
410 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
411 set->cmd2 & CMD2_CLR ? " CLR" : "",
412 set->cmd2 & CMD2_SET ? " SET" : "",
413 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
414 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
415 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
416 }
417 #endif
418
419 /*
420 * Given an array of bitcmd structures, compress by compacting consecutive
421 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
422 * 'g' and 'o' commands continue to be separate. They could probably be
423 * compacted, but it's not worth the effort.
424 */
425 static void
426 compress_mode(BITCMD *set)
427 {
428 BITCMD *nset;
429 int setbits, clrbits, Xbits, op;
430
431 for (nset = set;;) {
432 /* Copy over any 'u', 'g' and 'o' commands. */
433 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
434 *set++ = *nset++;
435 if (!op)
436 return;
437 }
438
439 for (setbits = clrbits = Xbits = 0;; nset++) {
440 if ((op = nset->cmd) == '-') {
441 clrbits |= nset->bits;
442 setbits &= ~nset->bits;
443 Xbits &= ~nset->bits;
444 } else if (op == '+') {
445 setbits |= nset->bits;
446 clrbits &= ~nset->bits;
447 Xbits &= ~nset->bits;
448 } else if (op == 'X')
449 Xbits |= nset->bits & ~setbits;
450 else
451 break;
452 }
453 if (clrbits) {
454 set->cmd = '-';
455 set->cmd2 = 0;
456 set->bits = clrbits;
457 set++;
458 }
459 if (setbits) {
460 set->cmd = '+';
461 set->cmd2 = 0;
462 set->bits = setbits;
463 set++;
464 }
465 if (Xbits) {
466 set->cmd = 'X';
467 set->cmd2 = 0;
468 set->bits = Xbits;
469 set++;
470 }
471 }
472 }