2 * Copyright (c) 2000-2007 Apple 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 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * March 24, 2000 Allan Nathanson <ajn@apple.com>
35 #include "configd_server.h"
38 #include <bsm/libbsm.h>
39 #include <sys/types.h>
44 __SCDynamicStoreOpen(SCDynamicStoreRef
*store
, CFStringRef name
)
47 * allocate and initialize a new session
49 *store
= (SCDynamicStoreRef
)__SCDynamicStoreCreatePrivate(NULL
, name
, NULL
, NULL
);
52 * If necessary, initialize the store and session data dictionaries
54 if (storeData
== NULL
) {
55 sessionData
= CFDictionaryCreateMutable(NULL
,
57 &kCFTypeDictionaryKeyCallBacks
,
58 &kCFTypeDictionaryValueCallBacks
);
59 storeData
= CFDictionaryCreateMutable(NULL
,
61 &kCFTypeDictionaryKeyCallBacks
,
62 &kCFTypeDictionaryValueCallBacks
);
63 patternData
= CFDictionaryCreateMutable(NULL
,
65 &kCFTypeDictionaryKeyCallBacks
,
66 &kCFTypeDictionaryValueCallBacks
);
67 changedKeys
= CFSetCreateMutable(NULL
,
69 &kCFTypeSetCallBacks
);
70 deferredRemovals
= CFSetCreateMutable(NULL
,
72 &kCFTypeSetCallBacks
);
73 removedSessionKeys
= CFSetCreateMutable(NULL
,
75 &kCFTypeSetCallBacks
);
83 openMPCopyDescription(const void *info
)
85 return CFStringCreateWithFormat(NULL
, NULL
, CFSTR("<SCDynamicStore MP>"));
91 _configopen(mach_port_t server
,
92 xmlData_t nameRef
, /* raw XML bytes */
93 mach_msg_type_number_t nameLen
,
94 xmlData_t optionsRef
, /* raw XML bytes */
95 mach_msg_type_number_t optionsLen
,
96 mach_port_t
*newServer
,
98 audit_token_t audit_token
)
100 CFDictionaryRef info
;
101 serverSessionRef mySession
;
102 CFStringRef name
= NULL
; /* name (un-serialized) */
103 CFMutableDictionaryRef newInfo
;
104 mach_port_t oldNotify
;
105 CFDictionaryRef options
= NULL
; /* options (un-serialized) */
106 CFStringRef sessionKey
;
107 kern_return_t status
;
108 SCDynamicStorePrivateRef storePrivate
;
109 CFBooleanRef useSessionKeys
= NULL
;
111 *sc_status
= kSCStatusOK
;
113 /* un-serialize the name */
114 if (!_SCUnserializeString(&name
, NULL
, (void *)nameRef
, nameLen
)) {
115 *sc_status
= kSCStatusFailed
;
118 if ((optionsRef
!= NULL
) && (optionsLen
> 0)) {
119 /* un-serialize the [session] options */
120 if (!_SCUnserialize((CFPropertyListRef
*)&options
, NULL
, (void *)optionsRef
, optionsLen
)) {
121 *sc_status
= kSCStatusFailed
;
125 if (*sc_status
!= kSCStatusOK
) {
129 if (!isA_CFString(name
)) {
130 *sc_status
= kSCStatusInvalidArgument
;
134 if (options
!= NULL
) {
135 if (!isA_CFDictionary(options
)) {
136 *sc_status
= kSCStatusInvalidArgument
;
141 * [pre-]process any provided options
143 useSessionKeys
= CFDictionaryGetValue(options
, kSCDynamicStoreUseSessionKeys
);
144 if (useSessionKeys
!= NULL
) {
145 if (!isA_CFBoolean(useSessionKeys
)) {
146 *sc_status
= kSCStatusInvalidArgument
;
152 mySession
= getSession(server
);
153 if ((mySession
!= NULL
) && (mySession
->store
!= NULL
)) {
155 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_configopen(): session is already open."));
157 *sc_status
= kSCStatusFailed
; /* you can't re-open an "open" session */
162 * establish the new session
164 mySession
= addSession(MACH_PORT_NULL
, openMPCopyDescription
);
165 *newServer
= mySession
->key
;
168 * get the credentials associated with the caller.
170 audit_token_to_au32(audit_token
,
172 &mySession
->callerEUID
, // euid
180 /* Create and add a run loop source for the port */
181 mySession
->serverRunLoopSource
= CFMachPortCreateRunLoopSource(NULL
, mySession
->serverPort
, 0);
182 CFRunLoopAddSource(CFRunLoopGetCurrent(),
183 mySession
->serverRunLoopSource
,
184 kCFRunLoopDefaultMode
);
186 if (_configd_trace
) {
187 SCTrace(TRUE
, _configd_trace
,
188 CFSTR("open : %5d : %@\n"),
193 *sc_status
= __SCDynamicStoreOpen(&mySession
->store
, name
);
194 storePrivate
= (SCDynamicStorePrivateRef
)mySession
->store
;
197 * Make the server port accessible to the framework routines.
199 storePrivate
->server
= *newServer
;
202 * Process any provided [session] options
204 if (useSessionKeys
!= NULL
) {
205 storePrivate
->useSessionKeys
= CFBooleanGetValue(useSessionKeys
);
208 /* Request a notification when/if the client dies */
209 status
= mach_port_request_notification(mach_task_self(),
211 MACH_NOTIFY_NO_SENDERS
,
214 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
216 if (status
!= KERN_SUCCESS
) {
217 SCLog(TRUE
, LOG_DEBUG
, CFSTR("_configopen() mach_port_request_notification() failed: %s"), mach_error_string(status
));
218 cleanupSession(*newServer
);
219 *newServer
= MACH_PORT_NULL
;
220 *sc_status
= kSCStatusFailed
;
225 if (oldNotify
!= MACH_PORT_NULL
) {
226 SCLog(TRUE
, LOG_ERR
, CFSTR("_configopen(): why is oldNotify != MACH_PORT_NULL?"));
231 * Save the name of the calling application / plug-in with the session data.
233 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), *newServer
);
234 info
= CFDictionaryGetValue(sessionData
, sessionKey
);
236 newInfo
= CFDictionaryCreateMutableCopy(NULL
, 0, info
);
238 newInfo
= CFDictionaryCreateMutable(NULL
,
240 &kCFTypeDictionaryKeyCallBacks
,
241 &kCFTypeDictionaryValueCallBacks
);
243 CFDictionarySetValue(newInfo
, kSCDName
, name
);
244 CFDictionarySetValue(sessionData
, sessionKey
, newInfo
);
246 CFRelease(sessionKey
);
250 if (name
!= NULL
) CFRelease(name
);
251 if (options
!= NULL
) CFRelease(options
);