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
, 0)) {
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 /* check credentials */
135 mySession
= getSession(storePrivate
->server
);
136 if (mySession
->callerEUID
!= 0) {
137 return kSCStatusAccessError
;
140 /* Save a snapshot of the "store" data */
142 (void) unlink(SNAPSHOT_PATH_STORE
);
143 fd
= open(SNAPSHOT_PATH_STORE
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
145 return kSCStatusFailed
;
148 expandedStoreData
= _expandStore(storeData
);
149 xmlData
= CFPropertyListCreateXMLData(NULL
, expandedStoreData
);
150 CFRelease(expandedStoreData
);
152 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed"));
154 return kSCStatusFailed
;
156 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
160 /* Save a snapshot of the "pattern" data */
162 (void) unlink(SNAPSHOT_PATH_PATTERN
);
163 fd
= open(SNAPSHOT_PATH_PATTERN
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
165 return kSCStatusFailed
;
168 xmlData
= CFPropertyListCreateXMLData(NULL
, patternData
);
170 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed"));
172 return kSCStatusFailed
;
174 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
178 /* Save a snapshot of the "session" data */
180 (void) unlink(SNAPSHOT_PATH_SESSION
);
181 fd
= open(SNAPSHOT_PATH_SESSION
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644);
183 return kSCStatusFailed
;
186 xmlData
= CFPropertyListCreateXMLData(NULL
, sessionData
);
188 SCLog(TRUE
, LOG_ERR
, CFSTR("__SCDynamicStoreSnapshot CFPropertyListCreateXMLData() failed"));
190 return kSCStatusFailed
;
192 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
202 _snapshot(mach_port_t server
, int *sc_status
)
204 serverSessionRef mySession
= getSession(server
);
207 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
211 *sc_status
= __SCDynamicStoreSnapshot(mySession
->store
);