xref: /titanic_52/usr/src/cmd/chmod/chmod.c (revision 5a5eeccada4b11bc692e9a5015d5f4a4f188226c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*5a5eeccaSmarks  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T			*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved						*/
297c478bd9Sstevel@tonic-gate /*									*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
337c478bd9Sstevel@tonic-gate  * The Regents of the University of California
347c478bd9Sstevel@tonic-gate  * All Rights Reserved
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
377c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
387c478bd9Sstevel@tonic-gate  * contributors.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * chmod option mode files
457c478bd9Sstevel@tonic-gate  * where
467c478bd9Sstevel@tonic-gate  *	mode is [ugoa][+-=][rwxXlstugo] or an octal number
47fa9e4066Sahrens  *	mode is [<+|->A[# <number] ]<aclspec>
487c478bd9Sstevel@tonic-gate  *	option is -R and -f
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  *  Note that many convolutions are necessary
537c478bd9Sstevel@tonic-gate  *  due to the re-use of bits between locking
547c478bd9Sstevel@tonic-gate  *  and setgid
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <unistd.h>
587c478bd9Sstevel@tonic-gate #include <stdlib.h>
597c478bd9Sstevel@tonic-gate #include <stdio.h>
607c478bd9Sstevel@tonic-gate #include <sys/types.h>
617c478bd9Sstevel@tonic-gate #include <sys/stat.h>
627c478bd9Sstevel@tonic-gate #include <dirent.h>
637c478bd9Sstevel@tonic-gate #include <locale.h>
647c478bd9Sstevel@tonic-gate #include <string.h>	/* strerror() */
657c478bd9Sstevel@tonic-gate #include <stdarg.h>
667c478bd9Sstevel@tonic-gate #include <limits.h>
67fa9e4066Sahrens #include <ctype.h>
687c478bd9Sstevel@tonic-gate #include <errno.h>
697c478bd9Sstevel@tonic-gate #include <sys/acl.h>
70fa9e4066Sahrens #include <aclutils.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static int	rflag;
737c478bd9Sstevel@tonic-gate static int	fflag;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate extern int	optind;
767c478bd9Sstevel@tonic-gate extern int	errno;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static int	mac;		/* Alternate to argc (for parseargs) */
797c478bd9Sstevel@tonic-gate static char	**mav;		/* Alternate to argv (for parseargs) */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static char	*ms;		/* Points to the mode argument */
827c478bd9Sstevel@tonic-gate 
83fa9e4066Sahrens #define	ACL_ADD		1
84fa9e4066Sahrens #define	ACL_DELETE	2
85fa9e4066Sahrens #define	ACL_SLOT_DELETE 3
86fa9e4066Sahrens #define	ACL_REPLACE	4
87fa9e4066Sahrens #define	ACL_STRIP	5
88fa9e4066Sahrens 
89fa9e4066Sahrens typedef struct acl_args {
90fa9e4066Sahrens 	acl_t	*acl_aclp;
91fa9e4066Sahrens 	int	acl_slot;
92fa9e4066Sahrens 	int	acl_action;
93fa9e4066Sahrens } acl_args_t;
94fa9e4066Sahrens 
957c478bd9Sstevel@tonic-gate extern mode_t
967c478bd9Sstevel@tonic-gate newmode_common(char *ms, mode_t new_mode, mode_t umsk, char *file, char *path,
977c478bd9Sstevel@tonic-gate 	o_mode_t *group_clear_bits, o_mode_t *group_set_bits);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static int
100fa9e4066Sahrens dochmod(char *name, char *path, mode_t umsk, acl_args_t *aclp),
101fa9e4066Sahrens chmodr(char *dir, char *path, mode_t mode, mode_t umsk, acl_args_t *aclp);
102fa9e4066Sahrens static int doacl(char *file, struct stat *st, acl_args_t *aclp);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static void handle_acl(char *name, o_mode_t group_clear_bits,
1057c478bd9Sstevel@tonic-gate     o_mode_t group_set_bits);
1067c478bd9Sstevel@tonic-gate 
107fa9e4066Sahrens static void usage(void);
1087c478bd9Sstevel@tonic-gate 
109fa9e4066Sahrens void errmsg(int severity, int code, char *format, ...);
1107c478bd9Sstevel@tonic-gate 
111fa9e4066Sahrens static void parseargs(int ac, char *av[]);
112fa9e4066Sahrens 
113fa9e4066Sahrens int
114fa9e4066Sahrens parse_acl_args(char *arg, acl_args_t **acl_args);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate int
1177c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	int i, c;
1207c478bd9Sstevel@tonic-gate 	int status = 0;
1217c478bd9Sstevel@tonic-gate 	mode_t umsk;
122fa9e4066Sahrens 	acl_args_t *acl_args = NULL;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1257c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1267c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	parseargs(argc, argv);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	while ((c = getopt(mac, mav, "Rf")) != EOF) {
1337c478bd9Sstevel@tonic-gate 		switch (c) {
1347c478bd9Sstevel@tonic-gate 		case 'R':
1357c478bd9Sstevel@tonic-gate 			rflag++;
1367c478bd9Sstevel@tonic-gate 			break;
1377c478bd9Sstevel@tonic-gate 		case 'f':
1387c478bd9Sstevel@tonic-gate 			fflag++;
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 		case '?':
1417c478bd9Sstevel@tonic-gate 			usage();
1427c478bd9Sstevel@tonic-gate 			exit(2);
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * Check for sufficient arguments
1487c478bd9Sstevel@tonic-gate 	 * or a usage error.
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	mac -= optind;
1527c478bd9Sstevel@tonic-gate 	mav += optind;
1537c478bd9Sstevel@tonic-gate 
154fa9e4066Sahrens 	if (mac >= 2 && (mav[0][0] == 'A')) {
155fa9e4066Sahrens 		if (parse_acl_args(*mav, &acl_args)) {
156fa9e4066Sahrens 			usage();
157fa9e4066Sahrens 			exit(2);
158fa9e4066Sahrens 		}
159fa9e4066Sahrens 	} else {
1607c478bd9Sstevel@tonic-gate 		if (mac < 2) {
1617c478bd9Sstevel@tonic-gate 			usage();
1627c478bd9Sstevel@tonic-gate 			exit(2);
1637c478bd9Sstevel@tonic-gate 		}
164fa9e4066Sahrens 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	ms = mav[0];
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	umsk = umask(0);
1697c478bd9Sstevel@tonic-gate 	(void) umask(umsk);
1707c478bd9Sstevel@tonic-gate 
171fa9e4066Sahrens 	for (i = 1; i < mac; i++) {
172fa9e4066Sahrens 		status += dochmod(mav[i], mav[i], umsk, acl_args);
173fa9e4066Sahrens 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	return (fflag ? 0 : status);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static int
179fa9e4066Sahrens dochmod(char *name, char *path, mode_t umsk, acl_args_t *aclp)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	static struct stat st;
1827c478bd9Sstevel@tonic-gate 	int linkflg = 0;
1837c478bd9Sstevel@tonic-gate 	o_mode_t	group_clear_bits, group_set_bits;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (lstat(name, &st) < 0) {
1867c478bd9Sstevel@tonic-gate 		errmsg(2, 0, gettext("can't access %s\n"), path);
1877c478bd9Sstevel@tonic-gate 		return (1);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if ((st.st_mode & S_IFMT) == S_IFLNK) {
1917c478bd9Sstevel@tonic-gate 		linkflg = 1;
1927c478bd9Sstevel@tonic-gate 		if (stat(name, &st) < 0) {
1937c478bd9Sstevel@tonic-gate 			errmsg(2, 0, gettext("can't access %s\n"), path);
1947c478bd9Sstevel@tonic-gate 			return (1);
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/* Do not recurse if directory is object of symbolic link */
1997c478bd9Sstevel@tonic-gate 	if (rflag && ((st.st_mode & S_IFMT) == S_IFDIR) && !linkflg)
200fa9e4066Sahrens 		return (chmodr(name, path, st.st_mode, umsk, aclp));
2017c478bd9Sstevel@tonic-gate 
202fa9e4066Sahrens 	if (aclp) {
203fa9e4066Sahrens 		return (doacl(name, &st, aclp));
204fa9e4066Sahrens 	} else if (chmod(name, newmode_common(ms, st.st_mode, umsk, name, path,
2057c478bd9Sstevel@tonic-gate 	    &group_clear_bits, &group_set_bits)) == -1) {
2067c478bd9Sstevel@tonic-gate 		errmsg(2, 0, gettext("can't change %s\n"), path);
2077c478bd9Sstevel@tonic-gate 		return (1);
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/*
2117c478bd9Sstevel@tonic-gate 	 * If the group permissions of the file are being modified,
2127c478bd9Sstevel@tonic-gate 	 * make sure that the file's ACL (if it has one) is
2137c478bd9Sstevel@tonic-gate 	 * modified also, since chmod is supposed to apply group
2147c478bd9Sstevel@tonic-gate 	 * permissions changes to both the acl mask and the
2157c478bd9Sstevel@tonic-gate 	 * general group permissions.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	if (group_clear_bits || group_set_bits)
2187c478bd9Sstevel@tonic-gate 		handle_acl(name, group_clear_bits, group_set_bits);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	return (0);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate static int
225fa9e4066Sahrens chmodr(char *dir, char *path,  mode_t mode, mode_t umsk, acl_args_t *aclp)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	DIR *dirp;
2297c478bd9Sstevel@tonic-gate 	struct dirent *dp;
2307c478bd9Sstevel@tonic-gate 	char savedir[PATH_MAX];			/* dir name to restore */
2317c478bd9Sstevel@tonic-gate 	char currdir[PATH_MAX+1];		/* current dir name + '/' */
2327c478bd9Sstevel@tonic-gate 	char parentdir[PATH_MAX+1];		/* parent dir name  + '/' */
2337c478bd9Sstevel@tonic-gate 	int ecode;
234fa9e4066Sahrens 	struct stat st;
2357c478bd9Sstevel@tonic-gate 	o_mode_t	group_clear_bits, group_set_bits;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (getcwd(savedir, PATH_MAX) == 0)
2387c478bd9Sstevel@tonic-gate 		errmsg(2, 255, gettext("chmod: could not getcwd %s\n"),
2397c478bd9Sstevel@tonic-gate 		    savedir);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * Change what we are given before doing it's contents
2437c478bd9Sstevel@tonic-gate 	 */
244fa9e4066Sahrens 	if (aclp) {
245fa9e4066Sahrens 		if (lstat(dir, &st) < 0) {
246fa9e4066Sahrens 			errmsg(2, 0, gettext("can't access %s\n"), path);
247fa9e4066Sahrens 			return (1);
248fa9e4066Sahrens 		}
249fa9e4066Sahrens 		if (doacl(dir, &st, aclp) != 0)
250fa9e4066Sahrens 			return (1);
251fa9e4066Sahrens 	} else if (chmod(dir, newmode_common(ms, mode, umsk, dir, path,
2527c478bd9Sstevel@tonic-gate 	    &group_clear_bits, &group_set_bits)) < 0) {
2537c478bd9Sstevel@tonic-gate 		errmsg(2, 0, gettext("can't change %s\n"), path);
2547c478bd9Sstevel@tonic-gate 		return (1);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/*
2587c478bd9Sstevel@tonic-gate 	 * If the group permissions of the file are being modified,
2597c478bd9Sstevel@tonic-gate 	 * make sure that the file's ACL (if it has one) is
2607c478bd9Sstevel@tonic-gate 	 * modified also, since chmod is supposed to apply group
2617c478bd9Sstevel@tonic-gate 	 * permissions changes to both the acl mask and the
2627c478bd9Sstevel@tonic-gate 	 * general group permissions.
2637c478bd9Sstevel@tonic-gate 	 */
264fa9e4066Sahrens 
265fa9e4066Sahrens 	if (aclp == NULL) { /* only necessary when not setting ACL */
2667c478bd9Sstevel@tonic-gate 		if (group_clear_bits || group_set_bits)
2677c478bd9Sstevel@tonic-gate 			handle_acl(dir, group_clear_bits, group_set_bits);
268fa9e4066Sahrens 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (chdir(dir) < 0) {
2717c478bd9Sstevel@tonic-gate 		errmsg(2, 0, "%s/%s: %s\n", savedir, dir, strerror(errno));
2727c478bd9Sstevel@tonic-gate 		return (1);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(".")) == NULL) {
2757c478bd9Sstevel@tonic-gate 		errmsg(2, 0, "%s\n", strerror(errno));
2767c478bd9Sstevel@tonic-gate 		return (1);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 	dp = readdir(dirp);
2797c478bd9Sstevel@tonic-gate 	dp = readdir(dirp); /* read "." and ".." */
2807c478bd9Sstevel@tonic-gate 	ecode = 0;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/*
2837c478bd9Sstevel@tonic-gate 	 * Save parent directory path before recursive chmod.
2847c478bd9Sstevel@tonic-gate 	 * We'll need this for error printing purposes. Add
2857c478bd9Sstevel@tonic-gate 	 * a trailing '/' to the path except in the case where
2867c478bd9Sstevel@tonic-gate 	 * the path is just '/'
2877c478bd9Sstevel@tonic-gate 	 */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	(void) strcpy(parentdir, path);
2907c478bd9Sstevel@tonic-gate 	if (strcmp(path, "/") != 0)
2917c478bd9Sstevel@tonic-gate 		(void) strcat(parentdir, "/");
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))  {
2947c478bd9Sstevel@tonic-gate 		(void) strcpy(currdir, parentdir);
2957c478bd9Sstevel@tonic-gate 		(void) strcat(currdir, dp->d_name);
296fa9e4066Sahrens 		ecode += dochmod(dp->d_name, currdir, umsk, aclp);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
2997c478bd9Sstevel@tonic-gate 	if (chdir(savedir) < 0) {
3007c478bd9Sstevel@tonic-gate 		errmsg(2, 255, gettext("can't change back to %s\n"), savedir);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	return (ecode ? 1 : 0);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /* PRINTFLIKE3 */
3067c478bd9Sstevel@tonic-gate void
3077c478bd9Sstevel@tonic-gate errmsg(int severity, int code, char *format, ...)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	va_list ap;
3107c478bd9Sstevel@tonic-gate 	static char *msg[] = {
3117c478bd9Sstevel@tonic-gate 	"",
3127c478bd9Sstevel@tonic-gate 	"ERROR",
3137c478bd9Sstevel@tonic-gate 	"WARNING",
3147c478bd9Sstevel@tonic-gate 	""
3157c478bd9Sstevel@tonic-gate 	};
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	va_start(ap, format);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 * Always print error message if this is a fatal error (code == 0);
3217c478bd9Sstevel@tonic-gate 	 * otherwise, print message if fflag == 0 (no -f option specified)
3227c478bd9Sstevel@tonic-gate 	 */
3237c478bd9Sstevel@tonic-gate 	if (!fflag || (code != 0)) {
3247c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3257c478bd9Sstevel@tonic-gate 			"chmod: %s: ", gettext(msg[severity]));
3267c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, format, ap);
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	va_end(ap);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	if (code != 0)
3327c478bd9Sstevel@tonic-gate 		exit(fflag ? 0 : code);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate static void
3367c478bd9Sstevel@tonic-gate usage(void)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
3397c478bd9Sstevel@tonic-gate 	    "usage:\tchmod [-fR] <absolute-mode> file ...\n"));
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
342fa9e4066Sahrens 	    "\tchmod [-fR] <ACL-operation> file ...\n"));
343fa9e4066Sahrens 
344fa9e4066Sahrens 	(void) fprintf(stderr, gettext(
3457c478bd9Sstevel@tonic-gate 	    "\tchmod [-fR] <symbolic-mode-list> file ...\n"));
3467c478bd9Sstevel@tonic-gate 
347fa9e4066Sahrens 
3487c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
3497c478bd9Sstevel@tonic-gate 	    "where \t<symbolic-mode-list> is a comma-separated list of\n"));
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
3527c478bd9Sstevel@tonic-gate 	    "\t[ugoa]{+|-|=}[rwxXlstugo]\n"));
353fa9e4066Sahrens 
354fa9e4066Sahrens 	(void) fprintf(stderr, gettext(
355fa9e4066Sahrens 	    "where \t<ACL-operation> is one of the following\n"));
356fa9e4066Sahrens 	(void) fprintf(stderr, gettext("\tA-<acl_specification>\n"));
357fa9e4066Sahrens 	(void) fprintf(stderr, gettext("\tA[number]-\n"));
358fa9e4066Sahrens 	(void) fprintf(stderr, gettext(
359fa9e4066Sahrens 	    "\tA[number]{+|=}<acl_specification>\n"));
360fa9e4066Sahrens 	(void) fprintf(stderr, gettext(
361fa9e4066Sahrens 	    "where \t<acl-specification> is a comma-separated list of ACEs\n"));
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  *  parseargs - generate getopt-friendly argument list for backwards
3667c478bd9Sstevel@tonic-gate  *		compatibility with earlier Solaris usage (eg, chmod -w
3677c478bd9Sstevel@tonic-gate  *		foo).
3687c478bd9Sstevel@tonic-gate  *
3697c478bd9Sstevel@tonic-gate  *  assumes the existence of a static set of alternates to argc and argv,
3707c478bd9Sstevel@tonic-gate  *  (namely, mac, and mav[]).
3717c478bd9Sstevel@tonic-gate  *
3727c478bd9Sstevel@tonic-gate  */
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate static void
3757c478bd9Sstevel@tonic-gate parseargs(int ac, char *av[])
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	int i;			/* current argument			*/
3787c478bd9Sstevel@tonic-gate 	int fflag;		/* arg list contains "--"		*/
3797c478bd9Sstevel@tonic-gate 	size_t mav_num;		/* number of entries in mav[]		*/
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/*
3827c478bd9Sstevel@tonic-gate 	 * We add an extra argument slot, in case we need to jam a "--"
3837c478bd9Sstevel@tonic-gate 	 * argument into the list.
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	mav_num = (size_t)ac+2;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if ((mav = calloc(mav_num, sizeof (char *))) == NULL) {
3897c478bd9Sstevel@tonic-gate 		perror("chmod");
3907c478bd9Sstevel@tonic-gate 		exit(2);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/* scan for the use of "--" in the argument list */
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	for (fflag = i = 0; i < ac; i ++) {
3967c478bd9Sstevel@tonic-gate 		if (strcmp(av[i], "--") == 0)
3977c478bd9Sstevel@tonic-gate 		    fflag = 1;
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/* process the arguments */
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	for (i = mac = 0;
4037c478bd9Sstevel@tonic-gate 	    (av[i] != (char *)NULL) && (av[i][0] != (char)NULL);
4047c478bd9Sstevel@tonic-gate 	    i++) {
4057c478bd9Sstevel@tonic-gate 		if (!fflag && av[i][0] == '-') {
4067c478bd9Sstevel@tonic-gate 			/*
4077c478bd9Sstevel@tonic-gate 			 *  If there is not already a "--" argument specified,
4087c478bd9Sstevel@tonic-gate 			 *  and the argument starts with '-' but does not
4097c478bd9Sstevel@tonic-gate 			 *  contain any of the official option letters, then it
4107c478bd9Sstevel@tonic-gate 			 *  is probably a mode argument beginning with '-'.
4117c478bd9Sstevel@tonic-gate 			 *  Force a "--" into the argument stream in front of
4127c478bd9Sstevel@tonic-gate 			 *  it.
4137c478bd9Sstevel@tonic-gate 			 */
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 			if ((strchr(av[i], 'R') == NULL &&
4167c478bd9Sstevel@tonic-gate 			    strchr(av[i], 'f') == NULL)) {
4177c478bd9Sstevel@tonic-gate 				mav[mac++] = strdup("--");
4187c478bd9Sstevel@tonic-gate 			}
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		mav[mac++] = strdup(av[i]);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	mav[mac] = (char *)NULL;
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
427fa9e4066Sahrens int
428fa9e4066Sahrens parse_acl_args(char *arg, acl_args_t **acl_args)
429fa9e4066Sahrens {
430fa9e4066Sahrens 	acl_t *new_acl = NULL;
431fa9e4066Sahrens 	int slot;
432fa9e4066Sahrens 	int len;
433fa9e4066Sahrens 	int action;
434fa9e4066Sahrens 	acl_args_t *new_acl_args;
435fa9e4066Sahrens 	char *acl_spec = NULL;
436fa9e4066Sahrens 	char *end;
437fa9e4066Sahrens 
438fa9e4066Sahrens 	if (arg[0] != 'A')
439fa9e4066Sahrens 		return (1);
440fa9e4066Sahrens 
441fa9e4066Sahrens 	slot = strtol(&arg[1], &end, 10);
442fa9e4066Sahrens 
443fa9e4066Sahrens 	len = strlen(arg);
444fa9e4066Sahrens 	switch (*end) {
445fa9e4066Sahrens 	case '+':
446fa9e4066Sahrens 		action = ACL_ADD;
447fa9e4066Sahrens 		acl_spec = ++end;
448fa9e4066Sahrens 		break;
449fa9e4066Sahrens 	case '-':
450fa9e4066Sahrens 		if (len == 2 && arg[0] == 'A' && arg[1] == '-')
451fa9e4066Sahrens 			action = ACL_STRIP;
452fa9e4066Sahrens 		else
453fa9e4066Sahrens 			action = ACL_DELETE;
454fa9e4066Sahrens 		if (action != ACL_STRIP) {
455fa9e4066Sahrens 			acl_spec = ++end;
456fa9e4066Sahrens 			if (acl_spec[0] == '\0') {
457fa9e4066Sahrens 				action = ACL_SLOT_DELETE;
458fa9e4066Sahrens 				acl_spec = NULL;
459fa9e4066Sahrens 			} else if (arg[1] != '-')
460fa9e4066Sahrens 				return (1);
461fa9e4066Sahrens 		}
462fa9e4066Sahrens 		break;
463fa9e4066Sahrens 	case '=':
46455601ddbSmarks 		/*
46555601ddbSmarks 		 * Was slot specified?
46655601ddbSmarks 		 */
46755601ddbSmarks 		if (arg[1] == '=')
46855601ddbSmarks 			slot = -1;
469fa9e4066Sahrens 		action = ACL_REPLACE;
470fa9e4066Sahrens 		acl_spec = ++end;
471fa9e4066Sahrens 		break;
472fa9e4066Sahrens 	default:
473fa9e4066Sahrens 		return (1);
474fa9e4066Sahrens 	}
475fa9e4066Sahrens 
476fa9e4066Sahrens 	if ((action == ACL_REPLACE || action == ACL_ADD) && acl_spec[0] == '\0')
477fa9e4066Sahrens 		return (1);
478fa9e4066Sahrens 
479fa9e4066Sahrens 	if (acl_spec) {
480*5a5eeccaSmarks 		if (acl_parse(acl_spec, &new_acl)) {
481*5a5eeccaSmarks 			exit(1);
482fa9e4066Sahrens 		}
483fa9e4066Sahrens 	}
484fa9e4066Sahrens 
485fa9e4066Sahrens 	new_acl_args = malloc(sizeof (acl_args_t));
486fa9e4066Sahrens 	if (new_acl_args == NULL)
487fa9e4066Sahrens 		return (1);
488fa9e4066Sahrens 
489fa9e4066Sahrens 	new_acl_args->acl_aclp = new_acl;
490fa9e4066Sahrens 	new_acl_args->acl_slot = slot;
491fa9e4066Sahrens 	new_acl_args->acl_action = action;
492fa9e4066Sahrens 
493fa9e4066Sahrens 	*acl_args = new_acl_args;
494fa9e4066Sahrens 
495fa9e4066Sahrens 	return (0);
496fa9e4066Sahrens }
497fa9e4066Sahrens 
4987c478bd9Sstevel@tonic-gate /*
4997c478bd9Sstevel@tonic-gate  * This function is called whenever the group permissions of a file
5007c478bd9Sstevel@tonic-gate  * is being modified.  According to the chmod(1) manpage, any
5017c478bd9Sstevel@tonic-gate  * change made to the group permissions must be applied to both
5027c478bd9Sstevel@tonic-gate  * the acl mask and the acl's GROUP_OBJ.  The chmod(2) already
5037c478bd9Sstevel@tonic-gate  * set the mask, so this routine needs to make the same change
5047c478bd9Sstevel@tonic-gate  * to the GROUP_OBJ.
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate static void
5077c478bd9Sstevel@tonic-gate handle_acl(char *name, o_mode_t group_clear_bits, o_mode_t group_set_bits)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	int aclcnt, n;
5107c478bd9Sstevel@tonic-gate 	aclent_t *aclp, *tp;
5117c478bd9Sstevel@tonic-gate 	o_mode_t newperm;
5127c478bd9Sstevel@tonic-gate 
513fa9e4066Sahrens 	/*
514fa9e4066Sahrens 	 * if this file system support ace_t acl's
515fa9e4066Sahrens 	 * then simply return since we don't have an
516fa9e4066Sahrens 	 * acl mask to deal with
517fa9e4066Sahrens 	 */
518fa9e4066Sahrens 	if (pathconf(name, _PC_ACL_ENABLED) == _ACL_ACE_ENABLED)
519fa9e4066Sahrens 		return;
520fa9e4066Sahrens 
5217c478bd9Sstevel@tonic-gate 	if ((aclcnt = acl(name, GETACLCNT, 0, NULL)) <= MIN_ACL_ENTRIES)
5227c478bd9Sstevel@tonic-gate 		return;	/* it's just a trivial acl; no need to change it */
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	if ((aclp = (aclent_t *)malloc((sizeof (aclent_t)) * aclcnt))
5257c478bd9Sstevel@tonic-gate 	    == NULL) {
5267c478bd9Sstevel@tonic-gate 		perror("chmod");
5277c478bd9Sstevel@tonic-gate 		exit(2);
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (acl(name, GETACL, aclcnt, aclp) < 0) {
5317c478bd9Sstevel@tonic-gate 		free(aclp);
5327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "chmod: ");
5337c478bd9Sstevel@tonic-gate 		perror(name);
5347c478bd9Sstevel@tonic-gate 		return;
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	for (tp = aclp, n = aclcnt; n--; tp++) {
5387c478bd9Sstevel@tonic-gate 		if (tp->a_type == GROUP_OBJ) {
5397c478bd9Sstevel@tonic-gate 			newperm = tp->a_perm;
5407c478bd9Sstevel@tonic-gate 			if (group_clear_bits != 0)
5417c478bd9Sstevel@tonic-gate 				newperm &= ~group_clear_bits;
5427c478bd9Sstevel@tonic-gate 			if (group_set_bits != 0)
5437c478bd9Sstevel@tonic-gate 				newperm |= group_set_bits;
5447c478bd9Sstevel@tonic-gate 			if (newperm != tp->a_perm) {
5457c478bd9Sstevel@tonic-gate 				tp->a_perm = newperm;
5467c478bd9Sstevel@tonic-gate 				if (acl(name, SETACL, aclcnt, aclp)
5477c478bd9Sstevel@tonic-gate 				    < 0) {
5487c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "chmod: ");
5497c478bd9Sstevel@tonic-gate 					perror(name);
5507c478bd9Sstevel@tonic-gate 				}
5517c478bd9Sstevel@tonic-gate 			}
5527c478bd9Sstevel@tonic-gate 			break;
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 	free(aclp);
5567c478bd9Sstevel@tonic-gate }
557fa9e4066Sahrens 
558fa9e4066Sahrens static int
559fa9e4066Sahrens doacl(char *file, struct stat *st, acl_args_t *acl_args)
560fa9e4066Sahrens {
561fa9e4066Sahrens 	acl_t *aclp;
562fa9e4066Sahrens 	acl_t *set_aclp;
563fa9e4066Sahrens 	int error = 0;
564fa9e4066Sahrens 	void *to, *from;
565fa9e4066Sahrens 	int len;
566fa9e4066Sahrens 	int isdir;
567fa9e4066Sahrens 
568fa9e4066Sahrens 	isdir = S_ISDIR(st->st_mode);
569fa9e4066Sahrens 
570fa9e4066Sahrens 	error = acl_get(file, 0, &aclp);
571fa9e4066Sahrens 
572fa9e4066Sahrens 	if (error != 0) {
573fa9e4066Sahrens 		errmsg(1, 1, "%s\n", acl_strerror(error));
574fa9e4066Sahrens 		return (1);
575fa9e4066Sahrens 	}
576fa9e4066Sahrens 
577fa9e4066Sahrens 	switch (acl_args->acl_action) {
578fa9e4066Sahrens 	case ACL_ADD:
579fa9e4066Sahrens 		if ((error = acl_addentries(aclp,
580fa9e4066Sahrens 			acl_args->acl_aclp, acl_args->acl_slot)) != 0) {
581fa9e4066Sahrens 				errmsg(1, 1, "%s\n", acl_strerror(error));
582fa9e4066Sahrens 				acl_free(aclp);
583fa9e4066Sahrens 				return (1);
584fa9e4066Sahrens 		}
585fa9e4066Sahrens 		set_aclp = aclp;
586fa9e4066Sahrens 		break;
587fa9e4066Sahrens 	case ACL_SLOT_DELETE:
588fa9e4066Sahrens 
589fa9e4066Sahrens 		if (acl_args->acl_slot + 1 > aclp->acl_cnt) {
590fa9e4066Sahrens 			errmsg(1, 1,
591fa9e4066Sahrens 			    gettext("Invalid slot specified for removal\n"));
592fa9e4066Sahrens 			acl_free(aclp);
593fa9e4066Sahrens 			return (1);
594fa9e4066Sahrens 		}
595fa9e4066Sahrens 
596fa9e4066Sahrens 		if (acl_args->acl_slot == 0 && aclp->acl_cnt == 1) {
597fa9e4066Sahrens 			errmsg(1, 1,
598fa9e4066Sahrens 			    gettext("Can't remove all ACL "
599fa9e4066Sahrens 			    "entries from a file\n"));
600fa9e4066Sahrens 			acl_free(aclp);
601fa9e4066Sahrens 			return (1);
602fa9e4066Sahrens 		}
603fa9e4066Sahrens 
604fa9e4066Sahrens 		/*
605fa9e4066Sahrens 		 * remove a single entry
606fa9e4066Sahrens 		 *
607fa9e4066Sahrens 		 * if last entry just adjust acl_cnt
608fa9e4066Sahrens 		 */
609fa9e4066Sahrens 
610fa9e4066Sahrens 		if ((acl_args->acl_slot + 1) == aclp->acl_cnt)
611fa9e4066Sahrens 			aclp->acl_cnt--;
612fa9e4066Sahrens 		else {
613fa9e4066Sahrens 			to = (char *)aclp->acl_aclp +
614fa9e4066Sahrens 			    (acl_args->acl_slot * aclp->acl_entry_size);
615fa9e4066Sahrens 			from = (char *)to + aclp->acl_entry_size;
616fa9e4066Sahrens 			len = (aclp->acl_cnt - acl_args->acl_slot - 1) *
617fa9e4066Sahrens 			    aclp->acl_entry_size;
618fa9e4066Sahrens 			(void) memmove(to, from, len);
619fa9e4066Sahrens 			aclp->acl_cnt--;
620fa9e4066Sahrens 		}
621fa9e4066Sahrens 		set_aclp = aclp;
622fa9e4066Sahrens 		break;
623fa9e4066Sahrens 
624fa9e4066Sahrens 	case ACL_DELETE:
625fa9e4066Sahrens 		if ((error = acl_removeentries(aclp, acl_args->acl_aclp,
626fa9e4066Sahrens 		    acl_args->acl_slot, ACL_REMOVE_ALL)) != 0) {
627fa9e4066Sahrens 			errmsg(1, 1, "%s\n", acl_strerror(error));
628fa9e4066Sahrens 			acl_free(aclp);
629fa9e4066Sahrens 			return (1);
630fa9e4066Sahrens 		}
631fa9e4066Sahrens 
632fa9e4066Sahrens 		if (aclp->acl_cnt == 0) {
633fa9e4066Sahrens 			errmsg(1, 1,
634fa9e4066Sahrens 			    gettext("Can't remove all ACL "
635fa9e4066Sahrens 			    "entries from a file\n"));
636fa9e4066Sahrens 			acl_free(aclp);
637fa9e4066Sahrens 			return (1);
638fa9e4066Sahrens 		}
639fa9e4066Sahrens 
640fa9e4066Sahrens 		set_aclp = aclp;
641fa9e4066Sahrens 		break;
642fa9e4066Sahrens 	case ACL_REPLACE:
643fa9e4066Sahrens 		if (acl_args->acl_slot >= 0)  {
644fa9e4066Sahrens 			error = acl_modifyentries(aclp, acl_args->acl_aclp,
645fa9e4066Sahrens 			    acl_args->acl_slot);
646fa9e4066Sahrens 			if (error) {
647fa9e4066Sahrens 				errmsg(1, 1, "%s\n", acl_strerror(error));
648fa9e4066Sahrens 				acl_free(aclp);
649fa9e4066Sahrens 				return (1);
650fa9e4066Sahrens 			}
651fa9e4066Sahrens 			set_aclp = aclp;
652fa9e4066Sahrens 		} else {
653fa9e4066Sahrens 			set_aclp = acl_args->acl_aclp;
654fa9e4066Sahrens 		}
655fa9e4066Sahrens 		break;
656fa9e4066Sahrens 	case ACL_STRIP:
657fa9e4066Sahrens 		error = acl_strip(file, st->st_uid, st->st_gid, st->st_mode);
658fa9e4066Sahrens 		if (error) {
659fa9e4066Sahrens 			errmsg(1, 1, "%s\n", acl_strerror(error));
660fa9e4066Sahrens 			return (1);
661fa9e4066Sahrens 		}
662fa9e4066Sahrens 		acl_free(aclp);
663fa9e4066Sahrens 		return (0);
664fa9e4066Sahrens 		/*NOTREACHED*/
665fa9e4066Sahrens 	default:
666fa9e4066Sahrens 		errmsg(1, 0, gettext("Unknown ACL action requested\n"));
667fa9e4066Sahrens 		return (1);
668fa9e4066Sahrens 		break;
669fa9e4066Sahrens 	}
670fa9e4066Sahrens 
671fa9e4066Sahrens 	error = acl_check(set_aclp, isdir);
672fa9e4066Sahrens 
673fa9e4066Sahrens 	if (error) {
674fa9e4066Sahrens 		errmsg(1, 0, "%s\n%s", acl_strerror(error),
675fa9e4066Sahrens 		    gettext("See chmod(1) for more information on "
676fa9e4066Sahrens 		    "valid ACL syntax\n"));
677fa9e4066Sahrens 		return (1);
678fa9e4066Sahrens 	}
679fa9e4066Sahrens 	if ((error = acl_set(file, set_aclp)) != 0) {
680fa9e4066Sahrens 			errmsg(1, 0, gettext("Failed to set ACL: %s\n"),
681fa9e4066Sahrens 			    acl_strerror(error));
682fa9e4066Sahrens 			acl_free(aclp);
683fa9e4066Sahrens 			return (1);
684fa9e4066Sahrens 	}
685fa9e4066Sahrens 	acl_free(aclp);
686fa9e4066Sahrens 	return (0);
687fa9e4066Sahrens }
688