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