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>
39 acl_copy_ext(void *buf
, acl_t acl
, ssize_t size
)
41 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
45 /* validate arguments, compute required size */
46 reqsize
= acl_size(acl
);
54 /* export the header */
55 ext
->fsec_magic
= KAUTH_FILESEC_MAGIC
;
56 ext
->fsec_entrycount
= acl
->a_entries
;
57 ext
->fsec_flags
= acl
->a_flags
;
61 for (i
= 0; i
< acl
->a_entries
; i
++) {
62 /* ACE contents are almost identical */
63 ext
->fsec_ace
[i
].ace_applicable
= acl
->a_ace
[i
].ae_applicable
;
64 ext
->fsec_ace
[i
].ace_flags
=
65 (acl
->a_ace
[i
].ae_tag
& KAUTH_ACE_KINDMASK
) |
66 (acl
->a_ace
[i
].ae_flags
& ~KAUTH_ACE_KINDMASK
);
67 ext
->fsec_ace
[i
].ace_rights
= acl
->a_ace
[i
].ae_perms
;
74 acl_copy_int(const void *buf
)
76 struct kauth_filesec
*ext
= (struct kauth_filesec
*)buf
;
80 if (ext
->fsec_magic
!= KAUTH_FILESEC_MAGIC
) {
85 if ((ap
= acl_init(ext
->fsec_entrycount
)) != NULL
) {
86 /* copy useful header fields */
87 ap
->a_flags
= ext
->fsec_flags
;
88 ap
->a_entries
= ext
->fsec_entrycount
;
90 for (i
= 0; i
< ap
->a_entries
; i
++) {
91 /* ACE contents are literally identical */
92 /* XXX Consider writing the magic out to the persistent store
93 * to detect corruption
95 ap
->a_ace
[i
].ae_magic
= _ACL_ENTRY_MAGIC
;
96 ap
->a_ace
[i
].ae_applicable
= ext
->fsec_ace
[i
].ace_applicable
;
97 ap
->a_ace
[i
].ae_flags
= ext
->fsec_ace
[i
].ace_flags
& ~KAUTH_ACE_KINDMASK
;
98 ap
->a_ace
[i
].ae_tag
= ext
->fsec_ace
[i
].ace_flags
& KAUTH_ACE_KINDMASK
;
99 ap
->a_ace
[i
].ae_perms
= ext
->fsec_ace
[i
].ace_rights
;
105 #define ACL_TYPE_DIR (1<<0)
106 #define ACL_TYPE_FILE (1<<1)
107 #define ACL_TYPE_ACL (1<<2)
114 {ACL_READ_DATA
, "read", ACL_TYPE_FILE
},
115 // {ACL_LIST_DIRECTORY, "list", ACL_TYPE_DIR},
116 {ACL_WRITE_DATA
, "write", ACL_TYPE_FILE
},
117 // {ACL_ADD_FILE, "add_file", ACL_TYPE_DIR},
118 {ACL_EXECUTE
, "execute", ACL_TYPE_FILE
},
119 // {ACL_SEARCH, "search", ACL_TYPE_DIR},
120 {ACL_DELETE
, "delete", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
121 {ACL_APPEND_DATA
, "append", ACL_TYPE_FILE
},
122 // {ACL_ADD_SUBDIRECTORY, "add_subdirectory", ACL_TYPE_DIR},
123 {ACL_DELETE_CHILD
, "delete_child", ACL_TYPE_DIR
},
124 {ACL_READ_ATTRIBUTES
, "readattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
125 {ACL_WRITE_ATTRIBUTES
, "writeattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
126 {ACL_READ_EXTATTRIBUTES
, "readextattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
127 {ACL_WRITE_EXTATTRIBUTES
, "writeextattr", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
128 {ACL_READ_SECURITY
, "readsecurity", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
129 {ACL_WRITE_SECURITY
, "writesecurity", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
130 {ACL_CHANGE_OWNER
, "chown", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
139 {ACL_FLAG_DEFER_INHERIT
, "defer_inherit", ACL_TYPE_ACL
},
140 {ACL_ENTRY_FILE_INHERIT
, "file_inherit", ACL_TYPE_DIR
},
141 {ACL_ENTRY_DIRECTORY_INHERIT
, "directory_inherit", ACL_TYPE_DIR
},
142 {ACL_ENTRY_LIMIT_INHERIT
, "limit_inherit", ACL_TYPE_FILE
| ACL_TYPE_DIR
},
143 {ACL_ENTRY_ONLY_INHERIT
, "only_inherit", ACL_TYPE_DIR
},
148 * reallocing snprintf with offset
152 raosnprintf(char **buf
, size_t *size
, ssize_t
*offset
, char *fmt
, ...)
162 ret
= vsnprintf(*buf
+ *offset
, *size
- *offset
, fmt
, ap
);
170 *buf
= realloc(*buf
, (*size
*= 2));
173 //warn("realloc failure");
178 uuid_to_name(uuid_t
*uu
, uid_t
*id
, int *isgid
)
180 struct group
*tgrp
= NULL
;
181 struct passwd
*tpass
= NULL
;
183 if (0 == mbr_uuid_to_id(*uu
, id
, isgid
))
188 if (!(tpass
= getpwuid(*id
)))
190 return strdup(tpass
->pw_name
);
193 if (!(tgrp
= getgrgid((gid_t
) *id
)))
195 return strdup(tgrp
->gr_name
);
198 errout
: ; //warn("Unable to translate qualifier on ACL\n");
205 acl_from_text(const char *buf_p
)
207 int i
, error
= 0, need_tag
= 1, ug_tag
= -1;
209 char *entry
, *field
, *sub
,
210 *last_field
, *last_entry
, *last_sub
;
212 struct passwd
*tpass
= NULL
;
213 struct group
*tgrp
= NULL
;
214 acl_entry_t acl_entry
;
215 acl_flagset_t flags
= NULL
;
216 acl_permset_t perms
= NULL
;
220 if ((acl_ret
= acl_init(1)) == NULL
)
226 if ((buf
= strdup(buf_p
)) == NULL
)
230 if ((entry
= strtok_r(buf
, "\n", &last_entry
)) != NULL
)
233 field
= strtok_r(entry
, " ", &last_field
);
234 if (field
&& strncmp(field
, "!#acl", strlen("!#acl")))
241 field
= strtok_r(NULL
, " ", &last_field
);
243 if (field
== NULL
|| strtol(field
, NULL
, 0) != 1)
250 if((field
= strtok_r(NULL
, " ", &last_field
)) != NULL
)
252 acl_get_flagset_np(acl_ret
, &flags
);
253 for (sub
= strtok_r(field
, ",", &last_sub
); sub
;
254 sub
= strtok_r(NULL
, ",", &last_sub
))
256 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
258 if (acl_flags
[i
].type
& ACL_TYPE_ACL
259 && !strcmp(acl_flags
[i
].name
, sub
))
261 acl_add_flag_np(flags
, acl_flags
[i
].flag
);
265 if (acl_flags
[i
].name
== NULL
)
267 /* couldn't find flag */
275 for (entry
= strtok_r(NULL
, "\n", &last_entry
); entry
;
276 entry
= strtok_r(NULL
, "\n", &last_entry
))
278 field
= strtok_r(entry
, ":", &last_field
);
280 if((uu
= calloc(1, sizeof(uuid_t
))) == NULL
)
283 if(acl_create_entry(&acl_ret
, &acl_entry
))
286 acl_get_flagset_np(acl_entry
, &flags
);
287 acl_get_permset(acl_entry
, &perms
);
292 if(!strncmp(buf
, "user", strlen(field
)))
293 ug_tag
= ID_TYPE_UID
;
296 if(!strncmp(buf
, "group", strlen(field
)))
297 ug_tag
= ID_TYPE_GID
;
303 if ((field
= strtok_r(NULL
, ":", &last_field
)) != NULL
)
305 mbr_string_to_uuid(field
, *uu
);
309 if ((field
= strtok_r(NULL
, ":", &last_field
)) != NULL
&& need_tag
)
314 if((tpass
= getpwnam(field
)) != NULL
)
315 if (mbr_uid_to_uuid(tpass
->pw_uid
, *uu
) != 0)
322 if ((tgrp
= getgrnam(field
)) != NULL
)
323 if (mbr_gid_to_uuid(tgrp
->gr_gid
, *uu
) != 0)
333 if ((field
= strtok_r(NULL
, ":", &last_field
)) != NULL
&& need_tag
)
338 if((id
= strtol(field
, NULL
, 10)) == 0 && error
)
347 if((tpass
= getpwuid((uid_t
)id
)) != NULL
)
348 if (mbr_uid_to_uuid(tpass
->pw_uid
, *uu
) != 0)
355 if ((tgrp
= getgrgid((gid_t
)id
)) != NULL
)
356 if (mbr_gid_to_uuid(tgrp
->gr_gid
, *uu
) != 0)
366 /* nothing do set as qualifier */
374 if((field
= strtok_r(NULL
, ":", &last_field
)) == NULL
)
380 for (tag
= 0, sub
= strtok_r(field
, ",", &last_sub
); sub
;
381 sub
= strtok_r(NULL
, ",", &last_sub
))
383 if (!tag
&& !strcmp(sub
, "allow")) {
384 tag
= ACL_EXTENDED_ALLOW
;
386 } else if (!tag
&& !strcmp(sub
, "deny")) {
387 tag
= ACL_EXTENDED_DENY
;
390 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
392 if (acl_flags
[i
].type
& (ACL_TYPE_FILE
| ACL_TYPE_DIR
)
393 && !strcmp(acl_flags
[i
].name
, sub
))
395 acl_add_flag_np(flags
, acl_flags
[i
].flag
);
399 if (acl_flags
[i
].name
== NULL
)
401 /* couldn't find perm */
407 if((field
= strtok_r(NULL
, ":", &last_field
)) == NULL
)
413 for (sub
= strtok_r(field
, ",", &last_sub
); sub
;
414 sub
= strtok_r(NULL
, ",", &last_sub
))
416 for (i
= 0; acl_perms
[i
].name
!= NULL
; i
++)
418 if (acl_perms
[i
].type
& (ACL_TYPE_FILE
| ACL_TYPE_DIR
)
419 && !strcmp(acl_perms
[i
].name
, sub
))
421 acl_add_perm(perms
, acl_perms
[i
].perm
);
425 if (acl_perms
[i
].name
== NULL
)
427 /* couldn't find perm */
432 acl_set_tag_type(acl_entry
, tag
);
433 acl_set_qualifier(acl_entry
, *uu
);
447 acl_to_text(acl_t acl
, ssize_t
*len_p
)
451 acl_entry_t entry
= NULL
;
455 char *str
, uu_str
[256];
459 size_t bufsize
= 1024;
460 char *buf
= malloc(bufsize
);
463 len_p
= alloca(sizeof(ssize_t
));
467 if(!raosnprintf(&buf
, &bufsize
, len_p
, "!#acl %d", 1))
470 if (acl_get_flagset_np(acl
, &flags
) == 0)
472 for (i
= 0, first
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
474 if (acl_flags
[i
].type
& ACL_TYPE_ACL
475 && acl_get_flag_np(flags
, acl_flags
[i
].flag
) != 0)
477 if(!raosnprintf(&buf
, &bufsize
, len_p
, "%s%s",
478 first
++ ? "," : " ", acl_flags
[i
].name
))
483 for (;acl_get_entry(acl
,
484 entry
== NULL
? ACL_FIRST_ENTRY
: ACL_NEXT_ENTRY
, &entry
) == 0;)
486 if (((uu
= (uuid_t
*) acl_get_qualifier(entry
)) == NULL
)
487 || (acl_get_tag_type(entry
, &tag
) != 0)
488 || (acl_get_flagset_np(entry
, &flags
) != 0)
489 || (acl_get_permset(entry
, &perms
) != 0))
492 str
= uuid_to_name(uu
, &id
, &isgid
);
493 mbr_uuid_to_string(uu
, uu_str
); // XXX how big should uu_str be? // XXX error?
495 if(!raosnprintf(&buf
, &bufsize
, len_p
, "\n%s:%s:%s:%d:%s",
496 isgid
? "group" : "user",
500 (tag
== ACL_EXTENDED_ALLOW
) ? "allow" : "deny"))
505 for (i
= 0; acl_flags
[i
].name
!= NULL
; ++i
)
507 if (acl_flags
[i
].type
& (ACL_TYPE_DIR
| ACL_TYPE_FILE
))
509 if(acl_get_flag_np(flags
, acl_flags
[i
].flag
) != 0)
511 if(!raosnprintf(&buf
, &bufsize
, len_p
, ",%s",
518 for (i
= 0, first
= 0; acl_perms
[i
].name
!= NULL
; ++i
)
520 if (acl_perms
[i
].type
& (ACL_TYPE_DIR
| ACL_TYPE_FILE
))
522 if(acl_get_perm_np(perms
, acl_perms
[i
].perm
) != 0)
524 if(!raosnprintf(&buf
, &bufsize
, len_p
, "%s%s",
532 buf
[(*len_p
)++] = '\n';
540 _ACL_VALIDATE_ACL(acl
);
542 return(_ACL_HEADER_SIZE
+ acl
->a_entries
* _ACL_ENTRY_SIZE
);