xref: /titanic_51/usr/src/lib/libsec/common/acltext.c (revision db3659e514c8bf3f03bcca6dd082e363bd7b466a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*LINTLIBRARY*/
27 
28 #include <grp.h>
29 #include <pwd.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/acl.h>
38 #include <aclutils.h>
39 #include <idmap.h>
40 
41 #define	ID_STR_MAX	20	/* digits in LONG_MAX */
42 
43 #define	APPENDED_ID_MAX	ID_STR_MAX + 1		/* id + colon */
44 /*
45  * yyinteractive controls whether yyparse should print out
46  * error messages to stderr, and whether or not id's should be
47  * allowed from acl_fromtext().
48  */
49 int	yyinteractive;
50 acl_t	*yyacl;
51 char	*yybuf;
52 
53 extern acl_t *acl_alloc(enum acl_type);
54 
55 /*
56  * dynamic string that will increase in size on an
57  * as needed basis.
58  */
59 typedef struct dynaclstr {
60 	size_t d_bufsize;		/* current size of aclexport */
61 	char *d_aclexport;
62 	int d_pos;
63 } dynaclstr_t;
64 
65 static int str_append(dynaclstr_t *, char *);
66 static int aclent_perm_txt(dynaclstr_t *, o_mode_t);
67 
68 static void
69 aclent_perms(int perm, char *txt_perms)
70 {
71 	if (perm & S_IROTH)
72 		txt_perms[0] = 'r';
73 	else
74 		txt_perms[0] = '-';
75 	if (perm & S_IWOTH)
76 		txt_perms[1] = 'w';
77 	else
78 		txt_perms[1] = '-';
79 	if (perm & S_IXOTH)
80 		txt_perms[2] = 'x';
81 	else
82 		txt_perms[2] = '-';
83 	txt_perms[3] = '\0';
84 }
85 
86 static char *
87 pruname(uid_t uid, char *uidp, size_t buflen, int noresolve)
88 {
89 	struct passwd	*passwdp = NULL;
90 
91 	if (noresolve == 0)
92 		passwdp = getpwuid(uid);
93 	if (passwdp == (struct passwd *)NULL) {
94 		/* could not get passwd information: display uid instead */
95 		(void) snprintf(uidp, buflen, "%u", uid);
96 	} else {
97 		(void) strlcpy(uidp, passwdp->pw_name, buflen);
98 	}
99 	return (uidp);
100 }
101 
102 static char *
103 prgname(gid_t gid, char *gidp, size_t buflen, int noresolve)
104 {
105 	struct group	*groupp = NULL;
106 
107 	if (noresolve == 0)
108 		groupp = getgrgid(gid);
109 	if (groupp == (struct group *)NULL) {
110 		/* could not get group information: display gid instead */
111 		(void) snprintf(gidp, buflen, "%u", gid);
112 	} else {
113 		(void) strlcpy(gidp, groupp->gr_name, buflen);
114 	}
115 	return (gidp);
116 }
117 
118 static int
119 getsidname(uid_t who, boolean_t user, char **sidp, boolean_t noresolve)
120 {
121 	idmap_handle_t *idmap_hdl = NULL;
122 	idmap_get_handle_t *get_hdl = NULL;
123 	idmap_stat status;
124 	idmap_rid_t rid;
125 	int error = IDMAP_ERR_NORESULT;
126 	int len;
127 	char *domain;
128 	char *name;
129 
130 	*sidp = NULL;
131 
132 	/*
133 	 * First try and get windows name
134 	 */
135 
136 	if (!noresolve) {
137 		if (user)
138 			error = idmap_getwinnamebyuid(who,
139 			    IDMAP_REQ_FLG_USE_CACHE, &name, &domain);
140 		else
141 			error = idmap_getwinnamebygid(who,
142 			    IDMAP_REQ_FLG_USE_CACHE, &name, &domain);
143 	}
144 	if (error != IDMAP_SUCCESS) {
145 		if (idmap_init(&idmap_hdl) == IDMAP_SUCCESS &&
146 		    idmap_get_create(idmap_hdl, &get_hdl) == IDMAP_SUCCESS) {
147 			if (user)
148 				error = idmap_get_sidbyuid(get_hdl, who,
149 				    IDMAP_REQ_FLG_USE_CACHE, &domain, &rid,
150 				    &status);
151 			else
152 				error = idmap_get_sidbygid(get_hdl, who,
153 				    IDMAP_REQ_FLG_USE_CACHE, &domain, &rid,
154 				    &status);
155 			if (error == IDMAP_SUCCESS &&
156 			    idmap_get_mappings(get_hdl) == 0) {
157 				if (status == IDMAP_SUCCESS) {
158 					len = snprintf(NULL, 0,
159 					    "%s-%d", domain, rid);
160 					if (*sidp = malloc(len + 1)) {
161 						(void) snprintf(*sidp, len + 1,
162 						    "%s-%d", domain, rid);
163 					}
164 				}
165 			}
166 		}
167 		if (get_hdl)
168 			idmap_get_destroy(get_hdl);
169 		if (idmap_hdl)
170 			(void) idmap_fini(idmap_hdl);
171 	} else {
172 		int len;
173 
174 		len = snprintf(NULL, 0, "%s@%s", name, domain);
175 		if (*sidp = malloc(len + 1))
176 			(void) snprintf(*sidp, len + 1, "%s@%s", name, domain);
177 		free(name);
178 		free(domain);
179 	}
180 	return (*sidp ? 0 : 1);
181 }
182 
183 static void
184 aclent_printacl(acl_t *aclp)
185 {
186 	aclent_t *tp;
187 	int aclcnt;
188 	int mask;
189 	int slot = 0;
190 	char perm[4];
191 	char uidp[ID_STR_MAX];
192 	char gidp[ID_STR_MAX];
193 
194 	/* display ACL: assume it is sorted. */
195 	aclcnt = aclp->acl_cnt;
196 	for (tp = aclp->acl_aclp; tp && aclcnt--; tp++) {
197 		if (tp->a_type == CLASS_OBJ)
198 			mask = tp->a_perm;
199 	}
200 	aclcnt = aclp->acl_cnt;
201 	for (tp = aclp->acl_aclp; aclcnt--; tp++) {
202 		(void) printf("     %d:", slot++);
203 		switch (tp->a_type) {
204 		case USER:
205 			aclent_perms(tp->a_perm, perm);
206 			(void) printf("user:%s:%s\t\t",
207 			    pruname(tp->a_id, uidp, sizeof (uidp), 0), perm);
208 			aclent_perms((tp->a_perm & mask), perm);
209 			(void) printf("#effective:%s\n", perm);
210 			break;
211 		case USER_OBJ:
212 			/* no need to display uid */
213 			aclent_perms(tp->a_perm, perm);
214 			(void) printf("user::%s\n", perm);
215 			break;
216 		case GROUP:
217 			aclent_perms(tp->a_perm, perm);
218 			(void) printf("group:%s:%s\t\t",
219 			    prgname(tp->a_id, gidp, sizeof (gidp), 0), perm);
220 			aclent_perms(tp->a_perm & mask, perm);
221 			(void) printf("#effective:%s\n", perm);
222 			break;
223 		case GROUP_OBJ:
224 			aclent_perms(tp->a_perm, perm);
225 			(void) printf("group::%s\t\t", perm);
226 			aclent_perms(tp->a_perm & mask, perm);
227 			(void) printf("#effective:%s\n", perm);
228 			break;
229 		case CLASS_OBJ:
230 			aclent_perms(tp->a_perm, perm);
231 			(void) printf("mask:%s\n", perm);
232 			break;
233 		case OTHER_OBJ:
234 			aclent_perms(tp->a_perm, perm);
235 			(void) printf("other:%s\n", perm);
236 			break;
237 		case DEF_USER:
238 			aclent_perms(tp->a_perm, perm);
239 			(void) printf("default:user:%s:%s\n",
240 			    pruname(tp->a_id, uidp, sizeof (uidp), 0), perm);
241 			break;
242 		case DEF_USER_OBJ:
243 			aclent_perms(tp->a_perm, perm);
244 			(void) printf("default:user::%s\n", perm);
245 			break;
246 		case DEF_GROUP:
247 			aclent_perms(tp->a_perm, perm);
248 			(void) printf("default:group:%s:%s\n",
249 			    prgname(tp->a_id, gidp, sizeof (gidp), 0), perm);
250 			break;
251 		case DEF_GROUP_OBJ:
252 			aclent_perms(tp->a_perm, perm);
253 			(void) printf("default:group::%s\n", perm);
254 			break;
255 		case DEF_CLASS_OBJ:
256 			aclent_perms(tp->a_perm, perm);
257 			(void) printf("default:mask:%s\n", perm);
258 			break;
259 		case DEF_OTHER_OBJ:
260 			aclent_perms(tp->a_perm, perm);
261 			(void) printf("default:other:%s\n", perm);
262 			break;
263 		default:
264 			(void) fprintf(stderr,
265 			    dgettext(TEXT_DOMAIN, "unrecognized entry\n"));
266 			break;
267 		}
268 	}
269 }
270 
271 static void
272 split_line(char *str, int cols)
273 {
274 	char *ptr;
275 	int len;
276 	int i;
277 	int last_split;
278 	char *pad = "";
279 	int pad_len;
280 
281 	len = strlen(str);
282 	ptr = str;
283 	pad_len = 0;
284 
285 	ptr = str;
286 	last_split = 0;
287 	for (i = 0; i != len; i++) {
288 		if ((i + pad_len + 4) >= cols) {
289 			(void) printf("%s%.*s\n", pad, last_split, ptr);
290 			ptr = &ptr[last_split];
291 			len = strlen(ptr);
292 			i = 0;
293 			pad_len = 4;
294 			pad = "         ";
295 		} else {
296 			if (ptr[i] == '/' || ptr[i] == ':') {
297 				last_split = i;
298 			}
299 		}
300 	}
301 	if (i == len) {
302 		(void) printf("%s%s\n", pad, ptr);
303 	}
304 }
305 
306 /*
307  * compute entry type string, such as user:joe, group:staff,...
308  */
309 static int
310 aclent_type_txt(dynaclstr_t *dstr, aclent_t *aclp, int flags)
311 {
312 	char idp[ID_STR_MAX];
313 	int error;
314 
315 	switch (aclp->a_type) {
316 	case DEF_USER_OBJ:
317 	case USER_OBJ:
318 		if (aclp->a_type == USER_OBJ)
319 			error = str_append(dstr, "user::");
320 		else
321 			error = str_append(dstr, "defaultuser::");
322 		break;
323 
324 	case DEF_USER:
325 	case USER:
326 		if (aclp->a_type == USER)
327 			error = str_append(dstr, "user:");
328 		else
329 			error = str_append(dstr, "defaultuser:");
330 		if (error)
331 			break;
332 		error = str_append(dstr, pruname(aclp->a_id, idp,
333 		    sizeof (idp), flags & ACL_NORESOLVE));
334 		if (error == 0)
335 			error = str_append(dstr, ":");
336 		break;
337 
338 	case DEF_GROUP_OBJ:
339 	case GROUP_OBJ:
340 		if (aclp->a_type == GROUP_OBJ)
341 			error = str_append(dstr, "group::");
342 		else
343 			error = str_append(dstr, "defaultgroup::");
344 		break;
345 
346 	case DEF_GROUP:
347 	case GROUP:
348 		if (aclp->a_type == GROUP)
349 			error = str_append(dstr, "group:");
350 		else
351 			error = str_append(dstr, "defaultgroup:");
352 		if (error)
353 			break;
354 		error = str_append(dstr, prgname(aclp->a_id, idp,
355 		    sizeof (idp), flags & ACL_NORESOLVE));
356 		if (error == 0)
357 			error = str_append(dstr, ":");
358 		break;
359 
360 	case DEF_CLASS_OBJ:
361 	case CLASS_OBJ:
362 		if (aclp->a_type == CLASS_OBJ)
363 			error = str_append(dstr, "mask:");
364 		else
365 			error = str_append(dstr, "defaultmask:");
366 		break;
367 
368 	case DEF_OTHER_OBJ:
369 	case OTHER_OBJ:
370 		if (aclp->a_type == OTHER_OBJ)
371 			error = str_append(dstr, "other:");
372 		else
373 			error = str_append(dstr, "defaultother:");
374 		break;
375 
376 	default:
377 		error = 1;
378 		break;
379 	}
380 
381 	return (error);
382 }
383 
384 /*
385  * compute entry type string such as, owner@:, user:joe, group:staff,...
386  */
387 static int
388 ace_type_txt(dynaclstr_t *dynstr, ace_t *acep, int flags)
389 {
390 	char idp[ID_STR_MAX];
391 	int error;
392 	char *sidp = NULL;
393 
394 	switch (acep->a_flags & ACE_TYPE_FLAGS) {
395 	case ACE_OWNER:
396 		error = str_append(dynstr, OWNERAT_TXT);
397 		break;
398 
399 	case ACE_GROUP|ACE_IDENTIFIER_GROUP:
400 		error = str_append(dynstr, GROUPAT_TXT);
401 		break;
402 
403 	case ACE_IDENTIFIER_GROUP:
404 		if ((flags & ACL_SID_FMT) && acep->a_who > MAXUID) {
405 			if (error = str_append(dynstr,
406 			    GROUPSID_TXT))
407 				break;
408 			if (error = getsidname(acep->a_who, B_FALSE,
409 			    &sidp, flags & ACL_NORESOLVE))
410 				break;
411 			error = str_append(dynstr, sidp);
412 		} else {
413 			if (error = str_append(dynstr, GROUP_TXT))
414 				break;
415 			error = str_append(dynstr, prgname(acep->a_who, idp,
416 			    sizeof (idp), flags & ACL_NORESOLVE));
417 		}
418 		if (error == 0)
419 			error = str_append(dynstr, ":");
420 		break;
421 
422 	case ACE_EVERYONE:
423 		error = str_append(dynstr, EVERYONEAT_TXT);
424 		break;
425 
426 	case 0:
427 		if ((flags & ACL_SID_FMT) && acep->a_who > MAXUID) {
428 			if (error = str_append(dynstr, USERSID_TXT))
429 				break;
430 			if (error = getsidname(acep->a_who, B_TRUE,
431 			    &sidp, flags & ACL_NORESOLVE))
432 				break;
433 			error = str_append(dynstr, sidp);
434 		} else {
435 			if (error = str_append(dynstr, USER_TXT))
436 				break;
437 			error = str_append(dynstr, pruname(acep->a_who, idp,
438 			    sizeof (idp), flags & ACL_NORESOLVE));
439 		}
440 		if (error == 0)
441 			error = str_append(dynstr, ":");
442 		break;
443 	default:
444 		error = 0;
445 		break;
446 	}
447 
448 	if (sidp)
449 		free(sidp);
450 	return (error);
451 }
452 
453 /*
454  * compute string of permissions, such as read_data/write_data or
455  * rwxp,...
456  * The format depends on the flags field which indicates whether the compact
457  * or verbose format should be used.
458  */
459 static int
460 ace_perm_txt(dynaclstr_t *dstr, uint32_t mask,
461     uint32_t iflags, int isdir, int flags)
462 {
463 	int error = 0;
464 
465 	if (flags & ACL_COMPACT_FMT) {
466 		char buf[16];
467 
468 		if (mask & ACE_READ_DATA)
469 			buf[0] = 'r';
470 		else
471 			buf[0] = '-';
472 		if (mask & ACE_WRITE_DATA)
473 			buf[1] = 'w';
474 		else
475 			buf[1] = '-';
476 		if (mask & ACE_EXECUTE)
477 			buf[2] = 'x';
478 		else
479 			buf[2] = '-';
480 		if (mask & ACE_APPEND_DATA)
481 			buf[3] = 'p';
482 		else
483 			buf[3] = '-';
484 		if (mask & ACE_DELETE)
485 			buf[4] = 'd';
486 		else
487 			buf[4] = '-';
488 		if (mask & ACE_DELETE_CHILD)
489 			buf[5] = 'D';
490 		else
491 			buf[5] = '-';
492 		if (mask & ACE_READ_ATTRIBUTES)
493 			buf[6] = 'a';
494 		else
495 			buf[6] = '-';
496 		if (mask & ACE_WRITE_ATTRIBUTES)
497 			buf[7] = 'A';
498 		else
499 			buf[7] = '-';
500 		if (mask & ACE_READ_NAMED_ATTRS)
501 			buf[8] = 'R';
502 		else
503 			buf[8] = '-';
504 		if (mask & ACE_WRITE_NAMED_ATTRS)
505 			buf[9] = 'W';
506 		else
507 			buf[9] = '-';
508 		if (mask & ACE_READ_ACL)
509 			buf[10] = 'c';
510 		else
511 			buf[10] = '-';
512 		if (mask & ACE_WRITE_ACL)
513 			buf[11] = 'C';
514 		else
515 			buf[11] = '-';
516 		if (mask & ACE_WRITE_OWNER)
517 			buf[12] = 'o';
518 		else
519 			buf[12] = '-';
520 		if (mask & ACE_SYNCHRONIZE)
521 			buf[13] = 's';
522 		else
523 			buf[13] = '-';
524 		buf[14] = ':';
525 		buf[15] = '\0';
526 		error = str_append(dstr, buf);
527 	} else {
528 		/*
529 		 * If ACE is a directory, but inheritance indicates its
530 		 * for a file then print permissions for file rather than
531 		 * dir.
532 		 */
533 		if (isdir) {
534 			if (mask & ACE_LIST_DIRECTORY) {
535 				if (iflags == ACE_FILE_INHERIT_ACE) {
536 					error = str_append(dstr,
537 					    READ_DATA_TXT);
538 				} else {
539 					error =
540 					    str_append(dstr, READ_DIR_TXT);
541 				}
542 			}
543 			if (error == 0 && (mask & ACE_ADD_FILE)) {
544 				if (iflags == ACE_FILE_INHERIT_ACE) {
545 					error =
546 					    str_append(dstr, WRITE_DATA_TXT);
547 				} else {
548 					error =
549 					    str_append(dstr, ADD_FILE_TXT);
550 				}
551 			}
552 			if (error == 0 && (mask & ACE_ADD_SUBDIRECTORY)) {
553 				if (iflags == ACE_FILE_INHERIT_ACE) {
554 					error = str_append(dstr,
555 					    APPEND_DATA_TXT);
556 				} else {
557 					error = str_append(dstr,
558 					    ADD_DIR_TXT);
559 				}
560 			}
561 		} else {
562 			if (mask & ACE_READ_DATA) {
563 				error = str_append(dstr, READ_DATA_TXT);
564 			}
565 			if (error == 0 && (mask & ACE_WRITE_DATA)) {
566 				error = str_append(dstr, WRITE_DATA_TXT);
567 			}
568 			if (error == 0 && (mask & ACE_APPEND_DATA)) {
569 				error = str_append(dstr, APPEND_DATA_TXT);
570 			}
571 		}
572 		if (error == 0 && (mask & ACE_READ_NAMED_ATTRS)) {
573 			error = str_append(dstr, READ_XATTR_TXT);
574 		}
575 		if (error == 0 && (mask & ACE_WRITE_NAMED_ATTRS)) {
576 			error = str_append(dstr, WRITE_XATTR_TXT);
577 		}
578 		if (error == 0 && (mask & ACE_EXECUTE)) {
579 			error = str_append(dstr, EXECUTE_TXT);
580 		}
581 		if (error == 0 && (mask & ACE_DELETE_CHILD)) {
582 			error = str_append(dstr, DELETE_CHILD_TXT);
583 		}
584 		if (error == 0 && (mask & ACE_READ_ATTRIBUTES)) {
585 			error = str_append(dstr, READ_ATTRIBUTES_TXT);
586 		}
587 		if (error == 0 && (mask & ACE_WRITE_ATTRIBUTES)) {
588 			error = str_append(dstr, WRITE_ATTRIBUTES_TXT);
589 		}
590 		if (error == 0 && (mask & ACE_DELETE)) {
591 			error = str_append(dstr, DELETE_TXT);
592 		}
593 		if (error == 0 && (mask & ACE_READ_ACL)) {
594 			error = str_append(dstr, READ_ACL_TXT);
595 		}
596 		if (error == 0 && (mask & ACE_WRITE_ACL)) {
597 			error = str_append(dstr, WRITE_ACL_TXT);
598 		}
599 		if (error == 0 && (mask & ACE_WRITE_OWNER)) {
600 			error = str_append(dstr, WRITE_OWNER_TXT);
601 		}
602 		if (error == 0 && (mask & ACE_SYNCHRONIZE)) {
603 			error = str_append(dstr, SYNCHRONIZE_TXT);
604 		}
605 		if (error == 0 && dstr->d_aclexport[dstr->d_pos-1] == '/') {
606 			dstr->d_aclexport[--dstr->d_pos] = '\0';
607 		}
608 		if (error == 0)
609 			error = str_append(dstr, ":");
610 	}
611 	return (error);
612 }
613 
614 /*
615  * compute string of access type, such as allow, deny, ...
616  */
617 static int
618 ace_access_txt(dynaclstr_t *dstr, int type)
619 {
620 	int error;
621 
622 	if (type == ACE_ACCESS_ALLOWED_ACE_TYPE)
623 		error = str_append(dstr, ALLOW_TXT);
624 	else if (type == ACE_ACCESS_DENIED_ACE_TYPE)
625 		error = str_append(dstr, DENY_TXT);
626 	else if (type == ACE_SYSTEM_AUDIT_ACE_TYPE)
627 		error = str_append(dstr, AUDIT_TXT);
628 	else if (type == ACE_SYSTEM_ALARM_ACE_TYPE)
629 		error = str_append(dstr, ALARM_TXT);
630 	else
631 		error = str_append(dstr, UNKNOWN_TXT);
632 
633 	return (error);
634 }
635 
636 static int
637 ace_inherit_txt(dynaclstr_t *dstr, uint32_t iflags, int flags)
638 {
639 	int error = 0;
640 
641 	if (flags & ACL_COMPACT_FMT) {
642 		char buf[9];
643 
644 		if (iflags & ACE_FILE_INHERIT_ACE)
645 			buf[0] = 'f';
646 		else
647 			buf[0] = '-';
648 		if (iflags & ACE_DIRECTORY_INHERIT_ACE)
649 			buf[1] = 'd';
650 		else
651 			buf[1] = '-';
652 		if (iflags & ACE_INHERIT_ONLY_ACE)
653 			buf[2] = 'i';
654 		else
655 			buf[2] = '-';
656 		if (iflags & ACE_NO_PROPAGATE_INHERIT_ACE)
657 			buf[3] = 'n';
658 		else
659 			buf[3] = '-';
660 		if (iflags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG)
661 			buf[4] = 'S';
662 		else
663 			buf[4] = '-';
664 		if (iflags & ACE_FAILED_ACCESS_ACE_FLAG)
665 			buf[5] = 'F';
666 		else
667 			buf[5] = '-';
668 		if (iflags & ACE_INHERITED_ACE)
669 			buf[6] = 'I';
670 		else
671 			buf[6] = '-';
672 		buf[7] = ':';
673 		buf[8] = '\0';
674 		error = str_append(dstr, buf);
675 	} else {
676 		if (iflags & ACE_FILE_INHERIT_ACE) {
677 			error = str_append(dstr, FILE_INHERIT_TXT);
678 		}
679 		if (error == 0 && (iflags & ACE_DIRECTORY_INHERIT_ACE)) {
680 			error = str_append(dstr, DIR_INHERIT_TXT);
681 		}
682 		if (error == 0 && (iflags & ACE_NO_PROPAGATE_INHERIT_ACE)) {
683 			error = str_append(dstr, NO_PROPAGATE_TXT);
684 		}
685 		if (error == 0 && (iflags & ACE_INHERIT_ONLY_ACE)) {
686 			error = str_append(dstr, INHERIT_ONLY_TXT);
687 		}
688 		if (error == 0 && (iflags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG)) {
689 			error = str_append(dstr, SUCCESSFUL_ACCESS_TXT);
690 		}
691 		if (error == 0 && (iflags & ACE_FAILED_ACCESS_ACE_FLAG)) {
692 			error = str_append(dstr, FAILED_ACCESS_TXT);
693 		}
694 		if (error == 0 && (iflags & ACE_INHERITED_ACE)) {
695 			error = str_append(dstr, INHERITED_ACE_TXT);
696 		}
697 		if (error == 0 && dstr->d_aclexport[dstr->d_pos-1] == '/') {
698 			dstr->d_aclexport[--dstr->d_pos] = '\0';
699 			error = str_append(dstr, ":");
700 		}
701 	}
702 
703 	return (error);
704 }
705 
706 /*
707  * Convert internal acl representation to external representation.
708  *
709  * The length of a non-owning user name or non-owning group name ie entries
710  * of type DEF_USER, USER, DEF_GROUP or GROUP, can exceed LOGNAME_MAX.  We
711  * thus check the length of these entries, and if greater than LOGNAME_MAX,
712  * we realloc() via increase_length().
713  *
714  * The LOGNAME_MAX, ENTRYTYPELEN and PERMS limits are otherwise always
715  * adhered to.
716  */
717 
718 /*
719  * acltotext() converts each ACL entry to look like this:
720  *
721  *    entry_type:uid^gid^name:perms[:id]
722  *
723  * The maximum length of entry_type is 14 ("defaultgroup::" and
724  * "defaultother::") hence ENTRYTYPELEN is set to 14.
725  *
726  * The max length of a uid^gid^name entry (in theory) is 8, hence we use,
727  * however the ID could be a number so we therefore use ID_STR_MAX
728  *
729  * The length of a perms entry is 4 to allow for the comma appended to each
730  * to each acl entry.  Hence PERMS is set to 4.
731  */
732 
733 #define	ENTRYTYPELEN	14
734 #define	PERMS		4
735 #define	ACL_ENTRY_SIZE	(ENTRYTYPELEN + ID_STR_MAX + PERMS + APPENDED_ID_MAX)
736 
737 char *
738 aclent_acltotext(aclent_t  *aclp, int aclcnt, int flags)
739 {
740 	dynaclstr_t 	*dstr;
741 	char		*aclexport = NULL;
742 	int		i;
743 	int 		error = 0;
744 
745 	if (aclp == NULL)
746 		return (NULL);
747 	if ((dstr = malloc(sizeof (dynaclstr_t))) == NULL)
748 		return (NULL);
749 	dstr->d_bufsize = aclcnt * ACL_ENTRY_SIZE;
750 	if ((dstr->d_aclexport = malloc(dstr->d_bufsize)) == NULL) {
751 		free(dstr);
752 		return (NULL);
753 	}
754 	*dstr->d_aclexport = '\0';
755 	dstr->d_pos = 0;
756 
757 	for (i = 0; i < aclcnt; i++, aclp++) {
758 		if (error = aclent_type_txt(dstr, aclp, flags))
759 			break;
760 		if (error = aclent_perm_txt(dstr, aclp->a_perm))
761 			break;
762 
763 		if ((flags & ACL_APPEND_ID) && ((aclp->a_type == USER) ||
764 		    (aclp->a_type == DEF_USER) || (aclp->a_type == GROUP) ||
765 		    (aclp->a_type == DEF_GROUP))) {
766 			char id[ID_STR_MAX], *idstr;
767 
768 			if (error = str_append(dstr, ":"))
769 				break;
770 			id[ID_STR_MAX - 1] = '\0'; /* null terminate buffer */
771 			idstr = lltostr(aclp->a_id, &id[ID_STR_MAX - 1]);
772 			if (error = str_append(dstr, idstr))
773 				break;
774 		}
775 		if (i < aclcnt - 1)
776 			if (error = str_append(dstr, ","))
777 				break;
778 	}
779 	if (error) {
780 		if (dstr->d_aclexport)
781 			free(dstr->d_aclexport);
782 	} else {
783 		aclexport = dstr->d_aclexport;
784 	}
785 	free(dstr);
786 	return (aclexport);
787 }
788 
789 char *
790 acltotext(aclent_t *aclp, int aclcnt)
791 {
792 	return (aclent_acltotext(aclp, aclcnt, 0));
793 }
794 
795 
796 aclent_t *
797 aclfromtext(char *aclstr, int *aclcnt)
798 {
799 	acl_t *aclp;
800 	aclent_t *aclentp;
801 	int error;
802 
803 	error = acl_fromtext(aclstr, &aclp);
804 	if (error)
805 		return (NULL);
806 
807 	aclentp = aclp->acl_aclp;
808 	aclp->acl_aclp = NULL;
809 	*aclcnt = aclp->acl_cnt;
810 
811 	acl_free(aclp);
812 	return (aclentp);
813 }
814 
815 
816 /*
817  * Append string onto dynaclstr_t.
818  *
819  * Return 0 on success, 1 for failure.
820  */
821 static int
822 str_append(dynaclstr_t *dstr, char *newstr)
823 {
824 	size_t len = strlen(newstr);
825 
826 	if ((len + dstr->d_pos) >= dstr->d_bufsize) {
827 		dstr->d_aclexport = realloc(dstr->d_aclexport,
828 		    dstr->d_bufsize + len + 1);
829 		if (dstr->d_aclexport == NULL)
830 			return (1);
831 		dstr->d_bufsize += len;
832 	}
833 	(void) strcat(&dstr->d_aclexport[dstr->d_pos], newstr);
834 	dstr->d_pos += len;
835 	return (0);
836 }
837 
838 static int
839 aclent_perm_txt(dynaclstr_t *dstr, o_mode_t perm)
840 {
841 	char buf[4];
842 
843 	if (perm & S_IROTH)
844 		buf[0] = 'r';
845 	else
846 		buf[0] = '-';
847 	if (perm & S_IWOTH)
848 		buf[1] = 'w';
849 	else
850 		buf[1] = '-';
851 	if (perm & S_IXOTH)
852 		buf[2] = 'x';
853 	else
854 		buf[2] = '-';
855 	buf[3] = '\0';
856 	return (str_append(dstr, buf));
857 }
858 
859 /*
860  * ace_acltotext() convert each ace formatted acl to look like this:
861  *
862  * entry_type:uid^gid^name:perms[:flags]:<allow|deny>[:id][,]
863  *
864  * The maximum length of entry_type is 5 ("group")
865  *
866  * The max length of a uid^gid^name entry (in theory) is 8,
867  * however id could be a number so we therefore use ID_STR_MAX
868  *
869  * The length of a perms entry is 144 i.e read_data/write_data...
870  * to each acl entry.
871  *
872  * iflags: file_inherit/dir_inherit/inherit_only/no_propagate/successful_access
873  *         /failed_access
874  *
875  */
876 
877 #define	ACE_ENTRYTYPLEN		6
878 #define	IFLAGS_STR "file_inherit/dir_inherit/inherit_only/no_propagate/" \
879 	"successful_access/failed_access/inherited"
880 #define	IFLAGS_SIZE		(sizeof (IFLAGS_STR) - 1)
881 #define	ACCESS_TYPE_SIZE	7	/* if unknown */
882 #define	COLON_CNT		3
883 #define	PERMS_LEN		216
884 #define	ACE_ENTRY_SIZE	(ACE_ENTRYTYPLEN + ID_STR_MAX + PERMS_LEN + \
885     ACCESS_TYPE_SIZE + IFLAGS_SIZE + COLON_CNT + APPENDED_ID_MAX)
886 
887 static char *
888 ace_acltotext(acl_t *aceaclp, int flags)
889 {
890 	ace_t		*aclp = aceaclp->acl_aclp;
891 	int		aclcnt = aceaclp->acl_cnt;
892 	int		i;
893 	int		error = 0;
894 	int		isdir = (aceaclp->acl_flags & ACL_IS_DIR);
895 	dynaclstr_t 	*dstr;
896 	char		*aclexport = NULL;
897 
898 	if (aclp == NULL)
899 		return (NULL);
900 
901 	if ((dstr = malloc(sizeof (dynaclstr_t))) == NULL)
902 		return (NULL);
903 	dstr->d_bufsize = aclcnt * ACL_ENTRY_SIZE;
904 	if ((dstr->d_aclexport = malloc(dstr->d_bufsize)) == NULL) {
905 		free(dstr);
906 		return (NULL);
907 	}
908 	*dstr->d_aclexport = '\0';
909 	dstr->d_pos = 0;
910 
911 	for (i = 0; i < aclcnt; i++, aclp++) {
912 
913 		if (error = ace_type_txt(dstr, aclp, flags))
914 			break;
915 		if (error = ace_perm_txt(dstr, aclp->a_access_mask,
916 		    aclp->a_flags, isdir, flags))
917 			break;
918 		if (error = ace_inherit_txt(dstr, aclp->a_flags, flags))
919 			break;
920 		if (error = ace_access_txt(dstr, aclp->a_type))
921 			break;
922 
923 		if ((flags & ACL_APPEND_ID) &&
924 		    (((aclp->a_flags & ACE_TYPE_FLAGS) == 0) ||
925 		    ((aclp->a_flags & ACE_TYPE_FLAGS) ==
926 		    ACE_IDENTIFIER_GROUP))) {
927 			char id[ID_STR_MAX], *idstr;
928 			char *rawsidp;
929 
930 			if (error = str_append(dstr, ":"))
931 				break;
932 
933 			rawsidp = NULL;
934 			id[ID_STR_MAX -1] = '\0'; /* null terminate */
935 			if (aclp->a_who > MAXUID && (flags & ACL_SID_FMT)) {
936 
937 				error = getsidname(aclp->a_who,
938 				    ((aclp->a_flags & ACE_TYPE_FLAGS) == 0) ?
939 				    B_TRUE : B_FALSE, &idstr, 1);
940 				if (error)
941 					break;
942 				rawsidp = idstr;
943 			} else if (aclp->a_who > MAXUID &&
944 			    !(flags & ACL_NORESOLVE)) {
945 				idstr = lltostr(UID_NOBODY,
946 				    &id[ID_STR_MAX - 1]);
947 			} else {
948 				idstr = lltostr(aclp->a_who,
949 				    &id[ID_STR_MAX - 1]);
950 			}
951 			if (error = str_append(dstr, idstr))
952 				break;
953 			if (rawsidp)
954 				free(rawsidp);
955 		}
956 		if (i < aclcnt - 1) {
957 			if (error = str_append(dstr, ","))
958 				break;
959 		}
960 	}
961 	if (error) {
962 		if (dstr->d_aclexport)
963 			free(dstr->d_aclexport);
964 	} else {
965 		aclexport = dstr->d_aclexport;
966 	}
967 	free(dstr);
968 	return (aclexport);
969 }
970 
971 char *
972 acl_totext(acl_t *aclp, int flags)
973 {
974 	char *txtp;
975 
976 	if (aclp == NULL)
977 		return (NULL);
978 
979 	switch (aclp->acl_type) {
980 	case ACE_T:
981 		txtp = ace_acltotext(aclp, flags);
982 		break;
983 	case ACLENT_T:
984 		txtp = aclent_acltotext(aclp->acl_aclp, aclp->acl_cnt, flags);
985 		break;
986 	}
987 
988 	return (txtp);
989 }
990 
991 int
992 acl_fromtext(const char *acltextp, acl_t **ret_aclp)
993 {
994 	int error;
995 	char *buf;
996 
997 	buf = malloc(strlen(acltextp) + 2);
998 	if (buf == NULL)
999 		return (EACL_MEM_ERROR);
1000 	strcpy(buf, acltextp);
1001 	strcat(buf, "\n");
1002 	yybuf = buf;
1003 	yyreset();
1004 	error = yyparse();
1005 	free(buf);
1006 
1007 	if (yyacl) {
1008 		if (error == 0)
1009 			*ret_aclp = yyacl;
1010 		else {
1011 			acl_free(yyacl);
1012 		}
1013 		yyacl = NULL;
1014 	}
1015 	return (error);
1016 }
1017 
1018 int
1019 acl_parse(const char *acltextp, acl_t **aclp)
1020 {
1021 	int error;
1022 
1023 	yyinteractive = 1;
1024 	error = acl_fromtext(acltextp, aclp);
1025 	yyinteractive = 0;
1026 	return (error);
1027 }
1028 
1029 static void
1030 ace_compact_printacl(acl_t *aclp)
1031 {
1032 	int cnt;
1033 	ace_t *acep;
1034 	dynaclstr_t *dstr;
1035 	int len;
1036 
1037 	if ((dstr = malloc(sizeof (dynaclstr_t))) == NULL)
1038 		return;
1039 	dstr->d_bufsize = ACE_ENTRY_SIZE;
1040 	if ((dstr->d_aclexport = malloc(dstr->d_bufsize)) == NULL) {
1041 		free(dstr);
1042 		return;
1043 	}
1044 	*dstr->d_aclexport = '\0';
1045 
1046 	dstr->d_pos = 0;
1047 	for (cnt = 0, acep = aclp->acl_aclp;
1048 	    cnt != aclp->acl_cnt; cnt++, acep++) {
1049 		dstr->d_aclexport[0] = '\0';
1050 		dstr->d_pos = 0;
1051 
1052 		if (ace_type_txt(dstr, acep, 0))
1053 			break;
1054 		len = strlen(&dstr->d_aclexport[0]);
1055 		if (ace_perm_txt(dstr, acep->a_access_mask, acep->a_flags,
1056 		    aclp->acl_flags & ACL_IS_DIR, ACL_COMPACT_FMT))
1057 			break;
1058 		if (ace_inherit_txt(dstr, acep->a_flags, ACL_COMPACT_FMT))
1059 			break;
1060 		if (ace_access_txt(dstr, acep->a_type) == -1)
1061 			break;
1062 		(void) printf("    %20.*s%s\n", len, dstr->d_aclexport,
1063 		    &dstr->d_aclexport[len]);
1064 	}
1065 
1066 	if (dstr->d_aclexport)
1067 		free(dstr->d_aclexport);
1068 	free(dstr);
1069 }
1070 
1071 static void
1072 ace_printacl(acl_t *aclp, int cols, int compact)
1073 {
1074 	int  slot = 0;
1075 	char *token;
1076 	char *acltext;
1077 
1078 	if (compact) {
1079 		ace_compact_printacl(aclp);
1080 		return;
1081 	}
1082 
1083 	acltext = acl_totext(aclp, 0);
1084 
1085 	if (acltext == NULL)
1086 		return;
1087 
1088 	token = strtok(acltext, ",");
1089 	if (token == NULL) {
1090 		free(acltext);
1091 		return;
1092 	}
1093 
1094 	do {
1095 		(void) printf("     %d:", slot++);
1096 		split_line(token, cols - 5);
1097 	} while (token = strtok(NULL, ","));
1098 	free(acltext);
1099 }
1100 
1101 /*
1102  * pretty print an ACL.
1103  * For aclent_t ACL's the format is
1104  * similar to the old format used by getfacl,
1105  * with the addition of adding a "slot" number
1106  * before each entry.
1107  *
1108  * for ace_t ACL's the cols variable will break up
1109  * the long lines into multiple lines and will also
1110  * print a "slot" number.
1111  */
1112 void
1113 acl_printacl(acl_t *aclp, int cols, int compact)
1114 {
1115 
1116 	switch (aclp->acl_type) {
1117 	case ACLENT_T:
1118 		aclent_printacl(aclp);
1119 		break;
1120 	case ACE_T:
1121 		ace_printacl(aclp, cols, compact);
1122 		break;
1123 	}
1124 }
1125 
1126 typedef struct value_table {
1127 	char		p_letter; /* perm letter such as 'r' */
1128 	uint32_t	p_value; /* value for perm when pletter found */
1129 } value_table_t;
1130 
1131 /*
1132  * The permission tables are laid out in positional order
1133  * a '-' character will indicate a permission at a given
1134  * position is not specified.  The '-' is not part of the
1135  * table, but will be checked for in the permission computation
1136  * routine.
1137  */
1138 value_table_t ace_perm_table[] = {
1139 	{ 'r', ACE_READ_DATA},
1140 	{ 'w', ACE_WRITE_DATA},
1141 	{ 'x', ACE_EXECUTE},
1142 	{ 'p', ACE_APPEND_DATA},
1143 	{ 'd', ACE_DELETE},
1144 	{ 'D', ACE_DELETE_CHILD},
1145 	{ 'a', ACE_READ_ATTRIBUTES},
1146 	{ 'A', ACE_WRITE_ATTRIBUTES},
1147 	{ 'R', ACE_READ_NAMED_ATTRS},
1148 	{ 'W', ACE_WRITE_NAMED_ATTRS},
1149 	{ 'c', ACE_READ_ACL},
1150 	{ 'C', ACE_WRITE_ACL},
1151 	{ 'o', ACE_WRITE_OWNER},
1152 	{ 's', ACE_SYNCHRONIZE}
1153 };
1154 
1155 #define	ACE_PERM_COUNT (sizeof (ace_perm_table) / sizeof (value_table_t))
1156 
1157 value_table_t aclent_perm_table[] = {
1158 	{ 'r', S_IROTH},
1159 	{ 'w', S_IWOTH},
1160 	{ 'x', S_IXOTH}
1161 };
1162 
1163 #define	ACLENT_PERM_COUNT (sizeof (aclent_perm_table) / sizeof (value_table_t))
1164 
1165 value_table_t inherit_table[] = {
1166 	{'f', ACE_FILE_INHERIT_ACE},
1167 	{'d', ACE_DIRECTORY_INHERIT_ACE},
1168 	{'i', ACE_INHERIT_ONLY_ACE},
1169 	{'n', ACE_NO_PROPAGATE_INHERIT_ACE},
1170 	{'S', ACE_SUCCESSFUL_ACCESS_ACE_FLAG},
1171 	{'F', ACE_FAILED_ACCESS_ACE_FLAG},
1172 	{'I', ACE_INHERITED_ACE}
1173 };
1174 
1175 #define	IFLAG_COUNT (sizeof (inherit_table) / sizeof (value_table_t))
1176 #define	IFLAG_COUNT_V1 6 /* Older version compatibility */
1177 
1178 /*
1179  * compute value from a permission table or inheritance table
1180  * based on string passed in.  If positional is set then
1181  * string must match order in permtab, otherwise any order
1182  * is allowed.
1183  */
1184 int
1185 compute_values(value_table_t *permtab, int count,
1186     char *permstr, int positional, uint32_t *mask)
1187 {
1188 	uint32_t perm_val = 0;
1189 	char *pstr;
1190 	int i, found;
1191 
1192 	if (count < 0)
1193 		return (1);
1194 
1195 	if (positional) {
1196 		for (i = 0, pstr = permstr; i != count && pstr &&
1197 		    *pstr; i++, pstr++) {
1198 			if (*pstr == permtab[i].p_letter) {
1199 				perm_val |= permtab[i].p_value;
1200 			} else if (*pstr != '-') {
1201 				return (1);
1202 			}
1203 		}
1204 	} else {  /* random order single letters with no '-' */
1205 		for (pstr = permstr; pstr && *pstr; pstr++) {
1206 			for (found = 0, i = 0; i != count; i++) {
1207 				if (*pstr == permtab[i].p_letter) {
1208 					perm_val |= permtab[i].p_value;
1209 					found = 1;
1210 					break;
1211 				}
1212 			}
1213 			if (found == 0)
1214 				return (1);
1215 		}
1216 	}
1217 
1218 	*mask = perm_val;
1219 	return (0);
1220 }
1221 
1222 
1223 int
1224 ace_inherit_helper(char *str, uint32_t *imask, int table_length)
1225 {
1226 	int rc = 0;
1227 
1228 	if (strlen(str) == table_length) {
1229 		/*
1230 		 * If the string == table_length then first check to see it's
1231 		 * in positional format.  If that fails then see if it's in
1232 		 * non-positional format.
1233 		 */
1234 		if (compute_values(inherit_table, table_length, str,
1235 		    1, imask) && compute_values(inherit_table,
1236 		    table_length, str, 0, imask)) {
1237 			rc = 1;
1238 		}
1239 	} else {
1240 		rc = compute_values(inherit_table, table_length, str, 0, imask);
1241 	}
1242 
1243 	return (rc ? EACL_INHERIT_ERROR : 0);
1244 }
1245 
1246 /*
1247  * compute value for inheritance flags.
1248  */
1249 int
1250 compute_ace_inherit(char *str, uint32_t *imask)
1251 {
1252 	int rc = 0;
1253 
1254 	rc = ace_inherit_helper(str, imask, IFLAG_COUNT);
1255 
1256 	if (rc && strlen(str) != IFLAG_COUNT) {
1257 
1258 		/* is it an old formatted inherit string? */
1259 		rc = ace_inherit_helper(str, imask, IFLAG_COUNT_V1);
1260 	}
1261 
1262 	return (rc);
1263 }
1264 
1265 
1266 /*
1267  * compute value for ACE permissions.
1268  */
1269 int
1270 compute_ace_perms(char *str, uint32_t *mask)
1271 {
1272 	int positional = 0;
1273 	int error;
1274 
1275 	if (strlen(str) == ACE_PERM_COUNT)
1276 		positional = 1;
1277 
1278 	error = compute_values(ace_perm_table, ACE_PERM_COUNT,
1279 	    str, positional, mask);
1280 
1281 	if (error && positional) {
1282 		/*
1283 		 * If positional was set, then make sure permissions
1284 		 * aren't actually valid in non positional case where
1285 		 * all permissions are specified, just in random order.
1286 		 */
1287 		error = compute_values(ace_perm_table,
1288 		    ACE_PERM_COUNT, str, 0, mask);
1289 	}
1290 	if (error)
1291 		error = EACL_PERM_MASK_ERROR;
1292 
1293 	return (error);
1294 }
1295 
1296 
1297 
1298 /*
1299  * compute values for aclent permissions.
1300  */
1301 int
1302 compute_aclent_perms(char *str, o_mode_t *mask)
1303 {
1304 	int error;
1305 	uint32_t pmask;
1306 
1307 	if (strlen(str) != ACLENT_PERM_COUNT)
1308 		return (EACL_PERM_MASK_ERROR);
1309 
1310 	*mask = 0;
1311 	error = compute_values(aclent_perm_table, ACLENT_PERM_COUNT,
1312 	    str, 1, &pmask);
1313 	if (error == 0) {
1314 		*mask = (o_mode_t)pmask;
1315 	} else
1316 		error = EACL_PERM_MASK_ERROR;
1317 	return (error);
1318 }
1319 
1320 /*
1321  * determine ACE permissions.
1322  */
1323 int
1324 ace_perm_mask(struct acl_perm_type *aclperm, uint32_t *mask)
1325 {
1326 	int error;
1327 
1328 	if (aclperm->perm_style == PERM_TYPE_EMPTY) {
1329 		*mask = 0;
1330 		return (0);
1331 	}
1332 
1333 	if (aclperm->perm_style == PERM_TYPE_ACE) {
1334 		*mask = aclperm->perm_val;
1335 		return (0);
1336 	}
1337 
1338 	error = compute_ace_perms(aclperm->perm_str, mask);
1339 	if (error) {
1340 		acl_error(dgettext(TEXT_DOMAIN,
1341 		    "Invalid permission(s) '%s' specified\n"),
1342 		    aclperm->perm_str);
1343 		return (EACL_PERM_MASK_ERROR);
1344 	}
1345 
1346 	return (0);
1347 }
1348