2 * Copyright (c) 2003-2008, 2011-2017 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 29, 2003 Allan Nathanson <ajn@apple.com>
31 #include <TargetConditionals.h>
34 #include <sys/param.h>
35 #include <sys/types.h>
42 #include "SCNetworkConfigurationInternal.h"
45 #include <Security/Authorization.h>
46 #endif /* !TARGET_OS_IPHONE */
49 /* -------------------- */
54 __loadSecurity(void) {
55 static void *image
= NULL
;
56 static dispatch_once_t once
;
58 dispatch_once(&once
, ^{
59 image
= _SC_dlopen("/System/Library/Frameworks/Security.framework/Security");
67 _AuthorizationCreate(const AuthorizationRights
*rights
, const AuthorizationEnvironment
*environment
, AuthorizationFlags flags
, AuthorizationRef
*authorization
)
69 #undef AuthorizationCreate
70 static typeof (AuthorizationCreate
) *dyfunc
= NULL
;
72 void *image
= __loadSecurity();
73 if (image
) dyfunc
= dlsym(image
, "AuthorizationCreate");
75 return dyfunc
? dyfunc(rights
, environment
, flags
, authorization
) : -1;
77 #define AuthorizationCreate _AuthorizationCreate
81 _AuthorizationFree(AuthorizationRef authorization
, AuthorizationFlags flags
)
83 #undef AuthorizationFree
84 static typeof (AuthorizationFree
) *dyfunc
= NULL
;
86 void *image
= __loadSecurity();
87 if (image
) dyfunc
= dlsym(image
, "AuthorizationFree");
89 return dyfunc
? dyfunc(authorization
, flags
) : -1;
91 #define AuthorizationFree _AuthorizationFree
94 /* -------------------- */
98 _prefs_AuthorizationCreate()
100 AuthorizationRef authorization
= NULL
;
102 if (getenv("SCPREFERENCES_USE_ENTITLEMENTS") != NULL
) {
103 authorization
= kSCPreferencesUseEntitlementAuthorization
;
105 AuthorizationFlags flags
= kAuthorizationFlagDefaults
;
108 status
= AuthorizationCreate(NULL
,
109 kAuthorizationEmptyEnvironment
,
112 if (status
!= errAuthorizationSuccess
) {
115 CFSTR("AuthorizationCreate() failed: status = %d\n"),
121 return authorization
;
127 _prefs_AuthorizationFree(AuthorizationRef authorization
)
129 if (authorization
!= kSCPreferencesUseEntitlementAuthorization
) {
130 AuthorizationFree(authorization
, kAuthorizationFlagDefaults
);
131 // AuthorizationFree(authorization, kAuthorizationFlagDestroyRights);
137 #endif /* !TARGET_OS_IPHONE */
139 /* -------------------- */
142 __private_extern__ Boolean _prefs_changed
= FALSE
;
147 _prefs_open(CFStringRef name
, CFStringRef prefsID
)
150 CFMutableDictionaryRef options
= NULL
;
151 Boolean useHelper
= FALSE
;
152 Boolean useOptions
= FALSE
;
154 authorization
= NULL
;
156 if (geteuid() != 0) {
157 // if we need to use a helper
160 #if !TARGET_OS_IPHONE
161 authorization
= _prefs_AuthorizationCreate();
163 authorization
= kSCPreferencesUseEntitlementAuthorization
;
164 #endif /* !TARGET_OS_IPHONE */
167 if (getenv("SCPREFERENCES_REMOVE_WHEN_EMPTY") != NULL
) {
168 if (options
== NULL
) {
169 options
= CFDictionaryCreateMutable(NULL
,
171 &kCFTypeDictionaryKeyCallBacks
,
172 &kCFTypeDictionaryValueCallBacks
);
175 CFDictionarySetValue(options
, kSCPreferencesOptionRemoveWhenEmpty
, kCFBooleanTrue
);
178 env
= getenv("SCPREFERENCES_PROTECTION_CLASS");
182 if (options
== NULL
) {
183 options
= CFDictionaryCreateMutable(NULL
,
185 &kCFTypeDictionaryKeyCallBacks
,
186 &kCFTypeDictionaryValueCallBacks
);
189 str
= CFStringCreateWithCString(NULL
, env
, kCFStringEncodingASCII
);
190 CFDictionarySetValue(options
, kSCPreferencesOptionProtectionClass
, str
);
194 if (!useHelper
&& !useOptions
) {
195 // if no helper/options needed
196 prefs
= SCPreferencesCreate(NULL
, name
, prefsID
);
197 } else if (!useOptions
) {
198 // if no options needed
199 prefs
= SCPreferencesCreateWithAuthorization(NULL
, name
, prefsID
, authorization
);
201 prefs
= SCPreferencesCreateWithOptions(NULL
, name
, prefsID
, authorization
, options
);
209 _prefs_changed
= FALSE
;
218 if (!SCPreferencesCommitChanges(prefs
)) {
220 case kSCStatusAccessError
:
221 SCPrint(TRUE
, stderr
, CFSTR("Permission denied.\n"));
226 CFSTR("SCPreferencesCommitChanges() failed: %s\n"),
227 SCErrorString(SCError()));
233 _prefs_changed
= FALSE
;
235 if (!SCPreferencesApplyChanges(prefs
)) {
238 CFSTR("SCPreferencesApplyChanges() failed: %s\n"),
239 SCErrorString(SCError()));
254 _prefs_changed
= FALSE
;
257 if (authorization
!= NULL
) {
258 #if !TARGET_OS_IPHONE
259 _prefs_AuthorizationFree(authorization
);
260 #else /* !TARGET_OS_IPHONE */
261 // Uh...if authorization isn't NULL, something went horribly wrong.
262 #endif /* !TARGET_OS_IPHONE */
263 authorization
= NULL
;
272 _prefs_commitRequired(int argc
, char **argv
, const char *command
)
274 if (_prefs_changed
) {
275 if ((currentInput
!= NULL
) &&
276 isatty(fileno(currentInput
->fp
)) &&
277 ((argc
< 1) || (strcmp(argv
[0], "!") != 0))
279 SCPrint(TRUE
, stdout
,
280 CFSTR("preference changes have not been committed\n"
281 "use \"commit\" to save changes"));
282 if (command
!= NULL
) {
283 SCPrint(TRUE
, stdout
,
284 CFSTR(" or \"%s !\" to abandon changes"),
287 SCPrint(TRUE
, stdout
, CFSTR("\n"));
291 SCPrint(TRUE
, stdout
, CFSTR("preference changes abandoned\n"));
298 /* -------------------- */
302 get_ComputerName(int argc
, char **argv
)
306 CFStringEncoding encoding
;
307 CFStringRef hostname
;
309 hostname
= SCDynamicStoreCopyComputerName(NULL
, &encoding
);
310 if (hostname
== NULL
) {
311 int sc_status
= SCError();
314 case kSCStatusNoKey
:
317 CFSTR("ComputerName: not set\n"));
322 CFSTR("SCDynamicStoreCopyComputerName() failed: %s\n"),
323 SCErrorString(SCError()));
329 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), hostname
);
337 set_ComputerName(int argc
, char **argv
)
339 CFStringRef hostname
= NULL
;
342 ok
= _prefs_open(CFSTR("scutil --set ComputerName"), NULL
);
346 CFSTR("Could not open prefs: %s\n"),
347 SCErrorString(SCError()));
352 CFStringEncoding old_encoding
;
353 CFStringRef old_hostname
;
355 old_hostname
= SCDynamicStoreCopyComputerName(NULL
, &old_encoding
);
356 hostname
= _copyStringFromSTDIN(CFSTR("ComputerName"), old_hostname
);
357 if (old_hostname
) CFRelease(old_hostname
);
358 } else if (strlen(argv
[0]) > 0) {
359 hostname
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
362 ok
= SCPreferencesSetComputerName(prefs
, hostname
, kCFStringEncodingUTF8
);
363 if (hostname
!= NULL
) CFRelease(hostname
);
367 CFSTR("Could not open prefs: %s\n"),
368 SCErrorString(SCError()));
380 get_HostName(int argc
, char **argv
)
384 CFStringRef hostname
;
387 ok
= _prefs_open(CFSTR("scutil --get HostName"), NULL
);
391 CFSTR("SCPreferencesCreate() failed: %s\n"),
392 SCErrorString(SCError()));
396 hostname
= SCPreferencesGetHostName(prefs
);
397 if (hostname
== NULL
) {
398 int sc_status
= SCError();
401 case kSCStatusNoKey
:
404 CFSTR("HostName: not set\n"));
409 CFSTR("SCPreferencesGetHostName() failed: %s\n"),
410 SCErrorString(SCError()));
417 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), hostname
);
424 set_HostName(int argc
, char **argv
)
426 CFStringRef hostname
= NULL
;
429 ok
= _prefs_open(CFSTR("scutil --set HostName"), NULL
);
433 CFSTR("Could not open prefs: %s\n"),
434 SCErrorString(SCError()));
439 hostname
= _copyStringFromSTDIN(CFSTR("HostName"), SCPreferencesGetHostName(prefs
));
440 } else if (strlen(argv
[0]) > 0) {
441 hostname
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
444 ok
= SCPreferencesSetHostName(prefs
, hostname
);
445 if (hostname
!= NULL
) CFRelease(hostname
);
449 CFSTR("SCPreferencesSetHostName() failed: %s\n"),
450 SCErrorString(SCError()));
462 get_LocalHostName(int argc
, char **argv
)
466 CFStringRef hostname
;
468 hostname
= SCDynamicStoreCopyLocalHostName(NULL
);
469 if (hostname
== NULL
) {
470 int sc_status
= SCError();
473 case kSCStatusNoKey
:
476 CFSTR("LocalHostName: not set\n"));
481 CFSTR("SCDynamicStoreCopyLocalHostName() failed: %s\n"),
482 SCErrorString(SCError()));
488 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), hostname
);
496 set_LocalHostName(int argc
, char **argv
)
498 CFStringRef hostname
= NULL
;
501 ok
= _prefs_open(CFSTR("scutil --set LocalHostName"), NULL
);
505 CFSTR("Could not open prefs: %s\n"),
506 SCErrorString(SCError()));
511 CFStringRef old_hostname
;
513 old_hostname
= SCDynamicStoreCopyLocalHostName(NULL
);
514 hostname
= _copyStringFromSTDIN(CFSTR("LocalHostName"), old_hostname
);
515 if (old_hostname
) CFRelease(old_hostname
);
516 } else if (strlen(argv
[0]) > 0) {
517 hostname
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
520 ok
= SCPreferencesSetLocalHostName(prefs
, hostname
);
521 if (hostname
!= NULL
) CFRelease(hostname
);
525 CFSTR("SCPreferencesSetLocalHostName() failed: %s\n"),
526 SCErrorString(SCError()));
537 /* -------------------- */
540 typedef void (*pref_func
) (int argc
, char **argv
);
542 static const struct {
547 { "ComputerName", get_ComputerName
, set_ComputerName
},
548 { "HostName", get_HostName
, set_HostName
},
549 { "LocalHostName", get_LocalHostName
, set_LocalHostName
}
551 #define N_PREF_KEYS (sizeof(pref_keys) / sizeof(pref_keys[0]))
560 for (i
= 0; i
< (int)N_PREF_KEYS
; i
++) {
561 if (strcmp(pref
, pref_keys
[i
].pref
) == 0) {
572 do_getPref(char *pref
, int argc
, char **argv
)
579 (*pref_keys
[i
].get
)(argc
, argv
);
584 // process extended get
585 // ie. scutil --get <filename> <prefs path> <key>
588 do_prefs_open(argc
, argv
);
589 if (SCError() != kSCStatusOK
) {
590 SCPrint(TRUE
, stderr
, CFSTR("%s\n"), SCErrorString(SCError()));
595 do_prefs_get(--argc
, ++argv
);
598 CFStringRef prefs_val
;
600 key
= CFStringCreateWithCString(NULL
, *(++argv
), kCFStringEncodingUTF8
);
601 prefs_val
= CFDictionaryGetValue(value
, key
);
604 if (prefs_val
!= NULL
) {
605 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), prefs_val
);
611 // if path or key not found
619 do_setPref(char *pref
, int argc
, char **argv
)
625 (*pref_keys
[i
].set
)(argc
, argv
);
631 /* -------------------- */
644 do_prefs_open(int argc
, char **argv
)
647 CFStringRef prefsID
= NULL
;
650 if (_prefs_commitRequired(argc
, argv
, "close")) {
654 do_prefs_close(0, NULL
);
658 prefsID
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
661 ok
= _prefs_open(CFSTR("scutil --prefs"), prefsID
);
662 if (prefsID
!= NULL
) CFRelease(prefsID
);
666 CFSTR("Could not open prefs: %s\n"),
667 SCErrorString(SCError()));
677 do_prefs_lock(int argc
, char **argv
)
680 Boolean wait
= (argc
> 0) ? TRUE
: FALSE
;
682 if (!SCPreferencesLock(prefs
, wait
)) {
683 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
693 do_prefs_unlock(int argc
, char **argv
)
697 if (!SCPreferencesUnlock(prefs
)) {
698 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
708 do_prefs_commit(int argc
, char **argv
)
712 if (!SCPreferencesCommitChanges(prefs
)) {
713 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
717 _prefs_changed
= FALSE
;
724 do_prefs_apply(int argc
, char **argv
)
728 if (!SCPreferencesApplyChanges(prefs
)) {
729 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
738 do_prefs_close(int argc
, char **argv
)
740 if (_prefs_commitRequired(argc
, argv
, "close")) {
751 do_prefs_quit(int argc
, char **argv
)
753 if (_prefs_commitRequired(argc
, argv
, "quit")) {
759 termRequested
= TRUE
;
766 do_prefs_synchronize(int argc
, char **argv
)
770 SCPreferencesSynchronize(prefs
);
775 static CFComparisonResult
776 sort_paths(const void *p1
, const void *p2
, void *context
)
778 #pragma unused(context)
779 CFStringRef path1
= (CFStringRef
)p1
;
780 CFStringRef path2
= (CFStringRef
)p2
;
781 return CFStringCompare(path1
, path2
, 0);
787 do_prefs_list(int argc
, char **argv
)
791 CFMutableArrayRef paths
= NULL
;
793 CFDictionaryRef entity
;
795 prefix
= CFStringCreateWithCString(NULL
,
796 (argc
>= 1) ? argv
[0] : "/",
797 kCFStringEncodingUTF8
);
799 entity
= SCPreferencesPathGetValue(prefs
, prefix
);
800 if (entity
== NULL
) {
801 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
805 paths
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
807 n
= isA_CFDictionary(entity
) ? CFDictionaryGetCount(entity
) : 0;
813 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
814 vals
= CFAllocatorAllocate(NULL
, n
* sizeof(CFPropertyListRef
), 0);
815 CFDictionaryGetKeysAndValues(entity
, keys
, vals
);
816 for (i
= 0; i
< n
; i
++) {
817 if (isA_CFDictionary(vals
[i
])) {
818 CFArrayAppendValue(paths
, keys
[i
]);
821 CFAllocatorDeallocate(NULL
, keys
);
822 CFAllocatorDeallocate(NULL
, vals
);
825 n
= CFArrayGetCount(paths
);
826 CFArraySortValues(paths
,
832 for (i
= 0; i
< n
; i
++) {
835 CFSTR(" path [%d] = %@/%@\n"),
837 CFEqual(prefix
, CFSTR("/")) ? CFSTR("") : prefix
,
838 CFArrayGetValueAtIndex(paths
, i
));
841 SCPrint(TRUE
, stdout
, CFSTR(" no paths.\n"));
855 do_prefs_get(int argc
, char **argv
)
858 CFDictionaryRef dict
;
861 CFMutableDictionaryRef newDict
;
864 path
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
866 link
= SCPreferencesPathGetLink(prefs
, path
);
868 SCPrint(TRUE
, stdout
, CFSTR(" --> %@\n"), link
);
871 dict
= SCPreferencesPathGetValue(prefs
, path
);
874 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
878 newDict
= CFDictionaryCreateMutable(NULL
,
880 &kCFTypeDictionaryKeyCallBacks
,
881 &kCFTypeDictionaryValueCallBacks
);
883 // remove [path] children
884 n
= isA_CFDictionary(dict
) ? CFDictionaryGetCount(dict
) : 0;
890 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
891 vals
= CFAllocatorAllocate(NULL
, n
* sizeof(CFPropertyListRef
), 0);
892 CFDictionaryGetKeysAndValues(dict
, keys
, vals
);
893 for (i
= 0; i
< n
; i
++) {
894 if (!isA_CFDictionary(vals
[i
])) {
895 CFDictionaryAddValue(newDict
, keys
[i
], vals
[i
]);
898 CFAllocatorDeallocate(NULL
, keys
);
899 CFAllocatorDeallocate(NULL
, vals
);
903 CFRelease(value
); /* we have new information, release the old */
914 do_prefs_set(int argc
, char **argv
)
916 CFDictionaryRef dict
;
917 CFMutableDictionaryRef newDict
= NULL
;
920 path
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
921 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, value
);
923 dict
= SCPreferencesPathGetValue(prefs
, path
);
927 // retain [path] children
928 n
= CFDictionaryGetCount(dict
);
934 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
935 vals
= CFAllocatorAllocate(NULL
, n
* sizeof(CFPropertyListRef
), 0);
936 CFDictionaryGetKeysAndValues(dict
, keys
, vals
);
937 for (i
= 0; i
< n
; i
++) {
938 if (isA_CFDictionary(vals
[i
])) {
939 if (CFDictionaryContainsKey(newDict
, keys
[i
])) {
940 SCPrint(TRUE
, stdout
, CFSTR(" key %@ is already a path component and cannot be replaced\n"), keys
[i
]);
943 CFDictionaryAddValue(newDict
, keys
[i
], vals
[i
]);
946 CFAllocatorDeallocate(NULL
, keys
);
947 CFAllocatorDeallocate(NULL
, vals
);
949 } else if (SCError() == kSCStatusInvalidArgument
) {
950 SCPrint(TRUE
, stdout
, CFSTR(" a path component is not a dictionary\n"));
952 } else if (SCError() != kSCStatusNoKey
) {
953 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
962 link
= CFStringCreateWithCString(NULL
, argv
[1], kCFStringEncodingUTF8
);
963 ok
= SCPreferencesPathSetLink(prefs
, path
, link
);
966 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
971 if (!SCPreferencesPathSetValue(prefs
, path
, newDict
)) {
972 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
977 _prefs_changed
= TRUE
;
989 do_prefs_remove(int argc
, char **argv
)
994 path
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
996 if (!SCPreferencesPathRemoveValue(prefs
, path
)) {
997 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
1001 _prefs_changed
= TRUE
;
1010 on_off_str(Boolean on
)
1012 return (on
? "on" : "off");
1015 /* -------------------- */
1017 #if !TARGET_OS_IPHONE
1019 #include "InterfaceNamerControlPrefs.h"
1022 allow_new_interfaces_usage(void)
1024 fprintf(stderr
, "usage: scutil --allow-new-interfaces [on|off|default]\n");
1030 do_ifnamer(char * pref
, int argc
, char **argv
)
1032 Boolean allow
= FALSE
;
1035 allow_new_interfaces_usage();
1039 if (strcmp(pref
, "allow-new-interfaces")) {
1044 SCPrint(TRUE
, stdout
, CFSTR("AllowNewInterfaces is %s\n"),
1045 on_off_str(InterfaceNamerControlPrefsAllowNewInterfaces()));
1049 if ((strcasecmp(argv
[0], "disable") == 0) ||
1050 (strcasecmp(argv
[0], "no" ) == 0) ||
1051 (strcasecmp(argv
[0], "off" ) == 0) ||
1052 (strcasecmp(argv
[0], "0" ) == 0)) {
1054 } else if ((strcasecmp(argv
[0], "enable") == 0) ||
1055 (strcasecmp(argv
[0], "yes" ) == 0) ||
1056 (strcasecmp(argv
[0], "on" ) == 0) ||
1057 (strcasecmp(argv
[0], "1" ) == 0)) {
1059 } else if (strcasecmp(argv
[0], "default") == 0) {
1062 allow_new_interfaces_usage();
1066 if (!InterfaceNamerControlPrefsSetAllowNewInterfaces(allow
)) {
1067 SCPrint(TRUE
, stderr
, CFSTR("failed to set preferences\n"));
1075 #endif // !TARGET_OS_IPHONE
1077 /* -------------------- */
1079 #include "IPMonitorControlPrefs.h"
1083 do_log(char * log
, int argc
, char **argv
)
1085 Boolean verbose
= FALSE
;
1087 if (strcmp(log
, "IPMonitor")) {
1092 SCPrint(TRUE
, stdout
, CFSTR("IPMonitor log is %s\n"),
1093 on_off_str(IPMonitorControlPrefsIsVerbose()));
1097 if ((strcasecmp(argv
[0], "disable") == 0) ||
1098 (strcasecmp(argv
[0], "no" ) == 0) ||
1099 (strcasecmp(argv
[0], "off" ) == 0) ||
1100 (strcasecmp(argv
[0], "0" ) == 0)) {
1102 } else if ((strcasecmp(argv
[0], "enable") == 0) ||
1103 (strcasecmp(argv
[0], "yes" ) == 0) ||
1104 (strcasecmp(argv
[0], "on" ) == 0) ||
1105 (strcasecmp(argv
[0], "1" ) == 0)) {
1108 SCPrint(TRUE
, stdout
, CFSTR("invalid value, must be 'on' or 'off'\n"));
1112 if (!IPMonitorControlPrefsSetVerbose(verbose
)) {
1113 SCPrint(TRUE
, stderr
, CFSTR("failed to set preferences\n"));
1122 /* -------------------- */
1124 static SCNetworkInterfaceRef
1125 copy_configured_interface(SCPreferencesRef prefs
, CFStringRef if_name
)
1127 SCNetworkSetRef current_set
= NULL
;
1130 SCNetworkInterfaceRef ret_if
= NULL
;
1131 CFArrayRef services
= NULL
;
1133 if (prefs
== NULL
) {
1136 current_set
= SCNetworkSetCopyCurrent(prefs
);
1137 if (current_set
== NULL
) {
1140 services
= SCNetworkSetCopyServices(current_set
);
1141 if (services
== NULL
) {
1145 count
= CFArrayGetCount(services
);
1146 for (i
= 0; i
< count
; i
++) {
1147 CFStringRef this_if_name
;
1148 SCNetworkInterfaceRef this_if
;
1149 SCNetworkServiceRef s
;
1151 s
= (SCNetworkServiceRef
)CFArrayGetValueAtIndex(services
, i
);
1152 if (!SCNetworkServiceGetEnabled(s
)) {
1153 /* skip disabled services */
1156 this_if
= SCNetworkServiceGetInterface(s
);
1157 if (this_if
== NULL
) {
1160 this_if_name
= SCNetworkInterfaceGetBSDName(this_if
);
1161 if (this_if_name
== NULL
) {
1164 if (CFEqual(this_if_name
, if_name
)) {
1172 if (current_set
!= NULL
) {
1173 CFRelease(current_set
);
1175 if (services
!= NULL
) {
1176 CFRelease(services
);
1182 disable_until_needed_usage(void)
1184 fprintf(stderr
, "usage: scutil --disable-until-needed <interfaceName> [on|off|default]\n");
1188 #include <SystemConfiguration/SCNetworkConfigurationPrivate.h>
1192 do_disable_until_needed(int argc
, char **argv
)
1194 const char * if_name
;
1195 CFStringRef if_name_cf
;
1196 SCNetworkInterfaceRef net_if
;
1198 const char * on_off
= "?";
1200 Boolean set_default
= FALSE
;
1201 Boolean set_value
= FALSE
;
1203 if (argc
< 1 || argc
> 2) {
1204 disable_until_needed_usage();
1210 if (strcasecmp(on_off
, "on") == 0) {
1212 } else if (strcasecmp(on_off
, "off") == 0) {
1214 } else if ((strcmp(on_off
, "") == 0) || (strcasecmp(on_off
, "default") == 0)) {
1218 disable_until_needed_usage();
1223 ok
= _prefs_open(CFSTR("scutil --disable-until-needed"), NULL
);
1227 CFSTR("Could not open prefs: %s\n"),
1228 SCErrorString(SCError()));
1231 if_name_cf
= CFStringCreateWithCStringNoCopy(NULL
,
1233 kCFStringEncodingASCII
,
1235 net_if
= copy_configured_interface(prefs
, if_name_cf
);
1236 if (net_if
== NULL
) {
1237 fprintf(stderr
, "%s is not configured\n", if_name
);
1242 ok
= SCNetworkInterfaceSetDisableUntilNeeded(net_if
, on
);
1244 ok
= __SCNetworkInterfaceSetDisableUntilNeededValue(net_if
, NULL
);
1247 fprintf(stderr
, "failed to turn disable-until-needed %s\n",
1253 on
= SCNetworkInterfaceGetDisableUntilNeeded(net_if
);
1254 printf("%s disable-until-needed is %s\n", if_name
, on_off_str(on
));