]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/_snapshot.c
configd-130.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, 0)) {
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 /* check credentials */
134
135 mySession = getSession(storePrivate->server);
136 if (mySession->callerEUID != 0) {
137 return kSCStatusAccessError;
138 }
139
140 /* Save a snapshot of the "store" data */
141
142 (void) unlink(SNAPSHOT_PATH_STORE);
143 fd = open(SNAPSHOT_PATH_STORE, O_WRONLY|O_CREAT|O_TRUNC, 0644);
144 if (fd < 0) {
145 return kSCStatusFailed;
146 }
147
148 expandedStoreData = _expandStore(storeData);
149 xmlData = CFPropertyListCreateXMLData(NULL, expandedStoreData);
150 CFRelease(expandedStoreData);
151 if (!xmlData) {
152 SCLog(TRUE, LOG_ERR, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed"));
153 close(fd);
154 return kSCStatusFailed;
155 }
156 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
157 (void) close(fd);
158 CFRelease(xmlData);
159
160 /* Save a snapshot of the "pattern" data */
161
162 (void) unlink(SNAPSHOT_PATH_PATTERN);
163 fd = open(SNAPSHOT_PATH_PATTERN, O_WRONLY|O_CREAT|O_TRUNC, 0644);
164 if (fd < 0) {
165 return kSCStatusFailed;
166 }
167
168 xmlData = CFPropertyListCreateXMLData(NULL, patternData);
169 if (!xmlData) {
170 SCLog(TRUE, LOG_ERR, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed"));
171 close(fd);
172 return kSCStatusFailed;
173 }
174 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
175 (void) close(fd);
176 CFRelease(xmlData);
177
178 /* Save a snapshot of the "session" data */
179
180 (void) unlink(SNAPSHOT_PATH_SESSION);
181 fd = open(SNAPSHOT_PATH_SESSION, O_WRONLY|O_CREAT|O_TRUNC, 0644);
182 if (fd < 0) {
183 return kSCStatusFailed;
184 }
185
186 xmlData = CFPropertyListCreateXMLData(NULL, sessionData);
187 if (!xmlData) {
188 SCLog(TRUE, LOG_ERR, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed"));
189 close(fd);
190 return kSCStatusFailed;
191 }
192 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
193 (void) close(fd);
194 CFRelease(xmlData);
195
196 return kSCStatusOK;
197 }
198
199
200 __private_extern__
201 kern_return_t
202 _snapshot(mach_port_t server, int *sc_status)
203 {
204 serverSessionRef mySession = getSession(server);
205
206 if (!mySession) {
207 *sc_status = kSCStatusNoStoreSession; /* you must have an open session to play */
208 return KERN_SUCCESS;
209 }
210
211 *sc_status = __SCDynamicStoreSnapshot(mySession->store);
212 return KERN_SUCCESS;
213 }