2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * April 14, 2000 Allan Nathanson <ajn@apple.com>
39 #include "configd_server.h"
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"
50 static CFDictionaryRef
51 _expandStore(CFDictionaryRef storeData
)
53 const void * keys_q
[N_QUICK
];
54 const void ** keys
= keys_q
;
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
;
62 nElements
= CFDictionaryGetCount(storeData
);
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);
71 bzero(nValues
, nElements
* sizeof(CFTypeRef
));
73 CFDictionaryGetKeysAndValues(storeData
, keys
, oValues
);
74 for (i
= 0; i
< nElements
; i
++) {
77 data
= CFDictionaryGetValue(oValues
[i
], kSCDData
);
79 CFPropertyListRef plist
;
81 if (!_SCUnserialize(&plist
, data
, NULL
, NULL
)) {
85 nValues
[i
] = CFDictionaryCreateMutableCopy(NULL
, 0, oValues
[i
]);
86 CFDictionarySetValue((CFMutableDictionaryRef
)nValues
[i
],
91 nValues
[i
] = CFRetain(oValues
[i
]);
96 newStoreData
= CFDictionaryCreate(NULL
,
100 &kCFTypeDictionaryKeyCallBacks
,
101 &kCFTypeDictionaryValueCallBacks
);
108 for (i
= 0; i
< nElements
; i
++) {
109 if (nValues
[i
]) CFRelease(nValues
[i
]);
112 if (keys
!= keys_q
) {
113 CFAllocatorDeallocate(NULL
, keys
);
114 CFAllocatorDeallocate(NULL
, oValues
);
115 CFAllocatorDeallocate(NULL
, nValues
);
125 __SCDynamicStoreSnapshot(SCDynamicStoreRef store
)
127 CFDictionaryRef expandedStoreData
;
129 serverSessionRef mySession
;
130 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
133 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("__SCDynamicStoreSnapshot:"));
135 /* check credentials */
137 mySession
= getSession(storePrivate
->server
);
138 if (mySession
->callerEUID
!= 0) {
139 return kSCStatusAccessError
;
142 /* Save a snapshot of the "store" data */
144 (void) unlink(SNAPSHOT_PATH_STORE
);
145 fd
= open(SNAPSHOT_PATH_STORE
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
147 return kSCStatusFailed
;
150 expandedStoreData
= _expandStore(storeData
);
151 xmlData
= CFPropertyListCreateXMLData(NULL
, expandedStoreData
);
152 CFRelease(expandedStoreData
);
154 SCLog(TRUE
, LOG_ERR
, CFSTR("CFPropertyListCreateXMLData() failed"));
156 return kSCStatusFailed
;
158 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
162 /* Save a snapshot of the "pattern" data */
164 (void) unlink(SNAPSHOT_PATH_PATTERN
);
165 fd
= open(SNAPSHOT_PATH_PATTERN
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
167 return kSCStatusFailed
;
170 xmlData
= CFPropertyListCreateXMLData(NULL
, patternData
);
172 SCLog(TRUE
, LOG_ERR
, CFSTR("CFPropertyListCreateXMLData() failed"));
174 return kSCStatusFailed
;
176 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
180 /* Save a snapshot of the "session" data */
182 (void) unlink(SNAPSHOT_PATH_SESSION
);
183 fd
= open(SNAPSHOT_PATH_SESSION
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
185 return kSCStatusFailed
;
188 xmlData
= CFPropertyListCreateXMLData(NULL
, sessionData
);
190 SCLog(TRUE
, LOG_ERR
, CFSTR("CFPropertyListCreateXMLData() failed"));
192 return kSCStatusFailed
;
194 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
204 _snapshot(mach_port_t server
, int *sc_status
)
206 serverSessionRef mySession
= getSession(server
);
208 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("Snapshot configuration database."));
211 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
215 *sc_status
= __SCDynamicStoreSnapshot(mySession
->store
);
216 if (*sc_status
!= kSCStatusOK
) {
217 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" __SCDynamicStoreSnapshot(): %s"), SCErrorString(*sc_status
));