xref: /titanic_52/usr/src/cmd/setfacl/setfacl.c (revision 4f28cd70acfd52fd329e3d479f5bdecdbe1d3d9e)
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
594d2b9abSmarks  * Common Development and Distribution License (the "License").
694d2b9abSmarks  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21bdcaf822Sbasabi /*
22*4f28cd70S"Nagaraj Yedathore - Sun Microsystems - Bangalore India"  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens /*
277c478bd9Sstevel@tonic-gate  * setfacl [-r] -f aclfile file ...
287c478bd9Sstevel@tonic-gate  * setfacl [-r] -d acl_entries file ...
297c478bd9Sstevel@tonic-gate  * setfacl [-r] -m acl_entries file ...
307c478bd9Sstevel@tonic-gate  * setfacl [-r] -s acl_entries file ...
317c478bd9Sstevel@tonic-gate  * This command deletes/adds/modifies/sets discretionary information for a file
327c478bd9Sstevel@tonic-gate  * or files.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <pwd.h>
387c478bd9Sstevel@tonic-gate #include <grp.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <locale.h>
417c478bd9Sstevel@tonic-gate #include <sys/acl.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
44fa9e4066Sahrens #include <errno.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	ADD	1
477c478bd9Sstevel@tonic-gate #define	MODIFY	2
487c478bd9Sstevel@tonic-gate #define	DELETE	3
497c478bd9Sstevel@tonic-gate #define	SET	4
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static int get_acl_info(char *filep, aclent_t **aclpp);
527c478bd9Sstevel@tonic-gate static int mod_entries(aclent_t *, int, char *, char *, char *, int);
537c478bd9Sstevel@tonic-gate static int set_file_entries(char *, char *, int);
547c478bd9Sstevel@tonic-gate static int set_online_entries(char *, char *, int);
557c478bd9Sstevel@tonic-gate static void usage();
567c478bd9Sstevel@tonic-gate static int parse_entry_list(aclent_t **, int *, char *, int);
577c478bd9Sstevel@tonic-gate static int convert_to_aclent_t(char *, int *, aclent_t **, int);
587c478bd9Sstevel@tonic-gate static int parse_entry(char *, aclent_t *, int);
597c478bd9Sstevel@tonic-gate static void err_handle(int, aclent_t *);
607c478bd9Sstevel@tonic-gate static int conv_id(char *);
617c478bd9Sstevel@tonic-gate 
62bdcaf822Sbasabi int
637c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	int		c;
667c478bd9Sstevel@tonic-gate 	int		dflag = 0;
677c478bd9Sstevel@tonic-gate 	int		mflag = 0;
687c478bd9Sstevel@tonic-gate 	int		rflag = 0;
697c478bd9Sstevel@tonic-gate 	int		sflag = 0;
707c478bd9Sstevel@tonic-gate 	int		fflag = 0;
717c478bd9Sstevel@tonic-gate 	int		errflag = 0;
727c478bd9Sstevel@tonic-gate 	int		aclcnt;			/* used by -m -d */
737c478bd9Sstevel@tonic-gate 	aclent_t	*aclp;			/* used by -m -d */
747c478bd9Sstevel@tonic-gate 	char		*aclfilep;		/* acl file argument */
757c478bd9Sstevel@tonic-gate 	char		*d_entryp = NULL;	/* ptr to del entry list */
767c478bd9Sstevel@tonic-gate 	char		*m_entryp = NULL;	/* ptr to mod entry list */
777c478bd9Sstevel@tonic-gate 	char		*s_entryp = NULL;	/* ptr to set entry list */
787c478bd9Sstevel@tonic-gate 	char		*work_dp = NULL;	/* working ptrs for the above */
797c478bd9Sstevel@tonic-gate 	char		*work_mp = NULL;
807c478bd9Sstevel@tonic-gate 	char		*work_sp = NULL;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
837c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (argc < 3)
867c478bd9Sstevel@tonic-gate 		usage();
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "rm:d:s:f:")) != EOF) {
897c478bd9Sstevel@tonic-gate 		switch (c) {
907c478bd9Sstevel@tonic-gate 		case 'r':
917c478bd9Sstevel@tonic-gate 			rflag++;
927c478bd9Sstevel@tonic-gate 			break;
937c478bd9Sstevel@tonic-gate 		case 'd':
947c478bd9Sstevel@tonic-gate 			if (dflag || fflag || sflag)
957c478bd9Sstevel@tonic-gate 				usage();
967c478bd9Sstevel@tonic-gate 			dflag++;
977c478bd9Sstevel@tonic-gate 			d_entryp = optarg;
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		case 'm':
1007c478bd9Sstevel@tonic-gate 			if (mflag || fflag || sflag)
1017c478bd9Sstevel@tonic-gate 				usage();
1027c478bd9Sstevel@tonic-gate 			mflag++;
1037c478bd9Sstevel@tonic-gate 			m_entryp = optarg;
1047c478bd9Sstevel@tonic-gate 			break;
1057c478bd9Sstevel@tonic-gate 		case 's':
1067c478bd9Sstevel@tonic-gate 			if (fflag || sflag || mflag || dflag)
1077c478bd9Sstevel@tonic-gate 				usage();
1087c478bd9Sstevel@tonic-gate 			sflag++;
1097c478bd9Sstevel@tonic-gate 			s_entryp = optarg;
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		case 'f':
1127c478bd9Sstevel@tonic-gate 			if (fflag || sflag || mflag || dflag)
1137c478bd9Sstevel@tonic-gate 				usage();
1147c478bd9Sstevel@tonic-gate 			fflag++;
1157c478bd9Sstevel@tonic-gate 			aclfilep = optarg;
1167c478bd9Sstevel@tonic-gate 			break;
1177c478bd9Sstevel@tonic-gate 		case '?':
1187c478bd9Sstevel@tonic-gate 			errflag++;
1197c478bd9Sstevel@tonic-gate 			break;
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	if (errflag)
1237c478bd9Sstevel@tonic-gate 		usage();
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	/* one of these flags should be set */
1267c478bd9Sstevel@tonic-gate 	if (!fflag && !sflag && !mflag && !dflag)
1277c478bd9Sstevel@tonic-gate 		usage();
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/* no file arguments */
1307c478bd9Sstevel@tonic-gate 	if (optind >= argc)
1317c478bd9Sstevel@tonic-gate 		usage();
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	for (; optind < argc; optind++) {
1347c478bd9Sstevel@tonic-gate 		register char *filep;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		filep = argv[optind];
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		/* modify and delete: we need to get the ACL first */
1397c478bd9Sstevel@tonic-gate 		if (mflag || dflag) {
1407c478bd9Sstevel@tonic-gate 			if (m_entryp != NULL) {
1417c478bd9Sstevel@tonic-gate 				free(work_mp);
1427c478bd9Sstevel@tonic-gate 				work_mp = strdup(m_entryp);
1437c478bd9Sstevel@tonic-gate 				if (work_mp == NULL) {
1447c478bd9Sstevel@tonic-gate 					fprintf(stderr,
1457c478bd9Sstevel@tonic-gate 					    gettext("out of memory %s\n"),
1467c478bd9Sstevel@tonic-gate 					    m_entryp);
1477c478bd9Sstevel@tonic-gate 					exit(1);
1487c478bd9Sstevel@tonic-gate 				}
1497c478bd9Sstevel@tonic-gate 			}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 			if (d_entryp != NULL) {
1527c478bd9Sstevel@tonic-gate 				free(work_dp);
1537c478bd9Sstevel@tonic-gate 				work_dp = strdup(d_entryp);
1547c478bd9Sstevel@tonic-gate 				if (work_dp == NULL) {
1557c478bd9Sstevel@tonic-gate 					fprintf(stderr,
1567c478bd9Sstevel@tonic-gate 					    gettext("out of memory %s\n"),
1577c478bd9Sstevel@tonic-gate 					    d_entryp);
1587c478bd9Sstevel@tonic-gate 					exit(1);
1597c478bd9Sstevel@tonic-gate 				}
1607c478bd9Sstevel@tonic-gate 			}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 			aclcnt = get_acl_info(filep, &aclp);
1637c478bd9Sstevel@tonic-gate 			if (aclcnt == -1)
1647c478bd9Sstevel@tonic-gate 				exit(2);
1657c478bd9Sstevel@tonic-gate 			if (mod_entries(aclp, aclcnt, work_mp,
1667c478bd9Sstevel@tonic-gate 			    work_dp, filep, rflag) == -1)
1677c478bd9Sstevel@tonic-gate 				exit(2);
1687c478bd9Sstevel@tonic-gate 		} else if (fflag) {
1697c478bd9Sstevel@tonic-gate 			if (set_file_entries(aclfilep, filep, rflag) == -1)
1707c478bd9Sstevel@tonic-gate 				exit(2);
1717c478bd9Sstevel@tonic-gate 		} else if (sflag) {
1727c478bd9Sstevel@tonic-gate 			if (s_entryp != NULL) {
1737c478bd9Sstevel@tonic-gate 				free(work_sp);
1747c478bd9Sstevel@tonic-gate 				work_sp = strdup(s_entryp);
1757c478bd9Sstevel@tonic-gate 				if (work_sp == NULL) {
1767c478bd9Sstevel@tonic-gate 					fprintf(stderr,
1777c478bd9Sstevel@tonic-gate 					    gettext("out of memory %s\n"),
1787c478bd9Sstevel@tonic-gate 					    s_entryp);
1797c478bd9Sstevel@tonic-gate 					exit(1);
1807c478bd9Sstevel@tonic-gate 				}
1817c478bd9Sstevel@tonic-gate 			}
1827c478bd9Sstevel@tonic-gate 			if (set_online_entries(work_sp, filep, rflag) == -1)
1837c478bd9Sstevel@tonic-gate 				exit(2);
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 	}
186bdcaf822Sbasabi 	return (0);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * For add, modify, and delete, we need to get the ACL of the file first.
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate static int
1937c478bd9Sstevel@tonic-gate get_acl_info(char *filep, aclent_t **aclpp)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	int	aclcnt;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if ((aclcnt = acl(filep, GETACLCNT, 0, NULL)) < 0) {
198fa9e4066Sahrens 		if (errno == ENOSYS) {
199fa9e4066Sahrens 			(void) fprintf(stderr,
20094d2b9abSmarks 			    gettext("File system doesn't support aclent_t "
201fa9e4066Sahrens 			    "style ACL's.\n"
202fa9e4066Sahrens 			    "See acl(5) for more information on"
203fa9e4066Sahrens 			    " ACL styles support by Solaris.\n"));
204fa9e4066Sahrens 			return (-1);
205fa9e4066Sahrens 		}
2067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2077c478bd9Sstevel@tonic-gate 		    gettext("%s: failed to get acl count\n"), filep);
2087c478bd9Sstevel@tonic-gate 		perror("get acl count error");
2097c478bd9Sstevel@tonic-gate 		return (-1);
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	if (aclcnt < MIN_ACL_ENTRIES) {
2127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2137c478bd9Sstevel@tonic-gate 		    gettext("%d: acl count is too small from %s\n"),
2147c478bd9Sstevel@tonic-gate 		    aclcnt, filep);
2157c478bd9Sstevel@tonic-gate 		return (-1);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if ((*aclpp = (aclent_t *)malloc(sizeof (aclent_t) * aclcnt)) == NULL) {
2197c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("out of memory\n"));
2207c478bd9Sstevel@tonic-gate 		return (-1);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 	if (acl(filep, GETACL, aclcnt, *aclpp) < 0) {
2237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2247c478bd9Sstevel@tonic-gate 		    gettext("%s: failed to get acl entries\n"), filep);
2257c478bd9Sstevel@tonic-gate 		perror("getacl error");
2267c478bd9Sstevel@tonic-gate 		return (-1);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	return (aclcnt);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * mod_entries() handles add, delete, and modify ACL entries of a file.
2337c478bd9Sstevel@tonic-gate  * The real action is in convert_to_aclent_t() called by parse_entry_list().
2347c478bd9Sstevel@tonic-gate  * aclp: points ACL of a file and may be changed by lower level routine.
2357c478bd9Sstevel@tonic-gate  * modp: modify entry list in ascii format
2367c478bd9Sstevel@tonic-gate  * delp: delete entry list in ascii format
2377c478bd9Sstevel@tonic-gate  * fnamep: file of interest
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate static int
2407c478bd9Sstevel@tonic-gate mod_entries(aclent_t *aclp, int cnt, char *modp, char *delp,
2417c478bd9Sstevel@tonic-gate 	char *fnamep, int rfg)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	int	rc;		/* return code */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/* modify and add: from -m option */
2467c478bd9Sstevel@tonic-gate 	if (parse_entry_list(&aclp, &cnt, modp, MODIFY) == -1)
2477c478bd9Sstevel@tonic-gate 		return (-1);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/* deletion: from -d option */
2507c478bd9Sstevel@tonic-gate 	if (parse_entry_list(&aclp, &cnt, delp, DELETE) == -1)
2517c478bd9Sstevel@tonic-gate 		return (-1);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (aclsort(cnt, rfg, aclp) == -1) {
2547c478bd9Sstevel@tonic-gate 		(void) err_handle(cnt, aclp);
2557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2567c478bd9Sstevel@tonic-gate 		    gettext("aclcnt %d, file %s\n"), cnt, fnamep);
2577c478bd9Sstevel@tonic-gate 		return (-1);
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	if (acl(fnamep, SETACL, cnt, aclp) < 0) {
2617c478bd9Sstevel@tonic-gate 		fprintf(stderr,
2627c478bd9Sstevel@tonic-gate 		    gettext("%s: failed to set acl entries\n"), fnamep);
2637c478bd9Sstevel@tonic-gate 		perror("setacl error");
2647c478bd9Sstevel@tonic-gate 		return (-1);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	return (0);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  * set_file_entries() creates ACL entries from ACL file (acl_fnamep).
2717c478bd9Sstevel@tonic-gate  * It opens the file and converts every line (one line per acl entry)
2727c478bd9Sstevel@tonic-gate  * into aclent_t format. It then recalculates the mask according to rflag.
2737c478bd9Sstevel@tonic-gate  * Finally it sets ACL to the file (fnamep).
2747c478bd9Sstevel@tonic-gate  */
2757c478bd9Sstevel@tonic-gate static int
2767c478bd9Sstevel@tonic-gate set_file_entries(char *acl_fnamep, char *fnamep, int rflag)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	int		aclcnt = 0;
2797c478bd9Sstevel@tonic-gate 	FILE		*acl_fp;
2807c478bd9Sstevel@tonic-gate 	aclent_t	*aclp;
2817c478bd9Sstevel@tonic-gate 	char		buf[BUFSIZ];
2827c478bd9Sstevel@tonic-gate 	char		*tp;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if (strcmp(acl_fnamep, "-") == 0)
2857c478bd9Sstevel@tonic-gate 		acl_fp = stdin;
2867c478bd9Sstevel@tonic-gate 	else {
2877c478bd9Sstevel@tonic-gate 		if ((acl_fp = fopen(acl_fnamep, "r")) == NULL) {
2887c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Can't open acl file %s\n"),
2897c478bd9Sstevel@tonic-gate 			    acl_fnamep);
2907c478bd9Sstevel@tonic-gate 			return (-1);
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	while (fgets(buf, BUFSIZ, acl_fp) != NULL) {
2947c478bd9Sstevel@tonic-gate 		if (buf[0] == '#' || buf[0] == '\n')
2957c478bd9Sstevel@tonic-gate 			continue;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		/* check effective permission: add a null after real perm */
2987c478bd9Sstevel@tonic-gate 		if ((tp = (char *)strchr(buf, '#')) != NULL) {
2997c478bd9Sstevel@tonic-gate 			tp--;
3007c478bd9Sstevel@tonic-gate 			while (*tp == ' ' || *tp == '\t') {
3017c478bd9Sstevel@tonic-gate 				if (tp != buf)
3027c478bd9Sstevel@tonic-gate 					tp--;
3037c478bd9Sstevel@tonic-gate 				else {
3047c478bd9Sstevel@tonic-gate 					fprintf(stderr,
3057c478bd9Sstevel@tonic-gate 					    gettext("entry format error %s\n"),
3067c478bd9Sstevel@tonic-gate 					    buf);
3077c478bd9Sstevel@tonic-gate 					exit(1);
3087c478bd9Sstevel@tonic-gate 				}
3097c478bd9Sstevel@tonic-gate 			}
3107c478bd9Sstevel@tonic-gate 			*(tp+1) = '\0';
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 		/* remove <nl> at the end if there is one */
3147c478bd9Sstevel@tonic-gate 		if ((tp = (char *)strchr(buf, '\n')) != NULL)
3157c478bd9Sstevel@tonic-gate 			*tp = '\0';
3167c478bd9Sstevel@tonic-gate 		aclcnt++;
3177c478bd9Sstevel@tonic-gate 		if (convert_to_aclent_t(buf, &aclcnt, &aclp, SET) == -1)
3187c478bd9Sstevel@tonic-gate 			return (-1);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	if (aclsort(aclcnt, rflag, aclp) == -1) {
3227c478bd9Sstevel@tonic-gate 		(void) err_handle(aclcnt, aclp);
3237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("aclcnt %d, aclfile %s\n"),
3247c478bd9Sstevel@tonic-gate 		    aclcnt, acl_fnamep);
3257c478bd9Sstevel@tonic-gate 		return (-1);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (acl(fnamep, SETACL, aclcnt, aclp) < 0) {
3297c478bd9Sstevel@tonic-gate 		fprintf(stderr,
3307c478bd9Sstevel@tonic-gate 		    gettext("%s: failed to set acl entries\n"), fnamep);
3317c478bd9Sstevel@tonic-gate 		perror("setacl error");
3327c478bd9Sstevel@tonic-gate 		return (-1);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	return (0);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate  * set_online_entries() parses the acl entries from command line (setp).
3397c478bd9Sstevel@tonic-gate  * It converts the comma separated acl entries into aclent_t format.
3407c478bd9Sstevel@tonic-gate  * It then recalculates the mask according to rflag.
3417c478bd9Sstevel@tonic-gate  * Finally it sets ACL to the file (fnamep).
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate static int
3447c478bd9Sstevel@tonic-gate set_online_entries(char *setp, char *fnamep, int rflag)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	char		*commap;
3477c478bd9Sstevel@tonic-gate 	aclent_t	*aclp;
3487c478bd9Sstevel@tonic-gate 	int		aclcnt = 0;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if (parse_entry_list(&aclp, &aclcnt, setp, SET) == -1)
3517c478bd9Sstevel@tonic-gate 		return (-1);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (aclsort(aclcnt, rflag, aclp) == -1) {
3547c478bd9Sstevel@tonic-gate 		(void) err_handle(aclcnt, aclp);
3557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3567c478bd9Sstevel@tonic-gate 		    gettext("aclcnt %d, file %s\n"), aclcnt, fnamep);
3577c478bd9Sstevel@tonic-gate 		return (-1);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (acl(fnamep, SETACL, aclcnt, aclp) < 0) {
3617c478bd9Sstevel@tonic-gate 		fprintf(stderr,
3627c478bd9Sstevel@tonic-gate 		    gettext("%s: failed to set acl entries\n"), fnamep);
3637c478bd9Sstevel@tonic-gate 		perror("setacl error");
3647c478bd9Sstevel@tonic-gate 		return (-1);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	return (0);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * parse_entry_list() parses entry list (listp) separated by commas.
3717c478bd9Sstevel@tonic-gate  * Once it gets an ACL entry, it calls convert_to_aclent_t() to convert
3727c478bd9Sstevel@tonic-gate  * to internal format.
3737c478bd9Sstevel@tonic-gate  */
3747c478bd9Sstevel@tonic-gate static int
3757c478bd9Sstevel@tonic-gate parse_entry_list(aclent_t **aclpp, int *aclcntp, char *listp, int mode)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	char	*commap;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (listp == NULL)
3807c478bd9Sstevel@tonic-gate 		return (0);
3817c478bd9Sstevel@tonic-gate 	while ((commap = (char *)strchr(listp, ',')) != NULL) {
3827c478bd9Sstevel@tonic-gate 		*commap = '\0';
3837c478bd9Sstevel@tonic-gate 		*aclcntp += 1;
3847c478bd9Sstevel@tonic-gate 		/* aclcnt may be updated after the call: add or modify */
3857c478bd9Sstevel@tonic-gate 		if (convert_to_aclent_t(listp, aclcntp, aclpp, mode) == -1)
3867c478bd9Sstevel@tonic-gate 			return (-1);
3877c478bd9Sstevel@tonic-gate 		listp = ++commap;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	/* this is for only one entry or last entry */
3907c478bd9Sstevel@tonic-gate 	if (*listp != '\0') {
3917c478bd9Sstevel@tonic-gate 		*aclcntp += 1;
3927c478bd9Sstevel@tonic-gate 		if (convert_to_aclent_t(listp, aclcntp, aclpp, mode) == -1)
3937c478bd9Sstevel@tonic-gate 			return (-1);
3947c478bd9Sstevel@tonic-gate 	}
395bdcaf822Sbasabi 	return (0);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate /*
3997c478bd9Sstevel@tonic-gate  * convert_to_aclent_t() converts an acl entry in ascii format (fields separated
4007c478bd9Sstevel@tonic-gate  * by colon) into aclent_t and appends it to the current ACL. It also handles
4017c478bd9Sstevel@tonic-gate  * memory allocation/deallocation for acl entries in aclent_t format.
4027c478bd9Sstevel@tonic-gate  * aclpp that contains acl entries in acl format will be returned.
4037c478bd9Sstevel@tonic-gate  * We don't check duplicates.
4047c478bd9Sstevel@tonic-gate  */
4057c478bd9Sstevel@tonic-gate static int
4067c478bd9Sstevel@tonic-gate convert_to_aclent_t(char *entryp, int *cntp, aclent_t **aclpp, int mode)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	aclent_t	*new_aclp;
4097c478bd9Sstevel@tonic-gate 	aclent_t	tmpacl;
410da21eeb3Sny155746 	aclent_t	*taclp, *centry, *gentry;
4117c478bd9Sstevel@tonic-gate 	int		cur_cnt;
4127c478bd9Sstevel@tonic-gate 	int		found = 0;
4137c478bd9Sstevel@tonic-gate 	int		is_obj;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (entryp == NULL)
4167c478bd9Sstevel@tonic-gate 		return (0);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (*cntp > 1)
4197c478bd9Sstevel@tonic-gate 		new_aclp = (aclent_t *)realloc(*aclpp,
4207c478bd9Sstevel@tonic-gate 		    sizeof (aclent_t) * (*cntp));
4217c478bd9Sstevel@tonic-gate 	else
4227c478bd9Sstevel@tonic-gate 		new_aclp = (aclent_t *) malloc(sizeof (aclent_t) * (*cntp));
4237c478bd9Sstevel@tonic-gate 	if (new_aclp == NULL) {
4247c478bd9Sstevel@tonic-gate 		fprintf(stderr,
4257c478bd9Sstevel@tonic-gate 		    gettext("Insufficient memory for acl %d\n"), *cntp);
4267c478bd9Sstevel@tonic-gate 		return (-1);
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	tmpacl.a_id = 0;	/* id field needs to be initialized */
4307c478bd9Sstevel@tonic-gate 	if (entryp[0] == 'u')
4317c478bd9Sstevel@tonic-gate 		tmpacl.a_id = getuid();	/* id field for user */
4327c478bd9Sstevel@tonic-gate 	if (entryp[0] == 'g')
4337c478bd9Sstevel@tonic-gate 		tmpacl.a_id = getgid();	/* id field for group */
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	tmpacl.a_type = 0;
4367c478bd9Sstevel@tonic-gate 	if (parse_entry(entryp, &tmpacl, mode) == -1)
4377c478bd9Sstevel@tonic-gate 		return (-1);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	is_obj = ((tmpacl.a_type == USER_OBJ) ||
4407c478bd9Sstevel@tonic-gate 	    (tmpacl.a_type == GROUP_OBJ) ||
441*4f28cd70S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	    (tmpacl.a_type == CLASS_OBJ) ||
4427c478bd9Sstevel@tonic-gate 	    (tmpacl.a_type == DEF_USER_OBJ) ||
443*4f28cd70S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	    (tmpacl.a_type == DEF_GROUP_OBJ) ||
444*4f28cd70S"Nagaraj Yedathore - Sun Microsystems - Bangalore India" 	    (tmpacl.a_type == DEF_OTHER_OBJ));
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	cur_cnt = *cntp - 1;
4477c478bd9Sstevel@tonic-gate 	switch (mode) {
4487c478bd9Sstevel@tonic-gate 	case MODIFY:	/* and add */
4497c478bd9Sstevel@tonic-gate 		for (taclp = new_aclp; cur_cnt-- > 0; taclp++) {
4507c478bd9Sstevel@tonic-gate 			if (taclp->a_type == tmpacl.a_type &&
4517c478bd9Sstevel@tonic-gate 			    ((taclp->a_id == tmpacl.a_id) || is_obj)) {
4527c478bd9Sstevel@tonic-gate 				found++;
4537c478bd9Sstevel@tonic-gate 				/* cnt is added before it's called */
4547c478bd9Sstevel@tonic-gate 				*cntp -= 1;
4557c478bd9Sstevel@tonic-gate 				taclp->a_perm = tmpacl.a_perm;
4567c478bd9Sstevel@tonic-gate 				break;
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 		if (!found)	/* Add it to the end: no need to change cntp */
4607c478bd9Sstevel@tonic-gate 			memcpy(new_aclp + *cntp -1, &tmpacl, sizeof (aclent_t));
4617c478bd9Sstevel@tonic-gate 		break;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	case DELETE:
4647c478bd9Sstevel@tonic-gate 		for (taclp = new_aclp; cur_cnt-- > 0; taclp++) {
4657c478bd9Sstevel@tonic-gate 			if (taclp->a_type == tmpacl.a_type &&
4667c478bd9Sstevel@tonic-gate 			    ((taclp->a_id == tmpacl.a_id) || is_obj)) {
4677c478bd9Sstevel@tonic-gate 				found++;
4687c478bd9Sstevel@tonic-gate 				/* move up the rest */
4697c478bd9Sstevel@tonic-gate 				while (cur_cnt-- > 0) {
4707c478bd9Sstevel@tonic-gate 					memcpy(taclp, taclp+1,
4717c478bd9Sstevel@tonic-gate 					    sizeof (aclent_t));
4727c478bd9Sstevel@tonic-gate 					taclp++;
4737c478bd9Sstevel@tonic-gate 				}
4747c478bd9Sstevel@tonic-gate 				*cntp = *cntp - 2;
4757c478bd9Sstevel@tonic-gate 				break;
4767c478bd9Sstevel@tonic-gate 			}
4777c478bd9Sstevel@tonic-gate 		}
4787c478bd9Sstevel@tonic-gate 		if (!found)
4797c478bd9Sstevel@tonic-gate 			*cntp -= 1;
4807c478bd9Sstevel@tonic-gate 		break;
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	case SET:
4837c478bd9Sstevel@tonic-gate 		/* we may check duplicate before copying over?? */
4847c478bd9Sstevel@tonic-gate 		memcpy(new_aclp + *cntp -1, &tmpacl, sizeof (aclent_t));
4857c478bd9Sstevel@tonic-gate 		break;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	default:
4887c478bd9Sstevel@tonic-gate 		fprintf(stderr,
4897c478bd9Sstevel@tonic-gate 		    gettext("Unrecognized mode: internal error\n"));
4907c478bd9Sstevel@tonic-gate 		break;
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
493da21eeb3Sny155746 	/*
494da21eeb3Sny155746 	 * If converting from non-trivial acl entry to trivial one,
495da21eeb3Sny155746 	 * reset CLASS_OBJ's permission with that of GROUP_OBJ.
496da21eeb3Sny155746 	 */
497da21eeb3Sny155746 
498da21eeb3Sny155746 	if (mode == DELETE) {
499da21eeb3Sny155746 		boolean_t	trivial = B_TRUE;	/* assumption */
500da21eeb3Sny155746 		cur_cnt = *cntp;
501da21eeb3Sny155746 		for (taclp = new_aclp; cur_cnt-- > 0; taclp++) {
502da21eeb3Sny155746 			switch (taclp->a_type) {
503da21eeb3Sny155746 				case USER_OBJ:
504da21eeb3Sny155746 				case OTHER_OBJ:
505da21eeb3Sny155746 					break;
506da21eeb3Sny155746 				case CLASS_OBJ:
507da21eeb3Sny155746 					centry = taclp;
508da21eeb3Sny155746 					break;
509da21eeb3Sny155746 				case GROUP_OBJ:
510da21eeb3Sny155746 					gentry = taclp;
511da21eeb3Sny155746 					break;
512da21eeb3Sny155746 				default:
513da21eeb3Sny155746 					/*
514da21eeb3Sny155746 					 * Confirmed that the new acl set is
515da21eeb3Sny155746 					 * still a non-trivial acl.
516da21eeb3Sny155746 					 * Skip reset.
517da21eeb3Sny155746 					 */
518da21eeb3Sny155746 					trivial = B_FALSE;
519da21eeb3Sny155746 			}
520da21eeb3Sny155746 		}
521da21eeb3Sny155746 		if (centry != NULL && gentry != NULL && trivial == B_TRUE)
522da21eeb3Sny155746 			centry->a_perm = gentry->a_perm;
523da21eeb3Sny155746 	}
5247c478bd9Sstevel@tonic-gate 	*aclpp = new_aclp; 	/* return new acl entries */
5257c478bd9Sstevel@tonic-gate 	return (0);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate static void
5297c478bd9Sstevel@tonic-gate usage()
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("usage:\n"));
5327c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
5337c478bd9Sstevel@tonic-gate 	    gettext("\tsetfacl [-r] -f aclfile file ...\n"));
5347c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
5357c478bd9Sstevel@tonic-gate 	    gettext("\tsetfacl [-r] -d acl_entries file ...\n"));
5367c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
5377c478bd9Sstevel@tonic-gate 	    gettext("\tsetfacl [-r] -m acl_entries file ...\n"));
5387c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
5397c478bd9Sstevel@tonic-gate 	    gettext("\tsetfacl [-r] -s acl_entries file ...\n"));
5407c478bd9Sstevel@tonic-gate 	exit(1);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate static void
5447c478bd9Sstevel@tonic-gate err_handle(int cnt, aclent_t *aclentp)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	int	rc;
5477c478bd9Sstevel@tonic-gate 	int	which;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	rc = aclcheck(aclentp, cnt, &which);
5507c478bd9Sstevel@tonic-gate 	switch (rc) {
5517c478bd9Sstevel@tonic-gate 	case USER_ERROR:
5527c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5537c478bd9Sstevel@tonic-gate 		    gettext("There is more than one user owner entry"));
5547c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5557c478bd9Sstevel@tonic-gate 		    gettext(" -- error found at entry index %d\n"), which);
5567c478bd9Sstevel@tonic-gate 		break;
5577c478bd9Sstevel@tonic-gate 	case GRP_ERROR:
5587c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5597c478bd9Sstevel@tonic-gate 		    gettext("There is more than one group owner entry"));
5607c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5617c478bd9Sstevel@tonic-gate 		    gettext(" -- error found at entry index %d\n"), which);
5627c478bd9Sstevel@tonic-gate 		break;
5637c478bd9Sstevel@tonic-gate 	case CLASS_ERROR:
5647c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5657c478bd9Sstevel@tonic-gate 		    gettext("There is more than one mask entry"));
5667c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5677c478bd9Sstevel@tonic-gate 		    gettext(" -- error found at entry index %d\n"), which);
5687c478bd9Sstevel@tonic-gate 		break;
5697c478bd9Sstevel@tonic-gate 	case OTHER_ERROR:
5707c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5717c478bd9Sstevel@tonic-gate 		    gettext("There is more than one other entry"));
5727c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5737c478bd9Sstevel@tonic-gate 		    gettext(" -- error found at entry index %d\n"), which);
5747c478bd9Sstevel@tonic-gate 		break;
5757c478bd9Sstevel@tonic-gate 	case DUPLICATE_ERROR:
5767c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5777c478bd9Sstevel@tonic-gate 		    gettext("Duplicate user or group entries"));
5787c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5797c478bd9Sstevel@tonic-gate 		    gettext(" -- error found at entry index %d\n"), which);
5807c478bd9Sstevel@tonic-gate 		break;
5817c478bd9Sstevel@tonic-gate 	case MISS_ERROR:
5827c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5837c478bd9Sstevel@tonic-gate 		    gettext("Missing user/group owner, other, mask entry\n"));
5847c478bd9Sstevel@tonic-gate 		break;
5857c478bd9Sstevel@tonic-gate 	case MEM_ERROR:
5867c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5877c478bd9Sstevel@tonic-gate 		    gettext("Insufficient memory\n"));
5887c478bd9Sstevel@tonic-gate 		break;
5897c478bd9Sstevel@tonic-gate 	case ENTRY_ERROR:
5907c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5917c478bd9Sstevel@tonic-gate 		    gettext("Unrecognized entry type"));
5927c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5937c478bd9Sstevel@tonic-gate 		    gettext(" -- error found at entry index %d\n"), which);
5947c478bd9Sstevel@tonic-gate 		break;
5957c478bd9Sstevel@tonic-gate 	default:
5967c478bd9Sstevel@tonic-gate 		/* error is not from aclcheck */
5977c478bd9Sstevel@tonic-gate 		fprintf(stderr,
5987c478bd9Sstevel@tonic-gate 		    gettext("aclsort error\n"));
5997c478bd9Sstevel@tonic-gate 		break;
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate static int
6047c478bd9Sstevel@tonic-gate parse_entry(char *fieldp, aclent_t *aclentp, int mode)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	char		*colonp;
6077c478bd9Sstevel@tonic-gate 	int		def_flag = 0, mo_flag = 0;
6087c478bd9Sstevel@tonic-gate 	int		id;
6097c478bd9Sstevel@tonic-gate 	struct passwd	*pwp;
6107c478bd9Sstevel@tonic-gate 	struct group	*grp;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	colonp = (char *)strchr(fieldp, ':');
6137c478bd9Sstevel@tonic-gate 	if (colonp == NULL) {
6147c478bd9Sstevel@tonic-gate 		fprintf(stderr,
6157c478bd9Sstevel@tonic-gate 		    gettext("Can't find colon delimiter %s\n"), fieldp);
6167c478bd9Sstevel@tonic-gate 		return (-1);
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 	*colonp = '\0';
6197c478bd9Sstevel@tonic-gate 	if ((strcmp(fieldp, "default") == 0) || (strcmp(fieldp, "d") == 0)) {
6207c478bd9Sstevel@tonic-gate 		def_flag++;
6217c478bd9Sstevel@tonic-gate 		fieldp = ++colonp;
6227c478bd9Sstevel@tonic-gate 		colonp = (char *)strchr(fieldp, ':');
6237c478bd9Sstevel@tonic-gate 		if (colonp == NULL) {
6247c478bd9Sstevel@tonic-gate 			fprintf(stderr,
6257c478bd9Sstevel@tonic-gate 			    gettext("Can't find colon delimiter %s\n"), fieldp);
6267c478bd9Sstevel@tonic-gate 			return (-1);
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 		*colonp = '\0';
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/* process entry type */
6327c478bd9Sstevel@tonic-gate 	if ((strcmp(fieldp, "user") == 0) || (strcmp(fieldp, "u") == 0)) {
6337c478bd9Sstevel@tonic-gate 		if (def_flag)
6347c478bd9Sstevel@tonic-gate 			aclentp->a_type = DEF_USER;
6357c478bd9Sstevel@tonic-gate 		else
6367c478bd9Sstevel@tonic-gate 			aclentp->a_type = USER;
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	if ((strcmp(fieldp, "group") == 0) || (strcmp(fieldp, "g") == 0)) {
6397c478bd9Sstevel@tonic-gate 		if (def_flag)
6407c478bd9Sstevel@tonic-gate 			aclentp->a_type = DEF_GROUP;
6417c478bd9Sstevel@tonic-gate 		else
6427c478bd9Sstevel@tonic-gate 			aclentp->a_type = GROUP;
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate 	if ((strcmp(fieldp, "mask") == 0) || (strcmp(fieldp, "m") == 0)) {
6457c478bd9Sstevel@tonic-gate 		if (def_flag)
6467c478bd9Sstevel@tonic-gate 			aclentp->a_type = DEF_CLASS_OBJ;
6477c478bd9Sstevel@tonic-gate 		else
6487c478bd9Sstevel@tonic-gate 			aclentp->a_type = CLASS_OBJ;
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 	if ((strcmp(fieldp, "other") == 0) || (strcmp(fieldp, "o") == 0)) {
6517c478bd9Sstevel@tonic-gate 		if (def_flag)
6527c478bd9Sstevel@tonic-gate 			aclentp->a_type = DEF_OTHER_OBJ;
6537c478bd9Sstevel@tonic-gate 		else
6547c478bd9Sstevel@tonic-gate 			aclentp->a_type = OTHER_OBJ;
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/* still can't determine entry type */
6587c478bd9Sstevel@tonic-gate 	if (aclentp->a_type == 0) {
6597c478bd9Sstevel@tonic-gate 		fprintf(stderr,
6607c478bd9Sstevel@tonic-gate 		    gettext("Unrecognized entry type %s \n"), fieldp);
6617c478bd9Sstevel@tonic-gate 		return (-1);
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/* mask and other entries dont have id field */
6657c478bd9Sstevel@tonic-gate 	if (aclentp->a_type != CLASS_OBJ && aclentp->a_type != OTHER_OBJ &&
6667c478bd9Sstevel@tonic-gate 	    aclentp->a_type != DEF_CLASS_OBJ &&
6677c478bd9Sstevel@tonic-gate 	    aclentp->a_type != DEF_OTHER_OBJ) {
6687c478bd9Sstevel@tonic-gate 		/* process id: */
6697c478bd9Sstevel@tonic-gate 		fieldp = ++colonp;
6707c478bd9Sstevel@tonic-gate 		colonp = (char *)strchr(fieldp, ':');
6717c478bd9Sstevel@tonic-gate 		if (colonp == NULL) {
6727c478bd9Sstevel@tonic-gate 			if (mode != DELETE) {
6737c478bd9Sstevel@tonic-gate 				fprintf(stderr,
6747c478bd9Sstevel@tonic-gate 				    gettext("Can't find colon delimiter %s\n"),
6757c478bd9Sstevel@tonic-gate 				    fieldp);
6767c478bd9Sstevel@tonic-gate 				return (-1);
6777c478bd9Sstevel@tonic-gate 			}
6787c478bd9Sstevel@tonic-gate 		} else
6797c478bd9Sstevel@tonic-gate 			*colonp = '\0';
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 		if (*fieldp == '\0') {
6827c478bd9Sstevel@tonic-gate 			/* empty uid */
6837c478bd9Sstevel@tonic-gate 			if (aclentp->a_type == USER)
6847c478bd9Sstevel@tonic-gate 				aclentp->a_type = USER_OBJ;
6857c478bd9Sstevel@tonic-gate 			if (aclentp->a_type == DEF_USER)
6867c478bd9Sstevel@tonic-gate 				aclentp->a_type = DEF_USER_OBJ;
6877c478bd9Sstevel@tonic-gate 			if (aclentp->a_type == GROUP)
6887c478bd9Sstevel@tonic-gate 				aclentp->a_type = GROUP_OBJ;
6897c478bd9Sstevel@tonic-gate 			if (aclentp->a_type == DEF_GROUP)
6907c478bd9Sstevel@tonic-gate 				aclentp->a_type = DEF_GROUP_OBJ;
6917c478bd9Sstevel@tonic-gate 		} else {
6927c478bd9Sstevel@tonic-gate 			/* see if it's a user/group name */
6937c478bd9Sstevel@tonic-gate 			if (aclentp->a_type == USER ||
6947c478bd9Sstevel@tonic-gate 			    aclentp->a_type == USER_OBJ ||
6957c478bd9Sstevel@tonic-gate 			    aclentp->a_type == DEF_USER ||
6967c478bd9Sstevel@tonic-gate 			    aclentp->a_type == DEF_USER_OBJ) {
6977c478bd9Sstevel@tonic-gate 				if ((pwp = getpwnam(fieldp)) != NULL)
6987c478bd9Sstevel@tonic-gate 					aclentp->a_id = pwp->pw_uid;
6997c478bd9Sstevel@tonic-gate 				else {
7007c478bd9Sstevel@tonic-gate 					/* treat it as numeric id */
7017c478bd9Sstevel@tonic-gate 					id = conv_id(fieldp);
7027c478bd9Sstevel@tonic-gate 					if (id == -1)
7037c478bd9Sstevel@tonic-gate 						return (-1);
7047c478bd9Sstevel@tonic-gate 					aclentp->a_id = id;
7057c478bd9Sstevel@tonic-gate 				}
7067c478bd9Sstevel@tonic-gate 			} else {
7077c478bd9Sstevel@tonic-gate 				/* group name */
7087c478bd9Sstevel@tonic-gate 				if ((grp = getgrnam(fieldp)) != NULL)
7097c478bd9Sstevel@tonic-gate 					aclentp->a_id = grp->gr_gid;
7107c478bd9Sstevel@tonic-gate 				else {
7117c478bd9Sstevel@tonic-gate 					id = conv_id(fieldp);
7127c478bd9Sstevel@tonic-gate 					if (id == -1)
7137c478bd9Sstevel@tonic-gate 						return (-1);
7147c478bd9Sstevel@tonic-gate 					aclentp->a_id = id;
7157c478bd9Sstevel@tonic-gate 				}
7167c478bd9Sstevel@tonic-gate 			}
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 	} else {
7197c478bd9Sstevel@tonic-gate 		/* it is mask/other entry */
7207c478bd9Sstevel@tonic-gate 		mo_flag = 1;
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/* process permission: rwx and [0]n  format */
7247c478bd9Sstevel@tonic-gate 	if (mode == DELETE)
7257c478bd9Sstevel@tonic-gate 		/* delete format: no permission field */
7267c478bd9Sstevel@tonic-gate 		return (0);
7277c478bd9Sstevel@tonic-gate 	fieldp = ++colonp;
7287c478bd9Sstevel@tonic-gate 	colonp = (char *)strchr(fieldp, ':');
7297c478bd9Sstevel@tonic-gate 	if (colonp != NULL) {
7307c478bd9Sstevel@tonic-gate 		if (mo_flag == 1) {
7317c478bd9Sstevel@tonic-gate 			/* Use only single : on mask/other entry */
7327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("use only 1 colon for "
7337c478bd9Sstevel@tonic-gate 			    "mask and other entries.\n"));
7347c478bd9Sstevel@tonic-gate 			return (-1);
7357c478bd9Sstevel@tonic-gate 		} else {
7367c478bd9Sstevel@tonic-gate 			/* it's ok to have extra colon */
7377c478bd9Sstevel@tonic-gate 			*colonp = '\0';
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if ((int)strlen(fieldp) > 3) {
7427c478bd9Sstevel@tonic-gate 		fprintf(stderr,
7437c478bd9Sstevel@tonic-gate 		    gettext("only rwx or [0]n format is allowed\n"));
7447c478bd9Sstevel@tonic-gate 		return (-1);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 	if (strlen(fieldp) == 3) {
7477c478bd9Sstevel@tonic-gate 		aclentp->a_perm = 0;
7487c478bd9Sstevel@tonic-gate 		/* treat it as rwx */
7497c478bd9Sstevel@tonic-gate 		if (*fieldp == 'r')
7507c478bd9Sstevel@tonic-gate 			aclentp->a_perm += 4;
7517c478bd9Sstevel@tonic-gate 		else
7527c478bd9Sstevel@tonic-gate 			if (*fieldp != '-') {
7537c478bd9Sstevel@tonic-gate 				fprintf(stderr,
7547c478bd9Sstevel@tonic-gate 				    gettext("Unrecognized character "));
7557c478bd9Sstevel@tonic-gate 				fprintf(stderr,
7567c478bd9Sstevel@tonic-gate 				    gettext("found in mode field\n"));
7577c478bd9Sstevel@tonic-gate 				return (-1);
7587c478bd9Sstevel@tonic-gate 			}
7597c478bd9Sstevel@tonic-gate 		fieldp++;
7607c478bd9Sstevel@tonic-gate 		if (*fieldp == 'w')
7617c478bd9Sstevel@tonic-gate 			aclentp->a_perm += 2;
7627c478bd9Sstevel@tonic-gate 		else
7637c478bd9Sstevel@tonic-gate 			if (*fieldp != '-') {
7647c478bd9Sstevel@tonic-gate 				fprintf(stderr,
7657c478bd9Sstevel@tonic-gate 				    gettext("Unrecognized character "));
7667c478bd9Sstevel@tonic-gate 				fprintf(stderr,
7677c478bd9Sstevel@tonic-gate 				    gettext("found in mode field\n"));
7687c478bd9Sstevel@tonic-gate 				return (-1);
7697c478bd9Sstevel@tonic-gate 			}
7707c478bd9Sstevel@tonic-gate 		fieldp++;
7717c478bd9Sstevel@tonic-gate 		if (*fieldp == 'x')
7727c478bd9Sstevel@tonic-gate 			aclentp->a_perm += 1;
7737c478bd9Sstevel@tonic-gate 		else
7747c478bd9Sstevel@tonic-gate 			if (*fieldp != '-') {
7757c478bd9Sstevel@tonic-gate 				fprintf(stderr,
7767c478bd9Sstevel@tonic-gate 				    gettext("Unrecognized character "));
7777c478bd9Sstevel@tonic-gate 				fprintf(stderr,
7787c478bd9Sstevel@tonic-gate 				    gettext("found in mode field\n"));
7797c478bd9Sstevel@tonic-gate 				return (-1);
7807c478bd9Sstevel@tonic-gate 			}
7817c478bd9Sstevel@tonic-gate 		return (0);
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	if (*fieldp == '\0')
7857c478bd9Sstevel@tonic-gate 		return (0);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	if (*fieldp >= '0' && *fieldp <= '7')
7887c478bd9Sstevel@tonic-gate 		aclentp->a_perm = *fieldp - '0';
7897c478bd9Sstevel@tonic-gate 	else {
7907c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Unrecognized character "));
7917c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("found in mode field\n"));
7927c478bd9Sstevel@tonic-gate 		return (-1);
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 	if (aclentp->a_perm == 0 && *++fieldp != '\0') {
7957c478bd9Sstevel@tonic-gate 		/* look at next char */
7967c478bd9Sstevel@tonic-gate 		if (*fieldp >= '0' && *fieldp <= '7')
7977c478bd9Sstevel@tonic-gate 			aclentp->a_perm = *fieldp - '0';
7987c478bd9Sstevel@tonic-gate 		else {
7997c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Unrecognized character "));
8007c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("found in mode field\n"));
8017c478bd9Sstevel@tonic-gate 			fprintf(stderr,
8027c478bd9Sstevel@tonic-gate 			    gettext("Check also the number of fields "));
8037c478bd9Sstevel@tonic-gate 			fprintf(stderr,
8047c478bd9Sstevel@tonic-gate 			    gettext("(default) mask and other entries\n"));
8057c478bd9Sstevel@tonic-gate 			return (-1);
8067c478bd9Sstevel@tonic-gate 		}
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 	/* check for junk at the end ??? */
8097c478bd9Sstevel@tonic-gate 	return (0);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate  * This function is different from atoi() in that it checks for
8147c478bd9Sstevel@tonic-gate  * valid digit in the id field whereas atoi() won't report any
8157c478bd9Sstevel@tonic-gate  * error.
8167c478bd9Sstevel@tonic-gate  */
8177c478bd9Sstevel@tonic-gate static int
8187c478bd9Sstevel@tonic-gate conv_id(char *fieldp)
8197c478bd9Sstevel@tonic-gate {
8207c478bd9Sstevel@tonic-gate 	int	a_id = 0;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	for (; *fieldp != '\0'; fieldp++) {
8237c478bd9Sstevel@tonic-gate 		if (!isdigit(*fieldp)) {
8247c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("non-digit in id field\n"));
8257c478bd9Sstevel@tonic-gate 			return (-1);
8267c478bd9Sstevel@tonic-gate 		}
8277c478bd9Sstevel@tonic-gate 		a_id = a_id * 10 + (*fieldp - '0');
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 	return (a_id);
8307c478bd9Sstevel@tonic-gate }
831