xref: /titanic_52/usr/src/lib/libsec/common/acltext.c (revision 3ee87bca47e74aa2719352485b80973ca6e079b7)
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
5ee519a1fSgjelinek  * Common Development and Distribution License (the "License").
6ee519a1fSgjelinek  * 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  */
217c478bd9Sstevel@tonic-gate /*
22bf8b6031Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <grp.h>
297c478bd9Sstevel@tonic-gate #include <pwd.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <limits.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
33fa9e4066Sahrens #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
365a5eeccaSmarks #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/acl.h>
38fa9e4066Sahrens #include <aclutils.h>
39b249c65cSmarks #include <idmap.h>
40fa9e4066Sahrens 
415a5eeccaSmarks #define	ID_STR_MAX	20	/* digits in LONG_MAX */
425a5eeccaSmarks 
435a5eeccaSmarks #define	APPENDED_ID_MAX	ID_STR_MAX + 1		/* id + colon */
445a5eeccaSmarks /*
455a5eeccaSmarks  * yyinteractive controls whether yyparse should print out
465a5eeccaSmarks  * error messages to stderr, and whether or not id's should be
475a5eeccaSmarks  * allowed from acl_fromtext().
485a5eeccaSmarks  */
495a5eeccaSmarks int	yyinteractive;
505a5eeccaSmarks acl_t	*yyacl;
515a5eeccaSmarks char	*yybuf;
52fa9e4066Sahrens 
53fa9e4066Sahrens extern acl_t *acl_alloc(enum acl_type);
547c478bd9Sstevel@tonic-gate 
55b249c65cSmarks /*
56b249c65cSmarks  * dynamic string that will increase in size on an
57b249c65cSmarks  * as needed basis.
58b249c65cSmarks  */
59b249c65cSmarks typedef struct dynaclstr {
60b249c65cSmarks 	size_t d_bufsize;		/* current size of aclexport */
61b249c65cSmarks 	char *d_aclexport;
62b249c65cSmarks 	int d_pos;
63b249c65cSmarks } dynaclstr_t;
6411e32170Shm123892 
65b249c65cSmarks static int str_append(dynaclstr_t *, char *);
66b249c65cSmarks static int aclent_perm_txt(dynaclstr_t *, o_mode_t);
677c478bd9Sstevel@tonic-gate 
685a5eeccaSmarks static void
695a5eeccaSmarks aclent_perms(int perm, char *txt_perms)
70fa9e4066Sahrens {
715a5eeccaSmarks 	if (perm & S_IROTH)
725a5eeccaSmarks 		txt_perms[0] = 'r';
735a5eeccaSmarks 	else
745a5eeccaSmarks 		txt_perms[0] = '-';
755a5eeccaSmarks 	if (perm & S_IWOTH)
765a5eeccaSmarks 		txt_perms[1] = 'w';
775a5eeccaSmarks 	else
785a5eeccaSmarks 		txt_perms[1] = '-';
795a5eeccaSmarks 	if (perm & S_IXOTH)
805a5eeccaSmarks 		txt_perms[2] = 'x';
815a5eeccaSmarks 	else
825a5eeccaSmarks 		txt_perms[2] = '-';
835a5eeccaSmarks 	txt_perms[3] = '\0';
845a5eeccaSmarks }
85fa9e4066Sahrens 
865a5eeccaSmarks static char *
87afe1f701Smarks pruname(uid_t uid, char *uidp, size_t buflen, int noresolve)
885a5eeccaSmarks {
8945a17f45Sgjelinek 	struct passwd	*passwdp = NULL;
90fa9e4066Sahrens 
9145a17f45Sgjelinek 	if (noresolve == 0)
925a5eeccaSmarks 		passwdp = getpwuid(uid);
935a5eeccaSmarks 	if (passwdp == (struct passwd *)NULL) {
945a5eeccaSmarks 		/* could not get passwd information: display uid instead */
95f48205beScasper 		(void) snprintf(uidp, buflen, "%u", uid);
96afe1f701Smarks 	} else {
97afe1f701Smarks 		(void) strlcpy(uidp, passwdp->pw_name, buflen);
98afe1f701Smarks 	}
995a5eeccaSmarks 	return (uidp);
1005a5eeccaSmarks }
101fa9e4066Sahrens 
1025a5eeccaSmarks static char *
103afe1f701Smarks prgname(gid_t gid, char *gidp, size_t buflen, int noresolve)
1045a5eeccaSmarks {
10545a17f45Sgjelinek 	struct group	*groupp = NULL;
106fa9e4066Sahrens 
10745a17f45Sgjelinek 	if (noresolve == 0)
1085a5eeccaSmarks 		groupp = getgrgid(gid);
1095a5eeccaSmarks 	if (groupp == (struct group *)NULL) {
1105a5eeccaSmarks 		/* could not get group information: display gid instead */
111f48205beScasper 		(void) snprintf(gidp, buflen, "%u", gid);
112afe1f701Smarks 	} else {
113afe1f701Smarks 		(void) strlcpy(gidp, groupp->gr_name, buflen);
114afe1f701Smarks 	}
1155a5eeccaSmarks 	return (gidp);
1165a5eeccaSmarks }
117b249c65cSmarks 
118b249c65cSmarks static char *
119b249c65cSmarks prsidname(uid_t who, boolean_t user, char **sidp, int noresolve)
120b249c65cSmarks {
121b249c65cSmarks 	idmap_handle_t *idmap_hdl = NULL;
122b249c65cSmarks 	idmap_get_handle_t *get_hdl = NULL;
123b249c65cSmarks 	idmap_stat status;
124b249c65cSmarks 	idmap_rid_t rid;
125b249c65cSmarks 	int error = 1;
126b249c65cSmarks 	int len;
127b249c65cSmarks 	char *domain;
128b249c65cSmarks 	char *name;
129b249c65cSmarks 
130b249c65cSmarks 	if (noresolve) {
131b249c65cSmarks 		len = snprintf(NULL, 0, "%u", who);
132b249c65cSmarks 		*sidp = malloc(len + 1);
133b249c65cSmarks 		(void) snprintf(*sidp, len + 1, "%u", who);
134b249c65cSmarks 		return (*sidp);
135b249c65cSmarks 	}
136b249c65cSmarks 
137b249c65cSmarks 	/*
138b249c65cSmarks 	 * First try and get windows name
139b249c65cSmarks 	 */
140b249c65cSmarks 
141b249c65cSmarks 	if (user)
142*3ee87bcaSJulian Pullen 		error = idmap_getwinnamebyuid(who, IDMAP_REQ_FLG_USE_CACHE,
143*3ee87bcaSJulian Pullen 		    &name, &domain);
144b249c65cSmarks 	else
145*3ee87bcaSJulian Pullen 		error = idmap_getwinnamebygid(who, IDMAP_REQ_FLG_USE_CACHE,
146*3ee87bcaSJulian Pullen 		    &name, &domain);
147b249c65cSmarks 
148b249c65cSmarks 	if (error) {
149b249c65cSmarks 		if (idmap_init(&idmap_hdl) == 0 &&
150b249c65cSmarks 		    idmap_get_create(idmap_hdl, &get_hdl) == 0) {
151b249c65cSmarks 			if (user)
152b249c65cSmarks 				error = idmap_get_sidbyuid(get_hdl, who,
153*3ee87bcaSJulian Pullen 				    IDMAP_REQ_FLG_USE_CACHE, &domain, &rid,
154*3ee87bcaSJulian Pullen 				    &status);
155b249c65cSmarks 			else
156b249c65cSmarks 				error = idmap_get_sidbygid(get_hdl, who,
157*3ee87bcaSJulian Pullen 				    IDMAP_REQ_FLG_USE_CACHE, &domain, &rid,
158*3ee87bcaSJulian Pullen 				    &status);
159b249c65cSmarks 			if (error == 0)
160b249c65cSmarks 				error = idmap_get_mappings(get_hdl);
161b249c65cSmarks 		}
162b249c65cSmarks 		if (error == 0) {
163b249c65cSmarks 			len = snprintf(NULL, 0, "%s-%d", domain, rid);
164b249c65cSmarks 			*sidp = malloc(len + 1);
165b249c65cSmarks 			(void) snprintf(*sidp, len + 1, "%s-%d", domain, rid);
166b249c65cSmarks 		} else {
167b249c65cSmarks 			*sidp = NULL;
168b249c65cSmarks 		}
169b249c65cSmarks 		if (get_hdl)
170b249c65cSmarks 			idmap_get_destroy(get_hdl);
171b249c65cSmarks 		if (idmap_hdl)
172b249c65cSmarks 			(void) idmap_fini(idmap_hdl);
173b249c65cSmarks 	} else {
174b249c65cSmarks 		int len;
175b249c65cSmarks 		len = snprintf(NULL, 0, "%s@%d", name, domain);
176b249c65cSmarks 		*sidp = malloc(len + 1);
177b249c65cSmarks 		(void) snprintf(*sidp, len + 1, "%s@%s", name, domain);
178b249c65cSmarks 	}
179b249c65cSmarks 	return (*sidp);
180b249c65cSmarks }
181b249c65cSmarks 
1825a5eeccaSmarks static void
1835a5eeccaSmarks aclent_printacl(acl_t *aclp)
1845a5eeccaSmarks {
1855a5eeccaSmarks 	aclent_t *tp;
1865a5eeccaSmarks 	int aclcnt;
1875a5eeccaSmarks 	int mask;
1885a5eeccaSmarks 	int slot = 0;
1895a5eeccaSmarks 	char perm[4];
190afe1f701Smarks 	char uidp[ID_STR_MAX];
191afe1f701Smarks 	char gidp[ID_STR_MAX];
1925a5eeccaSmarks 
1935a5eeccaSmarks 	/* display ACL: assume it is sorted. */
1945a5eeccaSmarks 	aclcnt = aclp->acl_cnt;
1955a5eeccaSmarks 	for (tp = aclp->acl_aclp; tp && aclcnt--; tp++) {
1965a5eeccaSmarks 		if (tp->a_type == CLASS_OBJ)
1975a5eeccaSmarks 			mask = tp->a_perm;
1985a5eeccaSmarks 	}
1995a5eeccaSmarks 	aclcnt = aclp->acl_cnt;
2005a5eeccaSmarks 	for (tp = aclp->acl_aclp; aclcnt--; tp++) {
2015a5eeccaSmarks 		(void) printf("     %d:", slot++);
2025a5eeccaSmarks 		switch (tp->a_type) {
2035a5eeccaSmarks 		case USER:
2045a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2055a5eeccaSmarks 			(void) printf("user:%s:%s\t\t",
206afe1f701Smarks 			    pruname(tp->a_id, uidp, sizeof (uidp), 0), perm);
2075a5eeccaSmarks 			aclent_perms((tp->a_perm & mask), perm);
2085a5eeccaSmarks 			(void) printf("#effective:%s\n", perm);
2095a5eeccaSmarks 			break;
2105a5eeccaSmarks 		case USER_OBJ:
2115a5eeccaSmarks 			/* no need to display uid */
2125a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2135a5eeccaSmarks 			(void) printf("user::%s\n", perm);
2145a5eeccaSmarks 			break;
2155a5eeccaSmarks 		case GROUP:
2165a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2175a5eeccaSmarks 			(void) printf("group:%s:%s\t\t",
218afe1f701Smarks 			    prgname(tp->a_id, gidp, sizeof (gidp), 0), perm);
2195a5eeccaSmarks 			aclent_perms(tp->a_perm & mask, perm);
2205a5eeccaSmarks 			(void) printf("#effective:%s\n", perm);
2215a5eeccaSmarks 			break;
2225a5eeccaSmarks 		case GROUP_OBJ:
2235a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2245a5eeccaSmarks 			(void) printf("group::%s\t\t", perm);
2255a5eeccaSmarks 			aclent_perms(tp->a_perm & mask, perm);
2265a5eeccaSmarks 			(void) printf("#effective:%s\n", perm);
2275a5eeccaSmarks 			break;
2285a5eeccaSmarks 		case CLASS_OBJ:
2295a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2305a5eeccaSmarks 			(void) printf("mask:%s\n", perm);
2315a5eeccaSmarks 			break;
2325a5eeccaSmarks 		case OTHER_OBJ:
2335a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2345a5eeccaSmarks 			(void) printf("other:%s\n", perm);
2355a5eeccaSmarks 			break;
2365a5eeccaSmarks 		case DEF_USER:
2375a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2385a5eeccaSmarks 			(void) printf("default:user:%s:%s\n",
239afe1f701Smarks 			    pruname(tp->a_id, uidp, sizeof (uidp), 0), perm);
2405a5eeccaSmarks 			break;
2415a5eeccaSmarks 		case DEF_USER_OBJ:
2425a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2435a5eeccaSmarks 			(void) printf("default:user::%s\n", perm);
2445a5eeccaSmarks 			break;
2455a5eeccaSmarks 		case DEF_GROUP:
2465a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2475a5eeccaSmarks 			(void) printf("default:group:%s:%s\n",
248afe1f701Smarks 			    prgname(tp->a_id, gidp, sizeof (gidp), 0), perm);
2495a5eeccaSmarks 			break;
2505a5eeccaSmarks 		case DEF_GROUP_OBJ:
2515a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2525a5eeccaSmarks 			(void) printf("default:group::%s\n", perm);
2535a5eeccaSmarks 			break;
2545a5eeccaSmarks 		case DEF_CLASS_OBJ:
2555a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2565a5eeccaSmarks 			(void) printf("default:mask:%s\n", perm);
2575a5eeccaSmarks 			break;
2585a5eeccaSmarks 		case DEF_OTHER_OBJ:
2595a5eeccaSmarks 			aclent_perms(tp->a_perm, perm);
2605a5eeccaSmarks 			(void) printf("default:other:%s\n", perm);
2615a5eeccaSmarks 			break;
2625a5eeccaSmarks 		default:
2635a5eeccaSmarks 			(void) fprintf(stderr,
2645b233e2dSmarks 			    dgettext(TEXT_DOMAIN, "unrecognized entry\n"));
2655a5eeccaSmarks 			break;
2665a5eeccaSmarks 		}
2675a5eeccaSmarks 	}
2685a5eeccaSmarks }
2695a5eeccaSmarks 
2705a5eeccaSmarks static void
2715a5eeccaSmarks split_line(char *str, int cols)
2725a5eeccaSmarks {
2735a5eeccaSmarks 	char *ptr;
2745a5eeccaSmarks 	int len;
2755a5eeccaSmarks 	int i;
2765a5eeccaSmarks 	int last_split;
2775a5eeccaSmarks 	char *pad = "";
2785a5eeccaSmarks 	int pad_len;
2795a5eeccaSmarks 
2805a5eeccaSmarks 	len = strlen(str);
2815a5eeccaSmarks 	ptr = str;
2825a5eeccaSmarks 	pad_len = 0;
2835a5eeccaSmarks 
2845a5eeccaSmarks 	ptr = str;
2855a5eeccaSmarks 	last_split = 0;
2865a5eeccaSmarks 	for (i = 0; i != len; i++) {
2875a5eeccaSmarks 		if ((i + pad_len + 4) >= cols) {
2885a5eeccaSmarks 			(void) printf("%s%.*s\n", pad, last_split, ptr);
2895a5eeccaSmarks 			ptr = &ptr[last_split];
2905a5eeccaSmarks 			len = strlen(ptr);
2915a5eeccaSmarks 			i = 0;
2925a5eeccaSmarks 			pad_len = 4;
2935a5eeccaSmarks 			pad = "         ";
2945a5eeccaSmarks 		} else {
2955a5eeccaSmarks 			if (ptr[i] == '/' || ptr[i] == ':') {
2965a5eeccaSmarks 				last_split = i;
2975a5eeccaSmarks 			}
2985a5eeccaSmarks 		}
2995a5eeccaSmarks 	}
3005a5eeccaSmarks 	if (i == len) {
3015a5eeccaSmarks 		(void) printf("%s%s\n", pad, ptr);
3025a5eeccaSmarks 	}
3035a5eeccaSmarks }
3045a5eeccaSmarks 
305b249c65cSmarks /*
306b249c65cSmarks  * compute entry type string, such as user:joe, group:staff,...
307b249c65cSmarks  */
308b249c65cSmarks static int
309b249c65cSmarks aclent_type_txt(dynaclstr_t *dstr, aclent_t *aclp, int flags)
3105a5eeccaSmarks {
311afe1f701Smarks 	char idp[ID_STR_MAX];
312b249c65cSmarks 	int error;
3135a5eeccaSmarks 
314b249c65cSmarks 	switch (aclp->a_type) {
315b249c65cSmarks 	case DEF_USER_OBJ:
316b249c65cSmarks 	case USER_OBJ:
317b249c65cSmarks 		if (aclp->a_type == USER_OBJ)
318b249c65cSmarks 			error = str_append(dstr, "user::");
319b249c65cSmarks 		else
320b249c65cSmarks 			error = str_append(dstr, "defaultuser::");
321b249c65cSmarks 		break;
322b249c65cSmarks 
323b249c65cSmarks 	case DEF_USER:
324b249c65cSmarks 	case USER:
325b249c65cSmarks 		if (aclp->a_type == USER)
326b249c65cSmarks 			error = str_append(dstr, "user:");
327b249c65cSmarks 		else
328b249c65cSmarks 			error = str_append(dstr, "defaultuser:");
329b249c65cSmarks 		if (error)
330b249c65cSmarks 			break;
331b249c65cSmarks 		error = str_append(dstr, pruname(aclp->a_id, idp,
332b249c65cSmarks 		    sizeof (idp), flags & ACL_NORESOLVE));
333b249c65cSmarks 		if (error == 0)
334b249c65cSmarks 			error = str_append(dstr, ":");
335b249c65cSmarks 		break;
336b249c65cSmarks 
337b249c65cSmarks 	case DEF_GROUP_OBJ:
338b249c65cSmarks 	case GROUP_OBJ:
339b249c65cSmarks 		if (aclp->a_type == GROUP_OBJ)
340b249c65cSmarks 			error = str_append(dstr, "group::");
341b249c65cSmarks 		else
342b249c65cSmarks 			error = str_append(dstr, "defaultgroup::");
343b249c65cSmarks 		break;
344b249c65cSmarks 
345b249c65cSmarks 	case DEF_GROUP:
346b249c65cSmarks 	case GROUP:
347b249c65cSmarks 		if (aclp->a_type == GROUP)
348b249c65cSmarks 			error = str_append(dstr, "group:");
349b249c65cSmarks 		else
350b249c65cSmarks 			error = str_append(dstr, "defaultgroup:");
351b249c65cSmarks 		if (error)
352b249c65cSmarks 			break;
353b249c65cSmarks 		error = str_append(dstr, prgname(aclp->a_id, idp,
354b249c65cSmarks 		    sizeof (idp), flags & ACL_NORESOLVE));
355b249c65cSmarks 		if (error == 0)
356b249c65cSmarks 			error = str_append(dstr, ":");
357b249c65cSmarks 		break;
358b249c65cSmarks 
359b249c65cSmarks 	case DEF_CLASS_OBJ:
360b249c65cSmarks 	case CLASS_OBJ:
361b249c65cSmarks 		if (aclp->a_type == CLASS_OBJ)
362b249c65cSmarks 			error = str_append(dstr, "mask:");
363b249c65cSmarks 		else
364b249c65cSmarks 			error = str_append(dstr, "defaultmask:");
365b249c65cSmarks 		break;
366b249c65cSmarks 
367b249c65cSmarks 	case DEF_OTHER_OBJ:
368b249c65cSmarks 	case OTHER_OBJ:
369b249c65cSmarks 		if (aclp->a_type == OTHER_OBJ)
370b249c65cSmarks 			error = str_append(dstr, "other:");
371b249c65cSmarks 		else
372b249c65cSmarks 			error = str_append(dstr, "defaultother:");
373b249c65cSmarks 		break;
374b249c65cSmarks 
375b249c65cSmarks 	default:
376b249c65cSmarks 		error = 1;
377b249c65cSmarks 		break;
378b249c65cSmarks 	}
379b249c65cSmarks 
380b249c65cSmarks 	return (error);
381b249c65cSmarks }
382b249c65cSmarks 
383b249c65cSmarks /*
384b249c65cSmarks  * compute entry type string such as, owner@:, user:joe, group:staff,...
385b249c65cSmarks  */
386b249c65cSmarks static int
387b249c65cSmarks ace_type_txt(dynaclstr_t *dynstr, ace_t *acep, int flags)
388b249c65cSmarks {
389b249c65cSmarks 	char idp[ID_STR_MAX];
390b249c65cSmarks 	int error;
391b249c65cSmarks 	char *sidp = NULL;
3925a5eeccaSmarks 
3935a5eeccaSmarks 	switch (acep->a_flags & ACE_TYPE_FLAGS) {
3945a5eeccaSmarks 	case ACE_OWNER:
395b249c65cSmarks 		error = str_append(dynstr, OWNERAT_TXT);
3965a5eeccaSmarks 		break;
3975a5eeccaSmarks 
3985a5eeccaSmarks 	case ACE_GROUP|ACE_IDENTIFIER_GROUP:
399b249c65cSmarks 		error = str_append(dynstr, GROUPAT_TXT);
4005a5eeccaSmarks 		break;
4015a5eeccaSmarks 
4025a5eeccaSmarks 	case ACE_IDENTIFIER_GROUP:
403b249c65cSmarks 		if ((flags & ACL_SID_FMT) && acep->a_who > MAXUID) {
404b249c65cSmarks 			if (error = str_append(dynstr,
405b249c65cSmarks 			    GROUPSID_TXT))
406b249c65cSmarks 				break;
407b249c65cSmarks 			error = str_append(dynstr, prsidname(acep->a_who,
408b249c65cSmarks 			    B_FALSE, &sidp, flags & ACL_NORESOLVE));
409b249c65cSmarks 		} else {
410b249c65cSmarks 			if (error = str_append(dynstr, GROUP_TXT))
411b249c65cSmarks 				break;
412b249c65cSmarks 			error = str_append(dynstr, prgname(acep->a_who, idp,
413afe1f701Smarks 			    sizeof (idp), flags & ACL_NORESOLVE));
414b249c65cSmarks 		}
415b249c65cSmarks 		if (error == 0)
416b249c65cSmarks 			error = str_append(dynstr, ":");
4175a5eeccaSmarks 		break;
4185a5eeccaSmarks 
4195a5eeccaSmarks 	case ACE_EVERYONE:
420b249c65cSmarks 		error = str_append(dynstr, EVERYONEAT_TXT);
4215a5eeccaSmarks 		break;
4225a5eeccaSmarks 
4235a5eeccaSmarks 	case 0:
424b249c65cSmarks 		if ((flags & ACL_SID_FMT) && acep->a_who > MAXUID) {
425b249c65cSmarks 			if (error = str_append(dynstr, USERSID_TXT))
426b249c65cSmarks 				break;
427b249c65cSmarks 			error = str_append(dynstr, prsidname(acep->a_who,
428b249c65cSmarks 			    B_TRUE, &sidp, flags & ACL_NORESOLVE));
429b249c65cSmarks 		} else {
430b249c65cSmarks 			if (error = str_append(dynstr, USER_TXT))
431b249c65cSmarks 				break;
432b249c65cSmarks 			error = str_append(dynstr, pruname(acep->a_who, idp,
433afe1f701Smarks 			    sizeof (idp), flags & ACL_NORESOLVE));
434b249c65cSmarks 		}
435b249c65cSmarks 		if (error == 0)
436b249c65cSmarks 			error = str_append(dynstr, ":");
437b249c65cSmarks 		break;
438b249c65cSmarks 	default:
439b249c65cSmarks 		error = 0;
4405a5eeccaSmarks 		break;
4415a5eeccaSmarks 	}
4425a5eeccaSmarks 
443b249c65cSmarks 	if (sidp)
444b249c65cSmarks 		free(sidp);
445b249c65cSmarks 	return (error);
4465a5eeccaSmarks }
4475a5eeccaSmarks 
448b249c65cSmarks /*
449b249c65cSmarks  * compute string of permissions, such as read_data/write_data or
450b249c65cSmarks  * rwxp,...
451b249c65cSmarks  * The format depends on the flags field which indicates whether the compact
452b249c65cSmarks  * or verbose format should be used.
453b249c65cSmarks  */
454b249c65cSmarks static int
455b249c65cSmarks ace_perm_txt(dynaclstr_t *dstr, uint32_t mask,
4565a5eeccaSmarks     uint32_t iflags, int isdir, int flags)
4575a5eeccaSmarks {
458b249c65cSmarks 	int error = 0;
4595a5eeccaSmarks 
4605a5eeccaSmarks 	if (flags & ACL_COMPACT_FMT) {
461b249c65cSmarks 		char buf[16];
4625a5eeccaSmarks 
4635a5eeccaSmarks 		if (mask & ACE_READ_DATA)
4645a5eeccaSmarks 			buf[0] = 'r';
4655a5eeccaSmarks 		else
4665a5eeccaSmarks 			buf[0] = '-';
4675a5eeccaSmarks 		if (mask & ACE_WRITE_DATA)
4685a5eeccaSmarks 			buf[1] = 'w';
4695a5eeccaSmarks 		else
4705a5eeccaSmarks 			buf[1] = '-';
4715a5eeccaSmarks 		if (mask & ACE_EXECUTE)
4725a5eeccaSmarks 			buf[2] = 'x';
4735a5eeccaSmarks 		else
4745a5eeccaSmarks 			buf[2] = '-';
4755a5eeccaSmarks 		if (mask & ACE_APPEND_DATA)
4765a5eeccaSmarks 			buf[3] = 'p';
4775a5eeccaSmarks 		else
4785a5eeccaSmarks 			buf[3] = '-';
4795a5eeccaSmarks 		if (mask & ACE_DELETE)
4805a5eeccaSmarks 			buf[4] = 'd';
4815a5eeccaSmarks 		else
4825a5eeccaSmarks 			buf[4] = '-';
4835a5eeccaSmarks 		if (mask & ACE_DELETE_CHILD)
4845a5eeccaSmarks 			buf[5] = 'D';
4855a5eeccaSmarks 		else
4865a5eeccaSmarks 			buf[5] = '-';
4875a5eeccaSmarks 		if (mask & ACE_READ_ATTRIBUTES)
4885a5eeccaSmarks 			buf[6] = 'a';
4895a5eeccaSmarks 		else
4905a5eeccaSmarks 			buf[6] = '-';
4915a5eeccaSmarks 		if (mask & ACE_WRITE_ATTRIBUTES)
4925a5eeccaSmarks 			buf[7] = 'A';
4935a5eeccaSmarks 		else
4945a5eeccaSmarks 			buf[7] = '-';
4955a5eeccaSmarks 		if (mask & ACE_READ_NAMED_ATTRS)
4965a5eeccaSmarks 			buf[8] = 'R';
4975a5eeccaSmarks 		else
4985a5eeccaSmarks 			buf[8] = '-';
4995a5eeccaSmarks 		if (mask & ACE_WRITE_NAMED_ATTRS)
5005a5eeccaSmarks 			buf[9] = 'W';
5015a5eeccaSmarks 		else
5025a5eeccaSmarks 			buf[9] = '-';
5035a5eeccaSmarks 		if (mask & ACE_READ_ACL)
5045a5eeccaSmarks 			buf[10] = 'c';
5055a5eeccaSmarks 		else
5065a5eeccaSmarks 			buf[10] = '-';
5075a5eeccaSmarks 		if (mask & ACE_WRITE_ACL)
5085a5eeccaSmarks 			buf[11] = 'C';
5095a5eeccaSmarks 		else
5105a5eeccaSmarks 			buf[11] = '-';
5115a5eeccaSmarks 		if (mask & ACE_WRITE_OWNER)
5125a5eeccaSmarks 			buf[12] = 'o';
5135a5eeccaSmarks 		else
5145a5eeccaSmarks 			buf[12] = '-';
5155a5eeccaSmarks 		if (mask & ACE_SYNCHRONIZE)
5165a5eeccaSmarks 			buf[13] = 's';
5175a5eeccaSmarks 		else
5185a5eeccaSmarks 			buf[13] = '-';
519b249c65cSmarks 		buf[14] = ':';
520b249c65cSmarks 		buf[15] = '\0';
521b249c65cSmarks 		error = str_append(dstr, buf);
5225a5eeccaSmarks 	} else {
5235a5eeccaSmarks 		/*
5245a5eeccaSmarks 		 * If ACE is a directory, but inheritance indicates its
5255a5eeccaSmarks 		 * for a file then print permissions for file rather than
5265a5eeccaSmarks 		 * dir.
5275a5eeccaSmarks 		 */
5285a5eeccaSmarks 		if (isdir) {
5295a5eeccaSmarks 			if (mask & ACE_LIST_DIRECTORY) {
5305a5eeccaSmarks 				if (iflags == ACE_FILE_INHERIT_ACE) {
531b249c65cSmarks 					error = str_append(dstr,
532b249c65cSmarks 					    READ_DATA_TXT);
5335a5eeccaSmarks 				} else {
534b249c65cSmarks 					error =
535b249c65cSmarks 					    str_append(dstr, READ_DIR_TXT);
5365a5eeccaSmarks 				}
5375a5eeccaSmarks 			}
538b249c65cSmarks 			if (error == 0 && (mask & ACE_ADD_FILE)) {
5395a5eeccaSmarks 				if (iflags == ACE_FILE_INHERIT_ACE) {
540b249c65cSmarks 					error =
541b249c65cSmarks 					    str_append(dstr, WRITE_DATA_TXT);
5425a5eeccaSmarks 				} else {
543b249c65cSmarks 					error =
544b249c65cSmarks 					    str_append(dstr, ADD_FILE_TXT);
5455a5eeccaSmarks 				}
5465a5eeccaSmarks 			}
547b249c65cSmarks 			if (error == 0 && (mask & ACE_ADD_SUBDIRECTORY)) {
5485a5eeccaSmarks 				if (iflags == ACE_FILE_INHERIT_ACE) {
549b249c65cSmarks 					error = str_append(dstr,
550b249c65cSmarks 					    APPEND_DATA_TXT);
5515a5eeccaSmarks 				} else {
552b249c65cSmarks 					error = str_append(dstr,
553b249c65cSmarks 					    ADD_DIR_TXT);
5545a5eeccaSmarks 				}
5555a5eeccaSmarks 			}
5565a5eeccaSmarks 		} else {
5575a5eeccaSmarks 			if (mask & ACE_READ_DATA) {
558b249c65cSmarks 				error = str_append(dstr, READ_DATA_TXT);
5595a5eeccaSmarks 			}
560b249c65cSmarks 			if (error == 0 && (mask & ACE_WRITE_DATA)) {
561b249c65cSmarks 				error = str_append(dstr, WRITE_DATA_TXT);
5625a5eeccaSmarks 			}
563b249c65cSmarks 			if (error == 0 && (mask & ACE_APPEND_DATA)) {
564b249c65cSmarks 				error = str_append(dstr, APPEND_DATA_TXT);
5655a5eeccaSmarks 			}
5665a5eeccaSmarks 		}
567b249c65cSmarks 		if (error == 0 && (mask & ACE_READ_NAMED_ATTRS)) {
568b249c65cSmarks 			error = str_append(dstr, READ_XATTR_TXT);
5695a5eeccaSmarks 		}
570b249c65cSmarks 		if (error == 0 && (mask & ACE_WRITE_NAMED_ATTRS)) {
571b249c65cSmarks 			error = str_append(dstr, WRITE_XATTR_TXT);
5725a5eeccaSmarks 		}
573b249c65cSmarks 		if (error == 0 && (mask & ACE_EXECUTE)) {
574b249c65cSmarks 			error = str_append(dstr, EXECUTE_TXT);
5755a5eeccaSmarks 		}
576b249c65cSmarks 		if (error == 0 && (mask & ACE_DELETE_CHILD)) {
577b249c65cSmarks 			error = str_append(dstr, DELETE_CHILD_TXT);
5785a5eeccaSmarks 		}
579b249c65cSmarks 		if (error == 0 && (mask & ACE_READ_ATTRIBUTES)) {
580b249c65cSmarks 			error = str_append(dstr, READ_ATTRIBUTES_TXT);
5815a5eeccaSmarks 		}
582b249c65cSmarks 		if (error == 0 && (mask & ACE_WRITE_ATTRIBUTES)) {
583b249c65cSmarks 			error = str_append(dstr, WRITE_ATTRIBUTES_TXT);
5845a5eeccaSmarks 		}
585b249c65cSmarks 		if (error == 0 && (mask & ACE_DELETE)) {
586b249c65cSmarks 			error = str_append(dstr, DELETE_TXT);
5875a5eeccaSmarks 		}
588b249c65cSmarks 		if (error == 0 && (mask & ACE_READ_ACL)) {
589b249c65cSmarks 			error = str_append(dstr, READ_ACL_TXT);
5905a5eeccaSmarks 		}
591b249c65cSmarks 		if (error == 0 && (mask & ACE_WRITE_ACL)) {
592b249c65cSmarks 			error = str_append(dstr, WRITE_ACL_TXT);
5935a5eeccaSmarks 		}
594b249c65cSmarks 		if (error == 0 && (mask & ACE_WRITE_OWNER)) {
595b249c65cSmarks 			error = str_append(dstr, WRITE_OWNER_TXT);
5965a5eeccaSmarks 		}
597b249c65cSmarks 		if (error == 0 && (mask & ACE_SYNCHRONIZE)) {
598b249c65cSmarks 			error = str_append(dstr, SYNCHRONIZE_TXT);
599b249c65cSmarks 		}
600b249c65cSmarks 		if (error == 0 && dstr->d_aclexport[dstr->d_pos-1] == '/') {
601b249c65cSmarks 			dstr->d_aclexport[--dstr->d_pos] = '\0';
602b249c65cSmarks 		}
603b249c65cSmarks 		if (error == 0)
604b249c65cSmarks 			error = str_append(dstr, ":");
605b249c65cSmarks 	}
606b249c65cSmarks 	return (error);
6075a5eeccaSmarks }
6085a5eeccaSmarks 
609b249c65cSmarks /*
610b249c65cSmarks  * compute string of access type, such as allow, deny, ...
611b249c65cSmarks  */
612b249c65cSmarks static int
613b249c65cSmarks ace_access_txt(dynaclstr_t *dstr, int type)
6145a5eeccaSmarks {
615b249c65cSmarks 	int error;
6165a5eeccaSmarks 
617b249c65cSmarks 	if (type == ACE_ACCESS_ALLOWED_ACE_TYPE)
618b249c65cSmarks 		error = str_append(dstr, ALLOW_TXT);
619b249c65cSmarks 	else if (type == ACE_ACCESS_DENIED_ACE_TYPE)
620b249c65cSmarks 		error = str_append(dstr, DENY_TXT);
621b249c65cSmarks 	else if (type == ACE_SYSTEM_AUDIT_ACE_TYPE)
622b249c65cSmarks 		error = str_append(dstr, AUDIT_TXT);
623b249c65cSmarks 	else if (type == ACE_SYSTEM_ALARM_ACE_TYPE)
624b249c65cSmarks 		error = str_append(dstr, ALARM_TXT);
625b249c65cSmarks 	else
626b249c65cSmarks 		error = str_append(dstr, UNKNOWN_TXT);
6275a5eeccaSmarks 
628b249c65cSmarks 	return (error);
6295a5eeccaSmarks }
6305a5eeccaSmarks 
631b249c65cSmarks static int
632b249c65cSmarks ace_inherit_txt(dynaclstr_t *dstr, uint32_t iflags, int flags)
6335a5eeccaSmarks {
634b249c65cSmarks 	int error = 0;
6355a5eeccaSmarks 
6365a5eeccaSmarks 	if (flags & ACL_COMPACT_FMT) {
637b249c65cSmarks 		char buf[9];
638b249c65cSmarks 
6395a5eeccaSmarks 		if (iflags & ACE_FILE_INHERIT_ACE)
6405a5eeccaSmarks 			buf[0] = 'f';
6415a5eeccaSmarks 		else
6425a5eeccaSmarks 			buf[0] = '-';
6435a5eeccaSmarks 		if (iflags & ACE_DIRECTORY_INHERIT_ACE)
6445a5eeccaSmarks 			buf[1] = 'd';
6455a5eeccaSmarks 		else
6465a5eeccaSmarks 			buf[1] = '-';
6475a5eeccaSmarks 		if (iflags & ACE_INHERIT_ONLY_ACE)
6485a5eeccaSmarks 			buf[2] = 'i';
6495a5eeccaSmarks 		else
6505a5eeccaSmarks 			buf[2] = '-';
6515a5eeccaSmarks 		if (iflags & ACE_NO_PROPAGATE_INHERIT_ACE)
6525a5eeccaSmarks 			buf[3] = 'n';
6535a5eeccaSmarks 		else
6545a5eeccaSmarks 			buf[3] = '-';
6555a5eeccaSmarks 		if (iflags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG)
6565a5eeccaSmarks 			buf[4] = 'S';
6575a5eeccaSmarks 		else
6585a5eeccaSmarks 			buf[4] = '-';
6595a5eeccaSmarks 		if (iflags & ACE_FAILED_ACCESS_ACE_FLAG)
6605a5eeccaSmarks 			buf[5] = 'F';
6615a5eeccaSmarks 		else
6625a5eeccaSmarks 			buf[5] = '-';
663da6c28aaSamw 		if (iflags & ACE_INHERITED_ACE)
664da6c28aaSamw 			buf[6] = 'I';
665da6c28aaSamw 		else
666da6c28aaSamw 			buf[6] = '-';
667b249c65cSmarks 		buf[7] = ':';
668b249c65cSmarks 		buf[8] = '\0';
669b249c65cSmarks 		error = str_append(dstr, buf);
6705a5eeccaSmarks 	} else {
6715a5eeccaSmarks 		if (iflags & ACE_FILE_INHERIT_ACE) {
672b249c65cSmarks 			error = str_append(dstr, FILE_INHERIT_TXT);
6735a5eeccaSmarks 		}
674b249c65cSmarks 		if (error == 0 && (iflags & ACE_DIRECTORY_INHERIT_ACE)) {
675b249c65cSmarks 			error = str_append(dstr, DIR_INHERIT_TXT);
6765a5eeccaSmarks 		}
677b249c65cSmarks 		if (error == 0 && (iflags & ACE_NO_PROPAGATE_INHERIT_ACE)) {
678b249c65cSmarks 			error = str_append(dstr, NO_PROPAGATE_TXT);
6795a5eeccaSmarks 		}
680b249c65cSmarks 		if (error == 0 && (iflags & ACE_INHERIT_ONLY_ACE)) {
681b249c65cSmarks 			error = str_append(dstr, INHERIT_ONLY_TXT);
6825a5eeccaSmarks 		}
683b249c65cSmarks 		if (error == 0 && (iflags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG)) {
684b249c65cSmarks 			error = str_append(dstr, SUCCESSFUL_ACCESS_TXT);
685da6c28aaSamw 		}
686b249c65cSmarks 		if (error == 0 && (iflags & ACE_FAILED_ACCESS_ACE_FLAG)) {
687b249c65cSmarks 			error = str_append(dstr, FAILED_ACCESS_TXT);
688da6c28aaSamw 		}
689b249c65cSmarks 		if (error == 0 && (iflags & ACE_INHERITED_ACE)) {
690b249c65cSmarks 			error = str_append(dstr, INHERITED_ACE_TXT);
691b249c65cSmarks 		}
692b249c65cSmarks 		if (error == 0 && dstr->d_aclexport[dstr->d_pos-1] == '/') {
693b249c65cSmarks 			dstr->d_aclexport[--dstr->d_pos] = '\0';
694b249c65cSmarks 			error = str_append(dstr, ":");
695b249c65cSmarks 		}
696da6c28aaSamw 	}
6975a5eeccaSmarks 
698b249c65cSmarks 	return (error);
699fa9e4066Sahrens }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * Convert internal acl representation to external representation.
7037c478bd9Sstevel@tonic-gate  *
7047c478bd9Sstevel@tonic-gate  * The length of a non-owning user name or non-owning group name ie entries
7057c478bd9Sstevel@tonic-gate  * of type DEF_USER, USER, DEF_GROUP or GROUP, can exceed LOGNAME_MAX.  We
7067c478bd9Sstevel@tonic-gate  * thus check the length of these entries, and if greater than LOGNAME_MAX,
7077c478bd9Sstevel@tonic-gate  * we realloc() via increase_length().
7087c478bd9Sstevel@tonic-gate  *
7097c478bd9Sstevel@tonic-gate  * The LOGNAME_MAX, ENTRYTYPELEN and PERMS limits are otherwise always
7107c478bd9Sstevel@tonic-gate  * adhered to.
7117c478bd9Sstevel@tonic-gate  */
7125a5eeccaSmarks 
7135a5eeccaSmarks /*
7145a5eeccaSmarks  * acltotext() converts each ACL entry to look like this:
7155a5eeccaSmarks  *
7165a5eeccaSmarks  *    entry_type:uid^gid^name:perms[:id]
7175a5eeccaSmarks  *
7185a5eeccaSmarks  * The maximum length of entry_type is 14 ("defaultgroup::" and
7195a5eeccaSmarks  * "defaultother::") hence ENTRYTYPELEN is set to 14.
7205a5eeccaSmarks  *
7215a5eeccaSmarks  * The max length of a uid^gid^name entry (in theory) is 8, hence we use,
7225a5eeccaSmarks  * however the ID could be a number so we therefore use ID_STR_MAX
7235a5eeccaSmarks  *
7245a5eeccaSmarks  * The length of a perms entry is 4 to allow for the comma appended to each
7255a5eeccaSmarks  * to each acl entry.  Hence PERMS is set to 4.
7265a5eeccaSmarks  */
7275a5eeccaSmarks 
7285a5eeccaSmarks #define	ENTRYTYPELEN	14
7295a5eeccaSmarks #define	PERMS		4
7305a5eeccaSmarks #define	ACL_ENTRY_SIZE	(ENTRYTYPELEN + ID_STR_MAX + PERMS + APPENDED_ID_MAX)
7315a5eeccaSmarks 
7327c478bd9Sstevel@tonic-gate char *
7335a5eeccaSmarks aclent_acltotext(aclent_t  *aclp, int aclcnt, int flags)
7347c478bd9Sstevel@tonic-gate {
735b249c65cSmarks 	dynaclstr_t 	*dstr;
7367c478bd9Sstevel@tonic-gate 	char		*aclexport;
737b249c65cSmarks 	int		i;
738b249c65cSmarks 	int 		error = 0;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if (aclp == NULL)
7417c478bd9Sstevel@tonic-gate 		return (NULL);
742b249c65cSmarks 	if ((dstr = malloc(sizeof (dynaclstr_t))) == NULL)
7437c478bd9Sstevel@tonic-gate 		return (NULL);
744b249c65cSmarks 	dstr->d_bufsize = aclcnt * ACL_ENTRY_SIZE;
745b249c65cSmarks 	if ((dstr->d_aclexport = malloc(dstr->d_bufsize)) == NULL) {
7467c478bd9Sstevel@tonic-gate 		free(dstr);
7477c478bd9Sstevel@tonic-gate 		return (NULL);
7487c478bd9Sstevel@tonic-gate 	}
749b249c65cSmarks 	*dstr->d_aclexport = '\0';
750b249c65cSmarks 	dstr->d_pos = 0;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	for (i = 0; i < aclcnt; i++, aclp++) {
753b249c65cSmarks 		if (error = aclent_type_txt(dstr, aclp, flags))
7547c478bd9Sstevel@tonic-gate 			break;
755b249c65cSmarks 		if (error = aclent_perm_txt(dstr, aclp->a_perm))
7567c478bd9Sstevel@tonic-gate 			break;
7575a5eeccaSmarks 
7585a5eeccaSmarks 		if ((flags & ACL_APPEND_ID) && ((aclp->a_type == USER) ||
7595a5eeccaSmarks 		    (aclp->a_type == DEF_USER) || (aclp->a_type == GROUP) ||
7605a5eeccaSmarks 		    (aclp->a_type == DEF_GROUP))) {
761b249c65cSmarks 			char id[ID_STR_MAX], *idstr;
762b249c65cSmarks 
763b249c65cSmarks 			if (error = str_append(dstr, ":"))
764b249c65cSmarks 				break;
7655a5eeccaSmarks 			id[ID_STR_MAX - 1] = '\0'; /* null terminate buffer */
7665a5eeccaSmarks 			idstr = lltostr(aclp->a_id, &id[ID_STR_MAX - 1]);
767b249c65cSmarks 			if (error = str_append(dstr, idstr))
768b249c65cSmarks 				break;
7695a5eeccaSmarks 		}
7707c478bd9Sstevel@tonic-gate 		if (i < aclcnt - 1)
771b249c65cSmarks 			if (error = str_append(dstr, ","))
772b249c65cSmarks 				break;
7737c478bd9Sstevel@tonic-gate 	}
774b249c65cSmarks 	if (error) {
775b249c65cSmarks 		if (dstr->d_aclexport)
776b249c65cSmarks 			free(dstr->d_aclexport);
777b249c65cSmarks 	} else {
778b249c65cSmarks 		aclexport = dstr->d_aclexport;
779b249c65cSmarks 	}
7807c478bd9Sstevel@tonic-gate 	free(dstr);
7817c478bd9Sstevel@tonic-gate 	return (aclexport);
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7845a5eeccaSmarks char *
7855a5eeccaSmarks acltotext(aclent_t *aclp, int aclcnt)
7867c478bd9Sstevel@tonic-gate {
7875a5eeccaSmarks 	return (aclent_acltotext(aclp, aclcnt, 0));
788fa9e4066Sahrens }
789fa9e4066Sahrens 
7907c478bd9Sstevel@tonic-gate 
791fa9e4066Sahrens aclent_t *
792fa9e4066Sahrens aclfromtext(char *aclstr, int *aclcnt)
793fa9e4066Sahrens {
794fa9e4066Sahrens 	acl_t *aclp;
795fa9e4066Sahrens 	aclent_t *aclentp;
796fa9e4066Sahrens 	int error;
797fa9e4066Sahrens 
7985a5eeccaSmarks 	error = acl_fromtext(aclstr, &aclp);
799fa9e4066Sahrens 	if (error)
800fa9e4066Sahrens 		return (NULL);
801fa9e4066Sahrens 
802fa9e4066Sahrens 	aclentp = aclp->acl_aclp;
803fa9e4066Sahrens 	aclp->acl_aclp = NULL;
804fa9e4066Sahrens 	*aclcnt = aclp->acl_cnt;
8055a5eeccaSmarks 
8065a5eeccaSmarks 	acl_free(aclp);
807fa9e4066Sahrens 	return (aclentp);
808fa9e4066Sahrens }
809fa9e4066Sahrens 
810fa9e4066Sahrens 
8117c478bd9Sstevel@tonic-gate /*
812b249c65cSmarks  * returns a character position index of the start of the newly
813b249c65cSmarks  * appended string.  Returns -1 if operation couldn't be completed.
8147c478bd9Sstevel@tonic-gate  */
8157c478bd9Sstevel@tonic-gate static int
816b249c65cSmarks str_append(dynaclstr_t *dstr, char *newstr)
8177c478bd9Sstevel@tonic-gate {
818b249c65cSmarks 	size_t len = strlen(newstr);
8197c478bd9Sstevel@tonic-gate 
820b249c65cSmarks 	if ((len + dstr->d_pos) >= dstr->d_bufsize) {
821b249c65cSmarks 		dstr->d_aclexport = realloc(dstr->d_aclexport,
822b249c65cSmarks 		    dstr->d_bufsize + len + 1);
823b249c65cSmarks 		if (dstr->d_aclexport == NULL)
8247c478bd9Sstevel@tonic-gate 			return (1);
825b249c65cSmarks 		dstr->d_bufsize += len;
826b249c65cSmarks 	}
827b249c65cSmarks 	(void) strcat(&dstr->d_aclexport[dstr->d_pos], newstr);
828b249c65cSmarks 	dstr->d_pos += len;
8297c478bd9Sstevel@tonic-gate 	return (0);
8307c478bd9Sstevel@tonic-gate }
831fa9e4066Sahrens 
832b249c65cSmarks static int
833b249c65cSmarks aclent_perm_txt(dynaclstr_t *dstr, o_mode_t perm)
834b249c65cSmarks {
835b249c65cSmarks 	char buf[4];
836b249c65cSmarks 
837b249c65cSmarks 	if (perm & S_IROTH)
838b249c65cSmarks 		buf[0] = 'r';
839b249c65cSmarks 	else
840b249c65cSmarks 		buf[0] = '-';
841b249c65cSmarks 	if (perm & S_IWOTH)
842b249c65cSmarks 		buf[1] = 'w';
843b249c65cSmarks 	else
844b249c65cSmarks 		buf[1] = '-';
845b249c65cSmarks 	if (perm & S_IXOTH)
846b249c65cSmarks 		buf[2] = 'x';
847b249c65cSmarks 	else
848b249c65cSmarks 		buf[2] = '-';
849b249c65cSmarks 	buf[3] = '\0';
850b249c65cSmarks 	return (str_append(dstr, buf));
851b249c65cSmarks }
852b249c65cSmarks 
853fa9e4066Sahrens /*
8545a5eeccaSmarks  * ace_acltotext() convert each ace formatted acl to look like this:
855fa9e4066Sahrens  *
8565a5eeccaSmarks  * entry_type:uid^gid^name:perms[:flags]:<allow|deny>[:id][,]
857fa9e4066Sahrens  *
858fa9e4066Sahrens  * The maximum length of entry_type is 5 ("group")
859fa9e4066Sahrens  *
8605a5eeccaSmarks  * The max length of a uid^gid^name entry (in theory) is 8,
8615a5eeccaSmarks  * however id could be a number so we therefore use ID_STR_MAX
862fa9e4066Sahrens  *
863fa9e4066Sahrens  * The length of a perms entry is 144 i.e read_data/write_data...
864fa9e4066Sahrens  * to each acl entry.
865fa9e4066Sahrens  *
866da6c28aaSamw  * iflags: file_inherit/dir_inherit/inherit_only/no_propagate/successful_access
867da6c28aaSamw  *         /failed_access
868fa9e4066Sahrens  *
869fa9e4066Sahrens  */
870fa9e4066Sahrens 
871fa9e4066Sahrens #define	ACE_ENTRYTYPLEN		6
872da6c28aaSamw #define	IFLAGS_STR "file_inherit/dir_inherit/inherit_only/no_propagate/" \
873da6c28aaSamw 	"successful_access/failed_access/inherited"
874da6c28aaSamw #define	IFLAGS_SIZE		(sizeof (IFLAGS_STR) - 1)
8755a5eeccaSmarks #define	ACCESS_TYPE_SIZE	7	/* if unknown */
876fa9e4066Sahrens #define	COLON_CNT		3
877fa9e4066Sahrens #define	PERMS_LEN		216
8785a5eeccaSmarks #define	ACE_ENTRY_SIZE	(ACE_ENTRYTYPLEN + ID_STR_MAX + PERMS_LEN + \
8795a5eeccaSmarks     ACCESS_TYPE_SIZE + IFLAGS_SIZE + COLON_CNT + APPENDED_ID_MAX)
880fa9e4066Sahrens 
881fa9e4066Sahrens static char *
8825a5eeccaSmarks ace_acltotext(acl_t *aceaclp, int flags)
883fa9e4066Sahrens {
884fa9e4066Sahrens 	ace_t		*aclp = aceaclp->acl_aclp;
885fa9e4066Sahrens 	int		aclcnt = aceaclp->acl_cnt;
8865a5eeccaSmarks 	int		i;
887b249c65cSmarks 	int		error = 0;
888fa9e4066Sahrens 	int		isdir = (aceaclp->acl_flags & ACL_IS_DIR);
889b249c65cSmarks 	dynaclstr_t 	*dstr;
890b249c65cSmarks 	char		*aclexport;
891fa9e4066Sahrens 
892fa9e4066Sahrens 	if (aclp == NULL)
893fa9e4066Sahrens 		return (NULL);
894fa9e4066Sahrens 
895b249c65cSmarks 	if ((dstr = malloc(sizeof (dynaclstr_t))) == NULL)
896b249c65cSmarks 		return (NULL);
897b249c65cSmarks 	dstr->d_bufsize = aclcnt * ACL_ENTRY_SIZE;
898b249c65cSmarks 	if ((dstr->d_aclexport = malloc(dstr->d_bufsize)) == NULL) {
899b249c65cSmarks 		free(dstr);
900b249c65cSmarks 		return (NULL);
901b249c65cSmarks 	}
902b249c65cSmarks 	*dstr->d_aclexport = '\0';
903b249c65cSmarks 	dstr->d_pos = 0;
904b249c65cSmarks 
905fa9e4066Sahrens 	for (i = 0; i < aclcnt; i++, aclp++) {
906fa9e4066Sahrens 
907b249c65cSmarks 		if (error = ace_type_txt(dstr, aclp, flags))
908b249c65cSmarks 			break;
909b249c65cSmarks 		if (error = ace_perm_txt(dstr, aclp->a_access_mask,
910b249c65cSmarks 		    aclp->a_flags, isdir, flags))
911b249c65cSmarks 			break;
912b249c65cSmarks 		if (error = ace_inherit_txt(dstr, aclp->a_flags, flags))
913b249c65cSmarks 			break;
914b249c65cSmarks 		if (error = ace_access_txt(dstr, aclp->a_type))
915b249c65cSmarks 			break;
916fa9e4066Sahrens 
9175a5eeccaSmarks 		if ((flags & ACL_APPEND_ID) &&
9185a5eeccaSmarks 		    (((aclp->a_flags & ACE_TYPE_FLAGS) == 0) ||
9195a5eeccaSmarks 		    ((aclp->a_flags & ACE_TYPE_FLAGS) ==
9205a5eeccaSmarks 		    ACE_IDENTIFIER_GROUP))) {
921b249c65cSmarks 			char id[ID_STR_MAX], *idstr;
922b249c65cSmarks 
923b249c65cSmarks 			if (error = str_append(dstr, ":"))
924b249c65cSmarks 				break;
9255a5eeccaSmarks 			id[ID_STR_MAX -1] = '\0'; /* null terminate buffer */
926b249c65cSmarks 			idstr = lltostr((aclp->a_who > MAXUID &&
927b249c65cSmarks 			    !(flags & ACL_NORESOLVE)) ? UID_NOBODY :
928b249c65cSmarks 			    aclp->a_who, &id[ID_STR_MAX - 1]);
929b249c65cSmarks 			if (error = str_append(dstr, idstr))
930b249c65cSmarks 				break;
9315a5eeccaSmarks 		}
9325a5eeccaSmarks 		if (i < aclcnt - 1) {
933b249c65cSmarks 			if (error = str_append(dstr, ","))
934b249c65cSmarks 				break;
935fa9e4066Sahrens 		}
936fa9e4066Sahrens 	}
937b249c65cSmarks 	if (error) {
938b249c65cSmarks 		if (dstr->d_aclexport)
939b249c65cSmarks 			free(dstr->d_aclexport);
940b249c65cSmarks 	} else {
941b249c65cSmarks 		aclexport = dstr->d_aclexport;
942b249c65cSmarks 	}
943b249c65cSmarks 	free(dstr);
944fa9e4066Sahrens 	return (aclexport);
945fa9e4066Sahrens }
946fa9e4066Sahrens 
9475a5eeccaSmarks char *
9485a5eeccaSmarks acl_totext(acl_t *aclp, int flags)
949fa9e4066Sahrens {
9505a5eeccaSmarks 	char *txtp;
951fa9e4066Sahrens 
952fa9e4066Sahrens 	if (aclp == NULL)
953fa9e4066Sahrens 		return (NULL);
954fa9e4066Sahrens 
955fa9e4066Sahrens 	switch (aclp->acl_type) {
956fa9e4066Sahrens 	case ACE_T:
9575a5eeccaSmarks 		txtp = ace_acltotext(aclp, flags);
9585a5eeccaSmarks 		break;
959fa9e4066Sahrens 	case ACLENT_T:
9605a5eeccaSmarks 		txtp = aclent_acltotext(aclp->acl_aclp, aclp->acl_cnt, flags);
9615a5eeccaSmarks 		break;
962fa9e4066Sahrens 	}
9635a5eeccaSmarks 
9645a5eeccaSmarks 	return (txtp);
965fa9e4066Sahrens }
966fa9e4066Sahrens 
967fa9e4066Sahrens int
968fa9e4066Sahrens acl_fromtext(const char *acltextp, acl_t **ret_aclp)
969fa9e4066Sahrens {
9705a5eeccaSmarks 	int error;
9715a5eeccaSmarks 	char *buf;
9725a5eeccaSmarks 
9735a5eeccaSmarks 	buf = malloc(strlen(acltextp) + 2);
9745a5eeccaSmarks 	if (buf == NULL)
9755a5eeccaSmarks 		return (EACL_MEM_ERROR);
9765a5eeccaSmarks 	strcpy(buf, acltextp);
9775a5eeccaSmarks 	strcat(buf, "\n");
9785a5eeccaSmarks 	yybuf = buf;
9795a5eeccaSmarks 	yyreset();
9805a5eeccaSmarks 	error = yyparse();
9815a5eeccaSmarks 	free(buf);
9825a5eeccaSmarks 
9835a5eeccaSmarks 	if (yyacl) {
9845a5eeccaSmarks 		if (error == 0)
9855a5eeccaSmarks 			*ret_aclp = yyacl;
9865a5eeccaSmarks 		else {
9875a5eeccaSmarks 			acl_free(yyacl);
9885a5eeccaSmarks 		}
9895a5eeccaSmarks 		yyacl = NULL;
9905a5eeccaSmarks 	}
9915a5eeccaSmarks 	return (error);
9925a5eeccaSmarks }
9935a5eeccaSmarks 
9945a5eeccaSmarks int
9955a5eeccaSmarks acl_parse(const char *acltextp, acl_t **aclp)
9965a5eeccaSmarks {
997fa9e4066Sahrens 	int error;
998fa9e4066Sahrens 
9995a5eeccaSmarks 	yyinteractive = 1;
10005a5eeccaSmarks 	error = acl_fromtext(acltextp, aclp);
10015a5eeccaSmarks 	yyinteractive = 0;
1002fa9e4066Sahrens 	return (error);
1003fa9e4066Sahrens }
10045a5eeccaSmarks 
10055a5eeccaSmarks static void
10065a5eeccaSmarks ace_compact_printacl(acl_t *aclp)
10075a5eeccaSmarks {
10085a5eeccaSmarks 	int cnt;
10095a5eeccaSmarks 	ace_t *acep;
1010b249c65cSmarks 	dynaclstr_t *dstr;
1011b249c65cSmarks 	int len;
10125a5eeccaSmarks 
1013b249c65cSmarks 	if ((dstr = malloc(sizeof (dynaclstr_t))) == NULL)
1014b249c65cSmarks 		return;
1015b249c65cSmarks 	dstr->d_bufsize = ACE_ENTRY_SIZE;
1016b249c65cSmarks 	if ((dstr->d_aclexport = malloc(dstr->d_bufsize)) == NULL) {
1017b249c65cSmarks 		free(dstr);
1018b249c65cSmarks 		return;
1019b249c65cSmarks 	}
1020b249c65cSmarks 	*dstr->d_aclexport = '\0';
1021b249c65cSmarks 
1022b249c65cSmarks 	dstr->d_pos = 0;
10235a5eeccaSmarks 	for (cnt = 0, acep = aclp->acl_aclp;
10245a5eeccaSmarks 	    cnt != aclp->acl_cnt; cnt++, acep++) {
1025b249c65cSmarks 		dstr->d_aclexport[0] = '\0';
1026b249c65cSmarks 		dstr->d_pos = 0;
1027b249c65cSmarks 
1028b249c65cSmarks 		if (ace_type_txt(dstr, acep, 0))
1029b249c65cSmarks 			break;
1030b249c65cSmarks 		len = strlen(&dstr->d_aclexport[0]);
1031b249c65cSmarks 		if (ace_perm_txt(dstr, acep->a_access_mask, acep->a_flags,
1032b249c65cSmarks 		    aclp->acl_flags & ACL_IS_DIR, ACL_COMPACT_FMT))
1033b249c65cSmarks 			break;
1034b249c65cSmarks 		if (ace_inherit_txt(dstr, acep->a_flags, ACL_COMPACT_FMT))
1035b249c65cSmarks 			break;
1036b249c65cSmarks 		if (ace_access_txt(dstr, acep->a_type) == -1)
1037b249c65cSmarks 			break;
1038b249c65cSmarks 		(void) printf("    %20.*s%s\n", len, dstr->d_aclexport,
1039b249c65cSmarks 		    &dstr->d_aclexport[len]);
10405a5eeccaSmarks 	}
1041b249c65cSmarks 
1042b249c65cSmarks 	if (dstr->d_aclexport)
1043b249c65cSmarks 		free(dstr->d_aclexport);
1044b249c65cSmarks 	free(dstr);
10455a5eeccaSmarks }
10465a5eeccaSmarks 
10475a5eeccaSmarks static void
10485a5eeccaSmarks ace_printacl(acl_t *aclp, int cols, int compact)
10495a5eeccaSmarks {
10505a5eeccaSmarks 	int  slot = 0;
10515a5eeccaSmarks 	char *token;
10525a5eeccaSmarks 	char *acltext;
10535a5eeccaSmarks 
10545a5eeccaSmarks 	if (compact) {
10555a5eeccaSmarks 		ace_compact_printacl(aclp);
10565a5eeccaSmarks 		return;
10575a5eeccaSmarks 	}
10585a5eeccaSmarks 
10595a5eeccaSmarks 	acltext = acl_totext(aclp, 0);
10605a5eeccaSmarks 
10615a5eeccaSmarks 	if (acltext == NULL)
10625a5eeccaSmarks 		return;
10635a5eeccaSmarks 
10645a5eeccaSmarks 	token = strtok(acltext, ",");
10655a5eeccaSmarks 	if (token == NULL) {
10665a5eeccaSmarks 		free(acltext);
10675a5eeccaSmarks 		return;
10685a5eeccaSmarks 	}
10695a5eeccaSmarks 
10705a5eeccaSmarks 	do {
10715a5eeccaSmarks 		(void) printf("     %d:", slot++);
10725a5eeccaSmarks 		split_line(token, cols - 5);
10735a5eeccaSmarks 	} while (token = strtok(NULL, ","));
10745a5eeccaSmarks 	free(acltext);
10755a5eeccaSmarks }
10765a5eeccaSmarks 
10775a5eeccaSmarks /*
10785a5eeccaSmarks  * pretty print an ACL.
10795a5eeccaSmarks  * For aclent_t ACL's the format is
10805a5eeccaSmarks  * similar to the old format used by getfacl,
10815a5eeccaSmarks  * with the addition of adding a "slot" number
10825a5eeccaSmarks  * before each entry.
10835a5eeccaSmarks  *
10845a5eeccaSmarks  * for ace_t ACL's the cols variable will break up
10855a5eeccaSmarks  * the long lines into multiple lines and will also
10865a5eeccaSmarks  * print a "slot" number.
10875a5eeccaSmarks  */
10885a5eeccaSmarks void
10895a5eeccaSmarks acl_printacl(acl_t *aclp, int cols, int compact)
10905a5eeccaSmarks {
10915a5eeccaSmarks 
10925a5eeccaSmarks 	switch (aclp->acl_type) {
10935a5eeccaSmarks 	case ACLENT_T:
10945a5eeccaSmarks 		aclent_printacl(aclp);
10955a5eeccaSmarks 		break;
10965a5eeccaSmarks 	case ACE_T:
10975a5eeccaSmarks 		ace_printacl(aclp, cols, compact);
10985a5eeccaSmarks 		break;
10995a5eeccaSmarks 	}
11005a5eeccaSmarks }
11015a5eeccaSmarks 
11025a5eeccaSmarks typedef struct value_table {
11035a5eeccaSmarks 	char		p_letter; /* perm letter such as 'r' */
11045a5eeccaSmarks 	uint32_t	p_value; /* value for perm when pletter found */
11055a5eeccaSmarks } value_table_t;
11065a5eeccaSmarks 
11075a5eeccaSmarks /*
1108da6c28aaSamw  * The permission tables are laid out in positional order
11095a5eeccaSmarks  * a '-' character will indicate a permission at a given
11105a5eeccaSmarks  * position is not specified.  The '-' is not part of the
11115a5eeccaSmarks  * table, but will be checked for in the permission computation
11125a5eeccaSmarks  * routine.
11135a5eeccaSmarks  */
1114da6c28aaSamw value_table_t ace_perm_table[] = {
11155a5eeccaSmarks 	{ 'r', ACE_READ_DATA},
11165a5eeccaSmarks 	{ 'w', ACE_WRITE_DATA},
11175a5eeccaSmarks 	{ 'x', ACE_EXECUTE},
11185a5eeccaSmarks 	{ 'p', ACE_APPEND_DATA},
11195a5eeccaSmarks 	{ 'd', ACE_DELETE},
11205a5eeccaSmarks 	{ 'D', ACE_DELETE_CHILD},
11215a5eeccaSmarks 	{ 'a', ACE_READ_ATTRIBUTES},
11225a5eeccaSmarks 	{ 'A', ACE_WRITE_ATTRIBUTES},
11235a5eeccaSmarks 	{ 'R', ACE_READ_NAMED_ATTRS},
11245a5eeccaSmarks 	{ 'W', ACE_WRITE_NAMED_ATTRS},
11255a5eeccaSmarks 	{ 'c', ACE_READ_ACL},
11265a5eeccaSmarks 	{ 'C', ACE_WRITE_ACL},
11275a5eeccaSmarks 	{ 'o', ACE_WRITE_OWNER},
11285a5eeccaSmarks 	{ 's', ACE_SYNCHRONIZE}
11295a5eeccaSmarks };
11305a5eeccaSmarks 
1131da6c28aaSamw #define	ACE_PERM_COUNT (sizeof (ace_perm_table) / sizeof (value_table_t))
11325a5eeccaSmarks 
1133da6c28aaSamw value_table_t aclent_perm_table[] = {
11345a5eeccaSmarks 	{ 'r', S_IROTH},
11355a5eeccaSmarks 	{ 'w', S_IWOTH},
11365a5eeccaSmarks 	{ 'x', S_IXOTH}
11375a5eeccaSmarks };
11385a5eeccaSmarks 
1139da6c28aaSamw #define	ACLENT_PERM_COUNT (sizeof (aclent_perm_table) / sizeof (value_table_t))
1140da6c28aaSamw 
1141da6c28aaSamw value_table_t inherit_table[] = {
11425a5eeccaSmarks 	{'f', ACE_FILE_INHERIT_ACE},
11435a5eeccaSmarks 	{'d', ACE_DIRECTORY_INHERIT_ACE},
11445a5eeccaSmarks 	{'i', ACE_INHERIT_ONLY_ACE},
11455a5eeccaSmarks 	{'n', ACE_NO_PROPAGATE_INHERIT_ACE},
11465a5eeccaSmarks 	{'S', ACE_SUCCESSFUL_ACCESS_ACE_FLAG},
1147da6c28aaSamw 	{'F', ACE_FAILED_ACCESS_ACE_FLAG},
1148da6c28aaSamw 	{'I', ACE_INHERITED_ACE}
11495a5eeccaSmarks };
11505a5eeccaSmarks 
1151da6c28aaSamw #define	IFLAG_COUNT (sizeof (inherit_table) / sizeof (value_table_t))
1152bf8b6031Smarks #define	IFLAG_COUNT_V1 6 /* Older version compatibility */
1153da6c28aaSamw 
11545a5eeccaSmarks /*
11555a5eeccaSmarks  * compute value from a permission table or inheritance table
11565a5eeccaSmarks  * based on string passed in.  If positional is set then
11575a5eeccaSmarks  * string must match order in permtab, otherwise any order
11585a5eeccaSmarks  * is allowed.
11595a5eeccaSmarks  */
11605a5eeccaSmarks int
11615a5eeccaSmarks compute_values(value_table_t *permtab, int count,
11625a5eeccaSmarks     char *permstr, int positional, uint32_t *mask)
11635a5eeccaSmarks {
11645a5eeccaSmarks 	uint32_t perm_val = 0;
11655a5eeccaSmarks 	char *pstr;
11665a5eeccaSmarks 	int i, found;
11675a5eeccaSmarks 
11685a5eeccaSmarks 	if (count < 0)
11695a5eeccaSmarks 		return (1);
11705a5eeccaSmarks 
11715a5eeccaSmarks 	if (positional) {
11725a5eeccaSmarks 		for (i = 0, pstr = permstr; i != count && pstr &&
11735a5eeccaSmarks 		    *pstr; i++, pstr++) {
11745a5eeccaSmarks 			if (*pstr == permtab[i].p_letter) {
11755a5eeccaSmarks 				perm_val |= permtab[i].p_value;
11765a5eeccaSmarks 			} else if (*pstr != '-') {
11775a5eeccaSmarks 				return (1);
11785a5eeccaSmarks 			}
11795a5eeccaSmarks 		}
11805a5eeccaSmarks 	} else {  /* random order single letters with no '-' */
11815a5eeccaSmarks 		for (pstr = permstr; pstr && *pstr; pstr++) {
11825a5eeccaSmarks 			for (found = 0, i = 0; i != count; i++) {
11835a5eeccaSmarks 				if (*pstr == permtab[i].p_letter) {
11845a5eeccaSmarks 					perm_val |= permtab[i].p_value;
11855a5eeccaSmarks 					found = 1;
11865a5eeccaSmarks 					break;
11875a5eeccaSmarks 				}
11885a5eeccaSmarks 			}
11895a5eeccaSmarks 			if (found == 0)
11905a5eeccaSmarks 				return (1);
11915a5eeccaSmarks 		}
11925a5eeccaSmarks 	}
11935a5eeccaSmarks 
11945a5eeccaSmarks 	*mask = perm_val;
11955a5eeccaSmarks 	return (0);
11965a5eeccaSmarks }
11975a5eeccaSmarks 
1198bf8b6031Smarks 
1199bf8b6031Smarks int
1200bf8b6031Smarks ace_inherit_helper(char *str, uint32_t *imask, int table_length)
1201bf8b6031Smarks {
1202bf8b6031Smarks 	int rc = 0;
1203bf8b6031Smarks 
1204bf8b6031Smarks 	if (strlen(str) == table_length) {
1205bf8b6031Smarks 		/*
1206bf8b6031Smarks 		 * If the string == table_length then first check to see it's
1207bf8b6031Smarks 		 * in positional format.  If that fails then see if it's in
1208bf8b6031Smarks 		 * non-positional format.
1209bf8b6031Smarks 		 */
1210bf8b6031Smarks 		if (compute_values(inherit_table, table_length, str,
1211bf8b6031Smarks 		    1, imask) && compute_values(inherit_table,
1212bf8b6031Smarks 		    table_length, str, 0, imask)) {
1213bf8b6031Smarks 			rc = 1;
1214bf8b6031Smarks 		}
1215bf8b6031Smarks 	} else {
1216bf8b6031Smarks 		rc = compute_values(inherit_table, table_length, str, 0, imask);
1217bf8b6031Smarks 	}
1218bf8b6031Smarks 
1219bf8b6031Smarks 	return (rc ? EACL_INHERIT_ERROR : 0);
1220bf8b6031Smarks }
1221bf8b6031Smarks 
12225a5eeccaSmarks /*
12235a5eeccaSmarks  * compute value for inheritance flags.
12245a5eeccaSmarks  */
12255a5eeccaSmarks int
12265a5eeccaSmarks compute_ace_inherit(char *str, uint32_t *imask)
12275a5eeccaSmarks {
1228bf8b6031Smarks 	int rc = 0;
12295a5eeccaSmarks 
1230bf8b6031Smarks 	rc = ace_inherit_helper(str, imask, IFLAG_COUNT);
12315a5eeccaSmarks 
1232bf8b6031Smarks 	if (rc && strlen(str) != IFLAG_COUNT) {
12335a5eeccaSmarks 
1234bf8b6031Smarks 		/* is it an old formatted inherit string? */
1235bf8b6031Smarks 		rc = ace_inherit_helper(str, imask, IFLAG_COUNT_V1);
1236bf8b6031Smarks 	}
12375a5eeccaSmarks 
1238bf8b6031Smarks 	return (rc);
12395a5eeccaSmarks }
12405a5eeccaSmarks 
12415a5eeccaSmarks 
12425a5eeccaSmarks /*
12435a5eeccaSmarks  * compute value for ACE permissions.
12445a5eeccaSmarks  */
12455a5eeccaSmarks int
12465a5eeccaSmarks compute_ace_perms(char *str, uint32_t *mask)
12475a5eeccaSmarks {
12485a5eeccaSmarks 	int positional = 0;
12495a5eeccaSmarks 	int error;
12505a5eeccaSmarks 
12515a5eeccaSmarks 	if (strlen(str) == ACE_PERM_COUNT)
12525a5eeccaSmarks 		positional = 1;
12535a5eeccaSmarks 
12545a5eeccaSmarks 	error = compute_values(ace_perm_table, ACE_PERM_COUNT,
12555a5eeccaSmarks 	    str, positional, mask);
12565a5eeccaSmarks 
12575a5eeccaSmarks 	if (error && positional) {
12585a5eeccaSmarks 		/*
12595a5eeccaSmarks 		 * If positional was set, then make sure permissions
12605a5eeccaSmarks 		 * aren't actually valid in non positional case where
12615a5eeccaSmarks 		 * all permissions are specified, just in random order.
12625a5eeccaSmarks 		 */
12635a5eeccaSmarks 		error = compute_values(ace_perm_table,
12645a5eeccaSmarks 		    ACE_PERM_COUNT, str, 0, mask);
12655a5eeccaSmarks 	}
12665a5eeccaSmarks 	if (error)
12675a5eeccaSmarks 		error = EACL_PERM_MASK_ERROR;
12685a5eeccaSmarks 
12695a5eeccaSmarks 	return (error);
12705a5eeccaSmarks }
12715a5eeccaSmarks 
12725a5eeccaSmarks 
12735a5eeccaSmarks 
12745a5eeccaSmarks /*
12755a5eeccaSmarks  * compute values for aclent permissions.
12765a5eeccaSmarks  */
12775a5eeccaSmarks int
12785a5eeccaSmarks compute_aclent_perms(char *str, o_mode_t *mask)
12795a5eeccaSmarks {
12805a5eeccaSmarks 	int error;
12815a5eeccaSmarks 	uint32_t pmask;
12825a5eeccaSmarks 
12835a5eeccaSmarks 	if (strlen(str) != ACLENT_PERM_COUNT)
12845a5eeccaSmarks 		return (EACL_PERM_MASK_ERROR);
12855a5eeccaSmarks 
12865a5eeccaSmarks 	*mask = 0;
12875a5eeccaSmarks 	error = compute_values(aclent_perm_table, ACLENT_PERM_COUNT,
12885a5eeccaSmarks 	    str, 1, &pmask);
12895a5eeccaSmarks 	if (error == 0) {
12905a5eeccaSmarks 		*mask = (o_mode_t)pmask;
12915a5eeccaSmarks 	} else
12925a5eeccaSmarks 		error = EACL_PERM_MASK_ERROR;
12935a5eeccaSmarks 	return (error);
12945a5eeccaSmarks }
12955a5eeccaSmarks 
12965a5eeccaSmarks /*
12975a5eeccaSmarks  * determine ACE permissions.
12985a5eeccaSmarks  */
12995a5eeccaSmarks int
13005a5eeccaSmarks ace_perm_mask(struct acl_perm_type *aclperm, uint32_t *mask)
13015a5eeccaSmarks {
13025a5eeccaSmarks 	int error;
13035a5eeccaSmarks 
13045a5eeccaSmarks 	if (aclperm->perm_style == PERM_TYPE_EMPTY) {
13055a5eeccaSmarks 		*mask = 0;
13065a5eeccaSmarks 		return (0);
13075a5eeccaSmarks 	}
13085a5eeccaSmarks 
13095a5eeccaSmarks 	if (aclperm->perm_style == PERM_TYPE_ACE) {
13105a5eeccaSmarks 		*mask = aclperm->perm_val;
13115a5eeccaSmarks 		return (0);
13125a5eeccaSmarks 	}
13135a5eeccaSmarks 
13145a5eeccaSmarks 	error = compute_ace_perms(aclperm->perm_str, mask);
13155a5eeccaSmarks 	if (error) {
13165b233e2dSmarks 		acl_error(dgettext(TEXT_DOMAIN,
13175b233e2dSmarks 		    "Invalid permission(s) '%s' specified\n"),
13185a5eeccaSmarks 		    aclperm->perm_str);
13195a5eeccaSmarks 		return (EACL_PERM_MASK_ERROR);
13205a5eeccaSmarks 	}
13215a5eeccaSmarks 
13225a5eeccaSmarks 	return (0);
13235a5eeccaSmarks }
1324