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 <os/activity.h>
38 #include <opendirectory/odipc.h>
40 #include <mach-o/dyld_priv.h>
43 static const uuid_t _user_compat_prefix
= {0xff, 0xff, 0xee, 0xee, 0xdd, 0xdd, 0xcc, 0xcc, 0xbb, 0xbb, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00};
44 static const uuid_t _group_compat_prefix
= {0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0x00, 0x00, 0x00, 0x00};
46 #define COMPAT_PREFIX_LEN (sizeof(uuid_t) - sizeof(id_t))
49 #define MBR_OS_ACTIVITY(_desc) \
50 os_activity_t activity __attribute__((__cleanup__(_mbr_auto_os_release))) = os_activity_create(_desc, OS_ACTIVITY_CURRENT, OS_ACTIVITY_FLAG_DEFAULT); \
51 os_activity_scope(activity)
53 #define MBR_OS_ACTIVITY(_desc)
58 int _si_opendirectory_disabled
;
59 static xpc_pipe_t __mbr_pipe
; /* use accessor */
60 static pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
61 __private_extern__ xpc_object_t
_od_rpc_call(const char *procname
, xpc_object_t payload
, xpc_pipe_t (*get_pipe
)(bool));
69 if (__mbr_pipe
!= NULL
) {
70 xpc_pipe_invalidate(__mbr_pipe
);
71 /* disable release due to 10649340, it will cause a minor leak for each fork without exec */
72 // xpc_release(__mbr_pipe);
76 pthread_mutex_unlock(&mutex
);
82 _mbr_fork_prepare(void)
84 pthread_mutex_lock(&mutex
);
90 _mbr_fork_parent(void)
92 pthread_mutex_unlock(&mutex
);
98 _mbr_auto_os_release(os_activity_t
*activity
)
100 os_release(*activity
);
106 _mbr_xpc_pipe(bool resetPipe
)
108 static dispatch_once_t once
;
109 xpc_pipe_t pipe
= NULL
;
111 dispatch_once(&once
, ^(void) {
114 /* if this is a build environment we ignore opendirectoryd */
115 xbs_disable
= getenv("XBS_DISABLE_LIBINFO");
116 if (xbs_disable
!= NULL
&& strcmp(xbs_disable
, "YES") == 0) {
117 _si_opendirectory_disabled
= 1;
121 pthread_atfork(_mbr_fork_prepare
, _mbr_fork_parent
, _mbr_fork_child
);
124 if (_si_opendirectory_disabled
== 1) {
128 pthread_mutex_lock(&mutex
);
130 xpc_release(__mbr_pipe
);
134 if (__mbr_pipe
== NULL
) {
135 if (!dyld_process_is_restricted() && getenv("OD_DEBUG_MODE") != NULL
) {
136 __mbr_pipe
= xpc_pipe_create(kODMachMembershipPortNameDebug
, 0);
138 __mbr_pipe
= xpc_pipe_create(kODMachMembershipPortName
, XPC_PIPE_FLAG_PRIVILEGED
);
142 if (__mbr_pipe
!= NULL
) pipe
= xpc_retain(__mbr_pipe
);
143 pthread_mutex_unlock(&mutex
);
150 _mbr_od_available(void)
153 xpc_pipe_t pipe
= _mbr_xpc_pipe(false);
163 parse_compatibility_uuid(const uuid_t uu
, id_t
*result
, int *rec_type
)
167 if (memcmp(uu
, _user_compat_prefix
, COMPAT_PREFIX_LEN
) == 0) {
168 memcpy(&tempID
, &uu
[COMPAT_PREFIX_LEN
], sizeof(tempID
));
169 (*result
) = ntohl(tempID
);
170 if (rec_type
!= NULL
) {
171 (*rec_type
) = MBR_REC_TYPE_USER
;
174 } else if (memcmp(uu
, _group_compat_prefix
, COMPAT_PREFIX_LEN
) == 0) {
175 memcpy(&tempID
, &uu
[COMPAT_PREFIX_LEN
], sizeof(tempID
));
176 (*result
) = ntohl(tempID
);
177 if (rec_type
!= NULL
) {
178 (*rec_type
) = MBR_REC_TYPE_GROUP
;
187 compatibility_name_for_id(id_t id
, int rec_type
, char **result
)
191 if ((bufsize
= sysconf(_SC_GETPW_R_SIZE_MAX
)) == -1)
194 if (rec_type
== MBR_REC_TYPE_USER
) {
195 char buffer
[bufsize
];
196 struct passwd pwd
, *pwdp
= NULL
;
198 if (getpwuid_r(id
, &pwd
, buffer
, bufsize
, &pwdp
) != 0 || pwdp
== NULL
) {
201 (*result
) = strdup(pwd
.pw_name
);
202 return (*result
) != NULL
;
203 } else if (rec_type
== MBR_REC_TYPE_GROUP
) {
204 char buffer
[bufsize
];
205 struct group grp
, *grpp
= NULL
;
207 if (getgrgid_r(id
, &grp
, buffer
, bufsize
, &grpp
) != 0 || grpp
== NULL
) {
210 (*result
) = strdup(grp
.gr_name
);
211 return (*result
) != NULL
;
217 compatibility_name_for_uuid(const uuid_t uu
, char **result
, int *rec_type
)
222 if (parse_compatibility_uuid(uu
, &id
, &temp_type
) &&
223 compatibility_name_for_id(id
, temp_type
, result
)) {
224 if (rec_type
!= NULL
) {
225 (*rec_type
) = temp_type
;
235 mbr_identifier_translate(int id_type
, const void *identifier
, size_t identifier_size
, int target_type
, void **result
, int *rec_type
)
238 xpc_object_t payload
, reply
;
241 size_t identifier_len
;
244 if (identifier
== NULL
|| result
== NULL
|| identifier_size
== 0) return EIO
;
246 if (identifier_size
== -1) {
247 identifier_size
= strlen(identifier
);
249 /* 10898647: For types that are known to be strings, send the smallest necessary amount of data. */
251 case ID_TYPE_USERNAME
:
252 case ID_TYPE_GROUPNAME
:
253 case ID_TYPE_GROUP_NFS
:
254 case ID_TYPE_USER_NFS
:
255 case ID_TYPE_X509_DN
:
256 case ID_TYPE_KERBEROS
:
258 identifier_len
= strlen(identifier
);
259 if (identifier_size
> identifier_len
) {
260 identifier_size
= identifier_len
;
266 switch (target_type
) {
269 case ID_TYPE_UID_OR_GID
:
270 /* shortcut UUIDs using compatibility prefixes */
271 if (id_type
== ID_TYPE_UUID
) {
274 if (identifier_size
!= sizeof(uuid_t
)) return EINVAL
;
276 tempRes
= malloc(sizeof(*tempRes
));
277 if (tempRes
== NULL
) return ENOMEM
;
279 if (parse_compatibility_uuid(identifier
, tempRes
, rec_type
)) {
287 /* if this is a UID or GID translation, we shortcut UID/GID 0 */
288 /* or if no OD, we return compatibility UUIDs */
291 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
293 tempID
= *((id_t
*) identifier
);
294 if ((tempID
== 0) || (_mbr_od_available() == false)) {
295 uint8_t *tempUU
= malloc(sizeof(uuid_t
));
296 if (tempUU
== NULL
) return ENOMEM
;
297 uuid_copy(tempUU
, _user_compat_prefix
);
298 *((id_t
*) &tempUU
[COMPAT_PREFIX_LEN
]) = htonl(tempID
);
300 if (rec_type
!= NULL
) {
301 (*rec_type
) = MBR_REC_TYPE_USER
;
308 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
310 tempID
= *((id_t
*) identifier
);
311 if ((tempID
== 0) || (_mbr_od_available() == false)) {
312 uint8_t *tempUU
= malloc(sizeof(uuid_t
));
313 if (tempUU
== NULL
) return ENOMEM
;
314 uuid_copy(tempUU
, _group_compat_prefix
);
315 *((id_t
*) &tempUU
[COMPAT_PREFIX_LEN
]) = htonl(tempID
);
317 if (rec_type
!= NULL
) {
318 (*rec_type
) = MBR_REC_TYPE_GROUP
;
326 case ID_TYPE_USERNAME
:
327 case ID_TYPE_GROUPNAME
:
330 /* Convert compatibility UUIDs to names in-process. */
331 if (id_type
== ID_TYPE_UUID
) {
332 if (identifier_size
!= sizeof(uuid_t
)) return EINVAL
;
333 if (compatibility_name_for_uuid(identifier
, (char **)result
, rec_type
)) {
336 } else if (id_type
== ID_TYPE_UID
) {
337 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
339 tempID
= *((id_t
*) identifier
);
340 if (compatibility_name_for_id(tempID
, MBR_REC_TYPE_USER
, (char **)result
)) {
341 if (rec_type
!= NULL
) {
342 (*rec_type
) = MBR_REC_TYPE_USER
;
346 } else if (id_type
== ID_TYPE_GID
) {
347 if (identifier_size
!= sizeof(tempID
)) return EINVAL
;
349 tempID
= *((id_t
*) identifier
);
350 if (compatibility_name_for_id(tempID
, MBR_REC_TYPE_GROUP
, (char **)result
)) {
351 if (rec_type
!= NULL
) {
352 (*rec_type
) = MBR_REC_TYPE_GROUP
;
362 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
363 if (payload
== NULL
) return EIO
;
365 MBR_OS_ACTIVITY("Membership API: translate identifier");
367 xpc_dictionary_set_int64(payload
, "requesting", target_type
);
368 xpc_dictionary_set_int64(payload
, "type", id_type
);
369 xpc_dictionary_set_data(payload
, "identifier", identifier
, identifier_size
);
371 reply
= _od_rpc_call("mbr_identifier_translate", payload
, _mbr_xpc_pipe
);
373 const void *reply_id
;
376 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
378 reply_id
= xpc_dictionary_get_data(reply
, "identifier", &idLen
);
379 if (reply_id
!= NULL
) {
380 char *identifier
= malloc(idLen
);
381 if (identifier
== NULL
) return ENOMEM
;
383 memcpy(identifier
, reply_id
, idLen
); // should already be NULL terminated, etc.
384 (*result
) = identifier
;
386 if (rec_type
!= NULL
) {
387 (*rec_type
) = (int) xpc_dictionary_get_int64(reply
, "rectype");
398 xpc_release(payload
);
405 mbr_uid_to_uuid(uid_t id
, uuid_t uu
)
407 return mbr_identifier_to_uuid(ID_TYPE_UID
, &id
, sizeof(id
), uu
);
411 mbr_gid_to_uuid(gid_t id
, uuid_t uu
)
413 return mbr_identifier_to_uuid(ID_TYPE_GID
, &id
, sizeof(id
), uu
);
417 mbr_uuid_to_id(const uuid_t uu
, uid_t
*id
, int *id_type
)
423 rc
= mbr_identifier_translate(ID_TYPE_UUID
, uu
, sizeof(uuid_t
), ID_TYPE_UID_OR_GID
, (void **) &result
, &local_type
);
425 switch (local_type
) {
426 case MBR_REC_TYPE_GROUP
:
427 (*id_type
) = ID_TYPE_GID
;
430 case MBR_REC_TYPE_USER
:
431 (*id_type
) = ID_TYPE_UID
;
447 mbr_sid_to_uuid(const nt_sid_t
*sid
, uuid_t uu
)
450 return mbr_identifier_to_uuid(ID_TYPE_SID
, sid
, sizeof(*sid
), uu
);
457 mbr_identifier_to_uuid(int id_type
, const void *identifier
, size_t identifier_size
, uuid_t uu
)
462 rc
= mbr_identifier_translate(id_type
, identifier
, identifier_size
, ID_TYPE_UUID
, (void **) &result
, NULL
);
464 uuid_copy(uu
, result
);
467 else if ((rc
== EIO
) && (_mbr_od_available() == false)) {
469 case ID_TYPE_USERNAME
:
471 struct passwd
*pw
= getpwnam(identifier
);
473 rc
= mbr_identifier_translate(ID_TYPE_UID
, &(pw
->pw_uid
), sizeof(id_t
), ID_TYPE_UUID
, (void **) &result
, NULL
);
475 uuid_copy(uu
, result
);
481 case ID_TYPE_GROUPNAME
:
483 struct group
*grp
= getgrnam(identifier
);
485 rc
= mbr_identifier_translate(ID_TYPE_GID
, &(grp
->gr_gid
), sizeof(id_t
), ID_TYPE_UUID
, (void **) &result
, NULL
);
487 uuid_copy(uu
, result
);
503 mbr_uuid_to_sid_type(const uuid_t uu
, nt_sid_t
*sid
, int *id_type
)
510 rc
= mbr_identifier_translate(ID_TYPE_UUID
, uu
, sizeof(uuid_t
), ID_TYPE_SID
, &result
, &local_type
);
512 memcpy(sid
, result
, sizeof(nt_sid_t
));
513 if (id_type
!= NULL
) {
515 switch (local_type
) {
516 case MBR_REC_TYPE_USER
:
517 (*id_type
) = SID_TYPE_USER
;
520 case MBR_REC_TYPE_GROUP
:
521 (*id_type
) = SID_TYPE_GROUP
;
539 mbr_uuid_to_sid(const uuid_t uu
, nt_sid_t
*sid
)
546 status
= mbr_uuid_to_sid_type(uu
, sid
, &type
);
547 if (status
!= 0) return status
;
556 mbr_check_membership(const uuid_t user
, const uuid_t group
, int *ismember
)
558 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_UUID
, group
, 0, ismember
);
562 mbr_check_membership_refresh(const uuid_t user
, uuid_t group
, int *ismember
)
564 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_UUID
, group
, 1, ismember
);
568 mbr_check_membership_ext(int userid_type
, const void *userid
, size_t userid_size
, int groupid_type
, const void *groupid
, int refresh
, int *isMember
)
571 xpc_object_t payload
, reply
;
574 MBR_OS_ACTIVITY("Membership API: Validating user is a member of group");
575 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
576 if (payload
== NULL
) return ENOMEM
;
578 xpc_dictionary_set_int64(payload
, "user_idtype", userid_type
);
579 xpc_dictionary_set_data(payload
, "user_id", userid
, userid_size
);
580 xpc_dictionary_set_int64(payload
, "group_idtype", groupid_type
);
581 xpc_dictionary_set_bool(payload
, "refresh", refresh
);
583 switch (groupid_type
) {
584 case ID_TYPE_GROUPNAME
:
585 case ID_TYPE_GROUP_NFS
:
586 xpc_dictionary_set_data(payload
, "group_id", groupid
, strlen(groupid
));
590 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(id_t
));
594 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(nt_sid_t
));
598 xpc_dictionary_set_data(payload
, "group_id", groupid
, sizeof(uuid_t
));
607 reply
= _od_rpc_call("mbr_check_membership", payload
, _mbr_xpc_pipe
);
609 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
610 (*isMember
) = xpc_dictionary_get_bool(reply
, "ismember");
617 xpc_release(payload
);
626 mbr_check_membership_by_id(uuid_t user
, gid_t group
, int *ismember
)
628 return mbr_check_membership_ext(ID_TYPE_UUID
, user
, sizeof(uuid_t
), ID_TYPE_GID
, &group
, 0, ismember
);
635 MBR_OS_ACTIVITY("Membership API: Flush the membership cache");
636 _od_rpc_call("mbr_cache_flush", NULL
, _mbr_xpc_pipe
);
644 mbr_user_name_to_uuid(const char *name
, uuid_t uu
)
646 return mbr_identifier_to_uuid(ID_TYPE_USERNAME
, name
, -1, uu
);
650 mbr_group_name_to_uuid(const char *name
, uuid_t uu
)
652 return mbr_identifier_to_uuid(ID_TYPE_GROUPNAME
, name
, -1, uu
);
656 mbr_check_service_membership(const uuid_t user
, const char *servicename
, int *ismember
)
659 xpc_object_t payload
, reply
;
662 if (ismember
== NULL
|| servicename
== NULL
) return EINVAL
;
664 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
665 if (payload
== NULL
) return EIO
;
667 MBR_OS_ACTIVITY("Membership API: Validating user is allowed by service");
669 xpc_dictionary_set_data(payload
, "user_id", user
, sizeof(uuid_t
));
670 xpc_dictionary_set_int64(payload
, "user_idtype", ID_TYPE_UUID
);
671 xpc_dictionary_set_string(payload
, "service", servicename
);
673 reply
= _od_rpc_call("mbr_check_service_membership", payload
, _mbr_xpc_pipe
);
675 result
= (int) xpc_dictionary_get_int64(reply
, "error");
676 (*ismember
) = xpc_dictionary_get_bool(reply
, "ismember");
683 xpc_release(payload
);
693 ConvertBytesToDecimal(char *buffer
, unsigned long long value
)
706 *temp
= '0' + (value
% 10);
715 mbr_sid_to_string(const nt_sid_t
*sid
, char *string
)
718 char *current
= string
;
723 if (sid
->sid_authcount
> NTSID_MAX_AUTHORITIES
) return EINVAL
;
725 for (i
= 0; i
< 6; i
++)
726 temp
= (temp
<< 8) | sid
->sid_authority
[i
];
731 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, sid
->sid_kind
));
732 current
= current
+ strlen(current
);
735 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, temp
));
737 for(i
=0; i
< sid
->sid_authcount
; i
++)
739 current
= current
+ strlen(current
);
742 strcpy(current
, ConvertBytesToDecimal(tempBuffer
, sid
->sid_authorities
[i
]));
752 mbr_string_to_sid(const char *string
, nt_sid_t
*sid
)
755 char *current
= (char *)string
+2;
759 if (string
== NULL
) return EINVAL
;
761 memset(sid
, 0, sizeof(nt_sid_t
));
762 if (string
[0] != 'S' || string
[1] != '-') return EINVAL
;
764 sid
->sid_kind
= strtol(current
, ¤t
, 10);
765 if (*current
== '\0') return EINVAL
;
767 temp
= strtoll(current
, ¤t
, 10);
769 /* convert to BigEndian before copying */
770 temp
= OSSwapHostToBigInt64(temp
);
771 memcpy(sid
->sid_authority
, ((char*)&temp
)+2, 6);
772 while (*current
!= '\0' && count
< NTSID_MAX_AUTHORITIES
)
776 sid
->sid_authorities
[count
] = (u_int32_t
)strtoll(current
, ¤t
, 10);
777 if ((sid
->sid_authorities
[count
] == 0) && (errno
== EINVAL
)) {
783 if (*current
!= '\0') return EINVAL
;
785 sid
->sid_authcount
= count
;
794 mbr_uuid_to_string(const uuid_t uu
, char *string
)
796 uuid_unparse_upper(uu
, string
);
802 mbr_string_to_uuid(const char *string
, uuid_t uu
)
804 return uuid_parse(string
, uu
);
808 mbr_set_identifier_ttl(int id_type
, const void *identifier
, size_t identifier_size
, unsigned int seconds
)
811 xpc_object_t payload
, reply
;
814 payload
= xpc_dictionary_create(NULL
, NULL
, 0);
815 if (payload
== NULL
) return ENOMEM
;
817 MBR_OS_ACTIVITY("Membership API: Change the TTL of a given identifier in SystemCache");
819 xpc_dictionary_set_int64(payload
, "type", id_type
);
820 xpc_dictionary_set_data(payload
, "identifier", identifier
, identifier_size
);
821 xpc_dictionary_set_int64(payload
, "ttl", seconds
);
824 reply
= _od_rpc_call("mbr_set_identifier_ttl", payload
, _mbr_xpc_pipe
);
826 rc
= (int) xpc_dictionary_get_int64(reply
, "error");
833 xpc_release(payload
);