xref: /freebsd/lib/libc/posix1e/acl_support_nfs4.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008, 2009 Edward Tomasz Napierała <trasz@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <err.h>
36 #include <sys/acl.h>
37 #include "acl_support.h"
38 
39 struct flagnames_struct {
40 	uint32_t	flag;
41 	const char	*name;
42 	char		letter;
43 };
44 
45 struct flagnames_struct a_flags[] =
46     {{ ACL_ENTRY_FILE_INHERIT, "file_inherit", 'f'},
47      { ACL_ENTRY_DIRECTORY_INHERIT, "dir_inherit", 'd'},
48      { ACL_ENTRY_INHERIT_ONLY, "inherit_only", 'i'},
49      { ACL_ENTRY_NO_PROPAGATE_INHERIT, "no_propagate", 'n'},
50      { ACL_ENTRY_SUCCESSFUL_ACCESS, "successfull_access", 'S'},
51      { ACL_ENTRY_FAILED_ACCESS, "failed_access", 'F'},
52      { ACL_ENTRY_INHERITED, "inherited", 'I' },
53      /*
54       * There is no ACE_IDENTIFIER_GROUP here - SunOS does not show it
55       * in the "flags" field.  There is no ACE_OWNER, ACE_GROUP or
56       * ACE_EVERYONE either, for obvious reasons.
57       */
58      { 0, 0, 0}};
59 
60 struct flagnames_struct a_access_masks[] =
61     {{ ACL_READ_DATA, "read_data", 'r'},
62      { ACL_WRITE_DATA, "write_data", 'w'},
63      { ACL_EXECUTE, "execute", 'x'},
64      { ACL_APPEND_DATA, "append_data", 'p'},
65      { ACL_DELETE_CHILD, "delete_child", 'D'},
66      { ACL_DELETE, "delete", 'd'},
67      { ACL_READ_ATTRIBUTES, "read_attributes", 'a'},
68      { ACL_WRITE_ATTRIBUTES, "write_attributes", 'A'},
69      { ACL_READ_NAMED_ATTRS, "read_xattr", 'R'},
70      { ACL_WRITE_NAMED_ATTRS, "write_xattr", 'W'},
71      { ACL_READ_ACL, "read_acl", 'c'},
72      { ACL_WRITE_ACL, "write_acl", 'C'},
73      { ACL_WRITE_OWNER, "write_owner", 'o'},
74      { ACL_SYNCHRONIZE, "synchronize", 's'},
75      { ACL_FULL_SET, "full_set", '\0'},
76      { ACL_MODIFY_SET, "modify_set", '\0'},
77      { ACL_READ_SET, "read_set", '\0'},
78      { ACL_WRITE_SET, "write_set", '\0'},
79      { 0, 0, 0}};
80 
81 static const char *
82 format_flag(uint32_t *var, const struct flagnames_struct *flags)
83 {
84 
85 	for (; flags->name != NULL; flags++) {
86 		if ((flags->flag & *var) == 0)
87 			continue;
88 
89 		*var &= ~flags->flag;
90 		return (flags->name);
91 	}
92 
93 	return (NULL);
94 }
95 
96 static int
97 format_flags_verbose(char *str, size_t size, uint32_t var,
98     const struct flagnames_struct *flags)
99 {
100 	size_t off = 0;
101 	const char *tmp;
102 
103 	while ((tmp = format_flag(&var, flags)) != NULL) {
104 		off += snprintf(str + off, size - off, "%s/", tmp);
105 		assert (off < size);
106 	}
107 
108 	/* If there were any flags added... */
109 	if (off > 0) {
110 		off--;
111 		/* ... then remove the last slash. */
112 		assert(str[off] == '/');
113 	}
114 
115 	str[off] = '\0';
116 
117 	return (0);
118 }
119 
120 static int
121 format_flags_compact(char *str, size_t size, uint32_t var,
122     const struct flagnames_struct *flags)
123 {
124 	size_t i;
125 
126 	for (i = 0; flags[i].letter != '\0'; i++) {
127 		assert(i < size);
128 		if ((flags[i].flag & var) == 0)
129 			str[i] = '-';
130 		else
131 			str[i] = flags[i].letter;
132 	}
133 
134 	str[i] = '\0';
135 
136 	return (0);
137 }
138 
139 static int
140 parse_flags_verbose(const char *strp, uint32_t *var,
141     const struct flagnames_struct *flags, const char *flags_name,
142     int *try_compact)
143 {
144 	int i, found, ever_found = 0;
145 	char *str, *flag, *to_free;
146 
147 	str = strdup(strp);
148 	to_free = str;
149 	*try_compact = 0;
150 	*var = 0;
151 
152 	while (str != NULL) {
153 		flag = strsep(&str, "/:");
154 
155 		found = 0;
156 		for (i = 0; flags[i].name != NULL; i++) {
157 			if (strcmp(flags[i].name, flag) == 0) {
158 				*var |= flags[i].flag;
159 				found = 1;
160 				ever_found = 1;
161 			}
162 		}
163 
164 		if (!found) {
165 			if (ever_found)
166 				warnx("malformed ACL: \"%s\" field contains "
167 				    "invalid flag \"%s\"", flags_name, flag);
168 			else
169 				*try_compact = 1;
170 			free(to_free);
171 			return (-1);
172 		}
173 	}
174 
175 	free(to_free);
176 	return (0);
177 }
178 
179 static int
180 parse_flags_compact(const char *str, uint32_t *var,
181     const struct flagnames_struct *flags, const char *flags_name)
182 {
183 	int i, j, found;
184 
185 	*var = 0;
186 
187 	for (i = 0;; i++) {
188 		if (str[i] == '\0')
189 			return (0);
190 
191 		/* Ignore minus signs. */
192 		if (str[i] == '-')
193 			continue;
194 
195 		found = 0;
196 
197 		for (j = 0; flags[j].name != NULL; j++) {
198 			if (flags[j].letter == str[i]) {
199 				*var |= flags[j].flag;
200 				found = 1;
201 				break;
202 			}
203 		}
204 
205 		if (!found) {
206 			warnx("malformed ACL: \"%s\" field contains "
207 			    "invalid flag \"%c\"", flags_name, str[i]);
208 			return (-1);
209 		}
210 	}
211 }
212 
213 int
214 _nfs4_format_flags(char *str, size_t size, acl_flag_t var, int verbose)
215 {
216 
217 	if (verbose)
218 		return (format_flags_verbose(str, size, var, a_flags));
219 
220 	return (format_flags_compact(str, size, var, a_flags));
221 }
222 
223 int
224 _nfs4_format_access_mask(char *str, size_t size, acl_perm_t var, int verbose)
225 {
226 
227 	if (verbose)
228 		return (format_flags_verbose(str, size, var, a_access_masks));
229 
230 	return (format_flags_compact(str, size, var, a_access_masks));
231 }
232 
233 int
234 _nfs4_parse_flags(const char *str, acl_flag_t *flags)
235 {
236 	int error, try_compact;
237 	int tmpflags;
238 
239 	error = parse_flags_verbose(str, &tmpflags, a_flags, "flags", &try_compact);
240 	if (error && try_compact)
241 		error = parse_flags_compact(str, &tmpflags, a_flags, "flags");
242 
243 	*flags = tmpflags;
244 
245 	return (error);
246 }
247 
248 int
249 _nfs4_parse_access_mask(const char *str, acl_perm_t *perms)
250 {
251 	int error, try_compact;
252 	int tmpperms;
253 
254 	error = parse_flags_verbose(str, &tmpperms, a_access_masks,
255 	    "access permissions", &try_compact);
256 	if (error && try_compact)
257 		error = parse_flags_compact(str, &tmpperms,
258 		    a_access_masks, "access permissions");
259 
260 	*perms = tmpperms;
261 
262 	return (error);
263 }
264