xref: /titanic_52/usr/src/lib/libsec/common/acltext.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <grp.h>
31*7c478bd9Sstevel@tonic-gate #include <pwd.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <limits.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/acl.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate /*
40*7c478bd9Sstevel@tonic-gate  * acltotext() converts each ACL entry to look like this:
41*7c478bd9Sstevel@tonic-gate  *
42*7c478bd9Sstevel@tonic-gate  *    entry_type:uid^gid^name:perms
43*7c478bd9Sstevel@tonic-gate  *
44*7c478bd9Sstevel@tonic-gate  * The maximum length of entry_type is 14 ("defaultgroup::" and
45*7c478bd9Sstevel@tonic-gate  * "defaultother::") hence ENTRYTYPELEN is set to 14.
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  * The max length of a uid^gid^name entry (in theory) is 8, hence we use
48*7c478bd9Sstevel@tonic-gate  * LOGNAME_MAX.
49*7c478bd9Sstevel@tonic-gate  *
50*7c478bd9Sstevel@tonic-gate  * The length of a perms entry is 4 to allow for the comma appended to each
51*7c478bd9Sstevel@tonic-gate  * to each acl entry.  Hence PERMS is set to 4.
52*7c478bd9Sstevel@tonic-gate  */
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #define	ENTRYTYPELEN	14
55*7c478bd9Sstevel@tonic-gate #define	PERMS		4
56*7c478bd9Sstevel@tonic-gate #define	ACL_ENTRY_SIZE	(ENTRYTYPELEN + LOGNAME_MAX + PERMS)
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate struct dynaclstr {
59*7c478bd9Sstevel@tonic-gate 	size_t bufsize;		/* current size of aclexport */
60*7c478bd9Sstevel@tonic-gate 	char *aclexport;
61*7c478bd9Sstevel@tonic-gate };
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate static char *strappend(char *, char *);
64*7c478bd9Sstevel@tonic-gate static char *convert_perm(char *, o_mode_t);
65*7c478bd9Sstevel@tonic-gate static int increase_length(struct dynaclstr *, size_t);
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate #define	FREE	free(aclp);\
68*7c478bd9Sstevel@tonic-gate 		free(allocp)
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate /*
71*7c478bd9Sstevel@tonic-gate  * Convert internal acl representation to external representation.
72*7c478bd9Sstevel@tonic-gate  *
73*7c478bd9Sstevel@tonic-gate  * The length of a non-owning user name or non-owning group name ie entries
74*7c478bd9Sstevel@tonic-gate  * of type DEF_USER, USER, DEF_GROUP or GROUP, can exceed LOGNAME_MAX.  We
75*7c478bd9Sstevel@tonic-gate  * thus check the length of these entries, and if greater than LOGNAME_MAX,
76*7c478bd9Sstevel@tonic-gate  * we realloc() via increase_length().
77*7c478bd9Sstevel@tonic-gate  *
78*7c478bd9Sstevel@tonic-gate  * The LOGNAME_MAX, ENTRYTYPELEN and PERMS limits are otherwise always
79*7c478bd9Sstevel@tonic-gate  * adhered to.
80*7c478bd9Sstevel@tonic-gate  */
81*7c478bd9Sstevel@tonic-gate char *
82*7c478bd9Sstevel@tonic-gate acltotext(aclent_t *aclp, int aclcnt)
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	char		*aclexport;
85*7c478bd9Sstevel@tonic-gate 	char		*where;
86*7c478bd9Sstevel@tonic-gate 	struct group	*groupp;
87*7c478bd9Sstevel@tonic-gate 	struct passwd	*passwdp;
88*7c478bd9Sstevel@tonic-gate 	struct dynaclstr *dstr;
89*7c478bd9Sstevel@tonic-gate 	int		i, rtn;
90*7c478bd9Sstevel@tonic-gate 	size_t		excess = 0;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	if (aclp == NULL)
93*7c478bd9Sstevel@tonic-gate 		return (NULL);
94*7c478bd9Sstevel@tonic-gate 	if ((dstr = malloc(sizeof (struct dynaclstr))) == NULL)
95*7c478bd9Sstevel@tonic-gate 		return (NULL);
96*7c478bd9Sstevel@tonic-gate 	dstr->bufsize = aclcnt * ACL_ENTRY_SIZE;
97*7c478bd9Sstevel@tonic-gate 	if ((dstr->aclexport = malloc(dstr->bufsize)) == NULL) {
98*7c478bd9Sstevel@tonic-gate 		free(dstr);
99*7c478bd9Sstevel@tonic-gate 		return (NULL);
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 	*dstr->aclexport = '\0';
102*7c478bd9Sstevel@tonic-gate 	where = dstr->aclexport;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < aclcnt; i++, aclp++) {
105*7c478bd9Sstevel@tonic-gate 		switch (aclp->a_type) {
106*7c478bd9Sstevel@tonic-gate 		case DEF_USER_OBJ:
107*7c478bd9Sstevel@tonic-gate 		case USER_OBJ:
108*7c478bd9Sstevel@tonic-gate 			if (aclp->a_type == USER_OBJ)
109*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "user::");
110*7c478bd9Sstevel@tonic-gate 			else
111*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "defaultuser::");
112*7c478bd9Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
113*7c478bd9Sstevel@tonic-gate 			break;
114*7c478bd9Sstevel@tonic-gate 		case DEF_USER:
115*7c478bd9Sstevel@tonic-gate 		case USER:
116*7c478bd9Sstevel@tonic-gate 			if (aclp->a_type == USER)
117*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "user:");
118*7c478bd9Sstevel@tonic-gate 			else
119*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "defaultuser:");
120*7c478bd9Sstevel@tonic-gate 			passwdp = getpwuid(aclp->a_id);
121*7c478bd9Sstevel@tonic-gate 			if (passwdp == (struct passwd *)NULL) {
122*7c478bd9Sstevel@tonic-gate 				/* put in uid instead */
123*7c478bd9Sstevel@tonic-gate 				(void) sprintf(where, "%d", aclp->a_id);
124*7c478bd9Sstevel@tonic-gate 			} else {
125*7c478bd9Sstevel@tonic-gate 				excess = strlen(passwdp->pw_name) - LOGNAME_MAX;
126*7c478bd9Sstevel@tonic-gate 				if (excess > 0) {
127*7c478bd9Sstevel@tonic-gate 					rtn = increase_length(dstr, excess);
128*7c478bd9Sstevel@tonic-gate 					if (rtn == 1) {
129*7c478bd9Sstevel@tonic-gate 						/* reset where */
130*7c478bd9Sstevel@tonic-gate 						where = dstr->aclexport +
131*7c478bd9Sstevel@tonic-gate 							strlen(dstr->aclexport);
132*7c478bd9Sstevel@tonic-gate 					} else {
133*7c478bd9Sstevel@tonic-gate 						free(dstr->aclexport);
134*7c478bd9Sstevel@tonic-gate 						free(dstr);
135*7c478bd9Sstevel@tonic-gate 						return (NULL);
136*7c478bd9Sstevel@tonic-gate 					}
137*7c478bd9Sstevel@tonic-gate 				}
138*7c478bd9Sstevel@tonic-gate 				where = strappend(where, passwdp->pw_name);
139*7c478bd9Sstevel@tonic-gate 			}
140*7c478bd9Sstevel@tonic-gate 			where = strappend(where, ":");
141*7c478bd9Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
142*7c478bd9Sstevel@tonic-gate 			break;
143*7c478bd9Sstevel@tonic-gate 		case DEF_GROUP_OBJ:
144*7c478bd9Sstevel@tonic-gate 		case GROUP_OBJ:
145*7c478bd9Sstevel@tonic-gate 			if (aclp->a_type == GROUP_OBJ)
146*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "group::");
147*7c478bd9Sstevel@tonic-gate 			else
148*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "defaultgroup::");
149*7c478bd9Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
150*7c478bd9Sstevel@tonic-gate 			break;
151*7c478bd9Sstevel@tonic-gate 		case DEF_GROUP:
152*7c478bd9Sstevel@tonic-gate 		case GROUP:
153*7c478bd9Sstevel@tonic-gate 			if (aclp->a_type == GROUP)
154*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "group:");
155*7c478bd9Sstevel@tonic-gate 			else
156*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "defaultgroup:");
157*7c478bd9Sstevel@tonic-gate 			groupp = getgrgid(aclp->a_id);
158*7c478bd9Sstevel@tonic-gate 			if (groupp == (struct group *)NULL) {
159*7c478bd9Sstevel@tonic-gate 				/* put in gid instead */
160*7c478bd9Sstevel@tonic-gate 				(void) sprintf(where, "%d", aclp->a_id);
161*7c478bd9Sstevel@tonic-gate 			} else {
162*7c478bd9Sstevel@tonic-gate 				excess = strlen(groupp->gr_name) - LOGNAME_MAX;
163*7c478bd9Sstevel@tonic-gate 				if (excess > 0) {
164*7c478bd9Sstevel@tonic-gate 					rtn = increase_length(dstr, excess);
165*7c478bd9Sstevel@tonic-gate 					if (rtn == 1) {
166*7c478bd9Sstevel@tonic-gate 						/* reset where */
167*7c478bd9Sstevel@tonic-gate 						where = dstr->aclexport +
168*7c478bd9Sstevel@tonic-gate 							strlen(dstr->aclexport);
169*7c478bd9Sstevel@tonic-gate 					} else {
170*7c478bd9Sstevel@tonic-gate 						free(dstr->aclexport);
171*7c478bd9Sstevel@tonic-gate 						free(dstr);
172*7c478bd9Sstevel@tonic-gate 						return (NULL);
173*7c478bd9Sstevel@tonic-gate 					}
174*7c478bd9Sstevel@tonic-gate 				}
175*7c478bd9Sstevel@tonic-gate 				where = strappend(where, groupp->gr_name);
176*7c478bd9Sstevel@tonic-gate 			}
177*7c478bd9Sstevel@tonic-gate 			where = strappend(where, ":");
178*7c478bd9Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
179*7c478bd9Sstevel@tonic-gate 			break;
180*7c478bd9Sstevel@tonic-gate 		case DEF_CLASS_OBJ:
181*7c478bd9Sstevel@tonic-gate 		case CLASS_OBJ:
182*7c478bd9Sstevel@tonic-gate 			if (aclp->a_type == CLASS_OBJ)
183*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "mask:");
184*7c478bd9Sstevel@tonic-gate 			else
185*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "defaultmask:");
186*7c478bd9Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
187*7c478bd9Sstevel@tonic-gate 			break;
188*7c478bd9Sstevel@tonic-gate 		case DEF_OTHER_OBJ:
189*7c478bd9Sstevel@tonic-gate 		case OTHER_OBJ:
190*7c478bd9Sstevel@tonic-gate 			if (aclp->a_type == OTHER_OBJ)
191*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "other:");
192*7c478bd9Sstevel@tonic-gate 			else
193*7c478bd9Sstevel@tonic-gate 				where = strappend(where, "defaultother:");
194*7c478bd9Sstevel@tonic-gate 			where = convert_perm(where, aclp->a_perm);
195*7c478bd9Sstevel@tonic-gate 			break;
196*7c478bd9Sstevel@tonic-gate 		default:
197*7c478bd9Sstevel@tonic-gate 			free(dstr->aclexport);
198*7c478bd9Sstevel@tonic-gate 			free(dstr);
199*7c478bd9Sstevel@tonic-gate 			return (NULL);
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 		}
202*7c478bd9Sstevel@tonic-gate 		if (i < aclcnt - 1)
203*7c478bd9Sstevel@tonic-gate 			where = strappend(where, ",");
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 	aclexport = dstr->aclexport;
206*7c478bd9Sstevel@tonic-gate 	free(dstr);
207*7c478bd9Sstevel@tonic-gate 	return (aclexport);
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate /*
211*7c478bd9Sstevel@tonic-gate  * Convert external acl representation to internal representation.
212*7c478bd9Sstevel@tonic-gate  * The accepted syntax is: <acl_entry>[,<acl_entry>]*[,]
213*7c478bd9Sstevel@tonic-gate  * The comma at the end is not prescribed by the man pages.
214*7c478bd9Sstevel@tonic-gate  * But it is needed not to break the old programs.
215*7c478bd9Sstevel@tonic-gate  */
216*7c478bd9Sstevel@tonic-gate aclent_t *
217*7c478bd9Sstevel@tonic-gate aclfromtext(char *aclstr, int *aclcnt)
218*7c478bd9Sstevel@tonic-gate {
219*7c478bd9Sstevel@tonic-gate 	char		*fieldp;
220*7c478bd9Sstevel@tonic-gate 	char		*tp;
221*7c478bd9Sstevel@tonic-gate 	char		*nextp;
222*7c478bd9Sstevel@tonic-gate 	char		*allocp;
223*7c478bd9Sstevel@tonic-gate 	char		*aclimport;
224*7c478bd9Sstevel@tonic-gate 	int		entry_type;
225*7c478bd9Sstevel@tonic-gate 	int		id;
226*7c478bd9Sstevel@tonic-gate 	int		len;
227*7c478bd9Sstevel@tonic-gate 	o_mode_t	perm;
228*7c478bd9Sstevel@tonic-gate 	aclent_t	*tmpaclp;
229*7c478bd9Sstevel@tonic-gate 	aclent_t	*aclp;
230*7c478bd9Sstevel@tonic-gate 	struct group	*groupp;
231*7c478bd9Sstevel@tonic-gate 	struct passwd	*passwdp;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	*aclcnt = 0;
234*7c478bd9Sstevel@tonic-gate 	aclp = NULL;
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	if (! aclstr)
237*7c478bd9Sstevel@tonic-gate 		return (NULL);
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	len = strlen(aclstr);
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	if ((aclimport = allocp = strdup(aclstr)) == NULL) {
242*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "malloc() failed\n");
243*7c478bd9Sstevel@tonic-gate 		return (NULL);
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	if (aclimport[len - 1] == ',')
247*7c478bd9Sstevel@tonic-gate 		aclimport[len - 1] = '\0';
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	for (; aclimport; ) {
250*7c478bd9Sstevel@tonic-gate 		/* look for an ACL entry */
251*7c478bd9Sstevel@tonic-gate 		tp = strchr(aclimport, ',');
252*7c478bd9Sstevel@tonic-gate 		if (tp == NULL) {
253*7c478bd9Sstevel@tonic-gate 			nextp = NULL;
254*7c478bd9Sstevel@tonic-gate 		} else {
255*7c478bd9Sstevel@tonic-gate 			*tp = '\0';
256*7c478bd9Sstevel@tonic-gate 			nextp = tp + 1;
257*7c478bd9Sstevel@tonic-gate 		}
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 		*aclcnt += 1;
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 		/*
262*7c478bd9Sstevel@tonic-gate 		 * get additional memory:
263*7c478bd9Sstevel@tonic-gate 		 * can be more efficient by allocating a bigger block
264*7c478bd9Sstevel@tonic-gate 		 * each time.
265*7c478bd9Sstevel@tonic-gate 		 */
266*7c478bd9Sstevel@tonic-gate 		if (*aclcnt > 1)
267*7c478bd9Sstevel@tonic-gate 			tmpaclp = (aclent_t *)realloc(aclp,
268*7c478bd9Sstevel@tonic-gate 			    sizeof (aclent_t) * (*aclcnt));
269*7c478bd9Sstevel@tonic-gate 		else
270*7c478bd9Sstevel@tonic-gate 			tmpaclp = (aclent_t *)malloc(sizeof (aclent_t));
271*7c478bd9Sstevel@tonic-gate 		if (tmpaclp == NULL) {
272*7c478bd9Sstevel@tonic-gate 			free(allocp);
273*7c478bd9Sstevel@tonic-gate 			if (aclp)
274*7c478bd9Sstevel@tonic-gate 				free(aclp);
275*7c478bd9Sstevel@tonic-gate 			return (NULL);
276*7c478bd9Sstevel@tonic-gate 		}
277*7c478bd9Sstevel@tonic-gate 		aclp = tmpaclp;
278*7c478bd9Sstevel@tonic-gate 		tmpaclp = aclp + (*aclcnt - 1);
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 		/* look for entry type field */
281*7c478bd9Sstevel@tonic-gate 		tp = strchr(aclimport, ':');
282*7c478bd9Sstevel@tonic-gate 		if (tp == NULL) {
283*7c478bd9Sstevel@tonic-gate 			FREE;
284*7c478bd9Sstevel@tonic-gate 			return (NULL);
285*7c478bd9Sstevel@tonic-gate 		} else
286*7c478bd9Sstevel@tonic-gate 			*tp = '\0';
287*7c478bd9Sstevel@tonic-gate 		if (strcmp(aclimport, "user") == 0) {
288*7c478bd9Sstevel@tonic-gate 			if (*(tp+1) == ':')
289*7c478bd9Sstevel@tonic-gate 				entry_type = USER_OBJ;
290*7c478bd9Sstevel@tonic-gate 			else
291*7c478bd9Sstevel@tonic-gate 				entry_type = USER;
292*7c478bd9Sstevel@tonic-gate 		} else if (strcmp(aclimport, "group") == 0) {
293*7c478bd9Sstevel@tonic-gate 			if (*(tp+1) == ':')
294*7c478bd9Sstevel@tonic-gate 				entry_type = GROUP_OBJ;
295*7c478bd9Sstevel@tonic-gate 			else
296*7c478bd9Sstevel@tonic-gate 				entry_type = GROUP;
297*7c478bd9Sstevel@tonic-gate 		} else if (strcmp(aclimport, "other") == 0)
298*7c478bd9Sstevel@tonic-gate 			entry_type = OTHER_OBJ;
299*7c478bd9Sstevel@tonic-gate 		else if (strcmp(aclimport, "mask") == 0)
300*7c478bd9Sstevel@tonic-gate 			entry_type = CLASS_OBJ;
301*7c478bd9Sstevel@tonic-gate 		else if (strcmp(aclimport, "defaultuser") == 0) {
302*7c478bd9Sstevel@tonic-gate 			if (*(tp+1) == ':')
303*7c478bd9Sstevel@tonic-gate 				entry_type = DEF_USER_OBJ;
304*7c478bd9Sstevel@tonic-gate 			else
305*7c478bd9Sstevel@tonic-gate 				entry_type = DEF_USER;
306*7c478bd9Sstevel@tonic-gate 		} else if (strcmp(aclimport, "defaultgroup") == 0) {
307*7c478bd9Sstevel@tonic-gate 			if (*(tp+1) == ':')
308*7c478bd9Sstevel@tonic-gate 				entry_type = DEF_GROUP_OBJ;
309*7c478bd9Sstevel@tonic-gate 			else
310*7c478bd9Sstevel@tonic-gate 				entry_type = DEF_GROUP;
311*7c478bd9Sstevel@tonic-gate 		} else if (strcmp(aclimport, "defaultmask") == 0)
312*7c478bd9Sstevel@tonic-gate 			entry_type = DEF_CLASS_OBJ;
313*7c478bd9Sstevel@tonic-gate 		else if (strcmp(aclimport, "defaultother") == 0)
314*7c478bd9Sstevel@tonic-gate 			entry_type = DEF_OTHER_OBJ;
315*7c478bd9Sstevel@tonic-gate 		else {
316*7c478bd9Sstevel@tonic-gate 			FREE;
317*7c478bd9Sstevel@tonic-gate 			return (NULL);
318*7c478bd9Sstevel@tonic-gate 		}
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 		/* look for user/group name */
321*7c478bd9Sstevel@tonic-gate 		if (entry_type != CLASS_OBJ && entry_type != OTHER_OBJ &&
322*7c478bd9Sstevel@tonic-gate 		    entry_type != DEF_CLASS_OBJ &&
323*7c478bd9Sstevel@tonic-gate 		    entry_type != DEF_OTHER_OBJ) {
324*7c478bd9Sstevel@tonic-gate 			fieldp = tp + 1;
325*7c478bd9Sstevel@tonic-gate 			tp = strchr(fieldp, ':');
326*7c478bd9Sstevel@tonic-gate 			if (tp == NULL) {
327*7c478bd9Sstevel@tonic-gate 				FREE;
328*7c478bd9Sstevel@tonic-gate 				return (NULL);
329*7c478bd9Sstevel@tonic-gate 			} else
330*7c478bd9Sstevel@tonic-gate 				*tp = '\0';
331*7c478bd9Sstevel@tonic-gate 			if (fieldp != tp) {
332*7c478bd9Sstevel@tonic-gate 				/*
333*7c478bd9Sstevel@tonic-gate 				 * The second field could be empty. We only care
334*7c478bd9Sstevel@tonic-gate 				 * when the field has user/group name.
335*7c478bd9Sstevel@tonic-gate 				 */
336*7c478bd9Sstevel@tonic-gate 				if (entry_type == USER ||
337*7c478bd9Sstevel@tonic-gate 				    entry_type == DEF_USER) {
338*7c478bd9Sstevel@tonic-gate 					/*
339*7c478bd9Sstevel@tonic-gate 					 * The reentrant interface getpwnam_r()
340*7c478bd9Sstevel@tonic-gate 					 * is uncommitted and subject to
341*7c478bd9Sstevel@tonic-gate 					 * change. Use the friendlier interface
342*7c478bd9Sstevel@tonic-gate 					 * getpwnam().
343*7c478bd9Sstevel@tonic-gate 					 */
344*7c478bd9Sstevel@tonic-gate 					passwdp = getpwnam(fieldp);
345*7c478bd9Sstevel@tonic-gate 					if (passwdp == NULL) {
346*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
347*7c478bd9Sstevel@tonic-gate 						"user %s not found\n", fieldp);
348*7c478bd9Sstevel@tonic-gate 						id = UID_NOBODY; /* nobody */
349*7c478bd9Sstevel@tonic-gate 					}
350*7c478bd9Sstevel@tonic-gate 					else
351*7c478bd9Sstevel@tonic-gate 						id = passwdp->pw_uid;
352*7c478bd9Sstevel@tonic-gate 				} else {
353*7c478bd9Sstevel@tonic-gate 					if (entry_type == GROUP ||
354*7c478bd9Sstevel@tonic-gate 					    entry_type == DEF_GROUP) {
355*7c478bd9Sstevel@tonic-gate 						groupp = getgrnam(fieldp);
356*7c478bd9Sstevel@tonic-gate 						if (groupp == NULL) {
357*7c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
358*7c478bd9Sstevel@tonic-gate 							"group %s not found\n",
359*7c478bd9Sstevel@tonic-gate 							fieldp);
360*7c478bd9Sstevel@tonic-gate 							/* no group? */
361*7c478bd9Sstevel@tonic-gate 							id = GID_NOBODY;
362*7c478bd9Sstevel@tonic-gate 						}
363*7c478bd9Sstevel@tonic-gate 						else
364*7c478bd9Sstevel@tonic-gate 							id = groupp->gr_gid;
365*7c478bd9Sstevel@tonic-gate 					} else {
366*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
367*7c478bd9Sstevel@tonic-gate 						"acl import errors\n");
368*7c478bd9Sstevel@tonic-gate 						FREE;
369*7c478bd9Sstevel@tonic-gate 						return (NULL);
370*7c478bd9Sstevel@tonic-gate 					}
371*7c478bd9Sstevel@tonic-gate 				}
372*7c478bd9Sstevel@tonic-gate 			} else {
373*7c478bd9Sstevel@tonic-gate 				/*
374*7c478bd9Sstevel@tonic-gate 				 * The second field is empty.
375*7c478bd9Sstevel@tonic-gate 				 * Treat it as undefined (-1)
376*7c478bd9Sstevel@tonic-gate 				 */
377*7c478bd9Sstevel@tonic-gate 				id = -1;
378*7c478bd9Sstevel@tonic-gate 			}
379*7c478bd9Sstevel@tonic-gate 		} else {
380*7c478bd9Sstevel@tonic-gate 			/*
381*7c478bd9Sstevel@tonic-gate 			 * Let's not break the old applications
382*7c478bd9Sstevel@tonic-gate 			 * that use mask::rwx, other::rwx format,
383*7c478bd9Sstevel@tonic-gate 			 * though they violate the man pages.
384*7c478bd9Sstevel@tonic-gate 			 */
385*7c478bd9Sstevel@tonic-gate 			if (*(tp + 1) == ':')
386*7c478bd9Sstevel@tonic-gate 				*++tp = 0;
387*7c478bd9Sstevel@tonic-gate 		}
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 		/* next field: permission */
390*7c478bd9Sstevel@tonic-gate 		fieldp = tp + 1;
391*7c478bd9Sstevel@tonic-gate 		if (strlen(fieldp) != 3) {
392*7c478bd9Sstevel@tonic-gate 			/*  not "rwx" format */
393*7c478bd9Sstevel@tonic-gate 			FREE;
394*7c478bd9Sstevel@tonic-gate 			return (NULL);
395*7c478bd9Sstevel@tonic-gate 		} else {
396*7c478bd9Sstevel@tonic-gate 			char	s[] = "rwx";
397*7c478bd9Sstevel@tonic-gate 			int	mask = 0x04;
398*7c478bd9Sstevel@tonic-gate 			int	i;
399*7c478bd9Sstevel@tonic-gate 			perm = 0;
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < 3; i++, mask /= 2) {
402*7c478bd9Sstevel@tonic-gate 				if (fieldp[i] == s[i])
403*7c478bd9Sstevel@tonic-gate 					perm |= mask;
404*7c478bd9Sstevel@tonic-gate 				else if (fieldp[i] != '-') {
405*7c478bd9Sstevel@tonic-gate 					FREE;
406*7c478bd9Sstevel@tonic-gate 					return (NULL);
407*7c478bd9Sstevel@tonic-gate 				}
408*7c478bd9Sstevel@tonic-gate 			}
409*7c478bd9Sstevel@tonic-gate 		}
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 		tmpaclp->a_type = entry_type;
412*7c478bd9Sstevel@tonic-gate 		tmpaclp->a_id = id;
413*7c478bd9Sstevel@tonic-gate 		tmpaclp->a_perm = perm;
414*7c478bd9Sstevel@tonic-gate 		aclimport = nextp;
415*7c478bd9Sstevel@tonic-gate 	}
416*7c478bd9Sstevel@tonic-gate 	free(allocp);
417*7c478bd9Sstevel@tonic-gate 	return (aclp);
418*7c478bd9Sstevel@tonic-gate }
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate static char *
421*7c478bd9Sstevel@tonic-gate strappend(char *where, char *newstr)
422*7c478bd9Sstevel@tonic-gate {
423*7c478bd9Sstevel@tonic-gate 	(void) strcat(where, newstr);
424*7c478bd9Sstevel@tonic-gate 	return (where + strlen(newstr));
425*7c478bd9Sstevel@tonic-gate }
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate static char *
428*7c478bd9Sstevel@tonic-gate convert_perm(char *where, o_mode_t perm)
429*7c478bd9Sstevel@tonic-gate {
430*7c478bd9Sstevel@tonic-gate 	if (perm & 04)
431*7c478bd9Sstevel@tonic-gate 		where = strappend(where, "r");
432*7c478bd9Sstevel@tonic-gate 	else
433*7c478bd9Sstevel@tonic-gate 		where = strappend(where, "-");
434*7c478bd9Sstevel@tonic-gate 	if (perm & 02)
435*7c478bd9Sstevel@tonic-gate 		where = strappend(where, "w");
436*7c478bd9Sstevel@tonic-gate 	else
437*7c478bd9Sstevel@tonic-gate 		where = strappend(where, "-");
438*7c478bd9Sstevel@tonic-gate 	if (perm & 01)
439*7c478bd9Sstevel@tonic-gate 		where = strappend(where, "x");
440*7c478bd9Sstevel@tonic-gate 	else
441*7c478bd9Sstevel@tonic-gate 		where = strappend(where, "-");
442*7c478bd9Sstevel@tonic-gate 	/* perm is the last field */
443*7c478bd9Sstevel@tonic-gate 	return (where);
444*7c478bd9Sstevel@tonic-gate }
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate /*
447*7c478bd9Sstevel@tonic-gate  * Callers should check the return code as this routine may change the string
448*7c478bd9Sstevel@tonic-gate  * pointer in dynaclstr.
449*7c478bd9Sstevel@tonic-gate  */
450*7c478bd9Sstevel@tonic-gate static int
451*7c478bd9Sstevel@tonic-gate increase_length(struct dynaclstr *dacl, size_t increase)
452*7c478bd9Sstevel@tonic-gate {
453*7c478bd9Sstevel@tonic-gate 	char *tptr;
454*7c478bd9Sstevel@tonic-gate 	size_t newsize;
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 	newsize = dacl->bufsize + increase;
457*7c478bd9Sstevel@tonic-gate 	tptr = realloc(dacl->aclexport, newsize);
458*7c478bd9Sstevel@tonic-gate 	if (tptr != NULL) {
459*7c478bd9Sstevel@tonic-gate 		dacl->aclexport = tptr;
460*7c478bd9Sstevel@tonic-gate 		dacl->bufsize = newsize;
461*7c478bd9Sstevel@tonic-gate 		return (1);
462*7c478bd9Sstevel@tonic-gate 	} else
463*7c478bd9Sstevel@tonic-gate 		return (0);
464*7c478bd9Sstevel@tonic-gate }
465