acl.h revision 194955
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: head/sys/sys/acl.h 194955 2009-06-25 12:46:59Z trasz $
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	/* "allow" or "deny".  Unused in POSIX ACLs. */
106	acl_entry_type_t	ae_entry_type;
107	/* Flags control inheritance.  Unused in POSIX 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_NFS4_PERM_BITS	(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/*
227 * Possible entry_id values for acl_get_entry(3).
228 */
229#define	ACL_FIRST_ENTRY		0
230#define	ACL_NEXT_ENTRY		1
231
232/*
233 * Possible values in ae_flags field; valid only for NFSv4 ACLs.
234 */
235#define	ACL_ENTRY_FILE_INHERIT		0x0001
236#define	ACL_ENTRY_DIRECTORY_INHERIT	0x0002
237#define	ACL_ENTRY_NO_PROPAGATE_INHERIT	0x0004
238#define	ACL_ENTRY_INHERIT_ONLY		0x0008
239#define	ACL_ENTRY_SUCCESSFUL_ACCESS	0x0010
240#define	ACL_ENTRY_FAILED_ACCESS		0x0020
241
242#define	ACL_FLAGS_BITS			(ACL_ENTRY_FILE_INHERIT | \
243    ACL_ENTRY_DIRECTORY_INHERIT | ACL_ENTRY_NO_PROPAGATE_INHERIT | \
244    ACL_ENTRY_INHERIT_ONLY | ACL_ENTRY_SUCCESSFUL_ACCESS | \
245    ACL_ENTRY_FAILED_ACCESS)
246
247/*
248 * Undefined value in ae_id field.  ae_id should be set to this value
249 * iff ae_tag is ACL_USER_OBJ, ACL_GROUP_OBJ, ACL_OTHER or ACL_EVERYONE.
250 */
251#define	ACL_UNDEFINED_ID	((uid_t)-1)
252
253/*
254 * Possible values for _flags parameter in acl_to_text_np(3).
255 */
256#define	ACL_TEXT_VERBOSE	0x01
257#define	ACL_TEXT_NUMERIC_IDS	0x02
258#define	ACL_TEXT_APPEND_ID	0x04
259
260/*
261 * POSIX.1e ACLs are capable of expressing the read, write, and execute bits
262 * of the POSIX mode field.  We provide two masks: one that defines the bits
263 * the ACL will replace in the mode, and the other that defines the bits that
264 * must be preseved when an ACL is updating a mode.
265 */
266#define	ACL_OVERRIDE_MASK	(S_IRWXU | S_IRWXG | S_IRWXO)
267#define	ACL_PRESERVE_MASK	(~ACL_OVERRIDE_MASK)
268
269#ifdef _KERNEL
270
271/*
272 * Filesystem-independent code to move back and forth between POSIX mode and
273 * POSIX.1e ACL representations.
274 */
275acl_perm_t		acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode);
276struct acl_entry	acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid,
277			    gid_t gid, mode_t mode);
278mode_t			acl_posix1e_perms_to_mode(
279			    struct acl_entry *acl_user_obj_entry,
280			    struct acl_entry *acl_group_obj_entry,
281			    struct acl_entry *acl_other_entry);
282mode_t			acl_posix1e_acl_to_mode(struct acl *acl);
283mode_t			acl_posix1e_newfilemode(mode_t cmode,
284			    struct acl *dacl);
285struct acl		*acl_alloc(int flags);
286void			acl_free(struct acl *aclp);
287
288void			acl_nfs4_sync_acl_from_mode(struct acl *aclp,
289			    mode_t mode, int file_owner_id);
290void			acl_nfs4_sync_mode_from_acl(mode_t *mode,
291			    const struct acl *aclp);
292int			acl_nfs4_is_trivial(const struct acl *aclp,
293			    int file_owner_id);
294void			acl_nfs4_compute_inherited_acl(
295			    const struct acl *parent_aclp,
296			    struct acl *child_aclp, mode_t mode,
297			    int file_owner_id, int is_directory);
298int			acl_copy_oldacl_into_acl(const struct oldacl *source,
299			    struct acl *dest);
300int			acl_copy_acl_into_oldacl(const struct acl *source,
301			    struct oldacl *dest);
302
303/*
304 * To allocate 'struct acl', use acl_alloc()/acl_free() instead of this.
305 */
306MALLOC_DECLARE(M_ACL);
307/*
308 * Filesystem-independent syntax check for a POSIX.1e ACL.
309 */
310int			acl_posix1e_check(struct acl *acl);
311int 			acl_nfs4_check(const struct acl *aclp, int is_directory);
312
313#else /* !_KERNEL */
314
315#if defined(_ACL_PRIVATE)
316
317/*
318 * Syscall interface -- use the library calls instead as the syscalls have
319 * strict ACL entry ordering requirements.
320 */
321__BEGIN_DECLS
322int	__acl_aclcheck_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
323int	__acl_aclcheck_file(const char *_path, acl_type_t _type,
324	    struct acl *_aclp);
325int	__acl_aclcheck_link(const char *_path, acl_type_t _type,
326	    struct acl *_aclp);
327int	__acl_delete_fd(int _filedes, acl_type_t _type);
328int	__acl_delete_file(const char *_path_p, acl_type_t _type);
329int	__acl_delete_link(const char *_path_p, acl_type_t _type);
330int	__acl_get_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
331int	__acl_get_file(const char *_path, acl_type_t _type, struct acl *_aclp);
332int	__acl_get_link(const char *_path, acl_type_t _type, struct acl *_aclp);
333int	__acl_set_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
334int	__acl_set_file(const char *_path, acl_type_t _type, struct acl *_aclp);
335int	__acl_set_link(const char *_path, acl_type_t _type, struct acl *_aclp);
336__END_DECLS
337
338#endif /* _ACL_PRIVATE */
339
340/*
341 * Supported POSIX.1e ACL manipulation and assignment/retrieval API _np calls
342 * are local extensions that reflect an environment capable of opening file
343 * descriptors of directories, and allowing additional ACL type for different
344 * filesystems (i.e., AFS).
345 */
346__BEGIN_DECLS
347int	acl_add_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
348int	acl_add_perm(acl_permset_t _permset_d, acl_perm_t _perm);
349int	acl_calc_mask(acl_t *_acl_p);
350int	acl_clear_flags_np(acl_flagset_t _flagset_d);
351int	acl_clear_perms(acl_permset_t _permset_d);
352int	acl_copy_entry(acl_entry_t _dest_d, acl_entry_t _src_d);
353ssize_t	acl_copy_ext(void *_buf_p, acl_t _acl, ssize_t _size);
354acl_t	acl_copy_int(const void *_buf_p);
355int	acl_create_entry(acl_t *_acl_p, acl_entry_t *_entry_p);
356int	acl_create_entry_np(acl_t *_acl_p, acl_entry_t *_entry_p, int _index);
357int	acl_delete_entry(acl_t _acl, acl_entry_t _entry_d);
358int	acl_delete_entry_np(acl_t _acl, int _index);
359int	acl_delete_fd_np(int _filedes, acl_type_t _type);
360int	acl_delete_file_np(const char *_path_p, acl_type_t _type);
361int	acl_delete_link_np(const char *_path_p, acl_type_t _type);
362int	acl_delete_def_file(const char *_path_p);
363int	acl_delete_def_link_np(const char *_path_p);
364int	acl_delete_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
365int	acl_delete_perm(acl_permset_t _permset_d, acl_perm_t _perm);
366acl_t	acl_dup(acl_t _acl);
367int	acl_free(void *_obj_p);
368acl_t	acl_from_text(const char *_buf_p);
369int	acl_get_brand_np(acl_t _acl, int *_brand_p);
370int	acl_get_entry(acl_t _acl, int _entry_id, acl_entry_t *_entry_p);
371acl_t	acl_get_fd(int _fd);
372acl_t	acl_get_fd_np(int fd, acl_type_t _type);
373acl_t	acl_get_file(const char *_path_p, acl_type_t _type);
374int	acl_get_entry_type_np(acl_entry_t _entry_d, acl_entry_type_t *_entry_type_p);
375acl_t	acl_get_link_np(const char *_path_p, acl_type_t _type);
376void	*acl_get_qualifier(acl_entry_t _entry_d);
377int	acl_get_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
378int	acl_get_perm_np(acl_permset_t _permset_d, acl_perm_t _perm);
379int	acl_get_flagset_np(acl_entry_t _entry_d, acl_flagset_t *_flagset_p);
380int	acl_get_permset(acl_entry_t _entry_d, acl_permset_t *_permset_p);
381int	acl_get_tag_type(acl_entry_t _entry_d, acl_tag_t *_tag_type_p);
382acl_t	acl_init(int _count);
383int	acl_set_fd(int _fd, acl_t _acl);
384int	acl_set_fd_np(int _fd, acl_t _acl, acl_type_t _type);
385int	acl_set_file(const char *_path_p, acl_type_t _type, acl_t _acl);
386int	acl_set_entry_type_np(acl_entry_t _entry_d, acl_entry_type_t _entry_type);
387int	acl_set_link_np(const char *_path_p, acl_type_t _type, acl_t _acl);
388int	acl_set_flagset_np(acl_entry_t _entry_d, acl_flagset_t _flagset_d);
389int	acl_set_permset(acl_entry_t _entry_d, acl_permset_t _permset_d);
390int	acl_set_qualifier(acl_entry_t _entry_d, const void *_tag_qualifier_p);
391int	acl_set_tag_type(acl_entry_t _entry_d, acl_tag_t _tag_type);
392ssize_t	acl_size(acl_t _acl);
393char	*acl_to_text(acl_t _acl, ssize_t *_len_p);
394char	*acl_to_text_np(acl_t _acl, ssize_t *_len_p, int _flags);
395int	acl_valid(acl_t _acl);
396int	acl_valid_fd_np(int _fd, acl_type_t _type, acl_t _acl);
397int	acl_valid_file_np(const char *_path_p, acl_type_t _type, acl_t _acl);
398int	acl_valid_link_np(const char *_path_p, acl_type_t _type, acl_t _acl);
399int	acl_is_trivial_np(const acl_t _acl, int *_trivialp);
400acl_t	acl_strip_np(const acl_t _acl, int recalculate_mask);
401__END_DECLS
402
403#endif /* !_KERNEL */
404
405#endif /* !_SYS_ACL_H_ */
406