139978Sbostic /* 239978Sbostic * Copyright (c) 1989 The Regents of the University of California. 339978Sbostic * All rights reserved. 439978Sbostic * 542626Sbostic * %sccs.include.redist.c% 639978Sbostic */ 739978Sbostic 839978Sbostic #if defined(LIBC_SCCS) && !defined(lint) 9*46576Sbostic static char sccsid[] = "@(#)setmode.c 5.5 (Berkeley) 02/23/91"; 1039978Sbostic #endif /* LIBC_SCCS and not lint */ 1139978Sbostic 1245901Sbostic #include <sys/param.h> 1339978Sbostic #include <sys/stat.h> 1445901Sbostic #include <sys/errno.h> 1545901Sbostic #ifdef SETMODE_DEBUG 1641579Sbostic #include <stdio.h> 1745901Sbostic #endif 1845901Sbostic #include <stdlib.h> 1939978Sbostic 2045901Sbostic #define SET_LEN 6 /* initial # of bitcmd struct to malloc */ 2145901Sbostic #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */ 2239978Sbostic 2345901Sbostic struct bitcmd { 2445901Sbostic char cmd; 2545901Sbostic char cmd2; 2645901Sbostic mode_t bits; 2745901Sbostic }; 2845901Sbostic 2945901Sbostic #define CMD2_CLR 0x01 3045901Sbostic #define CMD2_SET 0x02 3145901Sbostic #define CMD2_GBITS 0x04 3245901Sbostic #define CMD2_OBITS 0x08 3345901Sbostic #define CMD2_UBITS 0x10 3445901Sbostic 3545901Sbostic /* 3645901Sbostic * Given the old mode and an array of bitcmd structures, apply the operations 3745901Sbostic * described in the bitcmd structures to the old mode, and return the new mode. 3845901Sbostic * Note that there is no '=' command; a strict assignment is just a '-' (clear 3945901Sbostic * bits) followed by a '+' (set bits). 4045901Sbostic */ 4139978Sbostic mode_t 42*46576Sbostic getmode(bbox, omode) 43*46576Sbostic void *bbox; 4445901Sbostic mode_t omode; 4539978Sbostic { 46*46576Sbostic register struct bitcmd *set; 4745901Sbostic register mode_t newmode, value; 4839978Sbostic 49*46576Sbostic set = (struct bitcmd *)bbox; 5045901Sbostic newmode = omode; 5145901Sbostic for (value = 0;; set++) 5245901Sbostic switch(set->cmd) { 5345901Sbostic /* 5445901Sbostic * When copying the user, group or other bits around, we "know" 5545901Sbostic * where the bit are in the mode so that we can do shifts to 5645901Sbostic * copy them around. If we don't use shifts, it gets real 5745901Sbostic * grundgy with lots of single bit checks and bit sets. 5845901Sbostic */ 5945901Sbostic case 'u': 6045901Sbostic value = (newmode & S_IRWXU) >> 6; 6145901Sbostic goto common; 6245901Sbostic 6345901Sbostic case 'g': 6445901Sbostic value = (newmode & S_IRWXG) >> 3; 6545901Sbostic goto common; 6645901Sbostic 6745901Sbostic case 'o': 6845901Sbostic value = newmode & S_IRWXO; 6945901Sbostic common: 7045901Sbostic if (set->cmd2 & CMD2_CLR) { 7145901Sbostic if (set->cmd2 & CMD2_UBITS) 7245901Sbostic newmode &= ~(S_IRWXU & set->bits); 7345901Sbostic if (set->cmd2 & CMD2_GBITS) 7445901Sbostic newmode &= ~(S_IRWXG & set->bits); 7545901Sbostic if (set->cmd2 & CMD2_OBITS) 7645901Sbostic newmode &= ~(S_IRWXO & set->bits); 7745901Sbostic } 7845901Sbostic if (set->cmd2 & CMD2_SET) { 7945901Sbostic if (set->cmd2 & CMD2_UBITS) 8045901Sbostic newmode |= (value<<6) & set->bits; 8145901Sbostic if (set->cmd2 & CMD2_GBITS) 8245901Sbostic newmode |= (value<<3) & set->bits; 8345901Sbostic if (set->cmd2 & CMD2_OBITS) 8445901Sbostic newmode |= value & set->bits; 8545901Sbostic } 8645901Sbostic break; 8745901Sbostic 8845901Sbostic case '+': 8945901Sbostic newmode |= set->bits; 9045901Sbostic break; 9145901Sbostic 9245901Sbostic case '-': 9345901Sbostic newmode &= ~set->bits; 9445901Sbostic break; 9545901Sbostic 9645901Sbostic case 'X': 9745901Sbostic if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH)) 9845901Sbostic newmode |= set->bits; 9945901Sbostic break; 10045901Sbostic 10145901Sbostic case '\0': 10245901Sbostic default: 10345901Sbostic #ifdef SETMODE_DEBUG 10445901Sbostic (void)printf("getmode(, %04o) -> %04o\n", 10545901Sbostic omode, newmode); 10645901Sbostic #endif 10745901Sbostic return(newmode); 10845901Sbostic } 10939978Sbostic } 11039978Sbostic 11139978Sbostic #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) 11239978Sbostic 11345901Sbostic static struct bitcmd * 11445901Sbostic addcmd(set, op, who, oparg, mask) 11545901Sbostic struct bitcmd *set; 11645901Sbostic register int oparg, who; 11745901Sbostic register int op; 11845901Sbostic mode_t mask; 11945901Sbostic { 12045901Sbostic switch (op) { 12145901Sbostic case '+': 12245901Sbostic case 'X': 12345901Sbostic set->cmd = op; 12445901Sbostic set->bits = (who ? who : mask) & oparg; 12545901Sbostic break; 12645901Sbostic 12745901Sbostic case '-': 12845901Sbostic set->cmd = '-'; 12945901Sbostic set->bits = (who ? who : (S_IRWXU|S_IRWXG|S_IRWXO)) & oparg; 13045901Sbostic break; 13145901Sbostic 13245901Sbostic case '=': 13345901Sbostic set->cmd = '-'; 13445901Sbostic if (!who) { 13545901Sbostic set->bits = STANDARD_BITS; 13645901Sbostic who = mask; 13745901Sbostic } else 13845901Sbostic set->bits = who; 13945901Sbostic set++; 14045901Sbostic 14145901Sbostic set->cmd = '+'; 14245901Sbostic set->bits = who & oparg; 14345901Sbostic break; 14445901Sbostic case 'u': 14545901Sbostic case 'g': 14645901Sbostic case 'o': 14745901Sbostic set->cmd = op; 14845901Sbostic if (who) { 14945901Sbostic set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) | 15045901Sbostic ((who & S_IRGRP) ? CMD2_GBITS : 0) | 15145901Sbostic ((who & S_IROTH) ? CMD2_OBITS : 0); 15245901Sbostic set->bits = ~0; 15345901Sbostic } else { 15445901Sbostic set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS; 15545901Sbostic set->bits = mask; 15645901Sbostic } 15745901Sbostic 15845901Sbostic if (oparg == '+') 15945901Sbostic set->cmd2 |= CMD2_SET; 16045901Sbostic else if (oparg == '-') 16145901Sbostic set->cmd2 |= CMD2_CLR; 16245901Sbostic else if (oparg == '=') 16345901Sbostic set->cmd2 |= CMD2_SET|CMD2_CLR; 16445901Sbostic break; 16545901Sbostic } 16645901Sbostic return(set+1); 16745901Sbostic } 16845901Sbostic 16945901Sbostic #define ADDCMD(a, b, c, d) \ 17045901Sbostic if (set >= endset) { \ 17145901Sbostic register struct bitcmd *newset; \ 17245901Sbostic setlen += SET_LEN_INCR; \ 17345901Sbostic newset = realloc(saveset, sizeof(struct bitcmd) * setlen); \ 17445901Sbostic if (!saveset) \ 17545901Sbostic return(NULL); \ 17645901Sbostic set = newset + (set - saveset); \ 17745901Sbostic saveset = newset; \ 17845901Sbostic endset = newset + (setlen - 2); \ 17945901Sbostic } \ 18045901Sbostic set = addcmd(set, (a), (b), (c), (d)) 18145901Sbostic 182*46576Sbostic void * 18339978Sbostic setmode(p) 18439978Sbostic register char *p; 18539978Sbostic { 18639978Sbostic register int perm, who; 18739978Sbostic register char op; 18845901Sbostic mode_t mask; 18945901Sbostic struct bitcmd *set, *saveset, *endset; 19045901Sbostic int permXbits, setlen; 191*46576Sbostic static int compress_mode(); 19239978Sbostic 19339978Sbostic /* 19445901Sbostic * Get a copy of the mask for the permissions that are mask relative. 19545901Sbostic * Flip the bits, we want what's not set. 19639978Sbostic */ 19739978Sbostic (void)umask(mask = umask(0)); 19839978Sbostic mask = ~mask; 19939978Sbostic 20045901Sbostic setlen = SET_LEN + 2; 20145901Sbostic 20245901Sbostic set = (struct bitcmd *)malloc((u_int)(sizeof(struct bitcmd) * setlen)); 20345901Sbostic if (!set) 20441579Sbostic return(NULL); 20545901Sbostic saveset = set; 20645901Sbostic endset = set + (setlen - 2); 20741579Sbostic 20839978Sbostic /* 20945901Sbostic * If an absolute number, get it and return; disallow non-octal digits 21045901Sbostic * or illegal bits. 21139978Sbostic */ 21239978Sbostic if (isdigit(*p)) { 21345901Sbostic perm = (mode_t)strtol(p, (char **)0, 8); 21445901Sbostic if (perm & ~(STANDARD_BITS|S_ISTXT)) { 21545901Sbostic free(saveset); 21645901Sbostic return(NULL); 21745901Sbostic } 21839978Sbostic while (*++p) 21945901Sbostic if (*p < '0' || *p > '7') { 22045901Sbostic free(saveset); 22141579Sbostic return(NULL); 22245901Sbostic } 22345901Sbostic ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask); 224*46576Sbostic return((void *)saveset); 22539978Sbostic } 22639978Sbostic 22745901Sbostic if (!*p) { 22845901Sbostic free(saveset); 22941579Sbostic return(NULL); 23045901Sbostic } 23139978Sbostic /* 23245901Sbostic * Build list of structures to set/clear/copy bits as described by 23345901Sbostic * each clause of the symbolic mode. 23439978Sbostic */ 23539978Sbostic for (;;) { 23645901Sbostic /* First, find out which bits might be modified. */ 23745901Sbostic for (who = 0;; ++p) { 23839978Sbostic switch (*p) { 23939978Sbostic case 'a': 24039978Sbostic who |= STANDARD_BITS; 24139978Sbostic break; 24239978Sbostic case 'u': 24339978Sbostic who |= S_ISUID|S_IRWXU; 24439978Sbostic break; 24539978Sbostic case 'g': 24639978Sbostic who |= S_ISGID|S_IRWXG; 24739978Sbostic break; 24839978Sbostic case 'o': 24939978Sbostic who |= S_IRWXO; 25039978Sbostic break; 25139978Sbostic default: 25239978Sbostic goto getop; 25339978Sbostic } 25445901Sbostic } 25545901Sbostic getop: 25639978Sbostic 25745901Sbostic if ((op = *p++) != '+' && op != '-' && op != '=') { 25845901Sbostic free(saveset); 25941579Sbostic return(NULL); 26045901Sbostic } 26139978Sbostic 26239978Sbostic who &= ~S_ISTXT; 26345901Sbostic for (perm = 0, permXbits = 0;; ++p) { 26439978Sbostic switch (*p) { 26539978Sbostic case 'r': 26639978Sbostic perm |= S_IRUSR|S_IRGRP|S_IROTH; 26739978Sbostic break; 26839978Sbostic case 's': 26945901Sbostic /* If only "other" bits ignore set-id. */ 27039978Sbostic if (who & ~S_IRWXO) 27139978Sbostic perm |= S_ISUID|S_ISGID; 27239978Sbostic break; 27339978Sbostic case 't': 27445901Sbostic /* If only "other" bits ignore sticky. */ 27539978Sbostic if (who & ~S_IRWXO) { 27639978Sbostic who |= S_ISTXT; 27739978Sbostic perm |= S_ISTXT; 27839978Sbostic } 27939978Sbostic break; 28039978Sbostic case 'w': 28139978Sbostic perm |= S_IWUSR|S_IWGRP|S_IWOTH; 28239978Sbostic break; 28339978Sbostic case 'X': 28439978Sbostic permXbits = S_IXUSR|S_IXGRP|S_IXOTH; 28539978Sbostic break; 28639978Sbostic case 'x': 28739978Sbostic perm |= S_IXUSR|S_IXGRP|S_IXOTH; 28839978Sbostic break; 28945901Sbostic case 'u': 29045901Sbostic case 'g': 29145901Sbostic case 'o': 29245901Sbostic /* 29345901Sbostic * When ever we hit 'u', 'g', or 'o', we have 29445901Sbostic * to flush out any partial mode that we have, 29545901Sbostic * and then do the copying of the mode bits. 29645901Sbostic */ 29745901Sbostic if (perm) { 29845901Sbostic ADDCMD(op, who, perm, mask); 29945901Sbostic perm = 0; 30045901Sbostic } 30145901Sbostic if (op == '+' && permXbits) { 30245901Sbostic ADDCMD('X', who, permXbits, mask); 30345901Sbostic permXbits = 0; 30445901Sbostic } 30545901Sbostic ADDCMD(*p, who, op, mask); 30645901Sbostic break; 30745901Sbostic 30839978Sbostic default: 30945901Sbostic /* 31045901Sbostic * Add any permissions that we haven't already 31145901Sbostic * done. 31245901Sbostic */ 31345901Sbostic if (perm) { 31445901Sbostic ADDCMD(op, who, perm, mask); 31545901Sbostic perm = 0; 31645901Sbostic } 31745901Sbostic if (permXbits) { 31845901Sbostic ADDCMD('X', who, permXbits, mask); 31945901Sbostic permXbits = 0; 32045901Sbostic } 32139978Sbostic goto apply; 32239978Sbostic } 32339978Sbostic } 32439978Sbostic 32545901Sbostic apply: if (!*p) 32639978Sbostic break; 32739978Sbostic if (*p != ',') 32839978Sbostic goto getop; 32939978Sbostic ++p; 33039978Sbostic } 33145901Sbostic set->cmd = 0; 33245901Sbostic #ifdef SETMODE_DEBUG 33345901Sbostic (void)printf("Before compress_mode()\n"); 33445901Sbostic dumpmode(saveset); 33545901Sbostic #endif 33645901Sbostic compress_mode(saveset); 33745901Sbostic #ifdef SETMODE_DEBUG 33845901Sbostic (void)printf("After compress_mode()\n"); 33945901Sbostic dumpmode(saveset); 34045901Sbostic #endif 341*46576Sbostic return((void *)saveset); 34239978Sbostic } 34345901Sbostic 34445901Sbostic #ifdef SETMODE_DEBUG 34545901Sbostic dumpmode(set) 34645901Sbostic register struct bitcmd *set; 34745901Sbostic { 34845901Sbostic for (; set->cmd; ++set) 34945901Sbostic (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n", 35045901Sbostic set->cmd, set->bits, set->cmd2 ? " cmd2:" : "", 35145901Sbostic set->cmd2 & CMD2_CLR ? " CLR" : "", 35245901Sbostic set->cmd2 & CMD2_SET ? " SET" : "", 35345901Sbostic set->cmd2 & CMD2_UBITS ? " UBITS" : "", 35445901Sbostic set->cmd2 & CMD2_GBITS ? " GBITS" : "", 35545901Sbostic set->cmd2 & CMD2_OBITS ? " OBITS" : ""); 35645901Sbostic } 35745901Sbostic #endif 35845901Sbostic 35945901Sbostic /* 36045901Sbostic * Given an array of bitcmd structures, compress by compacting consecutive 36145901Sbostic * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u', 36245901Sbostic * 'g' and 'o' commands continue to be separate. They could probably be 36345901Sbostic * compacted, but it's not worth the effort. 36445901Sbostic */ 36545901Sbostic static 36645901Sbostic compress_mode(set) 36745901Sbostic register struct bitcmd *set; 36845901Sbostic { 36945901Sbostic register struct bitcmd *nset; 37045901Sbostic register int setbits, clrbits, Xbits, op; 37145901Sbostic 37245901Sbostic for (nset = set;;) { 37345901Sbostic /* Copy over any 'u', 'g' and 'o' commands. */ 37445901Sbostic while ((op = nset->cmd) != '+' && op != '-' && op != 'X') { 37545901Sbostic *set++ = *nset++; 37645901Sbostic if (!op) 37745901Sbostic return; 37845901Sbostic } 37945901Sbostic 38045901Sbostic for (setbits = clrbits = Xbits = 0;; nset++) { 38145901Sbostic if ((op = nset->cmd) == '-') { 38245901Sbostic clrbits |= nset->bits; 38345901Sbostic setbits &= ~nset->bits; 38445901Sbostic Xbits &= ~nset->bits; 38545901Sbostic } else if (op == '+') { 38645901Sbostic setbits |= nset->bits; 38745901Sbostic clrbits &= ~nset->bits; 38845901Sbostic Xbits &= ~nset->bits; 38945901Sbostic } else if (op == 'X') 39045901Sbostic Xbits |= nset->bits & ~setbits; 39145901Sbostic else 39245901Sbostic break; 39345901Sbostic } 39445901Sbostic if (clrbits) { 39545901Sbostic set->cmd = '-'; 39645901Sbostic set->cmd2 = 0; 39745901Sbostic set->bits = clrbits; 39845901Sbostic set++; 39945901Sbostic } 40045901Sbostic if (setbits) { 40145901Sbostic set->cmd = '+'; 40245901Sbostic set->cmd2 = 0; 40345901Sbostic set->bits = setbits; 40445901Sbostic set++; 40545901Sbostic } 40645901Sbostic if (Xbits) { 40745901Sbostic set->cmd = 'X'; 40845901Sbostic set->cmd2 = 0; 40945901Sbostic set->bits = Xbits; 41045901Sbostic set++; 41145901Sbostic } 41245901Sbostic } 41345901Sbostic } 414