source: trunk/src/kash/setmode.c@ 1221

Last change on this file since 1221 was 1221, checked in by bird, 18 years ago

more cleanup.

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