acl_support_nfs4.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) 2008, 2009 Edward Tomasz Napieraa <trasz (at) FreeBSD.org>
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 #include <sys/cdefs.h>
30 1.1 christos #if 0
31 1.1 christos __FBSDID("$FreeBSD: head/lib/libc/posix1e/acl_support_nfs4.c 326193 2017-11-25 17:12:48Z pfg $");
32 1.1 christos #else
33 1.1 christos __RCSID("$NetBSD: acl_support_nfs4.c,v 1.1 2020/05/16 18:31:47 christos Exp $");
34 1.1 christos #endif
35 1.1 christos
36 1.1 christos #include <stdio.h>
37 1.1 christos #include <stdlib.h>
38 1.1 christos #include <string.h>
39 1.1 christos #include <assert.h>
40 1.1 christos #include <err.h>
41 1.1 christos #include <sys/acl.h>
42 1.1 christos #include "acl_support.h"
43 1.1 christos
44 1.1 christos struct flagnames_struct {
45 1.1 christos uint32_t flag;
46 1.1 christos const char *name;
47 1.1 christos char letter;
48 1.1 christos };
49 1.1 christos
50 1.1 christos struct flagnames_struct a_flags[] =
51 1.1 christos {{ ACL_ENTRY_FILE_INHERIT, "file_inherit", 'f'},
52 1.1 christos { ACL_ENTRY_DIRECTORY_INHERIT, "dir_inherit", 'd'},
53 1.1 christos { ACL_ENTRY_INHERIT_ONLY, "inherit_only", 'i'},
54 1.1 christos { ACL_ENTRY_NO_PROPAGATE_INHERIT, "no_propagate", 'n'},
55 1.1 christos { ACL_ENTRY_SUCCESSFUL_ACCESS, "successfull_access", 'S'},
56 1.1 christos { ACL_ENTRY_FAILED_ACCESS, "failed_access", 'F'},
57 1.1 christos { ACL_ENTRY_INHERITED, "inherited", 'I' },
58 1.1 christos /*
59 1.1 christos * There is no ACE_IDENTIFIER_GROUP here - SunOS does not show it
60 1.1 christos * in the "flags" field. There is no ACE_OWNER, ACE_GROUP or
61 1.1 christos * ACE_EVERYONE either, for obvious reasons.
62 1.1 christos */
63 1.1 christos { 0, 0, 0}};
64 1.1 christos
65 1.1 christos struct flagnames_struct a_access_masks[] =
66 1.1 christos {{ ACL_READ_DATA, "read_data", 'r'},
67 1.1 christos { ACL_WRITE_DATA, "write_data", 'w'},
68 1.1 christos { ACL_EXECUTE, "execute", 'x'},
69 1.1 christos { ACL_APPEND_DATA, "append_data", 'p'},
70 1.1 christos { ACL_DELETE_CHILD, "delete_child", 'D'},
71 1.1 christos { ACL_DELETE, "delete", 'd'},
72 1.1 christos { ACL_READ_ATTRIBUTES, "read_attributes", 'a'},
73 1.1 christos { ACL_WRITE_ATTRIBUTES, "write_attributes", 'A'},
74 1.1 christos { ACL_READ_NAMED_ATTRS, "read_xattr", 'R'},
75 1.1 christos { ACL_WRITE_NAMED_ATTRS, "write_xattr", 'W'},
76 1.1 christos { ACL_READ_ACL, "read_acl", 'c'},
77 1.1 christos { ACL_WRITE_ACL, "write_acl", 'C'},
78 1.1 christos { ACL_WRITE_OWNER, "write_owner", 'o'},
79 1.1 christos { ACL_SYNCHRONIZE, "synchronize", 's'},
80 1.1 christos { ACL_FULL_SET, "full_set", '\0'},
81 1.1 christos { ACL_MODIFY_SET, "modify_set", '\0'},
82 1.1 christos { ACL_READ_SET, "read_set", '\0'},
83 1.1 christos { ACL_WRITE_SET, "write_set", '\0'},
84 1.1 christos { 0, 0, 0}};
85 1.1 christos
86 1.1 christos static const char *
87 1.1 christos format_flag(uint32_t *var, const struct flagnames_struct *flags)
88 1.1 christos {
89 1.1 christos
90 1.1 christos for (; flags->name != NULL; flags++) {
91 1.1 christos if ((flags->flag & *var) == 0)
92 1.1 christos continue;
93 1.1 christos
94 1.1 christos *var &= ~flags->flag;
95 1.1 christos return (flags->name);
96 1.1 christos }
97 1.1 christos
98 1.1 christos return (NULL);
99 1.1 christos }
100 1.1 christos
101 1.1 christos static int
102 1.1 christos format_flags_verbose(char *str, size_t size, uint32_t var,
103 1.1 christos const struct flagnames_struct *flags)
104 1.1 christos {
105 1.1 christos size_t off = 0;
106 1.1 christos const char *tmp;
107 1.1 christos
108 1.1 christos while ((tmp = format_flag(&var, flags)) != NULL) {
109 1.1 christos off += snprintf(str + off, size - off, "%s/", tmp);
110 1.1 christos assert (off < size);
111 1.1 christos }
112 1.1 christos
113 1.1 christos /* If there were any flags added... */
114 1.1 christos if (off > 0) {
115 1.1 christos off--;
116 1.1 christos /* ... then remove the last slash. */
117 1.1 christos assert(str[off] == '/');
118 1.1 christos }
119 1.1 christos
120 1.1 christos str[off] = '\0';
121 1.1 christos
122 1.1 christos return (0);
123 1.1 christos }
124 1.1 christos
125 1.1 christos static int
126 1.1 christos format_flags_compact(char *str, size_t size, uint32_t var,
127 1.1 christos const struct flagnames_struct *flags)
128 1.1 christos {
129 1.1 christos size_t i;
130 1.1 christos
131 1.1 christos for (i = 0; flags[i].letter != '\0'; i++) {
132 1.1 christos assert(i < size);
133 1.1 christos if ((flags[i].flag & var) == 0)
134 1.1 christos str[i] = '-';
135 1.1 christos else
136 1.1 christos str[i] = flags[i].letter;
137 1.1 christos }
138 1.1 christos
139 1.1 christos str[i] = '\0';
140 1.1 christos
141 1.1 christos return (0);
142 1.1 christos }
143 1.1 christos
144 1.1 christos static int
145 1.1 christos parse_flags_verbose(const char *strp, uint32_t *var,
146 1.1 christos const struct flagnames_struct *flags, const char *flags_name,
147 1.1 christos int *try_compact)
148 1.1 christos {
149 1.1 christos int i, found, ever_found = 0;
150 1.1 christos char *str, *flag;
151 1.1 christos
152 1.1 christos str = strdup(strp);
153 1.1 christos *try_compact = 0;
154 1.1 christos *var = 0;
155 1.1 christos
156 1.1 christos while (str != NULL) {
157 1.1 christos flag = strsep(&str, "/:");
158 1.1 christos
159 1.1 christos found = 0;
160 1.1 christos for (i = 0; flags[i].name != NULL; i++) {
161 1.1 christos if (strcmp(flags[i].name, flag) == 0) {
162 1.1 christos *var |= flags[i].flag;
163 1.1 christos found = 1;
164 1.1 christos ever_found = 1;
165 1.1 christos }
166 1.1 christos }
167 1.1 christos
168 1.1 christos if (!found) {
169 1.1 christos if (ever_found)
170 1.1 christos warnx("malformed ACL: \"%s\" field contains "
171 1.1 christos "invalid flag \"%s\"", flags_name, flag);
172 1.1 christos else
173 1.1 christos *try_compact = 1;
174 1.1 christos free(str);
175 1.1 christos return (-1);
176 1.1 christos }
177 1.1 christos }
178 1.1 christos
179 1.1 christos free(str);
180 1.1 christos return (0);
181 1.1 christos }
182 1.1 christos
183 1.1 christos static int
184 1.1 christos parse_flags_compact(const char *str, uint32_t *var,
185 1.1 christos const struct flagnames_struct *flags, const char *flags_name)
186 1.1 christos {
187 1.1 christos int i, j, found;
188 1.1 christos
189 1.1 christos *var = 0;
190 1.1 christos
191 1.1 christos for (i = 0;; i++) {
192 1.1 christos if (str[i] == '\0')
193 1.1 christos return (0);
194 1.1 christos
195 1.1 christos /* Ignore minus signs. */
196 1.1 christos if (str[i] == '-')
197 1.1 christos continue;
198 1.1 christos
199 1.1 christos found = 0;
200 1.1 christos
201 1.1 christos for (j = 0; flags[j].name != NULL; j++) {
202 1.1 christos if (flags[j].letter == str[i]) {
203 1.1 christos *var |= flags[j].flag;
204 1.1 christos found = 1;
205 1.1 christos break;
206 1.1 christos }
207 1.1 christos }
208 1.1 christos
209 1.1 christos if (!found) {
210 1.1 christos warnx("malformed ACL: \"%s\" field contains "
211 1.1 christos "invalid flag \"%c\"", flags_name, str[i]);
212 1.1 christos return (-1);
213 1.1 christos }
214 1.1 christos }
215 1.1 christos }
216 1.1 christos
217 1.1 christos int
218 1.1 christos _nfs4_format_flags(char *str, size_t size, acl_flag_t var, int verbose)
219 1.1 christos {
220 1.1 christos
221 1.1 christos if (verbose)
222 1.1 christos return (format_flags_verbose(str, size, var, a_flags));
223 1.1 christos
224 1.1 christos return (format_flags_compact(str, size, var, a_flags));
225 1.1 christos }
226 1.1 christos
227 1.1 christos int
228 1.1 christos _nfs4_format_access_mask(char *str, size_t size, acl_perm_t var, int verbose)
229 1.1 christos {
230 1.1 christos
231 1.1 christos if (verbose)
232 1.1 christos return (format_flags_verbose(str, size, var, a_access_masks));
233 1.1 christos
234 1.1 christos return (format_flags_compact(str, size, var, a_access_masks));
235 1.1 christos }
236 1.1 christos
237 1.1 christos int
238 1.1 christos _nfs4_parse_flags(const char *str, acl_flag_t *flags)
239 1.1 christos {
240 1.1 christos int error, try_compact;
241 1.1 christos unsigned int tmpflags;
242 1.1 christos
243 1.1 christos error = parse_flags_verbose(str, &tmpflags, a_flags, "flags", &try_compact);
244 1.1 christos if (error && try_compact)
245 1.1 christos error = parse_flags_compact(str, &tmpflags, a_flags, "flags");
246 1.1 christos
247 1.1 christos *flags = tmpflags;
248 1.1 christos
249 1.1 christos return (error);
250 1.1 christos }
251 1.1 christos
252 1.1 christos int
253 1.1 christos _nfs4_parse_access_mask(const char *str, acl_perm_t *perms)
254 1.1 christos {
255 1.1 christos int error, try_compact;
256 1.1 christos unsigned int tmpperms;
257 1.1 christos
258 1.1 christos error = parse_flags_verbose(str, &tmpperms, a_access_masks,
259 1.1 christos "access permissions", &try_compact);
260 1.1 christos if (error && try_compact)
261 1.1 christos error = parse_flags_compact(str, &tmpperms,
262 1.1 christos a_access_masks, "access permissions");
263 1.1 christos
264 1.1 christos *perms = tmpperms;
265 1.1 christos
266 1.1 christos return (error);
267 1.1 christos }
268