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