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>
36 #include <libkern/OSByteOrder.h>
41 * NOTE: the copy_int/copy_ext functions are duplicated here, one version of each for
42 * each of native and portable endianity. A more elegant solution might be called for
43 * if the functions become much more complicated.
47 * acl_t -> external representation, portable endianity
50 acl_copy_ext(void *buf
, acl_t acl
, ssize_t size
)
52 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
56 /* validate arguments, compute required size */
57 reqsize
= acl_size(acl
);
65 /* export the header */
66 ext
->fsec_magic
= OSSwapHostToBigInt32(KAUTH_FILESEC_MAGIC
);
67 ext
->fsec_entrycount
= OSSwapHostToBigInt32(acl
->a_entries
);
68 ext
->fsec_flags
= OSSwapHostToBigInt32(acl
->a_flags
);
71 for (i
= 0; i
< acl
->a_entries
; i
++) {
72 /* ACE contents are almost identical */
73 ext
->fsec_ace
[i
].ace_applicable
= acl
->a_ace
[i
].ae_applicable
;
74 ext
->fsec_ace
[i
].ace_flags
=
75 OSSwapHostToBigInt32((acl
->a_ace
[i
].ae_tag
& KAUTH_ACE_KINDMASK
) | (acl
->a_ace
[i
].ae_flags
& ~KAUTH_ACE_KINDMASK
));
76 ext
->fsec_ace
[i
].ace_rights
= OSSwapHostToBigInt32(acl
->a_ace
[i
].ae_perms
);
83 * acl_t -> external representation, native system endianity
86 acl_copy_ext_native(void *buf
, acl_t acl
, ssize_t size
)
88 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
92 /* validate arguments, compute required size */
93 reqsize
= acl_size(acl
);
101 /* export the header */
102 ext
->fsec_magic
= KAUTH_FILESEC_MAGIC
;
103 ext
->fsec_entrycount
= acl
->a_entries
;
104 ext
->fsec_flags
= acl
->a_flags
;
108 for (i
= 0; i
< acl
->a_entries
; i
++) {
109 /* ACE contents are almost identical */
110 ext
->fsec_ace
[i
].ace_applicable
= acl
->a_ace
[i
].ae_applicable
;
111 ext
->fsec_ace
[i
].ace_flags
=
112 (acl
->a_ace
[i
].ae_tag
& KAUTH_ACE_KINDMASK
) |
113 (acl
->a_ace
[i
].ae_flags
& ~KAUTH_ACE_KINDMASK
);
114 ext
->fsec_ace
[i
].ace_rights
= acl
->a_ace
[i
].ae_perms
;
121 * external representation, portable system endianity -> acl_t
123 * Unlike acl_copy_ext, we can't mung the buffer as it doesn't belong to us.
126 acl_copy_int(const void *buf
)
128 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
132 if (ext
->fsec_magic
!= OSSwapHostToBigInt32(KAUTH_FILESEC_MAGIC
)) {
137 if ((ap
= acl_init(OSSwapBigToHostInt32(ext
->fsec_entrycount
))) != NULL
) {
138 /* copy useful header fields */
139 ap
->a_flags
= OSSwapBigToHostInt32(ext
->fsec_flags
);
140 ap
->a_entries
= OSSwapBigToHostInt32(ext
->fsec_entrycount
);
142 for (i
= 0; i
< ap
->a_entries
; i
++) {
143 /* ACE contents are literally identical */
144 ap
->a_ace
[i
].ae_magic
= _ACL_ENTRY_MAGIC
;
145 ap
->a_ace
[i
].ae_applicable
= ext
->fsec_ace
[i
].ace_applicable
;
146 ap
->a_ace
[i
].ae_flags
= OSSwapBigToHostInt32(ext
->fsec_ace
[i
].ace_flags
) & ~KAUTH_ACE_KINDMASK
;
147 ap
->a_ace
[i
].ae_tag
= OSSwapBigToHostInt32(ext
->fsec_ace
[i
].ace_flags
) & KAUTH_ACE_KINDMASK
;
148 ap
->a_ace
[i
].ae_perms
= OSSwapBigToHostInt32(ext
->fsec_ace
[i
].ace_rights
);
155 * external representation, native system endianity -> acl_t
158 acl_copy_int_native(const void *buf
)
160 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
164 if (ext
->fsec_magic
!= KAUTH_FILESEC_MAGIC
) {
169 if ((ap
= acl_init(ext
->fsec_entrycount
)) != NULL
) {
170 /* copy useful header fields */
171 ap
->a_flags
= ext
->fsec_flags
;
172 ap
->a_entries
= ext
->fsec_entrycount
;
174 for (i
= 0; i
< ap
->a_entries
; i
++) {
175 /* ACE contents are literally identical */
176 ap
->a_ace
[i
].ae_magic
= _ACL_ENTRY_MAGIC
;
177 ap
->a_ace
[i
].ae_applicable
= ext
->fsec_ace
[i
].ace_applicable
;
178 ap
->a_ace
[i
].ae_flags
= ext
->fsec_ace
[i
].ace_flags
& ~KAUTH_ACE_KINDMASK
;
179 ap
->a_ace
[i
].ae_tag
= ext
->fsec_ace
[i
].ace_flags
& KAUTH_ACE_KINDMASK
;
180 ap
->a_ace
[i
].ae_perms
= ext
->fsec_ace
[i
].ace_rights
;
186 #define ACL_TYPE_DIR (1<<0)
187 #define ACL_TYPE_FILE (1<<1)
188 #define ACL_TYPE_ACL (1<<2)
195 {ACL_READ_DATA
, "read", ACL_TYPE_FILE
},
196 // {ACL_LIST_DIRECTORY, "list", ACL_TYPE_DIR},
197 {ACL_WRITE_DATA
, "write", ACL_TYPE_FILE
},
198 // {ACL_ADD_FILE, "add_file", ACL_TYPE_DIR},
199 {ACL_EXECUTE
, "execute", ACL_TYPE_FILE
},
200 // {ACL_SEARCH, "search", ACL_TYPE_DIR},
201 {ACL_DELETE
, "delete", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
202 {ACL_APPEND_DATA
, "append", ACL_TYPE_FILE
},
203 // {ACL_ADD_SUBDIRECTORY, "add_subdirectory", ACL_TYPE_DIR},
204 {ACL_DELETE_CHILD
, "delete_child", ACL_TYPE_DIR
},
205 {ACL_READ_ATTRIBUTES
, "readattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
206 {ACL_WRITE_ATTRIBUTES
, "writeattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
207 {ACL_READ_EXTATTRIBUTES
, "readextattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
208 {ACL_WRITE_EXTATTRIBUTES
, "writeextattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
209 {ACL_READ_SECURITY
, "readsecurity", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
210 {ACL_WRITE_SECURITY
, "writesecurity", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
211 {ACL_CHANGE_OWNER
, "chown", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
220 {ACL_ENTRY_INHERITED
, "inherited", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
221 {ACL_FLAG_DEFER_INHERIT
, "defer_inherit", ACL_TYPE_ACL
},
222 {ACL_ENTRY_FILE_INHERIT
, "file_inherit", ACL_TYPE_DIR
},
223 {ACL_ENTRY_DIRECTORY_INHERIT
, "directory_inherit", ACL_TYPE_DIR
},
224 {ACL_ENTRY_LIMIT_INHERIT
, "limit_inherit", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
225 {ACL_ENTRY_ONLY_INHERIT
, "only_inherit", ACL_TYPE_DIR
},
230 * reallocing snprintf with offset
234 raosnprintf(char **buf
, size_t *size
, ssize_t
*offset
, char *fmt
, ...)
244 ret
= vsnprintf(*buf
+ *offset
, *size
- *offset
, fmt
, ap
);
246 if (ret
< (*size
- *offset
))
252 *buf
= reallocf(*buf
, (*size
*= 2));
255 //warn("reallocf failure");
260 uuid_to_name(uuid_t
*uu
, uid_t
*id
, int *isgid
)
262 struct group
*tgrp
= NULL
;
263 struct passwd
*tpass
= NULL
;
265 if (0 == mbr_uuid_to_id(*uu
, id
, isgid
))
270 if (!(tpass
= getpwuid(*id
)))
272 return strdup(tpass
->pw_name
);
275 if (!(tgrp
= getgrgid((gid_t
) *id
)))
277 return strdup(tgrp
->gr_name
);
280 errout
: ; //warn("Unable to translate qualifier on ACL\n");
287 acl_from_text(const char *buf_p
)
289 int i
, error
= 0, need_tag
= 1, ug_tag
= -1;
291 char *entry
, *field
, *sub
,
292 *last_field
, *last_entry
, *last_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
;
302 if ((acl_ret
= acl_init(1)) == NULL
)
308 if ((buf
= strdup(buf_p
)) == NULL
)
312 if ((entry
= strtok_r(buf
, "\n", &last_entry
)) != NULL
)
315 field
= strtok_r(entry
, " ", &last_field
);
316 if (field
&& strncmp(field
, "!#acl", strlen("!#acl")))
323 field
= strtok_r(NULL
, " ", &last_field
);
325 if (field
== NULL
|| strtol(field
, NULL
, 0) != 1)
332 if((field
= strtok_r(NULL
, " ", &last_field
)) != NULL
)
334 acl_get_flagset_np(acl_ret
, &flags
);
335 for (sub
= strtok_r(field
, ",", &last_sub
); sub
;
336 sub
= strtok_r(NULL
, ",", &last_sub
))
338 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
340 if (acl_flags
[i
].type
& ACL_TYPE_ACL
341 && !strcmp(acl_flags
[i
].name
, sub
))
343 acl_add_flag_np(flags
, acl_flags
[i
].flag
);
347 if (acl_flags
[i
].name
== NULL
)
349 /* couldn't find flag */
357 for (entry
= strtok_r(NULL
, "\n", &last_entry
); entry
;
358 entry
= strtok_r(NULL
, "\n", &last_entry
))
360 field
= strtok_r(entry
, ":", &last_field
);
362 if((uu
= calloc(1, sizeof(uuid_t
))) == NULL
)
365 if(acl_create_entry(&acl_ret
, &acl_entry
))
368 acl_get_flagset_np(acl_entry
, &flags
);
369 acl_get_permset(acl_entry
, &perms
);
374 if(!strncmp(buf
, "user", strlen(field
)))
375 ug_tag
= ID_TYPE_UID
;
378 if(!strncmp(buf
, "group", strlen(field
)))
379 ug_tag
= ID_TYPE_GID
;
385 if ((field
= strtok_r(NULL
, ":", &last_field
)) != NULL
)
387 mbr_string_to_uuid(field
, *uu
);
391 if (*last_field
== ':') // empty username field
393 else if ((field
= strtok_r(NULL
, ":", &last_field
)) != NULL
&& need_tag
)
398 if((tpass
= getpwnam(field
)) != NULL
)
399 if (mbr_uid_to_uuid(tpass
->pw_uid
, *uu
) != 0)
406 if ((tgrp
= getgrnam(field
)) != NULL
)
407 if (mbr_gid_to_uuid(tgrp
->gr_gid
, *uu
) != 0)
417 if (*last_field
== ':') // empty uid field
419 else if ((field
= strtok_r(NULL
, ":", &last_field
)) != NULL
&& need_tag
)
424 if((id
= strtol(field
, NULL
, 10)) == 0 && error
)
433 if((tpass
= getpwuid((uid_t
)id
)) != NULL
)
434 if (mbr_uid_to_uuid(tpass
->pw_uid
, *uu
) != 0)
441 if ((tgrp
= getgrgid((gid_t
)id
)) != NULL
)
442 if (mbr_gid_to_uuid(tgrp
->gr_gid
, *uu
) != 0)
452 /* nothing do set as qualifier */
460 if((field
= strtok_r(NULL
, ":", &last_field
)) == NULL
)
466 for (tag
= 0, sub
= strtok_r(field
, ",", &last_sub
); sub
;
467 sub
= strtok_r(NULL
, ",", &last_sub
))
469 if (!tag
&& !strcmp(sub
, "allow")) {
470 tag
= ACL_EXTENDED_ALLOW
;
472 } else if (!tag
&& !strcmp(sub
, "deny")) {
473 tag
= ACL_EXTENDED_DENY
;
476 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
478 if (acl_flags
[i
].type
& (ACL_TYPE_FILE
| ACL_TYPE_DIR
)
479 && !strcmp(acl_flags
[i
].name
, sub
))
481 acl_add_flag_np(flags
, acl_flags
[i
].flag
);
485 if (acl_flags
[i
].name
== NULL
)
487 /* couldn't find perm */
493 if((field
= strtok_r(NULL
, ":", &last_field
)) != NULL
) {
494 for (sub
= strtok_r(field
, ",", &last_sub
); sub
;
495 sub
= strtok_r(NULL
, ",", &last_sub
))
497 for (i
= 0; acl_perms
[i
].name
!= NULL
; i
++)
499 if (acl_perms
[i
].type
& (ACL_TYPE_FILE
| ACL_TYPE_DIR
)
500 && !strcmp(acl_perms
[i
].name
, sub
))
502 acl_add_perm(perms
, acl_perms
[i
].perm
);
506 if (acl_perms
[i
].name
== NULL
)
508 /* couldn't find perm */
514 acl_set_tag_type(acl_entry
, tag
);
515 acl_set_qualifier(acl_entry
, *uu
);
529 acl_to_text(acl_t acl
, ssize_t
*len_p
)
533 acl_entry_t entry
= NULL
;
537 char *str
, uu_str
[256];
540 size_t bufsize
= 1024;
543 if (!_ACL_VALID_ACL(acl
)) {
548 buf
= malloc(bufsize
);
550 len_p
= alloca(sizeof(ssize_t
));
554 if (!raosnprintf(&buf
, &bufsize
, len_p
, "!#acl %d", 1))
557 if (acl_get_flagset_np(acl
, &flags
) == 0)
559 for (i
= 0, first
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
561 if (acl_flags
[i
].type
& ACL_TYPE_ACL
562 && acl_get_flag_np(flags
, acl_flags
[i
].flag
) != 0)
564 if(!raosnprintf(&buf
, &bufsize
, len_p
, "%s%s",
565 first
++ ? "," : " ", acl_flags
[i
].name
))
570 for (;acl_get_entry(acl
,
571 entry
== NULL
? ACL_FIRST_ENTRY
: ACL_NEXT_ENTRY
, &entry
) == 0;)
573 if (((uu
= (uuid_t
*) acl_get_qualifier(entry
)) == NULL
)
574 || (acl_get_tag_type(entry
, &tag
) != 0)
575 || (acl_get_flagset_np(entry
, &flags
) != 0)
576 || (acl_get_permset(entry
, &perms
) != 0))
579 str
= uuid_to_name(uu
, &id
, &isgid
);
580 mbr_uuid_to_string(uu
, uu_str
); // XXX how big should uu_str be? // XXX error?
582 if(!raosnprintf(&buf
, &bufsize
, len_p
, "\n%s:%s:%s:%d:%s",
583 isgid
? "group" : "user",
587 (tag
== ACL_EXTENDED_ALLOW
) ? "allow" : "deny"))
592 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
594 if (acl_flags
[i
].type
& (ACL_TYPE_DIR
| ACL_TYPE_FILE
))
596 if(acl_get_flag_np(flags
, acl_flags
[i
].flag
) != 0)
598 if(!raosnprintf(&buf
, &bufsize
, len_p
, ",%s",
605 for (i
= 0, first
= 0; acl_perms
[i
].name
!= NULL
; ++i
)
607 if (acl_perms
[i
].type
& (ACL_TYPE_DIR
| ACL_TYPE_FILE
))
609 if(acl_get_perm_np(perms
, acl_perms
[i
].perm
) != 0)
611 if(!raosnprintf(&buf
, &bufsize
, len_p
, "%s%s",
619 buf
[(*len_p
)++] = '\n';
627 _ACL_VALIDATE_ACL(acl
);
629 return(_ACL_HEADER_SIZE
+ acl
->a_entries
* _ACL_ENTRY_SIZE
);