]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configlist.c
a43e889b6b146e93aeafda75cf073ea0b08d8973
[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 * 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 #include "pattern.h"
37
38 #define N_QUICK 64
39
40 __private_extern__
41 int
42 __SCDynamicStoreCopyKeyList(SCDynamicStoreRef store, CFStringRef key, Boolean isRegex, CFArrayRef *subKeys)
43 {
44 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
45 CFMutableArrayRef keyArray;
46 regex_t preg;
47 CFIndex storeCnt;
48 CFStringRef storeStr;
49 CFDictionaryRef storeValue;
50
51 if (_configd_verbose) {
52 SCLog(TRUE, LOG_DEBUG, CFSTR("__SCDynamicStoreCopyKeyList:"));
53 SCLog(TRUE, LOG_DEBUG, CFSTR(" key = %@"), key);
54 SCLog(TRUE, LOG_DEBUG, CFSTR(" isRegex = %s"), isRegex ? "TRUE" : "FALSE");
55 }
56
57 if (!store || (storePrivate->server == MACH_PORT_NULL)) {
58 return kSCStatusNoStoreSession; /* you must have an open session to play */
59 }
60
61 storeCnt = CFDictionaryGetCount(storeData);
62 keyArray = CFArrayCreateMutable(NULL, storeCnt, &kCFTypeArrayCallBacks);
63
64 if (isRegex) {
65 CFStringRef reErrStr;
66
67 if (!patternCompile(key, &preg, &reErrStr)) {
68 CFArrayAppendValue(keyArray, reErrStr);
69 CFRelease(reErrStr);
70 *subKeys = CFArrayCreateCopy(NULL, keyArray);
71 CFRelease(keyArray);
72 return kSCStatusFailed;
73 }
74 }
75
76 if (storeCnt > 0) {
77 int i;
78 const void * storeKeys_q[N_QUICK];
79 const void ** storeKeys = storeKeys_q;
80 const void * storeValues_q[N_QUICK];
81 const void ** storeValues = storeValues_q;
82
83 if (storeCnt > (CFIndex)(sizeof(storeKeys_q) / sizeof(CFStringRef))) {
84 storeKeys = CFAllocatorAllocate(NULL, storeCnt * sizeof(CFStringRef), 0);
85 storeValues = CFAllocatorAllocate(NULL, storeCnt * sizeof(CFStringRef), 0);
86 }
87
88 CFDictionaryGetKeysAndValues(storeData, storeKeys, storeValues);
89 for (i = 0; i < storeCnt; i++) {
90 storeStr = (CFStringRef)storeKeys[i];
91 storeValue = (CFDictionaryRef)storeValues[i];
92 if (isRegex) {
93 /*
94 * only return those keys which match the regular
95 * expression specified in the provided key.
96 */
97
98 int reError;
99 char storeKey_q[256];
100 char * storeKey = storeKey_q;
101 CFIndex storeKeyLen = CFStringGetLength(storeStr) + 1;
102
103 if (storeKeyLen > (CFIndex)sizeof(storeKey_q))
104 storeKey = CFAllocatorAllocate(NULL, storeKeyLen, 0);
105 if (_SC_cfstring_to_cstring(storeStr, storeKey, storeKeyLen, kCFStringEncodingASCII) == NULL) {
106 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("could not convert store key to C string"));
107 if (storeKey != storeKey_q) CFAllocatorDeallocate(NULL, storeKey);
108 continue;
109 }
110
111 reError = regexec(&preg, storeKey, 0, NULL, 0);
112 switch (reError) {
113 case 0 :
114 /* we've got a match */
115 if (CFDictionaryContainsKey(storeValue, kSCDData))
116 CFArrayAppendValue(keyArray, storeStr);
117 break;
118 case REG_NOMATCH :
119 /* no match */
120 break;
121 default : {
122 char reErrBuf[256];
123 int reErrStrLen;
124
125 reErrStrLen = regerror(reError,
126 &preg,
127 reErrBuf,
128 sizeof(reErrBuf));
129 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("regexec(): %s"), reErrBuf);
130 break;
131 }
132 }
133 if (storeKey != storeKey_q) CFAllocatorDeallocate(NULL, storeKey);
134 } else {
135 /*
136 * only return those keys which are prefixed by the
137 * provided key string and have data.
138 */
139 if (((CFStringGetLength(key) == 0) || CFStringHasPrefix(storeStr, key)) &&
140 CFDictionaryContainsKey(storeValue, kSCDData)) {
141 CFArrayAppendValue(keyArray, storeStr);
142 }
143 }
144 }
145
146 if (storeKeys != storeKeys_q) {
147 CFAllocatorDeallocate(NULL, storeKeys);
148 CFAllocatorDeallocate(NULL, storeValues);
149 }
150 }
151
152 if (isRegex) {
153 regfree(&preg);
154 }
155
156 *subKeys = keyArray;
157
158 return kSCStatusOK;
159 }
160
161
162 __private_extern__
163 kern_return_t
164 _configlist(mach_port_t server,
165 xmlData_t keyRef, /* raw XML bytes */
166 mach_msg_type_number_t keyLen,
167 int isRegex,
168 xmlDataOut_t *listRef, /* raw XML bytes */
169 mach_msg_type_number_t *listLen,
170 int *sc_status
171 )
172 {
173 CFStringRef key; /* key (un-serialized) */
174 serverSessionRef mySession = getSession(server);
175 Boolean ok;
176 CFArrayRef subKeys; /* array of CFStringRef's */
177
178 if (_configd_verbose) {
179 SCLog(TRUE, LOG_DEBUG, CFSTR("List keys in configuration database."));
180 SCLog(TRUE, LOG_DEBUG, CFSTR(" server = %d"), server);
181 }
182
183 *listRef = NULL;
184 *listLen = 0;
185
186 /* un-serialize the key */
187 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
188 *sc_status = kSCStatusFailed;
189 return KERN_SUCCESS;
190 }
191
192 if (!isA_CFString(key)) {
193 *sc_status = kSCStatusInvalidArgument;
194 CFRelease(key);
195 return KERN_SUCCESS;
196 }
197
198 if (!mySession) {
199 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
200 CFRelease(key);
201 return KERN_SUCCESS;
202 }
203
204 *sc_status = __SCDynamicStoreCopyKeyList(mySession->store, key, isRegex != 0, &subKeys);
205 CFRelease(key);
206 if (*sc_status != kSCStatusOK) {
207 return KERN_SUCCESS;
208 }
209
210 /* serialize the list of keys */
211 ok = _SCSerialize(subKeys, NULL, (void **)listRef, (CFIndex *)listLen);
212 CFRelease(subKeys);
213 if (!ok) {
214 *sc_status = kSCStatusFailed;
215 return KERN_SUCCESS;
216 }
217
218 return KERN_SUCCESS;
219 }