]> git.saurik.com Git - apple/configd.git/blame - configd.tproj/_configget.c
configd-53.1.tar.gz
[apple/configd.git] / configd.tproj / _configget.c
CommitLineData
5958d7c0 1/*
a5f60add 2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
5958d7c0
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
0fae82ee
A
23/*
24 * Modification History
25 *
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
28 *
29 * March 24, 2000 Allan Nathanson <ajn@apple.com>
30 * - initial revision
31 */
32
5958d7c0
A
33#include "configd.h"
34#include "session.h"
35
0fae82ee
A
36int
37__SCDynamicStoreCopyValue(SCDynamicStoreRef store, CFStringRef key, CFPropertyListRef *value)
5958d7c0 38{
0fae82ee
A
39 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
40 CFDictionaryRef dict;
5958d7c0 41
0fae82ee
A
42 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreCopyValue:"));
43 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" key = %@"), key);
5958d7c0 44
0fae82ee
A
45 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
46 return kSCStatusNoStoreSession; /* you must have an open session to play */
5958d7c0
A
47 }
48
0fae82ee 49 dict = CFDictionaryGetValue(storeData, key);
5958d7c0
A
50 if ((dict == NULL) || (CFDictionaryContainsKey(dict, kSCDData) == FALSE)) {
51 /* key doesn't exist (or data never defined) */
0fae82ee 52 return kSCStatusNoKey;
5958d7c0
A
53 }
54
5958d7c0 55 /* Return the data associated with the key */
0fae82ee 56 *value = CFRetain(CFDictionaryGetValue(dict, kSCDData));
5958d7c0 57
0fae82ee 58 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" value = %@"), *value);
5958d7c0 59
0fae82ee 60 return kSCStatusOK;
5958d7c0
A
61}
62
5958d7c0
A
63kern_return_t
64_configget(mach_port_t server,
65 xmlData_t keyRef, /* raw XML bytes */
66 mach_msg_type_number_t keyLen,
67 xmlDataOut_t *dataRef, /* raw XML bytes */
68 mach_msg_type_number_t *dataLen,
69 int *newInstance,
0fae82ee 70 int *sc_status
5958d7c0
A
71)
72{
5958d7c0 73 CFStringRef key; /* key (un-serialized) */
a5f60add
A
74 serverSessionRef mySession = getSession(server);
75 Boolean ok;
0fae82ee 76 CFPropertyListRef value;
5958d7c0 77
0fae82ee
A
78 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Get key from configuration database."));
79 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" server = %d"), server);
5958d7c0 80
a5f60add
A
81 *dataRef = NULL;
82 *dataLen = 0;
83
5958d7c0 84 /* un-serialize the key */
a5f60add 85 if (!_SCUnserialize((CFPropertyListRef *)&key, (void *)keyRef, keyLen)) {
0fae82ee
A
86 *sc_status = kSCStatusFailed;
87 return KERN_SUCCESS;
a5f60add
A
88 }
89
90 if (!isA_CFString(key)) {
91 CFRelease(key);
0fae82ee 92 *sc_status = kSCStatusInvalidArgument;
5958d7c0
A
93 return KERN_SUCCESS;
94 }
95
0fae82ee 96 *sc_status = __SCDynamicStoreCopyValue(mySession->store, key, &value);
5958d7c0 97 CFRelease(key);
0fae82ee 98 if (*sc_status != kSCStatusOK) {
5958d7c0
A
99 return KERN_SUCCESS;
100 }
101
a5f60add
A
102 /* serialize the data */
103 ok = _SCSerialize(value, NULL, (void **)dataRef, (CFIndex *)dataLen);
0fae82ee 104 CFRelease(value);
a5f60add 105 if (!ok) {
0fae82ee 106 *sc_status = kSCStatusFailed;
5958d7c0
A
107 return KERN_SUCCESS;
108 }
109
5958d7c0
A
110 /*
111 * return the instance number associated with the returned data.
112 */
0fae82ee
A
113 *newInstance = 1;
114
115 return KERN_SUCCESS;
116}
117
118/*
119 * "context" argument for addSpecificKey() and addSpecificPattern()
120 */
121typedef struct {
122 SCDynamicStoreRef store;
123 CFMutableDictionaryRef dict;
124} addSpecific, *addSpecificRef;
125
126static void
127addSpecificKey(const void *value, void *context)
128{
129 CFStringRef key = (CFStringRef)value;
130 addSpecificRef myContextRef = (addSpecificRef)context;
131 int sc_status;
132 CFPropertyListRef data;
5958d7c0 133
0fae82ee
A
134 if (!isA_CFString(key)) {
135 return;
136 }
137
138 sc_status = __SCDynamicStoreCopyValue(myContextRef->store, key, &data);
139 if (sc_status == kSCStatusOK) {
140 CFDictionaryAddValue(myContextRef->dict, key, data);
141 CFRelease(data);
142 }
143
144 return;
145}
146
147static void
148addSpecificPattern(const void *value, void *context)
149{
150 CFStringRef pattern = (CFStringRef)value;
151 addSpecificRef myContextRef = (addSpecificRef)context;
152 int sc_status;
153 CFArrayRef keys;
154
155 if (!isA_CFString(pattern)) {
156 return;
157 }
158
159 sc_status = __SCDynamicStoreCopyKeyList(myContextRef->store, pattern, TRUE, &keys);
160 if (sc_status == kSCStatusOK) {
161 CFArrayApplyFunction(keys,
162 CFRangeMake(0, CFArrayGetCount(keys)),
163 addSpecificKey,
164 context);
165 CFRelease(keys);
166 }
167
168 return;
169}
170
171kern_return_t
172_configget_m(mach_port_t server,
173 xmlData_t keysRef,
174 mach_msg_type_number_t keysLen,
175 xmlData_t patternsRef,
176 mach_msg_type_number_t patternsLen,
177 xmlDataOut_t *dataRef,
178 mach_msg_type_number_t *dataLen,
179 int *sc_status)
180{
0fae82ee 181 CFArrayRef keys = NULL; /* keys (un-serialized) */
0fae82ee 182 addSpecific myContext;
a5f60add
A
183 serverSessionRef mySession = getSession(server);
184 Boolean ok;
185 CFArrayRef patterns = NULL; /* patterns (un-serialized) */
0fae82ee
A
186
187 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Get key from configuration database."));
188 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" server = %d"), server);
189
a5f60add
A
190 *dataRef = NULL;
191 *dataLen = 0;
0fae82ee
A
192 *sc_status = kSCStatusOK;
193
194 if (keysRef && (keysLen > 0)) {
0fae82ee 195 /* un-serialize the keys */
a5f60add 196 if (!_SCUnserialize((CFPropertyListRef *)&keys, (void *)keysRef, keysLen)) {
0fae82ee 197 *sc_status = kSCStatusFailed;
a5f60add
A
198 }
199
200 if (!isA_CFArray(keys)) {
0fae82ee
A
201 *sc_status = kSCStatusInvalidArgument;
202 }
203 }
204
205 if (patternsRef && (patternsLen > 0)) {
0fae82ee 206 /* un-serialize the patterns */
a5f60add 207 if (!_SCUnserialize((CFPropertyListRef *)&patterns, (void *)patternsRef, patternsLen)) {
0fae82ee 208 *sc_status = kSCStatusFailed;
a5f60add
A
209 }
210
211 if (!isA_CFArray(patterns)) {
0fae82ee
A
212 *sc_status = kSCStatusInvalidArgument;
213 }
214 }
215
216 if (*sc_status != kSCStatusOK) {
217 if (keys) CFRelease(keys);
218 if (patterns) CFRelease(patterns);
0fae82ee
A
219 return KERN_SUCCESS;
220 }
221
222 myContext.store = mySession->store;
223 myContext.dict = CFDictionaryCreateMutable(NULL,
224 0,
225 &kCFTypeDictionaryKeyCallBacks,
226 &kCFTypeDictionaryValueCallBacks);
227
228 if (keys) {
229 CFArrayApplyFunction(keys,
230 CFRangeMake(0, CFArrayGetCount(keys)),
231 addSpecificKey,
232 &myContext);
233 CFRelease(keys);
234 }
235
236 if (patterns) {
237 CFArrayApplyFunction(patterns,
238 CFRangeMake(0, CFArrayGetCount(patterns)),
239 addSpecificPattern,
240 &myContext);
241 CFRelease(patterns);
242 }
243
a5f60add
A
244 /* serialize the dictionary of matching keys/patterns */
245 ok = _SCSerialize(myContext.dict, NULL, (void **)dataRef, (CFIndex *)dataLen);
0fae82ee 246 CFRelease(myContext.dict);
a5f60add 247 if (!ok) {
0fae82ee 248 *sc_status = kSCStatusFailed;
0fae82ee
A
249 return KERN_SUCCESS;
250 }
251
5958d7c0
A
252 return KERN_SUCCESS;
253}