158f0484fSRodney W. Grimes /* 258f0484fSRodney W. Grimes * Copyright (c) 1989, 1993, 1994 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 658f0484fSRodney W. Grimes * Dave Borman at Cray Research, Inc. 758f0484fSRodney W. Grimes * 858f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 958f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1058f0484fSRodney W. Grimes * are met: 1158f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1258f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1358f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1458f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1558f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1658f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1758f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1858f0484fSRodney W. Grimes * without specific prior written permission. 1958f0484fSRodney W. Grimes * 2058f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2158f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2258f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2358f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2458f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2558f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2658f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2758f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2858f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2958f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3058f0484fSRodney W. Grimes * SUCH DAMAGE. 3158f0484fSRodney W. Grimes */ 3258f0484fSRodney W. Grimes 3358f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3458f0484fSRodney W. Grimes static char sccsid[] = "@(#)setmode.c 8.2 (Berkeley) 3/25/94"; 3558f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 36b231cb39SDavid E. O'Brien #include <sys/cdefs.h> 37b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3858f0484fSRodney W. Grimes 39d201fe46SDaniel Eischen #include "namespace.h" 4058f0484fSRodney W. Grimes #include <sys/types.h> 4158f0484fSRodney W. Grimes #include <sys/stat.h> 42*73cc817eSJilles Tjoelker #include <sys/sysctl.h> 4358f0484fSRodney W. Grimes 4458f0484fSRodney W. Grimes #include <ctype.h> 45e382e86bSPedro F. Giffuni #include <errno.h> 46e382e86bSPedro F. Giffuni #include <limits.h> 4758f0484fSRodney W. Grimes #include <signal.h> 4858f0484fSRodney W. Grimes #include <stddef.h> 4958f0484fSRodney W. Grimes #include <stdlib.h> 50215d1a9eSMike Heffner #include <unistd.h> 5158f0484fSRodney W. Grimes 5258f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG 5358f0484fSRodney W. Grimes #include <stdio.h> 5458f0484fSRodney W. Grimes #endif 55d201fe46SDaniel Eischen #include "un-namespace.h" 5658f0484fSRodney W. Grimes 5758f0484fSRodney W. Grimes #define SET_LEN 6 /* initial # of bitcmd struct to malloc */ 5858f0484fSRodney W. Grimes #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */ 5958f0484fSRodney W. Grimes 6058f0484fSRodney W. Grimes typedef struct bitcmd { 6158f0484fSRodney W. Grimes char cmd; 6258f0484fSRodney W. Grimes char cmd2; 6358f0484fSRodney W. Grimes mode_t bits; 6458f0484fSRodney W. Grimes } BITCMD; 6558f0484fSRodney W. Grimes 6658f0484fSRodney W. Grimes #define CMD2_CLR 0x01 6758f0484fSRodney W. Grimes #define CMD2_SET 0x02 6858f0484fSRodney W. Grimes #define CMD2_GBITS 0x04 6958f0484fSRodney W. Grimes #define CMD2_OBITS 0x08 7058f0484fSRodney W. Grimes #define CMD2_UBITS 0x10 7158f0484fSRodney W. Grimes 72*73cc817eSJilles Tjoelker static mode_t getumask(void); 73e382e86bSPedro F. Giffuni static BITCMD *addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t); 74b231cb39SDavid E. O'Brien static void compress_mode(BITCMD *); 7558f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG 76b231cb39SDavid E. O'Brien static void dumpmode(BITCMD *); 7758f0484fSRodney W. Grimes #endif 7858f0484fSRodney W. Grimes 7958f0484fSRodney W. Grimes /* 8058f0484fSRodney W. Grimes * Given the old mode and an array of bitcmd structures, apply the operations 8158f0484fSRodney W. Grimes * described in the bitcmd structures to the old mode, and return the new mode. 8258f0484fSRodney W. Grimes * Note that there is no '=' command; a strict assignment is just a '-' (clear 8358f0484fSRodney W. Grimes * bits) followed by a '+' (set bits). 8458f0484fSRodney W. Grimes */ 8558f0484fSRodney W. Grimes mode_t 86fd42c4d8SStefan Farfeleder getmode(const void *bbox, mode_t omode) 8758f0484fSRodney W. Grimes { 88215d1a9eSMike Heffner const BITCMD *set; 89b231cb39SDavid E. O'Brien mode_t clrval, newmode, value; 9058f0484fSRodney W. Grimes 91215d1a9eSMike Heffner set = (const BITCMD *)bbox; 9258f0484fSRodney W. Grimes newmode = omode; 9358f0484fSRodney W. Grimes for (value = 0;; set++) 9458f0484fSRodney W. Grimes switch(set->cmd) { 9558f0484fSRodney W. Grimes /* 9658f0484fSRodney W. Grimes * When copying the user, group or other bits around, we "know" 9758f0484fSRodney W. Grimes * where the bits are in the mode so that we can do shifts to 9858f0484fSRodney W. Grimes * copy them around. If we don't use shifts, it gets real 9958f0484fSRodney W. Grimes * grundgy with lots of single bit checks and bit sets. 10058f0484fSRodney W. Grimes */ 10158f0484fSRodney W. Grimes case 'u': 10258f0484fSRodney W. Grimes value = (newmode & S_IRWXU) >> 6; 10358f0484fSRodney W. Grimes goto common; 10458f0484fSRodney W. Grimes 10558f0484fSRodney W. Grimes case 'g': 10658f0484fSRodney W. Grimes value = (newmode & S_IRWXG) >> 3; 10758f0484fSRodney W. Grimes goto common; 10858f0484fSRodney W. Grimes 10958f0484fSRodney W. Grimes case 'o': 11058f0484fSRodney W. Grimes value = newmode & S_IRWXO; 11158f0484fSRodney W. Grimes common: if (set->cmd2 & CMD2_CLR) { 11258f0484fSRodney W. Grimes clrval = 11358f0484fSRodney W. Grimes (set->cmd2 & CMD2_SET) ? S_IRWXO : value; 11458f0484fSRodney W. Grimes if (set->cmd2 & CMD2_UBITS) 11558f0484fSRodney W. Grimes newmode &= ~((clrval<<6) & set->bits); 11658f0484fSRodney W. Grimes if (set->cmd2 & CMD2_GBITS) 11758f0484fSRodney W. Grimes newmode &= ~((clrval<<3) & set->bits); 11858f0484fSRodney W. Grimes if (set->cmd2 & CMD2_OBITS) 11958f0484fSRodney W. Grimes newmode &= ~(clrval & set->bits); 12058f0484fSRodney W. Grimes } 12158f0484fSRodney W. Grimes if (set->cmd2 & CMD2_SET) { 12258f0484fSRodney W. Grimes if (set->cmd2 & CMD2_UBITS) 12358f0484fSRodney W. Grimes newmode |= (value<<6) & set->bits; 12458f0484fSRodney W. Grimes if (set->cmd2 & CMD2_GBITS) 12558f0484fSRodney W. Grimes newmode |= (value<<3) & set->bits; 12658f0484fSRodney W. Grimes if (set->cmd2 & CMD2_OBITS) 12758f0484fSRodney W. Grimes newmode |= value & set->bits; 12858f0484fSRodney W. Grimes } 12958f0484fSRodney W. Grimes break; 13058f0484fSRodney W. Grimes 13158f0484fSRodney W. Grimes case '+': 13258f0484fSRodney W. Grimes newmode |= set->bits; 13358f0484fSRodney W. Grimes break; 13458f0484fSRodney W. Grimes 13558f0484fSRodney W. Grimes case '-': 13658f0484fSRodney W. Grimes newmode &= ~set->bits; 13758f0484fSRodney W. Grimes break; 13858f0484fSRodney W. Grimes 13958f0484fSRodney W. Grimes case 'X': 14058f0484fSRodney W. Grimes if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH)) 14158f0484fSRodney W. Grimes newmode |= set->bits; 14258f0484fSRodney W. Grimes break; 14358f0484fSRodney W. Grimes 14458f0484fSRodney W. Grimes case '\0': 14558f0484fSRodney W. Grimes default: 14658f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG 14758f0484fSRodney W. Grimes (void)printf("getmode:%04o -> %04o\n", omode, newmode); 14858f0484fSRodney W. Grimes #endif 14958f0484fSRodney W. Grimes return (newmode); 15058f0484fSRodney W. Grimes } 15158f0484fSRodney W. Grimes } 15258f0484fSRodney W. Grimes 15358f0484fSRodney W. Grimes #define ADDCMD(a, b, c, d) \ 15458f0484fSRodney W. Grimes if (set >= endset) { \ 155b231cb39SDavid E. O'Brien BITCMD *newset; \ 15658f0484fSRodney W. Grimes setlen += SET_LEN_INCR; \ 15758f0484fSRodney W. Grimes newset = realloc(saveset, sizeof(BITCMD) * setlen); \ 158e382e86bSPedro F. Giffuni if (newset == NULL) \ 159e382e86bSPedro F. Giffuni goto out; \ 16058f0484fSRodney W. Grimes set = newset + (set - saveset); \ 16158f0484fSRodney W. Grimes saveset = newset; \ 16258f0484fSRodney W. Grimes endset = newset + (setlen - 2); \ 16358f0484fSRodney W. Grimes } \ 164e382e86bSPedro F. Giffuni set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d)) 16558f0484fSRodney W. Grimes 16658f0484fSRodney W. Grimes #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) 16758f0484fSRodney W. Grimes 16858f0484fSRodney W. Grimes void * 169fd42c4d8SStefan Farfeleder setmode(const char *p) 17058f0484fSRodney W. Grimes { 171e382e86bSPedro F. Giffuni int serrno; 172ad4f1706SMike Heffner char op, *ep; 17358f0484fSRodney W. Grimes BITCMD *set, *saveset, *endset; 174e382e86bSPedro F. Giffuni mode_t mask, perm, permXbits, who; 175ad4f1706SMike Heffner long perml; 176e382e86bSPedro F. Giffuni int equalopdone; 177e382e86bSPedro F. Giffuni int setlen; 17858f0484fSRodney W. Grimes 179e382e86bSPedro F. Giffuni if (!*p) { 180e382e86bSPedro F. Giffuni errno = EINVAL; 18158f0484fSRodney W. Grimes return (NULL); 182e382e86bSPedro F. Giffuni } 18358f0484fSRodney W. Grimes 18458f0484fSRodney W. Grimes /* 18558f0484fSRodney W. Grimes * Get a copy of the mask for the permissions that are mask relative. 186*73cc817eSJilles Tjoelker * Flip the bits, we want what's not set. 18758f0484fSRodney W. Grimes */ 188*73cc817eSJilles Tjoelker mask = ~getumask(); 18958f0484fSRodney W. Grimes 19058f0484fSRodney W. Grimes setlen = SET_LEN + 2; 19158f0484fSRodney W. Grimes 19258f0484fSRodney W. Grimes if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL) 19358f0484fSRodney W. Grimes return (NULL); 19458f0484fSRodney W. Grimes saveset = set; 19558f0484fSRodney W. Grimes endset = set + (setlen - 2); 19658f0484fSRodney W. Grimes 19758f0484fSRodney W. Grimes /* 19858f0484fSRodney W. Grimes * If an absolute number, get it and return; disallow non-octal digits 19958f0484fSRodney W. Grimes * or illegal bits. 20058f0484fSRodney W. Grimes */ 201d67f4d91SAndrey A. Chernov if (isdigit((unsigned char)*p)) { 202e382e86bSPedro F. Giffuni errno = 0; 203ad4f1706SMike Heffner perml = strtol(p, &ep, 8); 204e382e86bSPedro F. Giffuni if (*ep) { 205e382e86bSPedro F. Giffuni errno = EINVAL; 206e382e86bSPedro F. Giffuni goto out; 207e382e86bSPedro F. Giffuni } 208e382e86bSPedro F. Giffuni if (errno == ERANGE && (perml == LONG_MAX || perml == LONG_MIN)) 209e382e86bSPedro F. Giffuni goto out; 210e382e86bSPedro F. Giffuni if (perml & ~(STANDARD_BITS|S_ISTXT)) { 211e382e86bSPedro F. Giffuni errno = EINVAL; 212e382e86bSPedro F. Giffuni goto out; 21358f0484fSRodney W. Grimes } 214ad4f1706SMike Heffner perm = (mode_t)perml; 21558f0484fSRodney W. Grimes ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask); 216ad4f1706SMike Heffner set->cmd = 0; 21758f0484fSRodney W. Grimes return (saveset); 21858f0484fSRodney W. Grimes } 21958f0484fSRodney W. Grimes 22058f0484fSRodney W. Grimes /* 22158f0484fSRodney W. Grimes * Build list of structures to set/clear/copy bits as described by 22258f0484fSRodney W. Grimes * each clause of the symbolic mode. 22358f0484fSRodney W. Grimes */ 224e382e86bSPedro F. Giffuni equalopdone = 0; 22558f0484fSRodney W. Grimes for (;;) { 22658f0484fSRodney W. Grimes /* First, find out which bits might be modified. */ 22758f0484fSRodney W. Grimes for (who = 0;; ++p) { 22858f0484fSRodney W. Grimes switch (*p) { 22958f0484fSRodney W. Grimes case 'a': 23058f0484fSRodney W. Grimes who |= STANDARD_BITS; 23158f0484fSRodney W. Grimes break; 23258f0484fSRodney W. Grimes case 'u': 23358f0484fSRodney W. Grimes who |= S_ISUID|S_IRWXU; 23458f0484fSRodney W. Grimes break; 23558f0484fSRodney W. Grimes case 'g': 23658f0484fSRodney W. Grimes who |= S_ISGID|S_IRWXG; 23758f0484fSRodney W. Grimes break; 23858f0484fSRodney W. Grimes case 'o': 23958f0484fSRodney W. Grimes who |= S_IRWXO; 24058f0484fSRodney W. Grimes break; 24158f0484fSRodney W. Grimes default: 24258f0484fSRodney W. Grimes goto getop; 24358f0484fSRodney W. Grimes } 24458f0484fSRodney W. Grimes } 24558f0484fSRodney W. Grimes 24658f0484fSRodney W. Grimes getop: if ((op = *p++) != '+' && op != '-' && op != '=') { 247e382e86bSPedro F. Giffuni errno = EINVAL; 248e382e86bSPedro F. Giffuni goto out; 24958f0484fSRodney W. Grimes } 25058f0484fSRodney W. Grimes if (op == '=') 25158f0484fSRodney W. Grimes equalopdone = 0; 25258f0484fSRodney W. Grimes 25358f0484fSRodney W. Grimes who &= ~S_ISTXT; 25458f0484fSRodney W. Grimes for (perm = 0, permXbits = 0;; ++p) { 25558f0484fSRodney W. Grimes switch (*p) { 25658f0484fSRodney W. Grimes case 'r': 25758f0484fSRodney W. Grimes perm |= S_IRUSR|S_IRGRP|S_IROTH; 25858f0484fSRodney W. Grimes break; 25958f0484fSRodney W. Grimes case 's': 26058f0484fSRodney W. Grimes /* If only "other" bits ignore set-id. */ 261bd8beb9bSRuslan Ermilov if (!who || who & ~S_IRWXO) 26258f0484fSRodney W. Grimes perm |= S_ISUID|S_ISGID; 26358f0484fSRodney W. Grimes break; 26458f0484fSRodney W. Grimes case 't': 26558f0484fSRodney W. Grimes /* If only "other" bits ignore sticky. */ 266bd8beb9bSRuslan Ermilov if (!who || who & ~S_IRWXO) { 26758f0484fSRodney W. Grimes who |= S_ISTXT; 26858f0484fSRodney W. Grimes perm |= S_ISTXT; 26958f0484fSRodney W. Grimes } 27058f0484fSRodney W. Grimes break; 27158f0484fSRodney W. Grimes case 'w': 27258f0484fSRodney W. Grimes perm |= S_IWUSR|S_IWGRP|S_IWOTH; 27358f0484fSRodney W. Grimes break; 27458f0484fSRodney W. Grimes case 'X': 27558f0484fSRodney W. Grimes permXbits = S_IXUSR|S_IXGRP|S_IXOTH; 27658f0484fSRodney W. Grimes break; 27758f0484fSRodney W. Grimes case 'x': 27858f0484fSRodney W. Grimes perm |= S_IXUSR|S_IXGRP|S_IXOTH; 27958f0484fSRodney W. Grimes break; 28058f0484fSRodney W. Grimes case 'u': 28158f0484fSRodney W. Grimes case 'g': 28258f0484fSRodney W. Grimes case 'o': 28358f0484fSRodney W. Grimes /* 28458f0484fSRodney W. Grimes * When ever we hit 'u', 'g', or 'o', we have 28558f0484fSRodney W. Grimes * to flush out any partial mode that we have, 28658f0484fSRodney W. Grimes * and then do the copying of the mode bits. 28758f0484fSRodney W. Grimes */ 28858f0484fSRodney W. Grimes if (perm) { 28958f0484fSRodney W. Grimes ADDCMD(op, who, perm, mask); 29058f0484fSRodney W. Grimes perm = 0; 29158f0484fSRodney W. Grimes } 29258f0484fSRodney W. Grimes if (op == '=') 29358f0484fSRodney W. Grimes equalopdone = 1; 29458f0484fSRodney W. Grimes if (op == '+' && permXbits) { 29558f0484fSRodney W. Grimes ADDCMD('X', who, permXbits, mask); 29658f0484fSRodney W. Grimes permXbits = 0; 29758f0484fSRodney W. Grimes } 29858f0484fSRodney W. Grimes ADDCMD(*p, who, op, mask); 29958f0484fSRodney W. Grimes break; 30058f0484fSRodney W. Grimes 30158f0484fSRodney W. Grimes default: 30258f0484fSRodney W. Grimes /* 30358f0484fSRodney W. Grimes * Add any permissions that we haven't already 30458f0484fSRodney W. Grimes * done. 30558f0484fSRodney W. Grimes */ 30658f0484fSRodney W. Grimes if (perm || (op == '=' && !equalopdone)) { 30758f0484fSRodney W. Grimes if (op == '=') 30858f0484fSRodney W. Grimes equalopdone = 1; 30958f0484fSRodney W. Grimes ADDCMD(op, who, perm, mask); 31058f0484fSRodney W. Grimes perm = 0; 31158f0484fSRodney W. Grimes } 31258f0484fSRodney W. Grimes if (permXbits) { 31358f0484fSRodney W. Grimes ADDCMD('X', who, permXbits, mask); 31458f0484fSRodney W. Grimes permXbits = 0; 31558f0484fSRodney W. Grimes } 31658f0484fSRodney W. Grimes goto apply; 31758f0484fSRodney W. Grimes } 31858f0484fSRodney W. Grimes } 31958f0484fSRodney W. Grimes 32058f0484fSRodney W. Grimes apply: if (!*p) 32158f0484fSRodney W. Grimes break; 32258f0484fSRodney W. Grimes if (*p != ',') 32358f0484fSRodney W. Grimes goto getop; 32458f0484fSRodney W. Grimes ++p; 32558f0484fSRodney W. Grimes } 32658f0484fSRodney W. Grimes set->cmd = 0; 32758f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG 32858f0484fSRodney W. Grimes (void)printf("Before compress_mode()\n"); 32958f0484fSRodney W. Grimes dumpmode(saveset); 33058f0484fSRodney W. Grimes #endif 33158f0484fSRodney W. Grimes compress_mode(saveset); 33258f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG 33358f0484fSRodney W. Grimes (void)printf("After compress_mode()\n"); 33458f0484fSRodney W. Grimes dumpmode(saveset); 33558f0484fSRodney W. Grimes #endif 33658f0484fSRodney W. Grimes return (saveset); 337e382e86bSPedro F. Giffuni out: 338e382e86bSPedro F. Giffuni serrno = errno; 339e382e86bSPedro F. Giffuni free(saveset); 340e382e86bSPedro F. Giffuni errno = serrno; 341e382e86bSPedro F. Giffuni return NULL; 34258f0484fSRodney W. Grimes } 34358f0484fSRodney W. Grimes 344*73cc817eSJilles Tjoelker static mode_t 345*73cc817eSJilles Tjoelker getumask(void) 346*73cc817eSJilles Tjoelker { 347*73cc817eSJilles Tjoelker sigset_t sigset, sigoset; 348*73cc817eSJilles Tjoelker size_t len; 349*73cc817eSJilles Tjoelker mode_t mask; 350*73cc817eSJilles Tjoelker u_short smask; 351*73cc817eSJilles Tjoelker 352*73cc817eSJilles Tjoelker /* 353*73cc817eSJilles Tjoelker * First try requesting the umask without temporarily modifying it. 354*73cc817eSJilles Tjoelker * Note that this does not work if the sysctl 355*73cc817eSJilles Tjoelker * security.bsd.unprivileged_proc_debug is set to 0. 356*73cc817eSJilles Tjoelker */ 357*73cc817eSJilles Tjoelker len = sizeof(smask); 358*73cc817eSJilles Tjoelker if (sysctl((int[4]){ CTL_KERN, KERN_PROC, KERN_PROC_UMASK, getpid() }, 359*73cc817eSJilles Tjoelker 4, &smask, &len, NULL, 0) == 0) 360*73cc817eSJilles Tjoelker return (smask); 361*73cc817eSJilles Tjoelker 362*73cc817eSJilles Tjoelker /* 363*73cc817eSJilles Tjoelker * Since it's possible that the caller is opening files inside a signal 364*73cc817eSJilles Tjoelker * handler, protect them as best we can. 365*73cc817eSJilles Tjoelker */ 366*73cc817eSJilles Tjoelker sigfillset(&sigset); 367*73cc817eSJilles Tjoelker (void)_sigprocmask(SIG_BLOCK, &sigset, &sigoset); 368*73cc817eSJilles Tjoelker (void)umask(mask = umask(0)); 369*73cc817eSJilles Tjoelker (void)_sigprocmask(SIG_SETMASK, &sigoset, NULL); 370*73cc817eSJilles Tjoelker return (mask); 371*73cc817eSJilles Tjoelker } 372*73cc817eSJilles Tjoelker 37358f0484fSRodney W. Grimes static BITCMD * 374e382e86bSPedro F. Giffuni addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask) 37558f0484fSRodney W. Grimes { 37658f0484fSRodney W. Grimes switch (op) { 37758f0484fSRodney W. Grimes case '=': 37858f0484fSRodney W. Grimes set->cmd = '-'; 37958f0484fSRodney W. Grimes set->bits = who ? who : STANDARD_BITS; 38058f0484fSRodney W. Grimes set++; 38158f0484fSRodney W. Grimes 38258f0484fSRodney W. Grimes op = '+'; 38358f0484fSRodney W. Grimes /* FALLTHROUGH */ 38458f0484fSRodney W. Grimes case '+': 38558f0484fSRodney W. Grimes case '-': 38658f0484fSRodney W. Grimes case 'X': 38758f0484fSRodney W. Grimes set->cmd = op; 38858f0484fSRodney W. Grimes set->bits = (who ? who : mask) & oparg; 38958f0484fSRodney W. Grimes break; 39058f0484fSRodney W. Grimes 39158f0484fSRodney W. Grimes case 'u': 39258f0484fSRodney W. Grimes case 'g': 39358f0484fSRodney W. Grimes case 'o': 39458f0484fSRodney W. Grimes set->cmd = op; 39558f0484fSRodney W. Grimes if (who) { 39658f0484fSRodney W. Grimes set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) | 39758f0484fSRodney W. Grimes ((who & S_IRGRP) ? CMD2_GBITS : 0) | 39858f0484fSRodney W. Grimes ((who & S_IROTH) ? CMD2_OBITS : 0); 399215d1a9eSMike Heffner set->bits = (mode_t)~0; 40058f0484fSRodney W. Grimes } else { 40158f0484fSRodney W. Grimes set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS; 40258f0484fSRodney W. Grimes set->bits = mask; 40358f0484fSRodney W. Grimes } 40458f0484fSRodney W. Grimes 40558f0484fSRodney W. Grimes if (oparg == '+') 40658f0484fSRodney W. Grimes set->cmd2 |= CMD2_SET; 40758f0484fSRodney W. Grimes else if (oparg == '-') 40858f0484fSRodney W. Grimes set->cmd2 |= CMD2_CLR; 40958f0484fSRodney W. Grimes else if (oparg == '=') 41058f0484fSRodney W. Grimes set->cmd2 |= CMD2_SET|CMD2_CLR; 41158f0484fSRodney W. Grimes break; 41258f0484fSRodney W. Grimes } 41358f0484fSRodney W. Grimes return (set + 1); 41458f0484fSRodney W. Grimes } 41558f0484fSRodney W. Grimes 41658f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG 41758f0484fSRodney W. Grimes static void 418fd42c4d8SStefan Farfeleder dumpmode(BITCMD *set) 41958f0484fSRodney W. Grimes { 42058f0484fSRodney W. Grimes for (; set->cmd; ++set) 42158f0484fSRodney W. Grimes (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n", 42258f0484fSRodney W. Grimes set->cmd, set->bits, set->cmd2 ? " cmd2:" : "", 42358f0484fSRodney W. Grimes set->cmd2 & CMD2_CLR ? " CLR" : "", 42458f0484fSRodney W. Grimes set->cmd2 & CMD2_SET ? " SET" : "", 42558f0484fSRodney W. Grimes set->cmd2 & CMD2_UBITS ? " UBITS" : "", 42658f0484fSRodney W. Grimes set->cmd2 & CMD2_GBITS ? " GBITS" : "", 42758f0484fSRodney W. Grimes set->cmd2 & CMD2_OBITS ? " OBITS" : ""); 42858f0484fSRodney W. Grimes } 42958f0484fSRodney W. Grimes #endif 43058f0484fSRodney W. Grimes 43158f0484fSRodney W. Grimes /* 43258f0484fSRodney W. Grimes * Given an array of bitcmd structures, compress by compacting consecutive 43358f0484fSRodney W. Grimes * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u', 43458f0484fSRodney W. Grimes * 'g' and 'o' commands continue to be separate. They could probably be 43558f0484fSRodney W. Grimes * compacted, but it's not worth the effort. 43658f0484fSRodney W. Grimes */ 4378b102407SPoul-Henning Kamp static void 438fd42c4d8SStefan Farfeleder compress_mode(BITCMD *set) 43958f0484fSRodney W. Grimes { 440b231cb39SDavid E. O'Brien BITCMD *nset; 441b231cb39SDavid E. O'Brien int setbits, clrbits, Xbits, op; 44258f0484fSRodney W. Grimes 44358f0484fSRodney W. Grimes for (nset = set;;) { 44458f0484fSRodney W. Grimes /* Copy over any 'u', 'g' and 'o' commands. */ 44558f0484fSRodney W. Grimes while ((op = nset->cmd) != '+' && op != '-' && op != 'X') { 44658f0484fSRodney W. Grimes *set++ = *nset++; 44758f0484fSRodney W. Grimes if (!op) 44858f0484fSRodney W. Grimes return; 44958f0484fSRodney W. Grimes } 45058f0484fSRodney W. Grimes 45158f0484fSRodney W. Grimes for (setbits = clrbits = Xbits = 0;; nset++) { 45258f0484fSRodney W. Grimes if ((op = nset->cmd) == '-') { 45358f0484fSRodney W. Grimes clrbits |= nset->bits; 45458f0484fSRodney W. Grimes setbits &= ~nset->bits; 45558f0484fSRodney W. Grimes Xbits &= ~nset->bits; 45658f0484fSRodney W. Grimes } else if (op == '+') { 45758f0484fSRodney W. Grimes setbits |= nset->bits; 45858f0484fSRodney W. Grimes clrbits &= ~nset->bits; 45958f0484fSRodney W. Grimes Xbits &= ~nset->bits; 46058f0484fSRodney W. Grimes } else if (op == 'X') 46158f0484fSRodney W. Grimes Xbits |= nset->bits & ~setbits; 46258f0484fSRodney W. Grimes else 46358f0484fSRodney W. Grimes break; 46458f0484fSRodney W. Grimes } 46558f0484fSRodney W. Grimes if (clrbits) { 46658f0484fSRodney W. Grimes set->cmd = '-'; 46758f0484fSRodney W. Grimes set->cmd2 = 0; 46858f0484fSRodney W. Grimes set->bits = clrbits; 46958f0484fSRodney W. Grimes set++; 47058f0484fSRodney W. Grimes } 47158f0484fSRodney W. Grimes if (setbits) { 47258f0484fSRodney W. Grimes set->cmd = '+'; 47358f0484fSRodney W. Grimes set->cmd2 = 0; 47458f0484fSRodney W. Grimes set->bits = setbits; 47558f0484fSRodney W. Grimes set++; 47658f0484fSRodney W. Grimes } 47758f0484fSRodney W. Grimes if (Xbits) { 47858f0484fSRodney W. Grimes set->cmd = 'X'; 47958f0484fSRodney W. Grimes set->cmd2 = 0; 48058f0484fSRodney W. Grimes set->bits = Xbits; 48158f0484fSRodney W. Grimes set++; 48258f0484fSRodney W. Grimes } 48358f0484fSRodney W. Grimes } 48458f0484fSRodney W. Grimes } 485