xref: /freebsd/sys/sys/acl.h (revision a0ee8cc6)
1 /*-
2  * Copyright (c) 1999-2001 Robert N. M. Watson
3  * Copyright (c) 2008 Edward Tomasz Napierała <trasz@FreeBSD.org>
4  * All rights reserved.
5  *
6  * This software was developed by Robert Watson for the TrustedBSD Project.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 /*
32  * Developed by the TrustedBSD Project.
33  * Support for POSIX.1e and NFSv4 access control lists.
34  */
35 
36 #ifndef _SYS_ACL_H_
37 #define	_SYS_ACL_H_
38 
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <vm/uma.h>
42 
43 /*
44  * POSIX.1e and NFSv4 ACL types and related constants.
45  */
46 
47 typedef uint32_t	acl_tag_t;
48 typedef uint32_t	acl_perm_t;
49 typedef uint16_t	acl_entry_type_t;
50 typedef uint16_t	acl_flag_t;
51 typedef int		acl_type_t;
52 typedef int		*acl_permset_t;
53 typedef uint16_t	*acl_flagset_t;
54 
55 /*
56  * With 254 entries, "struct acl_t_struct" is exactly one 4kB page big.
57  * Note that with NFSv4 ACLs, the maximum number of ACL entries one
58  * may set on file or directory is about half of ACL_MAX_ENTRIES.
59  *
60  * If you increase this, you might also need to increase
61  * _ACL_T_ALIGNMENT_BITS in lib/libc/posix1e/acl_support.h.
62  *
63  * The maximum number of POSIX.1e ACLs is controlled
64  * by OLDACL_MAX_ENTRIES.  Changing that one will break binary
65  * compatibility with pre-8.0 userland and change on-disk ACL layout.
66  */
67 #define	ACL_MAX_ENTRIES				254
68 
69 #if defined(_KERNEL) || defined(_ACL_PRIVATE)
70 
71 #define	POSIX1E_ACL_ACCESS_EXTATTR_NAMESPACE	EXTATTR_NAMESPACE_SYSTEM
72 #define	POSIX1E_ACL_ACCESS_EXTATTR_NAME		"posix1e.acl_access"
73 #define	POSIX1E_ACL_DEFAULT_EXTATTR_NAMESPACE	EXTATTR_NAMESPACE_SYSTEM
74 #define	POSIX1E_ACL_DEFAULT_EXTATTR_NAME	"posix1e.acl_default"
75 #define	NFS4_ACL_EXTATTR_NAMESPACE		EXTATTR_NAMESPACE_SYSTEM
76 #define	NFS4_ACL_EXTATTR_NAME			"nfs4.acl"
77 #define	OLDACL_MAX_ENTRIES			32
78 
79 /*
80  * "struct oldacl" is used in compatibility ACL syscalls and for on-disk
81  * storage of POSIX.1e ACLs.
82  */
83 typedef int	oldacl_tag_t;
84 typedef mode_t	oldacl_perm_t;
85 
86 struct oldacl_entry {
87 	oldacl_tag_t	ae_tag;
88 	uid_t		ae_id;
89 	oldacl_perm_t	ae_perm;
90 };
91 typedef struct oldacl_entry	*oldacl_entry_t;
92 
93 struct oldacl {
94 	int			acl_cnt;
95 	struct oldacl_entry	acl_entry[OLDACL_MAX_ENTRIES];
96 };
97 
98 /*
99  * Current "struct acl".
100  */
101 struct acl_entry {
102 	acl_tag_t		ae_tag;
103 	uid_t			ae_id;
104 	acl_perm_t		ae_perm;
105 	/* NFSv4 entry type, "allow" or "deny".  Unused in POSIX.1e ACLs. */
106 	acl_entry_type_t	ae_entry_type;
107 	/* NFSv4 ACL inheritance.  Unused in POSIX.1e ACLs. */
108 	acl_flag_t		ae_flags;
109 };
110 typedef struct acl_entry	*acl_entry_t;
111 
112 /*
113  * Internal ACL structure, used in libc, kernel APIs and for on-disk
114  * storage of NFSv4 ACLs.  POSIX.1e ACLs use "struct oldacl" for on-disk
115  * storage.
116  */
117 struct acl {
118 	unsigned int		acl_maxcnt;
119 	unsigned int		acl_cnt;
120 	/* Will be required e.g. to implement NFSv4.1 ACL inheritance. */
121 	int			acl_spare[4];
122 	struct acl_entry	acl_entry[ACL_MAX_ENTRIES];
123 };
124 
125 /*
126  * ACL structure internal to libc.
127  */
128 struct acl_t_struct {
129 	struct acl		ats_acl;
130 	int			ats_cur_entry;
131 	/*
132 	 * ats_brand is for libc internal bookkeeping only.
133 	 * Applications should use acl_get_brand_np(3).
134 	 * Kernel code should use the "type" argument passed
135 	 * to VOP_SETACL, VOP_GETACL or VOP_ACLCHECK calls;
136 	 * ACL_TYPE_ACCESS or ACL_TYPE_DEFAULT mean POSIX.1e
137 	 * ACL, ACL_TYPE_NFS4 means NFSv4 ACL.
138 	 */
139 	int			ats_brand;
140 };
141 typedef struct acl_t_struct *acl_t;
142 
143 #else /* _KERNEL || _ACL_PRIVATE */
144 
145 typedef void *acl_entry_t;
146 typedef void *acl_t;
147 
148 #endif /* !_KERNEL && !_ACL_PRIVATE */
149 
150 /*
151  * Possible valid values for ats_brand field.
152  */
153 #define	ACL_BRAND_UNKNOWN	0
154 #define	ACL_BRAND_POSIX		1
155 #define	ACL_BRAND_NFS4		2
156 
157 /*
158  * Possible valid values for ae_tag field.  For explanation, see acl(9).
159  */
160 #define	ACL_UNDEFINED_TAG	0x00000000
161 #define	ACL_USER_OBJ		0x00000001
162 #define	ACL_USER		0x00000002
163 #define	ACL_GROUP_OBJ		0x00000004
164 #define	ACL_GROUP		0x00000008
165 #define	ACL_MASK		0x00000010
166 #define	ACL_OTHER		0x00000020
167 #define	ACL_OTHER_OBJ		ACL_OTHER
168 #define	ACL_EVERYONE		0x00000040
169 
170 /*
171  * Possible valid values for ae_entry_type field, valid only for NFSv4 ACLs.
172  */
173 #define	ACL_ENTRY_TYPE_ALLOW	0x0100
174 #define	ACL_ENTRY_TYPE_DENY	0x0200
175 #define	ACL_ENTRY_TYPE_AUDIT	0x0400
176 #define	ACL_ENTRY_TYPE_ALARM	0x0800
177 
178 /*
179  * Possible valid values for acl_type_t arguments.  First two
180  * are provided only for backwards binary compatibility.
181  */
182 #define	ACL_TYPE_ACCESS_OLD	0x00000000
183 #define	ACL_TYPE_DEFAULT_OLD	0x00000001
184 #define	ACL_TYPE_ACCESS		0x00000002
185 #define	ACL_TYPE_DEFAULT	0x00000003
186 #define	ACL_TYPE_NFS4		0x00000004
187 
188 /*
189  * Possible bits in ae_perm field for POSIX.1e ACLs.  Note
190  * that ACL_EXECUTE may be used in both NFSv4 and POSIX.1e ACLs.
191  */
192 #define	ACL_EXECUTE		0x0001
193 #define	ACL_WRITE		0x0002
194 #define	ACL_READ		0x0004
195 #define	ACL_PERM_NONE		0x0000
196 #define	ACL_PERM_BITS		(ACL_EXECUTE | ACL_WRITE | ACL_READ)
197 #define	ACL_POSIX1E_BITS	(ACL_EXECUTE | ACL_WRITE | ACL_READ)
198 
199 /*
200  * Possible bits in ae_perm field for NFSv4 ACLs.
201  */
202 #define	ACL_READ_DATA		0x00000008
203 #define	ACL_LIST_DIRECTORY	0x00000008
204 #define	ACL_WRITE_DATA		0x00000010
205 #define	ACL_ADD_FILE		0x00000010
206 #define	ACL_APPEND_DATA		0x00000020
207 #define	ACL_ADD_SUBDIRECTORY	0x00000020
208 #define	ACL_READ_NAMED_ATTRS	0x00000040
209 #define	ACL_WRITE_NAMED_ATTRS	0x00000080
210 /* ACL_EXECUTE is defined above. */
211 #define	ACL_DELETE_CHILD	0x00000100
212 #define	ACL_READ_ATTRIBUTES	0x00000200
213 #define	ACL_WRITE_ATTRIBUTES	0x00000400
214 #define	ACL_DELETE		0x00000800
215 #define	ACL_READ_ACL		0x00001000
216 #define	ACL_WRITE_ACL		0x00002000
217 #define	ACL_WRITE_OWNER		0x00004000
218 #define	ACL_SYNCHRONIZE		0x00008000
219 
220 #define	ACL_FULL_SET		(ACL_READ_DATA | ACL_WRITE_DATA | \
221     ACL_APPEND_DATA | ACL_READ_NAMED_ATTRS | ACL_WRITE_NAMED_ATTRS | \
222     ACL_EXECUTE | ACL_DELETE_CHILD | ACL_READ_ATTRIBUTES | \
223     ACL_WRITE_ATTRIBUTES | ACL_DELETE | ACL_READ_ACL | ACL_WRITE_ACL | \
224     ACL_WRITE_OWNER | ACL_SYNCHRONIZE)
225 
226 #define	ACL_MODIFY_SET		(ACL_FULL_SET & \
227     ~(ACL_WRITE_ACL | ACL_WRITE_OWNER))
228 
229 #define	ACL_READ_SET		(ACL_READ_DATA | ACL_READ_NAMED_ATTRS | \
230     ACL_READ_ATTRIBUTES | ACL_READ_ACL)
231 
232 #define	ACL_WRITE_SET		(ACL_WRITE_DATA | ACL_APPEND_DATA | \
233     ACL_WRITE_NAMED_ATTRS | ACL_WRITE_ATTRIBUTES)
234 
235 #define	ACL_NFS4_PERM_BITS	ACL_FULL_SET
236 
237 /*
238  * Possible entry_id values for acl_get_entry(3).
239  */
240 #define	ACL_FIRST_ENTRY		0
241 #define	ACL_NEXT_ENTRY		1
242 
243 /*
244  * Possible values in ae_flags field; valid only for NFSv4 ACLs.
245  */
246 #define	ACL_ENTRY_FILE_INHERIT		0x0001
247 #define	ACL_ENTRY_DIRECTORY_INHERIT	0x0002
248 #define	ACL_ENTRY_NO_PROPAGATE_INHERIT	0x0004
249 #define	ACL_ENTRY_INHERIT_ONLY		0x0008
250 #define	ACL_ENTRY_SUCCESSFUL_ACCESS	0x0010
251 #define	ACL_ENTRY_FAILED_ACCESS		0x0020
252 #define	ACL_ENTRY_INHERITED		0x0080
253 
254 #define	ACL_FLAGS_BITS			(ACL_ENTRY_FILE_INHERIT | \
255     ACL_ENTRY_DIRECTORY_INHERIT | ACL_ENTRY_NO_PROPAGATE_INHERIT | \
256     ACL_ENTRY_INHERIT_ONLY | ACL_ENTRY_SUCCESSFUL_ACCESS | \
257     ACL_ENTRY_FAILED_ACCESS | ACL_ENTRY_INHERITED)
258 
259 /*
260  * Undefined value in ae_id field.  ae_id should be set to this value
261  * iff ae_tag is ACL_USER_OBJ, ACL_GROUP_OBJ, ACL_OTHER or ACL_EVERYONE.
262  */
263 #define	ACL_UNDEFINED_ID	((uid_t)-1)
264 
265 /*
266  * Possible values for _flags parameter in acl_to_text_np(3).
267  */
268 #define	ACL_TEXT_VERBOSE	0x01
269 #define	ACL_TEXT_NUMERIC_IDS	0x02
270 #define	ACL_TEXT_APPEND_ID	0x04
271 
272 /*
273  * POSIX.1e ACLs are capable of expressing the read, write, and execute bits
274  * of the POSIX mode field.  We provide two masks: one that defines the bits
275  * the ACL will replace in the mode, and the other that defines the bits that
276  * must be preseved when an ACL is updating a mode.
277  */
278 #define	ACL_OVERRIDE_MASK	(S_IRWXU | S_IRWXG | S_IRWXO)
279 #define	ACL_PRESERVE_MASK	(~ACL_OVERRIDE_MASK)
280 
281 #ifdef _KERNEL
282 
283 /*
284  * Filesystem-independent code to move back and forth between POSIX mode and
285  * POSIX.1e ACL representations.
286  */
287 acl_perm_t		acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode);
288 struct acl_entry	acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid,
289 			    gid_t gid, mode_t mode);
290 mode_t			acl_posix1e_perms_to_mode(
291 			    struct acl_entry *acl_user_obj_entry,
292 			    struct acl_entry *acl_group_obj_entry,
293 			    struct acl_entry *acl_other_entry);
294 mode_t			acl_posix1e_acl_to_mode(struct acl *acl);
295 mode_t			acl_posix1e_newfilemode(mode_t cmode,
296 			    struct acl *dacl);
297 struct acl		*acl_alloc(int flags);
298 void			acl_free(struct acl *aclp);
299 
300 void			acl_nfs4_sync_acl_from_mode(struct acl *aclp,
301 			    mode_t mode, int file_owner_id);
302 void			acl_nfs4_sync_mode_from_acl(mode_t *mode,
303 			    const struct acl *aclp);
304 int			acl_nfs4_is_trivial(const struct acl *aclp,
305 			    int file_owner_id);
306 void			acl_nfs4_compute_inherited_acl(
307 			    const struct acl *parent_aclp,
308 			    struct acl *child_aclp, mode_t mode,
309 			    int file_owner_id, int is_directory);
310 int			acl_copy_oldacl_into_acl(const struct oldacl *source,
311 			    struct acl *dest);
312 int			acl_copy_acl_into_oldacl(const struct acl *source,
313 			    struct oldacl *dest);
314 
315 /*
316  * To allocate 'struct acl', use acl_alloc()/acl_free() instead of this.
317  */
318 MALLOC_DECLARE(M_ACL);
319 /*
320  * Filesystem-independent syntax check for a POSIX.1e ACL.
321  */
322 int			acl_posix1e_check(struct acl *acl);
323 int 			acl_nfs4_check(const struct acl *aclp, int is_directory);
324 
325 #else /* !_KERNEL */
326 
327 #if defined(_ACL_PRIVATE)
328 
329 /*
330  * Syscall interface -- use the library calls instead as the syscalls have
331  * strict ACL entry ordering requirements.
332  */
333 __BEGIN_DECLS
334 int	__acl_aclcheck_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
335 int	__acl_aclcheck_file(const char *_path, acl_type_t _type,
336 	    struct acl *_aclp);
337 int	__acl_aclcheck_link(const char *_path, acl_type_t _type,
338 	    struct acl *_aclp);
339 int	__acl_delete_fd(int _filedes, acl_type_t _type);
340 int	__acl_delete_file(const char *_path_p, acl_type_t _type);
341 int	__acl_delete_link(const char *_path_p, acl_type_t _type);
342 int	__acl_get_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
343 int	__acl_get_file(const char *_path, acl_type_t _type, struct acl *_aclp);
344 int	__acl_get_link(const char *_path, acl_type_t _type, struct acl *_aclp);
345 int	__acl_set_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
346 int	__acl_set_file(const char *_path, acl_type_t _type, struct acl *_aclp);
347 int	__acl_set_link(const char *_path, acl_type_t _type, struct acl *_aclp);
348 __END_DECLS
349 
350 #endif /* _ACL_PRIVATE */
351 
352 /*
353  * Supported POSIX.1e ACL manipulation and assignment/retrieval API _np calls
354  * are local extensions that reflect an environment capable of opening file
355  * descriptors of directories, and allowing additional ACL type for different
356  * filesystems (i.e., AFS).
357  */
358 __BEGIN_DECLS
359 int	acl_add_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
360 int	acl_add_perm(acl_permset_t _permset_d, acl_perm_t _perm);
361 int	acl_calc_mask(acl_t *_acl_p);
362 int	acl_clear_flags_np(acl_flagset_t _flagset_d);
363 int	acl_clear_perms(acl_permset_t _permset_d);
364 int	acl_copy_entry(acl_entry_t _dest_d, acl_entry_t _src_d);
365 ssize_t	acl_copy_ext(void *_buf_p, acl_t _acl, ssize_t _size);
366 acl_t	acl_copy_int(const void *_buf_p);
367 int	acl_create_entry(acl_t *_acl_p, acl_entry_t *_entry_p);
368 int	acl_create_entry_np(acl_t *_acl_p, acl_entry_t *_entry_p, int _index);
369 int	acl_delete_entry(acl_t _acl, acl_entry_t _entry_d);
370 int	acl_delete_entry_np(acl_t _acl, int _index);
371 int	acl_delete_fd_np(int _filedes, acl_type_t _type);
372 int	acl_delete_file_np(const char *_path_p, acl_type_t _type);
373 int	acl_delete_link_np(const char *_path_p, acl_type_t _type);
374 int	acl_delete_def_file(const char *_path_p);
375 int	acl_delete_def_link_np(const char *_path_p);
376 int	acl_delete_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
377 int	acl_delete_perm(acl_permset_t _permset_d, acl_perm_t _perm);
378 acl_t	acl_dup(acl_t _acl);
379 int	acl_free(void *_obj_p);
380 acl_t	acl_from_text(const char *_buf_p);
381 int	acl_get_brand_np(acl_t _acl, int *_brand_p);
382 int	acl_get_entry(acl_t _acl, int _entry_id, acl_entry_t *_entry_p);
383 acl_t	acl_get_fd(int _fd);
384 acl_t	acl_get_fd_np(int fd, acl_type_t _type);
385 acl_t	acl_get_file(const char *_path_p, acl_type_t _type);
386 int	acl_get_entry_type_np(acl_entry_t _entry_d, acl_entry_type_t *_entry_type_p);
387 acl_t	acl_get_link_np(const char *_path_p, acl_type_t _type);
388 void	*acl_get_qualifier(acl_entry_t _entry_d);
389 int	acl_get_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
390 int	acl_get_perm_np(acl_permset_t _permset_d, acl_perm_t _perm);
391 int	acl_get_flagset_np(acl_entry_t _entry_d, acl_flagset_t *_flagset_p);
392 int	acl_get_permset(acl_entry_t _entry_d, acl_permset_t *_permset_p);
393 int	acl_get_tag_type(acl_entry_t _entry_d, acl_tag_t *_tag_type_p);
394 acl_t	acl_init(int _count);
395 int	acl_set_fd(int _fd, acl_t _acl);
396 int	acl_set_fd_np(int _fd, acl_t _acl, acl_type_t _type);
397 int	acl_set_file(const char *_path_p, acl_type_t _type, acl_t _acl);
398 int	acl_set_entry_type_np(acl_entry_t _entry_d, acl_entry_type_t _entry_type);
399 int	acl_set_link_np(const char *_path_p, acl_type_t _type, acl_t _acl);
400 int	acl_set_flagset_np(acl_entry_t _entry_d, acl_flagset_t _flagset_d);
401 int	acl_set_permset(acl_entry_t _entry_d, acl_permset_t _permset_d);
402 int	acl_set_qualifier(acl_entry_t _entry_d, const void *_tag_qualifier_p);
403 int	acl_set_tag_type(acl_entry_t _entry_d, acl_tag_t _tag_type);
404 ssize_t	acl_size(acl_t _acl);
405 char	*acl_to_text(acl_t _acl, ssize_t *_len_p);
406 char	*acl_to_text_np(acl_t _acl, ssize_t *_len_p, int _flags);
407 int	acl_valid(acl_t _acl);
408 int	acl_valid_fd_np(int _fd, acl_type_t _type, acl_t _acl);
409 int	acl_valid_file_np(const char *_path_p, acl_type_t _type, acl_t _acl);
410 int	acl_valid_link_np(const char *_path_p, acl_type_t _type, acl_t _acl);
411 int	acl_is_trivial_np(const acl_t _acl, int *_trivialp);
412 acl_t	acl_strip_np(const acl_t _acl, int recalculate_mask);
413 __END_DECLS
414 
415 #endif /* !_KERNEL */
416 
417 #endif /* !_SYS_ACL_H_ */
418