Commit | Line | Data |
---|---|---|
5958d7c0 | 1 | /* |
a40a14f8 | 2 | * Copyright (c) 2000-2006, 2009 Apple 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 | * April 14, 2000 Allan Nathanson <ajn@apple.com> | |
31 | * - initial revision | |
32 | */ | |
33 | ||
5958d7c0 A |
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" | |
edebe297 | 41 | #include "plugin_support.h" |
5958d7c0 A |
42 | |
43 | ||
edebe297 | 44 | #define SNAPSHOT_PATH_STATE _PATH_VARTMP "configd-state" |
0fae82ee | 45 | #define SNAPSHOT_PATH_STORE _PATH_VARTMP "configd-store.xml" |
009ee3c6 | 46 | #define SNAPSHOT_PATH_PATTERN _PATH_VARTMP "configd-pattern.xml" |
5958d7c0 A |
47 | #define SNAPSHOT_PATH_SESSION _PATH_VARTMP "configd-session.xml" |
48 | ||
49 | ||
009ee3c6 A |
50 | #define N_QUICK 100 |
51 | ||
52 | static 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 | ||
dbf6a266 | 83 | if (!_SCUnserialize(&plist, data, NULL, 0)) { |
009ee3c6 A |
84 | goto done; |
85 | } | |
86 | ||
87 | nValues[i] = CFDictionaryCreateMutableCopy(NULL, 0, oValues[i]); | |
88 | CFDictionarySetValue((CFMutableDictionaryRef)nValues[i], | |
89 | kSCDData, | |
90 | plist); | |
91 | CFRelease(plist); | |
92 | } else { | |
93 | nValues[i] = CFRetain(oValues[i]); | |
94 | } | |
95 | } | |
96 | } | |
97 | ||
98 | newStoreData = CFDictionaryCreate(NULL, | |
99 | keys, | |
100 | nValues, | |
101 | nElements, | |
102 | &kCFTypeDictionaryKeyCallBacks, | |
103 | &kCFTypeDictionaryValueCallBacks); | |
104 | ||
105 | done : | |
106 | ||
107 | if (nElements > 0) { | |
108 | CFIndex i; | |
109 | ||
110 | for (i = 0; i < nElements; i++) { | |
111 | if (nValues[i]) 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__ | |
0fae82ee A |
126 | int |
127 | __SCDynamicStoreSnapshot(SCDynamicStoreRef store) | |
5958d7c0 | 128 | { |
009ee3c6 | 129 | CFDictionaryRef expandedStoreData; |
edebe297 | 130 | FILE *f; |
0fae82ee A |
131 | int fd; |
132 | serverSessionRef mySession; | |
133 | SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store; | |
134 | CFDataRef xmlData; | |
135 | ||
0fae82ee A |
136 | /* check credentials */ |
137 | ||
138 | mySession = getSession(storePrivate->server); | |
a40a14f8 | 139 | if (!hasRootAccess(mySession)) { |
0fae82ee A |
140 | return kSCStatusAccessError; |
141 | } | |
5958d7c0 | 142 | |
edebe297 A |
143 | /* Save a snapshot of configd's "state" */ |
144 | ||
145 | (void) unlink(SNAPSHOT_PATH_STATE); | |
146 | fd = open(SNAPSHOT_PATH_STATE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644); | |
147 | if (fd == -1) { | |
148 | return kSCStatusFailed; | |
149 | } | |
150 | f = fdopen(fd, "w"); | |
151 | if (f == NULL) { | |
152 | return kSCStatusFailed; | |
153 | } | |
154 | SCPrint(TRUE, f, CFSTR("Main thread :\n\n")); | |
155 | SCPrint(TRUE, f, CFSTR("%@\n"), CFRunLoopGetCurrent()); | |
156 | if (plugin_runLoop != NULL) { | |
157 | SCPrint(TRUE, f, CFSTR("Plug-in thread :\n\n")); | |
158 | SCPrint(TRUE, f, CFSTR("%@\n"), plugin_runLoop); | |
159 | } | |
a40a14f8 | 160 | listSessions(f); |
edebe297 A |
161 | (void) fclose(f); |
162 | ||
0fae82ee | 163 | /* Save a snapshot of the "store" data */ |
5958d7c0 | 164 | |
0fae82ee | 165 | (void) unlink(SNAPSHOT_PATH_STORE); |
edebe297 A |
166 | fd = open(SNAPSHOT_PATH_STORE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644); |
167 | if (fd == -1) { | |
0fae82ee | 168 | return kSCStatusFailed; |
5958d7c0 A |
169 | } |
170 | ||
009ee3c6 A |
171 | expandedStoreData = _expandStore(storeData); |
172 | xmlData = CFPropertyListCreateXMLData(NULL, expandedStoreData); | |
173 | CFRelease(expandedStoreData); | |
174 | if (!xmlData) { | |
dbf6a266 | 175 | SCLog(TRUE, LOG_ERR, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed")); |
009ee3c6 A |
176 | close(fd); |
177 | return kSCStatusFailed; | |
178 | } | |
179 | (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData)); | |
180 | (void) close(fd); | |
181 | CFRelease(xmlData); | |
182 | ||
183 | /* Save a snapshot of the "pattern" data */ | |
184 | ||
185 | (void) unlink(SNAPSHOT_PATH_PATTERN); | |
edebe297 A |
186 | fd = open(SNAPSHOT_PATH_PATTERN, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644); |
187 | if (fd == -1) { | |
009ee3c6 A |
188 | return kSCStatusFailed; |
189 | } | |
190 | ||
191 | xmlData = CFPropertyListCreateXMLData(NULL, patternData); | |
a5f60add | 192 | if (!xmlData) { |
dbf6a266 | 193 | SCLog(TRUE, LOG_ERR, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed")); |
a5f60add A |
194 | close(fd); |
195 | return kSCStatusFailed; | |
196 | } | |
5958d7c0 A |
197 | (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData)); |
198 | (void) close(fd); | |
199 | CFRelease(xmlData); | |
200 | ||
201 | /* Save a snapshot of the "session" data */ | |
202 | ||
203 | (void) unlink(SNAPSHOT_PATH_SESSION); | |
edebe297 A |
204 | fd = open(SNAPSHOT_PATH_SESSION, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644); |
205 | if (fd == -1) { | |
0fae82ee | 206 | return kSCStatusFailed; |
5958d7c0 A |
207 | } |
208 | ||
5958d7c0 | 209 | xmlData = CFPropertyListCreateXMLData(NULL, sessionData); |
a5f60add | 210 | if (!xmlData) { |
dbf6a266 | 211 | SCLog(TRUE, LOG_ERR, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed")); |
a5f60add A |
212 | close(fd); |
213 | return kSCStatusFailed; | |
214 | } | |
5958d7c0 A |
215 | (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData)); |
216 | (void) close(fd); | |
217 | CFRelease(xmlData); | |
218 | ||
0fae82ee | 219 | return kSCStatusOK; |
5958d7c0 A |
220 | } |
221 | ||
222 | ||
009ee3c6 | 223 | __private_extern__ |
5958d7c0 | 224 | kern_return_t |
0fae82ee | 225 | _snapshot(mach_port_t server, int *sc_status) |
5958d7c0 A |
226 | { |
227 | serverSessionRef mySession = getSession(server); | |
228 | ||
edebe297 | 229 | if (mySession == NULL) { |
009ee3c6 A |
230 | *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */ |
231 | return KERN_SUCCESS; | |
232 | } | |
233 | ||
0fae82ee | 234 | *sc_status = __SCDynamicStoreSnapshot(mySession->store); |
5958d7c0 A |
235 | return KERN_SUCCESS; |
236 | } |