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