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