xref: /onnv-gate/usr/src/cmd/chmod/chmod.c (revision 789)
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*789Sahrens  * Copyright 2005 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
47*789Sahrens  *	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>
67*789Sahrens #include <ctype.h>
680Sstevel@tonic-gate #include <errno.h>
690Sstevel@tonic-gate #include <sys/acl.h>
70*789Sahrens #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 
83*789Sahrens #define	ACL_ADD		1
84*789Sahrens #define	ACL_DELETE	2
85*789Sahrens #define	ACL_SLOT_DELETE 3
86*789Sahrens #define	ACL_REPLACE	4
87*789Sahrens #define	ACL_STRIP	5
88*789Sahrens 
89*789Sahrens typedef struct acl_args {
90*789Sahrens 	acl_t	*acl_aclp;
91*789Sahrens 	int	acl_slot;
92*789Sahrens 	int	acl_action;
93*789Sahrens } acl_args_t;
94*789Sahrens 
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
100*789Sahrens dochmod(char *name, char *path, mode_t umsk, acl_args_t *aclp),
101*789Sahrens chmodr(char *dir, char *path, mode_t mode, mode_t umsk, acl_args_t *aclp);
102*789Sahrens 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,
105*789Sahrens     o_mode_t group_set_bits);
1060Sstevel@tonic-gate 
107*789Sahrens static void usage(void);
1080Sstevel@tonic-gate 
109*789Sahrens void errmsg(int severity, int code, char *format, ...);
1100Sstevel@tonic-gate 
111*789Sahrens static void parseargs(int ac, char *av[]);
112*789Sahrens 
113*789Sahrens int
114*789Sahrens 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;
122*789Sahrens 	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 
154*789Sahrens 	if (mac >= 2 && (mav[0][0] == 'A')) {
155*789Sahrens 		if (parse_acl_args(*mav, &acl_args)) {
156*789Sahrens 			usage();
157*789Sahrens 			exit(2);
158*789Sahrens 		}
159*789Sahrens 	} else {
160*789Sahrens 		if (mac < 2) {
161*789Sahrens 			usage();
162*789Sahrens 			exit(2);
163*789Sahrens 		}
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 
171*789Sahrens 	for (i = 1; i < mac; i++) {
172*789Sahrens 		status += dochmod(mav[i], mav[i], umsk, acl_args);
173*789Sahrens 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	return (fflag ? 0 : status);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate static int
179*789Sahrens 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)
200*789Sahrens 		return (chmodr(name, path, st.st_mode, umsk, aclp));
2010Sstevel@tonic-gate 
202*789Sahrens 	if (aclp) {
203*789Sahrens 		return (doacl(name, &st, aclp));
204*789Sahrens 	} 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
225*789Sahrens 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;
234*789Sahrens 	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 	 */
244*789Sahrens 	if (aclp) {
245*789Sahrens 		if (lstat(dir, &st) < 0) {
246*789Sahrens 			errmsg(2, 0, gettext("can't access %s\n"), path);
247*789Sahrens 			return (1);
248*789Sahrens 		}
249*789Sahrens 		if (doacl(dir, &st, aclp) != 0)
250*789Sahrens 			return (1);
251*789Sahrens 	} 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 	 */
264*789Sahrens 
265*789Sahrens 	if (aclp == NULL) { /* only necessary when not setting ACL */
266*789Sahrens 		if (group_clear_bits || group_set_bits)
267*789Sahrens 			handle_acl(dir, group_clear_bits, group_set_bits);
268*789Sahrens 	}
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);
296*789Sahrens 		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(
342*789Sahrens 	    "\tchmod [-fR] <ACL-operation> file ...\n"));
343*789Sahrens 
344*789Sahrens 	(void) fprintf(stderr, gettext(
3450Sstevel@tonic-gate 	    "\tchmod [-fR] <symbolic-mode-list> file ...\n"));
3460Sstevel@tonic-gate 
347*789Sahrens 
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"));
353*789Sahrens 
354*789Sahrens 	(void) fprintf(stderr, gettext(
355*789Sahrens 	    "where \t<ACL-operation> is one of the following\n"));
356*789Sahrens 	(void) fprintf(stderr, gettext("\tA-<acl_specification>\n"));
357*789Sahrens 	(void) fprintf(stderr, gettext("\tA[number]-\n"));
358*789Sahrens 	(void) fprintf(stderr, gettext(
359*789Sahrens 	    "\tA[number]{+|=}<acl_specification>\n"));
360*789Sahrens 	(void) fprintf(stderr, gettext(
361*789Sahrens 	    "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 
427*789Sahrens int
428*789Sahrens parse_acl_args(char *arg, acl_args_t **acl_args)
429*789Sahrens {
430*789Sahrens 	acl_t *new_acl = NULL;
431*789Sahrens 	int slot;
432*789Sahrens 	int error;
433*789Sahrens 	int len;
434*789Sahrens 	int action;
435*789Sahrens 	acl_args_t *new_acl_args;
436*789Sahrens 	char *acl_spec = NULL;
437*789Sahrens 	char *end;
438*789Sahrens 
439*789Sahrens 	if (arg[0] != 'A')
440*789Sahrens 		return (1);
441*789Sahrens 
442*789Sahrens 	slot = strtol(&arg[1], &end, 10);
443*789Sahrens 
444*789Sahrens 	len = strlen(arg);
445*789Sahrens 	switch (*end) {
446*789Sahrens 	case '+':
447*789Sahrens 		action = ACL_ADD;
448*789Sahrens 		acl_spec = ++end;
449*789Sahrens 		break;
450*789Sahrens 	case '-':
451*789Sahrens 		if (len == 2 && arg[0] == 'A' && arg[1] == '-')
452*789Sahrens 			action = ACL_STRIP;
453*789Sahrens 		else
454*789Sahrens 			action = ACL_DELETE;
455*789Sahrens 		if (action != ACL_STRIP) {
456*789Sahrens 			acl_spec = ++end;
457*789Sahrens 			if (acl_spec[0] == '\0') {
458*789Sahrens 				action = ACL_SLOT_DELETE;
459*789Sahrens 				acl_spec = NULL;
460*789Sahrens 			} else if (arg[1] != '-')
461*789Sahrens 				return (1);
462*789Sahrens 		}
463*789Sahrens 		break;
464*789Sahrens 	case '=':
465*789Sahrens 		action = ACL_REPLACE;
466*789Sahrens 		acl_spec = ++end;
467*789Sahrens 		break;
468*789Sahrens 	default:
469*789Sahrens 		return (1);
470*789Sahrens 	}
471*789Sahrens 
472*789Sahrens 	if ((action == ACL_REPLACE || action == ACL_ADD) && acl_spec[0] == '\0')
473*789Sahrens 		return (1);
474*789Sahrens 
475*789Sahrens 	if (acl_spec) {
476*789Sahrens 		if (error = acl_fromtext(acl_spec, &new_acl)) {
477*789Sahrens 			errmsg(1, 1, "%s\n", acl_strerror(error));
478*789Sahrens 			return (1);
479*789Sahrens 		}
480*789Sahrens 	}
481*789Sahrens 
482*789Sahrens 	new_acl_args = malloc(sizeof (acl_args_t));
483*789Sahrens 	if (new_acl_args == NULL)
484*789Sahrens 		return (1);
485*789Sahrens 
486*789Sahrens 	new_acl_args->acl_aclp = new_acl;
487*789Sahrens 	new_acl_args->acl_slot = slot;
488*789Sahrens 	new_acl_args->acl_action = action;
489*789Sahrens 
490*789Sahrens 	*acl_args = new_acl_args;
491*789Sahrens 
492*789Sahrens 	return (0);
493*789Sahrens }
494*789Sahrens 
4950Sstevel@tonic-gate /*
4960Sstevel@tonic-gate  * This function is called whenever the group permissions of a file
4970Sstevel@tonic-gate  * is being modified.  According to the chmod(1) manpage, any
4980Sstevel@tonic-gate  * change made to the group permissions must be applied to both
4990Sstevel@tonic-gate  * the acl mask and the acl's GROUP_OBJ.  The chmod(2) already
5000Sstevel@tonic-gate  * set the mask, so this routine needs to make the same change
5010Sstevel@tonic-gate  * to the GROUP_OBJ.
5020Sstevel@tonic-gate  */
5030Sstevel@tonic-gate static void
5040Sstevel@tonic-gate handle_acl(char *name, o_mode_t group_clear_bits, o_mode_t group_set_bits)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate 	int aclcnt, n;
5070Sstevel@tonic-gate 	aclent_t *aclp, *tp;
5080Sstevel@tonic-gate 	o_mode_t newperm;
5090Sstevel@tonic-gate 
510*789Sahrens 	/*
511*789Sahrens 	 * if this file system support ace_t acl's
512*789Sahrens 	 * then simply return since we don't have an
513*789Sahrens 	 * acl mask to deal with
514*789Sahrens 	 */
515*789Sahrens 	if (pathconf(name, _PC_ACL_ENABLED) == _ACL_ACE_ENABLED)
516*789Sahrens 		return;
517*789Sahrens 
5180Sstevel@tonic-gate 	if ((aclcnt = acl(name, GETACLCNT, 0, NULL)) <= MIN_ACL_ENTRIES)
5190Sstevel@tonic-gate 		return;	/* it's just a trivial acl; no need to change it */
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if ((aclp = (aclent_t *)malloc((sizeof (aclent_t)) * aclcnt))
5220Sstevel@tonic-gate 	    == NULL) {
5230Sstevel@tonic-gate 		perror("chmod");
5240Sstevel@tonic-gate 		exit(2);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	if (acl(name, GETACL, aclcnt, aclp) < 0) {
5280Sstevel@tonic-gate 		free(aclp);
5290Sstevel@tonic-gate 		(void) fprintf(stderr, "chmod: ");
5300Sstevel@tonic-gate 		perror(name);
5310Sstevel@tonic-gate 		return;
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	for (tp = aclp, n = aclcnt; n--; tp++) {
5350Sstevel@tonic-gate 		if (tp->a_type == GROUP_OBJ) {
5360Sstevel@tonic-gate 			newperm = tp->a_perm;
5370Sstevel@tonic-gate 			if (group_clear_bits != 0)
5380Sstevel@tonic-gate 				newperm &= ~group_clear_bits;
5390Sstevel@tonic-gate 			if (group_set_bits != 0)
5400Sstevel@tonic-gate 				newperm |= group_set_bits;
5410Sstevel@tonic-gate 			if (newperm != tp->a_perm) {
5420Sstevel@tonic-gate 				tp->a_perm = newperm;
5430Sstevel@tonic-gate 				if (acl(name, SETACL, aclcnt, aclp)
5440Sstevel@tonic-gate 				    < 0) {
5450Sstevel@tonic-gate 					(void) fprintf(stderr, "chmod: ");
5460Sstevel@tonic-gate 					perror(name);
5470Sstevel@tonic-gate 				}
5480Sstevel@tonic-gate 			}
5490Sstevel@tonic-gate 			break;
5500Sstevel@tonic-gate 		}
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 	free(aclp);
5530Sstevel@tonic-gate }
554*789Sahrens 
555*789Sahrens static int
556*789Sahrens doacl(char *file, struct stat *st, acl_args_t *acl_args)
557*789Sahrens {
558*789Sahrens 	acl_t *aclp;
559*789Sahrens 	acl_t *set_aclp;
560*789Sahrens 	int error = 0;
561*789Sahrens 	void *to, *from;
562*789Sahrens 	int len;
563*789Sahrens 	int isdir;
564*789Sahrens 
565*789Sahrens 	isdir = S_ISDIR(st->st_mode);
566*789Sahrens 
567*789Sahrens 	error = acl_get(file, 0, &aclp);
568*789Sahrens 
569*789Sahrens 	if (error != 0) {
570*789Sahrens 		errmsg(1, 1, "%s\n", acl_strerror(error));
571*789Sahrens 		return (1);
572*789Sahrens 	}
573*789Sahrens 
574*789Sahrens 	switch (acl_args->acl_action) {
575*789Sahrens 	case ACL_ADD:
576*789Sahrens 		if ((error = acl_addentries(aclp,
577*789Sahrens 			acl_args->acl_aclp, acl_args->acl_slot)) != 0) {
578*789Sahrens 				errmsg(1, 1, "%s\n", acl_strerror(error));
579*789Sahrens 				acl_free(aclp);
580*789Sahrens 				return (1);
581*789Sahrens 		}
582*789Sahrens 		set_aclp = aclp;
583*789Sahrens 		break;
584*789Sahrens 	case ACL_SLOT_DELETE:
585*789Sahrens 
586*789Sahrens 		if (acl_args->acl_slot + 1 > aclp->acl_cnt) {
587*789Sahrens 			errmsg(1, 1,
588*789Sahrens 			    gettext("Invalid slot specified for removal\n"));
589*789Sahrens 			acl_free(aclp);
590*789Sahrens 			return (1);
591*789Sahrens 		}
592*789Sahrens 
593*789Sahrens 		if (acl_args->acl_slot == 0 && aclp->acl_cnt == 1) {
594*789Sahrens 			errmsg(1, 1,
595*789Sahrens 			    gettext("Can't remove all ACL "
596*789Sahrens 			    "entries from a file\n"));
597*789Sahrens 			acl_free(aclp);
598*789Sahrens 			return (1);
599*789Sahrens 		}
600*789Sahrens 
601*789Sahrens 		/*
602*789Sahrens 		 * remove a single entry
603*789Sahrens 		 *
604*789Sahrens 		 * if last entry just adjust acl_cnt
605*789Sahrens 		 */
606*789Sahrens 
607*789Sahrens 		if ((acl_args->acl_slot + 1) == aclp->acl_cnt)
608*789Sahrens 			aclp->acl_cnt--;
609*789Sahrens 		else {
610*789Sahrens 			to = (char *)aclp->acl_aclp +
611*789Sahrens 			    (acl_args->acl_slot * aclp->acl_entry_size);
612*789Sahrens 			from = (char *)to + aclp->acl_entry_size;
613*789Sahrens 			len = (aclp->acl_cnt - acl_args->acl_slot - 1) *
614*789Sahrens 			    aclp->acl_entry_size;
615*789Sahrens 			(void) memmove(to, from, len);
616*789Sahrens 			aclp->acl_cnt--;
617*789Sahrens 		}
618*789Sahrens 		set_aclp = aclp;
619*789Sahrens 		break;
620*789Sahrens 
621*789Sahrens 	case ACL_DELETE:
622*789Sahrens 		if ((error = acl_removeentries(aclp, acl_args->acl_aclp,
623*789Sahrens 		    acl_args->acl_slot, ACL_REMOVE_ALL)) != 0) {
624*789Sahrens 			errmsg(1, 1, "%s\n", acl_strerror(error));
625*789Sahrens 			acl_free(aclp);
626*789Sahrens 			return (1);
627*789Sahrens 		}
628*789Sahrens 
629*789Sahrens 		if (aclp->acl_cnt == 0) {
630*789Sahrens 			errmsg(1, 1,
631*789Sahrens 			    gettext("Can't remove all ACL "
632*789Sahrens 			    "entries from a file\n"));
633*789Sahrens 			acl_free(aclp);
634*789Sahrens 			return (1);
635*789Sahrens 		}
636*789Sahrens 
637*789Sahrens 		set_aclp = aclp;
638*789Sahrens 		break;
639*789Sahrens 	case ACL_REPLACE:
640*789Sahrens 		if (acl_args->acl_slot >= 0)  {
641*789Sahrens 			error = acl_modifyentries(aclp, acl_args->acl_aclp,
642*789Sahrens 			    acl_args->acl_slot);
643*789Sahrens 			if (error) {
644*789Sahrens 				errmsg(1, 1, "%s\n", acl_strerror(error));
645*789Sahrens 				acl_free(aclp);
646*789Sahrens 				return (1);
647*789Sahrens 			}
648*789Sahrens 			set_aclp = aclp;
649*789Sahrens 		} else {
650*789Sahrens 			set_aclp = acl_args->acl_aclp;
651*789Sahrens 		}
652*789Sahrens 		break;
653*789Sahrens 	case ACL_STRIP:
654*789Sahrens 		error = acl_strip(file, st->st_uid, st->st_gid, st->st_mode);
655*789Sahrens 		if (error) {
656*789Sahrens 			errmsg(1, 1, "%s\n", acl_strerror(error));
657*789Sahrens 			return (1);
658*789Sahrens 		}
659*789Sahrens 		acl_free(aclp);
660*789Sahrens 		return (0);
661*789Sahrens 		/*NOTREACHED*/
662*789Sahrens 	default:
663*789Sahrens 		errmsg(1, 0, gettext("Unknown ACL action requested\n"));
664*789Sahrens 		return (1);
665*789Sahrens 		break;
666*789Sahrens 	}
667*789Sahrens 
668*789Sahrens 	error = acl_check(set_aclp, isdir);
669*789Sahrens 
670*789Sahrens 	if (error) {
671*789Sahrens 		errmsg(1, 0, "%s\n%s", acl_strerror(error),
672*789Sahrens 		    gettext("See chmod(1) for more information on "
673*789Sahrens 		    "valid ACL syntax\n"));
674*789Sahrens 		return (1);
675*789Sahrens 	}
676*789Sahrens 	if ((error = acl_set(file, set_aclp)) != 0) {
677*789Sahrens 			errmsg(1, 0, gettext("Failed to set ACL: %s\n"),
678*789Sahrens 			    acl_strerror(error));
679*789Sahrens 			acl_free(aclp);
680*789Sahrens 			return (1);
681*789Sahrens 	}
682*789Sahrens 	acl_free(aclp);
683*789Sahrens 	return (0);
684*789Sahrens }
685