]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/net.c
configd-699.1.5.tar.gz
[apple/configd.git] / scutil.tproj / net.c
1 /*
2 * Copyright (c) 2004-2007, 2009-2011, 2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * August 5, 2004 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31
32 #include "scutil.h"
33 #include "commands.h"
34 #include "prefs.h"
35 #include "net.h"
36 #include "net_interface.h"
37 #include "net_protocol.h"
38 #include "net_service.h"
39 #include "net_set.h"
40
41 #include <unistd.h>
42
43
44 __private_extern__ CFMutableArrayRef new_interfaces = NULL;
45
46 __private_extern__ CFArrayRef interfaces = NULL;
47 __private_extern__ CFArrayRef services = NULL;
48 __private_extern__ CFArrayRef protocols = NULL;
49 __private_extern__ CFArrayRef sets = NULL;
50
51 __private_extern__ SCNetworkInterfaceRef net_interface = NULL;
52 __private_extern__ SCNetworkServiceRef net_service = NULL;
53 __private_extern__ SCNetworkProtocolRef net_protocol = NULL;
54 __private_extern__ SCNetworkSetRef net_set = NULL;
55
56 __private_extern__ CFNumberRef CFNumberRef_0 = NULL;
57 __private_extern__ CFNumberRef CFNumberRef_1 = NULL;
58
59
60 /* -------------------- */
61
62
63 __private_extern__
64 CF_RETURNS_RETAINED CFNumberRef
65 _copy_number(const char *arg)
66 {
67 int val;
68
69 if (sscanf(arg, "%d", &val) != 1) {
70 return NULL;
71 }
72
73 return CFNumberCreate(NULL, kCFNumberIntType, &val);
74 }
75
76
77 /* -------------------- */
78
79
80 __private_extern__
81 CFIndex
82 _find_option(const char *option, optionsRef options, const int nOptions)
83 {
84 CFIndex i;
85
86 for (i = 0; i < nOptions; i++) {
87 if (strcasecmp(option, options[i].option) == 0) {
88 return i;
89 }
90 }
91
92 return kCFNotFound;
93 }
94
95
96 __private_extern__
97 CFIndex
98 _find_selection(CFStringRef choice, selections choices[], unsigned int *flags)
99 {
100 CFIndex i;
101
102 i = 0;
103 while (choices[i].selection != NULL) {
104 if (CFStringCompare(choice,
105 choices[i].selection,
106 kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
107 if (flags != NULL) {
108 *flags = choices[i].flags;
109 }
110 return i;
111 }
112 i++;
113 }
114
115 return kCFNotFound;
116 }
117
118
119 __private_extern__
120 Boolean
121 _process_options(optionsRef options, int nOptions, int argc, char **argv, CFMutableDictionaryRef newConfiguration)
122 {
123 while (argc > 0) {
124 CFIndex optionIndex = kCFNotFound;
125
126 optionIndex = _find_option(argv[0], options, nOptions);
127 if (optionIndex == kCFNotFound) {
128 SCPrint(TRUE, stdout, CFSTR("set what?\n"));
129 return FALSE;
130 }
131 argv++;
132 argc--;
133
134 switch (options[optionIndex].type) {
135 case isOther :
136 // all option processing is managed by the "handler"
137 break;
138 case isHelp :
139 SCPrint(TRUE, stdout, CFSTR("%s\n"), options[optionIndex].info);
140 return FALSE;
141 case isChooseOne : {
142 CFStringRef choice;
143 selections *choices = (selections *)options[optionIndex].info;
144 unsigned int flags;
145 CFIndex i;
146
147 if (argc < 1) {
148 SCPrint(TRUE, stdout,
149 CFSTR("%s not specified\n"),
150 options[optionIndex].description != NULL ? options[optionIndex].description : "selection");
151 return FALSE;
152 }
153
154 choice = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
155 i = _find_selection(choice, choices, &flags);
156 CFRelease(choice);
157
158 if (i != kCFNotFound) {
159 if (choices[i].flags & selectionNotAvailable) {
160 SCPrint(TRUE, stdout,
161 CFSTR("cannot select %s\n"),
162 options[optionIndex].description != NULL ? options[optionIndex].description : "selection");
163 return FALSE;
164 }
165
166 CFDictionarySetValue(newConfiguration,
167 *(options[optionIndex].key),
168 *(choices[i].key));
169 } else {
170 SCPrint(TRUE, stdout,
171 CFSTR("invalid %s\n"),
172 options[optionIndex].description != NULL ? options[optionIndex].description : "selection");
173 return FALSE;
174 }
175
176 argv++;
177 argc--;
178 break;
179 }
180 case isChooseMultiple :
181 if (argc < 1) {
182 SCPrint(TRUE, stdout,
183 CFSTR("%s(s) not specified\n"),
184 options[optionIndex].description != NULL ? options[optionIndex].description : "selection");
185 return FALSE;
186 }
187
188 if (strlen(argv[0]) > 0) {
189 CFIndex i;
190 CFIndex n;
191 CFMutableArrayRef chosen;
192 CFStringRef str;
193 CFArrayRef str_array;
194
195 str = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
196 str_array = CFStringCreateArrayBySeparatingStrings(NULL, str, CFSTR(","));
197 CFRelease(str);
198
199 chosen = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
200
201 n = CFArrayGetCount(str_array);
202 for (i = 0; i < n; i++) {
203 CFStringRef choice;
204 selections *choices = (selections *)options[optionIndex].info;
205 unsigned int flags;
206 CFIndex j;
207
208 choice = CFArrayGetValueAtIndex(str_array, i);
209 j = _find_selection(choice, choices, &flags);
210
211 if (j != kCFNotFound) {
212 if (choices[j].flags & selectionNotAvailable) {
213 SCPrint(TRUE, stdout,
214 CFSTR("cannot select %s\n"),
215 options[optionIndex].description != NULL ? options[optionIndex].description : "selection");
216 CFArrayRemoveAllValues(chosen);
217 break;
218 }
219
220 CFArrayAppendValue(chosen, *(choices[j].key));
221 } else {
222 SCPrint(TRUE, stdout,
223 CFSTR("invalid %s\n"),
224 options[optionIndex].description != NULL ? options[optionIndex].description : "selection");
225 CFArrayRemoveAllValues(chosen);
226 break;
227 }
228 }
229 CFRelease(str_array);
230
231 if (CFArrayGetCount(chosen) > 0) {
232 CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), chosen);
233 } else {
234 CFDictionaryRemoveValue(newConfiguration, *(options[optionIndex].key));
235 }
236 CFRelease(chosen);
237 } else {
238 CFDictionaryRemoveValue(newConfiguration, *(options[optionIndex].key));
239 }
240
241 argv++;
242 argc--;
243 break;
244 case isBoolean :
245 if (argc < 1) {
246 SCPrint(TRUE, stdout,
247 CFSTR("%s not specified\n"),
248 options[optionIndex].description != NULL ? options[optionIndex].description : "enable/disable");
249 return FALSE;
250 }
251
252 if ((strcasecmp(argv[0], "disable") == 0) ||
253 (strcasecmp(argv[0], "no" ) == 0) ||
254 (strcasecmp(argv[0], "off" ) == 0) ||
255 (strcasecmp(argv[0], "0" ) == 0)) {
256 CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), CFNumberRef_0);
257 } else if ((strcasecmp(argv[0], "enable") == 0) ||
258 (strcasecmp(argv[0], "yes" ) == 0) ||
259 (strcasecmp(argv[0], "on" ) == 0) ||
260 (strcasecmp(argv[0], "1" ) == 0)) {
261 CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), CFNumberRef_1);
262 } else {
263 SCPrint(TRUE, stdout, CFSTR("invalid value\n"));
264 return FALSE;
265 }
266
267 argv++;
268 argc--;
269 break;
270 case isNumber :
271 if (argc < 1) {
272 SCPrint(TRUE, stdout,
273 CFSTR("%s not specified\n"),
274 options[optionIndex].description != NULL ? options[optionIndex].description : "value");
275 return FALSE;
276 }
277
278 if (strlen(argv[0]) > 0) {
279 CFNumberRef num;
280
281 num = _copy_number(argv[0]);
282 if (num != NULL) {
283 CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), num);
284 CFRelease(num);
285 } else {
286 SCPrint(TRUE, stdout, CFSTR("invalid value\n"));
287 return FALSE;
288 }
289 } else {
290 CFDictionaryRemoveValue(newConfiguration, *(options[optionIndex].key));
291 }
292
293 argv++;
294 argc--;
295 break;
296 case isString :
297 if (argc < 1) {
298 SCPrint(TRUE, stdout,
299 CFSTR("%s not specified\n"),
300 options[optionIndex].description != NULL ? options[optionIndex].description : "value");
301 return FALSE;
302 }
303
304 if (strlen(argv[0]) > 0) {
305 CFStringRef str;
306
307 str = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
308 CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), str);
309 CFRelease(str);
310 } else {
311 CFDictionaryRemoveValue(newConfiguration, *(options[optionIndex].key));
312 }
313
314 argv++;
315 argc--;
316 break;
317 case isStringArray :
318 if (argc < 1) {
319 SCPrint(TRUE, stdout,
320 CFSTR("%s(s) not specified\n"),
321 options[optionIndex].description != NULL ? options[optionIndex].description : "value");
322 return FALSE;
323 }
324
325 if (strlen(argv[0]) > 0) {
326 CFStringRef str;
327 CFArrayRef str_array;
328
329 str = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
330 str_array = CFStringCreateArrayBySeparatingStrings(NULL, str, CFSTR(","));
331 CFRelease(str);
332
333 CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), str_array);
334 CFRelease(str_array);
335 } else {
336 CFDictionaryRemoveValue(newConfiguration, *(options[optionIndex].key));
337 }
338
339 argv++;
340 argc--;
341 break;
342 }
343
344 if (options[optionIndex].handler != NULL) {
345 CFStringRef key;
346 int nArgs;
347
348 key = options[optionIndex].key != NULL ? *(options[optionIndex].key) : NULL;
349 nArgs = (*options[optionIndex].handler)(key,
350 options[optionIndex].description,
351 options[optionIndex].info,
352 argc,
353 argv,
354 newConfiguration);
355 if (nArgs < 0) {
356 return FALSE;
357 }
358
359 argv += nArgs;
360 argc -= nArgs;
361 }
362 }
363
364 return TRUE;
365 }
366
367
368 /* -------------------- */
369
370
371 #define N_QUICK 32
372
373 __private_extern__
374 void
375 _show_entity(CFDictionaryRef entity, CFStringRef prefix)
376 {
377 CFArrayRef array;
378 const void * keys_q[N_QUICK];
379 const void ** keys = keys_q;
380 CFIndex i;
381 CFIndex n;
382 CFMutableArrayRef sorted;
383
384 n = CFDictionaryGetCount(entity);
385 if (n > (CFIndex)(sizeof(keys_q) / sizeof(CFTypeRef))) {
386 keys = CFAllocatorAllocate(NULL, n * sizeof(CFTypeRef), 0);
387 }
388 CFDictionaryGetKeysAndValues(entity, keys, NULL);
389
390 array = CFArrayCreate(NULL, keys, n, &kCFTypeArrayCallBacks);
391 sorted = CFArrayCreateMutableCopy(NULL, n, array);
392 if (n > 1) {
393 CFArraySortValues(sorted,
394 CFRangeMake(0, n),
395 (CFComparatorFunction)CFStringCompare,
396 NULL);
397 }
398
399 for (i = 0; i < n; i++) {
400 CFStringRef key;
401 CFTypeRef value;
402
403 key = CFArrayGetValueAtIndex(sorted, i);
404 value = CFDictionaryGetValue(entity, key);
405 if (isA_CFArray(value)) {
406 CFIndex i;
407 CFIndex n = CFArrayGetCount(value);
408
409 SCPrint(TRUE, stdout, CFSTR("%@ %@ = ("), prefix, key);
410 for (i = 0; i < n; i++) {
411 CFTypeRef val;
412
413 val = CFArrayGetValueAtIndex(value, i);
414 SCPrint(TRUE, stdout,
415 CFSTR("%s%@"),
416 (i > 0) ? ", " : "",
417 val);
418 }
419 SCPrint(TRUE, stdout, CFSTR(")\n"));
420 } else {
421 SCPrint(TRUE, stdout, CFSTR("%@ %@ = %@\n"), prefix, key, value);
422 }
423 }
424
425 CFRelease(sorted);
426 CFRelease(array);
427 if (keys != keys_q) {
428 CFAllocatorDeallocate(NULL, keys);
429 }
430
431 return;
432 }
433
434
435 /* -------------------- */
436
437
438 static void
439 _net_close()
440 {
441 if (net_interface != NULL) {
442 CFRelease(net_interface);
443 net_interface = NULL;
444 }
445
446 if (net_service != NULL) {
447 CFRelease(net_service);
448 net_service = NULL;
449 }
450
451 if (net_protocol != NULL) {
452 CFRelease(net_protocol);
453 net_protocol = NULL;
454 }
455
456 if (net_set != NULL) {
457 CFRelease(net_set);
458 net_set = NULL;
459 }
460
461 if (interfaces != NULL) {
462 CFRelease(interfaces);
463 interfaces = NULL;
464 }
465
466 if (services != NULL) {
467 CFRelease(services);
468 services = NULL;
469 }
470
471 if (protocols != NULL) {
472 CFRelease(protocols);
473 protocols = NULL;
474 }
475
476 if (sets != NULL) {
477 CFRelease(sets);
478 sets = NULL;
479 }
480
481 if (new_interfaces != NULL) {
482 CFRelease(new_interfaces);
483 new_interfaces = NULL;
484 }
485
486 return;
487 }
488
489
490 __private_extern__
491 void
492 do_net_init()
493 {
494 int one = 1;
495 int zero = 0;
496
497 CFNumberRef_0 = CFNumberCreate(NULL, kCFNumberIntType, &zero);
498 CFNumberRef_1 = CFNumberCreate(NULL, kCFNumberIntType, &one);
499
500 return;
501 }
502
503
504 __private_extern__
505 void
506 do_net_open(int argc, char **argv)
507 {
508 Boolean ok;
509 CFStringRef prefsID = NULL;
510
511 if (prefs != NULL) {
512 if (_prefs_commitRequired(argc, argv, "close")) {
513 return;
514 }
515
516 _net_close();
517 _prefs_close();
518 }
519
520 if (argc > 0) {
521 prefsID = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
522 }
523
524 ok = _prefs_open(CFSTR("scutil --net"), prefsID);
525 if (prefsID != NULL) CFRelease(prefsID);
526 if (!ok) {
527 SCPrint(TRUE,
528 stdout,
529 CFSTR("Could not open prefs: %s\n"),
530 SCErrorString(SCError()));
531 return;
532 }
533
534 net_set = SCNetworkSetCopyCurrent(prefs);
535 if (net_set != NULL) {
536 CFStringRef setName;
537
538 setName = SCNetworkSetGetName(net_set);
539 if (setName != NULL) {
540 SCPrint(TRUE, stdout, CFSTR("set \"%@\" selected\n"), setName);
541 } else {
542 SCPrint(TRUE, stdout,
543 CFSTR("set ID \"%@\" selected\n"),
544 SCNetworkSetGetSetID(net_set));
545 }
546 }
547
548 return;
549 }
550
551
552 __private_extern__
553 void
554 do_net_commit(int argc, char **argv)
555 {
556 if (!SCPreferencesCommitChanges(prefs)) {
557 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
558 return;
559 }
560
561 _prefs_changed = FALSE;
562 return;
563 }
564
565
566 __private_extern__
567 void
568 do_net_apply(int argc, char **argv)
569 {
570 if (!SCPreferencesApplyChanges(prefs)) {
571 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
572 }
573 return;
574 }
575
576
577 __private_extern__
578 void
579 do_net_close(int argc, char **argv)
580 {
581 if (_prefs_commitRequired(argc, argv, "close")) {
582 return;
583 }
584
585 _net_close();
586 _prefs_close();
587
588 return;
589 }
590
591
592 __private_extern__
593 void
594 do_net_quit(int argc, char **argv)
595 {
596 if (_prefs_commitRequired(argc, argv, "quit")) {
597 return;
598 }
599
600 _net_close();
601 _prefs_close();
602
603 termRequested = TRUE;
604 return;
605 }
606
607
608 /* -------------------- */
609
610
611 typedef void (*net_func) (int argc, char **argv);
612
613 static const struct {
614 char *key;
615 net_func create;
616 net_func disable;
617 net_func enable;
618 net_func select;
619 net_func set;
620 net_func show;
621 net_func remove;
622 } net_keys[] = {
623
624 { "interfaces", NULL , NULL , NULL ,
625 NULL , NULL , show_interfaces ,
626 NULL },
627
628 { "interface", create_interface, NULL , NULL ,
629 select_interface, set_interface , show_interface ,
630 NULL },
631
632 { "services", NULL , NULL , NULL ,
633 NULL , NULL , show_services ,
634 NULL },
635
636 { "service", create_service , disable_service , enable_service ,
637 select_service , set_service , show_service ,
638 remove_service },
639
640 { "protocols", NULL , NULL , NULL ,
641 NULL , NULL , show_protocols ,
642 NULL },
643
644 { "protocol", create_protocol , disable_protocol, enable_protocol ,
645 select_protocol , set_protocol , show_protocol ,
646 remove_protocol },
647
648 { "sets", NULL , NULL , NULL ,
649 NULL , NULL , show_sets ,
650 NULL },
651
652 { "set", create_set , NULL , NULL ,
653 select_set , set_set , show_set ,
654 remove_set }
655
656 };
657 #define N_NET_KEYS (sizeof(net_keys) / sizeof(net_keys[0]))
658
659
660 static int
661 findNetKey(char *key)
662 {
663 int i;
664
665 for (i = 0; i < (int)N_NET_KEYS; i++) {
666 if (strcmp(key, net_keys[i].key) == 0) {
667 return i;
668 }
669 }
670
671 return -1;
672 }
673
674
675 /* -------------------- */
676
677
678 __private_extern__
679 void
680 do_net_create(int argc, char **argv)
681 {
682 char *key;
683 int i;
684
685 key = argv[0];
686 argv++;
687 argc--;
688
689 i = findNetKey(key);
690 if (i < 0) {
691 SCPrint(TRUE, stderr, CFSTR("create what?\n"));
692 return;
693 }
694
695 if (net_keys[i].create == NULL) {
696 SCPrint(TRUE, stderr, CFSTR("create what?\n"));
697 return;
698 }
699
700 (*net_keys[i].create)(argc, argv);
701 return;
702 }
703
704
705 __private_extern__
706 void
707 do_net_disable(int argc, char **argv)
708 {
709 char *key;
710 int i;
711
712 key = argv[0];
713 argv++;
714 argc--;
715
716 i = findNetKey(key);
717 if (i < 0) {
718 SCPrint(TRUE, stderr, CFSTR("disable what?\n"));
719 return;
720 }
721
722 if (net_keys[i].disable == NULL) {
723 SCPrint(TRUE, stderr, CFSTR("disable what?\n"));
724 return;
725 }
726
727 (*net_keys[i].disable)(argc, argv);
728 return;
729 }
730
731
732 __private_extern__
733 void
734 do_net_enable(int argc, char **argv)
735 {
736 char *key;
737 int i;
738
739 key = argv[0];
740 argv++;
741 argc--;
742
743 i = findNetKey(key);
744 if (i < 0) {
745 SCPrint(TRUE, stderr, CFSTR("enable what?\n"));
746 return;
747 }
748
749 if (net_keys[i].enable == NULL) {
750 SCPrint(TRUE, stderr, CFSTR("enable what?\n"));
751 return;
752 }
753
754 (*net_keys[i].enable)(argc, argv);
755 return;
756 }
757
758
759 static void
760 do_net_migrate_perform(int argc, char **argv)
761 {
762 char * sourceConfiguration = NULL;
763 char * targetConfiguration = NULL;
764 char * currentConfiguration = NULL;
765 CFStringRef str = NULL;
766 CFURLRef sourceConfigurationURL = NULL;
767 CFURLRef targetConfigurationURL = NULL;
768 CFURLRef currentConfigurationURL = NULL;
769 CFArrayRef migrationFiles = NULL;
770
771 sourceConfiguration = argv[0];
772 targetConfiguration = argv[1];
773
774 if (argc == 3) {
775 currentConfiguration = argv[2];
776 }
777
778 SCPrint(_sc_debug, stdout, CFSTR("sourceConfiguration: %s\ntargetConfiguration: %s\ncurrentConfiguration: %s\n"),
779 sourceConfiguration, targetConfiguration, (currentConfiguration != NULL) ? currentConfiguration : "<current system>" );
780
781 str = CFStringCreateWithCString(NULL, sourceConfiguration, kCFStringEncodingUTF8);
782 sourceConfigurationURL = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, TRUE);
783 CFRelease(str);
784
785 str = CFStringCreateWithCString(NULL, targetConfiguration, kCFStringEncodingUTF8);
786 targetConfigurationURL = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, TRUE);
787 CFRelease(str);
788
789 if (currentConfiguration != NULL) {
790 str = CFStringCreateWithCString(NULL, currentConfiguration, kCFStringEncodingUTF8);
791 currentConfigurationURL = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, TRUE);
792 CFRelease(str);
793 }
794
795 migrationFiles = _SCNetworkConfigurationPerformMigration(sourceConfigurationURL, currentConfigurationURL, targetConfigurationURL, NULL);
796
797 if (migrationFiles != NULL) {
798 SCPrint(TRUE, stdout, CFSTR("Migration Successful: %@ \n"), migrationFiles);
799 }
800 else {
801 SCPrint(TRUE, stdout, CFSTR("Migration Unsuccessful \n"));
802 }
803
804 if (sourceConfigurationURL != NULL) {
805 CFRelease(sourceConfigurationURL);
806 }
807 if (targetConfigurationURL != NULL) {
808 CFRelease(targetConfigurationURL);
809 }
810 if (currentConfigurationURL != NULL) {
811 CFRelease(currentConfigurationURL);
812 }
813 if (migrationFiles != NULL) {
814 CFRelease(migrationFiles);
815 }
816 }
817
818
819 static void
820 do_net_migrate_validate(int argc, char **argv)
821 {
822 char *configuration = NULL;
823 CFURLRef configurationURL = NULL;
824 char *expectedConfiguration = NULL;
825 CFURLRef expectedConfigurationURL = NULL;
826 Boolean isValid = FALSE;
827 CFStringRef str = NULL;
828
829 configuration = argv[0];
830 str = CFStringCreateWithCString(NULL, configuration, kCFStringEncodingUTF8);
831 configurationURL = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, TRUE);
832 CFRelease(str);
833
834 expectedConfiguration = argv[1];
835 str = CFStringCreateWithCString(NULL, expectedConfiguration, kCFStringEncodingUTF8);
836 expectedConfigurationURL = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, TRUE);
837 CFRelease(str);
838
839 isValid = _SCNetworkMigrationAreConfigurationsIdentical(configurationURL, expectedConfigurationURL);
840
841 SCPrint(TRUE, stdout, CFSTR("Configuration at location %s %s\n"), configuration, isValid ? "is valid" : "is NOT valid");
842
843 if (configurationURL != NULL) {
844 CFRelease(configurationURL);
845 }
846 if (expectedConfigurationURL != NULL) {
847 CFRelease(expectedConfigurationURL);
848 }
849 }
850
851
852 __private_extern__
853 void
854 do_net_migrate(int argc, char **argv)
855 {
856 char *key;
857 SCPrint(TRUE, stdout, CFSTR("do_net_migrate called, %d\n"), argc);
858
859 key = argv[0];
860 argv++;
861 argc--;
862
863 if (strncmp(key, "perform", strlen(key)) == 0) {
864 do_net_migrate_perform(argc, argv);
865 }
866 else if (strncmp(key, "validate", strlen(key)) == 0) {
867 do_net_migrate_validate(argc, argv);
868 }
869 else {
870 SCPrint(TRUE, stderr, CFSTR("migrate what?\n"));
871 return;
872 }
873
874 }
875
876
877 __private_extern__
878 void
879 do_net_remove(int argc, char **argv)
880 {
881 char *key;
882 int i;
883
884 key = argv[0];
885 argv++;
886 argc--;
887
888 i = findNetKey(key);
889 if (i < 0) {
890 SCPrint(TRUE, stderr, CFSTR("remove what?\n"));
891 return;
892 }
893
894 if (net_keys[i].remove == NULL) {
895 SCPrint(TRUE, stderr, CFSTR("remove what?\n"));
896 return;
897 }
898
899 (*net_keys[i].remove)(argc, argv);
900 return;
901 }
902
903
904 __private_extern__
905 void
906 do_net_select(int argc, char **argv)
907 {
908 char *key;
909 int i;
910
911 key = argv[0];
912 argv++;
913 argc--;
914
915 i = findNetKey(key);
916 if (i < 0) {
917 SCPrint(TRUE, stderr, CFSTR("select what?\n"));
918 return;
919 }
920
921 if (*net_keys[i].select == NULL) {
922 SCPrint(TRUE, stderr, CFSTR("select what?\n"));
923 return;
924 }
925
926 (*net_keys[i].select)(argc, argv);
927 return;
928 }
929
930
931 __private_extern__
932 void
933 do_net_set(int argc, char **argv)
934 {
935 char *key;
936 int i;
937
938 key = argv[0];
939 argv++;
940 argc--;
941
942 i = findNetKey(key);
943 if (i < 0) {
944 SCPrint(TRUE, stderr, CFSTR("set what?\n"));
945 return;
946 }
947
948 (*net_keys[i].set)(argc, argv);
949 return;
950 }
951
952
953 __private_extern__
954 void
955 do_net_show(int argc, char **argv)
956 {
957 char *key;
958 int i;
959
960 key = argv[0];
961 argv++;
962 argc--;
963
964 i = findNetKey(key);
965 if (i < 0) {
966 SCPrint(TRUE, stderr, CFSTR("show what?\n"));
967 return;
968 }
969
970 (*net_keys[i].show)(argc, argv);
971 return;
972 }
973
974
975 __private_extern__
976 void
977 do_net_update(int argc, char **argv)
978 {
979 SCNetworkSetRef set;
980 Boolean setCreated = FALSE;
981 Boolean setUpdated = FALSE;
982
983 if (prefs == NULL) {
984 SCPrint(TRUE, stdout, CFSTR("network configuration not open\n"));
985 return;
986 }
987
988 if (net_set != NULL) {
989 set = CFRetain(net_set);
990 } else {
991 set = SCNetworkSetCopyCurrent(prefs);
992 if (set == NULL) {
993 CFBundleRef bundle;
994 Boolean ok;
995 CFArrayRef sets;
996 CFStringRef setName = NULL;
997
998 sets = SCNetworkSetCopyAll(prefs);
999 if (sets != NULL) {
1000 CFIndex n;
1001
1002 n = CFArrayGetCount(sets);
1003 CFRelease(sets);
1004 sets = NULL;
1005 if (n > 0) {
1006 SCPrint(TRUE, stdout, CFSTR("no current set\n"));
1007 return;
1008 }
1009 }
1010
1011 bundle = _SC_CFBundleGet();
1012 if (bundle != NULL) {
1013 setName = CFBundleCopyLocalizedString(bundle,
1014 CFSTR("DEFAULT_SET_NAME"),
1015 CFSTR("Automatic"),
1016 NULL);
1017 }
1018 if (setName == NULL) {
1019 setName = CFSTR("Automatic");
1020 CFRetain(setName);
1021 }
1022
1023 set = SCNetworkSetCreate(prefs);
1024 if (set == NULL) {
1025 SCPrint(TRUE, stdout,
1026 CFSTR("could not initialize \"%@\": %s\n"),
1027 setName,
1028 SCErrorString(SCError()));
1029 CFRelease(setName);
1030 return;
1031 }
1032
1033 (void) SCNetworkSetSetName(set, setName);
1034
1035 ok = SCNetworkSetSetCurrent(set);
1036 if (!ok) {
1037 SCPrint(TRUE, stdout,
1038 CFSTR("could not initialize \"%@\": %s\n"),
1039 setName,
1040 SCErrorString(SCError()));
1041 (void) SCNetworkSetRemove(set);
1042 CFRelease(setName);
1043 CFRelease(set);
1044 return;
1045 }
1046
1047 if (net_set != NULL) CFRelease(net_set);
1048 net_set = set;
1049
1050 setCreated = TRUE;
1051
1052 CFRelease(setName);
1053 CFRetain(set);
1054 }
1055 }
1056
1057 setUpdated = SCNetworkSetEstablishDefaultConfiguration(set);
1058 if (setUpdated) {
1059 CFStringRef setName;
1060
1061 _prefs_changed = TRUE;
1062
1063 setName = SCNetworkSetGetName(set);
1064 if (setName != NULL) {
1065 SCPrint(TRUE, stdout,
1066 CFSTR("set \"%@\" (%@) %supdated\n"),
1067 setName,
1068 SCNetworkSetGetSetID(set),
1069 setCreated ? "created, selected, and " : "");
1070 } else {
1071 SCPrint(TRUE, stdout,
1072 CFSTR("set ID \"%@\" %supdated\n"),
1073 SCNetworkSetGetSetID(set),
1074 setCreated ? "created, selected, and " : "");
1075 }
1076 }
1077
1078 CFRelease(set);
1079 return;
1080 }
1081
1082
1083 #include "SCPreferencesInternal.h"
1084 #include <fcntl.h>
1085 #include <unistd.h>
1086 __private_extern__
1087 void
1088 do_net_snapshot(int argc, char **argv)
1089 {
1090 if (prefs == NULL) {
1091 SCPrint(TRUE, stdout, CFSTR("network configuration not open\n"));
1092 return;
1093 }
1094
1095 if (prefs != NULL) {
1096 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
1097
1098 if (prefsPrivate->prefs != NULL) {
1099 int fd;
1100 static int n_snapshot = 0;
1101 char *path;
1102 CFDataRef xmlData;
1103
1104 asprintf(&path, "/tmp/prefs_snapshot_%d", n_snapshot++);
1105 (void)unlink(path);
1106 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
1107 free(path);
1108 if (fd == -1) {
1109 SCPrint(TRUE, stdout, CFSTR("could not write snapshot: open() failed : %s\n"), strerror(errno));
1110 return;
1111 }
1112
1113 xmlData = CFPropertyListCreateData(NULL, prefsPrivate->prefs, kCFPropertyListXMLFormat_v1_0, 0, NULL);
1114 if (xmlData != NULL) {
1115 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
1116 CFRelease(xmlData);
1117 } else {
1118 SCPrint(TRUE, stdout, CFSTR("could not write snapshot: CFPropertyListCreateData() failed\n"));
1119 }
1120
1121 (void) close(fd);
1122 } else {
1123 SCPrint(TRUE, stdout, CFSTR("prefs have not been accessed\n"));
1124 }
1125 }
1126
1127 return;
1128 }