1 /* $OpenBSD: setmode.c,v 1.9 1998/11/18 23:28:34 deraadt Exp $ */ 2 /* $NetBSD: setmode.c,v 1.15 1997/02/07 22:21:06 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1989, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Dave Borman at Cray Research, Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 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 static char rcsid[] = "$OpenBSD: setmode.c,v 1.9 1998/11/18 23:28:34 deraadt 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 #ifdef __STDC__ 90 getmode(const void *bbox, mode_t omode) 91 #else 92 getmode(bbox, omode) 93 const void *bbox; 94 mode_t omode; 95 #endif 96 { 97 register const BITCMD *set; 98 register mode_t clrval, newmode, value; 99 100 set = (const BITCMD *)bbox; 101 newmode = omode; 102 for (value = 0;; set++) 103 switch(set->cmd) { 104 /* 105 * When copying the user, group or other bits around, we "know" 106 * where the bits are in the mode so that we can do shifts to 107 * copy them around. If we don't use shifts, it gets real 108 * grundgy with lots of single bit checks and bit sets. 109 */ 110 case 'u': 111 value = (newmode & S_IRWXU) >> 6; 112 goto common; 113 114 case 'g': 115 value = (newmode & S_IRWXG) >> 3; 116 goto common; 117 118 case 'o': 119 value = newmode & S_IRWXO; 120 common: if (set->cmd2 & CMD2_CLR) { 121 clrval = 122 (set->cmd2 & CMD2_SET) ? S_IRWXO : value; 123 if (set->cmd2 & CMD2_UBITS) 124 newmode &= ~((clrval<<6) & set->bits); 125 if (set->cmd2 & CMD2_GBITS) 126 newmode &= ~((clrval<<3) & set->bits); 127 if (set->cmd2 & CMD2_OBITS) 128 newmode &= ~(clrval & set->bits); 129 } 130 if (set->cmd2 & CMD2_SET) { 131 if (set->cmd2 & CMD2_UBITS) 132 newmode |= (value<<6) & set->bits; 133 if (set->cmd2 & CMD2_GBITS) 134 newmode |= (value<<3) & set->bits; 135 if (set->cmd2 & CMD2_OBITS) 136 newmode |= value & set->bits; 137 } 138 break; 139 140 case '+': 141 newmode |= set->bits; 142 break; 143 144 case '-': 145 newmode &= ~set->bits; 146 break; 147 148 case 'X': 149 if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH)) 150 newmode |= set->bits; 151 break; 152 153 case '\0': 154 default: 155 #ifdef SETMODE_DEBUG 156 (void)printf("getmode:%04o -> %04o\n", omode, newmode); 157 #endif 158 return (newmode); 159 } 160 } 161 162 #define ADDCMD(a, b, c, d) \ 163 if (set >= endset) { \ 164 register BITCMD *newset; \ 165 setlen += SET_LEN_INCR; \ 166 newset = realloc(saveset, sizeof(BITCMD) * setlen); \ 167 if (!newset) { \ 168 if (saveset) \ 169 free(saveset); \ 170 saveset = NULL; \ 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 179 #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) 180 181 void * 182 setmode(p) 183 register const char *p; 184 { 185 register int perm, who; 186 register char op; 187 BITCMD *set, *saveset, *endset; 188 sigset_t sigset, sigoset; 189 mode_t mask; 190 int equalopdone, permXbits, setlen; 191 long perml; 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. Since it's possible that 199 * the caller is opening files inside a signal handler, protect them 200 * as best we can. 201 */ 202 sigfillset(&sigset); 203 (void)sigprocmask(SIG_BLOCK, &sigset, &sigoset); 204 (void)umask(mask = umask(0)); 205 mask = ~mask; 206 (void)sigprocmask(SIG_SETMASK, &sigoset, NULL); 207 208 setlen = SET_LEN + 2; 209 210 if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL) 211 return (NULL); 212 saveset = set; 213 endset = set + (setlen - 2); 214 215 /* 216 * If an absolute number, get it and return; disallow non-octal digits 217 * or illegal bits. 218 */ 219 if (isdigit(*p)) { 220 perml = strtol(p, NULL, 8); 221 if (perml < 0 || (perml & ~(STANDARD_BITS|S_ISTXT))) { 222 free(saveset); 223 return (NULL); 224 } 225 perm = (mode_t)perml; 226 while (*++p) 227 if (*p < '0' || *p > '7') { 228 free(saveset); 229 return (NULL); 230 } 231 ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask); 232 return (saveset); 233 } 234 235 /* 236 * Build list of structures to set/clear/copy bits as described by 237 * each clause of the symbolic mode. 238 */ 239 for (;;) { 240 /* First, find out which bits might be modified. */ 241 for (who = 0;; ++p) { 242 switch (*p) { 243 case 'a': 244 who |= STANDARD_BITS; 245 break; 246 case 'u': 247 who |= S_ISUID|S_IRWXU; 248 break; 249 case 'g': 250 who |= S_ISGID|S_IRWXG; 251 break; 252 case 'o': 253 who |= S_IRWXO; 254 break; 255 default: 256 goto getop; 257 } 258 } 259 260 getop: if ((op = *p++) != '+' && op != '-' && op != '=') { 261 free(saveset); 262 return (NULL); 263 } 264 if (op == '=') 265 equalopdone = 0; 266 267 who &= ~S_ISTXT; 268 for (perm = 0, permXbits = 0;; ++p) { 269 switch (*p) { 270 case 'r': 271 perm |= S_IRUSR|S_IRGRP|S_IROTH; 272 break; 273 case 's': 274 /* 275 * If specific bits where requested and 276 * only "other" bits ignore set-id. 277 */ 278 if (who == 0 || (who & ~S_IRWXO)) 279 perm |= S_ISUID|S_ISGID; 280 break; 281 case 't': 282 /* 283 * If specific bits where requested and 284 * only "other" bits ignore sticky. 285 */ 286 if (who == 0 || (who & ~S_IRWXO)) { 287 who |= S_ISTXT; 288 perm |= S_ISTXT; 289 } 290 break; 291 case 'w': 292 perm |= S_IWUSR|S_IWGRP|S_IWOTH; 293 break; 294 case 'X': 295 permXbits = S_IXUSR|S_IXGRP|S_IXOTH; 296 break; 297 case 'x': 298 perm |= S_IXUSR|S_IXGRP|S_IXOTH; 299 break; 300 case 'u': 301 case 'g': 302 case 'o': 303 /* 304 * When ever we hit 'u', 'g', or 'o', we have 305 * to flush out any partial mode that we have, 306 * and then do the copying of the mode bits. 307 */ 308 if (perm) { 309 ADDCMD(op, who, perm, mask); 310 perm = 0; 311 } 312 if (op == '=') 313 equalopdone = 1; 314 if (op == '+' && permXbits) { 315 ADDCMD('X', who, permXbits, mask); 316 permXbits = 0; 317 } 318 ADDCMD(*p, who, op, mask); 319 break; 320 321 default: 322 /* 323 * Add any permissions that we haven't already 324 * done. 325 */ 326 if (perm || (op == '=' && !equalopdone)) { 327 if (op == '=') 328 equalopdone = 1; 329 ADDCMD(op, who, perm, mask); 330 perm = 0; 331 } 332 if (permXbits) { 333 ADDCMD('X', who, permXbits, mask); 334 permXbits = 0; 335 } 336 goto apply; 337 } 338 } 339 340 apply: if (!*p) 341 break; 342 if (*p != ',') 343 goto getop; 344 ++p; 345 } 346 set->cmd = 0; 347 #ifdef SETMODE_DEBUG 348 (void)printf("Before compress_mode()\n"); 349 dumpmode(saveset); 350 #endif 351 compress_mode(saveset); 352 #ifdef SETMODE_DEBUG 353 (void)printf("After compress_mode()\n"); 354 dumpmode(saveset); 355 #endif 356 return (saveset); 357 } 358 359 static BITCMD * 360 addcmd(set, op, who, oparg, mask) 361 BITCMD *set; 362 register int oparg, who; 363 register int op; 364 u_int mask; 365 { 366 switch (op) { 367 case '=': 368 set->cmd = '-'; 369 set->bits = who ? who : STANDARD_BITS; 370 set++; 371 372 op = '+'; 373 /* FALLTHROUGH */ 374 case '+': 375 case '-': 376 case 'X': 377 set->cmd = op; 378 set->bits = (who ? who : mask) & oparg; 379 break; 380 381 case 'u': 382 case 'g': 383 case 'o': 384 set->cmd = op; 385 if (who) { 386 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) | 387 ((who & S_IRGRP) ? CMD2_GBITS : 0) | 388 ((who & S_IROTH) ? CMD2_OBITS : 0); 389 set->bits = (mode_t)~0; 390 } else { 391 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS; 392 set->bits = mask; 393 } 394 395 if (oparg == '+') 396 set->cmd2 |= CMD2_SET; 397 else if (oparg == '-') 398 set->cmd2 |= CMD2_CLR; 399 else if (oparg == '=') 400 set->cmd2 |= CMD2_SET|CMD2_CLR; 401 break; 402 } 403 return (set + 1); 404 } 405 406 #ifdef SETMODE_DEBUG 407 static void 408 dumpmode(set) 409 register BITCMD *set; 410 { 411 for (; set->cmd; ++set) 412 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n", 413 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "", 414 set->cmd2 & CMD2_CLR ? " CLR" : "", 415 set->cmd2 & CMD2_SET ? " SET" : "", 416 set->cmd2 & CMD2_UBITS ? " UBITS" : "", 417 set->cmd2 & CMD2_GBITS ? " GBITS" : "", 418 set->cmd2 & CMD2_OBITS ? " OBITS" : ""); 419 } 420 #endif 421 422 /* 423 * Given an array of bitcmd structures, compress by compacting consecutive 424 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u', 425 * 'g' and 'o' commands continue to be separate. They could probably be 426 * compacted, but it's not worth the effort. 427 */ 428 static void 429 compress_mode(set) 430 register BITCMD *set; 431 { 432 register BITCMD *nset; 433 register int setbits, clrbits, Xbits, op; 434 435 for (nset = set;;) { 436 /* Copy over any 'u', 'g' and 'o' commands. */ 437 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') { 438 *set++ = *nset++; 439 if (!op) 440 return; 441 } 442 443 for (setbits = clrbits = Xbits = 0;; nset++) { 444 if ((op = nset->cmd) == '-') { 445 clrbits |= nset->bits; 446 setbits &= ~nset->bits; 447 Xbits &= ~nset->bits; 448 } else if (op == '+') { 449 setbits |= nset->bits; 450 clrbits &= ~nset->bits; 451 Xbits &= ~nset->bits; 452 } else if (op == 'X') 453 Xbits |= nset->bits & ~setbits; 454 else 455 break; 456 } 457 if (clrbits) { 458 set->cmd = '-'; 459 set->cmd2 = 0; 460 set->bits = clrbits; 461 set++; 462 } 463 if (setbits) { 464 set->cmd = '+'; 465 set->cmd2 = 0; 466 set->bits = setbits; 467 set++; 468 } 469 if (Xbits) { 470 set->cmd = 'X'; 471 set->cmd2 = 0; 472 set->bits = Xbits; 473 set++; 474 } 475 } 476 } 477