2 * Copyright (c) 2004-2015 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 <sys/types.h>
28 #include <mach/mach.h>
29 #include "membership.h"
30 #include "membershipPriv.h"
31 #include <servers/bootstrap.h>
32 #include <libkern/OSByteOrder.h>
35 #include <xpc/private.h>
36 #include <opendirectory/odipc.h>
38 #include <mach-o/dyld_priv.h>
41 static const uuid_t _user_compat_prefix
= {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd, 0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00};
42 static const uuid_t _group_compat_prefix
= {0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0x00, 0x00, 0x00, 0x00};
44 #define COMPAT_PREFIX_LEN (sizeof(uuid_t) - sizeof(id_t))
48 int _si_opendirectory_disabled
;
49 static xpc_pipe_t __mbr_pipe
; /* use accessor */
50 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
51 __private_extern__ xpc_object_t
_od_rpc_call(const char *procname
, xpc_object_t payload
, xpc_pipe_t (*get_pipe
)(bool));
59 if (__mbr_pipe
!= NULL
) {
60 xpc_pipe_invalidate(__mbr_pipe
);
61 /* disable release due to 10649340, it will cause a minor leak for each fork without exec */
62 // xpc_release(__mbr_pipe);
66 pthread_mutex_unlock(&mutex
);
72 _mbr_fork_prepare(void)
74 pthread_mutex_lock(&mutex
);
80 _mbr_fork_parent(void)
82 pthread_mutex_unlock(&mutex
);
89 _mbr_xpc_pipe(bool resetPipe
)
91 static dispatch_once_t once
;
92 xpc_pipe_t pipe
= NULL
;
94 dispatch_once(&once
, ^(void) {
97 /* if this is a build environment we ignore opendirectoryd */
98 xbs_disable
= getenv("XBS_DISABLE_LIBINFO");
99 if (xbs_disable
!= NULL
&& strcmp(xbs_disable
, "YES") == 0) {
100 _si_opendirectory_disabled
= 1;
104 pthread_atfork(_mbr_fork_prepare
, _mbr_fork_parent
, _mbr_fork_child
);
107 if (_si_opendirectory_disabled
== 1) {
111 pthread_mutex_lock(&mutex
);
113 xpc_release(__mbr_pipe
);
117 if (__mbr_pipe
== NULL
) {
118 if (!dyld_process_is_restricted() && getenv("OD_DEBUG_MODE") != NULL
) {
119 __mbr_pipe
= xpc_pipe_create(kODMachMembershipPortNameDebug
, 0);
121 __mbr_pipe
= xpc_pipe_create(kODMachMembershipPortName
, XPC_PIPE_FLAG_PRIVILEGED
);
125 if (__mbr_pipe
!= NULL
) pipe
= xpc_retain(__mbr_pipe
);
126 pthread_mutex_unlock(&mutex
);
133 _mbr_od_available(void)
136 xpc_pipe_t pipe
= _mbr_xpc_pipe(false);
146 mbr_identifier_translate(int id_type
, const void *identifier
, size_t identifier_size
, int target_type
, void **result
, int *rec_type
)
149 xpc_object_t payload
, reply
;
152 size_t identifier_len
;
155 if (identifier
== NULL
|| result
== NULL
|| identifier_size
== 0) return EIO
;
157 if (identifier_size
== -1) {
158 identifier_size
= strlen(identifier
);
160 /* 10898647: For types that are known to be strings, send the smallest necessary amount of data. */
162 case ID_TYPE_USERNAME
:
163 case ID_TYPE_GROUPNAME
:
164 case ID_TYPE_GROUP_NFS
:
165 case ID_TYPE_USER_NFS
:
166 case ID_TYPE_X509_DN
:
167 case ID_TYPE_KERBEROS
:
169 identifier_len
= strlen(identifier
);
170 if (identifier_size
> identifier_len
) {
171 identifier_size
= identifier_len
;
177 switch (target_type
) {
180 case ID_TYPE_UID_OR_GID
:
181 /* shortcut UUIDs using compatibilty prefixes */
182 if (id_type
== ID_TYPE_UUID
) {
183 const uint8_t *uu
= identifier
;
185 if (identifier_size
!= sizeof(uuid_t
)) return EINVAL
;
187 if (memcmp(uu
, _user_compat_prefix
, COMPAT_PREFIX_LEN
) == 0) {
188 id_t
*tempRes
= malloc(sizeof(*tempRes
));
189 if (tempRes
== NULL
) return ENOMEM
;
190 memcpy(&tempID
, &uu
[COMPAT_PREFIX_LEN
], sizeof(tempID
));
191 (*tempRes
) = ntohl(tempID
);
193 if (rec_type
!= NULL
) {
194 (*rec_type
) = MBR_REC_TYPE_USER
;
197 } else if (memcmp(uu
, _group_compat_prefix
, COMPAT_PREFIX_LEN
) == 0) {
198 id_t
*tempRes
= malloc(sizeof(*tempRes
));
199 if (tempRes
== NULL
) return ENOMEM
;
200 memcpy(&tempID
, &uu
[COMPAT_PREFIX_LEN
], sizeof(tempID
));
201 (*tempRes
) = ntohl(tempID
);
203 if (rec_type
!= NULL
) {
204 (*rec_type
) = MBR_REC_TYPE_GROUP
;
212 /* if this is a UID or GID translation, we shortcut UID/GID 0 */
213 /* or if no OD, we return compatibility UUIDs */
216 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
218 tempID
= *((id_t
*) identifier
);
219 if ((tempID
== 0) || (_mbr_od_available() == false)) {
220 uint8_t *tempUU
= malloc(sizeof(uuid_t
));
221 if (tempUU
== NULL
) return ENOMEM
;
222 uuid_copy(tempUU
, _user_compat_prefix
);
223 *((id_t
*) &tempUU
[COMPAT_PREFIX_LEN
]) = htonl(tempID
);
225 if (rec_type
!= NULL
) {
226 (*rec_type
) = MBR_REC_TYPE_USER
;
233 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
235 tempID
= *((id_t
*) identifier
);
236 if ((tempID
== 0) || (_mbr_od_available() == false)) {
237 uint8_t *tempUU
= malloc(sizeof(uuid_t
));
238 if (tempUU
== NULL
) return ENOMEM
;
239 uuid_copy(tempUU
, _group_compat_prefix
);
240 *((id_t
*) &tempUU
[COMPAT_PREFIX_LEN
]) = htonl(tempID
);
242 if (rec_type
!= NULL
) {
243 (*rec_type
) = MBR_REC_TYPE_GROUP
;
253 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
254 if (payload
== NULL
) return EIO
;
256 xpc_dictionary_set_int64(payload
, "requesting", target_type
);
257 xpc_dictionary_set_int64(payload
, "type", id_type
);
258 xpc_dictionary_set_data(payload
, "identifier", identifier
, identifier_size
);
260 reply
= _od_rpc_call("mbr_identifier_translate", payload
, _mbr_xpc_pipe
);
262 const void *reply_id
;
265 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
267 reply_id
= xpc_dictionary_get_data(reply
, "identifier", &idLen
);
268 if (reply_id
!= NULL
) {
269 char *identifier
= malloc(idLen
);
270 if (identifier
== NULL
) return ENOMEM
;
272 memcpy(identifier
, reply_id
, idLen
); // should already be NULL terminated, etc.
273 (*result
) = identifier
;
275 if (rec_type
!= NULL
) {
276 (*rec_type
) = (int) xpc_dictionary_get_int64(reply
, "rectype");
287 xpc_release(payload
);
294 mbr_uid_to_uuid(uid_t id
, uuid_t uu
)
296 return mbr_identifier_to_uuid(ID_TYPE_UID
, &id
, sizeof(id
), uu
);
300 mbr_gid_to_uuid(gid_t id
, uuid_t uu
)
302 return mbr_identifier_to_uuid(ID_TYPE_GID
, &id
, sizeof(id
), uu
);
306 mbr_uuid_to_id(const uuid_t uu
, uid_t
*id
, int *id_type
)
312 rc
= mbr_identifier_translate(ID_TYPE_UUID
, uu
, sizeof(uuid_t
), ID_TYPE_UID_OR_GID
, (void **) &result
, &local_type
);
314 switch (local_type
) {
315 case MBR_REC_TYPE_GROUP
:
316 (*id_type
) = ID_TYPE_GID
;
319 case MBR_REC_TYPE_USER
:
320 (*id_type
) = ID_TYPE_UID
;
336 mbr_sid_to_uuid(const nt_sid_t
*sid
, uuid_t uu
)
339 return mbr_identifier_to_uuid(ID_TYPE_SID
, sid
, sizeof(*sid
), uu
);
346 mbr_identifier_to_uuid(int id_type
, const void *identifier
, size_t identifier_size
, uuid_t uu
)
351 rc
= mbr_identifier_translate(id_type
, identifier
, identifier_size
, ID_TYPE_UUID
, (void **) &result
, NULL
);
353 uuid_copy(uu
, result
);
356 else if ((rc
== EIO
) && (_mbr_od_available() == false)) {
358 case ID_TYPE_USERNAME
:
360 struct passwd
*pw
= getpwnam(identifier
);
362 rc
= mbr_identifier_translate(ID_TYPE_UID
, &(pw
->pw_uid
), sizeof(id_t
), ID_TYPE_UUID
, (void **) &result
, NULL
);
364 uuid_copy(uu
, result
);
370 case ID_TYPE_GROUPNAME
:
372 struct group
*grp
= getgrnam(identifier
);
374 rc
= mbr_identifier_translate(ID_TYPE_GID
, &(grp
->gr_gid
), sizeof(id_t
), ID_TYPE_UUID
, (void **) &result
, NULL
);
376 uuid_copy(uu
, result
);
392 mbr_uuid_to_sid_type(const uuid_t uu
, nt_sid_t
*sid
, int *id_type
)
399 rc
= mbr_identifier_translate(ID_TYPE_UUID
, uu
, sizeof(uuid_t
), ID_TYPE_SID
, &result
, &local_type
);
401 memcpy(sid
, result
, sizeof(nt_sid_t
));
402 if (id_type
!= NULL
) {
404 switch (local_type
) {
405 case MBR_REC_TYPE_USER
:
406 (*id_type
) = SID_TYPE_USER
;
409 case MBR_REC_TYPE_GROUP
:
410 (*id_type
) = SID_TYPE_GROUP
;
428 mbr_uuid_to_sid(const uuid_t uu
, nt_sid_t
*sid
)
435 status
= mbr_uuid_to_sid_type(uu
, sid
, &type
);
436 if (status
!= 0) return status
;
445 mbr_check_membership(const uuid_t user
, const uuid_t group
, int *ismember
)
447 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_UUID
, group
, 0, ismember
);
451 mbr_check_membership_refresh(const uuid_t user
, uuid_t group
, int *ismember
)
453 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_UUID
, group
, 1, ismember
);
457 mbr_check_membership_ext(int userid_type
, const void *userid
, size_t userid_size
, int groupid_type
, const void *groupid
, int refresh
, int *isMember
)
460 xpc_object_t payload
, reply
;
463 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
464 if (payload
== NULL
) return ENOMEM
;
466 xpc_dictionary_set_int64(payload
, "user_idtype", userid_type
);
467 xpc_dictionary_set_data(payload
, "user_id", userid
, userid_size
);
468 xpc_dictionary_set_int64(payload
, "group_idtype", groupid_type
);
469 xpc_dictionary_set_bool(payload
, "refresh", refresh
);
471 switch (groupid_type
) {
472 case ID_TYPE_GROUPNAME
:
473 case ID_TYPE_GROUP_NFS
:
474 xpc_dictionary_set_data(payload
, "group_id", groupid
, strlen(groupid
));
478 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(id_t
));
482 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(nt_sid_t
));
486 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(uuid_t
));
495 reply
= _od_rpc_call("mbr_check_membership", payload
, _mbr_xpc_pipe
);
497 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
498 (*isMember
) = xpc_dictionary_get_bool(reply
, "ismember");
505 xpc_release(payload
);
514 mbr_check_membership_by_id(uuid_t user
, gid_t group
, int *ismember
)
516 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_GID
, &group
, 0, ismember
);
523 _od_rpc_call("mbr_cache_flush", NULL
, _mbr_xpc_pipe
);
531 mbr_user_name_to_uuid(const char *name
, uuid_t uu
)
533 return mbr_identifier_to_uuid(ID_TYPE_USERNAME
, name
, -1, uu
);
537 mbr_group_name_to_uuid(const char *name
, uuid_t uu
)
539 return mbr_identifier_to_uuid(ID_TYPE_GROUPNAME
, name
, -1, uu
);
543 mbr_check_service_membership(const uuid_t user
, const char *servicename
, int *ismember
)
546 xpc_object_t payload
, reply
;
549 if (ismember
== NULL
|| servicename
== NULL
) return EINVAL
;
551 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
552 if (payload
== NULL
) return EIO
;
554 xpc_dictionary_set_data(payload
, "user_id", user
, sizeof(uuid_t
));
555 xpc_dictionary_set_int64(payload
, "user_idtype", ID_TYPE_UUID
);
556 xpc_dictionary_set_string(payload
, "service", servicename
);
558 reply
= _od_rpc_call("mbr_check_service_membership", payload
, _mbr_xpc_pipe
);
560 result
= (int) xpc_dictionary_get_int64(reply
, "error");
561 (*ismember
) = xpc_dictionary_get_bool(reply
, "ismember");
568 xpc_release(payload
);
578 ConvertBytesToDecimal(char *buffer
, unsigned long long value
)
591 *temp
= '0' + (value
% 10);
600 mbr_sid_to_string(const nt_sid_t
*sid
, char *string
)
603 char *current
= string
;
608 if (sid
->sid_authcount
> NTSID_MAX_AUTHORITIES
) return EINVAL
;
610 for (i
= 0; i
< 6; i
++)
611 temp
= (temp
<< 8) | sid
->sid_authority
[i
];
616 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, sid
->sid_kind
));
617 current
= current
+ strlen(current
);
620 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, temp
));
622 for(i
=0; i
< sid
->sid_authcount
; i
++)
624 current
= current
+ strlen(current
);
627 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, sid
->sid_authorities
[i
]));
637 mbr_string_to_sid(const char *string
, nt_sid_t
*sid
)
640 char *current
= (char *)string
+2;
644 if (string
== NULL
) return EINVAL
;
646 memset(sid
, 0, sizeof(nt_sid_t
));
647 if (string
[0] != 'S' || string
[1] != '-') return EINVAL
;
649 sid
->sid_kind
= strtol(current
, ¤t
, 10);
650 if (*current
== '\0') return EINVAL
;
652 temp
= strtoll(current
, ¤t
, 10);
654 /* convert to BigEndian before copying */
655 temp
= OSSwapHostToBigInt64(temp
);
656 memcpy(sid
->sid_authority
, ((char*)&temp
)+2, 6);
657 while (*current
!= '\0' && count
< NTSID_MAX_AUTHORITIES
)
661 sid
->sid_authorities
[count
] = (u_int32_t
)strtoll(current
, ¤t
, 10);
662 if ((sid
->sid_authorities
[count
] == 0) && (errno
== EINVAL
)) {
668 if (*current
!= '\0') return EINVAL
;
670 sid
->sid_authcount
= count
;
679 mbr_uuid_to_string(const uuid_t uu
, char *string
)
681 uuid_unparse_upper(uu
, string
);
687 mbr_string_to_uuid(const char *string
, uuid_t uu
)
689 return uuid_parse(string
, uu
);
693 mbr_set_identifier_ttl(int id_type
, const void *identifier
, size_t identifier_size
, unsigned int seconds
)
696 xpc_object_t payload
, reply
;
699 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
700 if (payload
== NULL
) return ENOMEM
;
702 xpc_dictionary_set_int64(payload
, "type", id_type
);
703 xpc_dictionary_set_data(payload
, "identifier", identifier
, identifier_size
);
704 xpc_dictionary_set_int64(payload
, "ttl", seconds
);
707 reply
= _od_rpc_call("mbr_set_identifier_ttl", payload
, _mbr_xpc_pipe
);
709 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
716 xpc_release(payload
);