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
, 0);
143 xpc_connection_set_instance(agent
->agentConnection
, sessionUUID
);
144 os_log_debug(AUTHD_LOG
, "agent: creating a loginwindow security agent");
145 doSwitchAudit
= true;
146 doSwitchBootstrap
= true;
149 agent
->agentConnection
= xpc_connection_create_mach_service(AUTHORIZATIONHOST_BOOTSTRAP_NAME_BASE
, NULL
, 0);
150 xpc_connection_set_instance(agent
->agentConnection
, sessionUUID
);
151 os_log_debug(AUTHD_LOG
, "agent: creating a standard authhost");
152 doSwitchAudit
= true;
153 doSwitchBootstrap
= true;
156 // **************** Here's the event handler, since I can never find it
157 xpc_connection_set_target_queue(agent
->agentConnection
, agent
->eventQueue
);
158 xpc_connection_set_event_handler(agent
->agentConnection
, ^(xpc_object_t object
) {
159 char* objectDesc
= xpc_copy_description(object
);
162 if (agent
->pluginState
== dead
) {
163 // If the agent is dead for some reason, drop this message before we hurt ourselves.
167 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
168 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
169 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_INTERRUPT
)) {
170 agent
->pluginState
= interrupting
;
171 engine_interrupt_agent(agent
->engine
);
176 xpc_connection_resume(agent
->agentConnection
);
178 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
179 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_CREATE
);
180 xpc_dictionary_set_string(requestObject
, AUTH_XPC_PLUGIN_NAME
, mechanism_get_plugin(mech
));
181 xpc_dictionary_set_string(requestObject
, AUTH_XPC_MECHANISM_NAME
, mechanism_get_param(mech
));
183 mach_port_name_t jobPort
;
184 if (audit_info
!= NULL
) {
185 if (0 == audit_session_port(audit_info
->asid
, &jobPort
)) {
186 os_log_debug(AUTHD_LOG
, "agent: attaching an audit session port");
187 xpc_dictionary_set_mach_send(requestObject
, AUTH_XPC_AUDIT_SESSION_PORT
, jobPort
);
189 if (mach_port_mod_refs(mach_task_self(), jobPort
, MACH_PORT_RIGHT_SEND
, -1) != KERN_SUCCESS
) {
190 os_log_error(AUTHD_LOG
, "agent: unable to release send right for audit session, leaking");
196 if (doSwitchBootstrap
) {
197 os_log_debug(AUTHD_LOG
, "agent: attaching a bootstrap port");
198 xpc_dictionary_set_mach_send(requestObject
, AUTH_XPC_BOOTSTRAP_PORT
, process_get_bootstrap(proc
));
201 // This loop will be repeated until we can get ahold of a SecurityAgent, or get a fatal error
202 // This will cause us to retry any CONNECTION_INTERRUPTED status messages
204 xpc_object_t object
= xpc_connection_send_message_with_reply_sync(agent
->agentConnection
, requestObject
);
206 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
207 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
208 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_CREATE
)) {
209 agent
->status
= xpc_dictionary_get_uint64(object
, AUTH_XPC_REPLY_RESULT_VALUE
);
210 if (agent
->status
== kAuthorizationResultAllow
) {
211 // Only go here if we have an "allow" from the SecurityAgent (the plugin create might have failed)
212 // This is backwards compatible so that it goes gently into the build, as the default is "allow".
213 agent
->pluginState
= created
;
215 agent
->pluginState
= dead
;
218 } else if (xpc_get_type(object
) == XPC_TYPE_ERROR
) {
219 if (object
== XPC_ERROR_CONNECTION_INVALID
) {
220 agent
->pluginState
= dead
;
224 } while ((agent
->pluginState
== init
) && (firstMech
));
226 xpc_release(requestObject
);
228 if (agent
->pluginState
!= created
) {
237 agent_run(agent_t agent
, auth_items_t hints
, auth_items_t context
, auth_items_t immutable_hints
)
239 xpc_object_t hintsArray
= auth_items_export_xpc(hints
);
240 xpc_object_t contextArray
= auth_items_export_xpc(context
);
241 xpc_object_t immutableHintsArray
= auth_items_export_xpc(immutable_hints
);
243 dispatch_semaphore_t replyWaiter
= dispatch_semaphore_create(0);
244 dispatch_sync(agent
->actionQueue
, ^{
245 if (agent
->pluginState
!= mechinterrupting
) {
246 agent
->pluginState
= current
;
247 agent
->status
= kAuthorizationResultUndefined
; // Set this while the plugin chews on the user input.
249 auth_items_clear(agent
->hints
);
250 auth_items_clear(agent
->context
);
252 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
253 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_INVOKE
);
254 xpc_dictionary_set_value(requestObject
, AUTH_XPC_HINTS_NAME
, hintsArray
);
255 xpc_dictionary_set_value(requestObject
, AUTH_XPC_CONTEXT_NAME
, contextArray
);
256 xpc_dictionary_set_value(requestObject
, AUTH_XPC_IMMUTABLE_HINTS_NAME
, immutableHintsArray
);
258 xpc_connection_send_message_with_reply(agent
->agentConnection
, requestObject
, agent
->actionQueue
, ^(xpc_object_t object
){
259 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
260 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
261 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_RESULT
)) {
262 if (agent
->pluginState
== current
) {
263 xpc_object_t xpcContext
= xpc_dictionary_get_value(object
, AUTH_XPC_CONTEXT_NAME
);
264 xpc_object_t xpcHints
= xpc_dictionary_get_value(object
, AUTH_XPC_HINTS_NAME
);
265 auth_items_copy_xpc(agent
->context
, xpcContext
);
266 auth_items_copy_xpc(agent
->hints
, xpcHints
);
267 agent
->status
= xpc_dictionary_get_uint64(object
, AUTH_XPC_REPLY_RESULT_VALUE
);
268 agent
->pluginState
= active
;
271 } else if (xpc_get_type(object
) == XPC_TYPE_ERROR
) {
272 if ((object
== XPC_ERROR_CONNECTION_INVALID
) || (object
== XPC_ERROR_CONNECTION_INTERRUPTED
)) {
273 agent
->pluginState
= dead
;
276 dispatch_semaphore_signal(replyWaiter
);
278 xpc_release(requestObject
);
282 if (agent
->pluginState
== current
) {
283 dispatch_semaphore_wait(replyWaiter
, DISPATCH_TIME_FOREVER
);
285 dispatch_release(replyWaiter
);
287 os_log_debug(AUTHD_LOG
, "agent: Finished call to SecurityAgent");
289 xpc_release(hintsArray
);
290 xpc_release(contextArray
);
291 xpc_release(immutableHintsArray
);
293 return agent
->status
;
297 agent_get_hints(agent_t agent
)
303 agent_get_context(agent_t agent
)
305 return agent
->context
;
309 agent_get_mechanism(agent_t agent
)
315 agent_deactivate(agent_t agent
)
317 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
318 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_DEACTIVATE
);
320 agent
->pluginState
= deactivating
;
322 xpc_object_t object
= xpc_connection_send_message_with_reply_sync(agent
->agentConnection
, requestObject
);
323 if (xpc_get_type(object
) == XPC_TYPE_DICTIONARY
) {
324 const char *replyType
= xpc_dictionary_get_string(object
, AUTH_XPC_REPLY_METHOD_KEY
);
325 if (0 == strcmp(replyType
, AUTH_XPC_REPLY_METHOD_DEACTIVATE
)) {
326 agent
->pluginState
= active
; // This is strange, but true. We do actually want to set the state 'active'.
328 } else if (xpc_get_type(object
) == XPC_TYPE_ERROR
) {
329 if ((object
== XPC_ERROR_CONNECTION_INVALID
) || (object
== XPC_ERROR_CONNECTION_INTERRUPTED
)) {
330 agent
->pluginState
= dead
;
334 xpc_release(requestObject
);
337 void agent_destroy(agent_t agent
)
339 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
340 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_DESTROY
);
341 xpc_connection_send_message(agent
->agentConnection
, requestObject
);
342 xpc_release(requestObject
);
346 agent_get_state(agent_t agent
)
348 return agent
->pluginState
;
352 agent_notify_interrupt(agent_t agent
)
354 dispatch_sync(agent
->actionQueue
, ^{
355 if (agent
->pluginState
== current
) {
356 xpc_object_t requestObject
= xpc_dictionary_create(NULL
, NULL
, 0);
357 xpc_dictionary_set_string(requestObject
, AUTH_XPC_REQUEST_METHOD_KEY
, AUTH_XPC_REQUEST_METHOD_INTERRUPT
);
358 xpc_connection_send_message(agent
->agentConnection
, requestObject
);
359 xpc_release(requestObject
);
360 } else if (agent
->pluginState
== active
) {
361 agent
->pluginState
= mechinterrupting
;
367 agent_clear_interrupt(agent_t agent
)
369 dispatch_sync(agent
->actionQueue
, ^{
370 if (agent
->pluginState
== mechinterrupting
) {
371 agent
->pluginState
= active
;
377 agent_receive(agent_t __unused agent
)