2 * Copyright (c) 2004, 2010, 2011 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/appleapiopts.h>
25 #include <sys/types.h>
27 #include <sys/fcntl.h>
34 #include <membership.h>
35 #include <membershipPriv.h>
39 #include <libkern/OSByteOrder.h>
44 * NOTE: the copy_int/copy_ext functions are duplicated here, one version of each for
45 * each of native and portable endianity. A more elegant solution might be called for
46 * if the functions become much more complicated.
50 * acl_t -> external representation, portable endianity
53 acl_copy_ext(void *buf
, acl_t acl
, ssize_t size
)
55 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
59 /* validate arguments, compute required size */
60 reqsize
= acl_size(acl
);
69 ext
->fsec_magic
= OSSwapHostToBigInt32(KAUTH_FILESEC_MAGIC
);
71 /* special case for _FILESEC_REMOVE_ACL */
72 if (acl
== (acl_t
)_FILESEC_REMOVE_ACL
) {
73 ext
->fsec_entrycount
= OSSwapHostToBigInt32(KAUTH_FILESEC_NOACL
);
77 /* export the header */
78 ext
->fsec_entrycount
= OSSwapHostToBigInt32(acl
->a_entries
);
79 ext
->fsec_flags
= OSSwapHostToBigInt32(acl
->a_flags
);
82 for (i
= 0; i
< acl
->a_entries
; i
++) {
83 /* ACE contents are almost identical */
84 ext
->fsec_ace
[i
].ace_applicable
= acl
->a_ace
[i
].ae_applicable
;
85 ext
->fsec_ace
[i
].ace_flags
=
86 OSSwapHostToBigInt32((acl
->a_ace
[i
].ae_tag
& KAUTH_ACE_KINDMASK
) | (acl
->a_ace
[i
].ae_flags
& ~KAUTH_ACE_KINDMASK
));
87 ext
->fsec_ace
[i
].ace_rights
= OSSwapHostToBigInt32(acl
->a_ace
[i
].ae_perms
);
94 * acl_t -> external representation, native system endianity
97 acl_copy_ext_native(void *buf
, acl_t acl
, ssize_t size
)
99 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
103 /* validate arguments, compute required size */
104 reqsize
= acl_size(acl
);
107 if (reqsize
> size
) {
113 ext
->fsec_magic
= KAUTH_FILESEC_MAGIC
;
115 /* special case for _FILESEC_REMOVE_ACL */
116 if (acl
== (acl_t
)_FILESEC_REMOVE_ACL
) {
117 ext
->fsec_entrycount
= KAUTH_FILESEC_NOACL
;
121 /* export the header */
122 ext
->fsec_entrycount
= acl
->a_entries
;
123 ext
->fsec_flags
= acl
->a_flags
;
126 for (i
= 0; i
< acl
->a_entries
; i
++) {
127 /* ACE contents are almost identical */
128 ext
->fsec_ace
[i
].ace_applicable
= acl
->a_ace
[i
].ae_applicable
;
129 ext
->fsec_ace
[i
].ace_flags
=
130 (acl
->a_ace
[i
].ae_tag
& KAUTH_ACE_KINDMASK
) |
131 (acl
->a_ace
[i
].ae_flags
& ~KAUTH_ACE_KINDMASK
);
132 ext
->fsec_ace
[i
].ace_rights
= acl
->a_ace
[i
].ae_perms
;
139 * external representation, portable system endianity -> acl_t
141 * Unlike acl_copy_ext, we can't mung the buffer as it doesn't belong to us.
144 acl_copy_int(const void *buf
)
146 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
150 if (ext
->fsec_magic
!= OSSwapHostToBigInt32(KAUTH_FILESEC_MAGIC
)) {
155 if ((ap
= acl_init(OSSwapBigToHostInt32(ext
->fsec_entrycount
))) != NULL
) {
156 /* copy useful header fields */
157 ap
->a_flags
= OSSwapBigToHostInt32(ext
->fsec_flags
);
158 ap
->a_entries
= OSSwapBigToHostInt32(ext
->fsec_entrycount
);
160 for (i
= 0; i
< ap
->a_entries
; i
++) {
161 /* ACE contents are literally identical */
162 ap
->a_ace
[i
].ae_magic
= _ACL_ENTRY_MAGIC
;
163 ap
->a_ace
[i
].ae_applicable
= ext
->fsec_ace
[i
].ace_applicable
;
164 ap
->a_ace
[i
].ae_flags
= OSSwapBigToHostInt32(ext
->fsec_ace
[i
].ace_flags
) & ~KAUTH_ACE_KINDMASK
;
165 ap
->a_ace
[i
].ae_tag
= OSSwapBigToHostInt32(ext
->fsec_ace
[i
].ace_flags
) & KAUTH_ACE_KINDMASK
;
166 ap
->a_ace
[i
].ae_perms
= OSSwapBigToHostInt32(ext
->fsec_ace
[i
].ace_rights
);
173 * external representation, native system endianity -> acl_t
176 acl_copy_int_native(const void *buf
)
178 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
182 if (ext
->fsec_magic
!= KAUTH_FILESEC_MAGIC
) {
187 if ((ap
= acl_init(ext
->fsec_entrycount
)) != NULL
) {
188 /* copy useful header fields */
189 ap
->a_flags
= ext
->fsec_flags
;
190 ap
->a_entries
= ext
->fsec_entrycount
;
192 for (i
= 0; i
< ap
->a_entries
; i
++) {
193 /* ACE contents are literally identical */
194 ap
->a_ace
[i
].ae_magic
= _ACL_ENTRY_MAGIC
;
195 ap
->a_ace
[i
].ae_applicable
= ext
->fsec_ace
[i
].ace_applicable
;
196 ap
->a_ace
[i
].ae_flags
= ext
->fsec_ace
[i
].ace_flags
& ~KAUTH_ACE_KINDMASK
;
197 ap
->a_ace
[i
].ae_tag
= ext
->fsec_ace
[i
].ace_flags
& KAUTH_ACE_KINDMASK
;
198 ap
->a_ace
[i
].ae_perms
= ext
->fsec_ace
[i
].ace_rights
;
204 #define ACL_TYPE_DIR (1<<0)
205 #define ACL_TYPE_FILE (1<<1)
206 #define ACL_TYPE_ACL (1<<2)
213 {ACL_READ_DATA
, "read", ACL_TYPE_FILE
},
214 // {ACL_LIST_DIRECTORY, "list", ACL_TYPE_DIR},
215 {ACL_WRITE_DATA
, "write", ACL_TYPE_FILE
},
216 // {ACL_ADD_FILE, "add_file", ACL_TYPE_DIR},
217 {ACL_EXECUTE
, "execute", ACL_TYPE_FILE
},
218 // {ACL_SEARCH, "search", ACL_TYPE_DIR},
219 {ACL_DELETE
, "delete", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
220 {ACL_APPEND_DATA
, "append", ACL_TYPE_FILE
},
221 // {ACL_ADD_SUBDIRECTORY, "add_subdirectory", ACL_TYPE_DIR},
222 {ACL_DELETE_CHILD
, "delete_child", ACL_TYPE_DIR
},
223 {ACL_READ_ATTRIBUTES
, "readattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
224 {ACL_WRITE_ATTRIBUTES
, "writeattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
225 {ACL_READ_EXTATTRIBUTES
, "readextattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
226 {ACL_WRITE_EXTATTRIBUTES
, "writeextattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
227 {ACL_READ_SECURITY
, "readsecurity", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
228 {ACL_WRITE_SECURITY
, "writesecurity", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
229 {ACL_CHANGE_OWNER
, "chown", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
230 {ACL_SYNCHRONIZE
, "synchronize", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
239 {ACL_ENTRY_INHERITED
, "inherited", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
240 {ACL_FLAG_DEFER_INHERIT
, "defer_inherit", ACL_TYPE_ACL
},
241 {ACL_ENTRY_FILE_INHERIT
, "file_inherit", ACL_TYPE_DIR
},
242 {ACL_ENTRY_DIRECTORY_INHERIT
, "directory_inherit", ACL_TYPE_DIR
},
243 {ACL_ENTRY_LIMIT_INHERIT
, "limit_inherit", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
244 {ACL_ENTRY_ONLY_INHERIT
, "only_inherit", ACL_TYPE_DIR
},
245 {ACL_FLAG_NO_INHERIT
, "no_inherit", ACL_TYPE_ACL
},
250 * reallocing snprintf with offset
254 raosnprintf(char **buf
, size_t *size
, ssize_t
*offset
, char *fmt
, ...)
264 ret
= vsnprintf(*buf
+ *offset
, *size
- *offset
, fmt
, ap
);
266 if (ret
< (*size
- *offset
))
272 *buf
= reallocf(*buf
, (*size
*= 2));
275 //warn("reallocf failure");
280 uuid_to_name(uuid_t
*uu
, uid_t
*id
, int *isgid
)
282 struct group
*tgrp
= NULL
;
283 struct passwd
*tpass
= NULL
;
285 if (0 == mbr_uuid_to_id(*uu
, id
, isgid
))
290 if (!(tpass
= getpwuid(*id
)))
292 return strdup(tpass
->pw_name
);
295 if (!(tgrp
= getgrgid((gid_t
) *id
)))
297 return strdup(tgrp
->gr_name
);
300 errout
: ; //warn("Unable to translate qualifier on ACL\n");
307 acl_from_text(const char *buf_p
)
309 int i
, error
= 0, need_tag
, ug_tag
;
310 char *buf
, *orig_buf
;
311 char *entry
, *field
, *sub
;
313 struct passwd
*tpass
= NULL
;
314 struct group
*tgrp
= NULL
;
315 acl_entry_t acl_entry
;
316 acl_flagset_t flags
= NULL
;
317 acl_permset_t perms
= NULL
;
327 if ((buf
= strdup(buf_p
)) == NULL
)
330 if ((acl_ret
= acl_init(1)) == NULL
)
336 * format: !#acl <version> [<flags>]
338 if ((entry
= strsep(&buf
, "\n")) != NULL
&& *entry
)
341 field
= strsep(&entry
, " ");
342 if (*field
&& strncmp(field
, "!#acl", strlen("!#acl")))
348 /* field 2: <version>
349 * currently only accepts 1
351 field
= strsep(&entry
, " ");
353 if (!*field
|| strtol(field
, NULL
, 0) != 1)
362 if((field
= strsep(&entry
, " ")) != NULL
&& *field
)
364 acl_get_flagset_np(acl_ret
, &flags
);
365 while ((sub
= strsep(&field
, ",")) && *sub
)
367 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
369 if (acl_flags
[i
].type
& ACL_TYPE_ACL
370 && !strcmp(acl_flags
[i
].name
, sub
))
372 acl_add_flag_np(flags
, acl_flags
[i
].flag
);
376 if (acl_flags
[i
].name
== NULL
)
378 /* couldn't find flag */
389 /* parse each acl line
390 * format: <user|group>:
394 * <allow|deny>[,<flags>]
395 * [:<permissions>[,<permissions>]]
397 * only one of the user/group identifies is required
398 * the first one found is used
400 while ((entry
= strsep(&buf
, "\n")) && *entry
)
405 /* field 1: <user|group> */
406 field
= strsep(&entry
, ":");
409 bzero(uu
, sizeof(uuid_t
));
410 else if((uu
= calloc(1, sizeof(uuid_t
))) == NULL
)
416 if(acl_create_entry(&acl_ret
, &acl_entry
))
422 if (-1 == acl_get_flagset_np(acl_entry
, &flags
)
423 || -1 == acl_get_permset(acl_entry
, &perms
))
432 if(!strcmp(field
, "user"))
433 ug_tag
= ID_TYPE_UID
;
436 if(!strcmp(field
, "group"))
437 ug_tag
= ID_TYPE_GID
;
444 /* field 2: <uuid> */
445 if ((field
= strsep(&entry
, ":")) != NULL
&& *field
)
447 uuid_parse(field
, *uu
);
451 /* field 3: <username|groupname> */
452 if ((field
= strsep(&entry
, ":")) != NULL
&& *field
&& need_tag
)
457 if((tpass
= getpwnam(field
)) != NULL
)
458 if (mbr_uid_to_uuid(tpass
->pw_uid
, *uu
) != 0)
465 if ((tgrp
= getgrnam(field
)) != NULL
)
466 if (mbr_gid_to_uuid(tgrp
->gr_gid
, *uu
) != 0)
479 /* field 4: <uid|gid> */
480 if ((field
= strsep(&entry
, ":")) != NULL
&& *field
&& need_tag
)
485 if((id
= strtol(field
, NULL
, 10)) == 0 && error
)
494 if((tpass
= getpwuid((uid_t
)id
)) != NULL
)
495 if (mbr_uid_to_uuid(tpass
->pw_uid
, *uu
) != 0)
502 if ((tgrp
= getgrgid((gid_t
)id
)) != NULL
)
503 if (mbr_gid_to_uuid(tgrp
->gr_gid
, *uu
) != 0)
513 /* sanity check: nothing set as qualifier */
520 /* field 5: <flags> */
521 if((field
= strsep(&entry
, ":")) == NULL
|| !*field
)
527 for (tag
= 0; (sub
= strsep(&field
, ",")) && *sub
;)
531 if (!strcmp(sub
, "allow"))
532 tag
= ACL_EXTENDED_ALLOW
;
533 else if (!strcmp(sub
, "deny"))
534 tag
= ACL_EXTENDED_DENY
;
542 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
544 if (acl_flags
[i
].type
& (ACL_TYPE_FILE
| ACL_TYPE_DIR
)
545 && !strcmp(acl_flags
[i
].name
, sub
))
547 acl_add_flag_np(flags
, acl_flags
[i
].flag
);
551 if (acl_flags
[i
].name
== NULL
)
553 /* couldn't find perm */
559 /* field 6: <perms> (can be empty) */
560 if((field
= strsep(&entry
, ":")) != NULL
&& *field
)
562 while ((sub
= strsep(&field
, ",")) && *sub
)
564 for (i
= 0; acl_perms
[i
].name
!= NULL
; i
++)
566 if (acl_perms
[i
].type
& (ACL_TYPE_FILE
| ACL_TYPE_DIR
)
567 && !strcmp(acl_perms
[i
].name
, sub
))
569 acl_add_perm(perms
, acl_perms
[i
].perm
);
573 if (acl_perms
[i
].name
== NULL
)
575 /* couldn't find perm */
581 acl_set_tag_type(acl_entry
, tag
);
582 acl_set_qualifier(acl_entry
, *uu
);
598 acl_to_text(acl_t acl
, ssize_t
*len_p
)
601 acl_entry_t entry
= NULL
;
607 size_t bufsize
= 1024;
610 if (!_ACL_VALID_ACL(acl
)) {
616 if ((len_p
= alloca(sizeof(ssize_t
))) == NULL
)
621 if ((buf
= malloc(bufsize
)) == NULL
)
624 if (!raosnprintf(&buf
, &bufsize
, len_p
, "!#acl %d", 1))
627 if (acl_get_flagset_np(acl
, &flags
) == 0)
629 for (i
= 0, first
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
631 if (acl_flags
[i
].type
& ACL_TYPE_ACL
632 && acl_get_flag_np(flags
, acl_flags
[i
].flag
) != 0)
634 if(!raosnprintf(&buf
, &bufsize
, len_p
, "%s%s",
635 first
++ ? "," : " ", acl_flags
[i
].name
))
640 for (;acl_get_entry(acl
,
641 entry
== NULL
? ACL_FIRST_ENTRY
: ACL_NEXT_ENTRY
, &entry
) == 0;)
645 char *str
, uu_str
[37];
647 if (((uu
= (uuid_t
*) acl_get_qualifier(entry
)) == NULL
)
648 || (acl_get_tag_type(entry
, &tag
) != 0)
649 || (acl_get_flagset_np(entry
, &flags
) != 0)
650 || (acl_get_permset(entry
, &perms
) != 0)) {
651 if (uu
!= NULL
) acl_free(uu
);
655 uuid_unparse_upper(*uu
, uu_str
);
657 if ((str
= uuid_to_name(uu
, &id
, &isgid
)) != NULL
) {
658 valid
= raosnprintf(&buf
, &bufsize
, len_p
, "\n%s:%s:%s:%d:%s",
659 isgid
? "group" : "user",
663 (tag
== ACL_EXTENDED_ALLOW
) ? "allow" : "deny");
665 valid
= raosnprintf(&buf
, &bufsize
, len_p
, "\nuser:%s:::%s",
667 (tag
== ACL_EXTENDED_ALLOW
) ? "allow" : "deny");
676 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
678 if (acl_flags
[i
].type
& (ACL_TYPE_DIR
| ACL_TYPE_FILE
))
680 if(acl_get_flag_np(flags
, acl_flags
[i
].flag
) != 0)
682 if(!raosnprintf(&buf
, &bufsize
, len_p
, ",%s",
689 for (i
= 0, first
= 0; acl_perms
[i
].name
!= NULL
; ++i
)
691 if (acl_perms
[i
].type
& (ACL_TYPE_DIR
| ACL_TYPE_FILE
))
693 if(acl_get_perm_np(perms
, acl_perms
[i
].perm
) != 0)
695 if(!raosnprintf(&buf
, &bufsize
, len_p
, "%s%s",
696 first
++ ? "," : ":", acl_perms
[i
].name
))
702 buf
[(*len_p
)++] = '\n';
717 /* special case for _FILESEC_REMOVE_ACL */
718 if (acl
== (acl_t
)_FILESEC_REMOVE_ACL
)
719 return KAUTH_FILESEC_SIZE(0);
721 _ACL_VALIDATE_ACL(acl
);
723 return(KAUTH_FILESEC_SIZE(acl
->a_entries
));