]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configopen.c
configd-963.250.1.tar.gz
[apple/configd.git] / configd.tproj / _configopen.c
1 /*
2 * Copyright (c) 2000-2009, 2011, 2015-2017, 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 /*
25 * Modification History
26 *
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * March 24, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include "configd.h"
35 #include "configd_server.h"
36 #include "session.h"
37
38 #include <bsm/libbsm.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41
42 __private_extern__
43 int
44 __SCDynamicStoreOpen(SCDynamicStoreRef *store, CFStringRef name)
45 {
46 /*
47 * allocate and initialize a new session
48 */
49 *store = (SCDynamicStoreRef)__SCDynamicStoreCreatePrivate(NULL, name, NULL, NULL);
50
51 /*
52 * If necessary, initialize the store and session data dictionaries
53 */
54 if (storeData == NULL) {
55 sessionData = CFDictionaryCreateMutable(NULL,
56 0,
57 &kCFTypeDictionaryKeyCallBacks,
58 &kCFTypeDictionaryValueCallBacks);
59 storeData = CFDictionaryCreateMutable(NULL,
60 0,
61 &kCFTypeDictionaryKeyCallBacks,
62 &kCFTypeDictionaryValueCallBacks);
63 patternData = CFDictionaryCreateMutable(NULL,
64 0,
65 &kCFTypeDictionaryKeyCallBacks,
66 &kCFTypeDictionaryValueCallBacks);
67 changedKeys = CFSetCreateMutable(NULL,
68 0,
69 &kCFTypeSetCallBacks);
70 deferredRemovals = CFSetCreateMutable(NULL,
71 0,
72 &kCFTypeSetCallBacks);
73 removedSessionKeys = CFSetCreateMutable(NULL,
74 0,
75 &kCFTypeSetCallBacks);
76 }
77
78 return kSCStatusOK;
79 }
80
81
82 static CFStringRef
83 openMPCopyDescription(const void *info)
84 {
85 #pragma unused(info)
86 return CFStringCreateWithFormat(NULL, NULL, CFSTR("<SCDynamicStore MP>"));
87 }
88
89
90 __private_extern__
91 kern_return_t
92 _configopen(mach_port_t server,
93 xmlData_t nameRef, /* raw XML bytes */
94 mach_msg_type_number_t nameLen,
95 xmlData_t optionsRef, /* raw XML bytes */
96 mach_msg_type_number_t optionsLen,
97 mach_port_t *newServer,
98 int *sc_status,
99 audit_token_t audit_token)
100 {
101 CFDictionaryRef info;
102 serverSessionRef mySession;
103 CFStringRef name = NULL; /* name (un-serialized) */
104 CFMutableDictionaryRef newInfo;
105 mach_port_t oldNotify;
106 CFDictionaryRef options = NULL; /* options (un-serialized) */
107 CFStringRef sessionKey;
108 kern_return_t status;
109 SCDynamicStorePrivateRef storePrivate;
110 CFBooleanRef useSessionKeys = NULL;
111
112 *newServer = MACH_PORT_NULL;
113 *sc_status = kSCStatusOK;
114
115 /* un-serialize the name */
116 if (!_SCUnserializeString(&name, NULL, (void *)nameRef, nameLen)) {
117 *sc_status = kSCStatusFailed;
118 }
119
120 if ((optionsRef != NULL) && (optionsLen > 0)) {
121 /* un-serialize the [session] options */
122 if (!_SCUnserialize((CFPropertyListRef *)&options, NULL, (void *)optionsRef, optionsLen)) {
123 *sc_status = kSCStatusFailed;
124 }
125 }
126
127 if (*sc_status != kSCStatusOK) {
128 goto done;
129 }
130
131 if (!isA_CFString(name)) {
132 *sc_status = kSCStatusInvalidArgument;
133 goto done;
134 }
135
136 if (options != NULL) {
137 if (!isA_CFDictionary(options)) {
138 *sc_status = kSCStatusInvalidArgument;
139 goto done;
140 }
141
142 /*
143 * [pre-]process any provided options
144 */
145 useSessionKeys = CFDictionaryGetValue(options, kSCDynamicStoreUseSessionKeys);
146 if (useSessionKeys != NULL) {
147 if (!isA_CFBoolean(useSessionKeys)) {
148 *sc_status = kSCStatusInvalidArgument;
149 goto done;
150 }
151 }
152 }
153
154 /*
155 * establish the new session
156 */
157 mySession = addSession(server, openMPCopyDescription);
158 if (mySession == NULL) {
159 SC_log(LOG_NOTICE, "session is already open");
160 *sc_status = kSCStatusFailed; /* you can't re-open an "open" session */
161 goto done;
162 }
163
164 *newServer = mySession->key;
165 __MACH_PORT_DEBUG(TRUE, "*** _configopen (after addSession)", *newServer);
166
167 /* save the audit_token in case we need to check the callers credentials */
168 mySession->auditToken = audit_token;
169
170 /* Create and add a run loop source for the port */
171 mySession->serverRunLoopSource = CFMachPortCreateRunLoopSource(NULL, mySession->serverPort, 0);
172 CFRunLoopAddSource(CFRunLoopGetCurrent(),
173 mySession->serverRunLoopSource,
174 kCFRunLoopDefaultMode);
175
176 SC_trace("open : %5d : %@",
177 *newServer,
178 name);
179
180 *sc_status = __SCDynamicStoreOpen(&mySession->store, name);
181 storePrivate = (SCDynamicStorePrivateRef)mySession->store;
182
183 /*
184 * Make the server port accessible to the framework routines.
185 * ... and be sure to clear before calling CFRelease(store)
186 */
187 storePrivate->server = *newServer;
188
189 /*
190 * Process any provided [session] options
191 */
192 if (useSessionKeys != NULL) {
193 storePrivate->useSessionKeys = CFBooleanGetValue(useSessionKeys);
194 }
195
196 /* Request a notification when/if the client dies */
197 status = mach_port_request_notification(mach_task_self(),
198 *newServer,
199 MACH_NOTIFY_NO_SENDERS,
200 1,
201 *newServer,
202 MACH_MSG_TYPE_MAKE_SEND_ONCE,
203 &oldNotify);
204 if (status != KERN_SUCCESS) {
205 SC_log(LOG_NOTICE, "mach_port_request_notification() failed: %s", mach_error_string(status));
206 cleanupSession(*newServer);
207 *newServer = MACH_PORT_NULL;
208 *sc_status = kSCStatusFailed;
209 goto done;
210 }
211 __MACH_PORT_DEBUG(TRUE, "*** _configopen (after mach_port_request_notification)", *newServer);
212
213 if (oldNotify != MACH_PORT_NULL) {
214 SC_log(LOG_NOTICE, "oldNotify != MACH_PORT_NULL");
215 }
216
217 /*
218 * Save the name of the calling application / plug-in with the session data.
219 */
220 sessionKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), *newServer);
221 info = CFDictionaryGetValue(sessionData, sessionKey);
222 if (info != NULL) {
223 newInfo = CFDictionaryCreateMutableCopy(NULL, 0, info);
224 } else {
225 newInfo = CFDictionaryCreateMutable(NULL,
226 0,
227 &kCFTypeDictionaryKeyCallBacks,
228 &kCFTypeDictionaryValueCallBacks);
229 }
230 CFDictionarySetValue(newInfo, kSCDName, name);
231 CFDictionarySetValue(sessionData, sessionKey, newInfo);
232 CFRelease(newInfo);
233 CFRelease(sessionKey);
234
235 /*
236 * Note: at this time we should be holding ONE send right and
237 * ONE receive right to the server. The send right is
238 * moved to the caller.
239 */
240
241 done :
242
243 if (name != NULL) CFRelease(name);
244 if (options != NULL) CFRelease(options);
245 return KERN_SUCCESS;
246 }