2 * Copyright (c) 2004 Apple Computer, 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>
32 #include <membership.h>
33 #include <membershipPriv.h>
37 #include <libkern/OSByteOrder.h>
42 * NOTE: the copy_int/copy_ext functions are duplicated here, one version of each for
43 * each of native and portable endianity. A more elegant solution might be called for
44 * if the functions become much more complicated.
48 * acl_t -> external representation, portable endianity
51 acl_copy_ext(void *buf
, acl_t acl
, ssize_t size
)
53 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
57 /* validate arguments, compute required size */
58 reqsize
= acl_size(acl
);
66 /* export the header */
67 ext
->fsec_magic
= OSSwapHostToBigInt32(KAUTH_FILESEC_MAGIC
);
68 ext
->fsec_entrycount
= OSSwapHostToBigInt32(acl
->a_entries
);
69 ext
->fsec_flags
= OSSwapHostToBigInt32(acl
->a_flags
);
72 for (i
= 0; i
< acl
->a_entries
; i
++) {
73 /* ACE contents are almost identical */
74 ext
->fsec_ace
[i
].ace_applicable
= acl
->a_ace
[i
].ae_applicable
;
75 ext
->fsec_ace
[i
].ace_flags
=
76 OSSwapHostToBigInt32((acl
->a_ace
[i
].ae_tag
& KAUTH_ACE_KINDMASK
) | (acl
->a_ace
[i
].ae_flags
& ~KAUTH_ACE_KINDMASK
));
77 ext
->fsec_ace
[i
].ace_rights
= OSSwapHostToBigInt32(acl
->a_ace
[i
].ae_perms
);
84 * acl_t -> external representation, native system endianity
87 acl_copy_ext_native(void *buf
, acl_t acl
, ssize_t size
)
89 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
93 /* validate arguments, compute required size */
94 reqsize
= acl_size(acl
);
102 /* export the header */
103 ext
->fsec_magic
= KAUTH_FILESEC_MAGIC
;
104 ext
->fsec_entrycount
= acl
->a_entries
;
105 ext
->fsec_flags
= acl
->a_flags
;
109 for (i
= 0; i
< acl
->a_entries
; i
++) {
110 /* ACE contents are almost identical */
111 ext
->fsec_ace
[i
].ace_applicable
= acl
->a_ace
[i
].ae_applicable
;
112 ext
->fsec_ace
[i
].ace_flags
=
113 (acl
->a_ace
[i
].ae_tag
& KAUTH_ACE_KINDMASK
) |
114 (acl
->a_ace
[i
].ae_flags
& ~KAUTH_ACE_KINDMASK
);
115 ext
->fsec_ace
[i
].ace_rights
= acl
->a_ace
[i
].ae_perms
;
122 * external representation, portable system endianity -> acl_t
124 * Unlike acl_copy_ext, we can't mung the buffer as it doesn't belong to us.
127 acl_copy_int(const void *buf
)
129 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
133 if (ext
->fsec_magic
!= OSSwapHostToBigInt32(KAUTH_FILESEC_MAGIC
)) {
138 if ((ap
= acl_init(OSSwapBigToHostInt32(ext
->fsec_entrycount
))) != NULL
) {
139 /* copy useful header fields */
140 ap
->a_flags
= OSSwapBigToHostInt32(ext
->fsec_flags
);
141 ap
->a_entries
= OSSwapBigToHostInt32(ext
->fsec_entrycount
);
143 for (i
= 0; i
< ap
->a_entries
; i
++) {
144 /* ACE contents are literally identical */
145 ap
->a_ace
[i
].ae_magic
= _ACL_ENTRY_MAGIC
;
146 ap
->a_ace
[i
].ae_applicable
= ext
->fsec_ace
[i
].ace_applicable
;
147 ap
->a_ace
[i
].ae_flags
= OSSwapBigToHostInt32(ext
->fsec_ace
[i
].ace_flags
) & ~KAUTH_ACE_KINDMASK
;
148 ap
->a_ace
[i
].ae_tag
= OSSwapBigToHostInt32(ext
->fsec_ace
[i
].ace_flags
) & KAUTH_ACE_KINDMASK
;
149 ap
->a_ace
[i
].ae_perms
= OSSwapBigToHostInt32(ext
->fsec_ace
[i
].ace_rights
);
156 * external representation, native system endianity -> acl_t
159 acl_copy_int_native(const void *buf
)
161 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
165 if (ext
->fsec_magic
!= KAUTH_FILESEC_MAGIC
) {
170 if ((ap
= acl_init(ext
->fsec_entrycount
)) != NULL
) {
171 /* copy useful header fields */
172 ap
->a_flags
= ext
->fsec_flags
;
173 ap
->a_entries
= ext
->fsec_entrycount
;
175 for (i
= 0; i
< ap
->a_entries
; i
++) {
176 /* ACE contents are literally identical */
177 ap
->a_ace
[i
].ae_magic
= _ACL_ENTRY_MAGIC
;
178 ap
->a_ace
[i
].ae_applicable
= ext
->fsec_ace
[i
].ace_applicable
;
179 ap
->a_ace
[i
].ae_flags
= ext
->fsec_ace
[i
].ace_flags
& ~KAUTH_ACE_KINDMASK
;
180 ap
->a_ace
[i
].ae_tag
= ext
->fsec_ace
[i
].ace_flags
& KAUTH_ACE_KINDMASK
;
181 ap
->a_ace
[i
].ae_perms
= ext
->fsec_ace
[i
].ace_rights
;
187 #define ACL_TYPE_DIR (1<<0)
188 #define ACL_TYPE_FILE (1<<1)
189 #define ACL_TYPE_ACL (1<<2)
196 {ACL_READ_DATA
, "read", ACL_TYPE_FILE
},
197 // {ACL_LIST_DIRECTORY, "list", ACL_TYPE_DIR},
198 {ACL_WRITE_DATA
, "write", ACL_TYPE_FILE
},
199 // {ACL_ADD_FILE, "add_file", ACL_TYPE_DIR},
200 {ACL_EXECUTE
, "execute", ACL_TYPE_FILE
},
201 // {ACL_SEARCH, "search", ACL_TYPE_DIR},
202 {ACL_DELETE
, "delete", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
203 {ACL_APPEND_DATA
, "append", ACL_TYPE_FILE
},
204 // {ACL_ADD_SUBDIRECTORY, "add_subdirectory", ACL_TYPE_DIR},
205 {ACL_DELETE_CHILD
, "delete_child", ACL_TYPE_DIR
},
206 {ACL_READ_ATTRIBUTES
, "readattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
207 {ACL_WRITE_ATTRIBUTES
, "writeattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
208 {ACL_READ_EXTATTRIBUTES
, "readextattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
209 {ACL_WRITE_EXTATTRIBUTES
, "writeextattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
210 {ACL_READ_SECURITY
, "readsecurity", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
211 {ACL_WRITE_SECURITY
, "writesecurity", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
212 {ACL_CHANGE_OWNER
, "chown", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
221 {ACL_ENTRY_INHERITED
, "inherited", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
222 {ACL_FLAG_DEFER_INHERIT
, "defer_inherit", ACL_TYPE_ACL
},
223 {ACL_ENTRY_FILE_INHERIT
, "file_inherit", ACL_TYPE_DIR
},
224 {ACL_ENTRY_DIRECTORY_INHERIT
, "directory_inherit", ACL_TYPE_DIR
},
225 {ACL_ENTRY_LIMIT_INHERIT
, "limit_inherit", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
226 {ACL_ENTRY_ONLY_INHERIT
, "only_inherit", ACL_TYPE_DIR
},
231 * reallocing snprintf with offset
235 raosnprintf(char **buf
, size_t *size
, ssize_t
*offset
, char *fmt
, ...)
245 ret
= vsnprintf(*buf
+ *offset
, *size
- *offset
, fmt
, ap
);
247 if (ret
< (*size
- *offset
))
253 *buf
= reallocf(*buf
, (*size
*= 2));
256 //warn("reallocf failure");
261 uuid_to_name(uuid_t
*uu
, uid_t
*id
, int *isgid
)
263 struct group
*tgrp
= NULL
;
264 struct passwd
*tpass
= NULL
;
266 if (0 == mbr_uuid_to_id(*uu
, id
, isgid
))
271 if (!(tpass
= getpwuid(*id
)))
273 return strdup(tpass
->pw_name
);
276 if (!(tgrp
= getgrgid((gid_t
) *id
)))
278 return strdup(tgrp
->gr_name
);
281 errout
: ; //warn("Unable to translate qualifier on ACL\n");
288 acl_from_text(const char *buf_p
)
290 int i
, error
= 0, need_tag
, ug_tag
;
291 char *buf
, *orig_buf
;
292 char *entry
, *field
, *sub
;
294 struct passwd
*tpass
= NULL
;
295 struct group
*tgrp
= NULL
;
296 acl_entry_t acl_entry
;
297 acl_flagset_t flags
= NULL
;
298 acl_permset_t perms
= NULL
;
308 if ((buf
= strdup(buf_p
)) == NULL
)
311 if ((acl_ret
= acl_init(1)) == NULL
)
317 * format: !#acl <version> [<flags>]
319 if ((entry
= strsep(&buf
, "\n")) != NULL
&& *entry
)
322 field
= strsep(&entry
, " ");
323 if (*field
&& strncmp(field
, "!#acl", strlen("!#acl")))
329 /* field 2: <version>
330 * currently only accepts 1
332 field
= strsep(&entry
, " ");
334 if (!*field
|| strtol(field
, NULL
, 0) != 1)
343 if((field
= strsep(&entry
, " ")) != NULL
&& *field
)
345 acl_get_flagset_np(acl_ret
, &flags
);
346 while ((sub
= strsep(&field
, ",")) && *sub
)
348 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
350 if (acl_flags
[i
].type
& ACL_TYPE_ACL
351 && !strcmp(acl_flags
[i
].name
, sub
))
353 acl_add_flag_np(flags
, acl_flags
[i
].flag
);
357 if (acl_flags
[i
].name
== NULL
)
359 /* couldn't find flag */
370 /* parse each acl line
371 * format: <user|group>:
375 * <allow|deny>[,<flags>]
376 * [:<permissions>[,<permissions>]]
378 * only one of the user/group identifies is required
379 * the first one found is used
381 while ((entry
= strsep(&buf
, "\n")) && *entry
)
386 /* field 1: <user|group> */
387 field
= strsep(&entry
, ":");
390 bzero(uu
, sizeof(uuid_t
));
391 else if((uu
= calloc(1, sizeof(uuid_t
))) == NULL
)
397 if(acl_create_entry(&acl_ret
, &acl_entry
))
403 if (-1 == acl_get_flagset_np(acl_entry
, &flags
)
404 || -1 == acl_get_permset(acl_entry
, &perms
))
413 if(!strcmp(field
, "user"))
414 ug_tag
= ID_TYPE_UID
;
417 if(!strcmp(field
, "group"))
418 ug_tag
= ID_TYPE_GID
;
425 /* field 2: <uuid> */
426 if ((field
= strsep(&entry
, ":")) != NULL
&& *field
)
428 mbr_string_to_uuid(field
, *uu
);
432 /* field 3: <username|groupname> */
433 if ((field
= strsep(&entry
, ":")) != NULL
&& *field
&& need_tag
)
438 if((tpass
= getpwnam(field
)) != NULL
)
439 if (mbr_uid_to_uuid(tpass
->pw_uid
, *uu
) != 0)
446 if ((tgrp
= getgrnam(field
)) != NULL
)
447 if (mbr_gid_to_uuid(tgrp
->gr_gid
, *uu
) != 0)
460 /* field 4: <uid|gid> */
461 if ((field
= strsep(&entry
, ":")) != NULL
&& *field
&& need_tag
)
466 if((id
= strtol(field
, NULL
, 10)) == 0 && error
)
475 if((tpass
= getpwuid((uid_t
)id
)) != NULL
)
476 if (mbr_uid_to_uuid(tpass
->pw_uid
, *uu
) != 0)
483 if ((tgrp
= getgrgid((gid_t
)id
)) != NULL
)
484 if (mbr_gid_to_uuid(tgrp
->gr_gid
, *uu
) != 0)
494 /* sanity check: nothing set as qualifier */
501 /* field 5: <flags> */
502 if((field
= strsep(&entry
, ":")) == NULL
|| !*field
)
508 for (tag
= 0; (sub
= strsep(&field
, ",")) && *sub
;)
512 if (!strcmp(sub
, "allow"))
513 tag
= ACL_EXTENDED_ALLOW
;
514 else if (!strcmp(sub
, "deny"))
515 tag
= ACL_EXTENDED_DENY
;
523 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
525 if (acl_flags
[i
].type
& (ACL_TYPE_FILE
| ACL_TYPE_DIR
)
526 && !strcmp(acl_flags
[i
].name
, sub
))
528 acl_add_flag_np(flags
, acl_flags
[i
].flag
);
532 if (acl_flags
[i
].name
== NULL
)
534 /* couldn't find perm */
540 /* field 6: <perms> (can be empty) */
541 if((field
= strsep(&entry
, ":")) != NULL
&& *field
)
543 while ((sub
= strsep(&field
, ",")) && *sub
)
545 for (i
= 0; acl_perms
[i
].name
!= NULL
; i
++)
547 if (acl_perms
[i
].type
& (ACL_TYPE_FILE
| ACL_TYPE_DIR
)
548 && !strcmp(acl_perms
[i
].name
, sub
))
550 acl_add_perm(perms
, acl_perms
[i
].perm
);
554 if (acl_perms
[i
].name
== NULL
)
556 /* couldn't find perm */
562 acl_set_tag_type(acl_entry
, tag
);
563 acl_set_qualifier(acl_entry
, *uu
);
579 acl_to_text(acl_t acl
, ssize_t
*len_p
)
582 acl_entry_t entry
= NULL
;
588 size_t bufsize
= 1024;
591 if (!_ACL_VALID_ACL(acl
)) {
597 if ((len_p
= alloca(sizeof(ssize_t
))) == NULL
)
602 if ((buf
= malloc(bufsize
)) == NULL
)
605 if (!raosnprintf(&buf
, &bufsize
, len_p
, "!#acl %d", 1))
608 if (acl_get_flagset_np(acl
, &flags
) == 0)
610 for (i
= 0, first
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
612 if (acl_flags
[i
].type
& ACL_TYPE_ACL
613 && acl_get_flag_np(flags
, acl_flags
[i
].flag
) != 0)
615 if(!raosnprintf(&buf
, &bufsize
, len_p
, "%s%s",
616 first
++ ? "," : " ", acl_flags
[i
].name
))
621 for (;acl_get_entry(acl
,
622 entry
== NULL
? ACL_FIRST_ENTRY
: ACL_NEXT_ENTRY
, &entry
) == 0;)
626 char *str
, uu_str
[37];
628 if (((uu
= (uuid_t
*) acl_get_qualifier(entry
)) == NULL
)
629 || (acl_get_tag_type(entry
, &tag
) != 0)
630 || (acl_get_flagset_np(entry
, &flags
) != 0)
631 || (acl_get_permset(entry
, &perms
) != 0)
632 || ((str
= uuid_to_name(uu
, &id
, &isgid
)) == NULL
)) {
633 if (uu
!= NULL
) acl_free(uu
);
637 uuid_unparse_upper(*uu
, uu_str
);
639 valid
= raosnprintf(&buf
, &bufsize
, len_p
, "\n%s:%s:%s:%d:%s",
640 isgid
? "group" : "user",
644 (tag
== ACL_EXTENDED_ALLOW
) ? "allow" : "deny");
652 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
654 if (acl_flags
[i
].type
& (ACL_TYPE_DIR
| ACL_TYPE_FILE
))
656 if(acl_get_flag_np(flags
, acl_flags
[i
].flag
) != 0)
658 if(!raosnprintf(&buf
, &bufsize
, len_p
, ",%s",
665 for (i
= 0, first
= 0; acl_perms
[i
].name
!= NULL
; ++i
)
667 if (acl_perms
[i
].type
& (ACL_TYPE_DIR
| ACL_TYPE_FILE
))
669 if(acl_get_perm_np(perms
, acl_perms
[i
].perm
) != 0)
671 if(!raosnprintf(&buf
, &bufsize
, len_p
, "%s%s",
672 first
++ ? "," : ":", acl_perms
[i
].name
))
678 buf
[(*len_p
)++] = '\n';
693 _ACL_VALIDATE_ACL(acl
);
695 return(KAUTH_FILESEC_SIZE(acl
->a_entries
));