2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * March 9, 2004 Allan Nathanson <ajn@apple.com>
28 * - add DNS configuration server
30 * June 1, 2001 Allan Nathanson <ajn@apple.com>
31 * - public API conversion
33 * March 24, 2000 Allan Nathanson <ajn@apple.com>
39 #include <sys/types.h>
40 #include <servers/bootstrap.h>
43 #include "configd_server.h"
44 #include "notify_server.h"
47 /* MiG generated externals and functions */
48 extern struct mig_subsystem _config_subsystem
;
49 extern boolean_t
config_server(mach_msg_header_t
*, mach_msg_header_t
*);
51 #include "shared_dns_info_types.h"
52 #include "dnsinfo_server.h"
54 /* MiG generated externals and functions */
55 extern struct mig_subsystem _shared_dns_info_subsystem
;
56 extern boolean_t
shared_dns_info_server(mach_msg_header_t
*, mach_msg_header_t
*);
58 /* configd server port (for new session requests) */
59 static CFMachPortRef configd_port
= NULL
;
61 /* priviledged bootstrap port (for registering/unregistering w/mach_init) */
62 static mach_port_t priv_bootstrap_port
= MACH_PORT_NULL
;
66 config_demux(mach_msg_header_t
*request
, mach_msg_header_t
*reply
)
68 Boolean processed
= FALSE
;
69 serverSessionRef thisSession
;
70 mach_msg_format_0_trailer_t
*trailer
;
72 thisSession
= getSession(request
->msgh_local_port
);
75 * Get the caller's credentials (eUID/eGID) from the message trailer.
77 trailer
= (mach_msg_security_trailer_t
*)((vm_offset_t
)request
+
78 round_msg(request
->msgh_size
));
80 if ((trailer
->msgh_trailer_type
== MACH_MSG_TRAILER_FORMAT_0
) &&
81 (trailer
->msgh_trailer_size
>= MACH_MSG_TRAILER_FORMAT_0_SIZE
)) {
82 thisSession
->callerEUID
= trailer
->msgh_sender
.val
[0];
83 thisSession
->callerEGID
= trailer
->msgh_sender
.val
[1];
85 static Boolean warned
= FALSE
;
88 SCLog(TRUE
, LOG_WARNING
, CFSTR("caller's credentials not available."));
91 thisSession
->callerEUID
= 0;
92 thisSession
->callerEGID
= 0;
97 * (attempt to) process SCDynamicStore requests.
99 processed
= config_server(request
, reply
);
105 * (attempt to) process DNS configuration requests.
107 processed
= shared_dns_info_server(request
, reply
);
113 * (attempt to) process (NO MORE SENDERS) notification messages.
115 processed
= notify_server(request
, reply
);
121 * unknown message ID, log and return an error.
123 SCLog(TRUE
, LOG_ERR
, CFSTR("config_demux(): unknown message ID (%d) received"), request
->msgh_id
);
124 reply
->msgh_bits
= MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(request
->msgh_bits
), 0);
125 reply
->msgh_remote_port
= request
->msgh_remote_port
;
126 reply
->msgh_size
= sizeof(mig_reply_error_t
); /* Minimal size */
127 reply
->msgh_local_port
= MACH_PORT_NULL
;
128 reply
->msgh_id
= request
->msgh_id
+ 100;
129 ((mig_reply_error_t
*)reply
)->NDR
= NDR_record
;
130 ((mig_reply_error_t
*)reply
)->RetCode
= MIG_BAD_ID
;
136 #define MACH_MSG_BUFFER_SIZE 128
141 configdCallback(CFMachPortRef port
, void *msg
, CFIndex size
, void *info
)
143 mig_reply_error_t
* bufRequest
= msg
;
144 uint32_t bufReply_q
[MACH_MSG_BUFFER_SIZE
/sizeof(uint32_t)];
145 mig_reply_error_t
* bufReply
= (mig_reply_error_t
*)bufReply_q
;
146 static CFIndex bufSize
= 0;
147 mach_msg_return_t mr
;
151 // get max size for MiG reply buffers
152 bufSize
= _config_subsystem
.maxsize
;
153 if (_shared_dns_info_subsystem
.maxsize
> bufSize
) {
154 bufSize
= _shared_dns_info_subsystem
.maxsize
;
157 // check if our on-the-stack reply buffer will be big enough
158 if (bufSize
> sizeof(bufReply_q
)) {
159 SCLog(TRUE
, LOG_NOTICE
,
160 CFSTR("configdCallback(): buffer size should be increased > %d"),
161 _config_subsystem
.maxsize
);
165 if (bufSize
> sizeof(bufReply_q
)) {
166 bufReply
= CFAllocatorAllocate(NULL
, _config_subsystem
.maxsize
, 0);
169 /* we have a request message */
170 (void) config_demux(&bufRequest
->Head
, &bufReply
->Head
);
172 if (!(bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
173 if (bufReply
->RetCode
== MIG_NO_REPLY
) {
174 bufReply
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
175 } else if ((bufReply
->RetCode
!= KERN_SUCCESS
) &&
176 (bufRequest
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)) {
178 * destroy the request - but not the reply port
180 bufRequest
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
181 mach_msg_destroy(&bufRequest
->Head
);
185 if (bufReply
->Head
.msgh_remote_port
!= MACH_PORT_NULL
) {
189 * We don't want to block indefinitely because the client
190 * isn't receiving messages from the reply port.
191 * If we have a send-once right for the reply port, then
192 * this isn't a concern because the send won't block.
193 * If we have a send right, we need to use MACH_SEND_TIMEOUT.
194 * To avoid falling off the kernel's fast RPC path unnecessarily,
195 * we only supply MACH_SEND_TIMEOUT when absolutely necessary.
198 options
= MACH_SEND_MSG
;
199 if (MACH_MSGH_BITS_REMOTE(bufReply
->Head
.msgh_bits
) != MACH_MSG_TYPE_MOVE_SEND_ONCE
) {
200 options
|= MACH_SEND_TIMEOUT
;
202 mr
= mach_msg(&bufReply
->Head
, /* msg */
203 options
, /* option */
204 bufReply
->Head
.msgh_size
, /* send_size */
206 MACH_PORT_NULL
, /* rcv_name */
207 MACH_MSG_TIMEOUT_NONE
, /* timeout */
208 MACH_PORT_NULL
); /* notify */
210 /* Has a message error occurred? */
212 case MACH_SEND_INVALID_DEST
:
213 case MACH_SEND_TIMED_OUT
:
216 /* Includes success case. */
221 if (bufReply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
) {
222 mach_msg_destroy(&bufReply
->Head
);
227 if (bufReply
!= (mig_reply_error_t
*)bufReply_q
)
228 CFAllocatorDeallocate(NULL
, bufReply
);
235 server_active(mach_port_t
*restart_service_port
)
238 kern_return_t status
;
240 service_name
= getenv("SCD_SERVER");
242 service_name
= SCD_SERVER
;
245 /* Check "configd" server status */
246 status
= bootstrap_check_in(bootstrap_port
, service_name
, restart_service_port
);
248 case BOOTSTRAP_SUCCESS
:
249 /* if we are being restarted by mach_init */
250 priv_bootstrap_port
= bootstrap_port
;
252 case BOOTSTRAP_SERVICE_ACTIVE
:
253 case BOOTSTRAP_NOT_PRIVILEGED
:
254 /* if another instance of the server is active (or starting) */
255 fprintf(stderr
, "'%s' server already active\n",
258 case BOOTSTRAP_UNKNOWN_SERVICE
:
259 /* if the server is not currently registered/active */
260 *restart_service_port
= MACH_PORT_NULL
;
263 fprintf(stderr
, "bootstrap_check_in() failed: status=%d\n", status
);
264 exit (EX_UNAVAILABLE
);
273 server_init(mach_port_t restart_service_port
, Boolean enableRestart
)
275 CFRunLoopSourceRef rls
;
277 mach_port_t service_port
= restart_service_port
;
278 kern_return_t status
;
279 mach_port_t unpriv_bootstrap_port
;
281 service_name
= getenv("SCD_SERVER");
283 service_name
= SCD_SERVER
;
286 if (service_port
== MACH_PORT_NULL
) {
287 mach_port_t service_send_port
;
289 /* Check "configd" server status */
290 status
= bootstrap_check_in(bootstrap_port
, service_name
, &service_port
);
292 case BOOTSTRAP_SUCCESS
:
293 /* if we are being restarted by mach_init */
294 priv_bootstrap_port
= bootstrap_port
;
296 case BOOTSTRAP_NOT_PRIVILEGED
:
297 /* if another instance of the server is starting */
298 SCLog(TRUE
, LOG_ERR
, CFSTR("'%s' server already starting"), service_name
);
299 exit (EX_UNAVAILABLE
);
300 case BOOTSTRAP_UNKNOWN_SERVICE
:
301 /* service not currently registered, "a good thing" (tm) */
303 status
= bootstrap_create_server(bootstrap_port
,
306 FALSE
, /* not onDemand == restart now */
307 &priv_bootstrap_port
);
308 if (status
!= BOOTSTRAP_SUCCESS
) {
309 SCLog(TRUE
, LOG_ERR
, CFSTR("server_init bootstrap_create_server() failed: status=%d"), status
);
310 exit (EX_UNAVAILABLE
);
313 priv_bootstrap_port
= bootstrap_port
;
316 status
= bootstrap_create_service(priv_bootstrap_port
, service_name
, &service_send_port
);
317 if (status
!= BOOTSTRAP_SUCCESS
) {
318 SCLog(TRUE
, LOG_ERR
, CFSTR("server_init bootstrap_create_service() failed: status=%d"), status
);
319 exit (EX_UNAVAILABLE
);
322 status
= bootstrap_check_in(priv_bootstrap_port
, service_name
, &service_port
);
323 if (status
!= BOOTSTRAP_SUCCESS
) {
324 SCLog(TRUE
, LOG_ERR
, CFSTR("server_init bootstrap_check_in() failed: status=%d"), status
);
325 exit (EX_UNAVAILABLE
);
328 case BOOTSTRAP_SERVICE_ACTIVE
:
329 /* if another instance of the server is active */
330 SCLog(TRUE
, LOG_ERR
, CFSTR("'%s' server already active"), service_name
);
331 exit (EX_UNAVAILABLE
);
333 SCLog(TRUE
, LOG_ERR
, CFSTR("server_init bootstrap_check_in() failed: status=%d"), status
);
334 exit (EX_UNAVAILABLE
);
339 /* we don't want to pass our priviledged bootstrap port along to any spawned helpers so... */
340 status
= bootstrap_unprivileged(priv_bootstrap_port
, &unpriv_bootstrap_port
);
341 if (status
!= BOOTSTRAP_SUCCESS
) {
342 SCLog(TRUE
, LOG_ERR
, CFSTR("server_init bootstrap_unprivileged() failed: status=%d"), status
);
343 exit (EX_UNAVAILABLE
);
346 status
= task_set_bootstrap_port(mach_task_self(), unpriv_bootstrap_port
);
347 if (status
!= BOOTSTRAP_SUCCESS
) {
348 SCLog(TRUE
, LOG_ERR
, CFSTR("server_init task_set_bootstrap_port(): %s"),
349 mach_error_string(status
));
350 exit (EX_UNAVAILABLE
);
353 /* Create the primary / new connection port */
354 configd_port
= CFMachPortCreateWithPort(NULL
, service_port
, configdCallback
, NULL
, NULL
);
357 * Create and add a run loop source for the port and add this source
358 * to both the default run loop mode and the "locked" mode. These two
359 * modes will be used for normal (unlocked) communication with the
360 * server and when multiple (locked) updates are requested by a single
363 rls
= CFMachPortCreateRunLoopSource(NULL
, configd_port
, 0);
364 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, kCFRunLoopDefaultMode
);
365 CFRunLoopAddSource(CFRunLoopGetCurrent(), rls
, CFSTR("locked"));
368 /* Create a session for the primary / new connection port */
369 (void) addSession(configd_port
);
380 mach_port_t service_port
;
381 kern_return_t status
;
384 * Note: we can't use SCLog() since the signal may be received while the
385 * logging thread lock is held.
387 if ((priv_bootstrap_port
== MACH_PORT_NULL
) || (configd_port
== NULL
)) {
391 service_name
= getenv("SCD_SERVER");
393 service_name
= SCD_SERVER
;
396 service_port
= CFMachPortGetPort(configd_port
);
397 if (service_port
!= MACH_PORT_NULL
) {
398 (void) mach_port_destroy(mach_task_self(), service_port
);
401 status
= bootstrap_register(priv_bootstrap_port
, service_name
, MACH_PORT_NULL
);
403 case BOOTSTRAP_SUCCESS
:
405 case MACH_SEND_INVALID_DEST
:
406 case MIG_SERVER_DIED
:
407 /* something happened to mach_init */
410 if (_configd_verbose
) {
411 syslog (LOG_ERR
, "bootstrap_register() failed: status=%d" , status
);
413 fprintf(stderr
, "bootstrap_register() failed: status=%d\n", status
);
416 return EX_UNAVAILABLE
;
432 * if linked with a DEBUG version of the framework, display some
433 * debugging information
435 __showMachPortStatus();
438 * process one run loop event
440 rlMode
= (storeLocked
> 0) ? CFSTR("locked") : kCFRunLoopDefaultMode
;
441 rlStatus
= CFRunLoopRunInMode(rlMode
, 1.0e10
, TRUE
);
444 * check for, and if necessary, push out change notifications
445 * to other processes.