2 * Copyright (c) 2004-2007 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 * May 27, 2004 Allan Nathanson <ajn@apple.com>
32 #include <CoreFoundation/CoreFoundation.h>
33 #include <SystemConfiguration/SystemConfiguration.h>
34 #include <SystemConfiguration/SCValidation.h>
35 #include <SystemConfiguration/SCPrivate.h>
37 #include <sys/ioctl.h>
41 __private_extern__ CFDictionaryRef
42 __getPrefsConfiguration(SCPreferencesRef prefs
, CFStringRef path
)
44 CFDictionaryRef config
;
47 config
= SCPreferencesPathGetValue(prefs
, path
);
49 n
= isA_CFDictionary(config
) ? CFDictionaryGetCount(config
) : 0;
52 // ignore empty configuration entities
56 if (CFDictionaryContainsKey(config
, kSCResvInactive
)) {
57 // ignore [effectively] empty configuration entities
69 __private_extern__ Boolean
70 __setPrefsConfiguration(SCPreferencesRef prefs
,
72 CFDictionaryRef config
,
75 CFMutableDictionaryRef newConfig
= NULL
;
78 if ((config
!= NULL
) && !isA_CFDictionary(config
)) {
79 _SCErrorSet(kSCStatusInvalidArgument
);
84 newConfig
= CFDictionaryCreateMutableCopy(NULL
, 0, config
);
88 CFDictionaryRef curConfig
;
91 newConfig
= CFDictionaryCreateMutable(NULL
,
93 &kCFTypeDictionaryKeyCallBacks
,
94 &kCFTypeDictionaryValueCallBacks
);
97 curConfig
= SCPreferencesPathGetValue(prefs
, path
);
98 if (isA_CFDictionary(curConfig
) && CFDictionaryContainsKey(curConfig
, kSCResvInactive
)) {
99 // if currently disabled
100 CFDictionarySetValue(newConfig
, kSCResvInactive
, kCFBooleanTrue
);
102 // if currently enabled
103 CFDictionaryRemoveValue(newConfig
, kSCResvInactive
);
107 // set new configuration
108 if (newConfig
!= NULL
) {
109 // if new configuration (or we are preserving a disabled state)
110 ok
= SCPreferencesPathSetValue(prefs
, path
, newConfig
);
111 CFRelease(newConfig
);
113 ok
= SCPreferencesPathRemoveValue(prefs
, path
);
114 if (!ok
&& (SCError() == kSCStatusNoKey
)) {
123 __private_extern__ Boolean
124 __getPrefsEnabled(SCPreferencesRef prefs
, CFStringRef path
)
126 CFDictionaryRef config
;
128 config
= SCPreferencesPathGetValue(prefs
, path
);
129 if (isA_CFDictionary(config
) && CFDictionaryContainsKey(config
, kSCResvInactive
)) {
137 __private_extern__ Boolean
138 __setPrefsEnabled(SCPreferencesRef prefs
,
142 CFDictionaryRef curConfig
;
143 CFMutableDictionaryRef newConfig
= NULL
;
146 // preserve current configuration
147 curConfig
= SCPreferencesPathGetValue(prefs
, path
);
148 if (curConfig
!= NULL
) {
149 if (!isA_CFDictionary(curConfig
)) {
150 _SCErrorSet(kSCStatusFailed
);
153 newConfig
= CFDictionaryCreateMutableCopy(NULL
, 0, curConfig
);
157 CFDictionaryRemoveValue(newConfig
, kSCResvInactive
);
160 CFDictionarySetValue(newConfig
, kSCResvInactive
, kCFBooleanTrue
);
165 newConfig
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
166 CFDictionarySetValue(newConfig
, kSCResvInactive
, kCFBooleanTrue
);
170 // set new configuration
171 if (newConfig
!= NULL
) {
172 // if updated configuration (or we are establishing as disabled)
173 ok
= SCPreferencesPathSetValue(prefs
, path
, newConfig
);
174 CFRelease(newConfig
);
176 ok
= SCPreferencesPathRemoveValue(prefs
, path
);
177 if (!ok
&& (SCError() == kSCStatusNoKey
)) {
186 static CFDictionaryRef
191 CFDictionaryRef templates
;
193 CFStringRef xmlError
= NULL
;
194 CFDataRef xmlTemplates
= NULL
;
196 bundle
= _SC_CFBundleGet();
197 if (bundle
== NULL
) {
201 url
= CFBundleCopyResourceURL(bundle
, CFSTR("NetworkConfiguration"), CFSTR("plist"), NULL
);
206 ok
= CFURLCreateDataAndPropertiesFromResource(NULL
, url
, &xmlTemplates
, NULL
, NULL
, NULL
);
208 if (!ok
|| (xmlTemplates
== NULL
)) {
212 // convert the XML data into a property list
213 templates
= CFPropertyListCreateFromXMLData(NULL
, xmlTemplates
, kCFPropertyListImmutable
, &xmlError
);
214 CFRelease(xmlTemplates
);
215 if (templates
== NULL
) {
216 if (xmlError
!= NULL
) {
217 SCLog(TRUE
, LOG_DEBUG
, CFSTR("could not load SCNetworkConfiguration templates: %@"), xmlError
);
223 if (!isA_CFDictionary(templates
)) {
224 CFRelease(templates
);
232 __private_extern__ CFDictionaryRef
233 __copyInterfaceTemplate(CFStringRef interfaceType
,
234 CFStringRef childInterfaceType
)
236 CFDictionaryRef interface
= NULL
;
237 CFDictionaryRef interfaces
;
238 CFDictionaryRef templates
;
240 templates
= __copyTemplates();
241 if (templates
== NULL
) {
245 interfaces
= CFDictionaryGetValue(templates
, CFSTR("Interface"));
246 if (!isA_CFDictionary(interfaces
)) {
247 CFRelease(templates
);
251 if (childInterfaceType
== NULL
) {
252 interface
= CFDictionaryGetValue(interfaces
, interfaceType
);
254 CFStringRef expandedType
;
256 expandedType
= CFStringCreateWithFormat(NULL
,
261 interface
= CFDictionaryGetValue(interfaces
, expandedType
);
262 CFRelease(expandedType
);
265 if (isA_CFDictionary(interface
) && (CFDictionaryGetCount(interface
) > 0)) {
271 CFRelease(templates
);
277 __private_extern__ CFDictionaryRef
278 __copyProtocolTemplate(CFStringRef interfaceType
,
279 CFStringRef childInterfaceType
,
280 CFStringRef protocolType
)
282 CFDictionaryRef interface
= NULL
;
283 CFDictionaryRef protocol
= NULL
;
284 CFDictionaryRef protocols
;
285 CFDictionaryRef templates
;
287 templates
= __copyTemplates();
288 if (templates
== NULL
) {
292 protocols
= CFDictionaryGetValue(templates
, CFSTR("Protocol"));
293 if (!isA_CFDictionary(protocols
)) {
294 CFRelease(templates
);
298 if (childInterfaceType
== NULL
) {
299 interface
= CFDictionaryGetValue(protocols
, interfaceType
);
301 CFStringRef expandedType
;
303 expandedType
= CFStringCreateWithFormat(NULL
,
308 interface
= CFDictionaryGetValue(protocols
, expandedType
);
309 CFRelease(expandedType
);
312 if (isA_CFDictionary(interface
)) {
313 protocol
= CFDictionaryGetValue(interface
, protocolType
);
314 if (isA_CFDictionary(protocol
)) {
321 CFRelease(templates
);
327 __private_extern__ Boolean
328 __createInterface(int s
, CFStringRef interface
)
332 bzero(&ifr
, sizeof(ifr
));
333 (void) _SC_cfstring_to_cstring(interface
,
335 sizeof(ifr
.ifr_name
),
336 kCFStringEncodingASCII
);
338 if (ioctl(s
, SIOCIFCREATE
, &ifr
) == -1) {
341 CFSTR("could not create interface \"%@\": %s"),
351 __private_extern__ Boolean
352 __destroyInterface(int s
, CFStringRef interface
)
356 bzero(&ifr
, sizeof(ifr
));
357 (void) _SC_cfstring_to_cstring(interface
,
359 sizeof(ifr
.ifr_name
),
360 kCFStringEncodingASCII
);
362 if (ioctl(s
, SIOCIFDESTROY
, &ifr
) == -1) {
365 CFSTR("could not destroy interface \"%@\": %s"),
376 * For rdar://problem/4685223
378 * To keep MoreSCF happy we need to ensure that the first "Set" and
379 * "NetworkService" have a [less than] unique identifier that can
380 * be parsed as a numeric string.
382 * Note: this backwards compatibility code must be enabled using the
385 * sudo defaults write \
386 * /Library/Preferences/SystemConfiguration/preferences \
392 __SCPreferencesPathCreateUniqueChild_WithMoreSCFCompatibility(SCPreferencesRef prefs
, CFStringRef prefix
)
394 static int hack
= -1;
395 CFStringRef path
= NULL
;
400 enable
= SCPreferencesGetValue(prefs
, CFSTR("MoreSCF"));
401 hack
= (isA_CFBoolean(enable
) && CFBooleanGetValue(enable
)) ? 1 : 0;
405 CFDictionaryRef dict
;
408 path
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@/%@"), prefix
, CFSTR("0"));
409 dict
= SCPreferencesPathGetValue(prefs
, path
);
411 // if path "0" exists
416 // unique child with path "0" does not exist, create
417 dict
= CFDictionaryCreate(NULL
,
419 &kCFTypeDictionaryKeyCallBacks
,
420 &kCFTypeDictionaryValueCallBacks
);
421 ok
= SCPreferencesPathSetValue(prefs
, path
, dict
);
435 __copy_legacy_password(CFTypeRef password
)
437 if (password
== NULL
) {
441 if (isA_CFData(password
)) {
444 n
= CFDataGetLength(password
);
445 if ((n
% sizeof(UniChar
)) == 0) {
446 CFStringEncoding encoding
;
450 encoding
= (*(CFDataGetBytePtr(password
) + 1) == 0x00) ? kCFStringEncodingUTF16LE
: kCFStringEncodingUTF16BE
;
451 #else // __LITTLE_ENDIAN__
452 encoding
= (*(CFDataGetBytePtr(password
) ) == 0x00) ? kCFStringEncodingUTF16BE
: kCFStringEncodingUTF16LE
;
454 str
= CFStringCreateWithBytes(NULL
,
455 (const UInt8
*)CFDataGetBytePtr(password
),
459 password
= CFStringCreateExternalRepresentation(NULL
,
461 kCFStringEncodingUTF8
,
467 } else if (isA_CFString(password
) && (CFStringGetLength(password
) > 0)) {
468 // convert password to CFData
469 password
= CFStringCreateExternalRepresentation(NULL
,
471 kCFStringEncodingUTF8
,
483 __extract_password(SCPreferencesRef prefs
,
484 CFDictionaryRef config
,
485 CFStringRef passwordKey
,
486 CFStringRef encryptionKey
,
487 CFStringRef encryptionKeyChainValue
,
488 CFStringRef unique_id
,
491 CFStringRef encryption
= NULL
;
492 Boolean exists
= FALSE
;
494 // check for keychain password
495 if (config
!= NULL
) {
496 encryption
= CFDictionaryGetValue(config
, encryptionKey
);
498 if ((encryption
== NULL
) ||
499 (isA_CFString(encryption
) &&
500 CFEqual(encryption
, encryptionKeyChainValue
))) {
502 if (password
!= NULL
) {
504 *password
= _SCPreferencesSystemKeychainPasswordItemCopy(prefs
, unique_id
);
506 *password
= _SCSecKeychainPasswordItemCopy(NULL
, unique_id
);
508 exists
= (*password
!= NULL
);
511 exists
= _SCPreferencesSystemKeychainPasswordItemExists(prefs
, unique_id
);
513 exists
= _SCSecKeychainPasswordItemExists(NULL
, unique_id
);
518 // as needed, check for in-line password
519 if (!exists
&& (encryption
== NULL
) && (config
!= NULL
)) {
520 CFDataRef inline_password
;
522 inline_password
= CFDictionaryGetValue(config
, passwordKey
);
523 inline_password
= __copy_legacy_password(inline_password
);
524 if (inline_password
!= NULL
) {
527 if (password
!= NULL
) {
528 *password
= inline_password
;
530 CFRelease(inline_password
);