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