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