xref: /onnv-gate/usr/src/cmd/chmod/chmod.c (revision 865)
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 /*
23789Sahrens  * 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
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 error;
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 '=':
465*865Smarks 		/*
466*865Smarks 		 * Was slot specified?
467*865Smarks 		 */
468*865Smarks 		if (arg[1] == '=')
469*865Smarks 			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) {
481789Sahrens 		if (error = acl_fromtext(acl_spec, &new_acl)) {
482789Sahrens 			errmsg(1, 1, "%s\n", acl_strerror(error));
483789Sahrens 			return (1);
484789Sahrens 		}
485789Sahrens 	}
486789Sahrens 
487789Sahrens 	new_acl_args = malloc(sizeof (acl_args_t));
488789Sahrens 	if (new_acl_args == NULL)
489789Sahrens 		return (1);
490789Sahrens 
491789Sahrens 	new_acl_args->acl_aclp = new_acl;
492789Sahrens 	new_acl_args->acl_slot = slot;
493789Sahrens 	new_acl_args->acl_action = action;
494789Sahrens 
495789Sahrens 	*acl_args = new_acl_args;
496789Sahrens 
497789Sahrens 	return (0);
498789Sahrens }
499789Sahrens 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  * This function is called whenever the group permissions of a file
5020Sstevel@tonic-gate  * is being modified.  According to the chmod(1) manpage, any
5030Sstevel@tonic-gate  * change made to the group permissions must be applied to both
5040Sstevel@tonic-gate  * the acl mask and the acl's GROUP_OBJ.  The chmod(2) already
5050Sstevel@tonic-gate  * set the mask, so this routine needs to make the same change
5060Sstevel@tonic-gate  * to the GROUP_OBJ.
5070Sstevel@tonic-gate  */
5080Sstevel@tonic-gate static void
5090Sstevel@tonic-gate handle_acl(char *name, o_mode_t group_clear_bits, o_mode_t group_set_bits)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	int aclcnt, n;
5120Sstevel@tonic-gate 	aclent_t *aclp, *tp;
5130Sstevel@tonic-gate 	o_mode_t newperm;
5140Sstevel@tonic-gate 
515789Sahrens 	/*
516789Sahrens 	 * if this file system support ace_t acl's
517789Sahrens 	 * then simply return since we don't have an
518789Sahrens 	 * acl mask to deal with
519789Sahrens 	 */
520789Sahrens 	if (pathconf(name, _PC_ACL_ENABLED) == _ACL_ACE_ENABLED)
521789Sahrens 		return;
522789Sahrens 
5230Sstevel@tonic-gate 	if ((aclcnt = acl(name, GETACLCNT, 0, NULL)) <= MIN_ACL_ENTRIES)
5240Sstevel@tonic-gate 		return;	/* it's just a trivial acl; no need to change it */
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if ((aclp = (aclent_t *)malloc((sizeof (aclent_t)) * aclcnt))
5270Sstevel@tonic-gate 	    == NULL) {
5280Sstevel@tonic-gate 		perror("chmod");
5290Sstevel@tonic-gate 		exit(2);
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if (acl(name, GETACL, aclcnt, aclp) < 0) {
5330Sstevel@tonic-gate 		free(aclp);
5340Sstevel@tonic-gate 		(void) fprintf(stderr, "chmod: ");
5350Sstevel@tonic-gate 		perror(name);
5360Sstevel@tonic-gate 		return;
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	for (tp = aclp, n = aclcnt; n--; tp++) {
5400Sstevel@tonic-gate 		if (tp->a_type == GROUP_OBJ) {
5410Sstevel@tonic-gate 			newperm = tp->a_perm;
5420Sstevel@tonic-gate 			if (group_clear_bits != 0)
5430Sstevel@tonic-gate 				newperm &= ~group_clear_bits;
5440Sstevel@tonic-gate 			if (group_set_bits != 0)
5450Sstevel@tonic-gate 				newperm |= group_set_bits;
5460Sstevel@tonic-gate 			if (newperm != tp->a_perm) {
5470Sstevel@tonic-gate 				tp->a_perm = newperm;
5480Sstevel@tonic-gate 				if (acl(name, SETACL, aclcnt, aclp)
5490Sstevel@tonic-gate 				    < 0) {
5500Sstevel@tonic-gate 					(void) fprintf(stderr, "chmod: ");
5510Sstevel@tonic-gate 					perror(name);
5520Sstevel@tonic-gate 				}
5530Sstevel@tonic-gate 			}
5540Sstevel@tonic-gate 			break;
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 	free(aclp);
5580Sstevel@tonic-gate }
559789Sahrens 
560789Sahrens static int
561789Sahrens doacl(char *file, struct stat *st, acl_args_t *acl_args)
562789Sahrens {
563789Sahrens 	acl_t *aclp;
564789Sahrens 	acl_t *set_aclp;
565789Sahrens 	int error = 0;
566789Sahrens 	void *to, *from;
567789Sahrens 	int len;
568789Sahrens 	int isdir;
569789Sahrens 
570789Sahrens 	isdir = S_ISDIR(st->st_mode);
571789Sahrens 
572789Sahrens 	error = acl_get(file, 0, &aclp);
573789Sahrens 
574789Sahrens 	if (error != 0) {
575789Sahrens 		errmsg(1, 1, "%s\n", acl_strerror(error));
576789Sahrens 		return (1);
577789Sahrens 	}
578789Sahrens 
579789Sahrens 	switch (acl_args->acl_action) {
580789Sahrens 	case ACL_ADD:
581789Sahrens 		if ((error = acl_addentries(aclp,
582789Sahrens 			acl_args->acl_aclp, acl_args->acl_slot)) != 0) {
583789Sahrens 				errmsg(1, 1, "%s\n", acl_strerror(error));
584789Sahrens 				acl_free(aclp);
585789Sahrens 				return (1);
586789Sahrens 		}
587789Sahrens 		set_aclp = aclp;
588789Sahrens 		break;
589789Sahrens 	case ACL_SLOT_DELETE:
590789Sahrens 
591789Sahrens 		if (acl_args->acl_slot + 1 > aclp->acl_cnt) {
592789Sahrens 			errmsg(1, 1,
593789Sahrens 			    gettext("Invalid slot specified for removal\n"));
594789Sahrens 			acl_free(aclp);
595789Sahrens 			return (1);
596789Sahrens 		}
597789Sahrens 
598789Sahrens 		if (acl_args->acl_slot == 0 && aclp->acl_cnt == 1) {
599789Sahrens 			errmsg(1, 1,
600789Sahrens 			    gettext("Can't remove all ACL "
601789Sahrens 			    "entries from a file\n"));
602789Sahrens 			acl_free(aclp);
603789Sahrens 			return (1);
604789Sahrens 		}
605789Sahrens 
606789Sahrens 		/*
607789Sahrens 		 * remove a single entry
608789Sahrens 		 *
609789Sahrens 		 * if last entry just adjust acl_cnt
610789Sahrens 		 */
611789Sahrens 
612789Sahrens 		if ((acl_args->acl_slot + 1) == aclp->acl_cnt)
613789Sahrens 			aclp->acl_cnt--;
614789Sahrens 		else {
615789Sahrens 			to = (char *)aclp->acl_aclp +
616789Sahrens 			    (acl_args->acl_slot * aclp->acl_entry_size);
617789Sahrens 			from = (char *)to + aclp->acl_entry_size;
618789Sahrens 			len = (aclp->acl_cnt - acl_args->acl_slot - 1) *
619789Sahrens 			    aclp->acl_entry_size;
620789Sahrens 			(void) memmove(to, from, len);
621789Sahrens 			aclp->acl_cnt--;
622789Sahrens 		}
623789Sahrens 		set_aclp = aclp;
624789Sahrens 		break;
625789Sahrens 
626789Sahrens 	case ACL_DELETE:
627789Sahrens 		if ((error = acl_removeentries(aclp, acl_args->acl_aclp,
628789Sahrens 		    acl_args->acl_slot, ACL_REMOVE_ALL)) != 0) {
629789Sahrens 			errmsg(1, 1, "%s\n", acl_strerror(error));
630789Sahrens 			acl_free(aclp);
631789Sahrens 			return (1);
632789Sahrens 		}
633789Sahrens 
634789Sahrens 		if (aclp->acl_cnt == 0) {
635789Sahrens 			errmsg(1, 1,
636789Sahrens 			    gettext("Can't remove all ACL "
637789Sahrens 			    "entries from a file\n"));
638789Sahrens 			acl_free(aclp);
639789Sahrens 			return (1);
640789Sahrens 		}
641789Sahrens 
642789Sahrens 		set_aclp = aclp;
643789Sahrens 		break;
644789Sahrens 	case ACL_REPLACE:
645789Sahrens 		if (acl_args->acl_slot >= 0)  {
646789Sahrens 			error = acl_modifyentries(aclp, acl_args->acl_aclp,
647789Sahrens 			    acl_args->acl_slot);
648789Sahrens 			if (error) {
649789Sahrens 				errmsg(1, 1, "%s\n", acl_strerror(error));
650789Sahrens 				acl_free(aclp);
651789Sahrens 				return (1);
652789Sahrens 			}
653789Sahrens 			set_aclp = aclp;
654789Sahrens 		} else {
655789Sahrens 			set_aclp = acl_args->acl_aclp;
656789Sahrens 		}
657789Sahrens 		break;
658789Sahrens 	case ACL_STRIP:
659789Sahrens 		error = acl_strip(file, st->st_uid, st->st_gid, st->st_mode);
660789Sahrens 		if (error) {
661789Sahrens 			errmsg(1, 1, "%s\n", acl_strerror(error));
662789Sahrens 			return (1);
663789Sahrens 		}
664789Sahrens 		acl_free(aclp);
665789Sahrens 		return (0);
666789Sahrens 		/*NOTREACHED*/
667789Sahrens 	default:
668789Sahrens 		errmsg(1, 0, gettext("Unknown ACL action requested\n"));
669789Sahrens 		return (1);
670789Sahrens 		break;
671789Sahrens 	}
672789Sahrens 
673789Sahrens 	error = acl_check(set_aclp, isdir);
674789Sahrens 
675789Sahrens 	if (error) {
676789Sahrens 		errmsg(1, 0, "%s\n%s", acl_strerror(error),
677789Sahrens 		    gettext("See chmod(1) for more information on "
678789Sahrens 		    "valid ACL syntax\n"));
679789Sahrens 		return (1);
680789Sahrens 	}
681789Sahrens 	if ((error = acl_set(file, set_aclp)) != 0) {
682789Sahrens 			errmsg(1, 0, gettext("Failed to set ACL: %s\n"),
683789Sahrens 			    acl_strerror(error));
684789Sahrens 			acl_free(aclp);
685789Sahrens 			return (1);
686789Sahrens 	}
687789Sahrens 	acl_free(aclp);
688789Sahrens 	return (0);
689789Sahrens }
690