]> git.saurik.com Git - apple/configd.git/blame - configd.tproj/_snapshot.c
configd-1109.101.1.tar.gz
[apple/configd.git] / configd.tproj / _snapshot.c
CommitLineData
5958d7c0 1/*
afb19109 2 * Copyright (c) 2000-2006, 2009-2011, 2015, 2018, 2019 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 * 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"
6bb65964
A
45#define SNAPSHOT_PATH_STORE _PATH_VARTMP "configd-store.plist"
46#define SNAPSHOT_PATH_PATTERN _PATH_VARTMP "configd-pattern.plist"
5958d7c0
A
47
48
009ee3c6
A
49#define N_QUICK 100
50
17d3ee29 51static CF_RETURNS_RETAINED CFDictionaryRef
009ee3c6
A
52_expandStore(CFDictionaryRef storeData)
53{
54 const void * keys_q[N_QUICK];
55 const void ** keys = keys_q;
56 CFIndex nElements;
57 CFDictionaryRef newStoreData = NULL;
58 const void * nValues_q[N_QUICK];
59 const void ** nValues = nValues_q;
60 const void * oValues_q[N_QUICK];
61 const void ** oValues = oValues_q;
62
63 nElements = CFDictionaryGetCount(storeData);
64 if (nElements > 0) {
65 CFIndex i;
66
67 if (nElements > (CFIndex)(sizeof(keys_q) / sizeof(CFTypeRef))) {
68 keys = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
69 oValues = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
70 nValues = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
71 }
afb19109 72 memset(nValues, 0, nElements * sizeof(CFTypeRef));
009ee3c6
A
73
74 CFDictionaryGetKeysAndValues(storeData, keys, oValues);
75 for (i = 0; i < nElements; i++) {
76 CFDataRef data;
77
78 data = CFDictionaryGetValue(oValues[i], kSCDData);
79 if (data) {
80 CFPropertyListRef plist;
81
009ee3c6 82 nValues[i] = CFDictionaryCreateMutableCopy(NULL, 0, oValues[i]);
6bb65964 83
942cecd7
A
84 if (!_SCUnserialize(&plist, data, NULL, 0)) {
85 SC_log(LOG_NOTICE, "_SCUnserialize() failed, key=%@", keys[i]);
86 continue;
87 }
88
009ee3c6
A
89 CFDictionarySetValue((CFMutableDictionaryRef)nValues[i],
90 kSCDData,
91 plist);
92 CFRelease(plist);
93 } else {
94 nValues[i] = CFRetain(oValues[i]);
95 }
96 }
97 }
98
99 newStoreData = CFDictionaryCreate(NULL,
100 keys,
101 nValues,
102 nElements,
103 &kCFTypeDictionaryKeyCallBacks,
104 &kCFTypeDictionaryValueCallBacks);
105
009ee3c6
A
106 if (nElements > 0) {
107 CFIndex i;
108
109 for (i = 0; i < nElements; i++) {
6bb65964 110 CFRelease(nValues[i]);
009ee3c6
A
111 }
112
113 if (keys != keys_q) {
114 CFAllocatorDeallocate(NULL, keys);
115 CFAllocatorDeallocate(NULL, oValues);
116 CFAllocatorDeallocate(NULL, nValues);
117 }
118 }
119
120 return newStoreData;
121}
122
123
124__private_extern__
0fae82ee
A
125int
126__SCDynamicStoreSnapshot(SCDynamicStoreRef store)
5958d7c0 127{
1ef45fa4 128#pragma unused(store)
009ee3c6 129 CFDictionaryRef expandedStoreData;
edebe297 130 FILE *f;
0fae82ee 131 int fd;
0fae82ee
A
132 CFDataRef xmlData;
133
edebe297
A
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 }
c956c85e 145 SCPrint(TRUE, f, CFSTR("Main [plug-in] thread :\n\n"));
edebe297 146 SCPrint(TRUE, f, CFSTR("%@\n"), CFRunLoopGetCurrent());
a40a14f8 147 listSessions(f);
edebe297
A
148 (void) fclose(f);
149
0fae82ee 150 /* Save a snapshot of the "store" data */
5958d7c0 151
0fae82ee 152 (void) unlink(SNAPSHOT_PATH_STORE);
edebe297
A
153 fd = open(SNAPSHOT_PATH_STORE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
154 if (fd == -1) {
0fae82ee 155 return kSCStatusFailed;
5958d7c0
A
156 }
157
009ee3c6 158 expandedStoreData = _expandStore(storeData);
6bb65964 159 xmlData = CFPropertyListCreateData(NULL, expandedStoreData, kCFPropertyListXMLFormat_v1_0, 0, NULL);
009ee3c6 160 CFRelease(expandedStoreData);
6bb65964 161 if (xmlData == NULL) {
9de8ab86 162 SC_log(LOG_NOTICE, "CFPropertyListCreateData() failed");
009ee3c6
A
163 close(fd);
164 return kSCStatusFailed;
165 }
166 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
167 (void) close(fd);
168 CFRelease(xmlData);
169
170 /* Save a snapshot of the "pattern" data */
171
172 (void) unlink(SNAPSHOT_PATH_PATTERN);
edebe297
A
173 fd = open(SNAPSHOT_PATH_PATTERN, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
174 if (fd == -1) {
009ee3c6
A
175 return kSCStatusFailed;
176 }
177
6bb65964
A
178 xmlData = CFPropertyListCreateData(NULL, patternData, kCFPropertyListXMLFormat_v1_0, 0, NULL);
179 if (xmlData == NULL) {
9de8ab86 180 SC_log(LOG_NOTICE, "CFPropertyListCreateData() failed");
a5f60add
A
181 close(fd);
182 return kSCStatusFailed;
183 }
5958d7c0
A
184 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
185 (void) close(fd);
186 CFRelease(xmlData);
187
0fae82ee 188 return kSCStatusOK;
5958d7c0
A
189}
190
191
009ee3c6 192__private_extern__
5958d7c0 193kern_return_t
17d3ee29 194_snapshot(mach_port_t server, int *sc_status, audit_token_t audit_token)
5958d7c0 195{
17d3ee29 196 serverSessionRef mySession;
5958d7c0 197
17d3ee29 198 mySession = getSession(server);
edebe297 199 if (mySession == NULL) {
17d3ee29
A
200 mySession = tempSession(server, CFSTR("SCDynamicStoreSnapshot"), audit_token);
201 if (mySession == NULL) {
202 /* you must have an open session to play */
203 return kSCStatusNoStoreSession;
204 }
205 }
206
207 if (!hasRootAccess(mySession)) {
208 return kSCStatusAccessError;
009ee3c6
A
209 }
210
0fae82ee 211 *sc_status = __SCDynamicStoreSnapshot(mySession->store);
5958d7c0
A
212 return KERN_SUCCESS;
213}