2 * Copyright (c) 2000-2006, 2009-2011, 2015, 2018, 2019 Apple 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"
41 #include "plugin_support.h"
44 #define SNAPSHOT_PATH_STATE _PATH_VARTMP "configd-state"
45 #define SNAPSHOT_PATH_STORE _PATH_VARTMP "configd-store.plist"
46 #define SNAPSHOT_PATH_PATTERN _PATH_VARTMP "configd-pattern.plist"
51 static CF_RETURNS_RETAINED CFDictionaryRef
52 _expandStore(CFDictionaryRef storeData
)
54 const void * keys_q
[N_QUICK
];
55 const void ** keys
= keys_q
;
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
;
63 nElements
= CFDictionaryGetCount(storeData
);
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);
72 memset(nValues
, 0, nElements
* sizeof(CFTypeRef
));
74 CFDictionaryGetKeysAndValues(storeData
, keys
, oValues
);
75 for (i
= 0; i
< nElements
; i
++) {
78 data
= CFDictionaryGetValue(oValues
[i
], kSCDData
);
80 CFPropertyListRef plist
;
82 nValues
[i
] = CFDictionaryCreateMutableCopy(NULL
, 0, oValues
[i
]);
84 if (!_SCUnserialize(&plist
, data
, NULL
, 0)) {
85 SC_log(LOG_NOTICE
, "_SCUnserialize() failed, key=%@", keys
[i
]);
89 CFDictionarySetValue((CFMutableDictionaryRef
)nValues
[i
],
94 nValues
[i
] = CFRetain(oValues
[i
]);
99 newStoreData
= CFDictionaryCreate(NULL
,
103 &kCFTypeDictionaryKeyCallBacks
,
104 &kCFTypeDictionaryValueCallBacks
);
109 for (i
= 0; i
< nElements
; i
++) {
110 CFRelease(nValues
[i
]);
113 if (keys
!= keys_q
) {
114 CFAllocatorDeallocate(NULL
, keys
);
115 CFAllocatorDeallocate(NULL
, oValues
);
116 CFAllocatorDeallocate(NULL
, nValues
);
126 __SCDynamicStoreSnapshot(SCDynamicStoreRef store
)
128 #pragma unused(store)
129 CFDictionaryRef expandedStoreData
;
134 /* Save a snapshot of configd's "state" */
136 (void) unlink(SNAPSHOT_PATH_STATE
);
137 fd
= open(SNAPSHOT_PATH_STATE
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, 0644);
139 return kSCStatusFailed
;
143 return kSCStatusFailed
;
145 SCPrint(TRUE
, f
, CFSTR("Main [plug-in] thread :\n\n"));
146 SCPrint(TRUE
, f
, CFSTR("%@\n"), CFRunLoopGetCurrent());
150 /* Save a snapshot of the "store" data */
152 (void) unlink(SNAPSHOT_PATH_STORE
);
153 fd
= open(SNAPSHOT_PATH_STORE
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, 0644);
155 return kSCStatusFailed
;
158 expandedStoreData
= _expandStore(storeData
);
159 xmlData
= CFPropertyListCreateData(NULL
, expandedStoreData
, kCFPropertyListXMLFormat_v1_0
, 0, NULL
);
160 CFRelease(expandedStoreData
);
161 if (xmlData
== NULL
) {
162 SC_log(LOG_NOTICE
, "CFPropertyListCreateData() failed");
164 return kSCStatusFailed
;
166 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
170 /* Save a snapshot of the "pattern" data */
172 (void) unlink(SNAPSHOT_PATH_PATTERN
);
173 fd
= open(SNAPSHOT_PATH_PATTERN
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, 0644);
175 return kSCStatusFailed
;
178 xmlData
= CFPropertyListCreateData(NULL
, patternData
, kCFPropertyListXMLFormat_v1_0
, 0, NULL
);
179 if (xmlData
== NULL
) {
180 SC_log(LOG_NOTICE
, "CFPropertyListCreateData() failed");
182 return kSCStatusFailed
;
184 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
194 _snapshot(mach_port_t server
, int *sc_status
, audit_token_t audit_token
)
196 serverSessionRef mySession
;
198 mySession
= getSession(server
);
199 if (mySession
== NULL
) {
200 mySession
= tempSession(server
, CFSTR("SCDynamicStoreSnapshot"), audit_token
);
201 if (mySession
== NULL
) {
202 /* you must have an open session to play */
203 return kSCStatusNoStoreSession
;
207 if (!hasRootAccess(mySession
)) {
208 return kSCStatusAccessError
;
211 *sc_status
= __SCDynamicStoreSnapshot(mySession
->store
);