1 /* Copyright (c) 2012-2013 Apple Inc. All Rights Reserved. */
5 #include "authd_private.h"
8 #include "authutilities.h"
11 #include "debugging.h"
14 #include <xpc/private.h>
15 #include <Security/AuthorizationPlugin.h>
17 #include <mach/mach.h>
18 #include <servers/bootstrap.h>
19 #include <bootstrap_priv.h>
21 #define SECURITYAGENT_BOOTSTRAP_NAME_BASE "com.apple.security.agent"
22 #define SECURITYAGENT_LOGINWINDOW_BOOTSTRAP_NAME_BASE "com.apple.security.agent.login"
23 #define AUTHORIZATIONHOST_BOOTSTRAP_NAME_BASE "com.apple.security.authhost"
27 #define UUID_INITIALIZER_FROM_SESSIONID(sessionid) \
28 { 0,0,0,0, 0,0,0,0, 0,0,0,0, (unsigned char)((0xff000000 & (sessionid))>>24), (unsigned char)((0x00ff0000 & (sessionid))>>16), (unsigned char)((0x0000ff00 & (sessionid))>>8), (unsigned char)((0x000000ff & (sessionid))) }
31 __AUTH_BASE_STRUCT_HEADER__
;
37 PluginState pluginState
;
40 xpc_connection_t agentConnection
;
41 dispatch_queue_t eventQueue
;
42 dispatch_queue_t actionQueue
;
48 _agent_finalize(CFTypeRef value
)
50 agent_t agent
= (agent_t
)value
;
52 // If this ever becomes a concurrent queue, then this would need to be a barrier sync
53 dispatch_sync(agent
->eventQueue
, ^{
54 // Mark the agent as dead
55 agent
->pluginState
= dead
;
58 // We're going away, which means no outside references exist. It's safe to dispose of the XPC connection.
59 if (NULL
!= agent
->agentConnection
) {
60 xpc_release(agent
->agentConnection
);
61 agent
->agentConnection
= NULL
;
64 // Now that we've released any XPC connection that may (or may not) be present
65 // it's safe to go ahead and free our memory. This is provided that all other
66 // blocks that were added to the event queue before the axe came down on the
67 // xpc connection have been executed.
69 // If this ever becomes a concurrent queue, then this would need to be a barrier sync
70 dispatch_sync(agent
->eventQueue
, ^{
71 CFReleaseNull(agent
->hints
);
72 CFReleaseNull(agent
->context
);
73 CFReleaseNull(agent
->mech
);
74 CFReleaseNull(agent
->engine
);
75 dispatch_release(agent
->actionQueue
);
78 dispatch_release(agent
->eventQueue
);
80 os_log_debug(AUTHD_LOG
, "agent: finalizing");
83 AUTH_TYPE_INSTANCE(agent
,
86 .finalize
= _agent_finalize
,
89 .copyFormattingDesc
= NULL
,
93 static CFTypeID
agent_get_type_id() {
94 static CFTypeID type_id
= _kCFRuntimeNotATypeID
;
95 static dispatch_once_t onceToken
;
97 dispatch_once(&onceToken
, ^{
98 type_id
= _CFRuntimeRegisterClass(&_auth_type_agent
);
105 agent_create(engine_t engine
, mechanism_t mech
, auth_token_t auth
, process_t proc
, bool firstMech
)
107 bool doSwitchAudit
= false;
108 bool doSwitchBootstrap
= false;
109 agent_t agent
= NULL
;
111 agent
= (agent_t
)_CFRuntimeCreateInstance(kCFAllocatorDefault
, agent_get_type_id(), AUTH_CLASS_SIZE(agent
), NULL
);
112 require(agent
!= NULL
, done
);
114 agent
->mech
= (mechanism_t
)CFRetain(mech
);
115 agent
->engine
= (engine_t
)CFRetain(engine
);
116 agent
->hints
= auth_items_create();
117 agent
->context
= auth_items_create();
118 agent
->pluginState
= init
;
119 agent
->agentPid
= process_get_pid(proc
);
121 const audit_info_s
*audit_info
= auth_token_get_audit_info(auth
);
123 auditinfo_addr_t tempAddr
;
124 tempAddr
.ai_asid
= audit_info
->asid
;
125 auditon(A_GETSINFO_ADDR
, &tempAddr
, sizeof(tempAddr
));
127 os_log_debug(AUTHD_LOG
, "agent: stored auid %d fetched auid %d", audit_info
->auid
, tempAddr
.ai_auid
);
128 uid_t auid
= tempAddr
.ai_auid
;
129 uuid_t sessionUUID
= UUID_INITIALIZER_FROM_SESSIONID((uint32_t)audit_info
->asid
);
131 agent
->eventQueue
= dispatch_queue_create("Agent Event Queue", 0);
132 agent
->actionQueue
= dispatch_queue_create("Agent Action Queue", 0);
134 if (!mechanism_is_privileged(mech
)) {
135 if (auid
!= AU_DEFAUDITID
&& audit_info
->auid
!= AU_DEFAUDITID
) {
136 // User => regular user-level SecurityAgent
137 agent
->agentConnection
= xpc_connection_create_mach_service(SECURITYAGENT_BOOTSTRAP_NAME_BASE
, NULL
, 0);
138 xpc_connection_set_target_uid(agent
->agentConnection
, auid
);
139 os_log_debug(AUTHD_LOG
, "agent: creating a standard security agent");
141 // Root session => loginwindow SecurityAgent
142 agent
->agentConnection
= xpc_connection_create_mach_service(SECURITYAGENT_LOGINWINDOW_BOOTSTRAP_NAME_BASE
, NULL
,
143 XPC_CONNECTION_MACH_SERVICE_PRIVILEGED
);
144 xpc_connection_set_instance(agent
->agentConnection
, sessionUUID
);
145 os_log_debug(AUTHD_LOG
, "agent: creating a loginwindow security agent");
146 doSwitchAudit
= true;
147 doSwitchBootstrap
= true;
150 agent
->agentConnection
= xpc_connection_create_mach_service(AUTHORIZATIONHOST_BOOTSTRAP_NAME_BASE
, NULL
, 0);
151 xpc_connection_set_instance(agent
->agentConnection
, sessionUUID
);
152 os_log_debug(AUTHD_LOG
, "agent: creating a standard authhost");
153 doSwitchAudit
= true;
154 doSwitchBootstrap
= true;
157 // **************** Here's the event handler, since I can never find it
158 xpc_connection_set_target_queue(agent
->agentConnection
, agent
->eventQueue
);
159 xpc_connection_set_event_handler(agent
->agentConnection
, ^(xpc_object_t object
) {
160 char* objectDesc
= xpc_copy_description(object
);
163 if (agent
->pluginState
== dead
) {
164 // If the agent is dead for some reason, drop this message before we hurt ourselves.
168 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
169 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
170 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_INTERRUPT
)) {
171 agent
->pluginState
= interrupting
;
172 engine_interrupt_agent(agent
->engine
);
177 xpc_connection_resume(agent
->agentConnection
);
179 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
180 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_CREATE
);
181 xpc_dictionary_set_string(requestObject
, AUTH_XPC_PLUGIN_NAME
, mechanism_get_plugin(mech
));
182 xpc_dictionary_set_string(requestObject
, AUTH_XPC_MECHANISM_NAME
, mechanism_get_param(mech
));
184 mach_port_name_t jobPort
;
185 if (audit_info
!= NULL
) {
186 if (0 == audit_session_port(audit_info
->asid
, &jobPort
)) {
187 os_log_debug(AUTHD_LOG
, "agent: attaching an audit session port");
188 xpc_dictionary_set_mach_send(requestObject
, AUTH_XPC_AUDIT_SESSION_PORT
, jobPort
);
190 if (mach_port_mod_refs(mach_task_self(), jobPort
, MACH_PORT_RIGHT_SEND
, -1) != KERN_SUCCESS
) {
191 os_log_error(AUTHD_LOG
, "agent: unable to release send right for audit session, leaking");
197 if (doSwitchBootstrap
) {
198 os_log_debug(AUTHD_LOG
, "agent: attaching a bootstrap port");
199 xpc_dictionary_set_mach_send(requestObject
, AUTH_XPC_BOOTSTRAP_PORT
, process_get_bootstrap(proc
));
202 // This loop will be repeated until we can get ahold of a SecurityAgent, or get a fatal error
203 // This will cause us to retry any CONNECTION_INTERRUPTED status messages
205 xpc_object_t object
= xpc_connection_send_message_with_reply_sync(agent
->agentConnection
, requestObject
);
207 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
208 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
209 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_CREATE
)) {
210 agent
->status
= xpc_dictionary_get_uint64(object
, AUTH_XPC_REPLY_RESULT_VALUE
);
211 if (agent
->status
== kAuthorizationResultAllow
) {
212 // Only go here if we have an "allow" from the SecurityAgent (the plugin create might have failed)
213 // This is backwards compatible so that it goes gently into the build, as the default is "allow".
214 agent
->pluginState
= created
;
216 agent
->pluginState
= dead
;
219 } else if (xpc_get_type(object
) == XPC_TYPE_ERROR
) {
220 if (object
== XPC_ERROR_CONNECTION_INVALID
) {
221 agent
->pluginState
= dead
;
225 } while ((agent
->pluginState
== init
) && (firstMech
));
227 xpc_release(requestObject
);
229 if (agent
->pluginState
!= created
) {
238 agent_run(agent_t agent
, auth_items_t hints
, auth_items_t context
, auth_items_t immutable_hints
)
240 xpc_object_t hintsArray
= auth_items_export_xpc(hints
);
241 xpc_object_t contextArray
= auth_items_export_xpc(context
);
242 xpc_object_t immutableHintsArray
= auth_items_export_xpc(immutable_hints
);
244 dispatch_semaphore_t replyWaiter
= dispatch_semaphore_create(0);
245 dispatch_sync(agent
->actionQueue
, ^{
246 if (agent
->pluginState
!= mechinterrupting
) {
247 agent
->pluginState
= current
;
248 agent
->status
= kAuthorizationResultUndefined
; // Set this while the plugin chews on the user input.
250 auth_items_clear(agent
->hints
);
251 auth_items_clear(agent
->context
);
253 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
254 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_INVOKE
);
255 xpc_dictionary_set_value(requestObject
, AUTH_XPC_HINTS_NAME
, hintsArray
);
256 xpc_dictionary_set_value(requestObject
, AUTH_XPC_CONTEXT_NAME
, contextArray
);
257 xpc_dictionary_set_value(requestObject
, AUTH_XPC_IMMUTABLE_HINTS_NAME
, immutableHintsArray
);
259 xpc_connection_send_message_with_reply(agent
->agentConnection
, requestObject
, agent
->actionQueue
, ^(xpc_object_t object
){
260 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
261 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
262 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_RESULT
)) {
263 if (agent
->pluginState
== current
) {
264 xpc_object_t xpcContext
= xpc_dictionary_get_value(object
, AUTH_XPC_CONTEXT_NAME
);
265 xpc_object_t xpcHints
= xpc_dictionary_get_value(object
, AUTH_XPC_HINTS_NAME
);
266 auth_items_copy_xpc(agent
->context
, xpcContext
);
267 auth_items_copy_xpc(agent
->hints
, xpcHints
);
268 agent
->status
= xpc_dictionary_get_uint64(object
, AUTH_XPC_REPLY_RESULT_VALUE
);
269 agent
->pluginState
= active
;
272 } else if (xpc_get_type(object
) == XPC_TYPE_ERROR
) {
273 if ((object
== XPC_ERROR_CONNECTION_INVALID
) || (object
== XPC_ERROR_CONNECTION_INTERRUPTED
)) {
274 agent
->pluginState
= dead
;
277 dispatch_semaphore_signal(replyWaiter
);
279 xpc_release(requestObject
);
283 if (agent
->pluginState
== current
) {
284 dispatch_semaphore_wait(replyWaiter
, DISPATCH_TIME_FOREVER
);
286 dispatch_release(replyWaiter
);
288 os_log_debug(AUTHD_LOG
, "agent: Finished call to SecurityAgent");
290 xpc_release(hintsArray
);
291 xpc_release(contextArray
);
292 xpc_release(immutableHintsArray
);
294 return agent
->status
;
298 agent_get_hints(agent_t agent
)
304 agent_get_context(agent_t agent
)
306 return agent
->context
;
310 agent_get_mechanism(agent_t agent
)
316 agent_deactivate(agent_t agent
)
318 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
319 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_DEACTIVATE
);
321 agent
->pluginState
= deactivating
;
323 xpc_object_t object
= xpc_connection_send_message_with_reply_sync(agent
->agentConnection
, requestObject
);
324 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
325 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
326 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_DEACTIVATE
)) {
327 agent
->pluginState
= active
; // This is strange, but true. We do actually want to set the state 'active'.
329 } else if (xpc_get_type(object
) == XPC_TYPE_ERROR
) {
330 if ((object
== XPC_ERROR_CONNECTION_INVALID
) || (object
== XPC_ERROR_CONNECTION_INTERRUPTED
)) {
331 agent
->pluginState
= dead
;
335 xpc_release(requestObject
);
338 void agent_destroy(agent_t agent
)
340 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
341 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_DESTROY
);
342 xpc_connection_send_message(agent
->agentConnection
, requestObject
);
343 xpc_release(requestObject
);
347 agent_get_state(agent_t agent
)
349 return agent
->pluginState
;
353 agent_notify_interrupt(agent_t agent
)
355 dispatch_sync(agent
->actionQueue
, ^{
356 if (agent
->pluginState
== current
) {
357 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
358 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_INTERRUPT
);
359 xpc_connection_send_message(agent
->agentConnection
, requestObject
);
360 xpc_release(requestObject
);
361 } else if (agent
->pluginState
== active
) {
362 agent
->pluginState
= mechinterrupting
;
368 agent_clear_interrupt(agent_t agent
)
370 dispatch_sync(agent
->actionQueue
, ^{
371 if (agent
->pluginState
== mechinterrupting
) {
372 agent
->pluginState
= active
;
378 agent_receive(agent_t __unused agent
)