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