10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51507Sgjelinek  * Common Development and Distribution License (the "License").
61507Sgjelinek  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221420Smarks  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate /*LINTLIBRARY*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <grp.h>
300Sstevel@tonic-gate #include <pwd.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <limits.h>
330Sstevel@tonic-gate #include <stdlib.h>
34789Sahrens #include <errno.h>
350Sstevel@tonic-gate #include <sys/param.h>
360Sstevel@tonic-gate #include <sys/types.h>
371420Smarks #include <sys/stat.h>
380Sstevel@tonic-gate #include <sys/acl.h>
39789Sahrens #include <aclutils.h>
401420Smarks 
411420Smarks #define	ID_STR_MAX	20	/* digits in LONG_MAX */
42789Sahrens 
431420Smarks #define	APPENDED_ID_MAX	ID_STR_MAX + 1		/* id + colon */
441420Smarks /*
451420Smarks  * yyinteractive controls whether yyparse should print out
461420Smarks  * error messages to stderr, and whether or not id's should be
471420Smarks  * allowed from acl_fromtext().
481420Smarks  */
491420Smarks int	yyinteractive;
501420Smarks acl_t	*yyacl;
511420Smarks char	*yybuf;
52789Sahrens 
53789Sahrens extern acl_t *acl_alloc(enum acl_type);
540Sstevel@tonic-gate 
55922Shm123892 
560Sstevel@tonic-gate struct dynaclstr {
570Sstevel@tonic-gate 	size_t bufsize;		/* current size of aclexport */
580Sstevel@tonic-gate 	char *aclexport;
590Sstevel@tonic-gate };
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static char *strappend(char *, char *);
620Sstevel@tonic-gate static char *convert_perm(char *, o_mode_t);
630Sstevel@tonic-gate static int increase_length(struct dynaclstr *, size_t);
640Sstevel@tonic-gate 
651420Smarks static void
661420Smarks aclent_perms(int perm, char *txt_perms)
671420Smarks {
681420Smarks 	if (perm & S_IROTH)
691420Smarks 		txt_perms[0] = 'r';
701420Smarks 	else
711420Smarks 		txt_perms[0] = '-';
721420Smarks 	if (perm & S_IWOTH)
731420Smarks 		txt_perms[1] = 'w';
741420Smarks 	else
751420Smarks 		txt_perms[1] = '-';
761420Smarks 	if (perm & S_IXOTH)
771420Smarks 		txt_perms[2] = 'x';
781420Smarks 	else
791420Smarks 		txt_perms[2] = '-';
801420Smarks 	txt_perms[3] = '\0';
811420Smarks }
821420Smarks 
831420Smarks static char *
84*1515Sgjelinek pruname(uid_t uid, char *uidp, int noresolve)
851420Smarks {
86*1515Sgjelinek 	struct passwd	*passwdp = NULL;
871420Smarks 
88*1515Sgjelinek 	if (noresolve == 0)
89*1515Sgjelinek 		passwdp = getpwuid(uid);
901420Smarks 	if (passwdp == (struct passwd *)NULL) {
911420Smarks 		/* could not get passwd information: display uid instead */
921420Smarks 		(void) sprintf(uidp, "%ld", (long)uid);
931420Smarks 		return (uidp);
941420Smarks 	} else
951420Smarks 		return (passwdp->pw_name);
961420Smarks }
971420Smarks 
981420Smarks static char *
99*1515Sgjelinek prgname(gid_t gid, char *gidp, int noresolve)
1001420Smarks {
101*1515Sgjelinek 	struct group	*groupp = NULL;
1021420Smarks 
103*1515Sgjelinek 	if (noresolve == 0)
104*1515Sgjelinek 		groupp = getgrgid(gid);
1051420Smarks 	if (groupp == (struct group *)NULL) {
1061420Smarks 		/* could not get group information: display gid instead */
1071420Smarks 		(void) sprintf(gidp, "%ld", (long)gid);
1081420Smarks 		return (gidp);
1091420Smarks 	} else
1101420Smarks 		return (groupp->gr_name);
1111420Smarks }
1121420Smarks static void
1131420Smarks aclent_printacl(acl_t *aclp)
114789Sahrens {
1151420Smarks 	aclent_t *tp;
1161420Smarks 	int aclcnt;
1171420Smarks 	int mask;
1181420Smarks 	int slot = 0;
1191420Smarks 	char perm[4];
1201420Smarks 	char uidp[10];
1211420Smarks 	char gidp[10];
1221420Smarks 
1231420Smarks 	/* display ACL: assume it is sorted. */
1241420Smarks 	aclcnt = aclp->acl_cnt;
1251420Smarks 	for (tp = aclp->acl_aclp; tp && aclcnt--; tp++) {
1261420Smarks 		if (tp->a_type == CLASS_OBJ)
1271420Smarks 			mask = tp->a_perm;
1281420Smarks 	}
1291420Smarks 	aclcnt = aclp->acl_cnt;
1301420Smarks 	for (tp = aclp->acl_aclp; aclcnt--; tp++) {
1311420Smarks 		(void) printf("     %d:", slot++);
1321420Smarks 		switch (tp->a_type) {
1331420Smarks 		case USER:
1341420Smarks 			aclent_perms(tp->a_perm, perm);
1351420Smarks 			(void) printf("user:%s:%s\t\t",
136*1515Sgjelinek 			    pruname(tp->a_id, uidp, 0), perm);
1371420Smarks 			aclent_perms((tp->a_perm & mask), perm);
1381420Smarks 			(void) printf("#effective:%s\n", perm);
1391420Smarks 			break;
1401420Smarks 		case USER_OBJ:
1411420Smarks 			/* no need to display uid */
1421420Smarks 			aclent_perms(tp->a_perm, perm);
1431420Smarks 			(void) printf("user::%s\n", perm);
1441420Smarks 			break;
1451420Smarks 		case GROUP:
1461420Smarks 			aclent_perms(tp->a_perm, perm);
1471420Smarks 			(void) printf("group:%s:%s\t\t",
148*1515Sgjelinek 			    prgname(tp->a_id, gidp, 0), perm);
1491420Smarks 			aclent_perms(tp->a_perm & mask, perm);
1501420Smarks 			(void) printf("#effective:%s\n", perm);
1511420Smarks 			break;
1521420Smarks 		case GROUP_OBJ:
1531420Smarks 			aclent_perms(tp->a_perm, perm);
1541420Smarks 			(void) printf("group::%s\t\t", perm);
1551420Smarks 			aclent_perms(tp->a_perm & mask, perm);
1561420Smarks 			(void) printf("#effective:%s\n", perm);
1571420Smarks 			break;
1581420Smarks 		case CLASS_OBJ:
1591420Smarks 			aclent_perms(tp->a_perm, perm);
1601420Smarks 			(void) printf("mask:%s\n", perm);
1611420Smarks 			break;
1621420Smarks 		case OTHER_OBJ:
1631420Smarks 			aclent_perms(tp->a_perm, perm);
1641420Smarks 			(void) printf("other:%s\n", perm);
1651420Smarks 			break;
1661420Smarks 		case DEF_USER:
1671420Smarks 			aclent_perms(tp->a_perm, perm);
1681420Smarks 			(void) printf("default:user:%s:%s\n",
169*1515Sgjelinek 			    pruname(tp->a_id, uidp, 0), perm);
1701420Smarks 			break;
1711420Smarks 		case DEF_USER_OBJ:
1721420Smarks 			aclent_perms(tp->a_perm, perm);
1731420Smarks 			(void) printf("default:user::%s\n", perm);
1741420Smarks 			break;
1751420Smarks 		case DEF_GROUP:
1761420Smarks 			aclent_perms(tp->a_perm, perm);
1771420Smarks 			(void) printf("default:group:%s:%s\n",
178*1515Sgjelinek 			    prgname(tp->a_id, gidp, 0), perm);
1791420Smarks 			break;
1801420Smarks 		case DEF_GROUP_OBJ:
1811420Smarks 			aclent_perms(tp->a_perm, perm);
1821420Smarks 			(void) printf("default:group::%s\n", perm);
1831420Smarks 			break;
1841420Smarks 		case DEF_CLASS_OBJ:
1851420Smarks 			aclent_perms(tp->a_perm, perm);
1861420Smarks 			(void) printf("default:mask:%s\n", perm);
1871420Smarks 			break;
1881420Smarks 		case DEF_OTHER_OBJ:
1891420Smarks 			aclent_perms(tp->a_perm, perm);
1901420Smarks 			(void) printf("default:other:%s\n", perm);
1911420Smarks 			break;
1921420Smarks 		default:
1931420Smarks 			(void) fprintf(stderr,
1941420Smarks 			    gettext("unrecognized entry\n"));
1951420Smarks 			break;
1961420Smarks 		}
1971420Smarks 	}
1981420Smarks }
1991420Smarks 
2001420Smarks static void
2011420Smarks split_line(char *str, int cols)
2021420Smarks {
2031420Smarks 	char *ptr;
2041420Smarks 	int len;
2051420Smarks 	int i;
2061420Smarks 	int last_split;
2071420Smarks 	char *pad = "";
2081420Smarks 	int pad_len;
2091420Smarks 
2101420Smarks 	len = strlen(str);
2111420Smarks 	ptr = str;
2121420Smarks 	pad_len = 0;
2131420Smarks 
2141420Smarks 	ptr = str;
2151420Smarks 	last_split = 0;
2161420Smarks 	for (i = 0; i != len; i++) {
2171420Smarks 		if ((i + pad_len + 4) >= cols) {
2181420Smarks 			(void) printf("%s%.*s\n", pad, last_split, ptr);
2191420Smarks 			ptr = &ptr[last_split];
2201420Smarks 			len = strlen(ptr);
2211420Smarks 			i = 0;
2221420Smarks 			pad_len = 4;
2231420Smarks 			pad = "         ";
2241420Smarks 		} else {
2251420Smarks 			if (ptr[i] == '/' || ptr[i] == ':') {
2261420Smarks 				last_split = i;
2271420Smarks 			}
2281420Smarks 		}
2291420Smarks 	}
2301420Smarks 	if (i == len) {
2311420Smarks 		(void) printf("%s%s\n", pad, ptr);
2321420Smarks 	}
2331420Smarks }
2341420Smarks 
2351420Smarks #define	OWNERAT_TXT	"owner@"
2361420Smarks #define	GROUPAT_TXT	"group@"
2371420Smarks #define	EVERYONEAT_TXT	"everyone@"
2381420Smarks #define	GROUP_TXT	"group:"
2391420Smarks #define	USER_TXT	"user:"
2401420Smarks 
2411420Smarks char *
242*1515Sgjelinek ace_type_txt(char *buf, char **endp, ace_t *acep, int flags)
2431420Smarks {
2441420Smarks 
2451420Smarks 	char idp[10];
2461420Smarks 
2471420Smarks 	if (buf == NULL)
2481420Smarks 		return (NULL);
2491420Smarks 
2501420Smarks 	switch (acep->a_flags & ACE_TYPE_FLAGS) {
2511420Smarks 	case ACE_OWNER:
2521420Smarks 		strcpy(buf, OWNERAT_TXT);
2531420Smarks 		*endp = buf + sizeof (OWNERAT_TXT) - 1;
2541420Smarks 		break;
2551420Smarks 
2561420Smarks 	case ACE_GROUP|ACE_IDENTIFIER_GROUP:
2571420Smarks 		strcpy(buf, GROUPAT_TXT);
2581420Smarks 		*endp = buf + sizeof (GROUPAT_TXT) - 1;
2591420Smarks 		break;
2601420Smarks 
2611420Smarks 	case ACE_IDENTIFIER_GROUP:
2621420Smarks 		strcpy(buf, GROUP_TXT);
263*1515Sgjelinek 		strcat(buf, prgname(acep->a_who, idp, flags & ACL_NORESOLVE));
2641420Smarks 		*endp = buf + strlen(buf);
2651420Smarks 		break;
2661420Smarks 
2671420Smarks 	case ACE_EVERYONE:
2681420Smarks 		strcpy(buf, EVERYONEAT_TXT);
2691420Smarks 		*endp = buf + sizeof (EVERYONEAT_TXT) - 1;
2701420Smarks 		break;
2711420Smarks 
2721420Smarks 	case 0:
2731420Smarks 		strcpy(buf, USER_TXT);
274*1515Sgjelinek 		strcat(buf, pruname(acep->a_who, idp, flags & ACL_NORESOLVE));
2751420Smarks 		*endp = buf + strlen(buf);
2761420Smarks 		break;
2771420Smarks 	}
2781420Smarks 
2791420Smarks 	return (buf);
2801420Smarks }
2811420Smarks 
2821420Smarks #define	READ_DATA_TXT	"read_data/"
2831420Smarks #define	WRITE_DATA_TXT	"write_data/"
2841420Smarks #define	EXECUTE_TXT	"execute/"
2851420Smarks #define	READ_XATTR_TXT	"read_xattr/"
2861420Smarks #define	WRITE_XATTR_TXT	"write_xattr/"
2871420Smarks #define	READ_ATTRIBUTES_TXT "read_attributes/"
2881420Smarks #define	WRITE_ATTRIBUTES_TXT "write_attributes/"
2891420Smarks #define	DELETE_TXT	"delete/"
2901420Smarks #define	DELETE_CHILD_TXT "delete_child/"
2911420Smarks #define	WRITE_OWNER_TXT "write_owner/"
2921420Smarks #define	READ_ACL_TXT	"read_acl/"
2931420Smarks #define	WRITE_ACL_TXT	"write_acl/"
2941420Smarks #define	APPEND_DATA_TXT "append_data/"
2951420Smarks #define	READ_DIR_TXT	"list_directory/read_data/"
2961420Smarks #define	ADD_DIR_TXT	"add_subdirectory/append_data/"
2971420Smarks #define	ADD_FILE_TXT	"add_file/write_data/"
2981420Smarks #define	SYNCHRONIZE_TXT "synchronize"	/* not slash on this one */
2991420Smarks 
3001420Smarks char *
3011420Smarks ace_perm_txt(char *buf, char **endp, uint32_t mask,
3021420Smarks     uint32_t iflags, int isdir, int flags)
3031420Smarks {
3041420Smarks 	char *lend = buf;		/* local end */
3051420Smarks 
3061420Smarks 	if (buf == NULL)
3071420Smarks 		return (NULL);
3081420Smarks 
3091420Smarks 	if (flags & ACL_COMPACT_FMT) {
310789Sahrens 
3111420Smarks 		if (mask & ACE_READ_DATA)
3121420Smarks 			buf[0] = 'r';
3131420Smarks 		else
3141420Smarks 			buf[0] = '-';
3151420Smarks 		if (mask & ACE_WRITE_DATA)
3161420Smarks 			buf[1] = 'w';
3171420Smarks 		else
3181420Smarks 			buf[1] = '-';
3191420Smarks 		if (mask & ACE_EXECUTE)
3201420Smarks 			buf[2] = 'x';
3211420Smarks 		else
3221420Smarks 			buf[2] = '-';
3231420Smarks 		if (mask & ACE_APPEND_DATA)
3241420Smarks 			buf[3] = 'p';
3251420Smarks 		else
3261420Smarks 			buf[3] = '-';
3271420Smarks 		if (mask & ACE_DELETE)
3281420Smarks 			buf[4] = 'd';
3291420Smarks 		else
3301420Smarks 			buf[4] = '-';
3311420Smarks 		if (mask & ACE_DELETE_CHILD)
3321420Smarks 			buf[5] = 'D';
3331420Smarks 		else
3341420Smarks 			buf[5] = '-';
3351420Smarks 		if (mask & ACE_READ_ATTRIBUTES)
3361420Smarks 			buf[6] = 'a';
3371420Smarks 		else
3381420Smarks 			buf[6] = '-';
3391420Smarks 		if (mask & ACE_WRITE_ATTRIBUTES)
3401420Smarks 			buf[7] = 'A';
3411420Smarks 		else
3421420Smarks 			buf[7] = '-';
3431420Smarks 		if (mask & ACE_READ_NAMED_ATTRS)
3441420Smarks 			buf[8] = 'R';
3451420Smarks 		else
3461420Smarks 			buf[8] = '-';
3471420Smarks 		if (mask & ACE_WRITE_NAMED_ATTRS)
3481420Smarks 			buf[9] = 'W';
3491420Smarks 		else
3501420Smarks 			buf[9] = '-';
3511420Smarks 		if (mask & ACE_READ_ACL)
3521420Smarks 			buf[10] = 'c';
3531420Smarks 		else
3541420Smarks 			buf[10] = '-';
3551420Smarks 		if (mask & ACE_WRITE_ACL)
3561420Smarks 			buf[11] = 'C';
3571420Smarks 		else
3581420Smarks 			buf[11] = '-';
3591420Smarks 		if (mask & ACE_WRITE_OWNER)
3601420Smarks 			buf[12] = 'o';
3611420Smarks 		else
3621420Smarks 			buf[12] = '-';
3631420Smarks 		if (mask & ACE_SYNCHRONIZE)
3641420Smarks 			buf[13] = 's';
3651420Smarks 		else
3661420Smarks 			buf[13] = '-';
3671420Smarks 		buf[14] = '\0';
3681420Smarks 		*endp = buf + 14;
3691420Smarks 		return (buf);
3701420Smarks 	} else {
3711420Smarks 		/*
3721420Smarks 		 * If ACE is a directory, but inheritance indicates its
3731420Smarks 		 * for a file then print permissions for file rather than
3741420Smarks 		 * dir.
3751420Smarks 		 */
3761420Smarks 		if (isdir) {
3771420Smarks 			if (mask & ACE_LIST_DIRECTORY) {
3781420Smarks 				if (iflags == ACE_FILE_INHERIT_ACE) {
3791420Smarks 					strcpy(lend, READ_DATA_TXT);
3801420Smarks 					lend += sizeof (READ_DATA_TXT) - 1;
3811420Smarks 				} else {
3821420Smarks 					strcpy(lend, READ_DIR_TXT);
3831420Smarks 					lend += sizeof (READ_DIR_TXT) - 1;
3841420Smarks 				}
3851420Smarks 			}
3861420Smarks 			if (mask & ACE_ADD_FILE) {
3871420Smarks 				if (iflags == ACE_FILE_INHERIT_ACE) {
3881420Smarks 					strcpy(lend, WRITE_DATA_TXT);
3891420Smarks 					lend += sizeof (WRITE_DATA_TXT) - 1;
3901420Smarks 				} else {
3911420Smarks 					strcpy(lend, ADD_FILE_TXT);
3921420Smarks 					lend +=
3931420Smarks 					    sizeof (ADD_FILE_TXT) -1;
3941420Smarks 				}
3951420Smarks 			}
3961420Smarks 			if (mask & ACE_ADD_SUBDIRECTORY) {
3971420Smarks 				if (iflags == ACE_FILE_INHERIT_ACE) {
3981420Smarks 					strcpy(lend, APPEND_DATA_TXT);
3991420Smarks 					lend += sizeof (APPEND_DATA_TXT) - 1;
4001420Smarks 				} else {
4011420Smarks 					strcpy(lend, ADD_DIR_TXT);
4021420Smarks 					lend += sizeof (ADD_DIR_TXT) - 1;
4031420Smarks 				}
4041420Smarks 			}
4051420Smarks 		} else {
4061420Smarks 			if (mask & ACE_READ_DATA) {
4071420Smarks 				strcpy(lend, READ_DATA_TXT);
4081420Smarks 				lend += sizeof (READ_DATA_TXT) - 1;
4091420Smarks 			}
4101420Smarks 			if (mask & ACE_WRITE_DATA) {
4111420Smarks 				strcpy(lend, WRITE_DATA_TXT);
4121420Smarks 				lend += sizeof (WRITE_DATA_TXT) - 1;
4131420Smarks 			}
4141420Smarks 			if (mask & ACE_APPEND_DATA) {
4151420Smarks 				strcpy(lend, APPEND_DATA_TXT);
4161420Smarks 				lend += sizeof (APPEND_DATA_TXT) - 1;
4171420Smarks 			}
4181420Smarks 		}
4191420Smarks 		if (mask & ACE_READ_NAMED_ATTRS) {
4201420Smarks 			strcpy(lend, READ_XATTR_TXT);
4211420Smarks 			lend += sizeof (READ_XATTR_TXT) - 1;
4221420Smarks 		}
4231420Smarks 		if (mask & ACE_WRITE_NAMED_ATTRS) {
4241420Smarks 			strcpy(lend, WRITE_XATTR_TXT);
4251420Smarks 			lend += sizeof (WRITE_XATTR_TXT) - 1;
4261420Smarks 		}
4271420Smarks 		if (mask & ACE_EXECUTE) {
4281420Smarks 			strcpy(lend, EXECUTE_TXT);
4291420Smarks 			lend += sizeof (EXECUTE_TXT) - 1;
4301420Smarks 		}
4311420Smarks 		if (mask & ACE_DELETE_CHILD) {
4321420Smarks 			strcpy(lend, DELETE_CHILD_TXT);
4331420Smarks 			lend += sizeof (DELETE_CHILD_TXT) - 1;
4341420Smarks 		}
4351420Smarks 		if (mask & ACE_READ_ATTRIBUTES) {
4361420Smarks 			strcpy(lend, READ_ATTRIBUTES_TXT);
4371420Smarks 			lend += sizeof (READ_ATTRIBUTES_TXT) - 1;
4381420Smarks 		}
4391420Smarks 		if (mask & ACE_WRITE_ATTRIBUTES) {
4401420Smarks 			strcpy(lend, WRITE_ATTRIBUTES_TXT);
4411420Smarks 			lend += sizeof (WRITE_ATTRIBUTES_TXT) - 1;
4421420Smarks 		}
4431420Smarks 		if (mask & ACE_DELETE) {
4441420Smarks 			strcpy(lend, DELETE_TXT);
4451420Smarks 			lend += sizeof (DELETE_TXT) - 1;
4461420Smarks 		}
4471420Smarks 		if (mask & ACE_READ_ACL) {
4481420Smarks 			strcpy(lend, READ_ACL_TXT);
4491420Smarks 			lend += sizeof (READ_ACL_TXT) - 1;
4501420Smarks 		}
4511420Smarks 		if (mask & ACE_WRITE_ACL) {
4521420Smarks 			strcpy(lend, WRITE_ACL_TXT);
4531420Smarks 			lend += sizeof (WRITE_ACL_TXT) - 1;
4541420Smarks 		}
4551420Smarks 		if (mask & ACE_WRITE_OWNER) {
4561420Smarks 			strcpy(lend, WRITE_OWNER_TXT);
4571420Smarks 			lend += sizeof (WRITE_OWNER_TXT) - 1;
4581420Smarks 		}
4591420Smarks 		if (mask & ACE_SYNCHRONIZE) {
4601420Smarks 			strcpy(lend, SYNCHRONIZE_TXT);
4611420Smarks 			lend += sizeof (SYNCHRONIZE_TXT) - 1;
4621420Smarks 		}
463789Sahrens 
4641420Smarks 		if (*(lend - 1) == '/')
4651420Smarks 			*--lend = '\0';
4661420Smarks 	}
4671420Smarks 
4681420Smarks 	*endp = lend;
4691420Smarks 	return (buf);
4701420Smarks }
4711420Smarks 
4721420Smarks #define	ALLOW_TXT	"allow"
4731420Smarks #define	DENY_TXT	"deny"
4741420Smarks #define	ALARM_TXT	"alarm"
4751420Smarks #define	AUDIT_TXT	"audit"
4761420Smarks #define	UNKNOWN_TXT	"unknown"
4771420Smarks char *
4781420Smarks ace_access_txt(char *buf, char **endp, int type)
4791420Smarks {
4801420Smarks 
4811420Smarks 	if (buf == NULL)
4821420Smarks 		return (NULL);
4831420Smarks 
4841420Smarks 	if (type == ACE_ACCESS_ALLOWED_ACE_TYPE) {
4851420Smarks 		strcpy(buf, ALLOW_TXT);
4861420Smarks 		*endp += sizeof (ALLOW_TXT) - 1;
4871420Smarks 	} else if (type == ACE_ACCESS_DENIED_ACE_TYPE) {
4881420Smarks 		strcpy(buf, DENY_TXT);
4891420Smarks 		*endp += sizeof (DENY_TXT) - 1;
4901420Smarks 	} else if (type == ACE_SYSTEM_AUDIT_ACE_TYPE) {
4911420Smarks 		strcpy(buf, AUDIT_TXT);
4921420Smarks 		*endp += sizeof (AUDIT_TXT) - 1;
4931420Smarks 	} else if (type == ACE_SYSTEM_ALARM_ACE_TYPE) {
4941420Smarks 		strcpy(buf, ALARM_TXT);
4951420Smarks 		*endp += sizeof (ALARM_TXT) - 1;
4961420Smarks 	} else {
4971420Smarks 		strcpy(buf, UNKNOWN_TXT);
4981420Smarks 		*endp += sizeof (UNKNOWN_TXT) - 1;
4991420Smarks 	}
5001420Smarks 
5011420Smarks 	return (buf);
5021420Smarks }
5031420Smarks 
5041420Smarks static char *
5051420Smarks ace_inherit_txt(char *buf, char **endp, uint32_t iflags, int flags)
5061420Smarks {
5071420Smarks 
5081420Smarks 	char *lend = buf;
5091420Smarks 
5101420Smarks 	if (buf == NULL) {
5111420Smarks 		return (NULL);
5121420Smarks 	}
513789Sahrens 
5141420Smarks 	if (flags & ACL_COMPACT_FMT) {
5151420Smarks 		if (iflags & ACE_FILE_INHERIT_ACE)
5161420Smarks 			buf[0] = 'f';
5171420Smarks 		else
5181420Smarks 			buf[0] = '-';
5191420Smarks 		if (iflags & ACE_DIRECTORY_INHERIT_ACE)
5201420Smarks 			buf[1] = 'd';
5211420Smarks 		else
5221420Smarks 			buf[1] = '-';
5231420Smarks 		if (iflags & ACE_INHERIT_ONLY_ACE)
5241420Smarks 			buf[2] = 'i';
5251420Smarks 		else
5261420Smarks 			buf[2] = '-';
5271420Smarks 		if (iflags & ACE_NO_PROPAGATE_INHERIT_ACE)
5281420Smarks 			buf[3] = 'n';
5291420Smarks 		else
5301420Smarks 			buf[3] = '-';
5311420Smarks 		if (iflags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG)
5321420Smarks 			buf[4] = 'S';
5331420Smarks 		else
5341420Smarks 			buf[4] = '-';
5351420Smarks 		if (iflags & ACE_FAILED_ACCESS_ACE_FLAG)
5361420Smarks 			buf[5] = 'F';
5371420Smarks 		else
5381420Smarks 			buf[5] = '-';
5391420Smarks 		buf[6] = '\0';
5401420Smarks 		*endp = buf + 6;
5411420Smarks 	} else {
5421420Smarks 		if (iflags & ACE_FILE_INHERIT_ACE) {
5431420Smarks 			strcpy(lend, "file_inherit/");
5441420Smarks 			lend += sizeof ("file_inherit/") - 1;
5451420Smarks 		}
5461420Smarks 		if (iflags & ACE_DIRECTORY_INHERIT_ACE) {
5471420Smarks 			strcpy(lend, "dir_inherit/");
5481420Smarks 			lend += sizeof ("dir_inherit/") - 1;
5491420Smarks 		}
5501420Smarks 		if (iflags & ACE_NO_PROPAGATE_INHERIT_ACE) {
5511420Smarks 			strcpy(lend, "no_propagate/");
5521420Smarks 			lend += sizeof ("no_propagate/") - 1;
5531420Smarks 		}
5541420Smarks 		if (iflags & ACE_INHERIT_ONLY_ACE) {
5551420Smarks 			strcpy(lend, "inherit_only/");
5561420Smarks 			lend += sizeof ("inherit_only/") - 1;
5571420Smarks 		}
558789Sahrens 
5591420Smarks 		if (*(lend - 1) == '/')
5601420Smarks 			*--lend = '\0';
5611420Smarks 		*endp = lend;
5621420Smarks 	}
5631420Smarks 
5641420Smarks 	return (buf);
565789Sahrens }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate /*
5680Sstevel@tonic-gate  * Convert internal acl representation to external representation.
5690Sstevel@tonic-gate  *
5700Sstevel@tonic-gate  * The length of a non-owning user name or non-owning group name ie entries
5710Sstevel@tonic-gate  * of type DEF_USER, USER, DEF_GROUP or GROUP, can exceed LOGNAME_MAX.  We
5720Sstevel@tonic-gate  * thus check the length of these entries, and if greater than LOGNAME_MAX,
5730Sstevel@tonic-gate  * we realloc() via increase_length().
5740Sstevel@tonic-gate  *
5750Sstevel@tonic-gate  * The LOGNAME_MAX, ENTRYTYPELEN and PERMS limits are otherwise always
5760Sstevel@tonic-gate  * adhered to.
5770Sstevel@tonic-gate  */
5781420Smarks 
5791420Smarks /*
5801420Smarks  * acltotext() converts each ACL entry to look like this:
5811420Smarks  *
5821420Smarks  *    entry_type:uid^gid^name:perms[:id]
5831420Smarks  *
5841420Smarks  * The maximum length of entry_type is 14 ("defaultgroup::" and
5851420Smarks  * "defaultother::") hence ENTRYTYPELEN is set to 14.
5861420Smarks  *
5871420Smarks  * The max length of a uid^gid^name entry (in theory) is 8, hence we use,
5881420Smarks  * however the ID could be a number so we therefore use ID_STR_MAX
5891420Smarks  *
5901420Smarks  * The length of a perms entry is 4 to allow for the comma appended to each
5911420Smarks  * to each acl entry.  Hence PERMS is set to 4.
5921420Smarks  */
5931420Smarks 
5941420Smarks #define	ENTRYTYPELEN	14
5951420Smarks #define	PERMS		4
5961420Smarks #define	ACL_ENTRY_SIZE	(ENTRYTYPELEN + ID_STR_MAX + PERMS + APPENDED_ID_MAX)
5971420Smarks #define	UPDATE_WHERE	where = dstr->aclexport + strlen(dstr->aclexport)
5981420Smarks 
5990Sstevel@tonic-gate char *
6001420Smarks aclent_acltotext(aclent_t  *aclp, int aclcnt, int flags)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	char		*aclexport;
6030Sstevel@tonic-gate 	char		*where;
6041507Sgjelinek 	struct group	*groupp = NULL;
6051507Sgjelinek 	struct passwd	*passwdp = NULL;
6060Sstevel@tonic-gate 	struct dynaclstr *dstr;
6070Sstevel@tonic-gate 	int		i, rtn;
6080Sstevel@tonic-gate 	size_t		excess = 0;
6091420Smarks 	char		id[20], *idstr;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	if (aclp == NULL)
6120Sstevel@tonic-gate 		return (NULL);
6130Sstevel@tonic-gate 	if ((dstr = malloc(sizeof (struct dynaclstr))) == NULL)
6140Sstevel@tonic-gate 		return (NULL);
6150Sstevel@tonic-gate 	dstr->bufsize = aclcnt * ACL_ENTRY_SIZE;
6160Sstevel@tonic-gate 	if ((dstr->aclexport = malloc(dstr->bufsize)) == NULL) {
6170Sstevel@tonic-gate 		free(dstr);
6180Sstevel@tonic-gate 		return (NULL);
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 	*dstr->aclexport = '\0';
6210Sstevel@tonic-gate 	where = dstr->aclexport;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	for (i = 0; i < aclcnt; i++, aclp++) {
6240Sstevel@tonic-gate 		switch (aclp->a_type) {
6250Sstevel@tonic-gate 		case DEF_USER_OBJ:
6260Sstevel@tonic-gate 		case USER_OBJ:
6270Sstevel@tonic-gate 			if (aclp->a_type == USER_OBJ)
6280Sstevel@tonic-gate 				where = strappend(where, "user::");
6290Sstevel@tonic-gate 			else
6300Sstevel@tonic-gate 				where = strappend(where, "defaultuser::");
6310Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
6320Sstevel@tonic-gate 			break;
6330Sstevel@tonic-gate 		case DEF_USER:
6340Sstevel@tonic-gate 		case USER:
6350Sstevel@tonic-gate 			if (aclp->a_type == USER)
6360Sstevel@tonic-gate 				where = strappend(where, "user:");
6370Sstevel@tonic-gate 			else
6380Sstevel@tonic-gate 				where = strappend(where, "defaultuser:");
6391507Sgjelinek 			if ((flags & ACL_NORESOLVE) == 0)
6401507Sgjelinek 				passwdp = getpwuid(aclp->a_id);
6410Sstevel@tonic-gate 			if (passwdp == (struct passwd *)NULL) {
6420Sstevel@tonic-gate 				/* put in uid instead */
6430Sstevel@tonic-gate 				(void) sprintf(where, "%d", aclp->a_id);
644922Shm123892 				UPDATE_WHERE;
6450Sstevel@tonic-gate 			} else {
6460Sstevel@tonic-gate 				excess = strlen(passwdp->pw_name) - LOGNAME_MAX;
6470Sstevel@tonic-gate 				if (excess > 0) {
6480Sstevel@tonic-gate 					rtn = increase_length(dstr, excess);
6490Sstevel@tonic-gate 					if (rtn == 1) {
650922Shm123892 						UPDATE_WHERE;
6510Sstevel@tonic-gate 					} else {
6520Sstevel@tonic-gate 						free(dstr->aclexport);
6530Sstevel@tonic-gate 						free(dstr);
6540Sstevel@tonic-gate 						return (NULL);
6550Sstevel@tonic-gate 					}
6560Sstevel@tonic-gate 				}
6570Sstevel@tonic-gate 				where = strappend(where, passwdp->pw_name);
6580Sstevel@tonic-gate 			}
6590Sstevel@tonic-gate 			where = strappend(where, ":");
6600Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
6610Sstevel@tonic-gate 			break;
6620Sstevel@tonic-gate 		case DEF_GROUP_OBJ:
6630Sstevel@tonic-gate 		case GROUP_OBJ:
6640Sstevel@tonic-gate 			if (aclp->a_type == GROUP_OBJ)
6650Sstevel@tonic-gate 				where = strappend(where, "group::");
6660Sstevel@tonic-gate 			else
6670Sstevel@tonic-gate 				where = strappend(where, "defaultgroup::");
6680Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
6690Sstevel@tonic-gate 			break;
6700Sstevel@tonic-gate 		case DEF_GROUP:
6710Sstevel@tonic-gate 		case GROUP:
6720Sstevel@tonic-gate 			if (aclp->a_type == GROUP)
6730Sstevel@tonic-gate 				where = strappend(where, "group:");
6740Sstevel@tonic-gate 			else
6750Sstevel@tonic-gate 				where = strappend(where, "defaultgroup:");
6761507Sgjelinek 			if ((flags & ACL_NORESOLVE) == 0)
6771507Sgjelinek 				groupp = getgrgid(aclp->a_id);
6780Sstevel@tonic-gate 			if (groupp == (struct group *)NULL) {
6790Sstevel@tonic-gate 				/* put in gid instead */
6800Sstevel@tonic-gate 				(void) sprintf(where, "%d", aclp->a_id);
681922Shm123892 				UPDATE_WHERE;
6820Sstevel@tonic-gate 			} else {
6830Sstevel@tonic-gate 				excess = strlen(groupp->gr_name) - LOGNAME_MAX;
6840Sstevel@tonic-gate 				if (excess > 0) {
6850Sstevel@tonic-gate 					rtn = increase_length(dstr, excess);
6860Sstevel@tonic-gate 					if (rtn == 1) {
687922Shm123892 						UPDATE_WHERE;
6880Sstevel@tonic-gate 					} else {
6890Sstevel@tonic-gate 						free(dstr->aclexport);
6900Sstevel@tonic-gate 						free(dstr);
6910Sstevel@tonic-gate 						return (NULL);
6920Sstevel@tonic-gate 					}
6930Sstevel@tonic-gate 				}
6940Sstevel@tonic-gate 				where = strappend(where, groupp->gr_name);
6950Sstevel@tonic-gate 			}
6960Sstevel@tonic-gate 			where = strappend(where, ":");
6970Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
6980Sstevel@tonic-gate 			break;
6990Sstevel@tonic-gate 		case DEF_CLASS_OBJ:
7000Sstevel@tonic-gate 		case CLASS_OBJ:
7010Sstevel@tonic-gate 			if (aclp->a_type == CLASS_OBJ)
7020Sstevel@tonic-gate 				where = strappend(where, "mask:");
7030Sstevel@tonic-gate 			else
7040Sstevel@tonic-gate 				where = strappend(where, "defaultmask:");
7050Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
7060Sstevel@tonic-gate 			break;
7070Sstevel@tonic-gate 		case DEF_OTHER_OBJ:
7080Sstevel@tonic-gate 		case OTHER_OBJ:
7090Sstevel@tonic-gate 			if (aclp->a_type == OTHER_OBJ)
7100Sstevel@tonic-gate 				where = strappend(where, "other:");
7110Sstevel@tonic-gate 			else
7120Sstevel@tonic-gate 				where = strappend(where, "defaultother:");
7130Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
7140Sstevel@tonic-gate 			break;
7150Sstevel@tonic-gate 		default:
7160Sstevel@tonic-gate 			free(dstr->aclexport);
7170Sstevel@tonic-gate 			free(dstr);
7180Sstevel@tonic-gate 			return (NULL);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 		}
7211420Smarks 
7221420Smarks 		if ((flags & ACL_APPEND_ID) && ((aclp->a_type == USER) ||
7231420Smarks 		    (aclp->a_type == DEF_USER) || (aclp->a_type == GROUP) ||
7241420Smarks 		    (aclp->a_type == DEF_GROUP))) {
7251420Smarks 			where = strappend(where, ":");
7261420Smarks 			id[ID_STR_MAX - 1] = '\0'; /* null terminate buffer */
7271420Smarks 			idstr = lltostr(aclp->a_id, &id[ID_STR_MAX - 1]);
7281420Smarks 			where = strappend(where, idstr);
7291420Smarks 		}
7300Sstevel@tonic-gate 		if (i < aclcnt - 1)
7310Sstevel@tonic-gate 			where = strappend(where, ",");
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 	aclexport = dstr->aclexport;
7340Sstevel@tonic-gate 	free(dstr);
7350Sstevel@tonic-gate 	return (aclexport);
7361420Smarks 
7371420Smarks 
7381420Smarks 
7391420Smarks 
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate 
7421420Smarks char *
7431420Smarks acltotext(aclent_t *aclp, int aclcnt)
7440Sstevel@tonic-gate {
7451420Smarks 	return (aclent_acltotext(aclp, aclcnt, 0));
7461420Smarks }
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 
749789Sahrens aclent_t *
750789Sahrens aclfromtext(char *aclstr, int *aclcnt)
751789Sahrens {
752789Sahrens 	acl_t *aclp;
753789Sahrens 	aclent_t *aclentp;
754789Sahrens 	int error;
755789Sahrens 
7561420Smarks 	error = acl_fromtext(aclstr, &aclp);
757789Sahrens 	if (error)
758789Sahrens 		return (NULL);
759789Sahrens 
760789Sahrens 	aclentp = aclp->acl_aclp;
761789Sahrens 	aclp->acl_aclp = NULL;
7621420Smarks 	*aclcnt = aclp->acl_cnt;
763789Sahrens 
7641420Smarks 	acl_free(aclp);
765789Sahrens 	return (aclentp);
766789Sahrens }
767789Sahrens 
768789Sahrens 
7690Sstevel@tonic-gate static char *
7700Sstevel@tonic-gate strappend(char *where, char *newstr)
7710Sstevel@tonic-gate {
7720Sstevel@tonic-gate 	(void) strcat(where, newstr);
7730Sstevel@tonic-gate 	return (where + strlen(newstr));
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate static char *
7770Sstevel@tonic-gate convert_perm(char *where, o_mode_t perm)
7780Sstevel@tonic-gate {
7791420Smarks 	if (perm & S_IROTH)
7800Sstevel@tonic-gate 		where = strappend(where, "r");
7810Sstevel@tonic-gate 	else
7820Sstevel@tonic-gate 		where = strappend(where, "-");
7831420Smarks 	if (perm & S_IWOTH)
7840Sstevel@tonic-gate 		where = strappend(where, "w");
7850Sstevel@tonic-gate 	else
7860Sstevel@tonic-gate 		where = strappend(where, "-");
7871420Smarks 	if (perm & S_IXOTH)
7880Sstevel@tonic-gate 		where = strappend(where, "x");
7890Sstevel@tonic-gate 	else
7900Sstevel@tonic-gate 		where = strappend(where, "-");
7910Sstevel@tonic-gate 	/* perm is the last field */
7920Sstevel@tonic-gate 	return (where);
7930Sstevel@tonic-gate }
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate /*
7960Sstevel@tonic-gate  * Callers should check the return code as this routine may change the string
7970Sstevel@tonic-gate  * pointer in dynaclstr.
7980Sstevel@tonic-gate  */
7990Sstevel@tonic-gate static int
8000Sstevel@tonic-gate increase_length(struct dynaclstr *dacl, size_t increase)
8010Sstevel@tonic-gate {
8020Sstevel@tonic-gate 	char *tptr;
8030Sstevel@tonic-gate 	size_t newsize;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	newsize = dacl->bufsize + increase;
8060Sstevel@tonic-gate 	tptr = realloc(dacl->aclexport, newsize);
8070Sstevel@tonic-gate 	if (tptr != NULL) {
8080Sstevel@tonic-gate 		dacl->aclexport = tptr;
8090Sstevel@tonic-gate 		dacl->bufsize = newsize;
8100Sstevel@tonic-gate 		return (1);
8110Sstevel@tonic-gate 	} else
8120Sstevel@tonic-gate 		return (0);
8130Sstevel@tonic-gate }
814789Sahrens 
815789Sahrens /*
8161420Smarks  * ace_acltotext() convert each ace formatted acl to look like this:
817789Sahrens  *
8181420Smarks  * entry_type:uid^gid^name:perms[:flags]:<allow|deny>[:id][,]
819789Sahrens  *
820789Sahrens  * The maximum length of entry_type is 5 ("group")
821789Sahrens  *
8221420Smarks  * The max length of a uid^gid^name entry (in theory) is 8,
8231420Smarks  * however id could be a number so we therefore use ID_STR_MAX
824789Sahrens  *
825789Sahrens  * The length of a perms entry is 144 i.e read_data/write_data...
826789Sahrens  * to each acl entry.
827789Sahrens  *
828789Sahrens  * iflags: file_inherit/dir_inherit/inherit_only/no_propagate
829789Sahrens  *
830789Sahrens  */
831789Sahrens 
832789Sahrens #define	ACE_ENTRYTYPLEN		6
833789Sahrens #define	IFLAGS_SIZE		51
8341420Smarks #define	ACCESS_TYPE_SIZE	7	/* if unknown */
835789Sahrens #define	COLON_CNT		3
836789Sahrens #define	PERMS_LEN		216
8371420Smarks #define	ACE_ENTRY_SIZE	(ACE_ENTRYTYPLEN + ID_STR_MAX + PERMS_LEN +\
8381420Smarks     ACCESS_TYPE_SIZE + IFLAGS_SIZE + COLON_CNT + APPENDED_ID_MAX)
839789Sahrens 
840789Sahrens static char *
8411420Smarks ace_acltotext(acl_t *aceaclp, int flags)
842789Sahrens {
843789Sahrens 	ace_t		*aclp = aceaclp->acl_aclp;
844789Sahrens 	int		aclcnt = aceaclp->acl_cnt;
845789Sahrens 	char		*aclexport;
8461420Smarks 	char		*endp;
8471420Smarks 	int		i;
8481420Smarks 	char		id[ID_STR_MAX], *idstr;
849789Sahrens 	int		isdir = (aceaclp->acl_flags & ACL_IS_DIR);
850789Sahrens 
851789Sahrens 	if (aclp == NULL)
852789Sahrens 		return (NULL);
8531420Smarks 	if ((aclexport = malloc(aclcnt * ACE_ENTRY_SIZE)) == NULL)
854789Sahrens 		return (NULL);
855789Sahrens 
8561420Smarks 	aclexport[0] = '\0';
8571420Smarks 	endp = aclexport;
858789Sahrens 	for (i = 0; i < aclcnt; i++, aclp++) {
8591420Smarks 
860*1515Sgjelinek 		(void) ace_type_txt(endp, &endp, aclp, flags);
8611420Smarks 		*endp++ = ':';
8621420Smarks 		*endp = '\0';
8631420Smarks 		(void) ace_perm_txt(endp, &endp, aclp->a_access_mask,
8641420Smarks 		    aclp->a_flags, isdir, flags);
8651420Smarks 		*endp++ = ':';
8661420Smarks 		*endp = '\0';
8671420Smarks 		(void) ace_inherit_txt(endp, &endp, aclp->a_flags, flags);
8681420Smarks 		if (flags & ACL_COMPACT_FMT || aclp->a_flags &
8691420Smarks 		    (ACE_FILE_INHERIT_ACE | ACE_DIRECTORY_INHERIT_ACE |
8701420Smarks 		    (ACE_INHERIT_ONLY_ACE | ACE_NO_PROPAGATE_INHERIT_ACE))) {
8711420Smarks 			*endp++ = ':';
8721420Smarks 			*endp = '\0';
8731420Smarks 		}
8741420Smarks 		(void) ace_access_txt(endp, &endp, aclp->a_type);
875789Sahrens 
8761420Smarks 		if ((flags & ACL_APPEND_ID) &&
8771420Smarks 		    (((aclp->a_flags & ACE_TYPE_FLAGS) == 0) ||
8781420Smarks 		    ((aclp->a_flags & ACE_TYPE_FLAGS) ==
8791420Smarks 		    ACE_IDENTIFIER_GROUP))) {
8801420Smarks 			*endp++ = ':';
8811420Smarks 			*endp = '\0';
8821420Smarks 			id[ID_STR_MAX -1] = '\0'; /* null terminate buffer */
8831420Smarks 			idstr = lltostr(aclp->a_who, &id[ID_STR_MAX - 1]);
8841420Smarks 			strcpy(endp, idstr);
8851420Smarks 			endp += strlen(idstr);
886789Sahrens 		}
8871420Smarks 		if (i < aclcnt - 1) {
8881420Smarks 			*endp++ = ',';
8891420Smarks 			*(endp + 1) = '\0';
890789Sahrens 		}
891789Sahrens 	}
892789Sahrens 	return (aclexport);
893789Sahrens }
894789Sahrens 
8951420Smarks char *
8961420Smarks acl_totext(acl_t *aclp, int flags)
897789Sahrens {
898789Sahrens 
8991420Smarks 	char *txtp;
900789Sahrens 
901789Sahrens 	if (aclp == NULL)
902789Sahrens 		return (NULL);
903789Sahrens 
904789Sahrens 	switch (aclp->acl_type) {
905789Sahrens 	case ACE_T:
9061420Smarks 		txtp = ace_acltotext(aclp, flags);
9071420Smarks 		break;
908789Sahrens 	case ACLENT_T:
9091420Smarks 		txtp = aclent_acltotext(aclp->acl_aclp, aclp->acl_cnt, flags);
9101420Smarks 		break;
911789Sahrens 	}
9121420Smarks 
9131420Smarks 	return (txtp);
914789Sahrens }
915789Sahrens 
916789Sahrens int
917789Sahrens acl_fromtext(const char *acltextp, acl_t **ret_aclp)
918789Sahrens {
9191420Smarks 	int error;
9201420Smarks 	char *buf;
9211420Smarks 
9221420Smarks 	buf = malloc(strlen(acltextp) + 2);
9231420Smarks 	if (buf == NULL)
9241420Smarks 		return (EACL_MEM_ERROR);
9251420Smarks 	strcpy(buf, acltextp);
9261420Smarks 	strcat(buf, "\n");
9271420Smarks 	yybuf = buf;
9281420Smarks 	yyreset();
9291420Smarks 	error = yyparse();
9301420Smarks 	free(buf);
9311420Smarks 
9321420Smarks 	if (yyacl) {
9331420Smarks 		if (error == 0)
9341420Smarks 			*ret_aclp = yyacl;
9351420Smarks 		else {
9361420Smarks 			acl_free(yyacl);
9371420Smarks 		}
9381420Smarks 		yyacl = NULL;
9391420Smarks 	}
9401420Smarks 	return (error);
9411420Smarks }
9421420Smarks 
9431420Smarks int
9441420Smarks acl_parse(const char *acltextp, acl_t **aclp)
9451420Smarks {
946789Sahrens 	int error;
947789Sahrens 
9481420Smarks 	yyinteractive = 1;
9491420Smarks 	error = acl_fromtext(acltextp, aclp);
9501420Smarks 	yyinteractive = 0;
9511420Smarks 	return (error);
9521420Smarks }
9531420Smarks 
9541420Smarks static void
9551420Smarks ace_compact_printacl(acl_t *aclp)
9561420Smarks {
9571420Smarks 	int cnt;
9581420Smarks 	ace_t *acep;
9591420Smarks 	char *endp;
9601420Smarks 	char buf[ACE_ENTRY_SIZE];
961789Sahrens 
9621420Smarks 	for (cnt = 0, acep = aclp->acl_aclp;
9631420Smarks 	    cnt != aclp->acl_cnt; cnt++, acep++) {
9641420Smarks 		buf[0] = '\0';
965*1515Sgjelinek 		(void) printf("    %14s:", ace_type_txt(buf, &endp, acep, 0));
9661420Smarks 		(void) printf("%s:", ace_perm_txt(endp, &endp,
9671420Smarks 		    acep->a_access_mask, acep->a_flags,
9681420Smarks 		    aclp->acl_flags & ACL_IS_DIR, ACL_COMPACT_FMT));
9691420Smarks 		(void) printf("%s:",
9701420Smarks 		    ace_inherit_txt(endp, &endp, acep->a_flags,
9711420Smarks 			ACL_COMPACT_FMT));
9721420Smarks 		(void) printf("%s\n", ace_access_txt(endp, &endp,
9731420Smarks 		    acep->a_type));
9741420Smarks 	}
9751420Smarks }
976789Sahrens 
9771420Smarks static void
9781420Smarks ace_printacl(acl_t *aclp, int cols, int compact)
9791420Smarks {
9801420Smarks 	int  slot = 0;
9811420Smarks 	char *token;
9821420Smarks 	char *acltext;
9831420Smarks 
9841420Smarks 	if (compact) {
9851420Smarks 		ace_compact_printacl(aclp);
9861420Smarks 		return;
987789Sahrens 	}
988789Sahrens 
9891420Smarks 	acltext = acl_totext(aclp, 0);
9901420Smarks 
9911420Smarks 	if (acltext == NULL)
9921420Smarks 		return;
9931420Smarks 
9941420Smarks 	token = strtok(acltext, ",");
9951420Smarks 	if (token == NULL) {
9961420Smarks 		free(acltext);
9971420Smarks 		return;
998789Sahrens 	}
999789Sahrens 
10001420Smarks 	do {
10011420Smarks 		(void) printf("     %d:", slot++);
10021420Smarks 		split_line(token, cols - 5);
10031420Smarks 	} while (token = strtok(NULL, ","));
10041420Smarks 	free(acltext);
10051420Smarks }
10061420Smarks 
10071420Smarks /*
10081420Smarks  * pretty print an ACL.
10091420Smarks  * For aclent_t ACL's the format is
10101420Smarks  * similar to the old format used by getfacl,
10111420Smarks  * with the addition of adding a "slot" number
10121420Smarks  * before each entry.
10131420Smarks  *
10141420Smarks  * for ace_t ACL's the cols variable will break up
10151420Smarks  * the long lines into multiple lines and will also
10161420Smarks  * print a "slot" number.
10171420Smarks  */
10181420Smarks void
10191420Smarks acl_printacl(acl_t *aclp, int cols, int compact)
10201420Smarks {
10211420Smarks 
10221420Smarks 	switch (aclp->acl_type) {
10231420Smarks 	case ACLENT_T:
10241420Smarks 		aclent_printacl(aclp);
10251420Smarks 		break;
10261420Smarks 	case ACE_T:
10271420Smarks 		ace_printacl(aclp, cols, compact);
10281420Smarks 		break;
10291420Smarks 	}
10301420Smarks }
10311420Smarks 
10321420Smarks typedef struct value_table {
10331420Smarks 	char		p_letter; /* perm letter such as 'r' */
10341420Smarks 	uint32_t	p_value; /* value for perm when pletter found */
10351420Smarks } value_table_t;
10361420Smarks 
10371420Smarks #define	ACE_PERM_COUNT 14
10381420Smarks 
10391420Smarks /*
10401420Smarks  * The permission tables are layed out in positional order
10411420Smarks  * a '-' character will indicate a permission at a given
10421420Smarks  * position is not specified.  The '-' is not part of the
10431420Smarks  * table, but will be checked for in the permission computation
10441420Smarks  * routine.
10451420Smarks  */
10461420Smarks value_table_t ace_perm_table[ACE_PERM_COUNT] = {
10471420Smarks 	{ 'r', ACE_READ_DATA},
10481420Smarks 	{ 'w', ACE_WRITE_DATA},
10491420Smarks 	{ 'x', ACE_EXECUTE},
10501420Smarks 	{ 'p', ACE_APPEND_DATA},
10511420Smarks 	{ 'd', ACE_DELETE},
10521420Smarks 	{ 'D', ACE_DELETE_CHILD},
10531420Smarks 	{ 'a', ACE_READ_ATTRIBUTES},
10541420Smarks 	{ 'A', ACE_WRITE_ATTRIBUTES},
10551420Smarks 	{ 'R', ACE_READ_NAMED_ATTRS},
10561420Smarks 	{ 'W', ACE_WRITE_NAMED_ATTRS},
10571420Smarks 	{ 'c', ACE_READ_ACL},
10581420Smarks 	{ 'C', ACE_WRITE_ACL},
10591420Smarks 	{ 'o', ACE_WRITE_OWNER},
10601420Smarks 	{ 's', ACE_SYNCHRONIZE}
10611420Smarks };
10621420Smarks 
10631420Smarks #define	ACLENT_PERM_COUNT 3
10641420Smarks 
10651420Smarks value_table_t aclent_perm_table[ACLENT_PERM_COUNT] = {
10661420Smarks 	{ 'r', S_IROTH},
10671420Smarks 	{ 'w', S_IWOTH},
10681420Smarks 	{ 'x', S_IXOTH}
10691420Smarks };
10701420Smarks 
10711420Smarks #define	IFLAG_COUNT	6
10721420Smarks value_table_t inherit_table[IFLAG_COUNT] = {
10731420Smarks 	{'f', ACE_FILE_INHERIT_ACE},
10741420Smarks 	{'d', ACE_DIRECTORY_INHERIT_ACE},
10751420Smarks 	{'i', ACE_INHERIT_ONLY_ACE},
10761420Smarks 	{'n', ACE_NO_PROPAGATE_INHERIT_ACE},
10771420Smarks 	{'S', ACE_SUCCESSFUL_ACCESS_ACE_FLAG},
10781420Smarks 	{'F', ACE_FAILED_ACCESS_ACE_FLAG}
10791420Smarks };
10801420Smarks 
10811420Smarks /*
10821420Smarks  * compute value from a permission table or inheritance table
10831420Smarks  * based on string passed in.  If positional is set then
10841420Smarks  * string must match order in permtab, otherwise any order
10851420Smarks  * is allowed.
10861420Smarks  */
10871420Smarks int
10881420Smarks compute_values(value_table_t *permtab, int count,
10891420Smarks     char *permstr, int positional, uint32_t *mask)
10901420Smarks {
10911420Smarks 	uint32_t perm_val = 0;
10921420Smarks 	char *pstr;
10931420Smarks 	int i, found;
10941420Smarks 
10951420Smarks 	if (count < 0)
10961420Smarks 		return (1);
10971420Smarks 
10981420Smarks 	if (positional) {
10991420Smarks 		for (i = 0, pstr = permstr; i != count && pstr &&
11001420Smarks 		    *pstr; i++, pstr++) {
11011420Smarks 			if (*pstr == permtab[i].p_letter) {
11021420Smarks 				perm_val |= permtab[i].p_value;
11031420Smarks 			} else if (*pstr != '-') {
11041420Smarks 				return (1);
11051420Smarks 			}
1106789Sahrens 		}
11071420Smarks 	} else {  /* random order single letters with no '-' */
11081420Smarks 		for (pstr = permstr; pstr && *pstr; pstr++) {
11091420Smarks 			for (found = 0, i = 0; i != count; i++) {
11101420Smarks 				if (*pstr == permtab[i].p_letter) {
11111420Smarks 					perm_val |= permtab[i].p_value;
11121420Smarks 					found = 1;
11131420Smarks 					break;
11141420Smarks 				}
11151420Smarks 			}
11161420Smarks 			if (found == 0)
11171420Smarks 				return (1);
11181420Smarks 		}
1119789Sahrens 	}
1120789Sahrens 
11211420Smarks 	*mask = perm_val;
11221420Smarks 	return (0);
11231420Smarks }
1124789Sahrens 
11251420Smarks /*
11261420Smarks  * compute value for inheritance flags.
11271420Smarks  */
11281420Smarks int
11291420Smarks compute_ace_inherit(char *str, uint32_t *imask)
11301420Smarks {
11311420Smarks 	int error;
11321420Smarks 	int positional = 0;
1133789Sahrens 
11341420Smarks 	if (strlen(str) == IFLAG_COUNT)
11351420Smarks 		positional = 1;
11361420Smarks 
11371420Smarks 	error = compute_values(inherit_table, IFLAG_COUNT,
11381420Smarks 	    str, positional, imask);
11391420Smarks 
11401420Smarks 	if (error)
11411420Smarks 		return (EACL_INHERIT_ERROR);
11421420Smarks 
1143789Sahrens 	return (error);
1144789Sahrens }
11451420Smarks 
11461420Smarks 
11471420Smarks /*
11481420Smarks  * compute value for ACE permissions.
11491420Smarks  */
11501420Smarks int
11511420Smarks compute_ace_perms(char *str, uint32_t *mask)
11521420Smarks {
11531420Smarks 	int positional = 0;
11541420Smarks 	int error;
11551420Smarks 
11561420Smarks 	if (strlen(str) == ACE_PERM_COUNT)
11571420Smarks 		positional = 1;
11581420Smarks 
11591420Smarks 	error = compute_values(ace_perm_table, ACE_PERM_COUNT,
11601420Smarks 	    str, positional, mask);
11611420Smarks 
11621420Smarks 	if (error && positional) {
11631420Smarks 		/*
11641420Smarks 		 * If positional was set, then make sure permissions
11651420Smarks 		 * aren't actually valid in non positional case where
11661420Smarks 		 * all permissions are specified, just in random order.
11671420Smarks 		 */
11681420Smarks 		error = compute_values(ace_perm_table,
11691420Smarks 		    ACE_PERM_COUNT, str, 0, mask);
11701420Smarks 	}
11711420Smarks 	if (error)
11721420Smarks 		error = EACL_PERM_MASK_ERROR;
11731420Smarks 
11741420Smarks 	return (error);
11751420Smarks }
11761420Smarks 
11771420Smarks 
11781420Smarks 
11791420Smarks /*
11801420Smarks  * compute values for aclent permissions.
11811420Smarks  */
11821420Smarks int
11831420Smarks compute_aclent_perms(char *str, o_mode_t *mask)
11841420Smarks {
11851420Smarks 	int error;
11861420Smarks 	uint32_t pmask;
11871420Smarks 
11881420Smarks 	if (strlen(str) != ACLENT_PERM_COUNT)
11891420Smarks 		return (EACL_PERM_MASK_ERROR);
11901420Smarks 
11911420Smarks 	*mask = 0;
11921420Smarks 	error = compute_values(aclent_perm_table, ACLENT_PERM_COUNT,
11931420Smarks 	    str, 1, &pmask);
11941420Smarks 	if (error == 0) {
11951420Smarks 		*mask = (o_mode_t)pmask;
11961420Smarks 	} else
11971420Smarks 		error = EACL_PERM_MASK_ERROR;
11981420Smarks 	return (error);
11991420Smarks }
12001420Smarks 
12011420Smarks /*
12021420Smarks  * determine ACE permissions.
12031420Smarks  */
12041420Smarks int
12051420Smarks ace_perm_mask(struct acl_perm_type *aclperm, uint32_t *mask)
12061420Smarks {
12071420Smarks 	int error;
12081420Smarks 
12091420Smarks 	if (aclperm->perm_style == PERM_TYPE_EMPTY) {
12101420Smarks 		*mask = 0;
12111420Smarks 		return (0);
12121420Smarks 	}
12131420Smarks 
12141420Smarks 	if (aclperm->perm_style == PERM_TYPE_ACE) {
12151420Smarks 		*mask = aclperm->perm_val;
12161420Smarks 		return (0);
12171420Smarks 	}
12181420Smarks 
12191420Smarks 	error = compute_ace_perms(aclperm->perm_str, mask);
12201420Smarks 	if (error) {
12211420Smarks 		acl_error(gettext("Invalid permission(s) '%s' specified\n"),
12221420Smarks 		    aclperm->perm_str);
12231420Smarks 		return (EACL_PERM_MASK_ERROR);
12241420Smarks 	}
12251420Smarks 
12261420Smarks 	return (0);
12271420Smarks }
1228