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