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