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
)
149 #if TARGET_OS_EMBEDDED
151 #endif // TARGET_OS_EMBEDDED
152 CFMutableDictionaryRef options
= NULL
;
153 Boolean useHelper
= FALSE
;
154 Boolean useOptions
= FALSE
;
156 authorization
= NULL
;
158 if (geteuid() != 0) {
159 // if we need to use a helper
162 #if !TARGET_OS_IPHONE
163 authorization
= _prefs_AuthorizationCreate();
165 authorization
= kSCPreferencesUseEntitlementAuthorization
;
166 #endif /* !TARGET_OS_IPHONE */
169 if (getenv("SCPREFERENCES_REMOVE_WHEN_EMPTY") != NULL
) {
170 if (options
== NULL
) {
171 options
= CFDictionaryCreateMutable(NULL
,
173 &kCFTypeDictionaryKeyCallBacks
,
174 &kCFTypeDictionaryValueCallBacks
);
177 CFDictionarySetValue(options
, kSCPreferencesOptionRemoveWhenEmpty
, kCFBooleanTrue
);
180 #if TARGET_OS_EMBEDDED
181 env
= getenv("SCPREFERENCES_PROTECTION_CLASS");
185 if (options
== NULL
) {
186 options
= CFDictionaryCreateMutable(NULL
,
188 &kCFTypeDictionaryKeyCallBacks
,
189 &kCFTypeDictionaryValueCallBacks
);
192 str
= CFStringCreateWithCString(NULL
, env
, kCFStringEncodingASCII
);
193 CFDictionarySetValue(options
, kSCPreferencesOptionProtectionClass
, str
);
196 #endif // TARGET_OS_EMBEDDED
198 if (!useHelper
&& !useOptions
) {
199 // if no helper/options needed
200 prefs
= SCPreferencesCreate(NULL
, name
, prefsID
);
201 } else if (!useOptions
) {
202 // if no options needed
203 prefs
= SCPreferencesCreateWithAuthorization(NULL
, name
, prefsID
, authorization
);
205 prefs
= SCPreferencesCreateWithOptions(NULL
, name
, prefsID
, authorization
, options
);
213 _prefs_changed
= FALSE
;
222 if (!SCPreferencesCommitChanges(prefs
)) {
224 case kSCStatusAccessError
:
225 SCPrint(TRUE
, stderr
, CFSTR("Permission denied.\n"));
230 CFSTR("SCPreferencesCommitChanges() failed: %s\n"),
231 SCErrorString(SCError()));
237 _prefs_changed
= FALSE
;
239 if (!SCPreferencesApplyChanges(prefs
)) {
242 CFSTR("SCPreferencesApplyChanges() failed: %s\n"),
243 SCErrorString(SCError()));
258 _prefs_changed
= FALSE
;
261 if (authorization
!= NULL
) {
262 #if !TARGET_OS_IPHONE
263 _prefs_AuthorizationFree(authorization
);
264 #else /* !TARGET_OS_IPHONE */
265 // Uh...if authorization isn't NULL, something went horribly wrong.
266 #endif /* !TARGET_OS_IPHONE */
267 authorization
= NULL
;
276 _prefs_commitRequired(int argc
, char **argv
, const char *command
)
278 if (_prefs_changed
) {
279 if ((currentInput
!= NULL
) &&
280 isatty(fileno(currentInput
->fp
)) &&
281 ((argc
< 1) || (strcmp(argv
[0], "!") != 0))
283 SCPrint(TRUE
, stdout
,
284 CFSTR("preference changes have not been committed\n"
285 "use \"commit\" to save changes"));
286 if (command
!= NULL
) {
287 SCPrint(TRUE
, stdout
,
288 CFSTR(" or \"%s !\" to abandon changes"),
291 SCPrint(TRUE
, stdout
, CFSTR("\n"));
295 SCPrint(TRUE
, stdout
, CFSTR("preference changes abandoned\n"));
302 /* -------------------- */
306 get_ComputerName(int argc
, char **argv
)
310 CFStringEncoding encoding
;
311 CFStringRef hostname
;
313 hostname
= SCDynamicStoreCopyComputerName(NULL
, &encoding
);
314 if (hostname
== NULL
) {
315 int sc_status
= SCError();
318 case kSCStatusNoKey
:
321 CFSTR("ComputerName: not set\n"));
326 CFSTR("SCDynamicStoreCopyComputerName() failed: %s\n"),
327 SCErrorString(SCError()));
333 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), hostname
);
341 set_ComputerName(int argc
, char **argv
)
343 CFStringRef hostname
= NULL
;
346 ok
= _prefs_open(CFSTR("scutil --set ComputerName"), NULL
);
350 CFSTR("Could not open prefs: %s\n"),
351 SCErrorString(SCError()));
356 CFStringEncoding old_encoding
;
357 CFStringRef old_hostname
;
359 old_hostname
= SCDynamicStoreCopyComputerName(NULL
, &old_encoding
);
360 hostname
= _copyStringFromSTDIN(CFSTR("ComputerName"), old_hostname
);
361 if (old_hostname
) CFRelease(old_hostname
);
362 } else if (strlen(argv
[0]) > 0) {
363 hostname
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
366 ok
= SCPreferencesSetComputerName(prefs
, hostname
, kCFStringEncodingUTF8
);
367 if (hostname
!= NULL
) CFRelease(hostname
);
371 CFSTR("Could not open prefs: %s\n"),
372 SCErrorString(SCError()));
384 get_HostName(int argc
, char **argv
)
388 CFStringRef hostname
;
391 ok
= _prefs_open(CFSTR("scutil --get HostName"), NULL
);
395 CFSTR("SCPreferencesCreate() failed: %s\n"),
396 SCErrorString(SCError()));
400 hostname
= SCPreferencesGetHostName(prefs
);
401 if (hostname
== NULL
) {
402 int sc_status
= SCError();
405 case kSCStatusNoKey
:
408 CFSTR("HostName: not set\n"));
413 CFSTR("SCPreferencesGetHostName() failed: %s\n"),
414 SCErrorString(SCError()));
421 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), hostname
);
428 set_HostName(int argc
, char **argv
)
430 CFStringRef hostname
= NULL
;
433 ok
= _prefs_open(CFSTR("scutil --set HostName"), NULL
);
437 CFSTR("Could not open prefs: %s\n"),
438 SCErrorString(SCError()));
443 hostname
= _copyStringFromSTDIN(CFSTR("HostName"), SCPreferencesGetHostName(prefs
));
444 } else if (strlen(argv
[0]) > 0) {
445 hostname
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
448 ok
= SCPreferencesSetHostName(prefs
, hostname
);
449 if (hostname
!= NULL
) CFRelease(hostname
);
453 CFSTR("SCPreferencesSetHostName() failed: %s\n"),
454 SCErrorString(SCError()));
466 get_LocalHostName(int argc
, char **argv
)
470 CFStringRef hostname
;
472 hostname
= SCDynamicStoreCopyLocalHostName(NULL
);
473 if (hostname
== NULL
) {
474 int sc_status
= SCError();
477 case kSCStatusNoKey
:
480 CFSTR("LocalHostName: not set\n"));
485 CFSTR("SCDynamicStoreCopyLocalHostName() failed: %s\n"),
486 SCErrorString(SCError()));
492 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), hostname
);
500 set_LocalHostName(int argc
, char **argv
)
502 CFStringRef hostname
= NULL
;
505 ok
= _prefs_open(CFSTR("scutil --set LocalHostName"), NULL
);
509 CFSTR("Could not open prefs: %s\n"),
510 SCErrorString(SCError()));
515 CFStringRef old_hostname
;
517 old_hostname
= SCDynamicStoreCopyLocalHostName(NULL
);
518 hostname
= _copyStringFromSTDIN(CFSTR("LocalHostName"), old_hostname
);
519 if (old_hostname
) CFRelease(old_hostname
);
520 } else if (strlen(argv
[0]) > 0) {
521 hostname
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
524 ok
= SCPreferencesSetLocalHostName(prefs
, hostname
);
525 if (hostname
!= NULL
) CFRelease(hostname
);
529 CFSTR("SCPreferencesSetLocalHostName() failed: %s\n"),
530 SCErrorString(SCError()));
541 /* -------------------- */
544 typedef void (*pref_func
) (int argc
, char **argv
);
546 static const struct {
551 { "ComputerName", get_ComputerName
, set_ComputerName
},
552 { "HostName", get_HostName
, set_HostName
},
553 { "LocalHostName", get_LocalHostName
, set_LocalHostName
}
555 #define N_PREF_KEYS (sizeof(pref_keys) / sizeof(pref_keys[0]))
564 for (i
= 0; i
< (int)N_PREF_KEYS
; i
++) {
565 if (strcmp(pref
, pref_keys
[i
].pref
) == 0) {
576 do_getPref(char *pref
, int argc
, char **argv
)
583 (*pref_keys
[i
].get
)(argc
, argv
);
588 // process extended get
589 // ie. scutil --get <filename> <prefs path> <key>
592 do_prefs_open(argc
, argv
);
593 if (SCError() != kSCStatusOK
) {
594 SCPrint(TRUE
, stderr
, CFSTR("%s\n"), SCErrorString(SCError()));
599 do_prefs_get(--argc
, ++argv
);
602 CFStringRef prefs_val
;
604 key
= CFStringCreateWithCString(NULL
, *(++argv
), kCFStringEncodingUTF8
);
605 prefs_val
= CFDictionaryGetValue(value
, key
);
608 if (prefs_val
!= NULL
) {
609 SCPrint(TRUE
, stdout
, CFSTR("%@\n"), prefs_val
);
615 // if path or key not found
623 do_setPref(char *pref
, int argc
, char **argv
)
629 (*pref_keys
[i
].set
)(argc
, argv
);
635 /* -------------------- */
648 do_prefs_open(int argc
, char **argv
)
651 CFStringRef prefsID
= NULL
;
654 if (_prefs_commitRequired(argc
, argv
, "close")) {
658 do_prefs_close(0, NULL
);
662 prefsID
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
665 ok
= _prefs_open(CFSTR("scutil --prefs"), prefsID
);
666 if (prefsID
!= NULL
) CFRelease(prefsID
);
670 CFSTR("Could not open prefs: %s\n"),
671 SCErrorString(SCError()));
681 do_prefs_lock(int argc
, char **argv
)
684 Boolean wait
= (argc
> 0) ? TRUE
: FALSE
;
686 if (!SCPreferencesLock(prefs
, wait
)) {
687 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
697 do_prefs_unlock(int argc
, char **argv
)
701 if (!SCPreferencesUnlock(prefs
)) {
702 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
712 do_prefs_commit(int argc
, char **argv
)
716 if (!SCPreferencesCommitChanges(prefs
)) {
717 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
721 _prefs_changed
= FALSE
;
728 do_prefs_apply(int argc
, char **argv
)
732 if (!SCPreferencesApplyChanges(prefs
)) {
733 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
742 do_prefs_close(int argc
, char **argv
)
744 if (_prefs_commitRequired(argc
, argv
, "close")) {
755 do_prefs_quit(int argc
, char **argv
)
757 if (_prefs_commitRequired(argc
, argv
, "quit")) {
763 termRequested
= TRUE
;
770 do_prefs_synchronize(int argc
, char **argv
)
774 SCPreferencesSynchronize(prefs
);
779 static CFComparisonResult
780 sort_paths(const void *p1
, const void *p2
, void *context
)
782 #pragma unused(context)
783 CFStringRef path1
= (CFStringRef
)p1
;
784 CFStringRef path2
= (CFStringRef
)p2
;
785 return CFStringCompare(path1
, path2
, 0);
791 do_prefs_list(int argc
, char **argv
)
795 CFMutableArrayRef paths
= NULL
;
797 CFDictionaryRef entity
;
799 prefix
= CFStringCreateWithCString(NULL
,
800 (argc
>= 1) ? argv
[0] : "/",
801 kCFStringEncodingUTF8
);
803 entity
= SCPreferencesPathGetValue(prefs
, prefix
);
804 if (entity
== NULL
) {
805 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
809 paths
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
811 n
= isA_CFDictionary(entity
) ? CFDictionaryGetCount(entity
) : 0;
817 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
818 vals
= CFAllocatorAllocate(NULL
, n
* sizeof(CFPropertyListRef
), 0);
819 CFDictionaryGetKeysAndValues(entity
, keys
, vals
);
820 for (i
= 0; i
< n
; i
++) {
821 if (isA_CFDictionary(vals
[i
])) {
822 CFArrayAppendValue(paths
, keys
[i
]);
825 CFAllocatorDeallocate(NULL
, keys
);
826 CFAllocatorDeallocate(NULL
, vals
);
829 n
= CFArrayGetCount(paths
);
830 CFArraySortValues(paths
,
836 for (i
= 0; i
< n
; i
++) {
839 CFSTR(" path [%d] = %@/%@\n"),
841 CFEqual(prefix
, CFSTR("/")) ? CFSTR("") : prefix
,
842 CFArrayGetValueAtIndex(paths
, i
));
845 SCPrint(TRUE
, stdout
, CFSTR(" no paths.\n"));
859 do_prefs_get(int argc
, char **argv
)
862 CFDictionaryRef dict
;
865 CFMutableDictionaryRef newDict
;
868 path
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
870 link
= SCPreferencesPathGetLink(prefs
, path
);
872 SCPrint(TRUE
, stdout
, CFSTR(" --> %@\n"), link
);
875 dict
= SCPreferencesPathGetValue(prefs
, path
);
878 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
882 newDict
= CFDictionaryCreateMutable(NULL
,
884 &kCFTypeDictionaryKeyCallBacks
,
885 &kCFTypeDictionaryValueCallBacks
);
887 // remove [path] children
888 n
= isA_CFDictionary(dict
) ? CFDictionaryGetCount(dict
) : 0;
894 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
895 vals
= CFAllocatorAllocate(NULL
, n
* sizeof(CFPropertyListRef
), 0);
896 CFDictionaryGetKeysAndValues(dict
, keys
, vals
);
897 for (i
= 0; i
< n
; i
++) {
898 if (!isA_CFDictionary(vals
[i
])) {
899 CFDictionaryAddValue(newDict
, keys
[i
], vals
[i
]);
902 CFAllocatorDeallocate(NULL
, keys
);
903 CFAllocatorDeallocate(NULL
, vals
);
907 CFRelease(value
); /* we have new information, release the old */
918 do_prefs_set(int argc
, char **argv
)
920 CFDictionaryRef dict
;
921 CFMutableDictionaryRef newDict
= NULL
;
924 path
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
925 newDict
= CFDictionaryCreateMutableCopy(NULL
, 0, value
);
927 dict
= SCPreferencesPathGetValue(prefs
, path
);
931 // retain [path] children
932 n
= CFDictionaryGetCount(dict
);
938 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFStringRef
), 0);
939 vals
= CFAllocatorAllocate(NULL
, n
* sizeof(CFPropertyListRef
), 0);
940 CFDictionaryGetKeysAndValues(dict
, keys
, vals
);
941 for (i
= 0; i
< n
; i
++) {
942 if (isA_CFDictionary(vals
[i
])) {
943 if (CFDictionaryContainsKey(newDict
, keys
[i
])) {
944 SCPrint(TRUE
, stdout
, CFSTR(" key %@ is already a path component and cannot be replaced\n"), keys
[i
]);
947 CFDictionaryAddValue(newDict
, keys
[i
], vals
[i
]);
950 CFAllocatorDeallocate(NULL
, keys
);
951 CFAllocatorDeallocate(NULL
, vals
);
953 } else if (SCError() == kSCStatusInvalidArgument
) {
954 SCPrint(TRUE
, stdout
, CFSTR(" a path component is not a dictionary\n"));
956 } else if (SCError() != kSCStatusNoKey
) {
957 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
966 link
= CFStringCreateWithCString(NULL
, argv
[1], kCFStringEncodingUTF8
);
967 ok
= SCPreferencesPathSetLink(prefs
, path
, link
);
970 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
975 if (!SCPreferencesPathSetValue(prefs
, path
, newDict
)) {
976 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
981 _prefs_changed
= TRUE
;
993 do_prefs_remove(int argc
, char **argv
)
998 path
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
1000 if (!SCPreferencesPathRemoveValue(prefs
, path
)) {
1001 SCPrint(TRUE
, stdout
, CFSTR(" %s\n"), SCErrorString(SCError()));
1005 _prefs_changed
= TRUE
;
1014 on_off_str(Boolean on
)
1016 return (on
? "on" : "off");
1019 /* -------------------- */
1021 #if !TARGET_OS_IPHONE
1023 #include "InterfaceNamerControlPrefs.h"
1026 allow_new_interfaces_usage(void)
1028 fprintf(stderr
, "usage: scutil --allow-new-interfaces [on|off|default]\n");
1034 do_ifnamer(char * pref
, int argc
, char **argv
)
1036 Boolean allow
= FALSE
;
1039 allow_new_interfaces_usage();
1043 if (strcmp(pref
, "allow-new-interfaces")) {
1048 SCPrint(TRUE
, stdout
, CFSTR("AllowNewInterfaces is %s\n"),
1049 on_off_str(InterfaceNamerControlPrefsAllowNewInterfaces()));
1053 if ((strcasecmp(argv
[0], "disable") == 0) ||
1054 (strcasecmp(argv
[0], "no" ) == 0) ||
1055 (strcasecmp(argv
[0], "off" ) == 0) ||
1056 (strcasecmp(argv
[0], "0" ) == 0)) {
1058 } else if ((strcasecmp(argv
[0], "enable") == 0) ||
1059 (strcasecmp(argv
[0], "yes" ) == 0) ||
1060 (strcasecmp(argv
[0], "on" ) == 0) ||
1061 (strcasecmp(argv
[0], "1" ) == 0)) {
1063 } else if (strcasecmp(argv
[0], "default") == 0) {
1066 allow_new_interfaces_usage();
1070 if (!InterfaceNamerControlPrefsSetAllowNewInterfaces(allow
)) {
1071 SCPrint(TRUE
, stderr
, CFSTR("failed to set preferences\n"));
1079 #endif // !TARGET_OS_IPHONE
1081 /* -------------------- */
1083 #include "IPMonitorControlPrefs.h"
1087 do_log(char * log
, int argc
, char **argv
)
1089 Boolean verbose
= FALSE
;
1091 if (strcmp(log
, "IPMonitor")) {
1096 SCPrint(TRUE
, stdout
, CFSTR("IPMonitor log is %s\n"),
1097 on_off_str(IPMonitorControlPrefsIsVerbose()));
1101 if ((strcasecmp(argv
[0], "disable") == 0) ||
1102 (strcasecmp(argv
[0], "no" ) == 0) ||
1103 (strcasecmp(argv
[0], "off" ) == 0) ||
1104 (strcasecmp(argv
[0], "0" ) == 0)) {
1106 } else if ((strcasecmp(argv
[0], "enable") == 0) ||
1107 (strcasecmp(argv
[0], "yes" ) == 0) ||
1108 (strcasecmp(argv
[0], "on" ) == 0) ||
1109 (strcasecmp(argv
[0], "1" ) == 0)) {
1112 SCPrint(TRUE
, stdout
, CFSTR("invalid value, must be 'on' or 'off'\n"));
1116 if (!IPMonitorControlPrefsSetVerbose(verbose
)) {
1117 SCPrint(TRUE
, stderr
, CFSTR("failed to set preferences\n"));
1126 /* -------------------- */
1128 static SCNetworkInterfaceRef
1129 copy_configured_interface(SCPreferencesRef prefs
, CFStringRef if_name
)
1131 SCNetworkSetRef current_set
= NULL
;
1134 SCNetworkInterfaceRef ret_if
= NULL
;
1135 CFArrayRef services
= NULL
;
1137 if (prefs
== NULL
) {
1140 current_set
= SCNetworkSetCopyCurrent(prefs
);
1141 if (current_set
== NULL
) {
1144 services
= SCNetworkSetCopyServices(current_set
);
1145 if (services
== NULL
) {
1149 count
= CFArrayGetCount(services
);
1150 for (i
= 0; i
< count
; i
++) {
1151 CFStringRef this_if_name
;
1152 SCNetworkInterfaceRef this_if
;
1153 SCNetworkServiceRef s
;
1155 s
= (SCNetworkServiceRef
)CFArrayGetValueAtIndex(services
, i
);
1156 if (!SCNetworkServiceGetEnabled(s
)) {
1157 /* skip disabled services */
1160 this_if
= SCNetworkServiceGetInterface(s
);
1161 if (this_if
== NULL
) {
1164 this_if_name
= SCNetworkInterfaceGetBSDName(this_if
);
1165 if (this_if_name
== NULL
) {
1168 if (CFEqual(this_if_name
, if_name
)) {
1176 if (current_set
!= NULL
) {
1177 CFRelease(current_set
);
1179 if (services
!= NULL
) {
1180 CFRelease(services
);
1186 disable_until_needed_usage(void)
1188 fprintf(stderr
, "usage: scutil --disable-until-needed <interfaceName> [on|off|default]\n");
1192 #include <SystemConfiguration/SCNetworkConfigurationPrivate.h>
1196 do_disable_until_needed(int argc
, char **argv
)
1198 const char * if_name
;
1199 CFStringRef if_name_cf
;
1200 SCNetworkInterfaceRef net_if
;
1202 const char * on_off
= "?";
1204 Boolean set_default
= FALSE
;
1205 Boolean set_value
= FALSE
;
1207 if (argc
< 1 || argc
> 2) {
1208 disable_until_needed_usage();
1214 if (strcasecmp(on_off
, "on") == 0) {
1216 } else if (strcasecmp(on_off
, "off") == 0) {
1218 } else if ((strcmp(on_off
, "") == 0) || (strcasecmp(on_off
, "default") == 0)) {
1222 disable_until_needed_usage();
1227 ok
= _prefs_open(CFSTR("scutil --disable-until-needed"), NULL
);
1231 CFSTR("Could not open prefs: %s\n"),
1232 SCErrorString(SCError()));
1235 if_name_cf
= CFStringCreateWithCStringNoCopy(NULL
,
1237 kCFStringEncodingASCII
,
1239 net_if
= copy_configured_interface(prefs
, if_name_cf
);
1240 if (net_if
== NULL
) {
1241 fprintf(stderr
, "%s is not configured\n", if_name
);
1246 ok
= SCNetworkInterfaceSetDisableUntilNeeded(net_if
, on
);
1248 ok
= __SCNetworkInterfaceSetDisableUntilNeededValue(net_if
, NULL
);
1251 fprintf(stderr
, "failed to turn disable-until-needed %s\n",
1257 on
= SCNetworkInterfaceGetDisableUntilNeeded(net_if
);
1258 printf("%s disable-until-needed is %s\n", if_name
, on_off_str(on
));