2 * Copyright (c) 2004-2010 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/errno.h>
25 #include <mach/mach.h>
26 #include "membership.h"
27 #include "membershipPriv.h"
28 #include <servers/bootstrap.h>
29 #include <libkern/OSByteOrder.h>
32 #include <xpc/private.h>
33 #include <opendirectory/odipc.h>
35 #include <mach-o/dyld_priv.h>
38 static const uuid_t _user_compat_prefix
= {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd, 0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00};
39 static const uuid_t _group_compat_prefix
= {0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0x00, 0x00, 0x00, 0x00};
41 #define COMPAT_PREFIX_LEN (sizeof(uuid_t) - sizeof(id_t))
45 int _si_opendirectory_disabled
;
46 static xpc_pipe_t __mbr_pipe
; /* use accessor */
47 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
48 __private_extern__ xpc_object_t
_od_rpc_call(const char *procname
, xpc_object_t payload
, xpc_pipe_t (*get_pipe
)(bool));
56 if (__mbr_pipe
!= NULL
) {
57 xpc_pipe_invalidate(__mbr_pipe
);
58 /* disable release due to 10649340, it will cause a minor leak for each fork without exec */
59 // xpc_release(__mbr_pipe);
63 pthread_mutex_unlock(&mutex
);
69 _mbr_fork_prepare(void)
71 pthread_mutex_lock(&mutex
);
77 _mbr_fork_parent(void)
79 pthread_mutex_unlock(&mutex
);
86 _mbr_xpc_pipe(bool resetPipe
)
88 static dispatch_once_t once
;
89 xpc_pipe_t pipe
= NULL
;
91 dispatch_once(&once
, ^(void) {
94 /* if this is a build environment we ignore opendirectoryd */
95 xbs_disable
= getenv("XBS_DISABLE_LIBINFO");
96 if (xbs_disable
!= NULL
&& strcmp(xbs_disable
, "YES") == 0) {
97 _si_opendirectory_disabled
= 1;
101 pthread_atfork(_mbr_fork_prepare
, _mbr_fork_parent
, _mbr_fork_child
);
104 if (_si_opendirectory_disabled
== 1) {
108 pthread_mutex_lock(&mutex
);
110 xpc_release(__mbr_pipe
);
114 if (__mbr_pipe
== NULL
) {
115 if (!dyld_process_is_restricted() && getenv("OD_DEBUG_MODE") != NULL
) {
116 __mbr_pipe
= xpc_pipe_create(kODMachMembershipPortNameDebug
, 0);
118 __mbr_pipe
= xpc_pipe_create(kODMachMembershipPortName
, XPC_PIPE_FLAG_PRIVILEGED
);
122 if (__mbr_pipe
!= NULL
) pipe
= xpc_retain(__mbr_pipe
);
123 pthread_mutex_unlock(&mutex
);
130 _mbr_od_available(void)
133 xpc_pipe_t pipe
= _mbr_xpc_pipe(false);
143 mbr_identifier_translate(int id_type
, const void *identifier
, size_t identifier_size
, int target_type
, void **result
, int *rec_type
)
146 xpc_object_t payload
, reply
;
149 size_t identifier_len
;
152 if (identifier
== NULL
|| result
== NULL
|| identifier_size
== 0) return EIO
;
154 if (identifier_size
== -1) {
155 identifier_size
= strlen(identifier
);
157 /* 10898647: For types that are known to be strings, send the smallest necessary amount of data. */
159 case ID_TYPE_USERNAME
:
160 case ID_TYPE_GROUPNAME
:
161 case ID_TYPE_GROUP_NFS
:
162 case ID_TYPE_USER_NFS
:
163 case ID_TYPE_X509_DN
:
164 case ID_TYPE_KERBEROS
:
166 identifier_len
= strlen(identifier
);
167 if (identifier_size
> identifier_len
) {
168 identifier_size
= identifier_len
;
174 switch (target_type
) {
177 case ID_TYPE_UID_OR_GID
:
178 /* shortcut UUIDs using compatibilty prefixes */
179 if (id_type
== ID_TYPE_UUID
) {
180 const uint8_t *uu
= identifier
;
182 if (identifier_size
!= sizeof(uuid_t
)) return EINVAL
;
184 if (memcmp(uu
, _user_compat_prefix
, COMPAT_PREFIX_LEN
) == 0) {
185 id_t
*tempRes
= malloc(sizeof(*tempRes
));
186 memcpy(&tempID
, &uu
[COMPAT_PREFIX_LEN
], sizeof(tempID
));
187 (*tempRes
) = ntohl(tempID
);
189 if (rec_type
!= NULL
) {
190 (*rec_type
) = MBR_REC_TYPE_USER
;
193 } else if (memcmp(uu
, _group_compat_prefix
, COMPAT_PREFIX_LEN
) == 0) {
194 id_t
*tempRes
= malloc(sizeof(*tempRes
));
195 memcpy(&tempID
, &uu
[COMPAT_PREFIX_LEN
], sizeof(tempID
));
196 (*tempRes
) = ntohl(tempID
);
198 if (rec_type
!= NULL
) {
199 (*rec_type
) = MBR_REC_TYPE_GROUP
;
207 /* if this is a UID or GID translation, we shortcut UID/GID 0 */
208 /* or if no OD, we return compatibility UUIDs */
211 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
213 tempID
= *((id_t
*) identifier
);
214 if ((tempID
== 0) || (_mbr_od_available() == false)) {
215 uint8_t *tempUU
= malloc(sizeof(uuid_t
));
216 uuid_copy(tempUU
, _user_compat_prefix
);
217 *((id_t
*) &tempUU
[COMPAT_PREFIX_LEN
]) = htonl(tempID
);
219 if (rec_type
!= NULL
) {
220 (*rec_type
) = MBR_REC_TYPE_USER
;
227 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
229 tempID
= *((id_t
*) identifier
);
230 if ((tempID
== 0) || (_mbr_od_available() == false)) {
231 uint8_t *tempUU
= malloc(sizeof(uuid_t
));
232 uuid_copy(tempUU
, _group_compat_prefix
);
233 *((id_t
*) &tempUU
[COMPAT_PREFIX_LEN
]) = htonl(tempID
);
235 if (rec_type
!= NULL
) {
236 (*rec_type
) = MBR_REC_TYPE_GROUP
;
246 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
247 if (payload
== NULL
) return EIO
;
249 xpc_dictionary_set_int64(payload
, "requesting", target_type
);
250 xpc_dictionary_set_int64(payload
, "type", id_type
);
251 xpc_dictionary_set_data(payload
, "identifier", identifier
, identifier_size
);
253 reply
= _od_rpc_call("mbr_identifier_translate", payload
, _mbr_xpc_pipe
);
255 const void *reply_id
;
258 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
260 reply_id
= xpc_dictionary_get_data(reply
, "identifier", &idLen
);
261 if (reply_id
!= NULL
) {
262 char *identifier
= malloc(idLen
);
264 memcpy(identifier
, reply_id
, idLen
); // should already be NULL terminated, etc.
265 (*result
) = identifier
;
267 if (rec_type
!= NULL
) {
268 (*rec_type
) = (int) xpc_dictionary_get_int64(reply
, "rectype");
279 xpc_release(payload
);
286 mbr_uid_to_uuid(uid_t id
, uuid_t uu
)
288 return mbr_identifier_to_uuid(ID_TYPE_UID
, &id
, sizeof(id
), uu
);
292 mbr_gid_to_uuid(gid_t id
, uuid_t uu
)
294 return mbr_identifier_to_uuid(ID_TYPE_GID
, &id
, sizeof(id
), uu
);
298 mbr_uuid_to_id(const uuid_t uu
, uid_t
*id
, int *id_type
)
304 rc
= mbr_identifier_translate(ID_TYPE_UUID
, uu
, sizeof(uuid_t
), ID_TYPE_UID_OR_GID
, (void **) &result
, &local_type
);
306 switch (local_type
) {
307 case MBR_REC_TYPE_GROUP
:
308 (*id_type
) = ID_TYPE_GID
;
311 case MBR_REC_TYPE_USER
:
312 (*id_type
) = ID_TYPE_UID
;
328 mbr_sid_to_uuid(const nt_sid_t
*sid
, uuid_t uu
)
331 return mbr_identifier_to_uuid(ID_TYPE_SID
, sid
, sizeof(*sid
), uu
);
338 mbr_identifier_to_uuid(int id_type
, const void *identifier
, size_t identifier_size
, uuid_t uu
)
343 rc
= mbr_identifier_translate(id_type
, identifier
, identifier_size
, ID_TYPE_UUID
, (void **) &result
, NULL
);
345 uuid_copy(uu
, result
);
353 mbr_uuid_to_sid_type(const uuid_t uu
, nt_sid_t
*sid
, int *id_type
)
360 rc
= mbr_identifier_translate(ID_TYPE_UUID
, uu
, sizeof(uuid_t
), ID_TYPE_SID
, &result
, &local_type
);
362 memcpy(sid
, result
, sizeof(nt_sid_t
));
363 if (id_type
!= NULL
) {
365 switch (local_type
) {
366 case MBR_REC_TYPE_USER
:
367 (*id_type
) = SID_TYPE_USER
;
370 case MBR_REC_TYPE_GROUP
:
371 (*id_type
) = SID_TYPE_GROUP
;
389 mbr_uuid_to_sid(const uuid_t uu
, nt_sid_t
*sid
)
396 status
= mbr_uuid_to_sid_type(uu
, sid
, &type
);
397 if (status
!= 0) return status
;
406 mbr_check_membership(const uuid_t user
, const uuid_t group
, int *ismember
)
408 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_UUID
, group
, 0, ismember
);
412 mbr_check_membership_refresh(const uuid_t user
, uuid_t group
, int *ismember
)
414 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_UUID
, group
, 1, ismember
);
418 mbr_check_membership_ext(int userid_type
, const void *userid
, size_t userid_size
, int groupid_type
, const void *groupid
, int refresh
, int *isMember
)
421 xpc_object_t payload
, reply
;
424 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
425 if (payload
== NULL
) return ENOMEM
;
427 xpc_dictionary_set_int64(payload
, "user_idtype", userid_type
);
428 xpc_dictionary_set_data(payload
, "user_id", userid
, userid_size
);
429 xpc_dictionary_set_int64(payload
, "group_idtype", groupid_type
);
431 switch (groupid_type
) {
432 case ID_TYPE_GROUPNAME
:
433 case ID_TYPE_GROUP_NFS
:
434 xpc_dictionary_set_data(payload
, "group_id", groupid
, strlen(groupid
));
438 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(id_t
));
442 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(nt_sid_t
));
446 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(uuid_t
));
455 reply
= _od_rpc_call("mbr_check_membership", payload
, _mbr_xpc_pipe
);
457 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
458 (*isMember
) = xpc_dictionary_get_bool(reply
, "ismember");
465 xpc_release(payload
);
474 mbr_check_membership_by_id(uuid_t user
, gid_t group
, int *ismember
)
476 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_GID
, &group
, 0, ismember
);
483 _od_rpc_call("mbr_cache_flush", NULL
, _mbr_xpc_pipe
);
491 mbr_user_name_to_uuid(const char *name
, uuid_t uu
)
493 return mbr_identifier_to_uuid(ID_TYPE_USERNAME
, name
, -1, uu
);
497 mbr_group_name_to_uuid(const char *name
, uuid_t uu
)
499 return mbr_identifier_to_uuid(ID_TYPE_GROUPNAME
, name
, -1, uu
);
503 mbr_check_service_membership(const uuid_t user
, const char *servicename
, int *ismember
)
506 xpc_object_t payload
, reply
;
509 if (ismember
== NULL
|| servicename
== NULL
) return EINVAL
;
511 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
512 if (payload
== NULL
) return EIO
;
514 xpc_dictionary_set_data(payload
, "user_id", user
, sizeof(uuid_t
));
515 xpc_dictionary_set_int64(payload
, "user_idtype", ID_TYPE_UUID
);
516 xpc_dictionary_set_string(payload
, "service", servicename
);
518 reply
= _od_rpc_call("mbr_check_service_membership", payload
, _mbr_xpc_pipe
);
520 result
= (int) xpc_dictionary_get_int64(reply
, "error");
521 (*ismember
) = xpc_dictionary_get_bool(reply
, "ismember");
528 xpc_release(payload
);
538 ConvertBytesToDecimal(char *buffer
, unsigned long long value
)
551 *temp
= '0' + (value
% 10);
560 mbr_sid_to_string(const nt_sid_t
*sid
, char *string
)
563 char *current
= string
;
568 if (sid
->sid_authcount
> NTSID_MAX_AUTHORITIES
) return EINVAL
;
570 for (i
= 0; i
< 6; i
++)
571 temp
= (temp
<< 8) | sid
->sid_authority
[i
];
576 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, sid
->sid_kind
));
577 current
= current
+ strlen(current
);
580 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, temp
));
582 for(i
=0; i
< sid
->sid_authcount
; i
++)
584 current
= current
+ strlen(current
);
587 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, sid
->sid_authorities
[i
]));
597 mbr_string_to_sid(const char *string
, nt_sid_t
*sid
)
600 char *current
= (char *)string
+2;
604 if (string
== NULL
) return EINVAL
;
606 memset(sid
, 0, sizeof(nt_sid_t
));
607 if (string
[0] != 'S' || string
[1] != '-') return EINVAL
;
609 sid
->sid_kind
= strtol(current
, ¤t
, 10);
610 if (*current
== '\0') return EINVAL
;
612 temp
= strtoll(current
, ¤t
, 10);
614 /* convert to BigEndian before copying */
615 temp
= OSSwapHostToBigInt64(temp
);
616 memcpy(sid
->sid_authority
, ((char*)&temp
)+2, 6);
617 while (*current
!= '\0' && count
< NTSID_MAX_AUTHORITIES
)
621 sid
->sid_authorities
[count
] = (u_int32_t
)strtoll(current
, ¤t
, 10);
622 if ((sid
->sid_authorities
[count
] == 0) && (errno
== EINVAL
)) {
628 if (*current
!= '\0') return EINVAL
;
630 sid
->sid_authcount
= count
;
639 mbr_uuid_to_string(const uuid_t uu
, char *string
)
641 uuid_unparse_upper(uu
, string
);
647 mbr_string_to_uuid(const char *string
, uuid_t uu
)
649 return uuid_parse(string
, uu
);
653 mbr_set_identifier_ttl(int id_type
, const void *identifier
, size_t identifier_size
, unsigned int seconds
)
656 xpc_object_t payload
, reply
;
659 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
660 if (payload
== NULL
) return ENOMEM
;
662 xpc_dictionary_set_int64(payload
, "type", id_type
);
663 xpc_dictionary_set_data(payload
, "identifier", identifier
, identifier_size
);
664 xpc_dictionary_set_int64(payload
, "ttl", seconds
);
667 reply
= _od_rpc_call("mbr_set_identifier_ttl", payload
, _mbr_xpc_pipe
);
669 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
676 xpc_release(payload
);