]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_snapshot.c
configd-84.6.tar.gz
[apple/configd.git] / configd.tproj / _snapshot.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, 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
42
43 #define SNAPSHOT_PATH_STORE _PATH_VARTMP "configd-store.xml"
44 #define SNAPSHOT_PATH_PATTERN _PATH_VARTMP "configd-pattern.xml"
45 #define SNAPSHOT_PATH_SESSION _PATH_VARTMP "configd-session.xml"
46
47
48 #define N_QUICK 100
49
50 static CFDictionaryRef
51 _expandStore(CFDictionaryRef storeData)
52 {
53 const void * keys_q[N_QUICK];
54 const void ** keys = keys_q;
55 CFIndex nElements;
56 CFDictionaryRef newStoreData = NULL;
57 const void * nValues_q[N_QUICK];
58 const void ** nValues = nValues_q;
59 const void * oValues_q[N_QUICK];
60 const void ** oValues = oValues_q;
61
62 nElements = CFDictionaryGetCount(storeData);
63 if (nElements > 0) {
64 CFIndex i;
65
66 if (nElements > (CFIndex)(sizeof(keys_q) / sizeof(CFTypeRef))) {
67 keys = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
68 oValues = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
69 nValues = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
70 }
71 bzero(nValues, nElements * sizeof(CFTypeRef));
72
73 CFDictionaryGetKeysAndValues(storeData, keys, oValues);
74 for (i = 0; i < nElements; i++) {
75 CFDataRef data;
76
77 data = CFDictionaryGetValue(oValues[i], kSCDData);
78 if (data) {
79 CFPropertyListRef plist;
80
81 if (!_SCUnserialize(&plist, data, NULL, NULL)) {
82 goto done;
83 }
84
85 nValues[i] = CFDictionaryCreateMutableCopy(NULL, 0, oValues[i]);
86 CFDictionarySetValue((CFMutableDictionaryRef)nValues[i],
87 kSCDData,
88 plist);
89 CFRelease(plist);
90 } else {
91 nValues[i] = CFRetain(oValues[i]);
92 }
93 }
94 }
95
96 newStoreData = CFDictionaryCreate(NULL,
97 keys,
98 nValues,
99 nElements,
100 &kCFTypeDictionaryKeyCallBacks,
101 &kCFTypeDictionaryValueCallBacks);
102
103 done :
104
105 if (nElements > 0) {
106 CFIndex i;
107
108 for (i = 0; i < nElements; i++) {
109 if (nValues[i]) CFRelease(nValues[i]);
110 }
111
112 if (keys != keys_q) {
113 CFAllocatorDeallocate(NULL, keys);
114 CFAllocatorDeallocate(NULL, oValues);
115 CFAllocatorDeallocate(NULL, nValues);
116 }
117 }
118
119 return newStoreData;
120 }
121
122
123 __private_extern__
124 int
125 __SCDynamicStoreSnapshot(SCDynamicStoreRef store)
126 {
127 CFDictionaryRef expandedStoreData;
128 int fd;
129 serverSessionRef mySession;
130 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
131 CFDataRef xmlData;
132
133 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("__SCDynamicStoreSnapshot:"));
134
135 /* check credentials */
136
137 mySession = getSession(storePrivate->server);
138 if (mySession->callerEUID != 0) {
139 return kSCStatusAccessError;
140 }
141
142 /* Save a snapshot of the "store" data */
143
144 (void) unlink(SNAPSHOT_PATH_STORE);
145 fd = open(SNAPSHOT_PATH_STORE, O_WRONLY|O_CREAT|O_TRUNC, 0644);
146 if (fd < 0) {
147 return kSCStatusFailed;
148 }
149
150 expandedStoreData = _expandStore(storeData);
151 xmlData = CFPropertyListCreateXMLData(NULL, expandedStoreData);
152 CFRelease(expandedStoreData);
153 if (!xmlData) {
154 SCLog(TRUE, LOG_ERR, CFSTR("CFPropertyListCreateXMLData() failed"));
155 close(fd);
156 return kSCStatusFailed;
157 }
158 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
159 (void) close(fd);
160 CFRelease(xmlData);
161
162 /* Save a snapshot of the "pattern" data */
163
164 (void) unlink(SNAPSHOT_PATH_PATTERN);
165 fd = open(SNAPSHOT_PATH_PATTERN, O_WRONLY|O_CREAT|O_TRUNC, 0644);
166 if (fd < 0) {
167 return kSCStatusFailed;
168 }
169
170 xmlData = CFPropertyListCreateXMLData(NULL, patternData);
171 if (!xmlData) {
172 SCLog(TRUE, LOG_ERR, CFSTR("CFPropertyListCreateXMLData() failed"));
173 close(fd);
174 return kSCStatusFailed;
175 }
176 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
177 (void) close(fd);
178 CFRelease(xmlData);
179
180 /* Save a snapshot of the "session" data */
181
182 (void) unlink(SNAPSHOT_PATH_SESSION);
183 fd = open(SNAPSHOT_PATH_SESSION, O_WRONLY|O_CREAT|O_TRUNC, 0644);
184 if (fd < 0) {
185 return kSCStatusFailed;
186 }
187
188 xmlData = CFPropertyListCreateXMLData(NULL, sessionData);
189 if (!xmlData) {
190 SCLog(TRUE, LOG_ERR, CFSTR("CFPropertyListCreateXMLData() failed"));
191 close(fd);
192 return kSCStatusFailed;
193 }
194 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
195 (void) close(fd);
196 CFRelease(xmlData);
197
198 return kSCStatusOK;
199 }
200
201
202 __private_extern__
203 kern_return_t
204 _snapshot(mach_port_t server, int *sc_status)
205 {
206 serverSessionRef mySession = getSession(server);
207
208 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("Snapshot configuration database."));
209
210 if (!mySession) {
211 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
212 return KERN_SUCCESS;
213 }
214
215 *sc_status = __SCDynamicStoreSnapshot(mySession->store);
216 if (*sc_status != kSCStatusOK) {
217 SCLog(_configd_verbose, LOG_DEBUG, CFSTR(" __SCDynamicStoreSnapshot(): %s"), SCErrorString(*sc_status));
218 }
219
220 return KERN_SUCCESS;
221 }