1 /* $NetBSD: setmode.c,v 1.16 1997/07/13 19:18:39 christos 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #if defined(LIBC_SCCS) && !defined(lint) 41 #if 0 42 static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94"; 43 #else 44 __RCSID("$NetBSD: setmode.c,v 1.16 1997/07/13 19:18:39 christos Exp $"); 45 #endif 46 #endif /* LIBC_SCCS and not lint */ 47 48 #include <sys/types.h> 49 #include <sys/stat.h> 50 51 #include <ctype.h> 52 #include <errno.h> 53 #include <signal.h> 54 #include <stdlib.h> 55 #include <unistd.h> 56 57 #ifdef SETMODE_DEBUG 58 #include <stdio.h> 59 #endif 60 61 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */ 62 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */ 63 64 typedef struct bitcmd { 65 char cmd; 66 char cmd2; 67 mode_t bits; 68 } BITCMD; 69 70 #define CMD2_CLR 0x01 71 #define CMD2_SET 0x02 72 #define CMD2_GBITS 0x04 73 #define CMD2_OBITS 0x08 74 #define CMD2_UBITS 0x10 75 76 static BITCMD *addcmd __P((BITCMD *, int, int, int, u_int)); 77 static void compress_mode __P((BITCMD *)); 78 #ifdef SETMODE_DEBUG 79 static void dumpmode __P((BITCMD *)); 80 #endif 81 82 /* 83 * Given the old mode and an array of bitcmd structures, apply the operations 84 * described in the bitcmd structures to the old mode, and return the new mode. 85 * Note that there is no '=' command; a strict assignment is just a '-' (clear 86 * bits) followed by a '+' (set bits). 87 */ 88 mode_t 89 getmode(bbox, omode) 90 const void *bbox; 91 mode_t omode; 92 { 93 register const BITCMD *set; 94 register mode_t clrval, newmode, value; 95 96 set = (const BITCMD *)bbox; 97 newmode = omode; 98 for (value = 0;; set++) 99 switch(set->cmd) { 100 /* 101 * When copying the user, group or other bits around, we "know" 102 * where the bits are in the mode so that we can do shifts to 103 * copy them around. If we don't use shifts, it gets real 104 * grundgy with lots of single bit checks and bit sets. 105 */ 106 case 'u': 107 value = (newmode & S_IRWXU) >> 6; 108 goto common; 109 110 case 'g': 111 value = (newmode & S_IRWXG) >> 3; 112 goto common; 113 114 case 'o': 115 value = newmode & S_IRWXO; 116 common: if (set->cmd2 & CMD2_CLR) { 117 clrval = 118 (set->cmd2 & CMD2_SET) ? S_IRWXO : value; 119 if (set->cmd2 & CMD2_UBITS) 120 newmode &= ~((clrval<<6) & set->bits); 121 if (set->cmd2 & CMD2_GBITS) 122 newmode &= ~((clrval<<3) & set->bits); 123 if (set->cmd2 & CMD2_OBITS) 124 newmode &= ~(clrval & set->bits); 125 } 126 if (set->cmd2 & CMD2_SET) { 127 if (set->cmd2 & CMD2_UBITS) 128 newmode |= (value<<6) & set->bits; 129 if (set->cmd2 & CMD2_GBITS) 130 newmode |= (value<<3) & set->bits; 131 if (set->cmd2 & CMD2_OBITS) 132 newmode |= value & set->bits; 133 } 134 break; 135 136 case '+': 137 newmode |= set->bits; 138 break; 139 140 case '-': 141 newmode &= ~set->bits; 142 break; 143 144 case 'X': 145 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH)) 146 newmode |= set->bits; 147 break; 148 149 case '\0': 150 default: 151 #ifdef SETMODE_DEBUG 152 (void)printf("getmode:%04o -> %04o\n", omode, newmode); 153 #endif 154 return (newmode); 155 } 156 } 157 158 #define ADDCMD(a, b, c, d) \ 159 if (set >= endset) { \ 160 register BITCMD *newset; \ 161 setlen += SET_LEN_INCR; \ 162 newset = realloc(saveset, sizeof(BITCMD) * setlen); \ 163 if (!saveset) \ 164 return (NULL); \ 165 set = newset + (set - saveset); \ 166 saveset = newset; \ 167 endset = newset + (setlen - 2); \ 168 } \ 169 set = addcmd(set, (a), (b), (c), (d)) 170 171 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) 172 173 void * 174 setmode(p) 175 register const char *p; 176 { 177 register int perm, who; 178 register char op; 179 BITCMD *set, *saveset, *endset; 180 sigset_t sigset, sigoset; 181 mode_t mask; 182 int equalopdone = 0; /* pacify gcc */ 183 int permXbits, setlen; 184 185 if (!*p) 186 return (NULL); 187 188 /* 189 * Get a copy of the mask for the permissions that are mask relative. 190 * Flip the bits, we want what's not set. Since it's possible that 191 * the caller is opening files inside a signal handler, protect them 192 * as best we can. 193 */ 194 sigfillset(&sigset); 195 (void)sigprocmask(SIG_BLOCK, &sigset, &sigoset); 196 (void)umask(mask = umask(0)); 197 mask = ~mask; 198 (void)sigprocmask(SIG_SETMASK, &sigoset, NULL); 199 200 setlen = SET_LEN + 2; 201 202 if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL) 203 return (NULL); 204 saveset = set; 205 endset = set + (setlen - 2); 206 207 /* 208 * If an absolute number, get it and return; disallow non-octal digits 209 * or illegal bits. 210 */ 211 if (isdigit(*p)) { 212 perm = (mode_t)strtol(p, NULL, 8); 213 if (perm & ~(STANDARD_BITS|S_ISTXT)) { 214 free(saveset); 215 return (NULL); 216 } 217 while (*++p) 218 if (*p < '0' || *p > '7') { 219 free(saveset); 220 return (NULL); 221 } 222 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask); 223 return (saveset); 224 } 225 226 /* 227 * Build list of structures to set/clear/copy bits as described by 228 * each clause of the symbolic mode. 229 */ 230 for (;;) { 231 /* First, find out which bits might be modified. */ 232 for (who = 0;; ++p) { 233 switch (*p) { 234 case 'a': 235 who |= STANDARD_BITS; 236 break; 237 case 'u': 238 who |= S_ISUID|S_IRWXU; 239 break; 240 case 'g': 241 who |= S_ISGID|S_IRWXG; 242 break; 243 case 'o': 244 who |= S_IRWXO; 245 break; 246 default: 247 goto getop; 248 } 249 } 250 251 getop: if ((op = *p++) != '+' && op != '-' && op != '=') { 252 free(saveset); 253 return (NULL); 254 } 255 if (op == '=') 256 equalopdone = 0; 257 258 who &= ~S_ISTXT; 259 for (perm = 0, permXbits = 0;; ++p) { 260 switch (*p) { 261 case 'r': 262 perm |= S_IRUSR|S_IRGRP|S_IROTH; 263 break; 264 case 's': 265 /* 266 * If specific bits where requested and 267 * only "other" bits ignore set-id. 268 */ 269 if (who == 0 || (who & ~S_IRWXO)) 270 perm |= S_ISUID|S_ISGID; 271 break; 272 case 't': 273 /* 274 * If specific bits where requested and 275 * only "other" bits ignore set-id. 276 */ 277 if (who == 0 || (who & ~S_IRWXO)) { 278 who |= S_ISTXT; 279 perm |= S_ISTXT; 280 } 281 break; 282 case 'w': 283 perm |= S_IWUSR|S_IWGRP|S_IWOTH; 284 break; 285 case 'X': 286 permXbits = S_IXUSR|S_IXGRP|S_IXOTH; 287 break; 288 case 'x': 289 perm |= S_IXUSR|S_IXGRP|S_IXOTH; 290 break; 291 case 'u': 292 case 'g': 293 case 'o': 294 /* 295 * When ever we hit 'u', 'g', or 'o', we have 296 * to flush out any partial mode that we have, 297 * and then do the copying of the mode bits. 298 */ 299 if (perm) { 300 ADDCMD(op, who, perm, mask); 301 perm = 0; 302 } 303 if (op == '=') 304 equalopdone = 1; 305 if (op == '+' && permXbits) { 306 ADDCMD('X', who, permXbits, mask); 307 permXbits = 0; 308 } 309 ADDCMD(*p, who, op, mask); 310 break; 311 312 default: 313 /* 314 * Add any permissions that we haven't already 315 * done. 316 */ 317 if (perm || (op == '=' && !equalopdone)) { 318 if (op == '=') 319 equalopdone = 1; 320 ADDCMD(op, who, perm, mask); 321 perm = 0; 322 } 323 if (permXbits) { 324 ADDCMD('X', who, permXbits, mask); 325 permXbits = 0; 326 } 327 goto apply; 328 } 329 } 330 331 apply: if (!*p) 332 break; 333 if (*p != ',') 334 goto getop; 335 ++p; 336 } 337 set->cmd = 0; 338 #ifdef SETMODE_DEBUG 339 (void)printf("Before compress_mode()\n"); 340 dumpmode(saveset); 341 #endif 342 compress_mode(saveset); 343 #ifdef SETMODE_DEBUG 344 (void)printf("After compress_mode()\n"); 345 dumpmode(saveset); 346 #endif 347 return (saveset); 348 } 349 350 static BITCMD * 351 addcmd(set, op, who, oparg, mask) 352 BITCMD *set; 353 register int oparg, who; 354 register int op; 355 u_int mask; 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 ? who : mask) & 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 = ~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 398 static void 399 dumpmode(set) 400 register BITCMD *set; 401 { 402 for (; set->cmd; ++set) 403 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n", 404 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "", 405 set->cmd2 & CMD2_CLR ? " CLR" : "", 406 set->cmd2 & CMD2_SET ? " SET" : "", 407 set->cmd2 & CMD2_UBITS ? " UBITS" : "", 408 set->cmd2 & CMD2_GBITS ? " GBITS" : "", 409 set->cmd2 & CMD2_OBITS ? " OBITS" : ""); 410 } 411 #endif 412 413 /* 414 * Given an array of bitcmd structures, compress by compacting consecutive 415 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u', 416 * 'g' and 'o' commands continue to be separate. They could probably be 417 * compacted, but it's not worth the effort. 418 */ 419 static void 420 compress_mode(set) 421 register BITCMD *set; 422 { 423 register BITCMD *nset; 424 register int setbits, clrbits, Xbits, op; 425 426 for (nset = set;;) { 427 /* Copy over any 'u', 'g' and 'o' commands. */ 428 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') { 429 *set++ = *nset++; 430 if (!op) 431 return; 432 } 433 434 for (setbits = clrbits = Xbits = 0;; nset++) { 435 if ((op = nset->cmd) == '-') { 436 clrbits |= nset->bits; 437 setbits &= ~nset->bits; 438 Xbits &= ~nset->bits; 439 } else if (op == '+') { 440 setbits |= nset->bits; 441 clrbits &= ~nset->bits; 442 Xbits &= ~nset->bits; 443 } else if (op == 'X') 444 Xbits |= nset->bits & ~setbits; 445 else 446 break; 447 } 448 if (clrbits) { 449 set->cmd = '-'; 450 set->cmd2 = 0; 451 set->bits = clrbits; 452 set++; 453 } 454 if (setbits) { 455 set->cmd = '+'; 456 set->cmd2 = 0; 457 set->bits = setbits; 458 set++; 459 } 460 if (Xbits) { 461 set->cmd = 'X'; 462 set->cmd2 = 0; 463 set->bits = Xbits; 464 set++; 465 } 466 } 467 } 468