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