xref: /titanic_52/usr/src/cmd/chmod/chmod.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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*fa9e4066Sahrens  * Copyright 2005 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
47*fa9e4066Sahrens  *	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>
67*fa9e4066Sahrens #include <ctype.h>
687c478bd9Sstevel@tonic-gate #include <errno.h>
697c478bd9Sstevel@tonic-gate #include <sys/acl.h>
70*fa9e4066Sahrens #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 
83*fa9e4066Sahrens #define	ACL_ADD		1
84*fa9e4066Sahrens #define	ACL_DELETE	2
85*fa9e4066Sahrens #define	ACL_SLOT_DELETE 3
86*fa9e4066Sahrens #define	ACL_REPLACE	4
87*fa9e4066Sahrens #define	ACL_STRIP	5
88*fa9e4066Sahrens 
89*fa9e4066Sahrens typedef struct acl_args {
90*fa9e4066Sahrens 	acl_t	*acl_aclp;
91*fa9e4066Sahrens 	int	acl_slot;
92*fa9e4066Sahrens 	int	acl_action;
93*fa9e4066Sahrens } acl_args_t;
94*fa9e4066Sahrens 
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
100*fa9e4066Sahrens dochmod(char *name, char *path, mode_t umsk, acl_args_t *aclp),
101*fa9e4066Sahrens chmodr(char *dir, char *path, mode_t mode, mode_t umsk, acl_args_t *aclp);
102*fa9e4066Sahrens 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 
107*fa9e4066Sahrens static void usage(void);
1087c478bd9Sstevel@tonic-gate 
109*fa9e4066Sahrens void errmsg(int severity, int code, char *format, ...);
1107c478bd9Sstevel@tonic-gate 
111*fa9e4066Sahrens static void parseargs(int ac, char *av[]);
112*fa9e4066Sahrens 
113*fa9e4066Sahrens int
114*fa9e4066Sahrens 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;
122*fa9e4066Sahrens 	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 
154*fa9e4066Sahrens 	if (mac >= 2 && (mav[0][0] == 'A')) {
155*fa9e4066Sahrens 		if (parse_acl_args(*mav, &acl_args)) {
156*fa9e4066Sahrens 			usage();
157*fa9e4066Sahrens 			exit(2);
158*fa9e4066Sahrens 		}
159*fa9e4066Sahrens 	} else {
1607c478bd9Sstevel@tonic-gate 		if (mac < 2) {
1617c478bd9Sstevel@tonic-gate 			usage();
1627c478bd9Sstevel@tonic-gate 			exit(2);
1637c478bd9Sstevel@tonic-gate 		}
164*fa9e4066Sahrens 	}
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 
171*fa9e4066Sahrens 	for (i = 1; i < mac; i++) {
172*fa9e4066Sahrens 		status += dochmod(mav[i], mav[i], umsk, acl_args);
173*fa9e4066Sahrens 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	return (fflag ? 0 : status);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static int
179*fa9e4066Sahrens 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)
200*fa9e4066Sahrens 		return (chmodr(name, path, st.st_mode, umsk, aclp));
2017c478bd9Sstevel@tonic-gate 
202*fa9e4066Sahrens 	if (aclp) {
203*fa9e4066Sahrens 		return (doacl(name, &st, aclp));
204*fa9e4066Sahrens 	} 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
225*fa9e4066Sahrens 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;
234*fa9e4066Sahrens 	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 	 */
244*fa9e4066Sahrens 	if (aclp) {
245*fa9e4066Sahrens 		if (lstat(dir, &st) < 0) {
246*fa9e4066Sahrens 			errmsg(2, 0, gettext("can't access %s\n"), path);
247*fa9e4066Sahrens 			return (1);
248*fa9e4066Sahrens 		}
249*fa9e4066Sahrens 		if (doacl(dir, &st, aclp) != 0)
250*fa9e4066Sahrens 			return (1);
251*fa9e4066Sahrens 	} 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 	 */
264*fa9e4066Sahrens 
265*fa9e4066Sahrens 	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);
268*fa9e4066Sahrens 	}
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);
296*fa9e4066Sahrens 		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(
342*fa9e4066Sahrens 	    "\tchmod [-fR] <ACL-operation> file ...\n"));
343*fa9e4066Sahrens 
344*fa9e4066Sahrens 	(void) fprintf(stderr, gettext(
3457c478bd9Sstevel@tonic-gate 	    "\tchmod [-fR] <symbolic-mode-list> file ...\n"));
3467c478bd9Sstevel@tonic-gate 
347*fa9e4066Sahrens 
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"));
353*fa9e4066Sahrens 
354*fa9e4066Sahrens 	(void) fprintf(stderr, gettext(
355*fa9e4066Sahrens 	    "where \t<ACL-operation> is one of the following\n"));
356*fa9e4066Sahrens 	(void) fprintf(stderr, gettext("\tA-<acl_specification>\n"));
357*fa9e4066Sahrens 	(void) fprintf(stderr, gettext("\tA[number]-\n"));
358*fa9e4066Sahrens 	(void) fprintf(stderr, gettext(
359*fa9e4066Sahrens 	    "\tA[number]{+|=}<acl_specification>\n"));
360*fa9e4066Sahrens 	(void) fprintf(stderr, gettext(
361*fa9e4066Sahrens 	    "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 
427*fa9e4066Sahrens int
428*fa9e4066Sahrens parse_acl_args(char *arg, acl_args_t **acl_args)
429*fa9e4066Sahrens {
430*fa9e4066Sahrens 	acl_t *new_acl = NULL;
431*fa9e4066Sahrens 	int slot;
432*fa9e4066Sahrens 	int error;
433*fa9e4066Sahrens 	int len;
434*fa9e4066Sahrens 	int action;
435*fa9e4066Sahrens 	acl_args_t *new_acl_args;
436*fa9e4066Sahrens 	char *acl_spec = NULL;
437*fa9e4066Sahrens 	char *end;
438*fa9e4066Sahrens 
439*fa9e4066Sahrens 	if (arg[0] != 'A')
440*fa9e4066Sahrens 		return (1);
441*fa9e4066Sahrens 
442*fa9e4066Sahrens 	slot = strtol(&arg[1], &end, 10);
443*fa9e4066Sahrens 
444*fa9e4066Sahrens 	len = strlen(arg);
445*fa9e4066Sahrens 	switch (*end) {
446*fa9e4066Sahrens 	case '+':
447*fa9e4066Sahrens 		action = ACL_ADD;
448*fa9e4066Sahrens 		acl_spec = ++end;
449*fa9e4066Sahrens 		break;
450*fa9e4066Sahrens 	case '-':
451*fa9e4066Sahrens 		if (len == 2 && arg[0] == 'A' && arg[1] == '-')
452*fa9e4066Sahrens 			action = ACL_STRIP;
453*fa9e4066Sahrens 		else
454*fa9e4066Sahrens 			action = ACL_DELETE;
455*fa9e4066Sahrens 		if (action != ACL_STRIP) {
456*fa9e4066Sahrens 			acl_spec = ++end;
457*fa9e4066Sahrens 			if (acl_spec[0] == '\0') {
458*fa9e4066Sahrens 				action = ACL_SLOT_DELETE;
459*fa9e4066Sahrens 				acl_spec = NULL;
460*fa9e4066Sahrens 			} else if (arg[1] != '-')
461*fa9e4066Sahrens 				return (1);
462*fa9e4066Sahrens 		}
463*fa9e4066Sahrens 		break;
464*fa9e4066Sahrens 	case '=':
465*fa9e4066Sahrens 		action = ACL_REPLACE;
466*fa9e4066Sahrens 		acl_spec = ++end;
467*fa9e4066Sahrens 		break;
468*fa9e4066Sahrens 	default:
469*fa9e4066Sahrens 		return (1);
470*fa9e4066Sahrens 	}
471*fa9e4066Sahrens 
472*fa9e4066Sahrens 	if ((action == ACL_REPLACE || action == ACL_ADD) && acl_spec[0] == '\0')
473*fa9e4066Sahrens 		return (1);
474*fa9e4066Sahrens 
475*fa9e4066Sahrens 	if (acl_spec) {
476*fa9e4066Sahrens 		if (error = acl_fromtext(acl_spec, &new_acl)) {
477*fa9e4066Sahrens 			errmsg(1, 1, "%s\n", acl_strerror(error));
478*fa9e4066Sahrens 			return (1);
479*fa9e4066Sahrens 		}
480*fa9e4066Sahrens 	}
481*fa9e4066Sahrens 
482*fa9e4066Sahrens 	new_acl_args = malloc(sizeof (acl_args_t));
483*fa9e4066Sahrens 	if (new_acl_args == NULL)
484*fa9e4066Sahrens 		return (1);
485*fa9e4066Sahrens 
486*fa9e4066Sahrens 	new_acl_args->acl_aclp = new_acl;
487*fa9e4066Sahrens 	new_acl_args->acl_slot = slot;
488*fa9e4066Sahrens 	new_acl_args->acl_action = action;
489*fa9e4066Sahrens 
490*fa9e4066Sahrens 	*acl_args = new_acl_args;
491*fa9e4066Sahrens 
492*fa9e4066Sahrens 	return (0);
493*fa9e4066Sahrens }
494*fa9e4066Sahrens 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * This function is called whenever the group permissions of a file
4977c478bd9Sstevel@tonic-gate  * is being modified.  According to the chmod(1) manpage, any
4987c478bd9Sstevel@tonic-gate  * change made to the group permissions must be applied to both
4997c478bd9Sstevel@tonic-gate  * the acl mask and the acl's GROUP_OBJ.  The chmod(2) already
5007c478bd9Sstevel@tonic-gate  * set the mask, so this routine needs to make the same change
5017c478bd9Sstevel@tonic-gate  * to the GROUP_OBJ.
5027c478bd9Sstevel@tonic-gate  */
5037c478bd9Sstevel@tonic-gate static void
5047c478bd9Sstevel@tonic-gate handle_acl(char *name, o_mode_t group_clear_bits, o_mode_t group_set_bits)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	int aclcnt, n;
5077c478bd9Sstevel@tonic-gate 	aclent_t *aclp, *tp;
5087c478bd9Sstevel@tonic-gate 	o_mode_t newperm;
5097c478bd9Sstevel@tonic-gate 
510*fa9e4066Sahrens 	/*
511*fa9e4066Sahrens 	 * if this file system support ace_t acl's
512*fa9e4066Sahrens 	 * then simply return since we don't have an
513*fa9e4066Sahrens 	 * acl mask to deal with
514*fa9e4066Sahrens 	 */
515*fa9e4066Sahrens 	if (pathconf(name, _PC_ACL_ENABLED) == _ACL_ACE_ENABLED)
516*fa9e4066Sahrens 		return;
517*fa9e4066Sahrens 
5187c478bd9Sstevel@tonic-gate 	if ((aclcnt = acl(name, GETACLCNT, 0, NULL)) <= MIN_ACL_ENTRIES)
5197c478bd9Sstevel@tonic-gate 		return;	/* it's just a trivial acl; no need to change it */
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if ((aclp = (aclent_t *)malloc((sizeof (aclent_t)) * aclcnt))
5227c478bd9Sstevel@tonic-gate 	    == NULL) {
5237c478bd9Sstevel@tonic-gate 		perror("chmod");
5247c478bd9Sstevel@tonic-gate 		exit(2);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if (acl(name, GETACL, aclcnt, aclp) < 0) {
5287c478bd9Sstevel@tonic-gate 		free(aclp);
5297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "chmod: ");
5307c478bd9Sstevel@tonic-gate 		perror(name);
5317c478bd9Sstevel@tonic-gate 		return;
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	for (tp = aclp, n = aclcnt; n--; tp++) {
5357c478bd9Sstevel@tonic-gate 		if (tp->a_type == GROUP_OBJ) {
5367c478bd9Sstevel@tonic-gate 			newperm = tp->a_perm;
5377c478bd9Sstevel@tonic-gate 			if (group_clear_bits != 0)
5387c478bd9Sstevel@tonic-gate 				newperm &= ~group_clear_bits;
5397c478bd9Sstevel@tonic-gate 			if (group_set_bits != 0)
5407c478bd9Sstevel@tonic-gate 				newperm |= group_set_bits;
5417c478bd9Sstevel@tonic-gate 			if (newperm != tp->a_perm) {
5427c478bd9Sstevel@tonic-gate 				tp->a_perm = newperm;
5437c478bd9Sstevel@tonic-gate 				if (acl(name, SETACL, aclcnt, aclp)
5447c478bd9Sstevel@tonic-gate 				    < 0) {
5457c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "chmod: ");
5467c478bd9Sstevel@tonic-gate 					perror(name);
5477c478bd9Sstevel@tonic-gate 				}
5487c478bd9Sstevel@tonic-gate 			}
5497c478bd9Sstevel@tonic-gate 			break;
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	free(aclp);
5537c478bd9Sstevel@tonic-gate }
554*fa9e4066Sahrens 
555*fa9e4066Sahrens static int
556*fa9e4066Sahrens doacl(char *file, struct stat *st, acl_args_t *acl_args)
557*fa9e4066Sahrens {
558*fa9e4066Sahrens 	acl_t *aclp;
559*fa9e4066Sahrens 	acl_t *set_aclp;
560*fa9e4066Sahrens 	int error = 0;
561*fa9e4066Sahrens 	void *to, *from;
562*fa9e4066Sahrens 	int len;
563*fa9e4066Sahrens 	int isdir;
564*fa9e4066Sahrens 
565*fa9e4066Sahrens 	isdir = S_ISDIR(st->st_mode);
566*fa9e4066Sahrens 
567*fa9e4066Sahrens 	error = acl_get(file, 0, &aclp);
568*fa9e4066Sahrens 
569*fa9e4066Sahrens 	if (error != 0) {
570*fa9e4066Sahrens 		errmsg(1, 1, "%s\n", acl_strerror(error));
571*fa9e4066Sahrens 		return (1);
572*fa9e4066Sahrens 	}
573*fa9e4066Sahrens 
574*fa9e4066Sahrens 	switch (acl_args->acl_action) {
575*fa9e4066Sahrens 	case ACL_ADD:
576*fa9e4066Sahrens 		if ((error = acl_addentries(aclp,
577*fa9e4066Sahrens 			acl_args->acl_aclp, acl_args->acl_slot)) != 0) {
578*fa9e4066Sahrens 				errmsg(1, 1, "%s\n", acl_strerror(error));
579*fa9e4066Sahrens 				acl_free(aclp);
580*fa9e4066Sahrens 				return (1);
581*fa9e4066Sahrens 		}
582*fa9e4066Sahrens 		set_aclp = aclp;
583*fa9e4066Sahrens 		break;
584*fa9e4066Sahrens 	case ACL_SLOT_DELETE:
585*fa9e4066Sahrens 
586*fa9e4066Sahrens 		if (acl_args->acl_slot + 1 > aclp->acl_cnt) {
587*fa9e4066Sahrens 			errmsg(1, 1,
588*fa9e4066Sahrens 			    gettext("Invalid slot specified for removal\n"));
589*fa9e4066Sahrens 			acl_free(aclp);
590*fa9e4066Sahrens 			return (1);
591*fa9e4066Sahrens 		}
592*fa9e4066Sahrens 
593*fa9e4066Sahrens 		if (acl_args->acl_slot == 0 && aclp->acl_cnt == 1) {
594*fa9e4066Sahrens 			errmsg(1, 1,
595*fa9e4066Sahrens 			    gettext("Can't remove all ACL "
596*fa9e4066Sahrens 			    "entries from a file\n"));
597*fa9e4066Sahrens 			acl_free(aclp);
598*fa9e4066Sahrens 			return (1);
599*fa9e4066Sahrens 		}
600*fa9e4066Sahrens 
601*fa9e4066Sahrens 		/*
602*fa9e4066Sahrens 		 * remove a single entry
603*fa9e4066Sahrens 		 *
604*fa9e4066Sahrens 		 * if last entry just adjust acl_cnt
605*fa9e4066Sahrens 		 */
606*fa9e4066Sahrens 
607*fa9e4066Sahrens 		if ((acl_args->acl_slot + 1) == aclp->acl_cnt)
608*fa9e4066Sahrens 			aclp->acl_cnt--;
609*fa9e4066Sahrens 		else {
610*fa9e4066Sahrens 			to = (char *)aclp->acl_aclp +
611*fa9e4066Sahrens 			    (acl_args->acl_slot * aclp->acl_entry_size);
612*fa9e4066Sahrens 			from = (char *)to + aclp->acl_entry_size;
613*fa9e4066Sahrens 			len = (aclp->acl_cnt - acl_args->acl_slot - 1) *
614*fa9e4066Sahrens 			    aclp->acl_entry_size;
615*fa9e4066Sahrens 			(void) memmove(to, from, len);
616*fa9e4066Sahrens 			aclp->acl_cnt--;
617*fa9e4066Sahrens 		}
618*fa9e4066Sahrens 		set_aclp = aclp;
619*fa9e4066Sahrens 		break;
620*fa9e4066Sahrens 
621*fa9e4066Sahrens 	case ACL_DELETE:
622*fa9e4066Sahrens 		if ((error = acl_removeentries(aclp, acl_args->acl_aclp,
623*fa9e4066Sahrens 		    acl_args->acl_slot, ACL_REMOVE_ALL)) != 0) {
624*fa9e4066Sahrens 			errmsg(1, 1, "%s\n", acl_strerror(error));
625*fa9e4066Sahrens 			acl_free(aclp);
626*fa9e4066Sahrens 			return (1);
627*fa9e4066Sahrens 		}
628*fa9e4066Sahrens 
629*fa9e4066Sahrens 		if (aclp->acl_cnt == 0) {
630*fa9e4066Sahrens 			errmsg(1, 1,
631*fa9e4066Sahrens 			    gettext("Can't remove all ACL "
632*fa9e4066Sahrens 			    "entries from a file\n"));
633*fa9e4066Sahrens 			acl_free(aclp);
634*fa9e4066Sahrens 			return (1);
635*fa9e4066Sahrens 		}
636*fa9e4066Sahrens 
637*fa9e4066Sahrens 		set_aclp = aclp;
638*fa9e4066Sahrens 		break;
639*fa9e4066Sahrens 	case ACL_REPLACE:
640*fa9e4066Sahrens 		if (acl_args->acl_slot >= 0)  {
641*fa9e4066Sahrens 			error = acl_modifyentries(aclp, acl_args->acl_aclp,
642*fa9e4066Sahrens 			    acl_args->acl_slot);
643*fa9e4066Sahrens 			if (error) {
644*fa9e4066Sahrens 				errmsg(1, 1, "%s\n", acl_strerror(error));
645*fa9e4066Sahrens 				acl_free(aclp);
646*fa9e4066Sahrens 				return (1);
647*fa9e4066Sahrens 			}
648*fa9e4066Sahrens 			set_aclp = aclp;
649*fa9e4066Sahrens 		} else {
650*fa9e4066Sahrens 			set_aclp = acl_args->acl_aclp;
651*fa9e4066Sahrens 		}
652*fa9e4066Sahrens 		break;
653*fa9e4066Sahrens 	case ACL_STRIP:
654*fa9e4066Sahrens 		error = acl_strip(file, st->st_uid, st->st_gid, st->st_mode);
655*fa9e4066Sahrens 		if (error) {
656*fa9e4066Sahrens 			errmsg(1, 1, "%s\n", acl_strerror(error));
657*fa9e4066Sahrens 			return (1);
658*fa9e4066Sahrens 		}
659*fa9e4066Sahrens 		acl_free(aclp);
660*fa9e4066Sahrens 		return (0);
661*fa9e4066Sahrens 		/*NOTREACHED*/
662*fa9e4066Sahrens 	default:
663*fa9e4066Sahrens 		errmsg(1, 0, gettext("Unknown ACL action requested\n"));
664*fa9e4066Sahrens 		return (1);
665*fa9e4066Sahrens 		break;
666*fa9e4066Sahrens 	}
667*fa9e4066Sahrens 
668*fa9e4066Sahrens 	error = acl_check(set_aclp, isdir);
669*fa9e4066Sahrens 
670*fa9e4066Sahrens 	if (error) {
671*fa9e4066Sahrens 		errmsg(1, 0, "%s\n%s", acl_strerror(error),
672*fa9e4066Sahrens 		    gettext("See chmod(1) for more information on "
673*fa9e4066Sahrens 		    "valid ACL syntax\n"));
674*fa9e4066Sahrens 		return (1);
675*fa9e4066Sahrens 	}
676*fa9e4066Sahrens 	if ((error = acl_set(file, set_aclp)) != 0) {
677*fa9e4066Sahrens 			errmsg(1, 0, gettext("Failed to set ACL: %s\n"),
678*fa9e4066Sahrens 			    acl_strerror(error));
679*fa9e4066Sahrens 			acl_free(aclp);
680*fa9e4066Sahrens 			return (1);
681*fa9e4066Sahrens 	}
682*fa9e4066Sahrens 	acl_free(aclp);
683*fa9e4066Sahrens 	return (0);
684*fa9e4066Sahrens }
685