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