]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configget.c
configd-963.30.1.tar.gz
[apple/configd.git] / configd.tproj / _configget.c
1 /*
2 * Copyright (c) 2000-2006, 2008, 2011, 2013-2016 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 "session.h"
36
37 __private_extern__
38 int
39 __SCDynamicStoreCopyValue(SCDynamicStoreRef store, CFStringRef key, CFDataRef *value, Boolean internal)
40 {
41 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
42 CFDictionaryRef dict;
43
44 SC_trace("%s : %5d : %@",
45 internal ? "*copy " : "copy ",
46 storePrivate->server,
47 key);
48
49 dict = CFDictionaryGetValue(storeData, key);
50 if ((dict == NULL) || !CFDictionaryContainsKey(dict, kSCDData)) {
51 /* key doesn't exist (or data never defined) */
52 return kSCStatusNoKey;
53 }
54
55 /* Return the data associated with the key */
56 *value = CFRetain(CFDictionaryGetValue(dict, kSCDData));
57
58 return kSCStatusOK;
59 }
60
61 __private_extern__
62 kern_return_t
63 _configget(mach_port_t server,
64 xmlData_t keyRef, /* raw XML bytes */
65 mach_msg_type_number_t keyLen,
66 xmlDataOut_t *dataRef, /* raw XML bytes */
67 mach_msg_type_number_t *dataLen,
68 int *newInstance,
69 int *sc_status,
70 audit_token_t audit_token)
71 {
72 CFStringRef key = NULL; /* key (un-serialized) */
73 CFIndex len;
74 serverSessionRef mySession;
75 Boolean ok;
76 CFDataRef value;
77
78 *dataRef = NULL;
79 *dataLen = 0;
80
81 /* un-serialize the key */
82 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
83 *sc_status = kSCStatusFailed;
84 goto done;
85 }
86
87 if (!isA_CFString(key)) {
88 *sc_status = kSCStatusInvalidArgument;
89 goto done;
90 }
91
92 mySession = getSession(server);
93 if (mySession == NULL) {
94 mySession = tempSession(server, CFSTR("SCDynamicStoreCopyValue"), audit_token);
95 if (mySession == NULL) {
96 /* you must have an open session to play */
97 *sc_status = kSCStatusNoStoreSession;
98 goto done;
99 }
100 }
101
102 *sc_status = __SCDynamicStoreCopyValue(mySession->store, key, &value, FALSE);
103 if (*sc_status != kSCStatusOK) {
104 goto done;
105 }
106
107 /* serialize the data */
108 ok = _SCSerializeData(value, (void **)dataRef, &len);
109 *dataLen = (mach_msg_type_number_t)len;
110 CFRelease(value);
111 if (!ok) {
112 *sc_status = kSCStatusFailed;
113 goto done;
114 }
115
116 /*
117 * return the instance number associated with the returned data.
118 */
119 *newInstance = 1;
120
121 done :
122
123 if (key != NULL) CFRelease(key);
124 return KERN_SUCCESS;
125 }
126
127 /*
128 * "context" argument for addSpecificKey() and addSpecificPattern()
129 */
130 typedef struct {
131 SCDynamicStoreRef store;
132 CFMutableDictionaryRef dict;
133 } addSpecific, *addSpecificRef;
134
135 static void
136 addSpecificKey(const void *value, void *context)
137 {
138 CFDataRef data;
139 CFStringRef key = (CFStringRef)value;
140 addSpecificRef myContextRef = (addSpecificRef)context;
141 int sc_status;
142
143 if (!isA_CFString(key)) {
144 return;
145 }
146
147 sc_status = __SCDynamicStoreCopyValue(myContextRef->store, key, &data, TRUE);
148 if (sc_status == kSCStatusOK) {
149 CFDictionaryAddValue(myContextRef->dict, key, data);
150 CFRelease(data);
151 }
152
153 return;
154 }
155
156 static void
157 addSpecificPattern(const void *value, void *context)
158 {
159 CFStringRef pattern = (CFStringRef)value;
160 addSpecificRef myContextRef = (addSpecificRef)context;
161 int sc_status;
162 CFArrayRef keys;
163
164 if (!isA_CFString(pattern)) {
165 return;
166 }
167
168 sc_status = __SCDynamicStoreCopyKeyList(myContextRef->store, pattern, TRUE, &keys);
169 if (sc_status == kSCStatusOK) {
170 CFArrayApplyFunction(keys,
171 CFRangeMake(0, CFArrayGetCount(keys)),
172 addSpecificKey,
173 context);
174 CFRelease(keys);
175 }
176
177 return;
178 }
179
180 __private_extern__
181 int
182 __SCDynamicStoreCopyMultiple(SCDynamicStoreRef store, CFArrayRef keys, CFArrayRef patterns, CFDictionaryRef *values)
183 {
184 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
185 addSpecific myContext;
186
187 SC_trace("copy m : %5d : %ld keys, %ld patterns",
188 storePrivate->server,
189 keys ? CFArrayGetCount(keys) : 0,
190 patterns ? CFArrayGetCount(patterns) : 0);
191
192 myContext.store = store;
193 myContext.dict = CFDictionaryCreateMutable(NULL,
194 0,
195 &kCFTypeDictionaryKeyCallBacks,
196 &kCFTypeDictionaryValueCallBacks);
197
198 if (keys) {
199 CFArrayApplyFunction(keys,
200 CFRangeMake(0, CFArrayGetCount(keys)),
201 addSpecificKey,
202 &myContext);
203 }
204
205 if (patterns) {
206 CFArrayApplyFunction(patterns,
207 CFRangeMake(0, CFArrayGetCount(patterns)),
208 addSpecificPattern,
209 &myContext);
210 }
211
212 /* Return the keys/values associated with the key */
213 *values = myContext.dict;
214
215 return kSCStatusOK;
216 }
217
218 __private_extern__
219 kern_return_t
220 _configget_m(mach_port_t server,
221 xmlData_t keysRef,
222 mach_msg_type_number_t keysLen,
223 xmlData_t patternsRef,
224 mach_msg_type_number_t patternsLen,
225 xmlDataOut_t *dataRef,
226 mach_msg_type_number_t *dataLen,
227 int *sc_status,
228 audit_token_t audit_token)
229 {
230 CFDictionaryRef dict = NULL; /* keys/values (un-serialized) */
231 CFArrayRef keys = NULL; /* keys (un-serialized) */
232 CFIndex len;
233 serverSessionRef mySession;
234 Boolean ok;
235 CFArrayRef patterns = NULL; /* patterns (un-serialized) */
236
237 *dataRef = NULL;
238 *dataLen = 0;
239
240 *sc_status = kSCStatusOK;
241
242 if (keysRef && (keysLen > 0)) {
243 /* un-serialize the keys */
244 if (!_SCUnserialize((CFPropertyListRef *)&keys, NULL, (void *)keysRef, keysLen)) {
245 *sc_status = kSCStatusFailed;
246 }
247 }
248
249 if (patternsRef && (patternsLen > 0)) {
250 /* un-serialize the patterns */
251 if (!_SCUnserialize((CFPropertyListRef *)&patterns, NULL, (void *)patternsRef, patternsLen)) {
252 *sc_status = kSCStatusFailed;
253 }
254 }
255
256 if (*sc_status != kSCStatusOK) {
257 goto done;
258 }
259
260 if ((keys != NULL) && !isA_CFArray(keys)) {
261 *sc_status = kSCStatusInvalidArgument;
262 goto done;
263 }
264
265 if ((patterns != NULL) && !isA_CFArray(patterns)) {
266 *sc_status = kSCStatusInvalidArgument;
267 goto done;
268 }
269
270 mySession = getSession(server);
271 if (mySession == NULL) {
272 mySession = tempSession(server, CFSTR("SCDynamicStoreCopyMultiple"), audit_token);
273 if (mySession == NULL) {
274 /* you must have an open session to play */
275 *sc_status = kSCStatusNoStoreSession;
276 goto done;
277 }
278 }
279
280 /* fetch the requested information */
281 *sc_status = __SCDynamicStoreCopyMultiple(mySession->store, keys, patterns, &dict);
282
283 /* serialize the dictionary of matching keys/patterns */
284 ok = _SCSerialize(dict, NULL, (void **)dataRef, &len);
285 *dataLen = (mach_msg_type_number_t)len;
286 CFRelease(dict);
287 if (!ok) {
288 *sc_status = kSCStatusFailed;
289 }
290
291 done :
292
293 if (keys) CFRelease(keys);
294 if (patterns) CFRelease(patterns);
295 return KERN_SUCCESS;
296 }