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>
29 #include <mach/mach.h>
30 #include "membership.h"
31 #include "membershipPriv.h"
32 #include <servers/bootstrap.h>
33 #include <libkern/OSByteOrder.h>
36 #include <xpc/private.h>
37 #include <opendirectory/odipc.h>
39 #include <mach-o/dyld_priv.h>
42 static const uuid_t _user_compat_prefix
= {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd, 0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00};
43 static const uuid_t _group_compat_prefix
= {0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0x00, 0x00, 0x00, 0x00};
45 #define COMPAT_PREFIX_LEN (sizeof(uuid_t) - sizeof(id_t))
49 int _si_opendirectory_disabled
;
50 static xpc_pipe_t __mbr_pipe
; /* use accessor */
51 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
52 __private_extern__ xpc_object_t
_od_rpc_call(const char *procname
, xpc_object_t payload
, xpc_pipe_t (*get_pipe
)(bool));
60 if (__mbr_pipe
!= NULL
) {
61 xpc_pipe_invalidate(__mbr_pipe
);
62 /* disable release due to 10649340, it will cause a minor leak for each fork without exec */
63 // xpc_release(__mbr_pipe);
67 pthread_mutex_unlock(&mutex
);
73 _mbr_fork_prepare(void)
75 pthread_mutex_lock(&mutex
);
81 _mbr_fork_parent(void)
83 pthread_mutex_unlock(&mutex
);
90 _mbr_xpc_pipe(bool resetPipe
)
92 static dispatch_once_t once
;
93 xpc_pipe_t pipe
= NULL
;
95 dispatch_once(&once
, ^(void) {
98 /* if this is a build environment we ignore opendirectoryd */
99 xbs_disable
= getenv("XBS_DISABLE_LIBINFO");
100 if (xbs_disable
!= NULL
&& strcmp(xbs_disable
, "YES") == 0) {
101 _si_opendirectory_disabled
= 1;
105 pthread_atfork(_mbr_fork_prepare
, _mbr_fork_parent
, _mbr_fork_child
);
108 if (_si_opendirectory_disabled
== 1) {
112 pthread_mutex_lock(&mutex
);
114 xpc_release(__mbr_pipe
);
118 if (__mbr_pipe
== NULL
) {
119 if (!dyld_process_is_restricted() && getenv("OD_DEBUG_MODE") != NULL
) {
120 __mbr_pipe
= xpc_pipe_create(kODMachMembershipPortNameDebug
, 0);
122 __mbr_pipe
= xpc_pipe_create(kODMachMembershipPortName
, XPC_PIPE_FLAG_PRIVILEGED
);
126 if (__mbr_pipe
!= NULL
) pipe
= xpc_retain(__mbr_pipe
);
127 pthread_mutex_unlock(&mutex
);
134 _mbr_od_available(void)
137 xpc_pipe_t pipe
= _mbr_xpc_pipe(false);
147 parse_compatibility_uuid(const uuid_t uu
, id_t
*result
, int *rec_type
)
151 if (memcmp(uu
, _user_compat_prefix
, COMPAT_PREFIX_LEN
) == 0) {
152 memcpy(&tempID
, &uu
[COMPAT_PREFIX_LEN
], sizeof(tempID
));
153 (*result
) = ntohl(tempID
);
154 if (rec_type
!= NULL
) {
155 (*rec_type
) = MBR_REC_TYPE_USER
;
158 } else if (memcmp(uu
, _group_compat_prefix
, COMPAT_PREFIX_LEN
) == 0) {
159 memcpy(&tempID
, &uu
[COMPAT_PREFIX_LEN
], sizeof(tempID
));
160 (*result
) = ntohl(tempID
);
161 if (rec_type
!= NULL
) {
162 (*rec_type
) = MBR_REC_TYPE_GROUP
;
170 compatibility_name_for_id(id_t id
, int rec_type
, char **result
)
174 if ((bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
)) == -1)
177 if (rec_type
== MBR_REC_TYPE_USER
) {
178 char buffer
[bufsize
];
179 struct passwd pwd
, *pwdp
= NULL
;
181 if (getpwuid_r(id
, &pwd
, buffer
, bufsize
, &pwdp
) != 0 || pwdp
== NULL
) {
184 (*result
) = strdup(pwd
.pw_name
);
185 return (*result
) != NULL
;
186 } else if (rec_type
== MBR_REC_TYPE_GROUP
) {
187 char buffer
[bufsize
];
188 struct group grp
, *grpp
= NULL
;
190 if (getgrgid_r(id
, &grp
, buffer
, bufsize
, &grpp
) != 0 || grpp
== NULL
) {
193 (*result
) = strdup(grp
.gr_name
);
194 return (*result
) != NULL
;
200 compatibility_name_for_uuid(const uuid_t uu
, char **result
, int *rec_type
)
205 if (parse_compatibility_uuid(uu
, &id
, &temp_type
) &&
206 compatibility_name_for_id(id
, temp_type
, result
)) {
207 if (rec_type
!= NULL
) {
208 (*rec_type
) = temp_type
;
217 mbr_identifier_translate(int id_type
, const void *identifier
, size_t identifier_size
, int target_type
, void **result
, int *rec_type
)
220 xpc_object_t payload
, reply
;
223 size_t identifier_len
;
226 if (identifier
== NULL
|| result
== NULL
|| identifier_size
== 0) return EIO
;
228 if (identifier_size
== -1) {
229 identifier_size
= strlen(identifier
);
231 /* 10898647: For types that are known to be strings, send the smallest necessary amount of data. */
233 case ID_TYPE_USERNAME
:
234 case ID_TYPE_GROUPNAME
:
235 case ID_TYPE_GROUP_NFS
:
236 case ID_TYPE_USER_NFS
:
237 case ID_TYPE_X509_DN
:
238 case ID_TYPE_KERBEROS
:
240 identifier_len
= strlen(identifier
);
241 if (identifier_size
> identifier_len
) {
242 identifier_size
= identifier_len
;
248 switch (target_type
) {
251 case ID_TYPE_UID_OR_GID
:
252 /* shortcut UUIDs using compatibility prefixes */
253 if (id_type
== ID_TYPE_UUID
) {
256 if (identifier_size
!= sizeof(uuid_t
)) return EINVAL
;
258 tempRes
= malloc(sizeof(*tempRes
));
259 if (tempRes
== NULL
) return ENOMEM
;
261 if (parse_compatibility_uuid(identifier
, tempRes
, rec_type
)) {
269 /* if this is a UID or GID translation, we shortcut UID/GID 0 */
270 /* or if no OD, we return compatibility UUIDs */
273 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
275 tempID
= *((id_t
*) identifier
);
276 if ((tempID
== 0) || (_mbr_od_available() == false)) {
277 uint8_t *tempUU
= malloc(sizeof(uuid_t
));
278 if (tempUU
== NULL
) return ENOMEM
;
279 uuid_copy(tempUU
, _user_compat_prefix
);
280 *((id_t
*) &tempUU
[COMPAT_PREFIX_LEN
]) = htonl(tempID
);
282 if (rec_type
!= NULL
) {
283 (*rec_type
) = MBR_REC_TYPE_USER
;
290 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
292 tempID
= *((id_t
*) identifier
);
293 if ((tempID
== 0) || (_mbr_od_available() == false)) {
294 uint8_t *tempUU
= malloc(sizeof(uuid_t
));
295 if (tempUU
== NULL
) return ENOMEM
;
296 uuid_copy(tempUU
, _group_compat_prefix
);
297 *((id_t
*) &tempUU
[COMPAT_PREFIX_LEN
]) = htonl(tempID
);
299 if (rec_type
!= NULL
) {
300 (*rec_type
) = MBR_REC_TYPE_GROUP
;
308 case ID_TYPE_USERNAME
:
309 case ID_TYPE_GROUPNAME
:
312 /* Convert compatibility UUIDs to names in-process. */
313 if (id_type
== ID_TYPE_UUID
) {
314 if (identifier_size
!= sizeof(uuid_t
)) return EINVAL
;
315 if (compatibility_name_for_uuid(identifier
, (char **)result
, rec_type
)) {
318 } else if (id_type
== ID_TYPE_UID
) {
319 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
321 tempID
= *((id_t
*) identifier
);
322 if (compatibility_name_for_id(tempID
, MBR_REC_TYPE_USER
, (char **)result
)) {
323 if (rec_type
!= NULL
) {
324 (*rec_type
) = MBR_REC_TYPE_USER
;
328 } else if (id_type
== ID_TYPE_GID
) {
329 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
331 tempID
= *((id_t
*) identifier
);
332 if (compatibility_name_for_id(tempID
, MBR_REC_TYPE_GROUP
, (char **)result
)) {
333 if (rec_type
!= NULL
) {
334 (*rec_type
) = MBR_REC_TYPE_GROUP
;
344 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
345 if (payload
== NULL
) return EIO
;
347 xpc_dictionary_set_int64(payload
, "requesting", target_type
);
348 xpc_dictionary_set_int64(payload
, "type", id_type
);
349 xpc_dictionary_set_data(payload
, "identifier", identifier
, identifier_size
);
351 reply
= _od_rpc_call("mbr_identifier_translate", payload
, _mbr_xpc_pipe
);
353 const void *reply_id
;
356 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
358 reply_id
= xpc_dictionary_get_data(reply
, "identifier", &idLen
);
359 if (reply_id
!= NULL
) {
360 char *identifier
= malloc(idLen
);
361 if (identifier
== NULL
) return ENOMEM
;
363 memcpy(identifier
, reply_id
, idLen
); // should already be NULL terminated, etc.
364 (*result
) = identifier
;
366 if (rec_type
!= NULL
) {
367 (*rec_type
) = (int) xpc_dictionary_get_int64(reply
, "rectype");
378 xpc_release(payload
);
385 mbr_uid_to_uuid(uid_t id
, uuid_t uu
)
387 return mbr_identifier_to_uuid(ID_TYPE_UID
, &id
, sizeof(id
), uu
);
391 mbr_gid_to_uuid(gid_t id
, uuid_t uu
)
393 return mbr_identifier_to_uuid(ID_TYPE_GID
, &id
, sizeof(id
), uu
);
397 mbr_uuid_to_id(const uuid_t uu
, uid_t
*id
, int *id_type
)
403 rc
= mbr_identifier_translate(ID_TYPE_UUID
, uu
, sizeof(uuid_t
), ID_TYPE_UID_OR_GID
, (void **) &result
, &local_type
);
405 switch (local_type
) {
406 case MBR_REC_TYPE_GROUP
:
407 (*id_type
) = ID_TYPE_GID
;
410 case MBR_REC_TYPE_USER
:
411 (*id_type
) = ID_TYPE_UID
;
427 mbr_sid_to_uuid(const nt_sid_t
*sid
, uuid_t uu
)
430 return mbr_identifier_to_uuid(ID_TYPE_SID
, sid
, sizeof(*sid
), uu
);
437 mbr_identifier_to_uuid(int id_type
, const void *identifier
, size_t identifier_size
, uuid_t uu
)
442 rc
= mbr_identifier_translate(id_type
, identifier
, identifier_size
, ID_TYPE_UUID
, (void **) &result
, NULL
);
444 uuid_copy(uu
, result
);
447 else if ((rc
== EIO
) && (_mbr_od_available() == false)) {
449 case ID_TYPE_USERNAME
:
451 struct passwd
*pw
= getpwnam(identifier
);
453 rc
= mbr_identifier_translate(ID_TYPE_UID
, &(pw
->pw_uid
), sizeof(id_t
), ID_TYPE_UUID
, (void **) &result
, NULL
);
455 uuid_copy(uu
, result
);
461 case ID_TYPE_GROUPNAME
:
463 struct group
*grp
= getgrnam(identifier
);
465 rc
= mbr_identifier_translate(ID_TYPE_GID
, &(grp
->gr_gid
), sizeof(id_t
), ID_TYPE_UUID
, (void **) &result
, NULL
);
467 uuid_copy(uu
, result
);
483 mbr_uuid_to_sid_type(const uuid_t uu
, nt_sid_t
*sid
, int *id_type
)
490 rc
= mbr_identifier_translate(ID_TYPE_UUID
, uu
, sizeof(uuid_t
), ID_TYPE_SID
, &result
, &local_type
);
492 memcpy(sid
, result
, sizeof(nt_sid_t
));
493 if (id_type
!= NULL
) {
495 switch (local_type
) {
496 case MBR_REC_TYPE_USER
:
497 (*id_type
) = SID_TYPE_USER
;
500 case MBR_REC_TYPE_GROUP
:
501 (*id_type
) = SID_TYPE_GROUP
;
519 mbr_uuid_to_sid(const uuid_t uu
, nt_sid_t
*sid
)
526 status
= mbr_uuid_to_sid_type(uu
, sid
, &type
);
527 if (status
!= 0) return status
;
536 mbr_check_membership(const uuid_t user
, const uuid_t group
, int *ismember
)
538 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_UUID
, group
, 0, ismember
);
542 mbr_check_membership_refresh(const uuid_t user
, uuid_t group
, int *ismember
)
544 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_UUID
, group
, 1, ismember
);
548 mbr_check_membership_ext(int userid_type
, const void *userid
, size_t userid_size
, int groupid_type
, const void *groupid
, int refresh
, int *isMember
)
551 xpc_object_t payload
, reply
;
554 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
555 if (payload
== NULL
) return ENOMEM
;
557 xpc_dictionary_set_int64(payload
, "user_idtype", userid_type
);
558 xpc_dictionary_set_data(payload
, "user_id", userid
, userid_size
);
559 xpc_dictionary_set_int64(payload
, "group_idtype", groupid_type
);
560 xpc_dictionary_set_bool(payload
, "refresh", refresh
);
562 switch (groupid_type
) {
563 case ID_TYPE_GROUPNAME
:
564 case ID_TYPE_GROUP_NFS
:
565 xpc_dictionary_set_data(payload
, "group_id", groupid
, strlen(groupid
));
569 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(id_t
));
573 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(nt_sid_t
));
577 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(uuid_t
));
586 reply
= _od_rpc_call("mbr_check_membership", payload
, _mbr_xpc_pipe
);
588 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
589 (*isMember
) = xpc_dictionary_get_bool(reply
, "ismember");
596 xpc_release(payload
);
605 mbr_check_membership_by_id(uuid_t user
, gid_t group
, int *ismember
)
607 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_GID
, &group
, 0, ismember
);
614 _od_rpc_call("mbr_cache_flush", NULL
, _mbr_xpc_pipe
);
622 mbr_user_name_to_uuid(const char *name
, uuid_t uu
)
624 return mbr_identifier_to_uuid(ID_TYPE_USERNAME
, name
, -1, uu
);
628 mbr_group_name_to_uuid(const char *name
, uuid_t uu
)
630 return mbr_identifier_to_uuid(ID_TYPE_GROUPNAME
, name
, -1, uu
);
634 mbr_check_service_membership(const uuid_t user
, const char *servicename
, int *ismember
)
637 xpc_object_t payload
, reply
;
640 if (ismember
== NULL
|| servicename
== NULL
) return EINVAL
;
642 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
643 if (payload
== NULL
) return EIO
;
645 xpc_dictionary_set_data(payload
, "user_id", user
, sizeof(uuid_t
));
646 xpc_dictionary_set_int64(payload
, "user_idtype", ID_TYPE_UUID
);
647 xpc_dictionary_set_string(payload
, "service", servicename
);
649 reply
= _od_rpc_call("mbr_check_service_membership", payload
, _mbr_xpc_pipe
);
651 result
= (int) xpc_dictionary_get_int64(reply
, "error");
652 (*ismember
) = xpc_dictionary_get_bool(reply
, "ismember");
659 xpc_release(payload
);
669 ConvertBytesToDecimal(char *buffer
, unsigned long long value
)
682 *temp
= '0' + (value
% 10);
691 mbr_sid_to_string(const nt_sid_t
*sid
, char *string
)
694 char *current
= string
;
699 if (sid
->sid_authcount
> NTSID_MAX_AUTHORITIES
) return EINVAL
;
701 for (i
= 0; i
< 6; i
++)
702 temp
= (temp
<< 8) | sid
->sid_authority
[i
];
707 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, sid
->sid_kind
));
708 current
= current
+ strlen(current
);
711 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, temp
));
713 for(i
=0; i
< sid
->sid_authcount
; i
++)
715 current
= current
+ strlen(current
);
718 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, sid
->sid_authorities
[i
]));
728 mbr_string_to_sid(const char *string
, nt_sid_t
*sid
)
731 char *current
= (char *)string
+2;
735 if (string
== NULL
) return EINVAL
;
737 memset(sid
, 0, sizeof(nt_sid_t
));
738 if (string
[0] != 'S' || string
[1] != '-') return EINVAL
;
740 sid
->sid_kind
= strtol(current
, ¤t
, 10);
741 if (*current
== '\0') return EINVAL
;
743 temp
= strtoll(current
, ¤t
, 10);
745 /* convert to BigEndian before copying */
746 temp
= OSSwapHostToBigInt64(temp
);
747 memcpy(sid
->sid_authority
, ((char*)&temp
)+2, 6);
748 while (*current
!= '\0' && count
< NTSID_MAX_AUTHORITIES
)
752 sid
->sid_authorities
[count
] = (u_int32_t
)strtoll(current
, ¤t
, 10);
753 if ((sid
->sid_authorities
[count
] == 0) && (errno
== EINVAL
)) {
759 if (*current
!= '\0') return EINVAL
;
761 sid
->sid_authcount
= count
;
770 mbr_uuid_to_string(const uuid_t uu
, char *string
)
772 uuid_unparse_upper(uu
, string
);
778 mbr_string_to_uuid(const char *string
, uuid_t uu
)
780 return uuid_parse(string
, uu
);
784 mbr_set_identifier_ttl(int id_type
, const void *identifier
, size_t identifier_size
, unsigned int seconds
)
787 xpc_object_t payload
, reply
;
790 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
791 if (payload
== NULL
) return ENOMEM
;
793 xpc_dictionary_set_int64(payload
, "type", id_type
);
794 xpc_dictionary_set_data(payload
, "identifier", identifier
, identifier_size
);
795 xpc_dictionary_set_int64(payload
, "ttl", seconds
);
798 reply
= _od_rpc_call("mbr_set_identifier_ttl", payload
, _mbr_xpc_pipe
);
800 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
807 xpc_release(payload
);