1 /* $NetBSD: setmode.c,v 1.17 1997/07/21 14:07:31 jtc 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.17 1997/07/21 14:07:31 jtc Exp $"); 45 #endif 46 #endif /* LIBC_SCCS and not lint */ 47 48 #include "namespace.h" 49 #include <sys/types.h> 50 #include <sys/stat.h> 51 52 #include <ctype.h> 53 #include <errno.h> 54 #include <signal.h> 55 #include <stdlib.h> 56 #include <unistd.h> 57 58 #ifdef SETMODE_DEBUG 59 #include <stdio.h> 60 #endif 61 62 #ifdef __weak_alias 63 __weak_alias(getmode,_getmode); 64 __weak_alias(setmode,_setmode); 65 #endif 66 67 #define SET_LEN 6 /* initial # of bitcmd struct to malloc */ 68 #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */ 69 70 typedef struct bitcmd { 71 char cmd; 72 char cmd2; 73 mode_t bits; 74 } BITCMD; 75 76 #define CMD2_CLR 0x01 77 #define CMD2_SET 0x02 78 #define CMD2_GBITS 0x04 79 #define CMD2_OBITS 0x08 80 #define CMD2_UBITS 0x10 81 82 static BITCMD *addcmd __P((BITCMD *, int, int, int, u_int)); 83 static void compress_mode __P((BITCMD *)); 84 #ifdef SETMODE_DEBUG 85 static void dumpmode __P((BITCMD *)); 86 #endif 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 */ 94 mode_t 95 getmode(bbox, omode) 96 const void *bbox; 97 mode_t omode; 98 { 99 register const BITCMD *set; 100 register mode_t clrval, newmode, value; 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; 122 common: 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) \ 165 if (set >= endset) { \ 166 register BITCMD *newset; \ 167 setlen += SET_LEN_INCR; \ 168 newset = realloc(saveset, sizeof(BITCMD) * setlen); \ 169 if (!saveset) \ 170 return (NULL); \ 171 set = newset + (set - saveset); \ 172 saveset = newset; \ 173 endset = newset + (setlen - 2); \ 174 } \ 175 set = addcmd(set, (a), (b), (c), (d)) 176 177 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) 178 179 void * 180 setmode(p) 181 register const char *p; 182 { 183 register int perm, who; 184 register char op; 185 BITCMD *set, *saveset, *endset; 186 sigset_t sigset, sigoset; 187 mode_t mask; 188 int equalopdone = 0; /* pacify gcc */ 189 int permXbits, setlen; 190 191 if (!*p) 192 return (NULL); 193 194 /* 195 * Get a copy of the mask for the permissions that are mask relative. 196 * Flip the bits, we want what's not set. Since it's possible that 197 * the caller is opening files inside a signal handler, protect them 198 * as best we can. 199 */ 200 sigfillset(&sigset); 201 (void)sigprocmask(SIG_BLOCK, &sigset, &sigoset); 202 (void)umask(mask = umask(0)); 203 mask = ~mask; 204 (void)sigprocmask(SIG_SETMASK, &sigoset, NULL); 205 206 setlen = SET_LEN + 2; 207 208 if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL) 209 return (NULL); 210 saveset = set; 211 endset = set + (setlen - 2); 212 213 /* 214 * If an absolute number, get it and return; disallow non-octal digits 215 * or illegal bits. 216 */ 217 if (isdigit(*p)) { 218 perm = (mode_t)strtol(p, NULL, 8); 219 if (perm & ~(STANDARD_BITS|S_ISTXT)) { 220 free(saveset); 221 return (NULL); 222 } 223 while (*++p) 224 if (*p < '0' || *p > '7') { 225 free(saveset); 226 return (NULL); 227 } 228 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask); 229 return (saveset); 230 } 231 232 /* 233 * Build list of structures to set/clear/copy bits as described by 234 * each clause of the symbolic mode. 235 */ 236 for (;;) { 237 /* First, find out which bits might be modified. */ 238 for (who = 0;; ++p) { 239 switch (*p) { 240 case 'a': 241 who |= STANDARD_BITS; 242 break; 243 case 'u': 244 who |= S_ISUID|S_IRWXU; 245 break; 246 case 'g': 247 who |= S_ISGID|S_IRWXG; 248 break; 249 case 'o': 250 who |= S_IRWXO; 251 break; 252 default: 253 goto getop; 254 } 255 } 256 257 getop: if ((op = *p++) != '+' && op != '-' && op != '=') { 258 free(saveset); 259 return (NULL); 260 } 261 if (op == '=') 262 equalopdone = 0; 263 264 who &= ~S_ISTXT; 265 for (perm = 0, permXbits = 0;; ++p) { 266 switch (*p) { 267 case 'r': 268 perm |= S_IRUSR|S_IRGRP|S_IROTH; 269 break; 270 case 's': 271 /* 272 * If specific bits where requested and 273 * only "other" bits ignore set-id. 274 */ 275 if (who == 0 || (who & ~S_IRWXO)) 276 perm |= S_ISUID|S_ISGID; 277 break; 278 case 't': 279 /* 280 * If specific bits where requested and 281 * only "other" bits ignore set-id. 282 */ 283 if (who == 0 || (who & ~S_IRWXO)) { 284 who |= S_ISTXT; 285 perm |= S_ISTXT; 286 } 287 break; 288 case 'w': 289 perm |= S_IWUSR|S_IWGRP|S_IWOTH; 290 break; 291 case 'X': 292 permXbits = S_IXUSR|S_IXGRP|S_IXOTH; 293 break; 294 case 'x': 295 perm |= S_IXUSR|S_IXGRP|S_IXOTH; 296 break; 297 case 'u': 298 case 'g': 299 case 'o': 300 /* 301 * When ever we hit 'u', 'g', or 'o', we have 302 * to flush out any partial mode that we have, 303 * and then do the copying of the mode bits. 304 */ 305 if (perm) { 306 ADDCMD(op, who, perm, mask); 307 perm = 0; 308 } 309 if (op == '=') 310 equalopdone = 1; 311 if (op == '+' && permXbits) { 312 ADDCMD('X', who, permXbits, mask); 313 permXbits = 0; 314 } 315 ADDCMD(*p, who, op, mask); 316 break; 317 318 default: 319 /* 320 * Add any permissions that we haven't already 321 * done. 322 */ 323 if (perm || (op == '=' && !equalopdone)) { 324 if (op == '=') 325 equalopdone = 1; 326 ADDCMD(op, who, perm, mask); 327 perm = 0; 328 } 329 if (permXbits) { 330 ADDCMD('X', who, permXbits, mask); 331 permXbits = 0; 332 } 333 goto apply; 334 } 335 } 336 337 apply: if (!*p) 338 break; 339 if (*p != ',') 340 goto getop; 341 ++p; 342 } 343 set->cmd = 0; 344 #ifdef SETMODE_DEBUG 345 (void)printf("Before compress_mode()\n"); 346 dumpmode(saveset); 347 #endif 348 compress_mode(saveset); 349 #ifdef SETMODE_DEBUG 350 (void)printf("After compress_mode()\n"); 351 dumpmode(saveset); 352 #endif 353 return (saveset); 354 } 355 356 static BITCMD * 357 addcmd(set, op, who, oparg, mask) 358 BITCMD *set; 359 register int oparg, who; 360 register int op; 361 u_int mask; 362 { 363 switch (op) { 364 case '=': 365 set->cmd = '-'; 366 set->bits = who ? who : STANDARD_BITS; 367 set++; 368 369 op = '+'; 370 /* FALLTHROUGH */ 371 case '+': 372 case '-': 373 case 'X': 374 set->cmd = op; 375 set->bits = (who ? who : mask) & oparg; 376 break; 377 378 case 'u': 379 case 'g': 380 case 'o': 381 set->cmd = op; 382 if (who) { 383 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) | 384 ((who & S_IRGRP) ? CMD2_GBITS : 0) | 385 ((who & S_IROTH) ? CMD2_OBITS : 0); 386 set->bits = ~0; 387 } else { 388 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS; 389 set->bits = mask; 390 } 391 392 if (oparg == '+') 393 set->cmd2 |= CMD2_SET; 394 else if (oparg == '-') 395 set->cmd2 |= CMD2_CLR; 396 else if (oparg == '=') 397 set->cmd2 |= CMD2_SET|CMD2_CLR; 398 break; 399 } 400 return (set + 1); 401 } 402 403 #ifdef SETMODE_DEBUG 404 static void 405 dumpmode(set) 406 register BITCMD *set; 407 { 408 for (; set->cmd; ++set) 409 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n", 410 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "", 411 set->cmd2 & CMD2_CLR ? " CLR" : "", 412 set->cmd2 & CMD2_SET ? " SET" : "", 413 set->cmd2 & CMD2_UBITS ? " UBITS" : "", 414 set->cmd2 & CMD2_GBITS ? " GBITS" : "", 415 set->cmd2 & CMD2_OBITS ? " OBITS" : ""); 416 } 417 #endif 418 419 /* 420 * Given an array of bitcmd structures, compress by compacting consecutive 421 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u', 422 * 'g' and 'o' commands continue to be separate. They could probably be 423 * compacted, but it's not worth the effort. 424 */ 425 static void 426 compress_mode(set) 427 register BITCMD *set; 428 { 429 register BITCMD *nset; 430 register int setbits, clrbits, Xbits, op; 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 } 474