2 * Copyright (c) 1999-2001 Robert N. M. Watson
3 * Copyright (c) 2008 Edward Tomasz NapieraĆa <trasz@FreeBSD.org>
6 * This software was developed by Robert Watson for the TrustedBSD Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * Developed by the TrustedBSD Project.
33 * Support for POSIX.1e and NFSv4 access control lists.
39 #include <sys/param.h>
40 #include <sys/queue.h>
44 * POSIX.1e and NFSv4 ACL types and related constants.
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;
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.
60 * If you increase this, you might also need to increase
61 * _ACL_T_ALIGNMENT_BITS in lib/libc/posix1e/acl_support.h.
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.
67 #define ACL_MAX_ENTRIES 254
69 #if defined(_KERNEL) || defined(_ACL_PRIVATE)
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
80 * "struct oldacl" is used in compatibility ACL syscalls and for on-disk
81 * storage of POSIX.1e ACLs.
83 typedef int oldacl_tag_t;
84 typedef mode_t oldacl_perm_t;
89 oldacl_perm_t ae_perm;
91 typedef struct oldacl_entry *oldacl_entry_t;
95 struct oldacl_entry acl_entry[OLDACL_MAX_ENTRIES];
99 * Current "struct acl".
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. */
110 typedef struct acl_entry *acl_entry_t;
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
118 unsigned int acl_maxcnt;
119 unsigned int acl_cnt;
120 /* Will be required e.g. to implement NFSv4.1 ACL inheritance. */
122 struct acl_entry acl_entry[ACL_MAX_ENTRIES];
126 * ACL structure internal to libc.
128 struct acl_t_struct {
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.
141 typedef struct acl_t_struct *acl_t;
143 #else /* _KERNEL || _ACL_PRIVATE */
145 typedef void *acl_entry_t;
148 #endif /* !_KERNEL && !_ACL_PRIVATE */
151 * Possible valid values for ats_brand field.
153 #define ACL_BRAND_UNKNOWN 0
154 #define ACL_BRAND_POSIX 1
155 #define ACL_BRAND_NFS4 2
158 * Possible valid values for ae_tag field. For explanation, see acl(9).
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
171 * Possible valid values for ae_entry_type field, valid only for NFSv4 ACLs.
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
179 * Possible valid values for acl_type_t arguments. First two
180 * are provided only for backwards binary compatibility.
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
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.
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)
200 * Possible bits in ae_perm field for NFSv4 ACLs.
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
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)
227 * Possible entry_id values for acl_get_entry(3).
229 #define ACL_FIRST_ENTRY 0
230 #define ACL_NEXT_ENTRY 1
233 * Possible values in ae_flags field; valid only for NFSv4 ACLs.
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
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)
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.
251 #define ACL_UNDEFINED_ID ((uid_t)-1)
254 * Possible values for _flags parameter in acl_to_text_np(3).
256 #define ACL_TEXT_VERBOSE 0x01
257 #define ACL_TEXT_NUMERIC_IDS 0x02
258 #define ACL_TEXT_APPEND_ID 0x04
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.
266 #define ACL_OVERRIDE_MASK (S_IRWXU | S_IRWXG | S_IRWXO)
267 #define ACL_PRESERVE_MASK (~ACL_OVERRIDE_MASK)
272 * Filesystem-independent code to move back and forth between POSIX mode and
273 * POSIX.1e ACL representations.
275 acl_perm_t acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode);
276 struct acl_entry acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid,
277 gid_t gid, mode_t mode);
278 mode_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);
282 mode_t acl_posix1e_acl_to_mode(struct acl *acl);
283 mode_t acl_posix1e_newfilemode(mode_t cmode,
285 struct acl *acl_alloc(int flags);
286 void acl_free(struct acl *aclp);
288 void acl_nfs4_sync_acl_from_mode(struct acl *aclp,
289 mode_t mode, int file_owner_id);
290 void acl_nfs4_sync_mode_from_acl(mode_t *mode,
291 const struct acl *aclp);
292 int acl_nfs4_is_trivial(const struct acl *aclp,
294 void 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);
298 int acl_copy_oldacl_into_acl(const struct oldacl *source,
300 int acl_copy_acl_into_oldacl(const struct acl *source,
301 struct oldacl *dest);
304 * To allocate 'struct acl', use acl_alloc()/acl_free() instead of this.
306 MALLOC_DECLARE(M_ACL);
308 * Filesystem-independent syntax check for a POSIX.1e ACL.
310 int acl_posix1e_check(struct acl *acl);
311 int acl_nfs4_check(const struct acl *aclp, int is_directory);
315 #if defined(_ACL_PRIVATE)
318 * Syscall interface -- use the library calls instead as the syscalls have
319 * strict ACL entry ordering requirements.
322 int __acl_aclcheck_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
323 int __acl_aclcheck_file(const char *_path, acl_type_t _type,
325 int __acl_aclcheck_link(const char *_path, acl_type_t _type,
327 int __acl_delete_fd(int _filedes, acl_type_t _type);
328 int __acl_delete_file(const char *_path_p, acl_type_t _type);
329 int __acl_delete_link(const char *_path_p, acl_type_t _type);
330 int __acl_get_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
331 int __acl_get_file(const char *_path, acl_type_t _type, struct acl *_aclp);
332 int __acl_get_link(const char *_path, acl_type_t _type, struct acl *_aclp);
333 int __acl_set_fd(int _filedes, acl_type_t _type, struct acl *_aclp);
334 int __acl_set_file(const char *_path, acl_type_t _type, struct acl *_aclp);
335 int __acl_set_link(const char *_path, acl_type_t _type, struct acl *_aclp);
338 #endif /* _ACL_PRIVATE */
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).
347 int acl_add_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
348 int acl_add_perm(acl_permset_t _permset_d, acl_perm_t _perm);
349 int acl_calc_mask(acl_t *_acl_p);
350 int acl_clear_flags_np(acl_flagset_t _flagset_d);
351 int acl_clear_perms(acl_permset_t _permset_d);
352 int acl_copy_entry(acl_entry_t _dest_d, acl_entry_t _src_d);
353 ssize_t acl_copy_ext(void *_buf_p, acl_t _acl, ssize_t _size);
354 acl_t acl_copy_int(const void *_buf_p);
355 int acl_create_entry(acl_t *_acl_p, acl_entry_t *_entry_p);
356 int acl_create_entry_np(acl_t *_acl_p, acl_entry_t *_entry_p, int _index);
357 int acl_delete_entry(acl_t _acl, acl_entry_t _entry_d);
358 int acl_delete_entry_np(acl_t _acl, int _index);
359 int acl_delete_fd_np(int _filedes, acl_type_t _type);
360 int acl_delete_file_np(const char *_path_p, acl_type_t _type);
361 int acl_delete_link_np(const char *_path_p, acl_type_t _type);
362 int acl_delete_def_file(const char *_path_p);
363 int acl_delete_def_link_np(const char *_path_p);
364 int acl_delete_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
365 int acl_delete_perm(acl_permset_t _permset_d, acl_perm_t _perm);
366 acl_t acl_dup(acl_t _acl);
367 int acl_free(void *_obj_p);
368 acl_t acl_from_text(const char *_buf_p);
369 int acl_get_brand_np(acl_t _acl, int *_brand_p);
370 int acl_get_entry(acl_t _acl, int _entry_id, acl_entry_t *_entry_p);
371 acl_t acl_get_fd(int _fd);
372 acl_t acl_get_fd_np(int fd, acl_type_t _type);
373 acl_t acl_get_file(const char *_path_p, acl_type_t _type);
374 int acl_get_entry_type_np(acl_entry_t _entry_d, acl_entry_type_t *_entry_type_p);
375 acl_t acl_get_link_np(const char *_path_p, acl_type_t _type);
376 void *acl_get_qualifier(acl_entry_t _entry_d);
377 int acl_get_flag_np(acl_flagset_t _flagset_d, acl_flag_t _flag);
378 int acl_get_perm_np(acl_permset_t _permset_d, acl_perm_t _perm);
379 int acl_get_flagset_np(acl_entry_t _entry_d, acl_flagset_t *_flagset_p);
380 int acl_get_permset(acl_entry_t _entry_d, acl_permset_t *_permset_p);
381 int acl_get_tag_type(acl_entry_t _entry_d, acl_tag_t *_tag_type_p);
382 acl_t acl_init(int _count);
383 int acl_set_fd(int _fd, acl_t _acl);
384 int acl_set_fd_np(int _fd, acl_t _acl, acl_type_t _type);
385 int acl_set_file(const char *_path_p, acl_type_t _type, acl_t _acl);
386 int acl_set_entry_type_np(acl_entry_t _entry_d, acl_entry_type_t _entry_type);
387 int acl_set_link_np(const char *_path_p, acl_type_t _type, acl_t _acl);
388 int acl_set_flagset_np(acl_entry_t _entry_d, acl_flagset_t _flagset_d);
389 int acl_set_permset(acl_entry_t _entry_d, acl_permset_t _permset_d);
390 int acl_set_qualifier(acl_entry_t _entry_d, const void *_tag_qualifier_p);
391 int acl_set_tag_type(acl_entry_t _entry_d, acl_tag_t _tag_type);
392 ssize_t acl_size(acl_t _acl);
393 char *acl_to_text(acl_t _acl, ssize_t *_len_p);
394 char *acl_to_text_np(acl_t _acl, ssize_t *_len_p, int _flags);
395 int acl_valid(acl_t _acl);
396 int acl_valid_fd_np(int _fd, acl_type_t _type, acl_t _acl);
397 int acl_valid_file_np(const char *_path_p, acl_type_t _type, acl_t _acl);
398 int acl_valid_link_np(const char *_path_p, acl_type_t _type, acl_t _acl);
399 int acl_is_trivial_np(const acl_t _acl, int *_trivialp);
400 acl_t acl_strip_np(const acl_t _acl, int recalculate_mask);
403 #endif /* !_KERNEL */
405 #endif /* !_SYS_ACL_H_ */