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