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