10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*1420Smarks * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate /* */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate /* 320Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 330Sstevel@tonic-gate * The Regents of the University of California 340Sstevel@tonic-gate * All Rights Reserved 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 370Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 380Sstevel@tonic-gate * contributors. 390Sstevel@tonic-gate */ 400Sstevel@tonic-gate 410Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 420Sstevel@tonic-gate 430Sstevel@tonic-gate /* 440Sstevel@tonic-gate * chmod option mode files 450Sstevel@tonic-gate * where 460Sstevel@tonic-gate * mode is [ugoa][+-=][rwxXlstugo] or an octal number 47789Sahrens * mode is [<+|->A[# <number] ]<aclspec> 480Sstevel@tonic-gate * option is -R and -f 490Sstevel@tonic-gate */ 500Sstevel@tonic-gate 510Sstevel@tonic-gate /* 520Sstevel@tonic-gate * Note that many convolutions are necessary 530Sstevel@tonic-gate * due to the re-use of bits between locking 540Sstevel@tonic-gate * and setgid 550Sstevel@tonic-gate */ 560Sstevel@tonic-gate 570Sstevel@tonic-gate #include <unistd.h> 580Sstevel@tonic-gate #include <stdlib.h> 590Sstevel@tonic-gate #include <stdio.h> 600Sstevel@tonic-gate #include <sys/types.h> 610Sstevel@tonic-gate #include <sys/stat.h> 620Sstevel@tonic-gate #include <dirent.h> 630Sstevel@tonic-gate #include <locale.h> 640Sstevel@tonic-gate #include <string.h> /* strerror() */ 650Sstevel@tonic-gate #include <stdarg.h> 660Sstevel@tonic-gate #include <limits.h> 67789Sahrens #include <ctype.h> 680Sstevel@tonic-gate #include <errno.h> 690Sstevel@tonic-gate #include <sys/acl.h> 70789Sahrens #include <aclutils.h> 710Sstevel@tonic-gate 720Sstevel@tonic-gate static int rflag; 730Sstevel@tonic-gate static int fflag; 740Sstevel@tonic-gate 750Sstevel@tonic-gate extern int optind; 760Sstevel@tonic-gate extern int errno; 770Sstevel@tonic-gate 780Sstevel@tonic-gate static int mac; /* Alternate to argc (for parseargs) */ 790Sstevel@tonic-gate static char **mav; /* Alternate to argv (for parseargs) */ 800Sstevel@tonic-gate 810Sstevel@tonic-gate static char *ms; /* Points to the mode argument */ 820Sstevel@tonic-gate 83789Sahrens #define ACL_ADD 1 84789Sahrens #define ACL_DELETE 2 85789Sahrens #define ACL_SLOT_DELETE 3 86789Sahrens #define ACL_REPLACE 4 87789Sahrens #define ACL_STRIP 5 88789Sahrens 89789Sahrens typedef struct acl_args { 90789Sahrens acl_t *acl_aclp; 91789Sahrens int acl_slot; 92789Sahrens int acl_action; 93789Sahrens } acl_args_t; 94789Sahrens 950Sstevel@tonic-gate extern mode_t 960Sstevel@tonic-gate newmode_common(char *ms, mode_t new_mode, mode_t umsk, char *file, char *path, 970Sstevel@tonic-gate o_mode_t *group_clear_bits, o_mode_t *group_set_bits); 980Sstevel@tonic-gate 990Sstevel@tonic-gate static int 100789Sahrens dochmod(char *name, char *path, mode_t umsk, acl_args_t *aclp), 101789Sahrens chmodr(char *dir, char *path, mode_t mode, mode_t umsk, acl_args_t *aclp); 102789Sahrens static int doacl(char *file, struct stat *st, acl_args_t *aclp); 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate static void handle_acl(char *name, o_mode_t group_clear_bits, 105789Sahrens o_mode_t group_set_bits); 1060Sstevel@tonic-gate 107789Sahrens static void usage(void); 1080Sstevel@tonic-gate 109789Sahrens void errmsg(int severity, int code, char *format, ...); 1100Sstevel@tonic-gate 111789Sahrens static void parseargs(int ac, char *av[]); 112789Sahrens 113789Sahrens int 114789Sahrens parse_acl_args(char *arg, acl_args_t **acl_args); 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate int 1170Sstevel@tonic-gate main(int argc, char *argv[]) 1180Sstevel@tonic-gate { 1190Sstevel@tonic-gate int i, c; 1200Sstevel@tonic-gate int status = 0; 1210Sstevel@tonic-gate mode_t umsk; 122789Sahrens acl_args_t *acl_args = NULL; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1250Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 1260Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1270Sstevel@tonic-gate #endif 1280Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate parseargs(argc, argv); 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate while ((c = getopt(mac, mav, "Rf")) != EOF) { 1330Sstevel@tonic-gate switch (c) { 1340Sstevel@tonic-gate case 'R': 1350Sstevel@tonic-gate rflag++; 1360Sstevel@tonic-gate break; 1370Sstevel@tonic-gate case 'f': 1380Sstevel@tonic-gate fflag++; 1390Sstevel@tonic-gate break; 1400Sstevel@tonic-gate case '?': 1410Sstevel@tonic-gate usage(); 1420Sstevel@tonic-gate exit(2); 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate /* 1470Sstevel@tonic-gate * Check for sufficient arguments 1480Sstevel@tonic-gate * or a usage error. 1490Sstevel@tonic-gate */ 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate mac -= optind; 1520Sstevel@tonic-gate mav += optind; 1530Sstevel@tonic-gate 154789Sahrens if (mac >= 2 && (mav[0][0] == 'A')) { 155789Sahrens if (parse_acl_args(*mav, &acl_args)) { 156789Sahrens usage(); 157789Sahrens exit(2); 158789Sahrens } 159789Sahrens } else { 160789Sahrens if (mac < 2) { 161789Sahrens usage(); 162789Sahrens exit(2); 163789Sahrens } 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate ms = mav[0]; 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate umsk = umask(0); 1690Sstevel@tonic-gate (void) umask(umsk); 1700Sstevel@tonic-gate 171789Sahrens for (i = 1; i < mac; i++) { 172789Sahrens status += dochmod(mav[i], mav[i], umsk, acl_args); 173789Sahrens } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate return (fflag ? 0 : status); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate static int 179789Sahrens dochmod(char *name, char *path, mode_t umsk, acl_args_t *aclp) 1800Sstevel@tonic-gate { 1810Sstevel@tonic-gate static struct stat st; 1820Sstevel@tonic-gate int linkflg = 0; 1830Sstevel@tonic-gate o_mode_t group_clear_bits, group_set_bits; 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate if (lstat(name, &st) < 0) { 1860Sstevel@tonic-gate errmsg(2, 0, gettext("can't access %s\n"), path); 1870Sstevel@tonic-gate return (1); 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate if ((st.st_mode & S_IFMT) == S_IFLNK) { 1910Sstevel@tonic-gate linkflg = 1; 1920Sstevel@tonic-gate if (stat(name, &st) < 0) { 1930Sstevel@tonic-gate errmsg(2, 0, gettext("can't access %s\n"), path); 1940Sstevel@tonic-gate return (1); 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate /* Do not recurse if directory is object of symbolic link */ 1990Sstevel@tonic-gate if (rflag && ((st.st_mode & S_IFMT) == S_IFDIR) && !linkflg) 200789Sahrens return (chmodr(name, path, st.st_mode, umsk, aclp)); 2010Sstevel@tonic-gate 202789Sahrens if (aclp) { 203789Sahrens return (doacl(name, &st, aclp)); 204789Sahrens } else if (chmod(name, newmode_common(ms, st.st_mode, umsk, name, path, 2050Sstevel@tonic-gate &group_clear_bits, &group_set_bits)) == -1) { 2060Sstevel@tonic-gate errmsg(2, 0, gettext("can't change %s\n"), path); 2070Sstevel@tonic-gate return (1); 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate /* 2110Sstevel@tonic-gate * If the group permissions of the file are being modified, 2120Sstevel@tonic-gate * make sure that the file's ACL (if it has one) is 2130Sstevel@tonic-gate * modified also, since chmod is supposed to apply group 2140Sstevel@tonic-gate * permissions changes to both the acl mask and the 2150Sstevel@tonic-gate * general group permissions. 2160Sstevel@tonic-gate */ 2170Sstevel@tonic-gate if (group_clear_bits || group_set_bits) 2180Sstevel@tonic-gate handle_acl(name, group_clear_bits, group_set_bits); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate return (0); 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate static int 225789Sahrens chmodr(char *dir, char *path, mode_t mode, mode_t umsk, acl_args_t *aclp) 2260Sstevel@tonic-gate { 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate DIR *dirp; 2290Sstevel@tonic-gate struct dirent *dp; 2300Sstevel@tonic-gate char savedir[PATH_MAX]; /* dir name to restore */ 2310Sstevel@tonic-gate char currdir[PATH_MAX+1]; /* current dir name + '/' */ 2320Sstevel@tonic-gate char parentdir[PATH_MAX+1]; /* parent dir name + '/' */ 2330Sstevel@tonic-gate int ecode; 234789Sahrens struct stat st; 2350Sstevel@tonic-gate o_mode_t group_clear_bits, group_set_bits; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate if (getcwd(savedir, PATH_MAX) == 0) 2380Sstevel@tonic-gate errmsg(2, 255, gettext("chmod: could not getcwd %s\n"), 2390Sstevel@tonic-gate savedir); 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate /* 2420Sstevel@tonic-gate * Change what we are given before doing it's contents 2430Sstevel@tonic-gate */ 244789Sahrens if (aclp) { 245789Sahrens if (lstat(dir, &st) < 0) { 246789Sahrens errmsg(2, 0, gettext("can't access %s\n"), path); 247789Sahrens return (1); 248789Sahrens } 249789Sahrens if (doacl(dir, &st, aclp) != 0) 250789Sahrens return (1); 251789Sahrens } else if (chmod(dir, newmode_common(ms, mode, umsk, dir, path, 2520Sstevel@tonic-gate &group_clear_bits, &group_set_bits)) < 0) { 2530Sstevel@tonic-gate errmsg(2, 0, gettext("can't change %s\n"), path); 2540Sstevel@tonic-gate return (1); 2550Sstevel@tonic-gate } 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate /* 2580Sstevel@tonic-gate * If the group permissions of the file are being modified, 2590Sstevel@tonic-gate * make sure that the file's ACL (if it has one) is 2600Sstevel@tonic-gate * modified also, since chmod is supposed to apply group 2610Sstevel@tonic-gate * permissions changes to both the acl mask and the 2620Sstevel@tonic-gate * general group permissions. 2630Sstevel@tonic-gate */ 264789Sahrens 265789Sahrens if (aclp == NULL) { /* only necessary when not setting ACL */ 266789Sahrens if (group_clear_bits || group_set_bits) 267789Sahrens handle_acl(dir, group_clear_bits, group_set_bits); 268789Sahrens } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if (chdir(dir) < 0) { 2710Sstevel@tonic-gate errmsg(2, 0, "%s/%s: %s\n", savedir, dir, strerror(errno)); 2720Sstevel@tonic-gate return (1); 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate if ((dirp = opendir(".")) == NULL) { 2750Sstevel@tonic-gate errmsg(2, 0, "%s\n", strerror(errno)); 2760Sstevel@tonic-gate return (1); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate dp = readdir(dirp); 2790Sstevel@tonic-gate dp = readdir(dirp); /* read "." and ".." */ 2800Sstevel@tonic-gate ecode = 0; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate /* 2830Sstevel@tonic-gate * Save parent directory path before recursive chmod. 2840Sstevel@tonic-gate * We'll need this for error printing purposes. Add 2850Sstevel@tonic-gate * a trailing '/' to the path except in the case where 2860Sstevel@tonic-gate * the path is just '/' 2870Sstevel@tonic-gate */ 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate (void) strcpy(parentdir, path); 2900Sstevel@tonic-gate if (strcmp(path, "/") != 0) 2910Sstevel@tonic-gate (void) strcat(parentdir, "/"); 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) { 2940Sstevel@tonic-gate (void) strcpy(currdir, parentdir); 2950Sstevel@tonic-gate (void) strcat(currdir, dp->d_name); 296789Sahrens ecode += dochmod(dp->d_name, currdir, umsk, aclp); 2970Sstevel@tonic-gate } 2980Sstevel@tonic-gate (void) closedir(dirp); 2990Sstevel@tonic-gate if (chdir(savedir) < 0) { 3000Sstevel@tonic-gate errmsg(2, 255, gettext("can't change back to %s\n"), savedir); 3010Sstevel@tonic-gate } 3020Sstevel@tonic-gate return (ecode ? 1 : 0); 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate /* PRINTFLIKE3 */ 3060Sstevel@tonic-gate void 3070Sstevel@tonic-gate errmsg(int severity, int code, char *format, ...) 3080Sstevel@tonic-gate { 3090Sstevel@tonic-gate va_list ap; 3100Sstevel@tonic-gate static char *msg[] = { 3110Sstevel@tonic-gate "", 3120Sstevel@tonic-gate "ERROR", 3130Sstevel@tonic-gate "WARNING", 3140Sstevel@tonic-gate "" 3150Sstevel@tonic-gate }; 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate va_start(ap, format); 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate /* 3200Sstevel@tonic-gate * Always print error message if this is a fatal error (code == 0); 3210Sstevel@tonic-gate * otherwise, print message if fflag == 0 (no -f option specified) 3220Sstevel@tonic-gate */ 3230Sstevel@tonic-gate if (!fflag || (code != 0)) { 3240Sstevel@tonic-gate (void) fprintf(stderr, 3250Sstevel@tonic-gate "chmod: %s: ", gettext(msg[severity])); 3260Sstevel@tonic-gate (void) vfprintf(stderr, format, ap); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate va_end(ap); 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate if (code != 0) 3320Sstevel@tonic-gate exit(fflag ? 0 : code); 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate static void 3360Sstevel@tonic-gate usage(void) 3370Sstevel@tonic-gate { 3380Sstevel@tonic-gate (void) fprintf(stderr, gettext( 3390Sstevel@tonic-gate "usage:\tchmod [-fR] <absolute-mode> file ...\n")); 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate (void) fprintf(stderr, gettext( 342789Sahrens "\tchmod [-fR] <ACL-operation> file ...\n")); 343789Sahrens 344789Sahrens (void) fprintf(stderr, gettext( 3450Sstevel@tonic-gate "\tchmod [-fR] <symbolic-mode-list> file ...\n")); 3460Sstevel@tonic-gate 347789Sahrens 3480Sstevel@tonic-gate (void) fprintf(stderr, gettext( 3490Sstevel@tonic-gate "where \t<symbolic-mode-list> is a comma-separated list of\n")); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate (void) fprintf(stderr, gettext( 3520Sstevel@tonic-gate "\t[ugoa]{+|-|=}[rwxXlstugo]\n")); 353789Sahrens 354789Sahrens (void) fprintf(stderr, gettext( 355789Sahrens "where \t<ACL-operation> is one of the following\n")); 356789Sahrens (void) fprintf(stderr, gettext("\tA-<acl_specification>\n")); 357789Sahrens (void) fprintf(stderr, gettext("\tA[number]-\n")); 358789Sahrens (void) fprintf(stderr, gettext( 359789Sahrens "\tA[number]{+|=}<acl_specification>\n")); 360789Sahrens (void) fprintf(stderr, gettext( 361789Sahrens "where \t<acl-specification> is a comma-separated list of ACEs\n")); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate /* 3650Sstevel@tonic-gate * parseargs - generate getopt-friendly argument list for backwards 3660Sstevel@tonic-gate * compatibility with earlier Solaris usage (eg, chmod -w 3670Sstevel@tonic-gate * foo). 3680Sstevel@tonic-gate * 3690Sstevel@tonic-gate * assumes the existence of a static set of alternates to argc and argv, 3700Sstevel@tonic-gate * (namely, mac, and mav[]). 3710Sstevel@tonic-gate * 3720Sstevel@tonic-gate */ 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate static void 3750Sstevel@tonic-gate parseargs(int ac, char *av[]) 3760Sstevel@tonic-gate { 3770Sstevel@tonic-gate int i; /* current argument */ 3780Sstevel@tonic-gate int fflag; /* arg list contains "--" */ 3790Sstevel@tonic-gate size_t mav_num; /* number of entries in mav[] */ 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate /* 3820Sstevel@tonic-gate * We add an extra argument slot, in case we need to jam a "--" 3830Sstevel@tonic-gate * argument into the list. 3840Sstevel@tonic-gate */ 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate mav_num = (size_t)ac+2; 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate if ((mav = calloc(mav_num, sizeof (char *))) == NULL) { 3890Sstevel@tonic-gate perror("chmod"); 3900Sstevel@tonic-gate exit(2); 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate /* scan for the use of "--" in the argument list */ 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate for (fflag = i = 0; i < ac; i ++) { 3960Sstevel@tonic-gate if (strcmp(av[i], "--") == 0) 3970Sstevel@tonic-gate fflag = 1; 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate /* process the arguments */ 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate for (i = mac = 0; 4030Sstevel@tonic-gate (av[i] != (char *)NULL) && (av[i][0] != (char)NULL); 4040Sstevel@tonic-gate i++) { 4050Sstevel@tonic-gate if (!fflag && av[i][0] == '-') { 4060Sstevel@tonic-gate /* 4070Sstevel@tonic-gate * If there is not already a "--" argument specified, 4080Sstevel@tonic-gate * and the argument starts with '-' but does not 4090Sstevel@tonic-gate * contain any of the official option letters, then it 4100Sstevel@tonic-gate * is probably a mode argument beginning with '-'. 4110Sstevel@tonic-gate * Force a "--" into the argument stream in front of 4120Sstevel@tonic-gate * it. 4130Sstevel@tonic-gate */ 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate if ((strchr(av[i], 'R') == NULL && 4160Sstevel@tonic-gate strchr(av[i], 'f') == NULL)) { 4170Sstevel@tonic-gate mav[mac++] = strdup("--"); 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate mav[mac++] = strdup(av[i]); 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate mav[mac] = (char *)NULL; 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 427789Sahrens int 428789Sahrens parse_acl_args(char *arg, acl_args_t **acl_args) 429789Sahrens { 430789Sahrens acl_t *new_acl = NULL; 431789Sahrens int slot; 432789Sahrens int len; 433789Sahrens int action; 434789Sahrens acl_args_t *new_acl_args; 435789Sahrens char *acl_spec = NULL; 436789Sahrens char *end; 437789Sahrens 438789Sahrens if (arg[0] != 'A') 439789Sahrens return (1); 440789Sahrens 441789Sahrens slot = strtol(&arg[1], &end, 10); 442789Sahrens 443789Sahrens len = strlen(arg); 444789Sahrens switch (*end) { 445789Sahrens case '+': 446789Sahrens action = ACL_ADD; 447789Sahrens acl_spec = ++end; 448789Sahrens break; 449789Sahrens case '-': 450789Sahrens if (len == 2 && arg[0] == 'A' && arg[1] == '-') 451789Sahrens action = ACL_STRIP; 452789Sahrens else 453789Sahrens action = ACL_DELETE; 454789Sahrens if (action != ACL_STRIP) { 455789Sahrens acl_spec = ++end; 456789Sahrens if (acl_spec[0] == '\0') { 457789Sahrens action = ACL_SLOT_DELETE; 458789Sahrens acl_spec = NULL; 459789Sahrens } else if (arg[1] != '-') 460789Sahrens return (1); 461789Sahrens } 462789Sahrens break; 463789Sahrens case '=': 464865Smarks /* 465865Smarks * Was slot specified? 466865Smarks */ 467865Smarks if (arg[1] == '=') 468865Smarks slot = -1; 469789Sahrens action = ACL_REPLACE; 470789Sahrens acl_spec = ++end; 471789Sahrens break; 472789Sahrens default: 473789Sahrens return (1); 474789Sahrens } 475789Sahrens 476789Sahrens if ((action == ACL_REPLACE || action == ACL_ADD) && acl_spec[0] == '\0') 477789Sahrens return (1); 478789Sahrens 479789Sahrens if (acl_spec) { 480*1420Smarks if (acl_parse(acl_spec, &new_acl)) { 481*1420Smarks exit(1); 482789Sahrens } 483789Sahrens } 484789Sahrens 485789Sahrens new_acl_args = malloc(sizeof (acl_args_t)); 486789Sahrens if (new_acl_args == NULL) 487789Sahrens return (1); 488789Sahrens 489789Sahrens new_acl_args->acl_aclp = new_acl; 490789Sahrens new_acl_args->acl_slot = slot; 491789Sahrens new_acl_args->acl_action = action; 492789Sahrens 493789Sahrens *acl_args = new_acl_args; 494789Sahrens 495789Sahrens return (0); 496789Sahrens } 497789Sahrens 4980Sstevel@tonic-gate /* 4990Sstevel@tonic-gate * This function is called whenever the group permissions of a file 5000Sstevel@tonic-gate * is being modified. According to the chmod(1) manpage, any 5010Sstevel@tonic-gate * change made to the group permissions must be applied to both 5020Sstevel@tonic-gate * the acl mask and the acl's GROUP_OBJ. The chmod(2) already 5030Sstevel@tonic-gate * set the mask, so this routine needs to make the same change 5040Sstevel@tonic-gate * to the GROUP_OBJ. 5050Sstevel@tonic-gate */ 5060Sstevel@tonic-gate static void 5070Sstevel@tonic-gate handle_acl(char *name, o_mode_t group_clear_bits, o_mode_t group_set_bits) 5080Sstevel@tonic-gate { 5090Sstevel@tonic-gate int aclcnt, n; 5100Sstevel@tonic-gate aclent_t *aclp, *tp; 5110Sstevel@tonic-gate o_mode_t newperm; 5120Sstevel@tonic-gate 513789Sahrens /* 514789Sahrens * if this file system support ace_t acl's 515789Sahrens * then simply return since we don't have an 516789Sahrens * acl mask to deal with 517789Sahrens */ 518789Sahrens if (pathconf(name, _PC_ACL_ENABLED) == _ACL_ACE_ENABLED) 519789Sahrens return; 520789Sahrens 5210Sstevel@tonic-gate if ((aclcnt = acl(name, GETACLCNT, 0, NULL)) <= MIN_ACL_ENTRIES) 5220Sstevel@tonic-gate return; /* it's just a trivial acl; no need to change it */ 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate if ((aclp = (aclent_t *)malloc((sizeof (aclent_t)) * aclcnt)) 5250Sstevel@tonic-gate == NULL) { 5260Sstevel@tonic-gate perror("chmod"); 5270Sstevel@tonic-gate exit(2); 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate if (acl(name, GETACL, aclcnt, aclp) < 0) { 5310Sstevel@tonic-gate free(aclp); 5320Sstevel@tonic-gate (void) fprintf(stderr, "chmod: "); 5330Sstevel@tonic-gate perror(name); 5340Sstevel@tonic-gate return; 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate for (tp = aclp, n = aclcnt; n--; tp++) { 5380Sstevel@tonic-gate if (tp->a_type == GROUP_OBJ) { 5390Sstevel@tonic-gate newperm = tp->a_perm; 5400Sstevel@tonic-gate if (group_clear_bits != 0) 5410Sstevel@tonic-gate newperm &= ~group_clear_bits; 5420Sstevel@tonic-gate if (group_set_bits != 0) 5430Sstevel@tonic-gate newperm |= group_set_bits; 5440Sstevel@tonic-gate if (newperm != tp->a_perm) { 5450Sstevel@tonic-gate tp->a_perm = newperm; 5460Sstevel@tonic-gate if (acl(name, SETACL, aclcnt, aclp) 5470Sstevel@tonic-gate < 0) { 5480Sstevel@tonic-gate (void) fprintf(stderr, "chmod: "); 5490Sstevel@tonic-gate perror(name); 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate } 5520Sstevel@tonic-gate break; 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate free(aclp); 5560Sstevel@tonic-gate } 557789Sahrens 558789Sahrens static int 559789Sahrens doacl(char *file, struct stat *st, acl_args_t *acl_args) 560789Sahrens { 561789Sahrens acl_t *aclp; 562789Sahrens acl_t *set_aclp; 563789Sahrens int error = 0; 564789Sahrens void *to, *from; 565789Sahrens int len; 566789Sahrens int isdir; 567789Sahrens 568789Sahrens isdir = S_ISDIR(st->st_mode); 569789Sahrens 570789Sahrens error = acl_get(file, 0, &aclp); 571789Sahrens 572789Sahrens if (error != 0) { 573789Sahrens errmsg(1, 1, "%s\n", acl_strerror(error)); 574789Sahrens return (1); 575789Sahrens } 576789Sahrens 577789Sahrens switch (acl_args->acl_action) { 578789Sahrens case ACL_ADD: 579789Sahrens if ((error = acl_addentries(aclp, 580789Sahrens acl_args->acl_aclp, acl_args->acl_slot)) != 0) { 581789Sahrens errmsg(1, 1, "%s\n", acl_strerror(error)); 582789Sahrens acl_free(aclp); 583789Sahrens return (1); 584789Sahrens } 585789Sahrens set_aclp = aclp; 586789Sahrens break; 587789Sahrens case ACL_SLOT_DELETE: 588789Sahrens 589789Sahrens if (acl_args->acl_slot + 1 > aclp->acl_cnt) { 590789Sahrens errmsg(1, 1, 591789Sahrens gettext("Invalid slot specified for removal\n")); 592789Sahrens acl_free(aclp); 593789Sahrens return (1); 594789Sahrens } 595789Sahrens 596789Sahrens if (acl_args->acl_slot == 0 && aclp->acl_cnt == 1) { 597789Sahrens errmsg(1, 1, 598789Sahrens gettext("Can't remove all ACL " 599789Sahrens "entries from a file\n")); 600789Sahrens acl_free(aclp); 601789Sahrens return (1); 602789Sahrens } 603789Sahrens 604789Sahrens /* 605789Sahrens * remove a single entry 606789Sahrens * 607789Sahrens * if last entry just adjust acl_cnt 608789Sahrens */ 609789Sahrens 610789Sahrens if ((acl_args->acl_slot + 1) == aclp->acl_cnt) 611789Sahrens aclp->acl_cnt--; 612789Sahrens else { 613789Sahrens to = (char *)aclp->acl_aclp + 614789Sahrens (acl_args->acl_slot * aclp->acl_entry_size); 615789Sahrens from = (char *)to + aclp->acl_entry_size; 616789Sahrens len = (aclp->acl_cnt - acl_args->acl_slot - 1) * 617789Sahrens aclp->acl_entry_size; 618789Sahrens (void) memmove(to, from, len); 619789Sahrens aclp->acl_cnt--; 620789Sahrens } 621789Sahrens set_aclp = aclp; 622789Sahrens break; 623789Sahrens 624789Sahrens case ACL_DELETE: 625789Sahrens if ((error = acl_removeentries(aclp, acl_args->acl_aclp, 626789Sahrens acl_args->acl_slot, ACL_REMOVE_ALL)) != 0) { 627789Sahrens errmsg(1, 1, "%s\n", acl_strerror(error)); 628789Sahrens acl_free(aclp); 629789Sahrens return (1); 630789Sahrens } 631789Sahrens 632789Sahrens if (aclp->acl_cnt == 0) { 633789Sahrens errmsg(1, 1, 634789Sahrens gettext("Can't remove all ACL " 635789Sahrens "entries from a file\n")); 636789Sahrens acl_free(aclp); 637789Sahrens return (1); 638789Sahrens } 639789Sahrens 640789Sahrens set_aclp = aclp; 641789Sahrens break; 642789Sahrens case ACL_REPLACE: 643789Sahrens if (acl_args->acl_slot >= 0) { 644789Sahrens error = acl_modifyentries(aclp, acl_args->acl_aclp, 645789Sahrens acl_args->acl_slot); 646789Sahrens if (error) { 647789Sahrens errmsg(1, 1, "%s\n", acl_strerror(error)); 648789Sahrens acl_free(aclp); 649789Sahrens return (1); 650789Sahrens } 651789Sahrens set_aclp = aclp; 652789Sahrens } else { 653789Sahrens set_aclp = acl_args->acl_aclp; 654789Sahrens } 655789Sahrens break; 656789Sahrens case ACL_STRIP: 657789Sahrens error = acl_strip(file, st->st_uid, st->st_gid, st->st_mode); 658789Sahrens if (error) { 659789Sahrens errmsg(1, 1, "%s\n", acl_strerror(error)); 660789Sahrens return (1); 661789Sahrens } 662789Sahrens acl_free(aclp); 663789Sahrens return (0); 664789Sahrens /*NOTREACHED*/ 665789Sahrens default: 666789Sahrens errmsg(1, 0, gettext("Unknown ACL action requested\n")); 667789Sahrens return (1); 668789Sahrens break; 669789Sahrens } 670789Sahrens 671789Sahrens error = acl_check(set_aclp, isdir); 672789Sahrens 673789Sahrens if (error) { 674789Sahrens errmsg(1, 0, "%s\n%s", acl_strerror(error), 675789Sahrens gettext("See chmod(1) for more information on " 676789Sahrens "valid ACL syntax\n")); 677789Sahrens return (1); 678789Sahrens } 679789Sahrens if ((error = acl_set(file, set_aclp)) != 0) { 680789Sahrens errmsg(1, 0, gettext("Failed to set ACL: %s\n"), 681789Sahrens acl_strerror(error)); 682789Sahrens acl_free(aclp); 683789Sahrens return (1); 684789Sahrens } 685789Sahrens acl_free(aclp); 686789Sahrens return (0); 687789Sahrens } 688