VirtualBox

source: kBuild/trunk/src/kash/setmode.c@ 3505

Last change on this file since 3505 was 3477, checked in by bird, 4 years ago

kash: Use kHlpAssert instead of assert.h (debugger stops on the assertion rather than at exit process code).

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 11.0 KB
Line 
1/* $NetBSD: setmode.c,v 1.30 2003/08/07 16:42:56 agc Exp $ */
2
3/*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Dave Borman at Cray Research, Inc.
9 *
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.
21 *
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.
33 */
34
35#if 0
36#if defined(LIBC_SCCS) && !defined(lint)
37static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94";
38#else
39__RCSID("$NetBSD: setmode.c,v 1.30 2003/08/07 16:42:56 agc Exp $");
40#endif /* LIBC_SCCS and not lint */
41#endif
42
43#include <sys/types.h>
44#include <sys/stat.h>
45
46#include <ctype.h>
47#include <errno.h>
48#include <stdlib.h>
49#include "shinstance.h" /* for unistd.h types/defines */
50
51#ifdef SETMODE_DEBUG
52#include <stdio.h>
53#endif
54
55#define SET_LEN 6 /* initial # of bitcmd struct to malloc */
56#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
57
58typedef struct bitcmd {
59 char cmd;
60 char cmd2;
61 mode_t bits;
62} BITCMD;
63
64#define CMD2_CLR 0x01
65#define CMD2_SET 0x02
66#define CMD2_GBITS 0x04
67#define CMD2_OBITS 0x08
68#define CMD2_UBITS 0x10
69
70static BITCMD *addcmd(BITCMD *, int, int, int, unsigned int);
71static void compress_mode(BITCMD *);
72#ifdef SETMODE_DEBUG
73static void dumpmode(BITCMD *);
74#endif
75
76#ifndef _DIAGASSERT
77# define _DIAGASSERT kHlpAssert
78#endif
79
80#ifndef S_ISTXT
81# ifdef S_ISVTX
82# define S_ISTXT S_ISVTX
83# else
84# define S_ISTXT 0
85# endif
86#endif /* !S_ISTXT */
87
88/*
89 * Given the old mode and an array of bitcmd structures, apply the operations
90 * described in the bitcmd structures to the old mode, and return the new mode.
91 * Note that there is no '=' command; a strict assignment is just a '-' (clear
92 * bits) followed by a '+' (set bits).
93 */
94mode_t
95kash_getmode(const void *bbox, mode_t omode)
96{
97 const BITCMD *set;
98 mode_t clrval, newmode, value;
99
100 _DIAGASSERT(bbox != NULL);
101
102 set = (const BITCMD *)bbox;
103 newmode = omode;
104 for (value = 0;; set++)
105 switch(set->cmd) {
106 /*
107 * When copying the user, group or other bits around, we "know"
108 * where the bits are in the mode so that we can do shifts to
109 * copy them around. If we don't use shifts, it gets real
110 * grundgy with lots of single bit checks and bit sets.
111 */
112 case 'u':
113 value = (newmode & S_IRWXU) >> 6;
114 goto common;
115
116 case 'g':
117 value = (newmode & S_IRWXG) >> 3;
118 goto common;
119
120 case 'o':
121 value = newmode & S_IRWXO;
122common: if (set->cmd2 & CMD2_CLR) {
123 clrval =
124 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
125 if (set->cmd2 & CMD2_UBITS)
126 newmode &= ~((clrval<<6) & set->bits);
127 if (set->cmd2 & CMD2_GBITS)
128 newmode &= ~((clrval<<3) & set->bits);
129 if (set->cmd2 & CMD2_OBITS)
130 newmode &= ~(clrval & set->bits);
131 }
132 if (set->cmd2 & CMD2_SET) {
133 if (set->cmd2 & CMD2_UBITS)
134 newmode |= (value<<6) & set->bits;
135 if (set->cmd2 & CMD2_GBITS)
136 newmode |= (value<<3) & set->bits;
137 if (set->cmd2 & CMD2_OBITS)
138 newmode |= value & set->bits;
139 }
140 break;
141
142 case '+':
143 newmode |= set->bits;
144 break;
145
146 case '-':
147 newmode &= ~set->bits;
148 break;
149
150 case 'X':
151 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
152 newmode |= set->bits;
153 break;
154
155 case '\0':
156 default:
157#ifdef SETMODE_DEBUG
158 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
159#endif
160 return (newmode);
161 }
162}
163
164#define ADDCMD(a, b, c, d) do { \
165 if (set >= endset) { \
166 BITCMD *newset; \
167 setlen += SET_LEN_INCR; \
168 newset = sh_realloc(NULL, saveset, sizeof(BITCMD) * setlen); \
169 if (newset == NULL) { \
170 sh_free(NULL, saveset); \
171 return (NULL); \
172 } \
173 set = newset + (set - saveset); \
174 saveset = newset; \
175 endset = newset + (setlen - 2); \
176 } \
177 set = addcmd(set, (a), (b), (c), (d)); \
178} while (/*CONSTCOND*/0)
179
180#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
181
182void *
183kash_setmode(shinstance *psh, const char *p)
184{
185 int perm, who;
186 char op, *ep;
187 BITCMD *set, *saveset, *endset;
188 mode_t mask;
189 int equalopdone = 0; /* pacify gcc */
190 int permXbits, setlen;
191
192 if (!*p)
193 return (NULL);
194
195 /*
196 * Get a copy of the mask for the permissions that are mask relative.
197 * Flip the bits, we want what's not set.
198 */
199 mask = shfile_get_umask(&psh->fdtab);
200
201 setlen = SET_LEN + 2;
202
203 if ((set = sh_malloc(NULL, sizeof(BITCMD) * setlen)) == NULL)
204 return (NULL);
205 saveset = set;
206 endset = set + (setlen - 2);
207
208 /*
209 * If an absolute number, get it and return; disallow non-octal digits
210 * or illegal bits.
211 */
212 if (isdigit((unsigned char)*p)) {
213 perm = (mode_t)strtol(p, &ep, 8);
214 if (*ep || perm & ~(STANDARD_BITS|S_ISTXT)) {
215 sh_free(NULL, saveset);
216 return (NULL);
217 }
218 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
219 set->cmd = 0;
220 return (saveset);
221 }
222
223 /*
224 * Build list of structures to set/clear/copy bits as described by
225 * each clause of the symbolic mode.
226 */
227 for (;;) {
228 /* First, find out which bits might be modified. */
229 for (who = 0;; ++p) {
230 switch (*p) {
231 case 'a':
232 who |= STANDARD_BITS;
233 break;
234 case 'u':
235 who |= S_ISUID|S_IRWXU;
236 break;
237 case 'g':
238 who |= S_ISGID|S_IRWXG;
239 break;
240 case 'o':
241 who |= S_IRWXO;
242 break;
243 default:
244 goto getop;
245 }
246 }
247
248getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
249 sh_free(NULL, saveset);
250 return (NULL);
251 }
252 if (op == '=')
253 equalopdone = 0;
254
255 who &= ~S_ISTXT;
256 for (perm = 0, permXbits = 0;; ++p) {
257 switch (*p) {
258 case 'r':
259 perm |= S_IRUSR|S_IRGRP|S_IROTH;
260 break;
261 case 's':
262 /*
263 * If specific bits where requested and
264 * only "other" bits ignore set-id.
265 */
266 if (who == 0 || (who & ~S_IRWXO))
267 perm |= S_ISUID|S_ISGID;
268 break;
269 case 't':
270 /*
271 * If specific bits where requested and
272 * only "other" bits ignore set-id.
273 */
274 if (who == 0 || (who & ~S_IRWXO)) {
275 who |= S_ISTXT;
276 perm |= S_ISTXT;
277 }
278 break;
279 case 'w':
280 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
281 break;
282 case 'X':
283 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
284 break;
285 case 'x':
286 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
287 break;
288 case 'u':
289 case 'g':
290 case 'o':
291 /*
292 * When ever we hit 'u', 'g', or 'o', we have
293 * to flush out any partial mode that we have,
294 * and then do the copying of the mode bits.
295 */
296 if (perm) {
297 ADDCMD(op, who, perm, mask);
298 perm = 0;
299 }
300 if (op == '=')
301 equalopdone = 1;
302 if (op == '+' && permXbits) {
303 ADDCMD('X', who, permXbits, mask);
304 permXbits = 0;
305 }
306 ADDCMD(*p, who, op, mask);
307 break;
308
309 default:
310 /*
311 * Add any permissions that we haven't already
312 * done.
313 */
314 if (perm || (op == '=' && !equalopdone)) {
315 if (op == '=')
316 equalopdone = 1;
317 ADDCMD(op, who, perm, mask);
318 perm = 0;
319 }
320 if (permXbits) {
321 ADDCMD('X', who, permXbits, mask);
322 permXbits = 0;
323 }
324 goto apply;
325 }
326 }
327
328apply: if (!*p)
329 break;
330 if (*p != ',')
331 goto getop;
332 ++p;
333 }
334 set->cmd = 0;
335#ifdef SETMODE_DEBUG
336 (void)printf("Before compress_mode()\n");
337 dumpmode(saveset);
338#endif
339 compress_mode(saveset);
340#ifdef SETMODE_DEBUG
341 (void)printf("After compress_mode()\n");
342 dumpmode(saveset);
343#endif
344 return (saveset);
345}
346
347static BITCMD *
348addcmd(set, op, who, oparg, mask)
349 BITCMD *set;
350 int oparg, who;
351 int op;
352 unsigned int mask;
353{
354
355 _DIAGASSERT(set != NULL);
356
357 switch (op) {
358 case '=':
359 set->cmd = '-';
360 set->bits = who ? who : STANDARD_BITS;
361 set++;
362
363 op = '+';
364 /* FALLTHROUGH */
365 case '+':
366 case '-':
367 case 'X':
368 set->cmd = op;
369 set->bits = (who ? (unsigned int)who : mask) & (unsigned int)oparg;
370 break;
371
372 case 'u':
373 case 'g':
374 case 'o':
375 set->cmd = op;
376 if (who) {
377 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
378 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
379 ((who & S_IROTH) ? CMD2_OBITS : 0);
380 set->bits = (mode_t)~0;
381 } else {
382 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
383 set->bits = mask;
384 }
385
386 if (oparg == '+')
387 set->cmd2 |= CMD2_SET;
388 else if (oparg == '-')
389 set->cmd2 |= CMD2_CLR;
390 else if (oparg == '=')
391 set->cmd2 |= CMD2_SET|CMD2_CLR;
392 break;
393 }
394 return (set + 1);
395}
396
397#ifdef SETMODE_DEBUG
398static void
399dumpmode(set)
400 BITCMD *set;
401{
402
403 _DIAGASSERT(set != NULL);
404
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" : "");
413}
414#endif
415
416/*
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.
421 */
422static void
423compress_mode(set)
424 BITCMD *set;
425{
426 BITCMD *nset;
427 int setbits, clrbits, Xbits, op;
428
429 _DIAGASSERT(set != NULL);
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}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette