]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/helper/SCHelper_client.c
configd-1061.0.2.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / helper / SCHelper_client.c
1 /*
2 * Copyright (c) 2005-2008, 2010, 2011, 2013, 2015, 2016, 2018, 2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <fcntl.h>
25 #include <paths.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <sys/wait.h>
36 #include <mach/mach.h>
37 #include <mach/mach_error.h>
38 #include <servers/bootstrap.h>
39 #include <bootstrap_priv.h>
40 #include <pthread.h>
41
42 #include "SCPreferencesInternal.h"
43 #include "SCHelper_client.h"
44 #include "helper.h" // MiG generated file
45
46
47 #define HELPER "SCHelper"
48 #define HELPER_LEN (sizeof(HELPER) - 1)
49
50 #define SUFFIX_SYM "~sym"
51 #define SUFFIX_SYM_LEN (sizeof(SUFFIX_SYM) - 1)
52
53
54 static pthread_mutex_t _helper_lock = PTHREAD_MUTEX_INITIALIZER;
55 static mach_port_t _helper_server = MACH_PORT_NULL;
56
57
58 static mach_port_t
59 __SCHelperServerPort(kern_return_t *status)
60 {
61 mach_port_t server = MACH_PORT_NULL;
62 char *server_name;
63
64 server_name = getenv("SCHELPER_SERVER");
65 if (!server_name) {
66 server_name = SCHELPER_SERVER;
67 }
68
69 #ifdef BOOTSTRAP_PRIVILEGED_SERVER
70 *status = bootstrap_look_up2(bootstrap_port,
71 server_name,
72 &server,
73 0,
74 BOOTSTRAP_PRIVILEGED_SERVER);
75 #else // BOOTSTRAP_PRIVILEGED_SERVER
76 *status = bootstrap_look_up(bootstrap_port, server_name, &server);
77 #endif // BOOTSTRAP_PRIVILEGED_SERVER
78
79 switch (*status) {
80 case BOOTSTRAP_SUCCESS :
81 /* service currently registered, "a good thing" (tm) */
82 return server;
83 case BOOTSTRAP_NOT_PRIVILEGED :
84 /* the service is not privileged */
85 break;
86 case BOOTSTRAP_UNKNOWN_SERVICE :
87 /* service not currently registered, try again later */
88 break;
89 default :
90 #ifdef DEBUG
91 SC_log(LOG_INFO, "bootstrap_look_up() failed: status=%s",
92 bootstrap_strerror(*status));
93 #endif /* DEBUG */
94 break;
95 }
96
97 return MACH_PORT_NULL;
98 }
99
100
101 __private_extern__
102 Boolean
103 _SCHelperOpen(CFDataRef authorizationData, mach_port_t *helper_port)
104 {
105 kern_return_t kr;
106 Boolean ok;
107 mach_port_t server;
108 uint32_t status = 0;
109
110 *helper_port = MACH_PORT_NULL;
111
112 // open a new session with the server
113 server = _helper_server;
114 while (TRUE) {
115 if (server != MACH_PORT_NULL) {
116 kr = helperinit(server,
117 helper_port,
118 &status);
119 if (kr == KERN_SUCCESS) {
120 break;
121 }
122
123 // our [cached] server port is not valid
124 if (kr != MACH_SEND_INVALID_DEST) {
125 // if we got an unexpected error, don't retry
126 status = kr;
127 break;
128 }
129 }
130
131 pthread_mutex_lock(&_helper_lock);
132 if (_helper_server != MACH_PORT_NULL) {
133 if (server == _helper_server) {
134 // if the server we tried returned the error
135 (void)mach_port_deallocate(mach_task_self(), _helper_server);
136 _helper_server = __SCHelperServerPort(&kr);
137 if (_helper_server == MACH_PORT_NULL) {
138 status = kr;
139 }
140 } else {
141 // another thread has refreshed the SCHelper server port
142 }
143 } else {
144 _helper_server = __SCHelperServerPort(&kr);
145 if (_helper_server == MACH_PORT_NULL) {
146 status = kr;
147 }
148 }
149 server = _helper_server;
150 pthread_mutex_unlock(&_helper_lock);
151
152 if (server == MACH_PORT_NULL) {
153 // if SCHelper server not available
154 break;
155 }
156 }
157 __MACH_PORT_DEBUG(TRUE, "*** _SCHelperOpen", *helper_port);
158
159 if (*helper_port == MACH_PORT_NULL) {
160 SC_log(LOG_NOTICE, "could not contact \"" HELPER "\": %s",
161 SCErrorString(status));
162 return FALSE;
163 }
164
165 ok = _SCHelperExec(*helper_port, SCHELPER_MSG_AUTH, authorizationData, &status, NULL);
166 if (!ok) {
167 SC_log(LOG_NOTICE, "could not send authorization");
168 goto error;
169 }
170
171 ok = (status == 0);
172 if (!ok) {
173 SC_log(LOG_NOTICE, "could not start \"" HELPER "\", status = %u", status);
174 goto error;
175 }
176
177 return TRUE;
178
179 error :
180
181 if (*helper_port != MACH_PORT_NULL) {
182 (void)mach_port_deallocate(mach_task_self(), *helper_port);
183 *helper_port = MACH_PORT_NULL;
184 }
185
186 return FALSE;
187
188 }
189
190
191 __private_extern__
192 void
193 _SCHelperClose(mach_port_t *helper_port)
194 {
195 if (!_SCHelperExec(*helper_port, SCHELPER_MSG_EXIT, NULL, NULL, NULL)) {
196 SC_log(LOG_INFO, "could not send exit request");
197 }
198
199 if (*helper_port != MACH_PORT_NULL) {
200 (void)mach_port_deallocate(mach_task_self(), *helper_port);
201 *helper_port = MACH_PORT_NULL;
202 }
203
204 return;
205 }
206
207
208 static CFDataRef
209 _SCHelperExecCopyBacktrace()
210 {
211 static Boolean loggingEnabled = FALSE;
212 static dispatch_once_t once;
213 CFDataRef traceData = NULL;
214
215 dispatch_once(&once, ^{
216 if(getenv("ENABLE_SCHELPER_BACKTRACES")) {
217 loggingEnabled = TRUE;
218 }
219 });
220
221 if (loggingEnabled) {
222 CFStringRef backtrace;
223
224 backtrace = _SC_copyBacktrace();
225 if (backtrace != NULL) {
226 (void)_SCSerializeString(backtrace, &traceData, NULL, NULL);
227 CFRelease(backtrace);
228 }
229 }
230
231 return traceData;
232 }
233
234
235 __private_extern__
236 Boolean
237 _SCHelperExec(mach_port_t port, uint32_t msgID, CFDataRef data, uint32_t *status, CFDataRef *reply)
238 {
239 kern_return_t kr;
240 CFDataRef myData = NULL;
241 xmlDataOut_t replyRef = NULL; /* raw bytes */
242 mach_msg_type_number_t replyLen = 0;
243 uint32_t replyStatus = 0;
244 CFDataRef traceData;
245
246 traceData = _SCHelperExecCopyBacktrace();
247
248 kr = helperexec(port,
249 msgID,
250 (data != NULL) ? (void *)CFDataGetBytePtr(data) : NULL,
251 (data != NULL) ? (mach_msg_type_number_t)CFDataGetLength(data) : 0,
252 (traceData != NULL) ? (void *)CFDataGetBytePtr(traceData) : NULL,
253 (traceData != NULL) ? (mach_msg_type_number_t)CFDataGetLength(traceData) : 0,
254 &replyStatus,
255 &replyRef,
256 &replyLen);
257
258 if (traceData != NULL) {
259 CFRelease(traceData);
260 }
261
262 if (kr != KERN_SUCCESS) {
263 if (replyRef != NULL) {
264 (void) vm_deallocate(mach_task_self(), (vm_address_t)replyRef, replyLen);
265 }
266
267 if (kr != MACH_SEND_INVALID_DEST) {
268 // if we got an unexpected error
269 SC_log(LOG_NOTICE, "_SCHelperExec() failed: %s", mach_error_string(kr));
270 }
271 _SCErrorSet(kr);
272
273 return FALSE;
274 }
275
276 // un-serialize the reply
277 if (replyRef != NULL) {
278 if (!_SCUnserializeData(&myData, replyRef, replyLen)) {
279 return FALSE;
280 }
281 }
282
283 if (status != NULL) {
284 *status = replyStatus;
285 }
286
287 if (reply != NULL) {
288 *reply = myData;
289 } else if (myData != NULL) {
290 SC_log(LOG_INFO, "data available with no place to go");
291 CFRelease(myData);
292 }
293
294 return TRUE;
295 }