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