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
47typedef uint32_t	acl_tag_t;
48typedef uint32_t	acl_perm_t;
49typedef uint16_t	acl_entry_type_t;
50typedef uint16_t	acl_flag_t;
51typedef int		acl_type_t;
52typedef int		*acl_permset_t;
53typedef 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 */
83typedef int	oldacl_tag_t;
84typedef mode_t	oldacl_perm_t;
85
86struct oldacl_entry {
87	oldacl_tag_t	ae_tag;
88	uid_t		ae_id;
89	oldacl_perm_t	ae_perm;
90};
91typedef struct oldacl_entry	*oldacl_entry_t;
92
93struct oldacl {
94	int			acl_cnt;
95	struct oldacl_entry	acl_entry[OLDACL_MAX_ENTRIES];
96};
97
98/*
99 * Current "struct acl".
100 */
101struct 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};
110typedef 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 */
117struct 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 */
128struct 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};
141typedef struct acl_t_struct *acl_t;
142
143#else /* _KERNEL || _ACL_PRIVATE */
144
145typedef void *acl_entry_t;
146typedef 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
253#define	ACL_FLAGS_BITS			(ACL_ENTRY_FILE_INHERIT | \
254    ACL_ENTRY_DIRECTORY_INHERIT | ACL_ENTRY_NO_PROPAGATE_INHERIT | \
255    ACL_ENTRY_INHERIT_ONLY | ACL_ENTRY_SUCCESSFUL_ACCESS | \
256    ACL_ENTRY_FAILED_ACCESS)
257
258/*
259 * Undefined value in ae_id field.  ae_id should be set to this value
260 * iff ae_tag is ACL_USER_OBJ, ACL_GROUP_OBJ, ACL_OTHER or ACL_EVERYONE.
261 */
262#define	ACL_UNDEFINED_ID	((uid_t)-1)
263
264/*
265 * Possible values for _flags parameter in acl_to_text_np(3).
266 */
267#define	ACL_TEXT_VERBOSE	0x01
268#define	ACL_TEXT_NUMERIC_IDS	0x02
269#define	ACL_TEXT_APPEND_ID	0x04
270
271/*
272 * POSIX.1e ACLs are capable of expressing the read, write, and execute bits
273 * of the POSIX mode field.  We provide two masks: one that defines the bits
274 * the ACL will replace in the mode, and the other that defines the bits that
275 * must be preseved when an ACL is updating a mode.
276 */
277#define	ACL_OVERRIDE_MASK	(S_IRWXU | S_IRWXG | S_IRWXO)
278#define	ACL_PRESERVE_MASK	(~ACL_OVERRIDE_MASK)
279
280#ifdef _KERNEL
281
282/*
283 * Filesystem-independent code to move back and forth between POSIX mode and
284 * POSIX.1e ACL representations.
285 */
286acl_perm_t		acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode);
287struct acl_entry	acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid,
288			    gid_t gid, mode_t mode);
289mode_t			acl_posix1e_perms_to_mode(
290			    struct acl_entry *acl_user_obj_entry,
291			    struct acl_entry *acl_group_obj_entry,
292			    struct acl_entry *acl_other_entry);
293mode_t			acl_posix1e_acl_to_mode(struct acl *acl);
294mode_t			acl_posix1e_newfilemode(mode_t cmode,
295			    struct acl *dacl);
296struct acl		*acl_alloc(int flags);
297void			acl_free(struct acl *aclp);
298
299void			acl_nfs4_sync_acl_from_mode(struct acl *aclp,
300			    mode_t mode, int file_owner_id);
301void			acl_nfs4_sync_mode_from_acl(mode_t *mode,
302			    const struct acl *aclp);
303int			acl_nfs4_is_trivial(const struct acl *aclp,
304			    int file_owner_id);
305void			acl_nfs4_compute_inherited_acl(
306			    const struct acl *parent_aclp,
307			    struct acl *child_aclp, mode_t mode,
308			    int file_owner_id, int is_directory);
309int			acl_copy_oldacl_into_acl(const struct oldacl *source,
310			    struct acl *dest);
311int			acl_copy_acl_into_oldacl(const struct acl *source,
312			    struct oldacl *dest);
313
314/*
315 * To allocate 'struct acl', use acl_alloc()/acl_free() instead of this.
316 */
317MALLOC_DECLARE(M_ACL);
318/*
319 * Filesystem-independent syntax check for a POSIX.1e ACL.
320 */
321int			acl_posix1e_check(struct acl *acl);
322int 			acl_nfs4_check(const struct acl *aclp, int is_directory);
323
324#else /* !_KERNEL */
325
326#if defined(_ACL_PRIVATE)
327
328/*
329 * Syscall interface -- use the library calls instead as the syscalls have
330 * strict ACL entry ordering requirements.
331 */
332__BEGIN_DECLS
333int	__acl_aclcheck_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
334int	__acl_aclcheck_file(const char *_path, acl_type_t _type,
335	    struct acl *_aclp);
336int	__acl_aclcheck_link(const char *_path, acl_type_t _type,
337	    struct acl *_aclp);
338int	__acl_delete_fd(int _filedes, acl_type_t _type);
339int	__acl_delete_file(const char *_path_p, acl_type_t _type);
340int	__acl_delete_link(const char *_path_p, acl_type_t _type);
341int	__acl_get_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
342int	__acl_get_file(const char *_path, acl_type_t _type, struct acl *_aclp);
343int	__acl_get_link(const char *_path, acl_type_t _type, struct acl *_aclp);
344int	__acl_set_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
345int	__acl_set_file(const char *_path, acl_type_t _type, struct acl *_aclp);
346int	__acl_set_link(const char *_path, acl_type_t _type, struct acl *_aclp);
347__END_DECLS
348
349#endif /* _ACL_PRIVATE */
350
351/*
352 * Supported POSIX.1e ACL manipulation and assignment/retrieval API _np calls
353 * are local extensions that reflect an environment capable of opening file
354 * descriptors of directories, and allowing additional ACL type for different
355 * filesystems (i.e., AFS).
356 */
357__BEGIN_DECLS
358int	acl_add_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
359int	acl_add_perm(acl_permset_t _permset_d, acl_perm_t _perm);
360int	acl_calc_mask(acl_t *_acl_p);
361int	acl_clear_flags_np(acl_flagset_t _flagset_d);
362int	acl_clear_perms(acl_permset_t _permset_d);
363int	acl_copy_entry(acl_entry_t _dest_d, acl_entry_t _src_d);
364ssize_t	acl_copy_ext(void *_buf_p, acl_t _acl, ssize_t _size);
365acl_t	acl_copy_int(const void *_buf_p);
366int	acl_create_entry(acl_t *_acl_p, acl_entry_t *_entry_p);
367int	acl_create_entry_np(acl_t *_acl_p, acl_entry_t *_entry_p, int _index);
368int	acl_delete_entry(acl_t _acl, acl_entry_t _entry_d);
369int	acl_delete_entry_np(acl_t _acl, int _index);
370int	acl_delete_fd_np(int _filedes, acl_type_t _type);
371int	acl_delete_file_np(const char *_path_p, acl_type_t _type);
372int	acl_delete_link_np(const char *_path_p, acl_type_t _type);
373int	acl_delete_def_file(const char *_path_p);
374int	acl_delete_def_link_np(const char *_path_p);
375int	acl_delete_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
376int	acl_delete_perm(acl_permset_t _permset_d, acl_perm_t _perm);
377acl_t	acl_dup(acl_t _acl);
378int	acl_free(void *_obj_p);
379acl_t	acl_from_text(const char *_buf_p);
380int	acl_get_brand_np(acl_t _acl, int *_brand_p);
381int	acl_get_entry(acl_t _acl, int _entry_id, acl_entry_t *_entry_p);
382acl_t	acl_get_fd(int _fd);
383acl_t	acl_get_fd_np(int fd, acl_type_t _type);
384acl_t	acl_get_file(const char *_path_p, acl_type_t _type);
385int	acl_get_entry_type_np(acl_entry_t _entry_d, acl_entry_type_t *_entry_type_p);
386acl_t	acl_get_link_np(const char *_path_p, acl_type_t _type);
387void	*acl_get_qualifier(acl_entry_t _entry_d);
388int	acl_get_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
389int	acl_get_perm_np(acl_permset_t _permset_d, acl_perm_t _perm);
390int	acl_get_flagset_np(acl_entry_t _entry_d, acl_flagset_t *_flagset_p);
391int	acl_get_permset(acl_entry_t _entry_d, acl_permset_t *_permset_p);
392int	acl_get_tag_type(acl_entry_t _entry_d, acl_tag_t *_tag_type_p);
393acl_t	acl_init(int _count);
394int	acl_set_fd(int _fd, acl_t _acl);
395int	acl_set_fd_np(int _fd, acl_t _acl, acl_type_t _type);
396int	acl_set_file(const char *_path_p, acl_type_t _type, acl_t _acl);
397int	acl_set_entry_type_np(acl_entry_t _entry_d, acl_entry_type_t _entry_type);
398int	acl_set_link_np(const char *_path_p, acl_type_t _type, acl_t _acl);
399int	acl_set_flagset_np(acl_entry_t _entry_d, acl_flagset_t _flagset_d);
400int	acl_set_permset(acl_entry_t _entry_d, acl_permset_t _permset_d);
401int	acl_set_qualifier(acl_entry_t _entry_d, const void *_tag_qualifier_p);
402int	acl_set_tag_type(acl_entry_t _entry_d, acl_tag_t _tag_type);
403ssize_t	acl_size(acl_t _acl);
404char	*acl_to_text(acl_t _acl, ssize_t *_len_p);
405char	*acl_to_text_np(acl_t _acl, ssize_t *_len_p, int _flags);
406int	acl_valid(acl_t _acl);
407int	acl_valid_fd_np(int _fd, acl_type_t _type, acl_t _acl);
408int	acl_valid_file_np(const char *_path_p, acl_type_t _type, acl_t _acl);
409int	acl_valid_link_np(const char *_path_p, acl_type_t _type, acl_t _acl);
410int	acl_is_trivial_np(const acl_t _acl, int *_trivialp);
411acl_t	acl_strip_np(const acl_t _acl, int recalculate_mask);
412__END_DECLS
413
414#endif /* !_KERNEL */
415
416#endif /* !_SYS_ACL_H_ */
417