setmode.c revision 1574
1184610Salfred/* 2184610Salfred * Copyright (c) 1989, 1993, 1994 3184610Salfred * The Regents of the University of California. All rights reserved. 4184610Salfred * 5184610Salfred * This code is derived from software contributed to Berkeley by 6184610Salfred * Dave Borman at Cray Research, Inc. 7184610Salfred * 8184610Salfred * Redistribution and use in source and binary forms, with or without 9184610Salfred * modification, are permitted provided that the following conditions 10184610Salfred * are met: 11184610Salfred * 1. Redistributions of source code must retain the above copyright 12184610Salfred * notice, this list of conditions and the following disclaimer. 13184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 14184610Salfred * notice, this list of conditions and the following disclaimer in the 15184610Salfred * documentation and/or other materials provided with the distribution. 16184610Salfred * 3. All advertising materials mentioning features or use of this software 17184610Salfred * must display the following acknowledgement: 18184610Salfred * This product includes software developed by the University of 19184610Salfred * California, Berkeley and its contributors. 20184610Salfred * 4. Neither the name of the University nor the names of its contributors 21184610Salfred * may be used to endorse or promote products derived from this software 22184610Salfred * without specific prior written permission. 23184610Salfred * 24184610Salfred * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34184610Salfred * SUCH DAMAGE. 35184610Salfred */ 36184610Salfred 37184610Salfred#if defined(LIBC_SCCS) && !defined(lint) 38184610Salfredstatic char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94"; 39184610Salfred#endif /* LIBC_SCCS and not lint */ 40184610Salfred 41184610Salfred#include <sys/types.h> 42184610Salfred#include <sys/stat.h> 43184610Salfred 44184610Salfred#include <ctype.h> 45184610Salfred#include <errno.h> 46184610Salfred#include <signal.h> 47184610Salfred#include <stddef.h> 48184610Salfred#include <stdlib.h> 49184610Salfred 50184610Salfred#ifdef SETMODE_DEBUG 51184610Salfred#include <stdio.h> 52184610Salfred#endif 53184610Salfred 54184610Salfred#define SET_LEN 6 /* initial # of bitcmd struct to malloc */ 55184610Salfred#define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */ 56184610Salfred 57184610Salfredtypedef struct bitcmd { 58184610Salfred char cmd; 59184610Salfred char cmd2; 60184610Salfred mode_t bits; 61184610Salfred} BITCMD; 62184610Salfred 63184610Salfred#define CMD2_CLR 0x01 64184610Salfred#define CMD2_SET 0x02 65184610Salfred#define CMD2_GBITS 0x04 66184610Salfred#define CMD2_OBITS 0x08 67194230Sthompsa#define CMD2_UBITS 0x10 68194230Sthompsa 69184610Salfredstatic BITCMD *addcmd __P((BITCMD *, int, int, int, u_int)); 70184610Salfredstatic int compress_mode __P((BITCMD *)); 71184610Salfred#ifdef SETMODE_DEBUG 72184610Salfredstatic void dumpmode __P((BITCMD *)); 73230204Shselasky#endif 74184610Salfred 75184610Salfred/* 76184610Salfred * Given the old mode and an array of bitcmd structures, apply the operations 77184610Salfred * described in the bitcmd structures to the old mode, and return the new mode. 78184610Salfred * Note that there is no '=' command; a strict assignment is just a '-' (clear 79184610Salfred * bits) followed by a '+' (set bits). 80184610Salfred */ 81242619Shselaskymode_t 82184610Salfredgetmode(bbox, omode) 83192984Sthompsa void *bbox; 84192984Sthompsa mode_t omode; 85192984Sthompsa{ 86184610Salfred register BITCMD *set; 87184610Salfred register mode_t clrval, newmode, value; 88188413Sthompsa 89194228Sthompsa set = (BITCMD *)bbox; 90194228Sthompsa newmode = omode; 91184610Salfred for (value = 0;; set++) 92184610Salfred switch(set->cmd) { 93192984Sthompsa /* 94194228Sthompsa * When copying the user, group or other bits around, we "know" 95194228Sthompsa * where the bits are in the mode so that we can do shifts to 96194228Sthompsa * copy them around. If we don't use shifts, it gets real 97194228Sthompsa * grundgy with lots of single bit checks and bit sets. 98197570Sthompsa */ 99194228Sthompsa case 'u': 100194228Sthompsa value = (newmode & S_IRWXU) >> 6; 101194228Sthompsa goto common; 102194228Sthompsa 103194228Sthompsa case 'g': 104194228Sthompsa value = (newmode & S_IRWXG) >> 3; 105194228Sthompsa goto common; 106194228Sthompsa 107194228Sthompsa case 'o': 108194228Sthompsa value = newmode & S_IRWXO; 109214761Sn_hibmacommon: if (set->cmd2 & CMD2_CLR) { 110197570Sthompsa clrval = 111239179Shselasky (set->cmd2 & CMD2_SET) ? S_IRWXO : value; 112184610Salfred if (set->cmd2 & CMD2_UBITS) 113184610Salfred newmode &= ~((clrval<<6) & set->bits); 114184610Salfred if (set->cmd2 & CMD2_GBITS) 115184610Salfred newmode &= ~((clrval<<3) & set->bits); 116184610Salfred if (set->cmd2 & CMD2_OBITS) 117184610Salfred newmode &= ~(clrval & set->bits); 118184610Salfred } 119184610Salfred if (set->cmd2 & CMD2_SET) { 120184610Salfred if (set->cmd2 & CMD2_UBITS) 121184610Salfred newmode |= (value<<6) & set->bits; 122184610Salfred if (set->cmd2 & CMD2_GBITS) 123184610Salfred newmode |= (value<<3) & set->bits; 124184610Salfred if (set->cmd2 & CMD2_OBITS) 125192984Sthompsa newmode |= value & set->bits; 126192984Sthompsa } 127192984Sthompsa break; 128187176Sthompsa 129187176Sthompsa case '+': 130192984Sthompsa newmode |= set->bits; 131192984Sthompsa break; 132192984Sthompsa 133188413Sthompsa case '-': 134187176Sthompsa newmode &= ~set->bits; 135187176Sthompsa break; 136192984Sthompsa 137192984Sthompsa case 'X': 138230204Shselasky if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH)) 139230204Shselasky newmode |= set->bits; 140239179Shselasky break; 141239299Shselasky 142230209Shselasky case '\0': 143230204Shselasky default: 144230209Shselasky#ifdef SETMODE_DEBUG 145184610Salfred (void)printf("getmode:%04o -> %04o\n", omode, newmode); 146184610Salfred#endif 147192984Sthompsa return (newmode); 148188413Sthompsa } 149250576Seadler} 150188413Sthompsa 151188413Sthompsa#define ADDCMD(a, b, c, d) \ 152188413Sthompsa if (set >= endset) { \ 153188413Sthompsa register BITCMD *newset; \ 154188413Sthompsa setlen += SET_LEN_INCR; \ 155188413Sthompsa newset = realloc(saveset, sizeof(BITCMD) * setlen); \ 156188413Sthompsa if (!saveset) \ 157188413Sthompsa return (NULL); \ 158188413Sthompsa set = newset + (set - saveset); \ 159192984Sthompsa saveset = newset; \ 160192984Sthompsa endset = newset + (setlen - 2); \ 161192984Sthompsa } \ 162192984Sthompsa set = addcmd(set, (a), (b), (c), (d)) 163192984Sthompsa 164192984Sthompsa#define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) 165190742Sthompsa 166192984Sthompsavoid * 167192984Sthompsasetmode(p) 168192984Sthompsa register char *p; 169184610Salfred{ 170188413Sthompsa register int perm, who; 171184610Salfred register char op; 172233774Shselasky BITCMD *set, *saveset, *endset; 173242619Shselasky sigset_t sigset, sigoset; 174242619Shselasky mode_t mask; 175184610Salfred int equalopdone, permXbits, setlen; 176197570Sthompsa 177184610Salfred if (!*p) 178184610Salfred return (NULL); 179184610Salfred 180184610Salfred /* 181184610Salfred * Get a copy of the mask for the permissions that are mask relative. 182184610Salfred * Flip the bits, we want what's not set. Since it's possible that 183197570Sthompsa * the caller is opening files inside a signal handler, protect them 184239299Shselasky * as best we can. 185239299Shselasky */ 186244489Shselasky sigfillset(&sigset); 187184610Salfred (void)sigprocmask(SIG_BLOCK, &sigset, &sigoset); 188184610Salfred (void)umask(mask = umask(0)); 189184610Salfred mask = ~mask; 190188413Sthompsa (void)sigprocmask(SIG_SETMASK, &sigoset, NULL); 191188413Sthompsa 192188413Sthompsa setlen = SET_LEN + 2; 193188413Sthompsa 194188413Sthompsa if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL) 195188413Sthompsa return (NULL); 196188413Sthompsa saveset = set; 197197570Sthompsa endset = set + (setlen - 2); 198242619Shselasky 199184610Salfred /* 200184610Salfred * If an absolute number, get it and return; disallow non-octal digits 201239179Shselasky * or illegal bits. 202239179Shselasky */ 203239179Shselasky if (isdigit(*p)) { 204239179Shselasky perm = (mode_t)strtol(p, NULL, 8); 205239179Shselasky if (perm & ~(STANDARD_BITS|S_ISTXT)) { 206239179Shselasky free(saveset); 207194228Sthompsa return (NULL); 208194228Sthompsa } 209188413Sthompsa while (*++p) 210194228Sthompsa if (*p < '0' || *p > '7') { 211233774Shselasky free(saveset); 212192984Sthompsa return (NULL); 213214761Sn_hibma } 214214843Sn_hibma ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask); 215194228Sthompsa return (saveset); 216194228Sthompsa } 217188413Sthompsa 218194228Sthompsa /* 219188413Sthompsa * Build list of structures to set/clear/copy bits as described by 220194228Sthompsa * each clause of the symbolic mode. 221239179Shselasky */ 222239179Shselasky for (;;) { 223239179Shselasky /* First, find out which bits might be modified. */ 224239179Shselasky for (who = 0;; ++p) { 225194230Sthompsa switch (*p) { 226 case 'a': 227 who |= STANDARD_BITS; 228 break; 229 case 'u': 230 who |= S_ISUID|S_IRWXU; 231 break; 232 case 'g': 233 who |= S_ISGID|S_IRWXG; 234 break; 235 case 'o': 236 who |= S_IRWXO; 237 break; 238 default: 239 goto getop; 240 } 241 } 242 243getop: if ((op = *p++) != '+' && op != '-' && op != '=') { 244 free(saveset); 245 return (NULL); 246 } 247 if (op == '=') 248 equalopdone = 0; 249 250 who &= ~S_ISTXT; 251 for (perm = 0, permXbits = 0;; ++p) { 252 switch (*p) { 253 case 'r': 254 perm |= S_IRUSR|S_IRGRP|S_IROTH; 255 break; 256 case 's': 257 /* If only "other" bits ignore set-id. */ 258 if (who & ~S_IRWXO) 259 perm |= S_ISUID|S_ISGID; 260 break; 261 case 't': 262 /* If only "other" bits ignore sticky. */ 263 if (who & ~S_IRWXO) { 264 who |= S_ISTXT; 265 perm |= S_ISTXT; 266 } 267 break; 268 case 'w': 269 perm |= S_IWUSR|S_IWGRP|S_IWOTH; 270 break; 271 case 'X': 272 permXbits = S_IXUSR|S_IXGRP|S_IXOTH; 273 break; 274 case 'x': 275 perm |= S_IXUSR|S_IXGRP|S_IXOTH; 276 break; 277 case 'u': 278 case 'g': 279 case 'o': 280 /* 281 * When ever we hit 'u', 'g', or 'o', we have 282 * to flush out any partial mode that we have, 283 * and then do the copying of the mode bits. 284 */ 285 if (perm) { 286 ADDCMD(op, who, perm, mask); 287 perm = 0; 288 } 289 if (op == '=') 290 equalopdone = 1; 291 if (op == '+' && permXbits) { 292 ADDCMD('X', who, permXbits, mask); 293 permXbits = 0; 294 } 295 ADDCMD(*p, who, op, mask); 296 break; 297 298 default: 299 /* 300 * Add any permissions that we haven't already 301 * done. 302 */ 303 if (perm || (op == '=' && !equalopdone)) { 304 if (op == '=') 305 equalopdone = 1; 306 ADDCMD(op, who, perm, mask); 307 perm = 0; 308 } 309 if (permXbits) { 310 ADDCMD('X', who, permXbits, mask); 311 permXbits = 0; 312 } 313 goto apply; 314 } 315 } 316 317apply: if (!*p) 318 break; 319 if (*p != ',') 320 goto getop; 321 ++p; 322 } 323 set->cmd = 0; 324#ifdef SETMODE_DEBUG 325 (void)printf("Before compress_mode()\n"); 326 dumpmode(saveset); 327#endif 328 compress_mode(saveset); 329#ifdef SETMODE_DEBUG 330 (void)printf("After compress_mode()\n"); 331 dumpmode(saveset); 332#endif 333 return (saveset); 334} 335 336static BITCMD * 337addcmd(set, op, who, oparg, mask) 338 BITCMD *set; 339 register int oparg, who; 340 register int op; 341 u_int mask; 342{ 343 switch (op) { 344 case '=': 345 set->cmd = '-'; 346 set->bits = who ? who : STANDARD_BITS; 347 set++; 348 349 op = '+'; 350 /* FALLTHROUGH */ 351 case '+': 352 case '-': 353 case 'X': 354 set->cmd = op; 355 set->bits = (who ? who : mask) & oparg; 356 break; 357 358 case 'u': 359 case 'g': 360 case 'o': 361 set->cmd = op; 362 if (who) { 363 set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) | 364 ((who & S_IRGRP) ? CMD2_GBITS : 0) | 365 ((who & S_IROTH) ? CMD2_OBITS : 0); 366 set->bits = ~0; 367 } else { 368 set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS; 369 set->bits = mask; 370 } 371 372 if (oparg == '+') 373 set->cmd2 |= CMD2_SET; 374 else if (oparg == '-') 375 set->cmd2 |= CMD2_CLR; 376 else if (oparg == '=') 377 set->cmd2 |= CMD2_SET|CMD2_CLR; 378 break; 379 } 380 return (set + 1); 381} 382 383#ifdef SETMODE_DEBUG 384static void 385dumpmode(set) 386 register BITCMD *set; 387{ 388 for (; set->cmd; ++set) 389 (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n", 390 set->cmd, set->bits, set->cmd2 ? " cmd2:" : "", 391 set->cmd2 & CMD2_CLR ? " CLR" : "", 392 set->cmd2 & CMD2_SET ? " SET" : "", 393 set->cmd2 & CMD2_UBITS ? " UBITS" : "", 394 set->cmd2 & CMD2_GBITS ? " GBITS" : "", 395 set->cmd2 & CMD2_OBITS ? " OBITS" : ""); 396} 397#endif 398 399/* 400 * Given an array of bitcmd structures, compress by compacting consecutive 401 * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u', 402 * 'g' and 'o' commands continue to be separate. They could probably be 403 * compacted, but it's not worth the effort. 404 */ 405static int 406compress_mode(set) 407 register BITCMD *set; 408{ 409 register BITCMD *nset; 410 register int setbits, clrbits, Xbits, op; 411 412 for (nset = set;;) { 413 /* Copy over any 'u', 'g' and 'o' commands. */ 414 while ((op = nset->cmd) != '+' && op != '-' && op != 'X') { 415 *set++ = *nset++; 416 if (!op) 417 return; 418 } 419 420 for (setbits = clrbits = Xbits = 0;; nset++) { 421 if ((op = nset->cmd) == '-') { 422 clrbits |= nset->bits; 423 setbits &= ~nset->bits; 424 Xbits &= ~nset->bits; 425 } else if (op == '+') { 426 setbits |= nset->bits; 427 clrbits &= ~nset->bits; 428 Xbits &= ~nset->bits; 429 } else if (op == 'X') 430 Xbits |= nset->bits & ~setbits; 431 else 432 break; 433 } 434 if (clrbits) { 435 set->cmd = '-'; 436 set->cmd2 = 0; 437 set->bits = clrbits; 438 set++; 439 } 440 if (setbits) { 441 set->cmd = '+'; 442 set->cmd2 = 0; 443 set->bits = setbits; 444 set++; 445 } 446 if (Xbits) { 447 set->cmd = 'X'; 448 set->cmd2 = 0; 449 set->bits = Xbits; 450 set++; 451 } 452 } 453} 454