2 * Copyright (c) 2004-2007, 2009-2011, 2014, 2016, 2017, 2019, 2020 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 * August 5, 2004 Allan Nathanson <ajn@apple.com>
36 #include "net_interface.h"
37 #include "net_protocol.h"
38 #include "net_service.h"
41 #include "SCNetworkConfigurationInternal.h"
42 #include "SCPreferencesInternal.h"
47 __private_extern__ CFMutableArrayRef new_interfaces
= NULL
;
49 __private_extern__ CFArrayRef interfaces
= NULL
;
50 __private_extern__ CFArrayRef services
= NULL
;
51 __private_extern__ CFArrayRef protocols
= NULL
;
52 __private_extern__ CFArrayRef sets
= NULL
;
54 __private_extern__ SCNetworkInterfaceRef net_interface
= NULL
;
55 __private_extern__ SCNetworkServiceRef net_service
= NULL
;
56 __private_extern__ SCNetworkProtocolRef net_protocol
= NULL
;
57 __private_extern__ SCNetworkSetRef net_set
= NULL
;
59 __private_extern__ CFNumberRef CFNumberRef_0
= NULL
;
60 __private_extern__ CFNumberRef CFNumberRef_1
= NULL
;
63 /* -------------------- */
67 CF_RETURNS_RETAINED CFNumberRef
68 _copy_number(const char *arg
)
72 if (sscanf(arg
, "%d", &val
) != 1) {
76 return CFNumberCreate(NULL
, kCFNumberIntType
, &val
);
80 /* -------------------- */
85 _find_option(const char *option
, optionsRef options
, const int nOptions
)
89 for (i
= 0; i
< nOptions
; i
++) {
90 if (strcasecmp(option
, options
[i
].option
) == 0) {
101 _find_selection(CFStringRef choice
, selections choices
[], unsigned int *flags
)
106 while (choices
[i
].selection
!= NULL
) {
107 if (CFStringCompare(choice
,
108 choices
[i
].selection
,
109 kCFCompareCaseInsensitive
) == kCFCompareEqualTo
) {
111 *flags
= choices
[i
].flags
;
124 _process_options(optionsRef options
, int nOptions
, int argc
, char **argv
, CFMutableDictionaryRef newConfiguration
)
127 CFIndex optionIndex
= kCFNotFound
;
129 optionIndex
= _find_option(argv
[0], options
, nOptions
);
130 if (optionIndex
== kCFNotFound
) {
131 SCPrint(TRUE
, stdout
, CFSTR("set what?\n"));
137 switch (options
[optionIndex
].type
) {
139 // all option processing is managed by the "handler"
142 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), options
[optionIndex
].info
);
146 selections
*choices
= (selections
*)options
[optionIndex
].info
;
151 SCPrint(TRUE
, stdout
,
152 CFSTR("%s not specified\n"),
153 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
157 choice
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
158 i
= _find_selection(choice
, choices
, &flags
);
161 if (i
!= kCFNotFound
) {
162 if (choices
[i
].flags
& selectionNotAvailable
) {
163 SCPrint(TRUE
, stdout
,
164 CFSTR("cannot select %s\n"),
165 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
169 if (choices
[i
].key
!= NULL
) {
170 CFDictionarySetValue(newConfiguration
,
171 *(options
[optionIndex
].key
),
174 CFDictionaryRemoveValue(newConfiguration
,
175 *(options
[optionIndex
].key
));
178 SCPrint(TRUE
, stdout
,
179 CFSTR("invalid %s\n"),
180 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
188 case isChooseMultiple
:
190 SCPrint(TRUE
, stdout
,
191 CFSTR("%s(s) not specified\n"),
192 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
196 if (strlen(argv
[0]) > 0) {
199 CFMutableArrayRef chosen
;
201 CFArrayRef str_array
;
203 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
204 str_array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
207 chosen
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
209 n
= CFArrayGetCount(str_array
);
210 for (i
= 0; i
< n
; i
++) {
212 selections
*choices
= (selections
*)options
[optionIndex
].info
;
216 choice
= CFArrayGetValueAtIndex(str_array
, i
);
217 j
= _find_selection(choice
, choices
, &flags
);
219 if (j
!= kCFNotFound
) {
220 if (choices
[j
].flags
& selectionNotAvailable
) {
221 SCPrint(TRUE
, stdout
,
222 CFSTR("cannot select %s\n"),
223 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
224 CFArrayRemoveAllValues(chosen
);
228 CFArrayAppendValue(chosen
, *(choices
[j
].key
));
230 SCPrint(TRUE
, stdout
,
231 CFSTR("invalid %s\n"),
232 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "selection");
233 CFArrayRemoveAllValues(chosen
);
237 CFRelease(str_array
);
239 if (CFArrayGetCount(chosen
) > 0) {
240 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), chosen
);
242 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
246 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
254 SCPrint(TRUE
, stdout
,
255 CFSTR("%s not specified\n"),
256 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "enable/disable");
260 if ((strcasecmp(argv
[0], "disable") == 0) ||
261 (strcasecmp(argv
[0], "no" ) == 0) ||
262 (strcasecmp(argv
[0], "off" ) == 0) ||
263 (strcasecmp(argv
[0], "0" ) == 0)) {
264 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), kCFBooleanFalse
);
265 } else if ((strcasecmp(argv
[0], "enable") == 0) ||
266 (strcasecmp(argv
[0], "yes" ) == 0) ||
267 (strcasecmp(argv
[0], "on" ) == 0) ||
268 (strcasecmp(argv
[0], "1" ) == 0)) {
269 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), kCFBooleanTrue
);
270 } else if (strcmp(argv
[0], "") == 0) {
271 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
273 SCPrint(TRUE
, stdout
, CFSTR("invalid value\n"));
282 SCPrint(TRUE
, stdout
,
283 CFSTR("%s not specified\n"),
284 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "enable/disable");
288 if ((strcasecmp(argv
[0], "disable") == 0) ||
289 (strcasecmp(argv
[0], "no" ) == 0) ||
290 (strcasecmp(argv
[0], "off" ) == 0) ||
291 (strcasecmp(argv
[0], "0" ) == 0)) {
292 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), CFNumberRef_0
);
293 } else if ((strcasecmp(argv
[0], "enable") == 0) ||
294 (strcasecmp(argv
[0], "yes" ) == 0) ||
295 (strcasecmp(argv
[0], "on" ) == 0) ||
296 (strcasecmp(argv
[0], "1" ) == 0)) {
297 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), CFNumberRef_1
);
298 } else if (strcmp(argv
[0], "") == 0) {
299 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
301 SCPrint(TRUE
, stdout
, CFSTR("invalid value\n"));
310 SCPrint(TRUE
, stdout
,
311 CFSTR("%s not specified\n"),
312 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "value");
316 if (strlen(argv
[0]) > 0) {
319 num
= _copy_number(argv
[0]);
321 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), num
);
324 SCPrint(TRUE
, stdout
, CFSTR("invalid value\n"));
328 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
336 SCPrint(TRUE
, stdout
,
337 CFSTR("%s not specified\n"),
338 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "value");
342 if (strlen(argv
[0]) > 0) {
345 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
346 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), str
);
349 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
357 SCPrint(TRUE
, stdout
,
358 CFSTR("%s(s) not specified\n"),
359 options
[optionIndex
].description
!= NULL
? options
[optionIndex
].description
: "value");
363 if (strlen(argv
[0]) > 0) {
365 CFArrayRef str_array
;
367 str
= CFStringCreateWithCString(NULL
, argv
[0], kCFStringEncodingUTF8
);
368 str_array
= CFStringCreateArrayBySeparatingStrings(NULL
, str
, CFSTR(","));
371 CFDictionarySetValue(newConfiguration
, *(options
[optionIndex
].key
), str_array
);
372 CFRelease(str_array
);
374 CFDictionaryRemoveValue(newConfiguration
, *(options
[optionIndex
].key
));
382 if (options
[optionIndex
].handler
!= NULL
) {
386 key
= options
[optionIndex
].key
!= NULL
? *(options
[optionIndex
].key
) : NULL
;
387 nArgs
= (*options
[optionIndex
].handler
)(key
,
388 options
[optionIndex
].description
,
389 options
[optionIndex
].info
,
406 /* -------------------- */
413 _show_entity(CFDictionaryRef entity
, CFStringRef prefix
)
416 const void * keys_q
[N_QUICK
];
417 const void ** keys
= keys_q
;
420 CFMutableArrayRef sorted
;
422 n
= CFDictionaryGetCount(entity
);
423 if (n
> (CFIndex
)(sizeof(keys_q
) / sizeof(CFTypeRef
))) {
424 keys
= CFAllocatorAllocate(NULL
, n
* sizeof(CFTypeRef
), 0);
426 CFDictionaryGetKeysAndValues(entity
, keys
, NULL
);
428 array
= CFArrayCreate(NULL
, keys
, n
, &kCFTypeArrayCallBacks
);
429 sorted
= CFArrayCreateMutableCopy(NULL
, n
, array
);
431 CFArraySortValues(sorted
,
433 (CFComparatorFunction
)CFStringCompare
,
437 for (i
= 0; i
< n
; i
++) {
441 key
= CFArrayGetValueAtIndex(sorted
, i
);
442 value
= CFDictionaryGetValue(entity
, key
);
443 if (isA_CFArray(value
)) {
445 CFIndex n
= CFArrayGetCount(value
);
447 SCPrint(TRUE
, stdout
, CFSTR("%@ %@ = ("), prefix
, key
);
448 for (i
= 0; i
< n
; i
++) {
451 val
= CFArrayGetValueAtIndex(value
, i
);
452 SCPrint(TRUE
, stdout
,
457 SCPrint(TRUE
, stdout
, CFSTR(")\n"));
459 SCPrint(TRUE
, stdout
, CFSTR("%@ %@ = %@\n"), prefix
, key
, value
);
465 if (keys
!= keys_q
) {
466 CFAllocatorDeallocate(NULL
, keys
);
473 /* -------------------- */
479 if (net_interface
!= NULL
) {
480 CFRelease(net_interface
);
481 net_interface
= NULL
;
484 if (net_service
!= NULL
) {
485 CFRelease(net_service
);
489 if (net_protocol
!= NULL
) {
490 CFRelease(net_protocol
);
494 if (net_set
!= NULL
) {
499 if (interfaces
!= NULL
) {
500 CFRelease(interfaces
);
504 if (services
!= NULL
) {
509 if (protocols
!= NULL
) {
510 CFRelease(protocols
);
519 if (new_interfaces
!= NULL
) {
520 CFRelease(new_interfaces
);
521 new_interfaces
= NULL
;
535 CFNumberRef_0
= CFNumberCreate(NULL
, kCFNumberIntType
, &zero
);
536 CFNumberRef_1
= CFNumberCreate(NULL
, kCFNumberIntType
, &one
);
544 do_net_open(int argc
, char **argv
)
549 if (_prefs_commitRequired(argc
, argv
, "close")) {
557 ok
= _prefs_open(CFSTR("scutil --net"), (argc
> 0) ? argv
[0] : NULL
);
561 CFSTR("Could not open prefs: %s\n"),
562 SCErrorString(SCError()));
566 net_set
= SCNetworkSetCopyCurrent(prefs
);
567 if (net_set
!= NULL
) {
570 setName
= SCNetworkSetGetName(net_set
);
571 if (setName
!= NULL
) {
572 SCPrint(TRUE
, stdout
, CFSTR("set \"%@\" selected\n"), setName
);
574 SCPrint(TRUE
, stdout
,
575 CFSTR("set ID \"%@\" selected\n"),
576 SCNetworkSetGetSetID(net_set
));
586 do_net_commit(int argc
, char **argv
)
590 if (!SCPreferencesCommitChanges(prefs
)) {
591 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
595 _prefs_changed
= FALSE
;
602 do_net_apply(int argc
, char **argv
)
606 if (!SCPreferencesApplyChanges(prefs
)) {
607 SCPrint(TRUE
, stdout
, CFSTR("%s\n"), SCErrorString(SCError()));
615 do_net_close(int argc
, char **argv
)
617 if (_prefs_commitRequired(argc
, argv
, "close")) {
630 do_net_quit(int argc
, char **argv
)
632 if (_prefs_commitRequired(argc
, argv
, "quit")) {
639 termRequested
= TRUE
;
644 /* -------------------- */
647 typedef void (*net_func
) (int argc
, char **argv
);
649 static const struct {
660 { "interfaces", NULL
, NULL
, NULL
,
661 NULL
, NULL
, show_interfaces
,
664 { "interface", create_interface
, NULL
, NULL
,
665 select_interface
, set_interface
, show_interface
,
668 { "services", NULL
, NULL
, NULL
,
669 NULL
, NULL
, show_services
,
672 { "service", create_service
, disable_service
, enable_service
,
673 select_service
, set_service
, show_service
,
676 { "protocols", NULL
, NULL
, NULL
,
677 NULL
, NULL
, show_protocols
,
680 { "protocol", create_protocol
, disable_protocol
, enable_protocol
,
681 select_protocol
, set_protocol
, show_protocol
,
684 { "sets", NULL
, NULL
, NULL
,
685 NULL
, NULL
, show_sets
,
688 { "set", create_set
, NULL
, NULL
,
689 select_set
, set_set
, show_set
,
693 #define N_NET_KEYS (sizeof(net_keys) / sizeof(net_keys[0]))
697 findNetKey(char *key
)
701 for (i
= 0; i
< (int)N_NET_KEYS
; i
++) {
702 if (strcmp(key
, net_keys
[i
].key
) == 0) {
711 /* -------------------- */
716 do_net_create(int argc
, char **argv
)
727 SCPrint(TRUE
, stderr
, CFSTR("create what?\n"));
731 if (net_keys
[i
].create
== NULL
) {
732 SCPrint(TRUE
, stderr
, CFSTR("create what?\n"));
736 (*net_keys
[i
].create
)(argc
, argv
);
743 do_net_disable(int argc
, char **argv
)
754 SCPrint(TRUE
, stderr
, CFSTR("disable what?\n"));
758 if (net_keys
[i
].disable
== NULL
) {
759 SCPrint(TRUE
, stderr
, CFSTR("disable what?\n"));
763 (*net_keys
[i
].disable
)(argc
, argv
);
770 do_net_enable(int argc
, char **argv
)
781 SCPrint(TRUE
, stderr
, CFSTR("enable what?\n"));
785 if (net_keys
[i
].enable
== NULL
) {
786 SCPrint(TRUE
, stderr
, CFSTR("enable what?\n"));
790 (*net_keys
[i
].enable
)(argc
, argv
);
796 do_net_migrate_perform(int argc
, char **argv
)
798 char * sourceConfiguration
= NULL
;
799 char * targetConfiguration
= NULL
;
800 char * currentConfiguration
= NULL
;
801 CFStringRef str
= NULL
;
802 CFURLRef sourceConfigurationURL
= NULL
;
803 CFURLRef targetConfigurationURL
= NULL
;
804 CFURLRef currentConfigurationURL
= NULL
;
805 CFArrayRef migrationFiles
= NULL
;
807 sourceConfiguration
= argv
[0];
808 targetConfiguration
= argv
[1];
811 currentConfiguration
= argv
[2];
814 SCPrint(_sc_debug
, stdout
,
815 CFSTR("sourceConfiguration: %s\n"
816 "targetConfiguration: %s\n"
817 "currentConfiguration: %s\n"),
820 (currentConfiguration
!= NULL
) ? currentConfiguration
: "<current system>" );
822 str
= CFStringCreateWithCString(NULL
, sourceConfiguration
, kCFStringEncodingUTF8
);
823 sourceConfigurationURL
= CFURLCreateWithFileSystemPath(NULL
, str
, kCFURLPOSIXPathStyle
, TRUE
);
826 str
= CFStringCreateWithCString(NULL
, targetConfiguration
, kCFStringEncodingUTF8
);
827 targetConfigurationURL
= CFURLCreateWithFileSystemPath(NULL
, str
, kCFURLPOSIXPathStyle
, TRUE
);
830 if (currentConfiguration
!= NULL
) {
831 str
= CFStringCreateWithCString(NULL
, currentConfiguration
, kCFStringEncodingUTF8
);
832 currentConfigurationURL
= CFURLCreateWithFileSystemPath(NULL
, str
, kCFURLPOSIXPathStyle
, TRUE
);
836 migrationFiles
= _SCNetworkConfigurationPerformMigration(sourceConfigurationURL
, currentConfigurationURL
, targetConfigurationURL
, NULL
);
838 if (migrationFiles
!= NULL
) {
839 SCPrint(TRUE
, stdout
, CFSTR("Migration complete\n"));
840 SCPrint(_sc_debug
, stdout
, CFSTR("updated files: %@\n"), migrationFiles
);
842 SCPrint(TRUE
, stdout
, CFSTR("Migration failed\n"));
845 if (sourceConfigurationURL
!= NULL
) {
846 CFRelease(sourceConfigurationURL
);
848 if (targetConfigurationURL
!= NULL
) {
849 CFRelease(targetConfigurationURL
);
851 if (currentConfigurationURL
!= NULL
) {
852 CFRelease(currentConfigurationURL
);
854 if (migrationFiles
!= NULL
) {
855 CFRelease(migrationFiles
);
861 do_net_migrate_validate(int argc
, char **argv
)
864 char *configuration
= NULL
;
865 CFURLRef configurationURL
= NULL
;
866 char *expectedConfiguration
= NULL
;
867 CFURLRef expectedConfigurationURL
= NULL
;
868 Boolean isValid
= FALSE
;
869 CFStringRef str
= NULL
;
871 configuration
= argv
[0];
872 str
= CFStringCreateWithCString(NULL
, configuration
, kCFStringEncodingUTF8
);
873 configurationURL
= CFURLCreateWithFileSystemPath(NULL
, str
, kCFURLPOSIXPathStyle
, TRUE
);
876 expectedConfiguration
= argv
[1];
877 str
= CFStringCreateWithCString(NULL
, expectedConfiguration
, kCFStringEncodingUTF8
);
878 expectedConfigurationURL
= CFURLCreateWithFileSystemPath(NULL
, str
, kCFURLPOSIXPathStyle
, TRUE
);
881 isValid
= _SCNetworkMigrationAreConfigurationsIdentical(configurationURL
, expectedConfigurationURL
);
883 SCPrint(TRUE
, stdout
, CFSTR("Configuration at location %s %s\n"), configuration
, isValid
? "is valid" : "is NOT valid");
885 if (configurationURL
!= NULL
) {
886 CFRelease(configurationURL
);
888 if (expectedConfigurationURL
!= NULL
) {
889 CFRelease(expectedConfigurationURL
);
896 do_net_migrate(int argc
, char **argv
)
904 if (strcmp(key
, "perform") == 0) {
905 do_net_migrate_perform(argc
, argv
);
906 } else if (strcmp(key
, "validate") == 0) {
907 do_net_migrate_validate(argc
, argv
);
909 SCPrint(TRUE
, stderr
, CFSTR("migrate what?\n"));
918 do_net_remove(int argc
, char **argv
)
929 SCPrint(TRUE
, stderr
, CFSTR("remove what?\n"));
933 if (net_keys
[i
].remove
== NULL
) {
934 SCPrint(TRUE
, stderr
, CFSTR("remove what?\n"));
938 (*net_keys
[i
].remove
)(argc
, argv
);
945 do_net_select(int argc
, char **argv
)
956 SCPrint(TRUE
, stderr
, CFSTR("select what?\n"));
960 if (*net_keys
[i
].select
== NULL
) {
961 SCPrint(TRUE
, stderr
, CFSTR("select what?\n"));
965 (*net_keys
[i
].select
)(argc
, argv
);
972 do_net_set(int argc
, char **argv
)
983 SCPrint(TRUE
, stderr
, CFSTR("set what?\n"));
987 (*net_keys
[i
].set
)(argc
, argv
);
994 do_net_show(int argc
, char **argv
)
1003 i
= findNetKey(key
);
1005 SCPrint(TRUE
, stderr
, CFSTR("show what?\n"));
1009 (*net_keys
[i
].show
)(argc
, argv
);
1016 do_net_clean(int argc
, char **argv
)
1018 #pragma unused(argc)
1019 #pragma unused(argv)
1022 if (prefs
== NULL
) {
1023 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1027 if (ni_prefs
== NULL
) {
1028 ni_prefs
= SCPreferencesCreateCompanion(prefs
, INTERFACES_DEFAULT_CONFIG
);
1029 if (ni_prefs
== NULL
) {
1030 SC_log(LOG_NOTICE
, "SCPreferencesCreate( <NetworkInterfaces.plist> ) failed: %s", SCErrorString(SCError()));
1034 updated
= __SCNetworkConfigurationClean(prefs
, ni_prefs
);
1037 SCPrint(TRUE
, stdout
,
1038 CFSTR("network configuration updated, use \"commit\" to save\n"));
1039 if ((prefsPath
== NULL
) || (strcmp(prefsPath
, PREFS_DEFAULT_CONFIG_PLIST
) == 0)) {
1040 SCPrint(TRUE
, stdout
,
1042 "NOTE: because you have modified the system's \"live\" network configuration,\n"
1043 " a <reboot> is also REQUIRED.\n"
1046 _prefs_changed
= TRUE
;
1048 SCPrint(TRUE
, stdout
, CFSTR("network configuration not updated\n"));
1057 do_net_update(int argc
, char **argv
)
1059 #pragma unused(argc)
1060 #pragma unused(argv)
1061 SCNetworkSetRef set
;
1062 Boolean setCreated
= FALSE
;
1063 Boolean setUpdated
= FALSE
;
1065 if (prefs
== NULL
) {
1066 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1070 if (net_set
!= NULL
) {
1071 set
= CFRetain(net_set
);
1073 set
= SCNetworkSetCopyCurrent(prefs
);
1075 // if no "current" set, create a new/default ("Automatic") set
1076 set
= _SCNetworkSetCreateDefault(prefs
);
1078 SCPrint(TRUE
, stdout
,
1079 CFSTR("could not initialize \"Automatic\" set: %s\n"),
1080 SCErrorString(SCError()));
1084 if (net_set
!= NULL
) CFRelease(net_set
);
1097 setUpdated
= SCNetworkSetEstablishDefaultConfiguration(set
);
1099 CFStringRef setName
;
1101 _prefs_changed
= TRUE
;
1103 setName
= SCNetworkSetGetName(set
);
1104 if (setName
!= NULL
) {
1105 SCPrint(TRUE
, stdout
,
1106 CFSTR("set \"%@\" (%@) %supdated\n"),
1108 SCNetworkSetGetSetID(set
),
1109 setCreated
? "created, selected, and " : "");
1111 SCPrint(TRUE
, stdout
,
1112 CFSTR("set ID \"%@\" %supdated\n"),
1113 SCNetworkSetGetSetID(set
),
1114 setCreated
? "created, selected, and " : "");
1116 _prefs_changed
= TRUE
;
1126 do_net_upgrade(int argc
, char **argv
)
1128 Boolean do_commit
= FALSE
;
1131 if (prefs
== NULL
) {
1132 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1136 if (prefsPath
!= NULL
) {
1137 const char *prefs_plist
;
1139 prefs_plist
= strrchr(prefsPath
, '/');
1140 if (prefs_plist
!= NULL
) {
1143 prefs_plist
= prefsPath
;
1146 if (strcmp(prefs_plist
, PREFS_DEFAULT_CONFIG_PLIST
) != 0) {
1147 SCPrint(TRUE
, stdout
, CFSTR("not updating a \"preferences.plist\" file\n"));
1153 if (strcmp(argv
[0], PREFS_DEFAULT_CONFIG_PLIST
) == 0) {
1154 upgraded
= __SCNetworkConfigurationUpgrade(&prefs
, NULL
, do_commit
);
1155 } else if (strcmp(argv
[0], INTERFACES_DEFAULT_CONFIG_PLIST
) == 0) {
1156 if (ni_prefs
== NULL
) {
1157 ni_prefs
= SCPreferencesCreateCompanion(prefs
, INTERFACES_DEFAULT_CONFIG
);
1158 if (ni_prefs
== NULL
) {
1159 SC_log(LOG_NOTICE
, "SCPreferencesCreate( <NetworkInterfaces.plist> ) failed: %s", SCErrorString(SCError()));
1163 do_commit
= TRUE
; // using alternate, created on-the-fly preferences.plist
1165 upgraded
= __SCNetworkConfigurationUpgrade(NULL
, &ni_prefs
, do_commit
);
1167 SCPrint(TRUE
, stdout
, CFSTR("invalid .plist (\"preferences.plist\", \"NetworkInterfaces.plist\"\n"));
1171 upgraded
= __SCNetworkConfigurationUpgrade(&prefs
, &ni_prefs
, do_commit
);
1175 SCPrint(TRUE
, stdout
,
1176 do_commit
? CFSTR("network configuration upgraded/saved\n")
1177 : CFSTR("network configuration upgraded, use \"commit\" to save\n"));
1178 if ((prefsPath
== NULL
) || (strcmp(prefsPath
, PREFS_DEFAULT_CONFIG_PLIST
) == 0)) {
1179 SCPrint(TRUE
, stdout
,
1180 do_commit
? CFSTR("\n"
1181 "NOTE: because you have modified the system's \"live\" network configuration,\n"
1182 " a <reboot> is also REQUIRED.\n"
1186 "NOTE: because you are modifying the system's \"live\" network configuration,\n"
1187 " a <reboot> will also be REQUIRED.\n"
1193 _prefs_changed
= TRUE
;
1196 SCPrint(TRUE
, stdout
, CFSTR("network configuration upgrade not needed\n"));
1203 #include "SCPreferencesInternal.h"
1208 do_net_snapshot(int argc
, char **argv
)
1210 #pragma unused(argc)
1211 #pragma unused(argv)
1212 if (prefs
== NULL
) {
1213 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1217 if (prefs
!= NULL
) {
1218 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
1220 if (prefsPrivate
->prefs
!= NULL
) {
1222 static int n_snapshot
= 0;
1226 asprintf(&path
, "/tmp/prefs_snapshot_%d", n_snapshot
++);
1228 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, 0644);
1231 SCPrint(TRUE
, stdout
, CFSTR("could not write snapshot: open() failed : %s\n"), strerror(errno
));
1235 xmlData
= CFPropertyListCreateData(NULL
, prefsPrivate
->prefs
, kCFPropertyListXMLFormat_v1_0
, 0, NULL
);
1236 if (xmlData
!= NULL
) {
1237 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
1240 SCPrint(TRUE
, stdout
, CFSTR("could not write snapshot: CFPropertyListCreateData() failed\n"));
1245 SCPrint(TRUE
, stdout
, CFSTR("prefs have not been accessed\n"));
1254 do_configuration(int argc
, char **argv
)
1256 const char *description
= NULL
;
1257 SCPreferencesRef ni_prefs
;
1258 CFStringRef path
= NULL
;
1259 SCPreferencesRef prefs
;
1262 // scutil --configuration
1263 // scutil --configuration <path-to-preferences.plist>
1264 // scutil --configuraiton <description> <path-to-preferences.plist>
1268 description
= argv
[0];
1270 path
= CFStringCreateWithCString(NULL
,
1271 argc
> 1 ? argv
[1] : argv
[0],
1272 kCFStringEncodingUTF8
);
1274 prefs
= SCPreferencesCreate(NULL
, CFSTR("scutil --configuration"), path
);
1275 ni_prefs
= SCPreferencesCreateCompanion(prefs
, INTERFACES_DEFAULT_CONFIG
);
1277 _sc_log_save
= _sc_log
;
1278 _sc_log
= kSCLogDestinationFile
;
1279 __SCNetworkConfigurationReport(LOG_NOTICE
, description
, prefs
, ni_prefs
);
1280 _sc_log
= _sc_log_save
;
1282 if (path
!= NULL
) CFRelease(path
);
1284 CFRelease(ni_prefs
);