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. 16fbbd9655SWarner Losh * 3. 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> 4273cc817eSJilles 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" 56bd6060a1SKonstantin Belousov #include "libc_private.h" 5758f0484fSRodney W. Grimes 5858f0484fSRodney W. Grimes #define SET_LEN 6 /* initial # of bitcmd struct to malloc */ 5958f0484fSRodney W. Grimes #define SET_LEN_INCR 4 /* # of bitcmd structs to add as needed */ 6058f0484fSRodney W. Grimes 6158f0484fSRodney W. Grimes typedef struct bitcmd { 6258f0484fSRodney W. Grimes char cmd; 6358f0484fSRodney W. Grimes char cmd2; 6458f0484fSRodney W. Grimes mode_t bits; 6558f0484fSRodney W. Grimes } BITCMD; 6658f0484fSRodney W. Grimes 6758f0484fSRodney W. Grimes #define CMD2_CLR 0x01 6858f0484fSRodney W. Grimes #define CMD2_SET 0x02 6958f0484fSRodney W. Grimes #define CMD2_GBITS 0x04 7058f0484fSRodney W. Grimes #define CMD2_OBITS 0x08 7158f0484fSRodney W. Grimes #define CMD2_UBITS 0x10 7258f0484fSRodney W. Grimes 7373cc817eSJilles Tjoelker static mode_t getumask(void); 74e382e86bSPedro F. Giffuni static BITCMD *addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t); 75b231cb39SDavid E. O'Brien static void compress_mode(BITCMD *); 7658f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG 77b231cb39SDavid E. O'Brien static void dumpmode(BITCMD *); 7858f0484fSRodney W. Grimes #endif 7958f0484fSRodney W. Grimes 8058f0484fSRodney W. Grimes /* 8158f0484fSRodney W. Grimes * Given the old mode and an array of bitcmd structures, apply the operations 8258f0484fSRodney W. Grimes * described in the bitcmd structures to the old mode, and return the new mode. 8358f0484fSRodney W. Grimes * Note that there is no '=' command; a strict assignment is just a '-' (clear 8458f0484fSRodney W. Grimes * bits) followed by a '+' (set bits). 8558f0484fSRodney W. Grimes */ 8658f0484fSRodney W. Grimes mode_t 87fd42c4d8SStefan Farfeleder getmode(const void *bbox, mode_t omode) 8858f0484fSRodney W. Grimes { 89215d1a9eSMike Heffner const BITCMD *set; 90b231cb39SDavid E. O'Brien mode_t clrval, newmode, value; 9158f0484fSRodney W. Grimes 92215d1a9eSMike Heffner set = (const BITCMD *)bbox; 9358f0484fSRodney W. Grimes newmode = omode; 9458f0484fSRodney W. Grimes for (value = 0;; set++) 9558f0484fSRodney W. Grimes switch(set->cmd) { 9658f0484fSRodney W. Grimes /* 9758f0484fSRodney W. Grimes * When copying the user, group or other bits around, we "know" 9858f0484fSRodney W. Grimes * where the bits are in the mode so that we can do shifts to 9958f0484fSRodney W. Grimes * copy them around. If we don't use shifts, it gets real 10058f0484fSRodney W. Grimes * grundgy with lots of single bit checks and bit sets. 10158f0484fSRodney W. Grimes */ 10258f0484fSRodney W. Grimes case 'u': 10358f0484fSRodney W. Grimes value = (newmode & S_IRWXU) >> 6; 10458f0484fSRodney W. Grimes goto common; 10558f0484fSRodney W. Grimes 10658f0484fSRodney W. Grimes case 'g': 10758f0484fSRodney W. Grimes value = (newmode & S_IRWXG) >> 3; 10858f0484fSRodney W. Grimes goto common; 10958f0484fSRodney W. Grimes 11058f0484fSRodney W. Grimes case 'o': 11158f0484fSRodney W. Grimes value = newmode & S_IRWXO; 11258f0484fSRodney W. Grimes common: if (set->cmd2 & CMD2_CLR) { 11358f0484fSRodney W. Grimes clrval = 11458f0484fSRodney W. Grimes (set->cmd2 & CMD2_SET) ? S_IRWXO : value; 11558f0484fSRodney W. Grimes if (set->cmd2 & CMD2_UBITS) 11658f0484fSRodney W. Grimes newmode &= ~((clrval<<6) & set->bits); 11758f0484fSRodney W. Grimes if (set->cmd2 & CMD2_GBITS) 11858f0484fSRodney W. Grimes newmode &= ~((clrval<<3) & set->bits); 11958f0484fSRodney W. Grimes if (set->cmd2 & CMD2_OBITS) 12058f0484fSRodney W. Grimes newmode &= ~(clrval & set->bits); 12158f0484fSRodney W. Grimes } 12258f0484fSRodney W. Grimes if (set->cmd2 & CMD2_SET) { 12358f0484fSRodney W. Grimes if (set->cmd2 & CMD2_UBITS) 12458f0484fSRodney W. Grimes newmode |= (value<<6) & set->bits; 12558f0484fSRodney W. Grimes if (set->cmd2 & CMD2_GBITS) 12658f0484fSRodney W. Grimes newmode |= (value<<3) & set->bits; 12758f0484fSRodney W. Grimes if (set->cmd2 & CMD2_OBITS) 12858f0484fSRodney W. Grimes newmode |= value & set->bits; 12958f0484fSRodney W. Grimes } 13058f0484fSRodney W. Grimes break; 13158f0484fSRodney W. Grimes 13258f0484fSRodney W. Grimes case '+': 13358f0484fSRodney W. Grimes newmode |= set->bits; 13458f0484fSRodney W. Grimes break; 13558f0484fSRodney W. Grimes 13658f0484fSRodney W. Grimes case '-': 13758f0484fSRodney W. Grimes newmode &= ~set->bits; 13858f0484fSRodney W. Grimes break; 13958f0484fSRodney W. Grimes 14058f0484fSRodney W. Grimes case 'X': 14158f0484fSRodney W. Grimes if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH)) 14258f0484fSRodney W. Grimes newmode |= set->bits; 14358f0484fSRodney W. Grimes break; 14458f0484fSRodney W. Grimes 14558f0484fSRodney W. Grimes case '\0': 14658f0484fSRodney W. Grimes default: 14758f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG 14858f0484fSRodney W. Grimes (void)printf("getmode:%04o -> %04o\n", omode, newmode); 14958f0484fSRodney W. Grimes #endif 15058f0484fSRodney W. Grimes return (newmode); 15158f0484fSRodney W. Grimes } 15258f0484fSRodney W. Grimes } 15358f0484fSRodney W. Grimes 15458f0484fSRodney W. Grimes #define ADDCMD(a, b, c, d) \ 15558f0484fSRodney W. Grimes if (set >= endset) { \ 156b231cb39SDavid E. O'Brien BITCMD *newset; \ 15758f0484fSRodney W. Grimes setlen += SET_LEN_INCR; \ 1589f36610fSPedro F. Giffuni newset = reallocarray(saveset, setlen, sizeof(BITCMD)); \ 159e382e86bSPedro F. Giffuni if (newset == NULL) \ 160e382e86bSPedro F. Giffuni goto out; \ 16158f0484fSRodney W. Grimes set = newset + (set - saveset); \ 16258f0484fSRodney W. Grimes saveset = newset; \ 16358f0484fSRodney W. Grimes endset = newset + (setlen - 2); \ 16458f0484fSRodney W. Grimes } \ 165e382e86bSPedro F. Giffuni set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d)) 16658f0484fSRodney W. Grimes 16758f0484fSRodney W. Grimes #define STANDARD_BITS (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO) 16858f0484fSRodney W. Grimes 16958f0484fSRodney W. Grimes void * 170fd42c4d8SStefan Farfeleder setmode(const char *p) 17158f0484fSRodney W. Grimes { 172e382e86bSPedro F. Giffuni int serrno; 173ad4f1706SMike Heffner char op, *ep; 17458f0484fSRodney W. Grimes BITCMD *set, *saveset, *endset; 175e382e86bSPedro F. Giffuni mode_t mask, perm, permXbits, who; 176ad4f1706SMike Heffner long perml; 177e382e86bSPedro F. Giffuni int equalopdone; 178ada46906SPedro F. Giffuni u_int setlen; 17958f0484fSRodney W. Grimes 180e382e86bSPedro F. Giffuni if (!*p) { 181e382e86bSPedro F. Giffuni errno = EINVAL; 18258f0484fSRodney W. Grimes return (NULL); 183e382e86bSPedro F. Giffuni } 18458f0484fSRodney W. Grimes 18558f0484fSRodney W. Grimes /* 18658f0484fSRodney W. Grimes * Get a copy of the mask for the permissions that are mask relative. 18773cc817eSJilles Tjoelker * Flip the bits, we want what's not set. 18858f0484fSRodney W. Grimes */ 18973cc817eSJilles Tjoelker mask = ~getumask(); 19058f0484fSRodney W. Grimes 19158f0484fSRodney W. Grimes setlen = SET_LEN + 2; 19258f0484fSRodney W. Grimes 193ada46906SPedro F. Giffuni if ((set = malloc(setlen * sizeof(BITCMD))) == NULL) 19458f0484fSRodney W. Grimes return (NULL); 19558f0484fSRodney W. Grimes saveset = set; 19658f0484fSRodney W. Grimes endset = set + (setlen - 2); 19758f0484fSRodney W. Grimes 19858f0484fSRodney W. Grimes /* 19958f0484fSRodney W. Grimes * If an absolute number, get it and return; disallow non-octal digits 20058f0484fSRodney W. Grimes * or illegal bits. 20158f0484fSRodney W. Grimes */ 202d67f4d91SAndrey A. Chernov if (isdigit((unsigned char)*p)) { 203e382e86bSPedro F. Giffuni errno = 0; 204ad4f1706SMike Heffner perml = strtol(p, &ep, 8); 205e382e86bSPedro F. Giffuni if (*ep) { 206e382e86bSPedro F. Giffuni errno = EINVAL; 207e382e86bSPedro F. Giffuni goto out; 208e382e86bSPedro F. Giffuni } 209e382e86bSPedro F. Giffuni if (errno == ERANGE && (perml == LONG_MAX || perml == LONG_MIN)) 210e382e86bSPedro F. Giffuni goto out; 211e382e86bSPedro F. Giffuni if (perml & ~(STANDARD_BITS|S_ISTXT)) { 212e382e86bSPedro F. Giffuni errno = EINVAL; 213e382e86bSPedro F. Giffuni goto out; 21458f0484fSRodney W. Grimes } 215ad4f1706SMike Heffner perm = (mode_t)perml; 21658f0484fSRodney W. Grimes ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask); 217ad4f1706SMike Heffner set->cmd = 0; 21858f0484fSRodney W. Grimes return (saveset); 21958f0484fSRodney W. Grimes } 22058f0484fSRodney W. Grimes 22158f0484fSRodney W. Grimes /* 22258f0484fSRodney W. Grimes * Build list of structures to set/clear/copy bits as described by 22358f0484fSRodney W. Grimes * each clause of the symbolic mode. 22458f0484fSRodney W. Grimes */ 225e382e86bSPedro F. Giffuni equalopdone = 0; 22658f0484fSRodney W. Grimes for (;;) { 22758f0484fSRodney W. Grimes /* First, find out which bits might be modified. */ 22858f0484fSRodney W. Grimes for (who = 0;; ++p) { 22958f0484fSRodney W. Grimes switch (*p) { 23058f0484fSRodney W. Grimes case 'a': 23158f0484fSRodney W. Grimes who |= STANDARD_BITS; 23258f0484fSRodney W. Grimes break; 23358f0484fSRodney W. Grimes case 'u': 23458f0484fSRodney W. Grimes who |= S_ISUID|S_IRWXU; 23558f0484fSRodney W. Grimes break; 23658f0484fSRodney W. Grimes case 'g': 23758f0484fSRodney W. Grimes who |= S_ISGID|S_IRWXG; 23858f0484fSRodney W. Grimes break; 23958f0484fSRodney W. Grimes case 'o': 24058f0484fSRodney W. Grimes who |= S_IRWXO; 24158f0484fSRodney W. Grimes break; 24258f0484fSRodney W. Grimes default: 24358f0484fSRodney W. Grimes goto getop; 24458f0484fSRodney W. Grimes } 24558f0484fSRodney W. Grimes } 24658f0484fSRodney W. Grimes 24758f0484fSRodney W. Grimes getop: if ((op = *p++) != '+' && op != '-' && op != '=') { 248e382e86bSPedro F. Giffuni errno = EINVAL; 249e382e86bSPedro F. Giffuni goto out; 25058f0484fSRodney W. Grimes } 25158f0484fSRodney W. Grimes if (op == '=') 25258f0484fSRodney W. Grimes equalopdone = 0; 25358f0484fSRodney W. Grimes 25458f0484fSRodney W. Grimes who &= ~S_ISTXT; 25558f0484fSRodney W. Grimes for (perm = 0, permXbits = 0;; ++p) { 25658f0484fSRodney W. Grimes switch (*p) { 25758f0484fSRodney W. Grimes case 'r': 25858f0484fSRodney W. Grimes perm |= S_IRUSR|S_IRGRP|S_IROTH; 25958f0484fSRodney W. Grimes break; 26058f0484fSRodney W. Grimes case 's': 26158f0484fSRodney W. Grimes /* If only "other" bits ignore set-id. */ 262bd8beb9bSRuslan Ermilov if (!who || who & ~S_IRWXO) 26358f0484fSRodney W. Grimes perm |= S_ISUID|S_ISGID; 26458f0484fSRodney W. Grimes break; 26558f0484fSRodney W. Grimes case 't': 26658f0484fSRodney W. Grimes /* If only "other" bits ignore sticky. */ 267bd8beb9bSRuslan Ermilov if (!who || who & ~S_IRWXO) { 26858f0484fSRodney W. Grimes who |= S_ISTXT; 26958f0484fSRodney W. Grimes perm |= S_ISTXT; 27058f0484fSRodney W. Grimes } 27158f0484fSRodney W. Grimes break; 27258f0484fSRodney W. Grimes case 'w': 27358f0484fSRodney W. Grimes perm |= S_IWUSR|S_IWGRP|S_IWOTH; 27458f0484fSRodney W. Grimes break; 27558f0484fSRodney W. Grimes case 'X': 27658f0484fSRodney W. Grimes permXbits = S_IXUSR|S_IXGRP|S_IXOTH; 27758f0484fSRodney W. Grimes break; 27858f0484fSRodney W. Grimes case 'x': 27958f0484fSRodney W. Grimes perm |= S_IXUSR|S_IXGRP|S_IXOTH; 28058f0484fSRodney W. Grimes break; 28158f0484fSRodney W. Grimes case 'u': 28258f0484fSRodney W. Grimes case 'g': 28358f0484fSRodney W. Grimes case 'o': 28458f0484fSRodney W. Grimes /* 28558f0484fSRodney W. Grimes * When ever we hit 'u', 'g', or 'o', we have 28658f0484fSRodney W. Grimes * to flush out any partial mode that we have, 28758f0484fSRodney W. Grimes * and then do the copying of the mode bits. 28858f0484fSRodney W. Grimes */ 28958f0484fSRodney W. Grimes if (perm) { 29058f0484fSRodney W. Grimes ADDCMD(op, who, perm, mask); 29158f0484fSRodney W. Grimes perm = 0; 29258f0484fSRodney W. Grimes } 29358f0484fSRodney W. Grimes if (op == '=') 29458f0484fSRodney W. Grimes equalopdone = 1; 29558f0484fSRodney W. Grimes if (op == '+' && permXbits) { 29658f0484fSRodney W. Grimes ADDCMD('X', who, permXbits, mask); 29758f0484fSRodney W. Grimes permXbits = 0; 29858f0484fSRodney W. Grimes } 29958f0484fSRodney W. Grimes ADDCMD(*p, who, op, mask); 30058f0484fSRodney W. Grimes break; 30158f0484fSRodney W. Grimes 30258f0484fSRodney W. Grimes default: 30358f0484fSRodney W. Grimes /* 30458f0484fSRodney W. Grimes * Add any permissions that we haven't already 30558f0484fSRodney W. Grimes * done. 30658f0484fSRodney W. Grimes */ 30758f0484fSRodney W. Grimes if (perm || (op == '=' && !equalopdone)) { 30858f0484fSRodney W. Grimes if (op == '=') 30958f0484fSRodney W. Grimes equalopdone = 1; 31058f0484fSRodney W. Grimes ADDCMD(op, who, perm, mask); 31158f0484fSRodney W. Grimes perm = 0; 31258f0484fSRodney W. Grimes } 31358f0484fSRodney W. Grimes if (permXbits) { 31458f0484fSRodney W. Grimes ADDCMD('X', who, permXbits, mask); 31558f0484fSRodney W. Grimes permXbits = 0; 31658f0484fSRodney W. Grimes } 31758f0484fSRodney W. Grimes goto apply; 31858f0484fSRodney W. Grimes } 31958f0484fSRodney W. Grimes } 32058f0484fSRodney W. Grimes 32158f0484fSRodney W. Grimes apply: if (!*p) 32258f0484fSRodney W. Grimes break; 32358f0484fSRodney W. Grimes if (*p != ',') 32458f0484fSRodney W. Grimes goto getop; 32558f0484fSRodney W. Grimes ++p; 32658f0484fSRodney W. Grimes } 32758f0484fSRodney W. Grimes set->cmd = 0; 32858f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG 32958f0484fSRodney W. Grimes (void)printf("Before compress_mode()\n"); 33058f0484fSRodney W. Grimes dumpmode(saveset); 33158f0484fSRodney W. Grimes #endif 33258f0484fSRodney W. Grimes compress_mode(saveset); 33358f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG 33458f0484fSRodney W. Grimes (void)printf("After compress_mode()\n"); 33558f0484fSRodney W. Grimes dumpmode(saveset); 33658f0484fSRodney W. Grimes #endif 33758f0484fSRodney W. Grimes return (saveset); 338e382e86bSPedro F. Giffuni out: 339e382e86bSPedro F. Giffuni serrno = errno; 340e382e86bSPedro F. Giffuni free(saveset); 341e382e86bSPedro F. Giffuni errno = serrno; 342e382e86bSPedro F. Giffuni return NULL; 34358f0484fSRodney W. Grimes } 34458f0484fSRodney W. Grimes 34573cc817eSJilles Tjoelker static mode_t 34673cc817eSJilles Tjoelker getumask(void) 34773cc817eSJilles Tjoelker { 34873cc817eSJilles Tjoelker sigset_t sigset, sigoset; 34973cc817eSJilles Tjoelker size_t len; 35073cc817eSJilles Tjoelker mode_t mask; 35173cc817eSJilles Tjoelker u_short smask; 35273cc817eSJilles Tjoelker 35373cc817eSJilles Tjoelker /* 35473cc817eSJilles Tjoelker * First try requesting the umask without temporarily modifying it. 35573cc817eSJilles Tjoelker * Note that this does not work if the sysctl 35673cc817eSJilles Tjoelker * security.bsd.unprivileged_proc_debug is set to 0. 35773cc817eSJilles Tjoelker */ 35873cc817eSJilles Tjoelker len = sizeof(smask); 359*cee09850SBartek Rutkowski if (sysctl((int[4]){ CTL_KERN, KERN_PROC, KERN_PROC_UMASK, 0 }, 36073cc817eSJilles Tjoelker 4, &smask, &len, NULL, 0) == 0) 36173cc817eSJilles Tjoelker return (smask); 36273cc817eSJilles Tjoelker 36373cc817eSJilles Tjoelker /* 36473cc817eSJilles Tjoelker * Since it's possible that the caller is opening files inside a signal 36573cc817eSJilles Tjoelker * handler, protect them as best we can. 36673cc817eSJilles Tjoelker */ 36773cc817eSJilles Tjoelker sigfillset(&sigset); 368bd6060a1SKonstantin Belousov (void)__libc_sigprocmask(SIG_BLOCK, &sigset, &sigoset); 36973cc817eSJilles Tjoelker (void)umask(mask = umask(0)); 370bd6060a1SKonstantin Belousov (void)__libc_sigprocmask(SIG_SETMASK, &sigoset, NULL); 37173cc817eSJilles Tjoelker return (mask); 37273cc817eSJilles Tjoelker } 37373cc817eSJilles Tjoelker 37458f0484fSRodney W. Grimes static BITCMD * 375e382e86bSPedro F. Giffuni addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask) 37658f0484fSRodney W. Grimes { 37758f0484fSRodney W. Grimes switch (op) { 37858f0484fSRodney W. Grimes case '=': 37958f0484fSRodney W. Grimes set->cmd = '-'; 38058f0484fSRodney W. Grimes set->bits = who ? who : STANDARD_BITS; 38158f0484fSRodney W. Grimes set++; 38258f0484fSRodney W. Grimes 38358f0484fSRodney W. Grimes op = '+'; 38458f0484fSRodney W. Grimes /* FALLTHROUGH */ 38558f0484fSRodney W. Grimes case '+': 38658f0484fSRodney W. Grimes case '-': 38758f0484fSRodney W. Grimes case 'X': 38858f0484fSRodney W. Grimes set->cmd = op; 38958f0484fSRodney W. Grimes set->bits = (who ? who : mask) & oparg; 39058f0484fSRodney W. Grimes break; 39158f0484fSRodney W. Grimes 39258f0484fSRodney W. Grimes case 'u': 39358f0484fSRodney W. Grimes case 'g': 39458f0484fSRodney W. Grimes case 'o': 39558f0484fSRodney W. Grimes set->cmd = op; 39658f0484fSRodney W. Grimes if (who) { 39758f0484fSRodney W. Grimes set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) | 39858f0484fSRodney W. Grimes ((who & S_IRGRP) ? CMD2_GBITS : 0) | 39958f0484fSRodney W. Grimes ((who & S_IROTH) ? CMD2_OBITS : 0); 400215d1a9eSMike Heffner set->bits = (mode_t)~0; 40158f0484fSRodney W. Grimes } else { 40258f0484fSRodney W. Grimes set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS; 40358f0484fSRodney W. Grimes set->bits = mask; 40458f0484fSRodney W. Grimes } 40558f0484fSRodney W. Grimes 40658f0484fSRodney W. Grimes if (oparg == '+') 40758f0484fSRodney W. Grimes set->cmd2 |= CMD2_SET; 40858f0484fSRodney W. Grimes else if (oparg == '-') 40958f0484fSRodney W. Grimes set->cmd2 |= CMD2_CLR; 41058f0484fSRodney W. Grimes else if (oparg == '=') 41158f0484fSRodney W. Grimes set->cmd2 |= CMD2_SET|CMD2_CLR; 41258f0484fSRodney W. Grimes break; 41358f0484fSRodney W. Grimes } 41458f0484fSRodney W. Grimes return (set + 1); 41558f0484fSRodney W. Grimes } 41658f0484fSRodney W. Grimes 41758f0484fSRodney W. Grimes #ifdef SETMODE_DEBUG 41858f0484fSRodney W. Grimes static void 419fd42c4d8SStefan Farfeleder dumpmode(BITCMD *set) 42058f0484fSRodney W. Grimes { 42158f0484fSRodney W. Grimes for (; set->cmd; ++set) 42258f0484fSRodney W. Grimes (void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n", 42358f0484fSRodney W. Grimes set->cmd, set->bits, set->cmd2 ? " cmd2:" : "", 42458f0484fSRodney W. Grimes set->cmd2 & CMD2_CLR ? " CLR" : "", 42558f0484fSRodney W. Grimes set->cmd2 & CMD2_SET ? " SET" : "", 42658f0484fSRodney W. Grimes set->cmd2 & CMD2_UBITS ? " UBITS" : "", 42758f0484fSRodney W. Grimes set->cmd2 & CMD2_GBITS ? " GBITS" : "", 42858f0484fSRodney W. Grimes set->cmd2 & CMD2_OBITS ? " OBITS" : ""); 42958f0484fSRodney W. Grimes } 43058f0484fSRodney W. Grimes #endif 43158f0484fSRodney W. Grimes 43258f0484fSRodney W. Grimes /* 43358f0484fSRodney W. Grimes * Given an array of bitcmd structures, compress by compacting consecutive 43458f0484fSRodney W. Grimes * '+', '-' and 'X' commands into at most 3 commands, one of each. The 'u', 43558f0484fSRodney W. Grimes * 'g' and 'o' commands continue to be separate. They could probably be 43658f0484fSRodney W. Grimes * compacted, but it's not worth the effort. 43758f0484fSRodney W. Grimes */ 4388b102407SPoul-Henning Kamp static void 439fd42c4d8SStefan Farfeleder compress_mode(BITCMD *set) 44058f0484fSRodney W. Grimes { 441b231cb39SDavid E. O'Brien BITCMD *nset; 442b231cb39SDavid E. O'Brien int setbits, clrbits, Xbits, op; 44358f0484fSRodney W. Grimes 44458f0484fSRodney W. Grimes for (nset = set;;) { 44558f0484fSRodney W. Grimes /* Copy over any 'u', 'g' and 'o' commands. */ 44658f0484fSRodney W. Grimes while ((op = nset->cmd) != '+' && op != '-' && op != 'X') { 44758f0484fSRodney W. Grimes *set++ = *nset++; 44858f0484fSRodney W. Grimes if (!op) 44958f0484fSRodney W. Grimes return; 45058f0484fSRodney W. Grimes } 45158f0484fSRodney W. Grimes 45258f0484fSRodney W. Grimes for (setbits = clrbits = Xbits = 0;; nset++) { 45358f0484fSRodney W. Grimes if ((op = nset->cmd) == '-') { 45458f0484fSRodney W. Grimes clrbits |= nset->bits; 45558f0484fSRodney W. Grimes setbits &= ~nset->bits; 45658f0484fSRodney W. Grimes Xbits &= ~nset->bits; 45758f0484fSRodney W. Grimes } else if (op == '+') { 45858f0484fSRodney W. Grimes setbits |= nset->bits; 45958f0484fSRodney W. Grimes clrbits &= ~nset->bits; 46058f0484fSRodney W. Grimes Xbits &= ~nset->bits; 46158f0484fSRodney W. Grimes } else if (op == 'X') 46258f0484fSRodney W. Grimes Xbits |= nset->bits & ~setbits; 46358f0484fSRodney W. Grimes else 46458f0484fSRodney W. Grimes break; 46558f0484fSRodney W. Grimes } 46658f0484fSRodney W. Grimes if (clrbits) { 46758f0484fSRodney W. Grimes set->cmd = '-'; 46858f0484fSRodney W. Grimes set->cmd2 = 0; 46958f0484fSRodney W. Grimes set->bits = clrbits; 47058f0484fSRodney W. Grimes set++; 47158f0484fSRodney W. Grimes } 47258f0484fSRodney W. Grimes if (setbits) { 47358f0484fSRodney W. Grimes set->cmd = '+'; 47458f0484fSRodney W. Grimes set->cmd2 = 0; 47558f0484fSRodney W. Grimes set->bits = setbits; 47658f0484fSRodney W. Grimes set++; 47758f0484fSRodney W. Grimes } 47858f0484fSRodney W. Grimes if (Xbits) { 47958f0484fSRodney W. Grimes set->cmd = 'X'; 48058f0484fSRodney W. Grimes set->cmd2 = 0; 48158f0484fSRodney W. Grimes set->bits = Xbits; 48258f0484fSRodney W. Grimes set++; 48358f0484fSRodney W. Grimes } 48458f0484fSRodney W. Grimes } 48558f0484fSRodney W. Grimes } 486