2 * Copyright (c) 1999 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/types.h>
30 #include <uuid/uuid.h>
33 * Versions of copy_int/copy_ext that retain native endianity.
35 extern ssize_t
acl_copy_ext_native(void *buf_p
, acl_t acl
, ssize_t size
);
36 extern acl_t
acl_copy_int_native(const void *buf_p
);
40 #define FS_VALID_UID (1<<0)
41 #define FS_VALID_GID (1<<1)
42 #define FS_VALID_UUID (1<<2)
43 #define FS_VALID_GRPUUID (1<<3)
44 #define FS_VALID_MODE (1<<4)
45 #define FS_VALID_ACL (1<<5)
55 #define FS_ISVALID(_fsec, _bit) ((_fsec)->fs_valid & (_bit))
56 #define FS_CLEAR(_fsec, _bit) ((_fsec)->fs_valid &= ~(_bit))
57 #define FS_SET(_fsec, _bit) ((_fsec)->fs_valid |= (_bit))
59 static void filesec_discard_aclbuf(struct _filesec
*fsec
);
66 fsp
= malloc(sizeof(*fsp
));
73 filesec_dup(filesec_t fsec
)
77 fsp
= malloc(sizeof(*fsp
));
80 if (FS_ISVALID(fsec
, FS_VALID_ACL
)) {
81 if (fsec
->fs_aclbuf
!= _FILESEC_REMOVE_ACL
) {
82 fsp
->fs_aclbuf
= malloc(fsec
->fs_aclsize
);
83 if (fsp
->fs_aclbuf
== NULL
) {
87 bcopy(fsec
->fs_aclbuf
, fsp
->fs_aclbuf
, fsec
->fs_aclsize
);
95 filesec_free(filesec_t fsec
)
97 filesec_discard_aclbuf(fsec
);
102 filesec_get_property(filesec_t fsec
, filesec_property_t property
, void *propptr
)
109 if (!FS_ISVALID(fsec
, FS_VALID_UID
)) {
114 *(uid_t
*)propptr
= fsec
->fs_uid
;
117 if (!FS_ISVALID(fsec
, FS_VALID_GID
)) {
122 *(gid_t
*)propptr
= fsec
->fs_gid
;
125 if (!FS_ISVALID(fsec
, FS_VALID_UUID
)) {
130 uuid_copy(*(uuid_t
*)propptr
, fsec
->fs_uuid
);
132 case FILESEC_GRPUUID
:
133 if (!FS_ISVALID(fsec
, FS_VALID_GRPUUID
)) {
138 uuid_copy(*(uuid_t
*)propptr
, fsec
->fs_grpuuid
);
141 if (!FS_ISVALID(fsec
, FS_VALID_MODE
)) {
146 *(mode_t
*)propptr
= fsec
->fs_mode
;
149 if (!FS_ISVALID(fsec
, FS_VALID_ACL
)) {
153 if (propptr
!= NULL
) {
154 if (fsec
->fs_aclbuf
== _FILESEC_REMOVE_ACL
) {
155 *(acl_t
*)propptr
= _FILESEC_REMOVE_ACL
;
157 *(acl_t
*)propptr
= acl_copy_int_native(fsec
->fs_aclbuf
);
158 if (*(acl_t
*)propptr
== NULL
)
163 case FILESEC_ACL_RAW
:
164 if (!FS_ISVALID(fsec
, FS_VALID_ACL
)) {
169 *(void **)propptr
= fsec
->fs_aclbuf
;
171 case FILESEC_ACL_ALLOCSIZE
:
172 if (!FS_ISVALID(fsec
, FS_VALID_ACL
)) {
177 *(size_t *)propptr
= fsec
->fs_aclsize
;
191 filesec_set_property(filesec_t fsec
, filesec_property_t property
, const void *propptr
)
194 ssize_t aclsize
, copysize
;
201 if (propptr
== _FILESEC_UNSET_PROPERTY
) {
202 FS_CLEAR(fsec
, FS_VALID_UID
);
204 fsec
->fs_uid
= *(uid_t
*)propptr
;
205 FS_SET(fsec
, FS_VALID_UID
);
209 if (propptr
== _FILESEC_UNSET_PROPERTY
) {
210 FS_CLEAR(fsec
, FS_VALID_GID
);
212 fsec
->fs_gid
= *(gid_t
*)propptr
;
213 FS_SET(fsec
, FS_VALID_GID
);
217 if (propptr
== _FILESEC_UNSET_PROPERTY
) {
218 FS_CLEAR(fsec
, FS_VALID_UUID
);
220 uuid_copy(fsec
->fs_uuid
, *(uuid_t
*)propptr
);
221 FS_SET(fsec
, FS_VALID_UUID
);
224 case FILESEC_GRPUUID
:
225 if (propptr
== _FILESEC_UNSET_PROPERTY
) {
226 FS_CLEAR(fsec
, FS_VALID_GRPUUID
);
228 uuid_copy(fsec
->fs_grpuuid
, *(uuid_t
*)propptr
);
229 FS_SET(fsec
, FS_VALID_GRPUUID
);
233 if (propptr
== _FILESEC_UNSET_PROPERTY
) {
234 FS_CLEAR(fsec
, FS_VALID_MODE
);
236 fsec
->fs_mode
= *(mode_t
*)propptr
;
237 FS_SET(fsec
, FS_VALID_MODE
);
241 if (propptr
== _FILESEC_UNSET_PROPERTY
) {
242 filesec_discard_aclbuf(fsec
);
243 FS_CLEAR(fsec
, FS_VALID_ACL
);
244 } else if (propptr
== _FILESEC_REMOVE_ACL
) {
245 filesec_discard_aclbuf(fsec
);
246 fsec
->fs_aclbuf
= _FILESEC_REMOVE_ACL
;
247 fsec
->fs_aclsize
= 0;
248 FS_SET(fsec
, FS_VALID_ACL
);
250 acl
= *(acl_t
*)propptr
;
251 aclsize
= acl_size(acl
);
256 if ((aclbuf
= malloc(aclsize
)) == NULL
) {
260 copysize
= acl_copy_ext_native(aclbuf
, acl
, aclsize
);
266 filesec_discard_aclbuf(fsec
);
267 fsec
->fs_aclbuf
= aclbuf
;
268 fsec
->fs_aclsize
= aclsize
;
269 FS_SET(fsec
, FS_VALID_ACL
);
272 case FILESEC_ACL_RAW
:
273 filesec_discard_aclbuf(fsec
);
274 if ((propptr
!= NULL
) && (*(void **)propptr
!= NULL
)) {
275 fsec
->fs_aclbuf
= *(void **)propptr
;
276 FS_SET(fsec
, FS_VALID_ACL
);
279 case FILESEC_ACL_ALLOCSIZE
:
280 if (propptr
!= NULL
) {
281 fsec
->fs_aclsize
= *(size_t *)propptr
;
298 filesec_unset_property(filesec_t fsec
, filesec_property_t property
)
300 return filesec_set_property(fsec
, property
, _FILESEC_UNSET_PROPERTY
);
304 filesec_query_property(filesec_t fsec
, filesec_property_t property
, int *validptr
)
311 valid
= FS_ISVALID(fsec
, FS_VALID_UID
);
314 valid
= FS_ISVALID(fsec
, FS_VALID_GID
);
317 valid
= FS_ISVALID(fsec
, FS_VALID_UUID
);
319 case FILESEC_GRPUUID
:
320 valid
= FS_ISVALID(fsec
, FS_VALID_GRPUUID
);
323 valid
= FS_ISVALID(fsec
, FS_VALID_MODE
);
326 case FILESEC_ACL_RAW
:
327 case FILESEC_ACL_ALLOCSIZE
:
328 valid
= FS_ISVALID(fsec
, FS_VALID_ACL
);
344 filesec_discard_aclbuf(struct _filesec
*fsec
)
346 if (FS_ISVALID(fsec
, FS_VALID_ACL
)) {
347 if (fsec
->fs_aclbuf
!= _FILESEC_REMOVE_ACL
)
348 free(fsec
->fs_aclbuf
);
349 fsec
->fs_aclbuf
= NULL
;
350 fsec
->fs_aclsize
= 0;
351 FS_CLEAR(fsec
, FS_VALID_ACL
);