2 * Copyright (c) 2004-2007, 2009, 2010, 2012, 2013 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 CFDictionaryRef curConfig
;
76 CFMutableDictionaryRef newConfig
= NULL
;
79 if ((config
!= NULL
) && !isA_CFDictionary(config
)) {
80 _SCErrorSet(kSCStatusInvalidArgument
);
84 curConfig
= SCPreferencesPathGetValue(prefs
, path
);
87 newConfig
= CFDictionaryCreateMutableCopy(NULL
, 0, config
);
92 newConfig
= CFDictionaryCreateMutable(NULL
,
94 &kCFTypeDictionaryKeyCallBacks
,
95 &kCFTypeDictionaryValueCallBacks
);
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 (_SC_CFEqual(curConfig
, newConfig
)) {
110 if (newConfig
!= NULL
) CFRelease(newConfig
);
112 } else if (newConfig
!= NULL
) {
113 // if new configuration (or we are preserving a disabled state)
114 ok
= SCPreferencesPathSetValue(prefs
, path
, newConfig
);
115 CFRelease(newConfig
);
117 ok
= SCPreferencesPathRemoveValue(prefs
, path
);
118 if (!ok
&& (SCError() == kSCStatusNoKey
)) {
127 __private_extern__ Boolean
128 __getPrefsEnabled(SCPreferencesRef prefs
, CFStringRef path
)
130 CFDictionaryRef config
;
132 config
= SCPreferencesPathGetValue(prefs
, path
);
133 if (isA_CFDictionary(config
) && CFDictionaryContainsKey(config
, kSCResvInactive
)) {
141 __private_extern__ Boolean
142 __setPrefsEnabled(SCPreferencesRef prefs
,
146 CFDictionaryRef curConfig
;
147 CFMutableDictionaryRef newConfig
= NULL
;
150 // preserve current configuration
151 curConfig
= SCPreferencesPathGetValue(prefs
, path
);
152 if (curConfig
!= NULL
) {
153 if (!isA_CFDictionary(curConfig
)) {
154 _SCErrorSet(kSCStatusFailed
);
157 newConfig
= CFDictionaryCreateMutableCopy(NULL
, 0, curConfig
);
161 CFDictionaryRemoveValue(newConfig
, kSCResvInactive
);
164 CFDictionarySetValue(newConfig
, kSCResvInactive
, kCFBooleanTrue
);
169 newConfig
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
170 CFDictionarySetValue(newConfig
, kSCResvInactive
, kCFBooleanTrue
);
174 // set new configuration
175 if (_SC_CFEqual(curConfig
, newConfig
)) {
177 if (newConfig
!= NULL
) CFRelease(newConfig
);
179 } else if (newConfig
!= NULL
) {
180 // if updated configuration (or we are establishing as disabled)
181 ok
= SCPreferencesPathSetValue(prefs
, path
, newConfig
);
182 CFRelease(newConfig
);
184 ok
= SCPreferencesPathRemoveValue(prefs
, path
);
185 if (!ok
&& (SCError() == kSCStatusNoKey
)) {
194 static CFDictionaryRef
198 CFErrorRef error
= NULL
;
200 CFDictionaryRef templates
;
202 CFDataRef xmlTemplates
= NULL
;
204 bundle
= _SC_CFBundleGet();
205 if (bundle
== NULL
) {
209 url
= CFBundleCopyResourceURL(bundle
, CFSTR("NetworkConfiguration"), CFSTR("plist"), NULL
);
214 #pragma GCC diagnostic push
215 #pragma GCC diagnostic ignored "-Wdeprecated"
216 ok
= CFURLCreateDataAndPropertiesFromResource(NULL
, url
, &xmlTemplates
, NULL
, NULL
, NULL
);
217 #pragma GCC diagnostic pop
219 if (!ok
|| (xmlTemplates
== NULL
)) {
223 // convert the XML data into a property list
224 templates
= CFPropertyListCreateWithData(NULL
, xmlTemplates
, kCFPropertyListImmutable
, NULL
, &error
);
225 CFRelease(xmlTemplates
);
226 if (templates
== NULL
) {
228 SCLog(TRUE
, LOG_DEBUG
, CFSTR("could not load SCNetworkConfiguration templates: %@"), error
);
234 if (!isA_CFDictionary(templates
)) {
235 CFRelease(templates
);
243 __private_extern__ CFDictionaryRef
244 __copyInterfaceTemplate(CFStringRef interfaceType
,
245 CFStringRef childInterfaceType
)
247 CFDictionaryRef interface
= NULL
;
248 CFDictionaryRef interfaces
;
249 CFDictionaryRef templates
;
251 templates
= __copyTemplates();
252 if (templates
== NULL
) {
256 interfaces
= CFDictionaryGetValue(templates
, CFSTR("Interface"));
257 if (!isA_CFDictionary(interfaces
)) {
258 CFRelease(templates
);
262 if (childInterfaceType
== NULL
) {
263 interface
= CFDictionaryGetValue(interfaces
, interfaceType
);
265 CFStringRef expandedType
;
267 if (CFStringFind(childInterfaceType
, CFSTR("."), 0).location
!= kCFNotFound
) {
269 childInterfaceType
= CFSTR("*");
272 expandedType
= CFStringCreateWithFormat(NULL
,
277 interface
= CFDictionaryGetValue(interfaces
, expandedType
);
278 CFRelease(expandedType
);
281 if (isA_CFDictionary(interface
) && (CFDictionaryGetCount(interface
) > 0)) {
287 CFRelease(templates
);
293 __private_extern__ CFDictionaryRef
294 __copyProtocolTemplate(CFStringRef interfaceType
,
295 CFStringRef childInterfaceType
,
296 CFStringRef protocolType
)
298 CFDictionaryRef interface
= NULL
;
299 CFDictionaryRef protocol
= NULL
;
300 CFDictionaryRef protocols
;
301 CFDictionaryRef templates
;
303 templates
= __copyTemplates();
304 if (templates
== NULL
) {
308 protocols
= CFDictionaryGetValue(templates
, CFSTR("Protocol"));
309 if (!isA_CFDictionary(protocols
)) {
310 CFRelease(templates
);
314 if (childInterfaceType
== NULL
) {
315 interface
= CFDictionaryGetValue(protocols
, interfaceType
);
317 CFStringRef expandedType
;
319 if (CFStringFind(childInterfaceType
, CFSTR("."), 0).location
!= kCFNotFound
) {
321 childInterfaceType
= CFSTR("*");
324 expandedType
= CFStringCreateWithFormat(NULL
,
329 interface
= CFDictionaryGetValue(protocols
, expandedType
);
330 CFRelease(expandedType
);
333 if (isA_CFDictionary(interface
)) {
334 protocol
= CFDictionaryGetValue(interface
, protocolType
);
335 if (isA_CFDictionary(protocol
)) {
342 CFRelease(templates
);
348 __private_extern__ Boolean
349 __createInterface(int s
, CFStringRef interface
)
353 bzero(&ifr
, sizeof(ifr
));
354 (void) _SC_cfstring_to_cstring(interface
,
356 sizeof(ifr
.ifr_name
),
357 kCFStringEncodingASCII
);
359 if (ioctl(s
, SIOCIFCREATE
, &ifr
) == -1) {
362 CFSTR("could not create interface \"%@\": %s"),
372 __private_extern__ Boolean
373 __destroyInterface(int s
, CFStringRef interface
)
377 bzero(&ifr
, sizeof(ifr
));
378 (void) _SC_cfstring_to_cstring(interface
,
380 sizeof(ifr
.ifr_name
),
381 kCFStringEncodingASCII
);
383 if (ioctl(s
, SIOCIFDESTROY
, &ifr
) == -1) {
386 CFSTR("could not destroy interface \"%@\": %s"),
397 * For rdar://problem/4685223
399 * To keep MoreSCF happy we need to ensure that the first "Set" and
400 * "NetworkService" have a [less than] unique identifier that can
401 * be parsed as a numeric string.
403 * Note: this backwards compatibility code must be enabled using the
406 * sudo defaults write \
407 * /Library/Preferences/SystemConfiguration/preferences \
413 __SCPreferencesPathCreateUniqueChild_WithMoreSCFCompatibility(SCPreferencesRef prefs
, CFStringRef prefix
)
415 static int hack
= -1;
416 CFStringRef path
= NULL
;
421 enable
= SCPreferencesGetValue(prefs
, CFSTR("MoreSCF"));
422 hack
= (isA_CFBoolean(enable
) && CFBooleanGetValue(enable
)) ? 1 : 0;
426 CFDictionaryRef dict
;
429 path
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%@/%@"), prefix
, CFSTR("0"));
430 dict
= SCPreferencesPathGetValue(prefs
, path
);
432 // if path "0" exists
437 // unique child with path "0" does not exist, create
438 dict
= CFDictionaryCreate(NULL
,
440 &kCFTypeDictionaryKeyCallBacks
,
441 &kCFTypeDictionaryValueCallBacks
);
442 ok
= SCPreferencesPathSetValue(prefs
, path
, dict
);
456 __copy_legacy_password(CFTypeRef password
)
458 if (password
== NULL
) {
462 if (isA_CFData(password
)) {
465 n
= CFDataGetLength(password
);
466 if ((n
% sizeof(UniChar
)) == 0) {
467 CFStringEncoding encoding
;
471 encoding
= (*(CFDataGetBytePtr(password
) + 1) == 0x00) ? kCFStringEncodingUTF16LE
: kCFStringEncodingUTF16BE
;
472 #else // __LITTLE_ENDIAN__
473 encoding
= (*(CFDataGetBytePtr(password
) ) == 0x00) ? kCFStringEncodingUTF16BE
: kCFStringEncodingUTF16LE
;
475 str
= CFStringCreateWithBytes(NULL
,
476 (const UInt8
*)CFDataGetBytePtr(password
),
480 password
= CFStringCreateExternalRepresentation(NULL
,
482 kCFStringEncodingUTF8
,
488 } else if (isA_CFString(password
) && (CFStringGetLength(password
) > 0)) {
489 // convert password to CFData
490 password
= CFStringCreateExternalRepresentation(NULL
,
492 kCFStringEncodingUTF8
,
504 __extract_password(SCPreferencesRef prefs
,
505 CFDictionaryRef config
,
506 CFStringRef passwordKey
,
507 CFStringRef encryptionKey
,
508 CFStringRef encryptionKeyChainValue
,
509 CFStringRef unique_id
,
512 CFStringRef encryption
= NULL
;
513 Boolean exists
= FALSE
;
515 // check for keychain password
516 if (config
!= NULL
) {
517 encryption
= CFDictionaryGetValue(config
, encryptionKey
);
519 if ((encryption
== NULL
) ||
520 (isA_CFString(encryption
) &&
521 CFEqual(encryption
, encryptionKeyChainValue
))) {
523 if (password
!= NULL
) {
525 *password
= _SCPreferencesSystemKeychainPasswordItemCopy(prefs
, unique_id
);
527 *password
= _SCSecKeychainPasswordItemCopy(NULL
, unique_id
);
529 exists
= (*password
!= NULL
);
532 exists
= _SCPreferencesSystemKeychainPasswordItemExists(prefs
, unique_id
);
534 exists
= _SCSecKeychainPasswordItemExists(NULL
, unique_id
);
539 // as needed, check for in-line password
540 if (!exists
&& (encryption
== NULL
) && (config
!= NULL
)) {
541 CFDataRef inline_password
;
543 inline_password
= CFDictionaryGetValue(config
, passwordKey
);
544 inline_password
= __copy_legacy_password(inline_password
);
545 if (inline_password
!= NULL
) {
548 if (password
!= NULL
) {
549 *password
= inline_password
;
551 CFRelease(inline_password
);
562 __remove_password(SCPreferencesRef prefs
,
563 CFDictionaryRef config
,
564 CFStringRef passwordKey
,
565 CFStringRef encryptionKey
,
566 CFStringRef encryptionKeyChainValue
,
567 CFStringRef unique_id
,
568 CFDictionaryRef
*newConfig
)
570 CFStringRef encryption
= NULL
;
573 // check for keychain password
574 if (config
!= NULL
) {
575 encryption
= CFDictionaryGetValue(config
, encryptionKey
);
577 if ((encryption
== NULL
) ||
578 (isA_CFString(encryption
) &&
579 CFEqual(encryption
, encryptionKeyChainValue
))) {
580 // remove keychain password
582 ok
= _SCPreferencesSystemKeychainPasswordItemRemove(prefs
, unique_id
);
584 ok
= _SCSecKeychainPasswordItemRemove(NULL
, unique_id
);
588 // as needed, check if we have an in-line password that we can remove
589 if (!ok
&& (encryption
== NULL
) && (config
!= NULL
)) {
590 CFDataRef inline_password
;
592 inline_password
= CFDictionaryGetValue(config
, passwordKey
);
593 inline_password
= __copy_legacy_password(inline_password
);
594 if (inline_password
!= NULL
) {
595 CFRelease(inline_password
);
600 if (newConfig
!= NULL
) {
601 if (ok
&& (config
!= NULL
)) {
602 CFMutableDictionaryRef temp
;
604 temp
= CFDictionaryCreateMutableCopy(NULL
, 0, config
);
605 CFDictionaryRemoveValue(temp
, passwordKey
);
606 CFDictionaryRemoveValue(temp
, encryptionKey
);
607 *newConfig
= (CFDictionaryRef
)temp
;
617 __private_extern__ Boolean
618 __rank_to_str(SCNetworkServicePrimaryRank rank
, CFStringRef
*rankStr
)
621 case kSCNetworkServicePrimaryRankDefault
:
624 case kSCNetworkServicePrimaryRankFirst
:
625 *rankStr
= kSCValNetServicePrimaryRankFirst
;
627 case kSCNetworkServicePrimaryRankLast
:
628 *rankStr
= kSCValNetServicePrimaryRankLast
;
630 case kSCNetworkServicePrimaryRankNever
:
631 *rankStr
= kSCValNetServicePrimaryRankNever
;
633 case kSCNetworkServicePrimaryRankScoped
:
634 *rankStr
= kSCValNetServicePrimaryRankScoped
;
644 __private_extern__ Boolean
645 __str_to_rank(CFStringRef rankStr
, SCNetworkServicePrimaryRank
*rank
)
647 if (isA_CFString(rankStr
)) {
648 if (CFEqual(rankStr
, kSCValNetServicePrimaryRankFirst
)) {
649 *rank
= kSCNetworkServicePrimaryRankFirst
;
650 } else if (CFEqual(rankStr
, kSCValNetServicePrimaryRankLast
)) {
651 *rank
= kSCNetworkServicePrimaryRankLast
;
652 } else if (CFEqual(rankStr
, kSCValNetServicePrimaryRankNever
)) {
653 *rank
= kSCNetworkServicePrimaryRankNever
;
654 } else if (CFEqual(rankStr
, kSCValNetServicePrimaryRankScoped
)) {
655 *rank
= kSCNetworkServicePrimaryRankScoped
;
659 } else if (rankStr
== NULL
) {
660 *rank
= kSCNetworkServicePrimaryRankDefault
;