xref: /dflybsd-src/lib/libposix1e/acl_support.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino  * Copyright (c) 1999 Robert N. M. Watson
3*86d7f5d3SJohn Marino  * All rights reserved.
4*86d7f5d3SJohn Marino  *
5*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino  * are met:
8*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
11*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
12*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
13*86d7f5d3SJohn Marino  *
14*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*86d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*86d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*86d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*86d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*86d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*86d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*86d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*86d7f5d3SJohn Marino  * SUCH DAMAGE.
25*86d7f5d3SJohn Marino  *
26*86d7f5d3SJohn Marino  *$FreeBSD: src/lib/libposix1e/acl_support.c,v 1.3 2000/01/26 04:19:37 rwatson Exp $
27*86d7f5d3SJohn Marino  *$DragonFly: src/lib/libposix1e/acl_support.c,v 1.3 2005/08/04 17:27:09 drhodus Exp $
28*86d7f5d3SJohn Marino  */
29*86d7f5d3SJohn Marino /*
30*86d7f5d3SJohn Marino  * Support functionality for the POSIX.1e ACL interface
31*86d7f5d3SJohn Marino  * These calls are intended only to be called within the library.
32*86d7f5d3SJohn Marino  */
33*86d7f5d3SJohn Marino 
34*86d7f5d3SJohn Marino #include <sys/types.h>
35*86d7f5d3SJohn Marino #include <sys/acl.h>
36*86d7f5d3SJohn Marino #include <errno.h>
37*86d7f5d3SJohn Marino #include <grp.h>
38*86d7f5d3SJohn Marino #include <pwd.h>
39*86d7f5d3SJohn Marino #include <stdio.h>
40*86d7f5d3SJohn Marino #include <stdlib.h>
41*86d7f5d3SJohn Marino 
42*86d7f5d3SJohn Marino #include "acl_support.h"
43*86d7f5d3SJohn Marino 
44*86d7f5d3SJohn Marino #define ACL_STRING_PERM_WRITE   'w'
45*86d7f5d3SJohn Marino #define ACL_STRING_PERM_READ    'r'
46*86d7f5d3SJohn Marino #define ACL_STRING_PERM_EXEC    'x'
47*86d7f5d3SJohn Marino #define ACL_STRING_PERM_NONE    '-'
48*86d7f5d3SJohn Marino 
49*86d7f5d3SJohn Marino /*
50*86d7f5d3SJohn Marino  * acl_entry_compare -- compare two acl_entry structures to determine the
51*86d7f5d3SJohn Marino  * order they should appear in.  Used by acl_sort to sort ACL entries into
52*86d7f5d3SJohn Marino  * the kernel-desired order -- i.e., the order useful for evaluation and
53*86d7f5d3SJohn Marino  * O(n) validity checking.  Beter to have an O(nlogn) sort in userland and
54*86d7f5d3SJohn Marino  * an O(n) in kernel than to have both in kernel.
55*86d7f5d3SJohn Marino  */
56*86d7f5d3SJohn Marino typedef int (*compare)(const void *, const void *);
57*86d7f5d3SJohn Marino static int
acl_entry_compare(struct acl_entry * a,struct acl_entry * b)58*86d7f5d3SJohn Marino acl_entry_compare(struct acl_entry *a, struct acl_entry *b)
59*86d7f5d3SJohn Marino {
60*86d7f5d3SJohn Marino 	/*
61*86d7f5d3SJohn Marino 	 * First, sort between tags -- conveniently defined in the correct
62*86d7f5d3SJohn Marino 	 * order for verification.
63*86d7f5d3SJohn Marino 	 */
64*86d7f5d3SJohn Marino 	if (a->ae_tag < b->ae_tag)
65*86d7f5d3SJohn Marino 		return (-1);
66*86d7f5d3SJohn Marino 	if (a->ae_tag > b->ae_tag)
67*86d7f5d3SJohn Marino 		return (1);
68*86d7f5d3SJohn Marino 
69*86d7f5d3SJohn Marino 	/*
70*86d7f5d3SJohn Marino 	 * Next compare uids/gids on appropriate types.
71*86d7f5d3SJohn Marino 	 */
72*86d7f5d3SJohn Marino 
73*86d7f5d3SJohn Marino 	if (a->ae_tag == ACL_USER || a->ae_tag == ACL_GROUP) {
74*86d7f5d3SJohn Marino 		if (a->ae_id < b->ae_id)
75*86d7f5d3SJohn Marino 			return (-1);
76*86d7f5d3SJohn Marino 		if (a->ae_id > b->ae_id)
77*86d7f5d3SJohn Marino 			return (1);
78*86d7f5d3SJohn Marino 
79*86d7f5d3SJohn Marino 		/* shouldn't be equal, fall through to the invalid case */
80*86d7f5d3SJohn Marino 	}
81*86d7f5d3SJohn Marino 
82*86d7f5d3SJohn Marino 	/*
83*86d7f5d3SJohn Marino 	 * Don't know how to sort multiple entries of the rest--either it's
84*86d7f5d3SJohn Marino 	 * a bad entry, or there shouldn't be more than one.  Ignore and the
85*86d7f5d3SJohn Marino 	 * validity checker can get it later.
86*86d7f5d3SJohn Marino 	 */
87*86d7f5d3SJohn Marino 	return (0);
88*86d7f5d3SJohn Marino }
89*86d7f5d3SJohn Marino 
90*86d7f5d3SJohn Marino /*
91*86d7f5d3SJohn Marino  * acl_sort -- sort ACL entries.
92*86d7f5d3SJohn Marino  * Give the opportunity to fail, althouh we don't currently have a way
93*86d7f5d3SJohn Marino  * to fail.
94*86d7f5d3SJohn Marino  */
95*86d7f5d3SJohn Marino int
acl_sort(acl_t acl)96*86d7f5d3SJohn Marino acl_sort(acl_t acl)
97*86d7f5d3SJohn Marino {
98*86d7f5d3SJohn Marino 
99*86d7f5d3SJohn Marino 	qsort(&acl->acl_entry[0], acl->acl_cnt, sizeof(struct acl_entry),
100*86d7f5d3SJohn Marino 	    (compare) acl_entry_compare);
101*86d7f5d3SJohn Marino 
102*86d7f5d3SJohn Marino 	return (0);
103*86d7f5d3SJohn Marino }
104*86d7f5d3SJohn Marino 
105*86d7f5d3SJohn Marino /*
106*86d7f5d3SJohn Marino  * acl_posix1e -- in what situations should we acl_sort before submission?
107*86d7f5d3SJohn Marino  * We apply posix1e ACL semantics for any ACL of type ACL_TYPE_ACCESS or
108*86d7f5d3SJohn Marino  * ACL_TYPE_DEFAULT
109*86d7f5d3SJohn Marino  */
110*86d7f5d3SJohn Marino int
acl_posix1e(acl_t acl,acl_type_t type)111*86d7f5d3SJohn Marino acl_posix1e(acl_t acl, acl_type_t type)
112*86d7f5d3SJohn Marino {
113*86d7f5d3SJohn Marino 
114*86d7f5d3SJohn Marino 	return ((type == ACL_TYPE_ACCESS) || (type == ACL_TYPE_DEFAULT));
115*86d7f5d3SJohn Marino }
116*86d7f5d3SJohn Marino 
117*86d7f5d3SJohn Marino /*
118*86d7f5d3SJohn Marino  * acl_check -- given an ACL, check its validity.  This is mirrored from
119*86d7f5d3SJohn Marino  * code in sys/kern/kern_acl.c, and if changes are made in one, they should
120*86d7f5d3SJohn Marino  * be made in the other also.  This copy of acl_check is made available
121*86d7f5d3SJohn Marino  * in userland for the benefit of processes wanting to check ACLs for
122*86d7f5d3SJohn Marino  * validity before submitting them to the kernel, or for performing
123*86d7f5d3SJohn Marino  * in userland file system checking.  Needless to say, the kernel makes
124*86d7f5d3SJohn Marino  * the real checks on calls to get/setacl.
125*86d7f5d3SJohn Marino  *
126*86d7f5d3SJohn Marino  * See the comments in kernel for explanation -- just briefly, it assumes
127*86d7f5d3SJohn Marino  * an already sorted ACL, and checks based on that assumption.  The
128*86d7f5d3SJohn Marino  * POSIX.1e interface, acl_valid(), will perform the sort before calling
129*86d7f5d3SJohn Marino  * this.  Returns 0 on success, EINVAL on failure.
130*86d7f5d3SJohn Marino  */
131*86d7f5d3SJohn Marino int
acl_check(struct acl * acl)132*86d7f5d3SJohn Marino acl_check(struct acl *acl)
133*86d7f5d3SJohn Marino {
134*86d7f5d3SJohn Marino 	struct acl_entry	*entry; 	/* current entry */
135*86d7f5d3SJohn Marino 	uid_t	obj_uid=-1, obj_gid=-1, highest_uid=0, highest_gid=0;
136*86d7f5d3SJohn Marino 	int	stage = ACL_USER_OBJ;
137*86d7f5d3SJohn Marino 	int	i = 0;
138*86d7f5d3SJohn Marino 	int	count_user_obj=0, count_user=0, count_group_obj=0,
139*86d7f5d3SJohn Marino 		count_group=0, count_mask=0, count_other=0;
140*86d7f5d3SJohn Marino 
141*86d7f5d3SJohn Marino 	/* printf("acl_check: checking acl with %d entries\n", acl->acl_cnt); */
142*86d7f5d3SJohn Marino 	while (i < acl->acl_cnt) {
143*86d7f5d3SJohn Marino 
144*86d7f5d3SJohn Marino 		entry = &acl->acl_entry[i];
145*86d7f5d3SJohn Marino 
146*86d7f5d3SJohn Marino 		if ((entry->ae_perm | ACL_PERM_BITS) != ACL_PERM_BITS)
147*86d7f5d3SJohn Marino 			return (EINVAL);
148*86d7f5d3SJohn Marino 
149*86d7f5d3SJohn Marino 		switch(entry->ae_tag) {
150*86d7f5d3SJohn Marino 		case ACL_USER_OBJ:
151*86d7f5d3SJohn Marino 			/* printf("acl_check: %d: ACL_USER_OBJ\n", i); */
152*86d7f5d3SJohn Marino 			if (stage > ACL_USER_OBJ)
153*86d7f5d3SJohn Marino 				return (EINVAL);
154*86d7f5d3SJohn Marino 			stage = ACL_USER;
155*86d7f5d3SJohn Marino 			count_user_obj++;
156*86d7f5d3SJohn Marino 			obj_uid = entry->ae_id;
157*86d7f5d3SJohn Marino 			break;
158*86d7f5d3SJohn Marino 
159*86d7f5d3SJohn Marino 		case ACL_USER:
160*86d7f5d3SJohn Marino 			/* printf("acl_check: %d: ACL_USER\n", i); */
161*86d7f5d3SJohn Marino 			if (stage > ACL_USER)
162*86d7f5d3SJohn Marino 				return (EINVAL);
163*86d7f5d3SJohn Marino 			stage = ACL_USER;
164*86d7f5d3SJohn Marino 			if (entry->ae_id == obj_uid)
165*86d7f5d3SJohn Marino 				return (EINVAL);
166*86d7f5d3SJohn Marino 			if (count_user && (entry->ae_id <= highest_uid))
167*86d7f5d3SJohn Marino 				return (EINVAL);
168*86d7f5d3SJohn Marino 			highest_uid = entry->ae_id;
169*86d7f5d3SJohn Marino 			count_user++;
170*86d7f5d3SJohn Marino 			break;
171*86d7f5d3SJohn Marino 
172*86d7f5d3SJohn Marino 		case ACL_GROUP_OBJ:
173*86d7f5d3SJohn Marino 			/* printf("acl_check: %d: ACL_GROUP_OBJ\n", i); */
174*86d7f5d3SJohn Marino 			if (stage > ACL_GROUP_OBJ)
175*86d7f5d3SJohn Marino 				return (EINVAL);
176*86d7f5d3SJohn Marino 			stage = ACL_GROUP;
177*86d7f5d3SJohn Marino 			count_group_obj++;
178*86d7f5d3SJohn Marino 			obj_gid = entry->ae_id;
179*86d7f5d3SJohn Marino 			break;
180*86d7f5d3SJohn Marino 
181*86d7f5d3SJohn Marino 		case ACL_GROUP:
182*86d7f5d3SJohn Marino 			/* printf("acl_check: %d: ACL_GROUP\n", i); */
183*86d7f5d3SJohn Marino 			if (stage > ACL_GROUP)
184*86d7f5d3SJohn Marino 				return (EINVAL);
185*86d7f5d3SJohn Marino 			stage = ACL_GROUP;
186*86d7f5d3SJohn Marino 			if (entry->ae_id == obj_gid)
187*86d7f5d3SJohn Marino 				return (EINVAL);
188*86d7f5d3SJohn Marino 			if (count_group && (entry->ae_id <= highest_gid))
189*86d7f5d3SJohn Marino 				return (EINVAL);
190*86d7f5d3SJohn Marino 			highest_gid = entry->ae_id;
191*86d7f5d3SJohn Marino 			count_group++;
192*86d7f5d3SJohn Marino 			break;
193*86d7f5d3SJohn Marino 
194*86d7f5d3SJohn Marino 		case ACL_MASK:
195*86d7f5d3SJohn Marino 			/* printf("acl_check: %d: ACL_MASK\n", i); */
196*86d7f5d3SJohn Marino 			if (stage > ACL_MASK)
197*86d7f5d3SJohn Marino 				return (EINVAL);
198*86d7f5d3SJohn Marino 			stage = ACL_MASK;
199*86d7f5d3SJohn Marino 			count_mask++;
200*86d7f5d3SJohn Marino 			break;
201*86d7f5d3SJohn Marino 
202*86d7f5d3SJohn Marino 		case ACL_OTHER:
203*86d7f5d3SJohn Marino 			/* printf("acl_check: %d: ACL_OTHER\n", i); */
204*86d7f5d3SJohn Marino 			if (stage > ACL_OTHER)
205*86d7f5d3SJohn Marino 				return (EINVAL);
206*86d7f5d3SJohn Marino 			stage = ACL_OTHER;
207*86d7f5d3SJohn Marino 			count_other++;
208*86d7f5d3SJohn Marino 			break;
209*86d7f5d3SJohn Marino 
210*86d7f5d3SJohn Marino 		default:
211*86d7f5d3SJohn Marino 			/* printf("acl_check: %d: INVALID\n", i); */
212*86d7f5d3SJohn Marino 			return (EINVAL);
213*86d7f5d3SJohn Marino 		}
214*86d7f5d3SJohn Marino 		i++;
215*86d7f5d3SJohn Marino 	}
216*86d7f5d3SJohn Marino 
217*86d7f5d3SJohn Marino 	if (count_user_obj != 1)
218*86d7f5d3SJohn Marino 		return (EINVAL);
219*86d7f5d3SJohn Marino 
220*86d7f5d3SJohn Marino 	if (count_group_obj != 1)
221*86d7f5d3SJohn Marino 		return (EINVAL);
222*86d7f5d3SJohn Marino 
223*86d7f5d3SJohn Marino 	if (count_mask != 0 && count_mask != 1)
224*86d7f5d3SJohn Marino 		return (EINVAL);
225*86d7f5d3SJohn Marino 
226*86d7f5d3SJohn Marino 	if (count_other != 1)
227*86d7f5d3SJohn Marino 		return (EINVAL);
228*86d7f5d3SJohn Marino 
229*86d7f5d3SJohn Marino 	return (0);
230*86d7f5d3SJohn Marino }
231*86d7f5d3SJohn Marino 
232*86d7f5d3SJohn Marino 
233*86d7f5d3SJohn Marino /*
234*86d7f5d3SJohn Marino  * Given a uid/gid, return a username/groupname for the text form of an ACL
235*86d7f5d3SJohn Marino  * XXX NOT THREAD SAFE, RELIES ON GETPWUID, GETGRGID
236*86d7f5d3SJohn Marino  * XXX USES *PW* AND *GR* WHICH ARE STATEFUL AND THEREFORE THIS ROUTINE
237*86d7f5d3SJohn Marino  * MAY HAVE SIDE-EFFECTS
238*86d7f5d3SJohn Marino  */
239*86d7f5d3SJohn Marino int
acl_id_to_name(acl_tag_t tag,uid_t id,ssize_t buf_len,char * buf)240*86d7f5d3SJohn Marino acl_id_to_name(acl_tag_t tag, uid_t id, ssize_t buf_len, char *buf)
241*86d7f5d3SJohn Marino {
242*86d7f5d3SJohn Marino 	struct group	*g;
243*86d7f5d3SJohn Marino 	struct passwd	*p;
244*86d7f5d3SJohn Marino 	int	i;
245*86d7f5d3SJohn Marino 
246*86d7f5d3SJohn Marino 	switch(tag) {
247*86d7f5d3SJohn Marino 	case ACL_USER:
248*86d7f5d3SJohn Marino 		p = getpwuid(id);
249*86d7f5d3SJohn Marino 		if (!p)
250*86d7f5d3SJohn Marino 			i = snprintf(buf, buf_len, "%d", id);
251*86d7f5d3SJohn Marino 		else
252*86d7f5d3SJohn Marino 			i = snprintf(buf, buf_len, "%s", p->pw_name);
253*86d7f5d3SJohn Marino 
254*86d7f5d3SJohn Marino 		if (i >= buf_len) {
255*86d7f5d3SJohn Marino 			errno = ENOMEM;
256*86d7f5d3SJohn Marino 			return (-1);
257*86d7f5d3SJohn Marino 		}
258*86d7f5d3SJohn Marino 		return (0);
259*86d7f5d3SJohn Marino 
260*86d7f5d3SJohn Marino 	case ACL_GROUP:
261*86d7f5d3SJohn Marino 		g = getgrgid(id);
262*86d7f5d3SJohn Marino 		if (!g)
263*86d7f5d3SJohn Marino 			i = snprintf(buf, buf_len, "%d", id);
264*86d7f5d3SJohn Marino 		else
265*86d7f5d3SJohn Marino 			i = snprintf(buf, buf_len, "%s", g->gr_name);
266*86d7f5d3SJohn Marino 
267*86d7f5d3SJohn Marino 		if (i >= buf_len) {
268*86d7f5d3SJohn Marino 			errno = ENOMEM;
269*86d7f5d3SJohn Marino 			return (-1);
270*86d7f5d3SJohn Marino 		}
271*86d7f5d3SJohn Marino 		return (0);
272*86d7f5d3SJohn Marino 
273*86d7f5d3SJohn Marino 	default:
274*86d7f5d3SJohn Marino 		return (EINVAL);
275*86d7f5d3SJohn Marino 	}
276*86d7f5d3SJohn Marino }
277*86d7f5d3SJohn Marino 
278*86d7f5d3SJohn Marino 
279*86d7f5d3SJohn Marino /*
280*86d7f5d3SJohn Marino  * Given a username/groupname from a text form of an ACL, return the uid/gid
281*86d7f5d3SJohn Marino  * XXX NOT THREAD SAFE, RELIES ON GETPWNAM, GETGRNAM
282*86d7f5d3SJohn Marino  * XXX USES *PW* AND *GR* WHICH ARE STATEFUL AND THEREFORE THIS ROUTINE
283*86d7f5d3SJohn Marino  * MAY HAVE SIDE-EFFECTS
284*86d7f5d3SJohn Marino  *
285*86d7f5d3SJohn Marino  * XXX currently doesn't deal correctly with a numeric uid being passed
286*86d7f5d3SJohn Marino  * instead of a username.  What is correct behavior here?  Check chown.
287*86d7f5d3SJohn Marino  */
288*86d7f5d3SJohn Marino int
acl_name_to_id(acl_tag_t tag,char * name,uid_t * id)289*86d7f5d3SJohn Marino acl_name_to_id(acl_tag_t tag, char *name, uid_t *id)
290*86d7f5d3SJohn Marino {
291*86d7f5d3SJohn Marino 	struct group	*g;
292*86d7f5d3SJohn Marino 	struct passwd	*p;
293*86d7f5d3SJohn Marino 
294*86d7f5d3SJohn Marino 	switch(tag) {
295*86d7f5d3SJohn Marino 	case ACL_USER:
296*86d7f5d3SJohn Marino 		p = getpwnam(name);
297*86d7f5d3SJohn Marino 		if (!p) {
298*86d7f5d3SJohn Marino 			errno = EINVAL;
299*86d7f5d3SJohn Marino 			return (-1);
300*86d7f5d3SJohn Marino 		}
301*86d7f5d3SJohn Marino 		*id = p->pw_uid;
302*86d7f5d3SJohn Marino 		return (0);
303*86d7f5d3SJohn Marino 
304*86d7f5d3SJohn Marino 	case ACL_GROUP:
305*86d7f5d3SJohn Marino 		g = getgrnam(name);
306*86d7f5d3SJohn Marino 		if (g) {
307*86d7f5d3SJohn Marino 			errno = EINVAL;
308*86d7f5d3SJohn Marino 			return (-1);
309*86d7f5d3SJohn Marino 		}
310*86d7f5d3SJohn Marino 		*id = g->gr_gid;
311*86d7f5d3SJohn Marino 		return (0);
312*86d7f5d3SJohn Marino 
313*86d7f5d3SJohn Marino 	default:
314*86d7f5d3SJohn Marino 		return (EINVAL);
315*86d7f5d3SJohn Marino 	}
316*86d7f5d3SJohn Marino }
317*86d7f5d3SJohn Marino 
318*86d7f5d3SJohn Marino 
319*86d7f5d3SJohn Marino /*
320*86d7f5d3SJohn Marino  * Given a right-shifted permission (i.e., direct ACL_PERM_* mask), fill
321*86d7f5d3SJohn Marino  * in a string describing the permissions.
322*86d7f5d3SJohn Marino  */
323*86d7f5d3SJohn Marino int
acl_perm_to_string(acl_perm_t perm,ssize_t buf_len,char * buf)324*86d7f5d3SJohn Marino acl_perm_to_string(acl_perm_t perm, ssize_t buf_len, char *buf)
325*86d7f5d3SJohn Marino {
326*86d7f5d3SJohn Marino 
327*86d7f5d3SJohn Marino 	if (buf_len < ACL_STRING_PERM_MAXSIZE + 1) {
328*86d7f5d3SJohn Marino 		errno = ENOMEM;
329*86d7f5d3SJohn Marino 		return (-1);
330*86d7f5d3SJohn Marino 	}
331*86d7f5d3SJohn Marino 
332*86d7f5d3SJohn Marino 	if ((perm | ACL_PERM_BITS) != ACL_PERM_BITS) {
333*86d7f5d3SJohn Marino 		errno = EINVAL;
334*86d7f5d3SJohn Marino 		return (-1);
335*86d7f5d3SJohn Marino 	}
336*86d7f5d3SJohn Marino 
337*86d7f5d3SJohn Marino 	buf[3] = 0;	/* null terminate */
338*86d7f5d3SJohn Marino 
339*86d7f5d3SJohn Marino 	if (perm & ACL_PERM_READ)
340*86d7f5d3SJohn Marino 		buf[0] = ACL_STRING_PERM_READ;
341*86d7f5d3SJohn Marino 	else
342*86d7f5d3SJohn Marino 		buf[0] = ACL_STRING_PERM_NONE;
343*86d7f5d3SJohn Marino 
344*86d7f5d3SJohn Marino 	if (perm & ACL_PERM_WRITE)
345*86d7f5d3SJohn Marino 		buf[1] = ACL_STRING_PERM_WRITE;
346*86d7f5d3SJohn Marino 	else
347*86d7f5d3SJohn Marino 		buf[1] = ACL_STRING_PERM_NONE;
348*86d7f5d3SJohn Marino 
349*86d7f5d3SJohn Marino 	if (perm & ACL_PERM_EXEC)
350*86d7f5d3SJohn Marino 		buf[2] = ACL_STRING_PERM_EXEC;
351*86d7f5d3SJohn Marino 	else
352*86d7f5d3SJohn Marino 		buf[2] = ACL_STRING_PERM_NONE;
353*86d7f5d3SJohn Marino 
354*86d7f5d3SJohn Marino 	return (0);
355*86d7f5d3SJohn Marino }
356*86d7f5d3SJohn Marino 
357*86d7f5d3SJohn Marino /*
358*86d7f5d3SJohn Marino  * given a string, return a permission describing it
359*86d7f5d3SJohn Marino  */
360*86d7f5d3SJohn Marino int
acl_string_to_perm(char * string,acl_perm_t * perm)361*86d7f5d3SJohn Marino acl_string_to_perm(char *string, acl_perm_t *perm)
362*86d7f5d3SJohn Marino {
363*86d7f5d3SJohn Marino 	acl_perm_t	myperm = ACL_PERM_NONE;
364*86d7f5d3SJohn Marino 	char	*ch;
365*86d7f5d3SJohn Marino 
366*86d7f5d3SJohn Marino 	ch = string;
367*86d7f5d3SJohn Marino 	while (*ch) {
368*86d7f5d3SJohn Marino 		switch(*ch) {
369*86d7f5d3SJohn Marino 		case ACL_STRING_PERM_READ:
370*86d7f5d3SJohn Marino 			myperm |= ACL_PERM_READ;
371*86d7f5d3SJohn Marino 			break;
372*86d7f5d3SJohn Marino 		case ACL_STRING_PERM_WRITE:
373*86d7f5d3SJohn Marino 			myperm |= ACL_PERM_WRITE;
374*86d7f5d3SJohn Marino 			break;
375*86d7f5d3SJohn Marino 		case ACL_STRING_PERM_EXEC:
376*86d7f5d3SJohn Marino 			myperm |= ACL_PERM_EXEC;
377*86d7f5d3SJohn Marino 			break;
378*86d7f5d3SJohn Marino 		case ACL_STRING_PERM_NONE:
379*86d7f5d3SJohn Marino 			break;
380*86d7f5d3SJohn Marino 		default:
381*86d7f5d3SJohn Marino 			return (EINVAL);
382*86d7f5d3SJohn Marino 		}
383*86d7f5d3SJohn Marino 		ch++;
384*86d7f5d3SJohn Marino 	}
385*86d7f5d3SJohn Marino 
386*86d7f5d3SJohn Marino 	*perm = myperm;
387*86d7f5d3SJohn Marino 	return (0);
388*86d7f5d3SJohn Marino }
389*86d7f5d3SJohn Marino 
390*86d7f5d3SJohn Marino /*
391*86d7f5d3SJohn Marino  * Add an ACL entry without doing much checking, et al
392*86d7f5d3SJohn Marino  */
393*86d7f5d3SJohn Marino int
acl_add_entry(acl_t acl,acl_tag_t tag,uid_t id,acl_perm_t perm)394*86d7f5d3SJohn Marino acl_add_entry(acl_t acl, acl_tag_t tag, uid_t id, acl_perm_t perm)
395*86d7f5d3SJohn Marino {
396*86d7f5d3SJohn Marino 	struct acl_entry	*e;
397*86d7f5d3SJohn Marino 
398*86d7f5d3SJohn Marino 	if (acl->acl_cnt >= ACL_MAX_ENTRIES) {
399*86d7f5d3SJohn Marino 		errno = ENOMEM;
400*86d7f5d3SJohn Marino 		return (-1);
401*86d7f5d3SJohn Marino 	}
402*86d7f5d3SJohn Marino 
403*86d7f5d3SJohn Marino 	e = &(acl->acl_entry[acl->acl_cnt]);
404*86d7f5d3SJohn Marino 	e->ae_perm = perm;
405*86d7f5d3SJohn Marino 	e->ae_tag = tag;
406*86d7f5d3SJohn Marino 	e->ae_id = id;
407*86d7f5d3SJohn Marino 	acl->acl_cnt++;
408*86d7f5d3SJohn Marino 
409*86d7f5d3SJohn Marino 	return (0);
410*86d7f5d3SJohn Marino }
411