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
, CFSTR("sourceConfiguration: %s\ntargetConfiguration: %s\ncurrentConfiguration: %s\n"),
815 sourceConfiguration
, targetConfiguration
, (currentConfiguration
!= NULL
) ? currentConfiguration
: "<current system>" );
817 str
= CFStringCreateWithCString(NULL
, sourceConfiguration
, kCFStringEncodingUTF8
);
818 sourceConfigurationURL
= CFURLCreateWithFileSystemPath(NULL
, str
, kCFURLPOSIXPathStyle
, TRUE
);
821 str
= CFStringCreateWithCString(NULL
, targetConfiguration
, kCFStringEncodingUTF8
);
822 targetConfigurationURL
= CFURLCreateWithFileSystemPath(NULL
, str
, kCFURLPOSIXPathStyle
, TRUE
);
825 if (currentConfiguration
!= NULL
) {
826 str
= CFStringCreateWithCString(NULL
, currentConfiguration
, kCFStringEncodingUTF8
);
827 currentConfigurationURL
= CFURLCreateWithFileSystemPath(NULL
, str
, kCFURLPOSIXPathStyle
, TRUE
);
831 migrationFiles
= _SCNetworkConfigurationPerformMigration(sourceConfigurationURL
, currentConfigurationURL
, targetConfigurationURL
, NULL
);
833 if (migrationFiles
!= NULL
) {
834 SCPrint(TRUE
, stdout
, CFSTR("Migration Successful: %@ \n"), migrationFiles
);
837 SCPrint(TRUE
, stdout
, CFSTR("Migration Unsuccessful \n"));
840 if (sourceConfigurationURL
!= NULL
) {
841 CFRelease(sourceConfigurationURL
);
843 if (targetConfigurationURL
!= NULL
) {
844 CFRelease(targetConfigurationURL
);
846 if (currentConfigurationURL
!= NULL
) {
847 CFRelease(currentConfigurationURL
);
849 if (migrationFiles
!= NULL
) {
850 CFRelease(migrationFiles
);
856 do_net_migrate_validate(int argc
, char **argv
)
859 char *configuration
= NULL
;
860 CFURLRef configurationURL
= NULL
;
861 char *expectedConfiguration
= NULL
;
862 CFURLRef expectedConfigurationURL
= NULL
;
863 Boolean isValid
= FALSE
;
864 CFStringRef str
= NULL
;
866 configuration
= argv
[0];
867 str
= CFStringCreateWithCString(NULL
, configuration
, kCFStringEncodingUTF8
);
868 configurationURL
= CFURLCreateWithFileSystemPath(NULL
, str
, kCFURLPOSIXPathStyle
, TRUE
);
871 expectedConfiguration
= argv
[1];
872 str
= CFStringCreateWithCString(NULL
, expectedConfiguration
, kCFStringEncodingUTF8
);
873 expectedConfigurationURL
= CFURLCreateWithFileSystemPath(NULL
, str
, kCFURLPOSIXPathStyle
, TRUE
);
876 isValid
= _SCNetworkMigrationAreConfigurationsIdentical(configurationURL
, expectedConfigurationURL
);
878 SCPrint(TRUE
, stdout
, CFSTR("Configuration at location %s %s\n"), configuration
, isValid
? "is valid" : "is NOT valid");
880 if (configurationURL
!= NULL
) {
881 CFRelease(configurationURL
);
883 if (expectedConfigurationURL
!= NULL
) {
884 CFRelease(expectedConfigurationURL
);
891 do_net_migrate(int argc
, char **argv
)
894 SCPrint(TRUE
, stdout
, CFSTR("do_net_migrate called, %d\n"), argc
);
900 if (strcmp(key
, "perform") == 0) {
901 do_net_migrate_perform(argc
, argv
);
902 } else if (strcmp(key
, "validate") == 0) {
903 do_net_migrate_validate(argc
, argv
);
905 SCPrint(TRUE
, stderr
, CFSTR("migrate what?\n"));
914 do_net_remove(int argc
, char **argv
)
925 SCPrint(TRUE
, stderr
, CFSTR("remove what?\n"));
929 if (net_keys
[i
].remove
== NULL
) {
930 SCPrint(TRUE
, stderr
, CFSTR("remove what?\n"));
934 (*net_keys
[i
].remove
)(argc
, argv
);
941 do_net_select(int argc
, char **argv
)
952 SCPrint(TRUE
, stderr
, CFSTR("select what?\n"));
956 if (*net_keys
[i
].select
== NULL
) {
957 SCPrint(TRUE
, stderr
, CFSTR("select what?\n"));
961 (*net_keys
[i
].select
)(argc
, argv
);
968 do_net_set(int argc
, char **argv
)
979 SCPrint(TRUE
, stderr
, CFSTR("set what?\n"));
983 (*net_keys
[i
].set
)(argc
, argv
);
990 do_net_show(int argc
, char **argv
)
1001 SCPrint(TRUE
, stderr
, CFSTR("show what?\n"));
1005 (*net_keys
[i
].show
)(argc
, argv
);
1012 do_net_clean(int argc
, char **argv
)
1014 #pragma unused(argc)
1015 #pragma unused(argv)
1018 if (prefs
== NULL
) {
1019 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1023 if (ni_prefs
== NULL
) {
1024 ni_prefs
= SCPreferencesCreateCompanion(prefs
, INTERFACES_DEFAULT_CONFIG
);
1025 if (ni_prefs
== NULL
) {
1026 SC_log(LOG_NOTICE
, "SCPreferencesCreate( <NetworkInterfaces.plist> ) failed: %s", SCErrorString(SCError()));
1030 updated
= __SCNetworkConfigurationClean(prefs
, ni_prefs
);
1033 SCPrint(TRUE
, stdout
,
1034 CFSTR("network configuration updated, use \"commit\" to save\n"));
1035 if ((prefsPath
== NULL
) || (strcmp(prefsPath
, PREFS_DEFAULT_CONFIG_PLIST
) == 0)) {
1036 SCPrint(TRUE
, stdout
,
1038 "NOTE: because you have modified the system's \"live\" network configuration,\n"
1039 " a <reboot> is also REQUIRED.\n"
1042 _prefs_changed
= TRUE
;
1044 SCPrint(TRUE
, stdout
, CFSTR("network configuration not updated\n"));
1053 do_net_update(int argc
, char **argv
)
1055 #pragma unused(argc)
1056 #pragma unused(argv)
1057 SCNetworkSetRef set
;
1058 Boolean setCreated
= FALSE
;
1059 Boolean setUpdated
= FALSE
;
1061 if (prefs
== NULL
) {
1062 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1066 if (net_set
!= NULL
) {
1067 set
= CFRetain(net_set
);
1069 set
= SCNetworkSetCopyCurrent(prefs
);
1071 // if no "current" set, create a new/default ("Automatic") set
1072 set
= _SCNetworkSetCreateDefault(prefs
);
1074 SCPrint(TRUE
, stdout
,
1075 CFSTR("could not initialize \"Automatic\" set: %s\n"),
1076 SCErrorString(SCError()));
1080 if (net_set
!= NULL
) CFRelease(net_set
);
1093 setUpdated
= SCNetworkSetEstablishDefaultConfiguration(set
);
1095 CFStringRef setName
;
1097 _prefs_changed
= TRUE
;
1099 setName
= SCNetworkSetGetName(set
);
1100 if (setName
!= NULL
) {
1101 SCPrint(TRUE
, stdout
,
1102 CFSTR("set \"%@\" (%@) %supdated\n"),
1104 SCNetworkSetGetSetID(set
),
1105 setCreated
? "created, selected, and " : "");
1107 SCPrint(TRUE
, stdout
,
1108 CFSTR("set ID \"%@\" %supdated\n"),
1109 SCNetworkSetGetSetID(set
),
1110 setCreated
? "created, selected, and " : "");
1112 _prefs_changed
= TRUE
;
1122 do_net_upgrade(int argc
, char **argv
)
1124 Boolean do_commit
= FALSE
;
1127 if (prefs
== NULL
) {
1128 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1132 if (prefsPath
!= NULL
) {
1133 const char *prefs_plist
;
1135 prefs_plist
= strrchr(prefsPath
, '/');
1136 if (prefs_plist
!= NULL
) {
1139 prefs_plist
= prefsPath
;
1142 if (strcmp(prefs_plist
, PREFS_DEFAULT_CONFIG_PLIST
) != 0) {
1143 SCPrint(TRUE
, stdout
, CFSTR("not updating a \"preferences.plist\" file\n"));
1149 if (strcmp(argv
[0], PREFS_DEFAULT_CONFIG_PLIST
) == 0) {
1150 upgraded
= __SCNetworkConfigurationUpgrade(&prefs
, NULL
, do_commit
);
1151 } else if (strcmp(argv
[0], INTERFACES_DEFAULT_CONFIG_PLIST
) == 0) {
1152 if (ni_prefs
== NULL
) {
1153 ni_prefs
= SCPreferencesCreateCompanion(prefs
, INTERFACES_DEFAULT_CONFIG
);
1154 if (ni_prefs
== NULL
) {
1155 SC_log(LOG_NOTICE
, "SCPreferencesCreate( <NetworkInterfaces.plist> ) failed: %s", SCErrorString(SCError()));
1159 do_commit
= TRUE
; // using alternate, created on-the-fly preferences.plist
1161 upgraded
= __SCNetworkConfigurationUpgrade(NULL
, &ni_prefs
, do_commit
);
1163 SCPrint(TRUE
, stdout
, CFSTR("invalid .plist (\"preferences.plist\", \"NetworkInterfaces.plist\"\n"));
1167 upgraded
= __SCNetworkConfigurationUpgrade(&prefs
, &ni_prefs
, do_commit
);
1171 SCPrint(TRUE
, stdout
,
1172 do_commit
? CFSTR("network configuration upgraded/saved\n")
1173 : CFSTR("network configuration upgraded, use \"commit\" to save\n"));
1174 if ((prefsPath
== NULL
) || (strcmp(prefsPath
, PREFS_DEFAULT_CONFIG_PLIST
) == 0)) {
1175 SCPrint(TRUE
, stdout
,
1176 do_commit
? CFSTR("\n"
1177 "NOTE: because you have modified the system's \"live\" network configuration,\n"
1178 " a <reboot> is also REQUIRED.\n"
1182 "NOTE: because you are modifying the system's \"live\" network configuration,\n"
1183 " a <reboot> will also be REQUIRED.\n"
1189 _prefs_changed
= TRUE
;
1192 SCPrint(TRUE
, stdout
, CFSTR("network configuration upgrade not needed\n"));
1199 #include "SCPreferencesInternal.h"
1204 do_net_snapshot(int argc
, char **argv
)
1206 #pragma unused(argc)
1207 #pragma unused(argv)
1208 if (prefs
== NULL
) {
1209 SCPrint(TRUE
, stdout
, CFSTR("network configuration not open\n"));
1213 if (prefs
!= NULL
) {
1214 SCPreferencesPrivateRef prefsPrivate
= (SCPreferencesPrivateRef
)prefs
;
1216 if (prefsPrivate
->prefs
!= NULL
) {
1218 static int n_snapshot
= 0;
1222 asprintf(&path
, "/tmp/prefs_snapshot_%d", n_snapshot
++);
1224 fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
, 0644);
1227 SCPrint(TRUE
, stdout
, CFSTR("could not write snapshot: open() failed : %s\n"), strerror(errno
));
1231 xmlData
= CFPropertyListCreateData(NULL
, prefsPrivate
->prefs
, kCFPropertyListXMLFormat_v1_0
, 0, NULL
);
1232 if (xmlData
!= NULL
) {
1233 (void) write(fd
, CFDataGetBytePtr(xmlData
), CFDataGetLength(xmlData
));
1236 SCPrint(TRUE
, stdout
, CFSTR("could not write snapshot: CFPropertyListCreateData() failed\n"));
1241 SCPrint(TRUE
, stdout
, CFSTR("prefs have not been accessed\n"));