]> git.saurik.com Git - apple/security.git/blob - OSX/authd/main.c
Security-59306.41.2.tar.gz
[apple/security.git] / OSX / authd / main.c
1 /* Copyright (c) 2012-2013 Apple Inc. All Rights Reserved. */
2
3 #include "debugging.h"
4 #include "server.h"
5 #include "process.h"
6 #include "session.h"
7 #include "authtoken.h"
8 #include "engine.h"
9 #include "authd_private.h"
10 #include "connection.h"
11
12 #include <Security/Authorization.h>
13
14 #include <xpc/xpc.h>
15 #include <xpc/private.h>
16 #include <dispatch/dispatch.h>
17 #include <assert.h>
18 #include <sandbox.h>
19
20 #if DEBUG
21 #include <malloc/malloc.h>
22 #endif
23
24 AUTHD_DEFINE_LOG
25
26 static void
27 security_auth_peer_event_handler(xpc_connection_t connection, xpc_object_t event)
28 {
29 __block OSStatus status = errAuthorizationDenied;
30
31 connection_t conn = (connection_t)xpc_connection_get_context(connection);
32 require_action(conn != NULL, done, os_log_error(AUTHD_LOG, "xpc: process context not found"));
33
34 CFRetainSafe(conn);
35
36 xpc_type_t type = xpc_get_type(event);
37
38 if (type == XPC_TYPE_ERROR) {
39 if (event == XPC_ERROR_CONNECTION_INVALID) {
40 // The client process on the other end of the connection has either
41 // crashed or canceled the connection. After receiving this error,
42 // the connection is in an invalid state, and you do not need to
43 // call xpc_connection_cancel(). Just tear down any associated state
44 // here.
45 os_log_debug(AUTHD_LOG, "xpc: client disconnected PID %d", xpc_connection_get_pid(connection));
46 connection_destroy_agents(conn);
47 } else if (event == XPC_ERROR_TERMINATION_IMMINENT) {
48 // Handle per-connection termination cleanup.
49 os_log_debug(AUTHD_LOG, "xpc: per-connection termination PID %d", xpc_connection_get_pid(connection));
50 }
51 } else {
52 assert(type == XPC_TYPE_DICTIONARY);
53
54 xpc_object_t reply = xpc_dictionary_create_reply(event);
55 require(reply != NULL, done);
56
57 uint64_t auth_type = xpc_dictionary_get_uint64(event, AUTH_XPC_TYPE);
58 os_log_debug(AUTHD_LOG, "xpc: received message PID %d, type=%llu", connection_get_pid(conn), auth_type);
59
60 switch (auth_type) {
61 case AUTHORIZATION_CREATE:
62 status = authorization_create(conn,event,reply);
63 break;
64 case AUTHORIZATION_CREATE_WITH_AUDIT_TOKEN:
65 status = authorization_create_with_audit_token(conn,event,reply);
66 break;
67 case AUTHORIZATION_FREE:
68 status = authorization_free(conn,event,reply);
69 break;
70 case AUTHORIZATION_COPY_RIGHTS:
71 status = authorization_copy_rights(conn,event,reply);
72 break;
73 case AUTHORIZATION_COPY_INFO:
74 status = authorization_copy_info(conn,event,reply);
75 break;
76 case AUTHORIZATION_MAKE_EXTERNAL_FORM:
77 status = authorization_make_external_form(conn,event,reply);
78 break;
79 case AUTHORIZATION_CREATE_FROM_EXTERNAL_FORM:
80 status = authorization_create_from_external_form(conn,event,reply);
81 break;
82 case AUTHORIZATION_RIGHT_GET:
83 status = authorization_right_get(conn,event,reply);
84 break;
85 case AUTHORIZATION_RIGHT_SET:
86 status = authorization_right_set(conn,event,reply);
87 break;
88 case AUTHORIZATION_RIGHT_REMOVE:
89 status = authorization_right_remove(conn,event,reply);
90 break;
91 case SESSION_SET_USER_PREFERENCES:
92 status = session_set_user_preferences(conn,event,reply);
93 break;
94 case AUTHORIZATION_DISMISS:
95 connection_destroy_agents(conn);
96 status = errAuthorizationSuccess;
97 break;
98 case AUTHORIZATION_ENABLE_SMARTCARD:
99 status = authorization_enable_smartcard(conn,event,reply);
100 break;
101 case AUTHORIZATION_SETUP:
102 {
103 mach_port_t bootstrap = xpc_dictionary_copy_mach_send(event, AUTH_XPC_BOOTSTRAP);
104 if (!process_set_bootstrap(connection_get_process(conn), bootstrap)) {
105 if (bootstrap != MACH_PORT_NULL) {
106 mach_port_deallocate(mach_task_self(), bootstrap);
107 }
108 }
109 }
110 status = errAuthorizationSuccess;
111 break;
112 case AUTHORIZATION_PREAUTHORIZE_CREDENTIALS:
113 status = authorization_preauthorize_credentials(conn,event,reply);
114 break;
115 #if DEBUG
116 case AUTHORIZATION_DEV:
117 server_dev();
118 break;
119 #endif
120 default:
121 break;
122 }
123
124 xpc_dictionary_set_int64(reply, AUTH_XPC_STATUS, status);
125 xpc_connection_send_message(connection, reply);
126 xpc_release(reply);
127 }
128
129 done:
130 CFReleaseSafe(conn);
131 }
132
133 static void
134 connection_finalizer(void * conn)
135 {
136 os_log_debug(AUTHD_LOG, "xpc: connection_finalizer for PID %d", connection_get_pid(conn));
137 server_unregister_connection(conn);
138
139 //#if DEBUG
140 // malloc_printf("-=-=-=- connection_finalizer() -=-=-=-\n");
141 // malloc_zone_print(malloc_default_zone(), false);
142 //#endif
143 }
144
145 static void
146 security_auth_event_handler(xpc_connection_t xpc_conn)
147 {
148 connection_t conn = server_register_connection(xpc_conn);
149
150 if (conn) {
151 xpc_connection_set_context(xpc_conn, conn);
152 xpc_connection_set_finalizer_f(xpc_conn, connection_finalizer);
153
154 xpc_connection_set_event_handler(xpc_conn, ^(xpc_object_t event) {
155 xpc_retain(xpc_conn);
156 xpc_retain(event);
157 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
158 security_auth_peer_event_handler(xpc_conn, event);
159 xpc_release(event);
160 xpc_release(xpc_conn);
161 });
162 });
163 xpc_connection_resume(xpc_conn);
164
165 } else {
166 os_log_error(AUTHD_LOG, "xpc: failed to register connection (PID %d)", xpc_connection_get_pid(xpc_conn));
167 xpc_connection_cancel(xpc_conn);
168 }
169 }
170
171 static void sandbox(const char *tmpdir)
172 {
173 char *errorbuf;
174 const char *sandbox_params[] = {"TMP_DIR", tmpdir, NULL};
175 int32_t rc;
176
177 rc = sandbox_init_with_parameters(SECURITY_AUTH_NAME, SANDBOX_NAMED, sandbox_params, &errorbuf);
178 if (rc) {
179 os_log_error(AUTHD_LOG, "server: sandbox_init failed %{public}s (%i)", errorbuf, rc);
180 sandbox_free_error(errorbuf);
181 #ifndef DEBUG
182 abort();
183 #endif
184 }
185 }
186
187 int main(int argc AUTH_UNUSED, const char *argv[] AUTH_UNUSED)
188 {
189 //#if DEBUG
190 // malloc_printf("-=-=-=- main() -=-=-=-\n");
191 // malloc_zone_print(malloc_default_zone(), false);
192 //#endif
193
194 os_log_debug(AUTHD_LOG, "starting");
195
196 // <rdar://problem/20900280> authd needs to provide a writeable temp dir for SQLite
197 // <rdar://problem/21223798> Insecure temporary directory in authd (/tmp/authd)
198 char darwin_tmp[PATH_MAX];
199 size_t len = confstr(_CS_DARWIN_USER_TEMP_DIR, darwin_tmp, sizeof(darwin_tmp));
200 if (len == 0 || len >= PATH_MAX) {
201 os_log_error(AUTHD_LOG, "Invalid _CS_DARWIN_USER_TEMP_DIR");
202 return errAuthorizationInternal;
203 }
204
205 char *real_tmp = realpath(darwin_tmp, NULL);
206 if (real_tmp == NULL) {
207 os_log_error(AUTHD_LOG, "realpath( %{public}s ) FAILED", darwin_tmp);
208 return errAuthorizationInternal;
209 }
210
211 setenv("SQLITE_TMPDIR", real_tmp, 1);
212 sandbox(real_tmp);
213 free(real_tmp);
214
215 if (server_init() != errAuthorizationSuccess) {
216 os_log_error(AUTHD_LOG, "auth: server_init() failed");
217 return errAuthorizationInternal;
218 }
219
220 //#if DEBUG
221 // malloc_printf("-=-=-=- server_init() -=-=-=-\n");
222 // malloc_zone_print(malloc_default_zone(), false);
223 //#endif
224
225 xpc_main(security_auth_event_handler);
226
227 // xpc_main() will never return, but if it did, here's what you'd call:
228 //server_cleanup();
229
230 return 0;
231 }