]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configlist.c
bd0277adb722502c07e3b7aeb3466549f68a49cf
[apple/configd.git] / configd.tproj / _configlist.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Modification History
28 *
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
31 *
32 * March 24, 2000 Allan Nathanson <ajn@apple.com>
33 * - initial revision
34 */
35
36 #include "configd.h"
37 #include "session.h"
38 #include "pattern.h"
39
40 #define N_QUICK 64
41
42 __private_extern__
43 int
44 __SCDynamicStoreCopyKeyList(SCDynamicStoreRef store, CFStringRef key, Boolean isRegex, CFArrayRef *subKeys)
45 {
46 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
47 CFMutableArrayRef keyArray;
48 regex_t preg;
49 CFIndex storeCnt;
50 CFStringRef storeStr;
51 CFDictionaryRef storeValue;
52
53 if (_configd_verbose) {
54 SCLog(TRUE, LOG_DEBUG, CFSTR("__SCDynamicStoreCopyKeyList:"));
55 SCLog(TRUE, LOG_DEBUG, CFSTR(" key = %@"), key);
56 SCLog(TRUE, LOG_DEBUG, CFSTR(" isRegex = %s"), isRegex ? "TRUE" : "FALSE");
57 }
58
59 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
60 return kSCStatusNoStoreSession; /* you must have an open session to play */
61 }
62
63 storeCnt = CFDictionaryGetCount(storeData);
64 keyArray = CFArrayCreateMutable(NULL, storeCnt, &kCFTypeArrayCallBacks);
65
66 if (isRegex) {
67 CFStringRef reErrStr;
68
69 if (!patternCompile(key, &preg, &reErrStr)) {
70 CFArrayAppendValue(keyArray, reErrStr);
71 CFRelease(reErrStr);
72 *subKeys = CFArrayCreateCopy(NULL, keyArray);
73 CFRelease(keyArray);
74 return kSCStatusFailed;
75 }
76 }
77
78 if (storeCnt > 0) {
79 int i;
80 const void * storeKeys_q[N_QUICK];
81 const void ** storeKeys = storeKeys_q;
82 const void * storeValues_q[N_QUICK];
83 const void ** storeValues = storeValues_q;
84
85 if (storeCnt > (CFIndex)(sizeof(storeKeys_q) / sizeof(CFStringRef))) {
86 storeKeys = CFAllocatorAllocate(NULL, storeCnt * sizeof(CFStringRef), 0);
87 storeValues = CFAllocatorAllocate(NULL, storeCnt * sizeof(CFStringRef), 0);
88 }
89
90 CFDictionaryGetKeysAndValues(storeData, storeKeys, storeValues);
91 for (i = 0; i < storeCnt; i++) {
92 storeStr = (CFStringRef)storeKeys[i];
93 storeValue = (CFDictionaryRef)storeValues[i];
94 if (isRegex) {
95 /*
96 * only return those keys which match the regular
97 * expression specified in the provided key.
98 */
99
100 int reError;
101 char storeKey_q[256];
102 char * storeKey = storeKey_q;
103 CFIndex storeKeyLen = CFStringGetLength(storeStr) + 1;
104
105 if (storeKeyLen > (CFIndex)sizeof(storeKey_q))
106 storeKey = CFAllocatorAllocate(NULL, storeKeyLen, 0);
107 if (_SC_cfstring_to_cstring(storeStr, storeKey, storeKeyLen, kCFStringEncodingASCII) == NULL) {
108 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("could not convert store key to C string"));
109 if (storeKey != storeKey_q) CFAllocatorDeallocate(NULL, storeKey);
110 continue;
111 }
112
113 reError = regexec(&preg, storeKey, 0, NULL, 0);
114 switch (reError) {
115 case 0 :
116 /* we've got a match */
117 if (CFDictionaryContainsKey(storeValue, kSCDData))
118 CFArrayAppendValue(keyArray, storeStr);
119 break;
120 case REG_NOMATCH :
121 /* no match */
122 break;
123 default : {
124 char reErrBuf[256];
125 int reErrStrLen;
126
127 reErrStrLen = regerror(reError,
128 &preg,
129 reErrBuf,
130 sizeof(reErrBuf));
131 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("regexec(): %s"), reErrBuf);
132 break;
133 }
134 }
135 if (storeKey != storeKey_q) CFAllocatorDeallocate(NULL, storeKey);
136 } else {
137 /*
138 * only return those keys which are prefixed by the
139 * provided key string and have data.
140 */
141 if (((CFStringGetLength(key) == 0) || CFStringHasPrefix(storeStr, key)) &&
142 CFDictionaryContainsKey(storeValue, kSCDData)) {
143 CFArrayAppendValue(keyArray, storeStr);
144 }
145 }
146 }
147
148 if (storeKeys != storeKeys_q) {
149 CFAllocatorDeallocate(NULL, storeKeys);
150 CFAllocatorDeallocate(NULL, storeValues);
151 }
152 }
153
154 if (isRegex) {
155 regfree(&preg);
156 }
157
158 *subKeys = keyArray;
159
160 return kSCStatusOK;
161 }
162
163
164 __private_extern__
165 kern_return_t
166 _configlist(mach_port_t server,
167 xmlData_t keyRef, /* raw XML bytes */
168 mach_msg_type_number_t keyLen,
169 int isRegex,
170 xmlDataOut_t *listRef, /* raw XML bytes */
171 mach_msg_type_number_t *listLen,
172 int *sc_status
173 )
174 {
175 CFStringRef key; /* key (un-serialized) */
176 serverSessionRef mySession = getSession(server);
177 Boolean ok;
178 CFArrayRef subKeys; /* array of CFStringRef's */
179
180 if (_configd_verbose) {
181 SCLog(TRUE, LOG_DEBUG, CFSTR("List keys in configuration database."));
182 SCLog(TRUE, LOG_DEBUG, CFSTR(" server = %d"), server);
183 }
184
185 *listRef = NULL;
186 *listLen = 0;
187
188 /* un-serialize the key */
189 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
190 *sc_status = kSCStatusFailed;
191 return KERN_SUCCESS;
192 }
193
194 if (!isA_CFString(key)) {
195 *sc_status = kSCStatusInvalidArgument;
196 CFRelease(key);
197 return KERN_SUCCESS;
198 }
199
200 if (!mySession) {
201 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
202 CFRelease(key);
203 return KERN_SUCCESS;
204 }
205
206 *sc_status = __SCDynamicStoreCopyKeyList(mySession->store, key, isRegex != 0, &subKeys);
207 CFRelease(key);
208 if (*sc_status != kSCStatusOK) {
209 return KERN_SUCCESS;
210 }
211
212 /* serialize the list of keys */
213 ok = _SCSerialize(subKeys, NULL, (void **)listRef, (CFIndex *)listLen);
214 CFRelease(subKeys);
215 if (!ok) {
216 *sc_status = kSCStatusFailed;
217 return KERN_SUCCESS;
218 }
219
220 return KERN_SUCCESS;
221 }