xref: /freebsd/lib/libc/posix1e/acl_to_text.c (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
1 /*-
2  * Copyright (c) 1999-2002 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 /*
29  * acl_to_text - return a text string with a text representation of the acl
30  * in it.
31  */
32 
33 #include <sys/types.h>
34 #include "namespace.h"
35 #include <sys/acl.h>
36 #include "un-namespace.h"
37 #include <sys/errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <utmp.h>
42 
43 #include "acl_support.h"
44 
45 /*
46  * acl_to_text - generate a text form of an acl
47  * spec says nothing about output ordering, so leave in acl order
48  *
49  * This function will not produce nice results if it is called with
50  * a non-POSIX.1e semantics ACL.
51  */
52 char *
53 acl_to_text(acl_t acl, ssize_t *len_p)
54 {
55 	struct acl	*acl_int;
56 	char		*buf, *tmpbuf;
57 	char		 name_buf[UT_NAMESIZE+1];
58 	char		 perm_buf[_POSIX1E_ACL_STRING_PERM_MAXSIZE+1],
59 			 effective_perm_buf[_POSIX1E_ACL_STRING_PERM_MAXSIZE+1];
60 	int		 i, error, len;
61 	uid_t		 ae_id;
62 	acl_tag_t	 ae_tag;
63 	acl_perm_t	 ae_perm, effective_perm, mask_perm;
64 
65 	buf = strdup("");
66 	if (!buf)
67 		return(NULL);
68 
69 	if (acl == NULL) {
70 		errno = EINVAL;
71 		return(NULL);
72 	}
73 
74 	acl_int = &acl->ats_acl;
75 
76 	mask_perm = ACL_PERM_BITS;	/* effective is regular if no mask */
77 	for (i = 0; i < acl_int->acl_cnt; i++)
78 		if (acl_int->acl_entry[i].ae_tag == ACL_MASK)
79 			mask_perm = acl_int->acl_entry[i].ae_perm;
80 
81 	for (i = 0; i < acl_int->acl_cnt; i++) {
82 		ae_tag = acl_int->acl_entry[i].ae_tag;
83 		ae_id = acl_int->acl_entry[i].ae_id;
84 		ae_perm = acl_int->acl_entry[i].ae_perm;
85 
86 		switch(ae_tag) {
87 		case ACL_USER_OBJ:
88 			error = _posix1e_acl_perm_to_string(ae_perm,
89 			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
90 			if (error)
91 				goto error_label;
92 			len = asprintf(&tmpbuf, "%suser::%s\n", buf,
93 			    perm_buf);
94 			if (len == -1)
95 				goto error_label;
96 			free(buf);
97 			buf = tmpbuf;
98 			break;
99 
100 		case ACL_USER:
101 			error = _posix1e_acl_perm_to_string(ae_perm,
102 			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
103 			if (error)
104 				goto error_label;
105 
106 			error = _posix1e_acl_id_to_name(ae_tag, ae_id,
107 			    UT_NAMESIZE+1, name_buf);
108 			if (error)
109 				goto error_label;
110 
111 			effective_perm = ae_perm & mask_perm;
112 			if (effective_perm != ae_perm) {
113 				error = _posix1e_acl_perm_to_string(
114 				    effective_perm,
115 				    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1,
116 				    effective_perm_buf);
117 				if (error)
118 					goto error_label;
119 				len = asprintf(&tmpbuf, "%suser:%s:%s\t\t# "
120 				    "effective: %s\n",
121 				    buf, name_buf, perm_buf,
122 				    effective_perm_buf);
123 			} else {
124 				len = asprintf(&tmpbuf, "%suser:%s:%s\n", buf,
125 				    name_buf, perm_buf);
126 			}
127 			if (len == -1)
128 				goto error_label;
129 			free(buf);
130 			buf = tmpbuf;
131 			break;
132 
133 		case ACL_GROUP_OBJ:
134 			error = _posix1e_acl_perm_to_string(ae_perm,
135 			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
136 			if (error)
137 				goto error_label;
138 
139 			effective_perm = ae_perm & mask_perm;
140 			if (effective_perm != ae_perm) {
141 				error = _posix1e_acl_perm_to_string(
142 				    effective_perm,
143 				    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1,
144 				    effective_perm_buf);
145 				if (error)
146 					goto error_label;
147 				len = asprintf(&tmpbuf, "%sgroup::%s\t\t# "
148 				    "effective: %s\n",
149 				    buf, perm_buf, effective_perm_buf);
150 			} else {
151 				len = asprintf(&tmpbuf, "%sgroup::%s\n", buf,
152 				    perm_buf);
153 			}
154 			if (len == -1)
155 				goto error_label;
156 			free(buf);
157 			buf = tmpbuf;
158 			break;
159 
160 		case ACL_GROUP:
161 			error = _posix1e_acl_perm_to_string(ae_perm,
162 			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
163 			if (error)
164 				goto error_label;
165 
166 			error = _posix1e_acl_id_to_name(ae_tag, ae_id,
167 			    UT_NAMESIZE+1, name_buf);
168 			if (error)
169 				goto error_label;
170 
171 			effective_perm = ae_perm & mask_perm;
172 			if (effective_perm != ae_perm) {
173 				error = _posix1e_acl_perm_to_string(
174 				    effective_perm,
175 				    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1,
176 				    effective_perm_buf);
177 				if (error)
178 					goto error_label;
179 				len = asprintf(&tmpbuf, "%sgroup::%s\t\t# "
180 				    "effective: %s\n",
181 				    buf, perm_buf, effective_perm_buf);
182 			} else {
183 				len = asprintf(&tmpbuf, "%sgroup:%s:%s\n", buf,
184 				    name_buf, perm_buf);
185 			}
186 			if (len == -1)
187 				goto error_label;
188 			free(buf);
189 			buf = tmpbuf;
190 			break;
191 
192 		case ACL_MASK:
193 			error = _posix1e_acl_perm_to_string(ae_perm,
194 			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
195 			if (error)
196 				goto error_label;
197 
198 			len = asprintf(&tmpbuf, "%smask::%s\n", buf,
199 			    perm_buf);
200 			if (len == -1)
201 				goto error_label;
202 			free(buf);
203 			buf = tmpbuf;
204 			break;
205 
206 		case ACL_OTHER:
207 			error = _posix1e_acl_perm_to_string(ae_perm,
208 			    _POSIX1E_ACL_STRING_PERM_MAXSIZE+1, perm_buf);
209 			if (error)
210 				goto error_label;
211 
212 			len = asprintf(&tmpbuf, "%sother::%s\n", buf,
213 			    perm_buf);
214 			if (len == -1)
215 				goto error_label;
216 			free(buf);
217 			buf = tmpbuf;
218 			break;
219 
220 		default:
221 			errno = EINVAL;
222 			goto error_label;
223 		}
224 	}
225 
226 	if (len_p) {
227 		*len_p = strlen(buf);
228 	}
229 	return (buf);
230 
231 error_label:
232 	/* jump to here sets errno already, we just clean up */
233 	if (buf) free(buf);
234 	return (NULL);
235 }
236