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