VirtualBox

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

Last change on this file since 1233 was 1233, checked in by bird, 17 years ago

keywords.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 10.9 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 <assert.h>
47#include <ctype.h>
48#include <errno.h>
49#include <stdlib.h>
50#include "shinstance.h" /* for unistd.h types/defines */
51
52#ifdef SETMODE_DEBUG
53#include <stdio.h>
54#endif
55
56#define SET_LEN 6 /* initial # of bitcmd struct to malloc */
57#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */
58
59typedef struct bitcmd {
60 char cmd;
61 char cmd2;
62 mode_t bits;
63} BITCMD;
64
65#define CMD2_CLR 0x01
66#define CMD2_SET 0x02
67#define CMD2_GBITS 0x04
68#define CMD2_OBITS 0x08
69#define CMD2_UBITS 0x10
70
71static BITCMD *addcmd(BITCMD *, int, int, int, unsigned int);
72static void compress_mode(BITCMD *);
73#ifdef SETMODE_DEBUG
74static void dumpmode(BITCMD *);
75#endif
76
77#ifndef _DIAGASSERT
78# define _DIAGASSERT assert
79#endif
80
81#ifndef S_ISTXT
82# ifdef S_ISVTX
83# define S_ISTXT S_ISVTX
84# else
85# define S_ISTXT 0
86# endif
87#endif /* !S_ISTXT */
88
89/*
90 * Given the old mode and an array of bitcmd structures, apply the operations
91 * described in the bitcmd structures to the old mode, and return the new mode.
92 * Note that there is no '=' command; a strict assignment is just a '-' (clear
93 * bits) followed by a '+' (set bits).
94 */
95mode_t
96bsd_getmode(const void *bbox, mode_t omode)
97{
98 const BITCMD *set;
99 mode_t clrval, newmode, value;
100
101 _DIAGASSERT(bbox != NULL);
102
103 set = (const BITCMD *)bbox;
104 newmode = omode;
105 for (value = 0;; set++)
106 switch(set->cmd) {
107 /*
108 * When copying the user, group or other bits around, we "know"
109 * where the bits are in the mode so that we can do shifts to
110 * copy them around. If we don't use shifts, it gets real
111 * grundgy with lots of single bit checks and bit sets.
112 */
113 case 'u':
114 value = (newmode & S_IRWXU) >> 6;
115 goto common;
116
117 case 'g':
118 value = (newmode & S_IRWXG) >> 3;
119 goto common;
120
121 case 'o':
122 value = newmode & S_IRWXO;
123common: if (set->cmd2 & CMD2_CLR) {
124 clrval =
125 (set->cmd2 & CMD2_SET) ? S_IRWXO : value;
126 if (set->cmd2 & CMD2_UBITS)
127 newmode &= ~((clrval<<6) & set->bits);
128 if (set->cmd2 & CMD2_GBITS)
129 newmode &= ~((clrval<<3) & set->bits);
130 if (set->cmd2 & CMD2_OBITS)
131 newmode &= ~(clrval & set->bits);
132 }
133 if (set->cmd2 & CMD2_SET) {
134 if (set->cmd2 & CMD2_UBITS)
135 newmode |= (value<<6) & set->bits;
136 if (set->cmd2 & CMD2_GBITS)
137 newmode |= (value<<3) & set->bits;
138 if (set->cmd2 & CMD2_OBITS)
139 newmode |= value & set->bits;
140 }
141 break;
142
143 case '+':
144 newmode |= set->bits;
145 break;
146
147 case '-':
148 newmode &= ~set->bits;
149 break;
150
151 case 'X':
152 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
153 newmode |= set->bits;
154 break;
155
156 case '\0':
157 default:
158#ifdef SETMODE_DEBUG
159 (void)printf("getmode:%04o -> %04o\n", omode, newmode);
160#endif
161 return (newmode);
162 }
163}
164
165#define ADDCMD(a, b, c, d) do { \
166 if (set >= endset) { \
167 BITCMD *newset; \
168 setlen += SET_LEN_INCR; \
169 newset = realloc(saveset, sizeof(BITCMD) * setlen); \
170 if (newset == NULL) { \
171 free(saveset); \
172 return (NULL); \
173 } \
174 set = newset + (set - saveset); \
175 saveset = newset; \
176 endset = newset + (setlen - 2); \
177 } \
178 set = addcmd(set, (a), (b), (c), (d)); \
179} while (/*CONSTCOND*/0)
180
181#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
182
183void *
184bsd_setmode(shinstance *psh, const char *p)
185{
186 int perm, who;
187 char op, *ep;
188 BITCMD *set, *saveset, *endset;
189 mode_t mask;
190 int equalopdone = 0; /* pacify gcc */
191 int permXbits, setlen;
192
193 if (!*p)
194 return (NULL);
195
196 /*
197 * Get a copy of the mask for the permissions that are mask relative.
198 * Flip the bits, we want what's not set.
199 */
200 mask = shfile_get_umask(&psh->fdtab);
201
202 setlen = SET_LEN + 2;
203
204 if ((set = malloc(sizeof(BITCMD) * setlen)) == 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 perm = (mode_t)strtol(p, &ep, 8);
215 if (*ep || perm & ~(STANDARD_BITS|S_ISTXT)) {
216 free(saveset);
217 return (NULL);
218 }
219 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
220 set->cmd = 0;
221 return (saveset);
222 }
223
224 /*
225 * Build list of structures to set/clear/copy bits as described by
226 * each clause of the symbolic mode.
227 */
228 for (;;) {
229 /* First, find out which bits might be modified. */
230 for (who = 0;; ++p) {
231 switch (*p) {
232 case 'a':
233 who |= STANDARD_BITS;
234 break;
235 case 'u':
236 who |= S_ISUID|S_IRWXU;
237 break;
238 case 'g':
239 who |= S_ISGID|S_IRWXG;
240 break;
241 case 'o':
242 who |= S_IRWXO;
243 break;
244 default:
245 goto getop;
246 }
247 }
248
249getop: if ((op = *p++) != '+' && op != '-' && op != '=') {
250 free(saveset);
251 return (NULL);
252 }
253 if (op == '=')
254 equalopdone = 0;
255
256 who &= ~S_ISTXT;
257 for (perm = 0, permXbits = 0;; ++p) {
258 switch (*p) {
259 case 'r':
260 perm |= S_IRUSR|S_IRGRP|S_IROTH;
261 break;
262 case 's':
263 /*
264 * If specific bits where requested and
265 * only "other" bits ignore set-id.
266 */
267 if (who == 0 || (who & ~S_IRWXO))
268 perm |= S_ISUID|S_ISGID;
269 break;
270 case 't':
271 /*
272 * If specific bits where requested and
273 * only "other" bits ignore set-id.
274 */
275 if (who == 0 || (who & ~S_IRWXO)) {
276 who |= S_ISTXT;
277 perm |= S_ISTXT;
278 }
279 break;
280 case 'w':
281 perm |= S_IWUSR|S_IWGRP|S_IWOTH;
282 break;
283 case 'X':
284 permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
285 break;
286 case 'x':
287 perm |= S_IXUSR|S_IXGRP|S_IXOTH;
288 break;
289 case 'u':
290 case 'g':
291 case 'o':
292 /*
293 * When ever we hit 'u', 'g', or 'o', we have
294 * to flush out any partial mode that we have,
295 * and then do the copying of the mode bits.
296 */
297 if (perm) {
298 ADDCMD(op, who, perm, mask);
299 perm = 0;
300 }
301 if (op == '=')
302 equalopdone = 1;
303 if (op == '+' && permXbits) {
304 ADDCMD('X', who, permXbits, mask);
305 permXbits = 0;
306 }
307 ADDCMD(*p, who, op, mask);
308 break;
309
310 default:
311 /*
312 * Add any permissions that we haven't already
313 * done.
314 */
315 if (perm || (op == '=' && !equalopdone)) {
316 if (op == '=')
317 equalopdone = 1;
318 ADDCMD(op, who, perm, mask);
319 perm = 0;
320 }
321 if (permXbits) {
322 ADDCMD('X', who, permXbits, mask);
323 permXbits = 0;
324 }
325 goto apply;
326 }
327 }
328
329apply: if (!*p)
330 break;
331 if (*p != ',')
332 goto getop;
333 ++p;
334 }
335 set->cmd = 0;
336#ifdef SETMODE_DEBUG
337 (void)printf("Before compress_mode()\n");
338 dumpmode(saveset);
339#endif
340 compress_mode(saveset);
341#ifdef SETMODE_DEBUG
342 (void)printf("After compress_mode()\n");
343 dumpmode(saveset);
344#endif
345 return (saveset);
346}
347
348static BITCMD *
349addcmd(set, op, who, oparg, mask)
350 BITCMD *set;
351 int oparg, who;
352 int op;
353 unsigned int mask;
354{
355
356 _DIAGASSERT(set != NULL);
357
358 switch (op) {
359 case '=':
360 set->cmd = '-';
361 set->bits = who ? who : STANDARD_BITS;
362 set++;
363
364 op = '+';
365 /* FALLTHROUGH */
366 case '+':
367 case '-':
368 case 'X':
369 set->cmd = op;
370 set->bits = (who ? who : mask) & oparg;
371 break;
372
373 case 'u':
374 case 'g':
375 case 'o':
376 set->cmd = op;
377 if (who) {
378 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
379 ((who & S_IRGRP) ? CMD2_GBITS : 0) |
380 ((who & S_IROTH) ? CMD2_OBITS : 0);
381 set->bits = (mode_t)~0;
382 } else {
383 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
384 set->bits = mask;
385 }
386
387 if (oparg == '+')
388 set->cmd2 |= CMD2_SET;
389 else if (oparg == '-')
390 set->cmd2 |= CMD2_CLR;
391 else if (oparg == '=')
392 set->cmd2 |= CMD2_SET|CMD2_CLR;
393 break;
394 }
395 return (set + 1);
396}
397
398#ifdef SETMODE_DEBUG
399static void
400dumpmode(set)
401 BITCMD *set;
402{
403
404 _DIAGASSERT(set != NULL);
405
406 for (; set->cmd; ++set)
407 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
408 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
409 set->cmd2 & CMD2_CLR ? " CLR" : "",
410 set->cmd2 & CMD2_SET ? " SET" : "",
411 set->cmd2 & CMD2_UBITS ? " UBITS" : "",
412 set->cmd2 & CMD2_GBITS ? " GBITS" : "",
413 set->cmd2 & CMD2_OBITS ? " OBITS" : "");
414}
415#endif
416
417/*
418 * Given an array of bitcmd structures, compress by compacting consecutive
419 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u',
420 * 'g' and 'o' commands continue to be separate. They could probably be
421 * compacted, but it's not worth the effort.
422 */
423static void
424compress_mode(set)
425 BITCMD *set;
426{
427 BITCMD *nset;
428 int setbits, clrbits, Xbits, op;
429
430 _DIAGASSERT(set != NULL);
431
432 for (nset = set;;) {
433 /* Copy over any 'u', 'g' and 'o' commands. */
434 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
435 *set++ = *nset++;
436 if (!op)
437 return;
438 }
439
440 for (setbits = clrbits = Xbits = 0;; nset++) {
441 if ((op = nset->cmd) == '-') {
442 clrbits |= nset->bits;
443 setbits &= ~nset->bits;
444 Xbits &= ~nset->bits;
445 } else if (op == '+') {
446 setbits |= nset->bits;
447 clrbits &= ~nset->bits;
448 Xbits &= ~nset->bits;
449 } else if (op == 'X')
450 Xbits |= nset->bits & ~setbits;
451 else
452 break;
453 }
454 if (clrbits) {
455 set->cmd = '-';
456 set->cmd2 = 0;
457 set->bits = clrbits;
458 set++;
459 }
460 if (setbits) {
461 set->cmd = '+';
462 set->cmd2 = 0;
463 set->bits = setbits;
464 set++;
465 }
466 if (Xbits) {
467 set->cmd = 'X';
468 set->cmd2 = 0;
469 set->bits = Xbits;
470 set++;
471 }
472 }
473}
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