1 /* Copyright (c) 2012-2013 Apple Inc. All Rights Reserved. */
9 #include "authutilities.h"
11 #include "mechanism.h"
13 #include "authitems.h"
14 #include "debugging.h"
16 #include "connection.h"
17 #include "AuthorizationTags.h"
19 #include <bsm/libbsm.h>
20 #include <Security/Authorization.h>
21 #include <Security/AuthorizationPriv.h>
22 #include <Security/AuthorizationTagsPriv.h>
23 #include <Security/AuthorizationPlugin.h>
24 #include <xpc/private.h>
25 #include <dispatch/dispatch.h>
26 #include <CoreFoundation/CoreFoundation.h>
27 #include <CoreFoundation/CFXPCBridge.h>
28 #include <IOKit/IOMessage.h>
29 #include <IOKit/pwr_mgt/IOPMLib.h>
30 #include <IOKit/pwr_mgt/IOPMLibPrivate.h>
34 #define MAX_PROCESS_RIGHTS 100
36 static CFMutableDictionaryRef gProcessMap
= NULL
;
37 static CFMutableDictionaryRef gSessionMap
= NULL
;
38 static CFMutableDictionaryRef gAuthTokenMap
= NULL
;
39 static authdb_t gDatabase
= NULL
;
41 static bool gXPCTransaction
= false;
43 static dispatch_queue_t
44 get_server_dispatch_queue()
46 static dispatch_once_t onceToken
;
47 static dispatch_queue_t server_queue
= NULL
;
49 dispatch_once(&onceToken
, ^{
50 server_queue
= dispatch_queue_create("com.apple.security.auth.server", DISPATCH_QUEUE_SERIAL
);
51 check(server_queue
!= NULL
);
57 static Boolean
_processEqualCallBack(const void *value1
, const void *value2
)
59 audit_info_s
* info1
= (audit_info_s
*)value1
;
60 audit_info_s
* info2
= (audit_info_s
*)value2
;
61 if (info1
->pid
== info2
->pid
) {
62 if (info1
->tid
== info2
->tid
) {
69 static CFHashCode
_processHashCallBack(const void *value
)
71 audit_info_s
* info
= (audit_info_s
*)value
;
72 uint64_t crc
= crc64_init();
73 crc
= crc64_update(crc
, &info
->pid
, sizeof(info
->pid
));
74 crc
= crc64_update(crc
, &info
->tid
, sizeof(info
->tid
));
75 crc
= crc64_final(crc
);
76 return (CFHashCode
)crc
;
79 static const CFDictionaryKeyCallBacks kProcessMapKeyCallBacks
= {
83 .copyDescription
= NULL
,
84 .equal
= &_processEqualCallBack
,
85 .hash
= &_processHashCallBack
88 static Boolean
_sessionEqualCallBack(const void *value1
, const void *value2
)
90 return (*(session_id_t
*)value1
) == (*(session_id_t
*)value2
);
93 static CFHashCode
_sessionHashCallBack(const void *value
)
95 return (CFHashCode
)(*(session_id_t
*)(value
));
98 static const CFDictionaryKeyCallBacks kSessionMapKeyCallBacks
= {
102 .copyDescription
= NULL
,
103 .equal
= &_sessionEqualCallBack
,
104 .hash
= &_sessionHashCallBack
107 void server_cleanup()
109 CFRelease(gProcessMap
);
110 CFRelease(gSessionMap
);
111 CFRelease(gAuthTokenMap
);
113 dispatch_queue_t queue
= get_server_dispatch_queue();
115 dispatch_release(queue
);
119 bool server_in_dark_wake()
121 return IOPMIsADarkWake(IOPMConnectionGetSystemCapabilities());
124 authdb_t
server_get_database()
129 static void _setupAuditSessionMonitor()
131 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0), ^{
132 au_sdev_handle_t
*dev
= au_sdev_open(AU_SDEVF_ALLSESSIONS
);
134 auditinfo_addr_t aia
;
137 os_log_error(AUTHD_LOG
, "server: could not open %{public}s %d", AUDIT_SDEV_PATH
, errno
);
142 if (0 != au_sdev_read_aia(dev
, &event
, &aia
)) {
143 os_log_error(AUTHD_LOG
, "server: au_sdev_read_aia failed: %d", errno
);
146 os_log_debug(AUTHD_LOG
, "server: au_sdev_handle_t event=%i, session=%i", event
, aia
.ai_asid
);
147 if (event
== AUE_SESSION_END
) {
148 dispatch_async(get_server_dispatch_queue(), ^{
149 os_log_debug(AUTHD_LOG
, "server: session %i destroyed", aia
.ai_asid
);
150 CFDictionaryRemoveValue(gSessionMap
, &aia
.ai_asid
);
158 static void _setupSignalHandlers()
160 signal(SIGTERM
, SIG_IGN
);
161 static dispatch_source_t sigtermHandler
;
162 sigtermHandler
= dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL
, SIGTERM
, 0, get_server_dispatch_queue());
163 if (sigtermHandler
) {
164 dispatch_source_set_event_handler(sigtermHandler
, ^{
166 // should we clean up any state?
169 dispatch_resume(sigtermHandler
);
173 OSStatus
server_init(void)
175 OSStatus status
= errAuthorizationSuccess
;
177 auditinfo_addr_t info
;
178 memset(&info
, 0, sizeof(info
));
179 getaudit_addr(&info
, sizeof(info
));
180 os_log_debug(AUTHD_LOG
, "server: uid=%i, sid=%i", info
.ai_auid
, info
.ai_asid
);
182 require_action(get_server_dispatch_queue() != NULL
, done
, status
= errAuthorizationInternal
);
184 gProcessMap
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kProcessMapKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
185 require_action(gProcessMap
!= NULL
, done
, status
= errAuthorizationInternal
);
187 gSessionMap
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kSessionMapKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
188 require_action(gSessionMap
!= NULL
, done
, status
= errAuthorizationInternal
);
190 gAuthTokenMap
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kAuthTokenKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
191 require_action(gAuthTokenMap
!= NULL
, done
, status
= errAuthorizationInternal
);
193 gDatabase
= authdb_create();
194 require_action(gDatabase
!= NULL
, done
, status
= errAuthorizationInternal
);
196 // check to see if we have an updates
197 authdb_connection_t dbconn
= authdb_connection_acquire(gDatabase
);
198 authdb_maintenance(dbconn
);
199 authdb_connection_release(&dbconn
);
201 _setupAuditSessionMonitor();
202 _setupSignalHandlers();
208 static void _server_parse_audit_token(audit_token_t
* token
, audit_info_s
* info
)
211 memset(info
, 0, sizeof(*info
));
213 memset(&tid
, 0, sizeof(tid
));
214 audit_token_to_au32(*token
, &info
->auid
, &info
->euid
,
215 &info
->egid
, &info
->ruid
, &info
->rgid
,
216 &info
->pid
, &info
->asid
, &tid
);
217 info
->tid
= tid
.port
;
218 info
->opaqueToken
= *token
;
223 server_register_connection(xpc_connection_t connection
)
225 __block connection_t conn
= NULL
;
226 __block session_t session
= NULL
;
227 __block process_t proc
= NULL
;
228 __block CFIndex conn_count
= 0;
230 require(connection
!= NULL
, done
);
232 audit_token_t auditToken
;
234 xpc_connection_get_audit_token(connection
, &auditToken
);
235 _server_parse_audit_token(&auditToken
, &info
);
238 dispatch_sync(get_server_dispatch_queue(), ^{
239 session
= (session_t
)CFDictionaryGetValue(gSessionMap
, &info
.asid
);
243 session
= session_create(info
.asid
);
244 CFDictionarySetValue(gSessionMap
, session_get_key(session
), session
);
247 proc
= (process_t
)CFDictionaryGetValue(gProcessMap
, &info
);
253 conn
= connection_create(proc
);
254 conn_count
= process_add_connection(proc
, conn
);
256 proc
= process_create(&info
, session
);
258 conn
= connection_create(proc
);
259 conn_count
= process_add_connection(proc
, conn
);
260 session_add_process(session
, proc
);
261 CFDictionarySetValue(gProcessMap
, process_get_key(proc
), proc
);
265 if (!gXPCTransaction
) {
266 xpc_transaction_begin();
267 gXPCTransaction
= true;
271 os_log_debug(AUTHD_LOG
, "server: registered connection (total=%li)", conn_count
);
274 CFReleaseSafe(session
);
280 server_unregister_connection(connection_t conn
)
282 assert(conn
); // marked non-null
283 process_t proc
= connection_get_process(conn
);
285 dispatch_sync(get_server_dispatch_queue(), ^{
286 CFIndex connectionCount
= process_get_connection_count(proc
);
287 os_log_debug(AUTHD_LOG
, "server: unregistered connection (total=%li)", connectionCount
);
289 if (connectionCount
== 1) {
290 CFDictionaryRemoveValue(gProcessMap
, process_get_key(proc
));
293 if (CFDictionaryGetCount(gProcessMap
) == 0) {
294 xpc_transaction_end();
295 gXPCTransaction
= false;
298 // move the destruction of the connection/process off the server queue
303 server_register_auth_token(auth_token_t auth
)
305 assert(auth
); // marked non-null
306 dispatch_sync(get_server_dispatch_queue(), ^{
307 os_log_debug(AUTHD_LOG
, "server: registering authorization");
308 CFDictionarySetValue(gAuthTokenMap
, auth_token_get_key(auth
), auth
);
309 auth_token_set_state(auth
, auth_token_state_registered
);
314 server_unregister_auth_token(auth_token_t auth
)
317 AuthorizationBlob blob
= *(AuthorizationBlob
*)auth_token_get_key(auth
);
318 dispatch_async(get_server_dispatch_queue(), ^{
319 os_log_debug(AUTHD_LOG
, "server: unregistering authorization");
320 CFDictionaryRemoveValue(gAuthTokenMap
, &blob
);
325 server_find_copy_auth_token(AuthorizationBlob
* blob
)
327 assert(blob
); // marked non-null
328 __block auth_token_t auth
= NULL
;
329 dispatch_sync(get_server_dispatch_queue(), ^{
330 auth
= (auth_token_t
)CFDictionaryGetValue(gAuthTokenMap
, blob
);
339 server_find_copy_session(session_id_t sid
, bool create
)
341 __block session_t session
= NULL
;
343 dispatch_sync(get_server_dispatch_queue(), ^{
344 session
= (session_t
)CFDictionaryGetValue(gSessionMap
, &sid
);
348 session
= session_create(sid
);
350 CFDictionarySetValue(gSessionMap
, session_get_key(session
), session
);
362 _process_find_copy_auth_token_from_xpc(process_t proc
, xpc_object_t message
, auth_token_t
* auth_out
)
364 OSStatus status
= errAuthorizationSuccess
;
365 require_action(auth_out
!= NULL
, done
, status
= errAuthorizationInternal
);
368 AuthorizationBlob
* blob
= (AuthorizationBlob
*)xpc_dictionary_get_data(message
, AUTH_XPC_BLOB
, &len
);
369 require_action(blob
!= NULL
, done
, status
= errAuthorizationInvalidRef
);
370 require_action(len
== sizeof(AuthorizationBlob
), done
, status
= errAuthorizationInvalidRef
);
372 auth_token_t auth
= process_find_copy_auth_token(proc
, blob
);
373 require_action(auth
!= NULL
, done
, status
= errAuthorizationInvalidRef
);
376 os_log_debug(AUTHD_LOG
, "server: authtoken lookup %#x%x %p", blob
->data
[1],blob
->data
[0], auth
);
378 os_log_debug(AUTHD_LOG
, "server: authtoken lookup");
387 static OSStatus
_server_preauthorize(connection_t conn
, auth_token_t auth
, auth_items_t context
, engine_t
* engine_out
)
389 __block OSStatus status
= errAuthorizationDenied
;
390 engine_t engine
= NULL
;
392 require_action(conn
, done
, status
= errAuthorizationInternal
);
394 engine
= engine_create(conn
, auth
);
395 require_action(engine
, done
, status
= errAuthorizationInternal
);
397 status
= engine_preauthorize(engine
, context
);
402 *engine_out
= engine
;
410 static OSStatus
_server_authorize(connection_t conn
, auth_token_t auth
, AuthorizationFlags flags
, auth_rights_t rights
, auth_items_t environment
, engine_t
* engine_out
)
412 __block OSStatus status
= errAuthorizationDenied
;
413 engine_t engine
= NULL
;
415 require_action(conn
, done
, status
= errAuthorizationInternal
);
417 engine
= engine_create(conn
, auth
);
418 require_action(engine
, done
, status
= errAuthorizationInternal
);
420 if (flags
& kAuthorizationFlagInteractionAllowed
) {
421 dispatch_sync(connection_get_dispatch_queue(conn
), ^{
422 connection_set_engine(conn
, engine
);
423 status
= engine_authorize(engine
, rights
, environment
, flags
);
424 connection_set_engine(conn
, NULL
);
427 status
= engine_authorize(engine
, rights
, environment
, flags
);
433 *engine_out
= engine
;
441 // IN: AUTH_XPC_RIGHTS, AUTH_XPC_ENVIRONMENT, AUTH_XPC_FLAGS
442 // OUT: AUTH_XPC_BLOB
444 authorization_create(connection_t conn
, xpc_object_t message
, xpc_object_t reply
)
446 OSStatus status
= errAuthorizationDenied
;
448 process_t proc
= connection_get_process(conn
);
451 auth_rights_t rights
= auth_rights_create_with_xpc(xpc_dictionary_get_value(message
, AUTH_XPC_RIGHTS
));
452 auth_items_t environment
= auth_items_create_with_xpc(xpc_dictionary_get_value(message
, AUTH_XPC_ENVIRONMENT
));
453 AuthorizationFlags flags
= (AuthorizationFlags
)xpc_dictionary_get_uint64(message
, AUTH_XPC_FLAGS
);
455 // Create Authorization Token
456 auth_token_t auth
= auth_token_create(proc
, flags
& kAuthorizationFlagLeastPrivileged
);
457 require_action(auth
!= NULL
, done
, status
= errAuthorizationInternal
);
459 if (!(flags
& kAuthorizationFlagNoData
)) {
460 process_add_auth_token(proc
,auth
);
463 status
= _server_authorize(conn
, auth
, flags
, rights
, environment
, NULL
);
464 require_noerr(status
, done
);
467 xpc_dictionary_set_data(reply
, AUTH_XPC_BLOB
, auth_token_get_blob(auth
), sizeof(AuthorizationBlob
));
470 CFReleaseSafe(rights
);
471 CFReleaseSafe(environment
);
476 // IN: AUTH_XPC_DATA, AUTH_XPC_ENVIRONMENT, AUTH_XPC_FLAGS
477 // OUT: AUTH_XPC_BLOB
478 OSStatus
authorization_create_with_audit_token(connection_t conn
, xpc_object_t message
, xpc_object_t reply
)
480 OSStatus status
= errAuthorizationDenied
;
481 auth_token_t auth
= NULL
;
483 process_t proc
= connection_get_process(conn
);
484 require(process_get_uid(proc
) == 0, done
); //only root can use this call
488 const char * data
= xpc_dictionary_get_data(message
, AUTH_XPC_DATA
, &len
);
489 require(data
!= NULL
, done
);
490 require(len
== sizeof(audit_token_t
), done
);
492 // auth_items_t environment = auth_items_create_with_xpc(xpc_dictionary_get_value(message, AUTH_XPC_ENVIRONMENT));
493 AuthorizationFlags flags
= (AuthorizationFlags
)xpc_dictionary_get_uint64(message
, AUTH_XPC_FLAGS
);
495 audit_info_s auditInfo
;
496 _server_parse_audit_token((audit_token_t
*)data
, &auditInfo
);
498 // Create Authorization Token
499 auth
= auth_token_create(proc
, flags
& kAuthorizationFlagLeastPrivileged
);
500 require_action(auth
!= NULL
, done
, status
= errAuthorizationInternal
);
502 process_add_auth_token(proc
,auth
);
505 xpc_dictionary_set_data(reply
, AUTH_XPC_BLOB
, auth_token_get_blob(auth
), sizeof(AuthorizationBlob
));
508 // CFReleaseSafe(environment);
513 // IN: AUTH_XPC_BLOB, AUTH_XPC_FLAGS
516 authorization_free(connection_t conn
, xpc_object_t message
, xpc_object_t reply AUTH_UNUSED
)
518 OSStatus status
= errAuthorizationSuccess
;
519 AuthorizationFlags flags
= 0;
520 process_t proc
= connection_get_process(conn
);
522 auth_token_t auth
= NULL
;
523 status
= _process_find_copy_auth_token_from_xpc(proc
, message
, &auth
);
524 require_noerr(status
, done
);
526 flags
= (AuthorizationFlags
)xpc_dictionary_get_uint64(message
, AUTH_XPC_FLAGS
);
528 if (flags
& kAuthorizationFlagDestroyRights
) {
529 auth_token_credentials_iterate(auth
, ^bool(credential_t cred
) {
530 credential_invalidate(cred
);
531 os_log_debug(AUTHD_LOG
, "engine[%i]: invalidating %{public}scredential %{public}s (%i)", connection_get_pid(conn
), credential_get_shared(cred
) ? "shared " : "", credential_get_name(cred
), credential_get_uid(cred
));
535 session_credentials_purge(auth_token_get_session(auth
));
538 process_remove_auth_token(proc
, auth
, flags
);
542 os_log_debug(AUTHD_LOG
, "server: AuthorizationFree %d (flags:%x)", (int)status
, (unsigned int)flags
);
546 // IN: AUTH_XPC_BLOB, AUTH_XPC_DATA
549 authorization_preauthorize_credentials(connection_t conn
, xpc_object_t message
, xpc_object_t reply
)
551 OSStatus status
= errAuthorizationDenied
;
552 engine_t engine
= NULL
;
554 process_t proc
= connection_get_process(conn
);
557 auth_items_t context
= auth_items_create_with_xpc(xpc_dictionary_get_value(message
, AUTH_XPC_DATA
));
559 auth_token_t auth
= NULL
;
560 status
= _process_find_copy_auth_token_from_xpc(proc
, message
, &auth
);
561 require_noerr_action_quiet(status
, done
, os_log_error(AUTHD_LOG
, "preauthorize_credentials: no auth token"));
563 status
= _server_preauthorize(conn
, auth
, context
, &engine
);
564 require_noerr_action_quiet(status
, done
, os_log_error(AUTHD_LOG
, "preauthorize_credentials: authorization failed"));
567 CFReleaseSafe(context
);
569 CFReleaseSafe(engine
);
575 // IN: AUTH_XPC_BLOB, AUTH_XPC_RIGHTS, AUTH_XPC_ENVIRONMENT, AUTH_XPC_FLAGS
576 // OUT: AUTH_XPC_OUT_ITEMS
578 authorization_copy_rights(connection_t conn
, xpc_object_t message
, xpc_object_t reply
)
580 OSStatus status
= errAuthorizationDenied
;
581 engine_t engine
= NULL
;
583 process_t proc
= connection_get_process(conn
);
586 auth_rights_t rights
= auth_rights_create_with_xpc(xpc_dictionary_get_value(message
, AUTH_XPC_RIGHTS
));
587 auth_items_t environment
= auth_items_create_with_xpc(xpc_dictionary_get_value(message
, AUTH_XPC_ENVIRONMENT
));
588 AuthorizationFlags flags
= (AuthorizationFlags
)xpc_dictionary_get_uint64(message
, AUTH_XPC_FLAGS
);
590 auth_token_t auth
= NULL
;
591 status
= _process_find_copy_auth_token_from_xpc(proc
, message
, &auth
);
592 require_noerr_action_quiet(status
, done
, os_log_error(AUTHD_LOG
, "copy_rights: no auth token"));
594 status
= _server_authorize(conn
, auth
, flags
, rights
, environment
, &engine
);
595 require_noerr_action_quiet(status
, done
, os_log_error(AUTHD_LOG
, "copy_rights: authorization failed"));
598 xpc_object_t outItems
= auth_rights_export_xpc(engine_get_granted_rights(engine
));
599 xpc_dictionary_set_value(reply
, AUTH_XPC_OUT_ITEMS
, outItems
);
600 xpc_release_safe(outItems
);
603 CFReleaseSafe(rights
);
604 CFReleaseSafe(environment
);
606 CFReleaseSafe(engine
);
611 // IN: AUTH_XPC_BLOB, AUTH_XPC_TAG
612 // OUT: AUTH_XPC_OUT_ITEMS
614 authorization_copy_info(connection_t conn
, xpc_object_t message
, xpc_object_t reply
)
617 OSStatus status
= errAuthorizationSuccess
;
618 auth_items_t items
= NULL
;
619 auth_items_t local_items
= NULL
;
620 const char * tag
= NULL
;
622 process_t proc
= connection_get_process(conn
);
624 auth_token_t auth
= NULL
;
625 status
= _process_find_copy_auth_token_from_xpc(proc
, message
, &auth
);
626 require_noerr_action_quiet(status
, done
, os_log_error(AUTHD_LOG
, "copy_info: no auth token"));
628 items
= auth_items_create();
630 tag
= xpc_dictionary_get_string(message
, AUTH_XPC_TAG
);
631 os_log_debug(AUTHD_LOG
, "server: requested tag: %{public}s", tag
? tag
: "(all)");
634 const void * data
= auth_items_get_data_with_flags(auth_token_get_context(auth
), tag
, &len
, kAuthorizationContextFlagExtractable
);
636 os_log_debug(AUTHD_LOG
, "server: requested tag found");
637 auth_items_set_data(items
, tag
, data
, len
);
640 auth_items_copy_with_flags(items
, auth_token_get_context(auth
), kAuthorizationContextFlagExtractable
);
643 local_items
= auth_items_create();
644 auth_items_content_copy(local_items
, items
); // we do not want decrypt content of the authorizationref memory which is where pointers point to
645 auth_items_decrypt(local_items
, auth_token_get_encryption_key(auth
));
646 os_log_debug(AUTHD_LOG
, "server: decrypted authorization context data");
649 os_log_debug(AUTHD_LOG
, "server: Dumping requested AuthRef items: %{public}@", items
);
652 if (auth_items_exist(local_items
, kAuthorizationEnvironmentPassword
)) {
653 // check if caller is entitled to get the password
654 CFTypeRef extract_password_entitlement
= process_copy_entitlement_value(proc
, "com.apple.authorization.extract-password");
655 if (extract_password_entitlement
&& (CFGetTypeID(extract_password_entitlement
) == CFBooleanGetTypeID()) && extract_password_entitlement
== kCFBooleanTrue
) {
656 os_log_debug(AUTHD_LOG
, "server: caller allowed to extract password");
658 os_log_error(AUTHD_LOG
, "server: caller NOT allowed to extract password");
659 auth_items_remove(local_items
, kAuthorizationEnvironmentPassword
);
661 CFReleaseSafe(extract_password_entitlement
);
665 xpc_object_t outItems
= auth_items_export_xpc(local_items
);
666 xpc_dictionary_set_value(reply
, AUTH_XPC_OUT_ITEMS
, outItems
);
667 xpc_release_safe(outItems
);
670 CFReleaseSafe(local_items
);
671 CFReleaseSafe(items
);
673 os_log_debug(AUTHD_LOG
, "server: AuthorizationCopyInfo %i", (int) status
);
678 // OUT: AUTH_XPC_EXTERNAL
680 authorization_make_external_form(connection_t conn
, xpc_object_t message
, xpc_object_t reply
)
682 OSStatus status
= errAuthorizationSuccess
;
684 process_t proc
= connection_get_process(conn
);
686 auth_token_t auth
= NULL
;
687 status
= _process_find_copy_auth_token_from_xpc(proc
, message
, &auth
);
688 require_noerr(status
, done
);
690 AuthorizationExternalForm exForm
;
691 AuthorizationExternalBlob
* exBlob
= (AuthorizationExternalBlob
*)&exForm
;
692 memset(&exForm
, 0, sizeof(exForm
));
694 exBlob
->blob
= *auth_token_get_blob(auth
);
695 exBlob
->session
= process_get_session_id(proc
);
697 xpc_dictionary_set_data(reply
, AUTH_XPC_EXTERNAL
, &exForm
, sizeof(exForm
));
698 server_register_auth_token(auth
);
702 os_log_debug(AUTHD_LOG
, "server: AuthorizationMakeExternalForm %d", (int)status
);
706 // IN: AUTH_XPC_EXTERNAL
707 // OUT: AUTH_XPC_BLOB
709 authorization_create_from_external_form(connection_t conn
, xpc_object_t message
, xpc_object_t reply
)
711 OSStatus status
= errAuthorizationSuccess
;
712 auth_token_t auth
= NULL
;
714 process_t proc
= connection_get_process(conn
);
717 AuthorizationExternalForm
* exForm
= (AuthorizationExternalForm
*)xpc_dictionary_get_data(message
, AUTH_XPC_EXTERNAL
, &len
);
718 require_action(exForm
!= NULL
, done
, status
= errAuthorizationInternal
);
719 require_action(len
== sizeof(AuthorizationExternalForm
), done
, status
= errAuthorizationInvalidRef
);
721 AuthorizationExternalBlob
* exBlob
= (AuthorizationExternalBlob
*)exForm
;
722 auth
= server_find_copy_auth_token(&exBlob
->blob
);
723 require_action(auth
!= NULL
, done
, status
= errAuthorizationDenied
);
725 process_add_auth_token(proc
, auth
);
726 xpc_dictionary_set_data(reply
, AUTH_XPC_BLOB
, auth_token_get_blob(auth
), sizeof(AuthorizationBlob
));
730 os_log_debug(AUTHD_LOG
, "server: AuthorizationCreateFromExternalForm %d", (int)status
);
734 // IN: AUTH_XPC_RIGHT_NAME
735 // OUT: AUTH_XPC_DATA
737 authorization_right_get(connection_t conn AUTH_UNUSED
, xpc_object_t message
, xpc_object_t reply
)
739 OSStatus status
= errAuthorizationDenied
;
741 CFTypeRef cfdict
= NULL
;
742 xpc_object_t xpcdict
= NULL
;
744 authdb_connection_t dbconn
= authdb_connection_acquire(server_get_database());
745 rule
= rule_create_with_string(xpc_dictionary_get_string(message
, AUTH_XPC_RIGHT_NAME
), dbconn
);
746 require(rule
!= NULL
, done
);
747 require(rule_get_id(rule
) != 0, done
);
749 cfdict
= rule_copy_to_cfobject(rule
, dbconn
);
750 require(cfdict
!= NULL
, done
);
752 xpcdict
= _CFXPCCreateXPCObjectFromCFObject(cfdict
);
753 require(xpcdict
!= NULL
, done
);
756 xpc_dictionary_set_value(reply
, AUTH_XPC_DATA
, xpcdict
);
758 status
= errAuthorizationSuccess
;
761 authdb_connection_release(&dbconn
);
762 CFReleaseSafe(cfdict
);
763 xpc_release_safe(xpcdict
);
765 os_log_debug(AUTHD_LOG
, "server: AuthorizationRightGet %d", (int)status
);
769 static bool _prompt_for_modifications(process_t __unused proc
, rule_t __unused rule
)
771 // <rdar://problem/13853228> will put back it back at some later date
772 // SecRequirementRef ruleReq = rule_get_requirement(rule);
774 // if (ruleReq && process_verify_requirment(proc, ruleReq)) {
781 static CFIndex
_get_mechanism_index(CFArrayRef mechanisms
, CFStringRef m_name
)
784 require(mechanisms
, done
);
786 CFIndex c
= CFArrayGetCount(mechanisms
);
787 CFStringRef i_name
= NULL
;
788 for (CFIndex i
= 0; i
< c
; ++i
)
790 i_name
= CFArrayGetValueAtIndex(mechanisms
, i
);
791 if (i_name
&& (CFGetTypeID(m_name
) == CFStringGetTypeID())) {
792 if (CFStringCompare(i_name
, m_name
, kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
803 static bool _update_rule_mechanism(authdb_connection_t dbconn
, const char * rule_name
, CFStringRef mechanism_name
, CFStringRef insert_after_name
, bool remove
)
805 bool updated
= false;
807 rule_t update_rule
= NULL
;
808 CFMutableDictionaryRef cfdict
= NULL
;
809 CFStringRef update_name
= NULL
;
811 require(mechanism_name
, done
);
813 rule
= rule_create_with_string(rule_name
, dbconn
);
814 require(rule_get_id(rule
) != 0, done
); // rule doesn't exist in the database
816 cfdict
= rule_copy_to_cfobject(rule
, dbconn
);
817 require(cfdict
!= NULL
, done
);
819 CFMutableArrayRef mechanisms
= NULL
;
820 bool res
= CFDictionaryGetValueIfPresent(cfdict
, CFSTR(kAuthorizationRuleParameterMechanisms
), (void*)&mechanisms
);
821 require(res
== true, done
);
826 index
= _get_mechanism_index(mechanisms
, mechanism_name
);
828 if (insert_after_name
) {
829 if ((index
= _get_mechanism_index(mechanisms
, insert_after_name
)) != -1) {
832 index
= 0; // if we couldn't find the index add it to the begining
841 CFArrayRemoveValueAtIndex(mechanisms
, index
);
843 if (index
< CFArrayGetCount(mechanisms
)) {
844 require_action(CFStringCompare(CFArrayGetValueAtIndex(mechanisms
, index
), mechanism_name
, kCFCompareCaseInsensitive
) != kCFCompareEqualTo
, done
, updated
= true);
846 CFArrayInsertValueAtIndex(mechanisms
, index
, mechanism_name
);
849 CFDictionarySetValue(cfdict
, CFSTR(kAuthorizationRuleParameterMechanisms
), mechanisms
);
852 update_name
= CFStringCreateWithCString(kCFAllocatorDefault
, rule_name
, kCFStringEncodingUTF8
);
853 require(update_name
, done
);
854 update_rule
= rule_create_with_plist(rule_get_type(rule
), update_name
, cfdict
, dbconn
);
855 require(update_rule
, done
);
857 require(rule_sql_commit(update_rule
, dbconn
, CFAbsoluteTimeGetCurrent(), NULL
), done
);
864 CFReleaseSafe(update_rule
);
865 CFReleaseSafe(cfdict
);
866 CFReleaseSafe(update_name
);
870 /// IN: AUTH_XPC_BLOB, AUTH_XPC_INT64
873 authorization_enable_smartcard(connection_t conn
, xpc_object_t message
, xpc_object_t reply AUTH_UNUSED
)
875 const CFStringRef SMARTCARD_LINE
= CFSTR("builtin:smartcard-sniffer,privileged");
876 const CFStringRef BUILTIN_LINE
= CFSTR("builtin:policy-banner");
877 const char* SYSTEM_LOGIN_CONSOLE
= "system.login.console";
878 const char* AUTHENTICATE
= "authenticate";
880 __block OSStatus status
= errAuthorizationSuccess
;
881 bool enable_smartcard
= false;
882 authdb_connection_t dbconn
= NULL
;
883 auth_token_t auth
= NULL
;
884 auth_rights_t checkRight
= NULL
;
886 process_t proc
= connection_get_process(conn
);
888 status
= _process_find_copy_auth_token_from_xpc(proc
, message
, &auth
);
889 require_noerr(status
, done
);
891 checkRight
= auth_rights_create();
892 auth_rights_add(checkRight
, "config.modify.smartcard");
893 status
= _server_authorize(conn
, auth
, kAuthorizationFlagDefaults
| kAuthorizationFlagInteractionAllowed
| kAuthorizationFlagExtendRights
, checkRight
, NULL
, NULL
);
894 require_noerr(status
, done
);
896 enable_smartcard
= xpc_dictionary_get_bool(message
, AUTH_XPC_DATA
);
898 dbconn
= authdb_connection_acquire(server_get_database());
900 if (!_update_rule_mechanism(dbconn
, SYSTEM_LOGIN_CONSOLE
, SMARTCARD_LINE
, BUILTIN_LINE
, enable_smartcard
? false : true)) {
901 status
= errAuthorizationInternal
;
902 os_log_error(AUTHD_LOG
, "server: smartcard: enable(%i) failed to update %{public}s", enable_smartcard
, SYSTEM_LOGIN_CONSOLE
);
904 if (!_update_rule_mechanism(dbconn
, AUTHENTICATE
, SMARTCARD_LINE
, NULL
, enable_smartcard
? false : true)) {
905 status
= errAuthorizationInternal
;
906 os_log_error(AUTHD_LOG
, "server: smartcard: enable(%i) failed to update %{public}s", enable_smartcard
, AUTHENTICATE
);
909 authdb_checkpoint(dbconn
);
912 authdb_connection_release(&dbconn
);
913 CFReleaseSafe(checkRight
);
918 static int64_t _process_get_identifier_count(process_t proc
, authdb_connection_t conn
)
920 __block
int64_t result
= 0;
922 authdb_step(conn
, "SELECT COUNT(*) AS cnt FROM rules WHERE identifier = ? ", ^(sqlite3_stmt
*stmt
) {
923 sqlite3_bind_text(stmt
, 1, process_get_identifier(proc
), -1, NULL
);
924 }, ^bool(auth_items_t data
) {
925 result
= auth_items_get_int64(data
, "cnt");
932 static int64_t _get_max_process_rights()
934 static dispatch_once_t onceToken
;
935 static int64_t max_rights
= MAX_PROCESS_RIGHTS
;
937 //sudo defaults write /Library/Preferences/com.apple.authd max_process_rights -bool true
938 dispatch_once(&onceToken
, ^{
939 CFTypeRef max
= (CFNumberRef
)CFPreferencesCopyValue(CFSTR("max_process_rights"), CFSTR(SECURITY_AUTH_NAME
), kCFPreferencesAnyUser
, kCFPreferencesCurrentHost
);
941 if (max
&& CFGetTypeID(max
) == CFNumberGetTypeID()) {
942 CFNumberGetValue(max
, kCFNumberSInt64Type
, &max_rights
);
950 // IN: AUTH_XPC_BLOB, AUTH_XPC_RIGHT_NAME, AUTH_XPC_DATA
953 authorization_right_set(connection_t conn
, xpc_object_t message
, xpc_object_t reply AUTH_UNUSED
)
955 __block OSStatus status
= errAuthorizationDenied
;
956 __block engine_t engine
= NULL
;
957 CFStringRef cf_rule_name
= NULL
;
958 CFDictionaryRef cf_rule_dict
= NULL
;
960 rule_t existingRule
= NULL
;
961 authdb_connection_t dbconn
= NULL
;
962 auth_token_t auth
= NULL
;
963 bool force_modify
= false;
964 RuleType rule_type
= RT_RIGHT
;
965 const char * rule_name
= NULL
;
966 bool auth_rule
= false;
968 process_t proc
= connection_get_process(conn
);
970 status
= _process_find_copy_auth_token_from_xpc(proc
, message
, &auth
);
971 require_noerr(status
, done
);
973 require_action(xpc_dictionary_get_string(message
, AUTH_XPC_RIGHT_NAME
) != NULL
, done
, status
= errAuthorizationInternal
);
974 require_action(xpc_dictionary_get_value(message
, AUTH_XPC_DATA
) != NULL
, done
, status
= errAuthorizationInternal
);
976 rule_name
= xpc_dictionary_get_string(message
, AUTH_XPC_RIGHT_NAME
);
977 require(rule_name
!= NULL
, done
);
979 if (_compare_string(rule_name
, "authenticate")) {
984 cf_rule_name
= CFStringCreateWithCString(kCFAllocatorDefault
, rule_name
, kCFStringEncodingUTF8
);
985 require(cf_rule_name
!= NULL
, done
);
987 cf_rule_dict
= _CFXPCCreateCFObjectFromXPCObject(xpc_dictionary_get_value(message
, AUTH_XPC_DATA
));
988 require(cf_rule_dict
!= NULL
, done
);
990 dbconn
= authdb_connection_acquire(server_get_database());
992 rule
= rule_create_with_plist(rule_type
, cf_rule_name
, cf_rule_dict
, dbconn
);
993 if (process_get_uid(proc
) != 0) {
994 require_action(rule_get_extract_password(rule
) == false, done
, status
= errAuthorizationDenied
; os_log_error(AUTHD_LOG
, "server: AuthorizationRightSet not allowed to set extract-password. (denied)"));
997 // if rule doesn't currently exist then we have to check to see if they are over the Max.
998 if (rule_get_id(rule
) == 0) {
999 if (process_get_identifier(proc
) == NULL
) {
1000 os_log_error(AUTHD_LOG
, "server: AuthorizationRightSet required for process %{public}s (missing code signature). To add rights to the Authorization database, your process must have a code signature.", process_get_code_url(proc
));
1001 force_modify
= true;
1003 int64_t process_rule_count
= _process_get_identifier_count(proc
, dbconn
);
1004 if ((process_rule_count
>= _get_max_process_rights())) {
1005 if (!connection_get_syslog_warn(conn
)) {
1006 os_log_error(AUTHD_LOG
, "server: AuthorizationRightSet Denied API abuse process %{public}s already contains %lli rights.", process_get_code_url(proc
), _get_max_process_rights());
1007 connection_set_syslog_warn(conn
);
1009 status
= errAuthorizationDenied
;
1015 if (process_get_uid(proc
) != 0) {
1016 os_log_error(AUTHD_LOG
, "server: AuthorizationRightSet denied, root required to update the 'authenticate' rule");
1017 status
= errAuthorizationDenied
;
1021 // verify they are updating a right and not a rule
1022 existingRule
= rule_create_with_string(rule_get_name(rule
), dbconn
);
1023 if (rule_get_type(existingRule
) == RT_RULE
) {
1024 os_log_error(AUTHD_LOG
, "server: AuthorizationRightSet Denied updating '%{public}s' rule is prohibited", rule_get_name(existingRule
));
1025 status
= errAuthorizationDenied
;
1031 if (_prompt_for_modifications(proc
,rule
)) {
1032 authdb_connection_release(&dbconn
);
1034 dispatch_sync(connection_get_dispatch_queue(conn
), ^{
1035 engine
= engine_create(conn
, auth
);
1036 connection_set_engine(conn
, engine
);
1037 status
= engine_verify_modification(engine
, rule
, false, force_modify
);
1038 connection_set_engine(conn
, NULL
);
1040 require_noerr(status
, done
);
1042 dbconn
= authdb_connection_acquire(server_get_database());
1045 if (rule_sql_commit(rule
, dbconn
, engine
? engine_get_time(engine
) : CFAbsoluteTimeGetCurrent(), proc
)) {
1046 os_log_debug(AUTHD_LOG
, "server: Successfully updated rule %{public}s", rule_get_name(rule
));
1047 authdb_checkpoint(dbconn
);
1048 status
= errAuthorizationSuccess
;
1050 os_log_error(AUTHD_LOG
, "server: Failed to update rule %{public}s", rule_get_name(rule
));
1051 status
= errAuthorizationDenied
;
1055 authdb_connection_release(&dbconn
);
1056 CFReleaseSafe(existingRule
);
1057 CFReleaseSafe(cf_rule_name
);
1058 CFReleaseSafe(cf_rule_dict
);
1059 CFReleaseSafe(auth
);
1060 CFReleaseSafe(rule
);
1061 CFReleaseSafe(engine
);
1065 // IN: AUTH_XPC_BLOB, AUTH_XPC_RIGHT_NAME
1068 authorization_right_remove(connection_t conn
, xpc_object_t message
, xpc_object_t reply AUTH_UNUSED
)
1070 __block OSStatus status
= errAuthorizationDenied
;
1071 __block engine_t engine
= NULL
;
1073 authdb_connection_t dbconn
= NULL
;
1075 process_t proc
= connection_get_process(conn
);
1077 auth_token_t auth
= NULL
;
1078 status
= _process_find_copy_auth_token_from_xpc(proc
, message
, &auth
);
1079 require_noerr(status
, done
);
1081 dbconn
= authdb_connection_acquire(server_get_database());
1083 rule
= rule_create_with_string(xpc_dictionary_get_string(message
, AUTH_XPC_RIGHT_NAME
), dbconn
);
1084 require(rule
!= NULL
, done
);
1086 if (_prompt_for_modifications(proc
,rule
)) {
1087 authdb_connection_release(&dbconn
);
1089 dispatch_sync(connection_get_dispatch_queue(conn
), ^{
1090 engine
= engine_create(conn
, auth
);
1091 connection_set_engine(conn
, engine
);
1092 status
= engine_verify_modification(engine
, rule
, true, false);
1093 connection_set_engine(conn
, NULL
);
1095 require_noerr(status
, done
);
1097 dbconn
= authdb_connection_acquire(server_get_database());
1100 if (rule_get_id(rule
) != 0) {
1101 rule_sql_remove(rule
, dbconn
, proc
);
1105 authdb_connection_release(&dbconn
);
1106 CFReleaseSafe(auth
);
1107 CFReleaseSafe(rule
);
1108 CFReleaseSafe(engine
);
1109 os_log_debug(AUTHD_LOG
, "server: AuthorizationRightRemove %d", (int)status
);
1114 #pragma mark test code
1117 session_set_user_preferences(connection_t conn
, xpc_object_t message
, xpc_object_t reply
)
1122 return errAuthorizationSuccess
;
1127 // rule_t rule = rule_create_with_string("system.preferences.accounts");
1128 // CFDictionaryRef dict = rule_copy_to_cfobject(rule);
1130 // CFReleaseSafe(rule);
1131 // CFReleaseSafe(dict);
1133 // auth_items_t config = NULL;
1134 // double d2 = 0, d1 = 5;
1135 // authdb_get_key_value(server_get_authdb_reader(), "config", &config);
1136 // auth_items_set_double(config, "test", d1);
1137 // d2 = auth_items_get_double(config, "test");
1138 // os_log_debug(AUTHD_LOG, "d1=%f d2=%f", d1, d2);
1139 // CFReleaseSafe(config);
1142 // auth_items_t items = auth_items_create();
1143 // auth_items_set_string(items, "test", "testing 1");
1144 // auth_items_set_string(items, "test2", "testing 2");
1145 // auth_items_set_string(items, "test3", "testing 3");
1146 // auth_items_set_flags(items, "test3", 4);
1147 // auth_items_set_string(items, "apple", "apple");
1148 // auth_items_set_flags(items, "apple", 1);
1149 // auth_items_set_int(items, "int", 45);
1150 // auth_items_set_flags(items, "int", 2);
1151 // auth_items_set_bool(items, "true", true);
1152 // auth_items_set_bool(items, "false", false);
1153 // auth_items_set(items, "com.apple.");
1154 // auth_show(items);
1155 // LOGD("Yeah it works: %s", auth_items_get_string(items, "test3"));
1156 // LOGD("Yeah it works: %i", auth_items_get_bool(items, "true"));
1157 // LOGD("Yeah it works: %i", auth_items_get_bool(items, "false"));
1158 // LOGD("Yeah it works: %i", auth_items_get_int(items, "int"));
1159 // (void)auth_items_get_bool(items, "test3");
1160 // AuthorizationItemSet * itemSet = auth_items_get_item_set(items);
1161 // for (uint32_t i = 0; i < itemSet->count; i++) {
1162 // LOGD("item: %s", itemSet->items[i].name);
1165 // xpc_object_t xpcdata = SerializeItemSet(auth_items_get_item_set(items));
1166 // auth_items_t items2 = auth_items_create_with_xpc(xpcdata);
1167 // xpc_release(xpcdata);
1168 // auth_items_remove_with_flags(items2, 7);
1169 //// auth_items_set_string(items2, "test3", "testing 3 very good");
1170 // auth_items_copy_with_flags(items2, items, 7);
1171 // LOGD("Yeah it works: %s", auth_items_get_string(items2, "test3"));
1172 // auth_show(items2);
1173 // CFReleaseSafe(items2);
1175 // CFReleaseSafe(items);