]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_snapshot.c
configd-888.1.2.tar.gz
[apple/configd.git] / configd.tproj / _snapshot.c
1 /*
2 * Copyright (c) 2000-2006, 2009-2011, 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 * April 14, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34 #include <fcntl.h>
35 #include <paths.h>
36 #include <unistd.h>
37
38 #include "configd.h"
39 #include "configd_server.h"
40 #include "session.h"
41 #include "plugin_support.h"
42
43
44 #define SNAPSHOT_PATH_STATE _PATH_VARTMP "configd-state"
45 #define SNAPSHOT_PATH_STORE _PATH_VARTMP "configd-store.plist"
46 #define SNAPSHOT_PATH_PATTERN _PATH_VARTMP "configd-pattern.plist"
47 #define SNAPSHOT_PATH_SESSION _PATH_VARTMP "configd-session.plist"
48
49
50 #define N_QUICK 100
51
52 static CF_RETURNS_RETAINED CFDictionaryRef
53 _expandStore(CFDictionaryRef storeData)
54 {
55 const void * keys_q[N_QUICK];
56 const void ** keys = keys_q;
57 CFIndex nElements;
58 CFDictionaryRef newStoreData = NULL;
59 const void * nValues_q[N_QUICK];
60 const void ** nValues = nValues_q;
61 const void * oValues_q[N_QUICK];
62 const void ** oValues = oValues_q;
63
64 nElements = CFDictionaryGetCount(storeData);
65 if (nElements > 0) {
66 CFIndex i;
67
68 if (nElements > (CFIndex)(sizeof(keys_q) / sizeof(CFTypeRef))) {
69 keys = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
70 oValues = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
71 nValues = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
72 }
73 bzero(nValues, nElements * sizeof(CFTypeRef));
74
75 CFDictionaryGetKeysAndValues(storeData, keys, oValues);
76 for (i = 0; i < nElements; i++) {
77 CFDataRef data;
78
79 data = CFDictionaryGetValue(oValues[i], kSCDData);
80 if (data) {
81 CFPropertyListRef plist;
82
83 nValues[i] = CFDictionaryCreateMutableCopy(NULL, 0, oValues[i]);
84
85 if (!_SCUnserialize(&plist, data, NULL, 0)) {
86 SC_log(LOG_NOTICE, "_SCUnserialize() failed, key=%@", keys[i]);
87 continue;
88 }
89
90 CFDictionarySetValue((CFMutableDictionaryRef)nValues[i],
91 kSCDData,
92 plist);
93 CFRelease(plist);
94 } else {
95 nValues[i] = CFRetain(oValues[i]);
96 }
97 }
98 }
99
100 newStoreData = CFDictionaryCreate(NULL,
101 keys,
102 nValues,
103 nElements,
104 &kCFTypeDictionaryKeyCallBacks,
105 &kCFTypeDictionaryValueCallBacks);
106
107 if (nElements > 0) {
108 CFIndex i;
109
110 for (i = 0; i < nElements; i++) {
111 CFRelease(nValues[i]);
112 }
113
114 if (keys != keys_q) {
115 CFAllocatorDeallocate(NULL, keys);
116 CFAllocatorDeallocate(NULL, oValues);
117 CFAllocatorDeallocate(NULL, nValues);
118 }
119 }
120
121 return newStoreData;
122 }
123
124
125 __private_extern__
126 int
127 __SCDynamicStoreSnapshot(SCDynamicStoreRef store)
128 {
129 CFDictionaryRef expandedStoreData;
130 FILE *f;
131 int fd;
132 CFDataRef xmlData;
133
134 /* Save a snapshot of configd's "state" */
135
136 (void) unlink(SNAPSHOT_PATH_STATE);
137 fd = open(SNAPSHOT_PATH_STATE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
138 if (fd == -1) {
139 return kSCStatusFailed;
140 }
141 f = fdopen(fd, "w");
142 if (f == NULL) {
143 return kSCStatusFailed;
144 }
145 SCPrint(TRUE, f, CFSTR("Main thread :\n\n"));
146 SCPrint(TRUE, f, CFSTR("%@\n"), CFRunLoopGetCurrent());
147 if (plugin_runLoop != NULL) {
148 SCPrint(TRUE, f, CFSTR("Plug-in thread :\n\n"));
149 SCPrint(TRUE, f, CFSTR("%@\n"), plugin_runLoop);
150 }
151 listSessions(f);
152 (void) fclose(f);
153
154 /* Save a snapshot of the "store" data */
155
156 (void) unlink(SNAPSHOT_PATH_STORE);
157 fd = open(SNAPSHOT_PATH_STORE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
158 if (fd == -1) {
159 return kSCStatusFailed;
160 }
161
162 expandedStoreData = _expandStore(storeData);
163 xmlData = CFPropertyListCreateData(NULL, expandedStoreData, kCFPropertyListXMLFormat_v1_0, 0, NULL);
164 CFRelease(expandedStoreData);
165 if (xmlData == NULL) {
166 SC_log(LOG_NOTICE, "CFPropertyListCreateData() failed");
167 close(fd);
168 return kSCStatusFailed;
169 }
170 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
171 (void) close(fd);
172 CFRelease(xmlData);
173
174 /* Save a snapshot of the "pattern" data */
175
176 (void) unlink(SNAPSHOT_PATH_PATTERN);
177 fd = open(SNAPSHOT_PATH_PATTERN, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
178 if (fd == -1) {
179 return kSCStatusFailed;
180 }
181
182 xmlData = CFPropertyListCreateData(NULL, patternData, kCFPropertyListXMLFormat_v1_0, 0, NULL);
183 if (xmlData == NULL) {
184 SC_log(LOG_NOTICE, "CFPropertyListCreateData() failed");
185 close(fd);
186 return kSCStatusFailed;
187 }
188 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
189 (void) close(fd);
190 CFRelease(xmlData);
191
192 /* Save a snapshot of the "session" data */
193
194 (void) unlink(SNAPSHOT_PATH_SESSION);
195 fd = open(SNAPSHOT_PATH_SESSION, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
196 if (fd == -1) {
197 return kSCStatusFailed;
198 }
199
200 xmlData = CFPropertyListCreateData(NULL, sessionData, kCFPropertyListXMLFormat_v1_0, 0, NULL);
201 if (xmlData == NULL) {
202 SC_log(LOG_NOTICE, "CFPropertyListCreateData() failed");
203 close(fd);
204 return kSCStatusFailed;
205 }
206 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
207 (void) close(fd);
208 CFRelease(xmlData);
209
210 return kSCStatusOK;
211 }
212
213
214 __private_extern__
215 kern_return_t
216 _snapshot(mach_port_t server, int *sc_status, audit_token_t audit_token)
217 {
218 serverSessionRef mySession;
219
220 mySession = getSession(server);
221 if (mySession == NULL) {
222 mySession = tempSession(server, CFSTR("SCDynamicStoreSnapshot"), audit_token);
223 if (mySession == NULL) {
224 /* you must have an open session to play */
225 return kSCStatusNoStoreSession;
226 }
227 }
228
229 if (!hasRootAccess(mySession)) {
230 return kSCStatusAccessError;
231 }
232
233 *sc_status = __SCDynamicStoreSnapshot(mySession->store);
234 return KERN_SUCCESS;
235 }