]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_configlist.c
configd-801.1.1.tar.gz
[apple/configd.git] / configd.tproj / _configlist.c
1 /*
2 * Copyright (c) 2000-2008, 2011, 2013, 2015 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 #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 CFMutableArrayRef keyArray;
45 CFIndex storeCnt;
46 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
47 CFStringRef storeStr;
48 CFDictionaryRef storeValue;
49
50 SC_trace(_configd_trace, "%s : %5d : %s : %@\n",
51 "list ",
52 storePrivate->server,
53 isRegex ? "pattern" : "key",
54 key);
55
56 if (isRegex) {
57 *subKeys = patternCopyMatches(key);
58 return (*subKeys != NULL) ? kSCStatusOK : kSCStatusFailed;
59 }
60
61 storeCnt = CFDictionaryGetCount(storeData);
62 keyArray = CFArrayCreateMutable(NULL, storeCnt, &kCFTypeArrayCallBacks);
63 if (storeCnt > 0) {
64 int i;
65 const void * storeKeys_q[N_QUICK];
66 const void ** storeKeys = storeKeys_q;
67 const void * storeValues_q[N_QUICK];
68 const void ** storeValues = storeValues_q;
69
70 if (storeCnt > (CFIndex)(sizeof(storeKeys_q) / sizeof(CFStringRef))) {
71 storeKeys = CFAllocatorAllocate(NULL, storeCnt * sizeof(CFStringRef), 0);
72 storeValues = CFAllocatorAllocate(NULL, storeCnt * sizeof(CFStringRef), 0);
73 }
74
75 CFDictionaryGetKeysAndValues(storeData, storeKeys, storeValues);
76 for (i = 0; i < storeCnt; i++) {
77 storeStr = (CFStringRef)storeKeys[i];
78 storeValue = (CFDictionaryRef)storeValues[i];
79 /*
80 * only return those keys which are prefixed by the
81 * provided key string and have data.
82 */
83 if (((CFStringGetLength(key) == 0) || CFStringHasPrefix(storeStr, key)) &&
84 CFDictionaryContainsKey(storeValue, kSCDData)) {
85 CFArrayAppendValue(keyArray, storeStr);
86 }
87 }
88
89 if (storeKeys != storeKeys_q) {
90 CFAllocatorDeallocate(NULL, storeKeys);
91 CFAllocatorDeallocate(NULL, storeValues);
92 }
93 }
94
95 *subKeys = CFArrayCreateCopy(NULL, keyArray);
96 CFRelease(keyArray);
97 return kSCStatusOK;
98 }
99
100
101 __private_extern__
102 kern_return_t
103 _configlist(mach_port_t server,
104 xmlData_t keyRef, /* raw XML bytes */
105 mach_msg_type_number_t keyLen,
106 int isRegex,
107 xmlDataOut_t *listRef, /* raw XML bytes */
108 mach_msg_type_number_t *listLen,
109 int *sc_status,
110 audit_token_t audit_token)
111 {
112 CFStringRef key = NULL; /* key (un-serialized) */
113 CFIndex len;
114 serverSessionRef mySession;
115 Boolean ok;
116 CFArrayRef subKeys; /* array of CFStringRef's */
117
118 *listRef = NULL;
119 *listLen = 0;
120
121 /* un-serialize the key */
122 if (!_SCUnserializeString(&key, NULL, (void *)keyRef, keyLen)) {
123 *sc_status = kSCStatusFailed;
124 goto done;
125 }
126
127 if (!isA_CFString(key)) {
128 *sc_status = kSCStatusInvalidArgument;
129 goto done;
130 }
131
132 mySession = getSession(server);
133 if (mySession == NULL) {
134 mySession = tempSession(server, CFSTR("SCDynamicStoreCopyKeyList"), audit_token);
135 if (mySession == NULL) {
136 /* you must have an open session to play */
137 *sc_status = kSCStatusNoStoreSession;
138 goto done;
139 }
140 }
141
142 *sc_status = __SCDynamicStoreCopyKeyList(mySession->store, key, isRegex != 0, &subKeys);
143 if (*sc_status != kSCStatusOK) {
144 goto done;
145 }
146
147 /* serialize the list of keys */
148 ok = _SCSerialize(subKeys, NULL, (void **)listRef, &len);
149 *listLen = (mach_msg_type_number_t)len;
150 CFRelease(subKeys);
151 if (!ok) {
152 *sc_status = kSCStatusFailed;
153 goto done;
154 }
155
156 done :
157
158 if (key) CFRelease(key);
159 return KERN_SUCCESS;
160 }