2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Modification History
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
32 * April 14, 2000 Allan Nathanson <ajn@apple.com>
41 #include "configd_server.h"
45 #define SNAPSHOT_PATH_STORE _PATH_VARTMP "configd-store.xml"
46 #define SNAPSHOT_PATH_PATTERN _PATH_VARTMP "configd-pattern.xml"
47 #define SNAPSHOT_PATH_SESSION _PATH_VARTMP "configd-session.xml"
52 static CFDictionaryRef
53 _expandStore(CFDictionaryRef storeData
)
55 const void * keys_q
[N_QUICK
];
56 const void ** keys
= keys_q
;
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
;
64 nElements
= CFDictionaryGetCount(storeData
);
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);
73 bzero(nValues
, nElements
* sizeof(CFTypeRef
));
75 CFDictionaryGetKeysAndValues(storeData
, keys
, oValues
);
76 for (i
= 0; i
< nElements
; i
++) {
79 data
= CFDictionaryGetValue(oValues
[i
], kSCDData
);
81 CFPropertyListRef plist
;
83 if (!_SCUnserialize(&plist
, data
, NULL
, NULL
)) {
87 nValues
[i
] = CFDictionaryCreateMutableCopy(NULL
, 0, oValues
[i
]);
88 CFDictionarySetValue((CFMutableDictionaryRef
)nValues
[i
],
93 nValues
[i
] = CFRetain(oValues
[i
]);
98 newStoreData
= CFDictionaryCreate(NULL
,
102 &kCFTypeDictionaryKeyCallBacks
,
103 &kCFTypeDictionaryValueCallBacks
);
110 for (i
= 0; i
< nElements
; i
++) {
111 if (nValues
[i
]) CFRelease(nValues
[i
]);
114 if (keys
!= keys_q
) {
115 CFAllocatorDeallocate(NULL
, keys
);
116 CFAllocatorDeallocate(NULL
, oValues
);
117 CFAllocatorDeallocate(NULL
, nValues
);
127 __SCDynamicStoreSnapshot(SCDynamicStoreRef store
)
129 CFDictionaryRef expandedStoreData
;
131 serverSessionRef mySession
;
132 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
135 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("__SCDynamicStoreSnapshot:"));
137 /* check credentials */
139 mySession
= getSession(storePrivate
->server
);
140 if (mySession
->callerEUID
!= 0) {
141 return kSCStatusAccessError
;
144 /* Save a snapshot of the "store" data */
146 (void) unlink(SNAPSHOT_PATH_STORE
);
147 fd
= open(SNAPSHOT_PATH_STORE
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
149 return kSCStatusFailed
;
152 expandedStoreData
= _expandStore(storeData
);
153 xmlData
= CFPropertyListCreateXMLData(NULL
, expandedStoreData
);
154 CFRelease(expandedStoreData
);
156 SCLog(TRUE
, LOG_ERR
, CFSTR("CFPropertyListCreateXMLData() failed"));
158 return kSCStatusFailed
;
160 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
164 /* Save a snapshot of the "pattern" data */
166 (void) unlink(SNAPSHOT_PATH_PATTERN
);
167 fd
= open(SNAPSHOT_PATH_PATTERN
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
169 return kSCStatusFailed
;
172 xmlData
= CFPropertyListCreateXMLData(NULL
, patternData
);
174 SCLog(TRUE
, LOG_ERR
, CFSTR("CFPropertyListCreateXMLData() failed"));
176 return kSCStatusFailed
;
178 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
182 /* Save a snapshot of the "session" data */
184 (void) unlink(SNAPSHOT_PATH_SESSION
);
185 fd
= open(SNAPSHOT_PATH_SESSION
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
187 return kSCStatusFailed
;
190 xmlData
= CFPropertyListCreateXMLData(NULL
, sessionData
);
192 SCLog(TRUE
, LOG_ERR
, CFSTR("CFPropertyListCreateXMLData() failed"));
194 return kSCStatusFailed
;
196 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
206 _snapshot(mach_port_t server
, int *sc_status
)
208 serverSessionRef mySession
= getSession(server
);
210 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("Snapshot configuration database."));
213 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
217 *sc_status
= __SCDynamicStoreSnapshot(mySession
->store
);
218 if (*sc_status
!= kSCStatusOK
) {
219 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" __SCDynamicStoreSnapshot(): %s"), SCErrorString(*sc_status
));