]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/net.c
configd-293.4.tar.gz
[apple/configd.git] / scutil.tproj / net.c
1 /*
2 * Copyright (c) 2004-2007, 2009 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 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], "0" ) == 0)) {
255 CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), CFNumberRef_0);
256 } else if ((strcasecmp(argv[0], "enable") == 0) ||
257 (strcasecmp(argv[0], "yes" ) == 0) ||
258 (strcasecmp(argv[0], "1" ) == 0)) {
259 CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), CFNumberRef_1);
260 } else {
261 SCPrint(TRUE, stdout, CFSTR("invalid value\n"));
262 return FALSE;
263 }
264
265 argv++;
266 argc--;
267 break;
268 case isNumber :
269 if (argc < 1) {
270 SCPrint(TRUE, stdout,
271 CFSTR("%s not specified\n"),
272 options[optionIndex].description != NULL ? options[optionIndex].description : "value");
273 return FALSE;
274 }
275
276 if (strlen(argv[0]) > 0) {
277 CFNumberRef num;
278
279 num = _copy_number(argv[0]);
280 if (num != NULL) {
281 CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), num);
282 CFRelease(num);
283 } else {
284 SCPrint(TRUE, stdout, CFSTR("invalid value\n"));
285 return FALSE;
286 }
287 } else {
288 CFDictionaryRemoveValue(newConfiguration, *(options[optionIndex].key));
289 }
290
291 argv++;
292 argc--;
293 break;
294 case isString :
295 if (argc < 1) {
296 SCPrint(TRUE, stdout,
297 CFSTR("%s not specified\n"),
298 options[optionIndex].description != NULL ? options[optionIndex].description : "value");
299 return FALSE;
300 }
301
302 if (strlen(argv[0]) > 0) {
303 CFStringRef str;
304
305 str = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
306 CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), str);
307 CFRelease(str);
308 } else {
309 CFDictionaryRemoveValue(newConfiguration, *(options[optionIndex].key));
310 }
311
312 argv++;
313 argc--;
314 break;
315 case isStringArray :
316 if (argc < 1) {
317 SCPrint(TRUE, stdout,
318 CFSTR("%s(s) not specified\n"),
319 options[optionIndex].description != NULL ? options[optionIndex].description : "value");
320 return FALSE;
321 }
322
323 if (strlen(argv[0]) > 0) {
324 CFStringRef str;
325 CFArrayRef str_array;
326
327 str = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
328 str_array = CFStringCreateArrayBySeparatingStrings(NULL, str, CFSTR(","));
329 CFRelease(str);
330
331 CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), str_array);
332 CFRelease(str_array);
333 } else {
334 CFDictionaryRemoveValue(newConfiguration, *(options[optionIndex].key));
335 }
336
337 argv++;
338 argc--;
339 break;
340 }
341
342 if (options[optionIndex].handler != NULL) {
343 CFStringRef key;
344 int nArgs;
345
346 key = options[optionIndex].key != NULL ? *(options[optionIndex].key) : NULL;
347 nArgs = (*options[optionIndex].handler)(key,
348 options[optionIndex].description,
349 options[optionIndex].info,
350 argc,
351 argv,
352 newConfiguration);
353 if (nArgs < 0) {
354 return FALSE;
355 }
356
357 argv += nArgs;
358 argc -= nArgs;
359 }
360 }
361
362 return TRUE;
363 }
364
365
366 /* -------------------- */
367
368
369 #define N_QUICK 32
370
371 __private_extern__
372 void
373 _show_entity(CFDictionaryRef entity, CFStringRef prefix)
374 {
375 CFArrayRef array;
376 const void * keys_q[N_QUICK];
377 const void ** keys = keys_q;
378 CFIndex i;
379 CFIndex n;
380 CFMutableArrayRef sorted;
381
382 n = CFDictionaryGetCount(entity);
383 if (n > (CFIndex)(sizeof(keys_q) / sizeof(CFTypeRef))) {
384 keys = CFAllocatorAllocate(NULL, n * sizeof(CFTypeRef), 0);
385 }
386 CFDictionaryGetKeysAndValues(entity, keys, NULL);
387
388 array = CFArrayCreate(NULL, keys, n, &kCFTypeArrayCallBacks);
389 sorted = CFArrayCreateMutableCopy(NULL, n, array);
390 if (n > 1) {
391 CFArraySortValues(sorted,
392 CFRangeMake(0, n),
393 (CFComparatorFunction)CFStringCompare,
394 NULL);
395 }
396
397 for (i = 0; i < n; i++) {
398 CFStringRef key;
399 CFTypeRef value;
400
401 key = CFArrayGetValueAtIndex(sorted, i);
402 value = CFDictionaryGetValue(entity, key);
403 if (isA_CFArray(value)) {
404 CFIndex i;
405 CFIndex n = CFArrayGetCount(value);
406
407 SCPrint(TRUE, stdout, CFSTR("%@ %@ = ("), prefix, key);
408 for (i = 0; i < n; i++) {
409 CFTypeRef val;
410
411 val = CFArrayGetValueAtIndex(value, i);
412 SCPrint(TRUE, stdout,
413 CFSTR("%s%@"),
414 (i > 0) ? ", " : "",
415 val);
416 }
417 SCPrint(TRUE, stdout, CFSTR(")\n"));
418 } else {
419 SCPrint(TRUE, stdout, CFSTR("%@ %@ = %@\n"), prefix, key, value);
420 }
421 }
422
423 CFRelease(sorted);
424 CFRelease(array);
425 if (keys != keys_q) {
426 CFAllocatorDeallocate(NULL, keys);
427 }
428
429 return;
430 }
431
432
433 /* -------------------- */
434
435
436 static void
437 _net_close()
438 {
439 if (net_interface != NULL) {
440 CFRelease(net_interface);
441 net_interface = NULL;
442 }
443
444 if (net_service != NULL) {
445 CFRelease(net_service);
446 net_service = NULL;
447 }
448
449 if (net_protocol != NULL) {
450 CFRelease(net_protocol);
451 net_protocol = NULL;
452 }
453
454 if (net_set != NULL) {
455 CFRelease(net_set);
456 net_set = NULL;
457 }
458
459 if (interfaces != NULL) {
460 CFRelease(interfaces);
461 interfaces = NULL;
462 }
463
464 if (services != NULL) {
465 CFRelease(services);
466 services = NULL;
467 }
468
469 if (protocols != NULL) {
470 CFRelease(protocols);
471 protocols = NULL;
472 }
473
474 if (sets != NULL) {
475 CFRelease(sets);
476 sets = NULL;
477 }
478
479 if (new_interfaces != NULL) {
480 CFRelease(new_interfaces);
481 new_interfaces = NULL;
482 }
483
484 return;
485 }
486
487
488 __private_extern__
489 void
490 do_net_init()
491 {
492 int one = 1;
493 int zero = 0;
494
495 CFNumberRef_0 = CFNumberCreate(NULL, kCFNumberIntType, &zero);
496 CFNumberRef_1 = CFNumberCreate(NULL, kCFNumberIntType, &one);
497
498 return;
499 }
500
501
502 __private_extern__
503 void
504 do_net_open(int argc, char **argv)
505 {
506 Boolean ok;
507 CFStringRef prefsID = NULL;
508
509 if (prefs != NULL) {
510 if (_prefs_commitRequired(argc, argv, "close")) {
511 return;
512 }
513
514 _net_close();
515 _prefs_close();
516 }
517
518 if (argc > 0) {
519 prefsID = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8);
520 }
521
522 ok = _prefs_open(CFSTR("scutil --net"), prefsID);
523 if (prefsID != NULL) CFRelease(prefsID);
524 if (!ok) {
525 SCPrint(TRUE,
526 stdout,
527 CFSTR("Could not open prefs: %s\n"),
528 SCErrorString(SCError()));
529 return;
530 }
531
532 net_set = SCNetworkSetCopyCurrent(prefs);
533 if (net_set != NULL) {
534 CFStringRef setName;
535
536 setName = SCNetworkSetGetName(net_set);
537 if (setName != NULL) {
538 SCPrint(TRUE, stdout, CFSTR("set \"%@\" selected\n"), setName);
539 } else {
540 SCPrint(TRUE, stdout,
541 CFSTR("set ID \"%@\" selected\n"),
542 SCNetworkSetGetSetID(net_set));
543 }
544 }
545
546 return;
547 }
548
549
550 __private_extern__
551 void
552 do_net_commit(int argc, char **argv)
553 {
554 if (!SCPreferencesCommitChanges(prefs)) {
555 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
556 return;
557 }
558
559 _prefs_changed = FALSE;
560 return;
561 }
562
563
564 __private_extern__
565 void
566 do_net_apply(int argc, char **argv)
567 {
568 if (!SCPreferencesApplyChanges(prefs)) {
569 SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError()));
570 }
571 return;
572 }
573
574
575 __private_extern__
576 void
577 do_net_close(int argc, char **argv)
578 {
579 if (_prefs_commitRequired(argc, argv, "close")) {
580 return;
581 }
582
583 _net_close();
584 _prefs_close();
585
586 return;
587 }
588
589
590 __private_extern__
591 void
592 do_net_quit(int argc, char **argv)
593 {
594 if (_prefs_commitRequired(argc, argv, "quit")) {
595 return;
596 }
597
598 _net_close();
599 _prefs_close();
600
601 termRequested = TRUE;
602 return;
603 }
604
605
606 /* -------------------- */
607
608
609 typedef void (*net_func) (int argc, char **argv);
610
611 static const struct {
612 char *key;
613 net_func create;
614 net_func disable;
615 net_func enable;
616 net_func select;
617 net_func set;
618 net_func show;
619 net_func remove;
620 } net_keys[] = {
621
622 { "interfaces", NULL , NULL , NULL ,
623 NULL , NULL , show_interfaces ,
624 NULL },
625
626 { "interface", create_interface, NULL , NULL ,
627 select_interface, set_interface , show_interface ,
628 NULL },
629
630 { "services", NULL , NULL , NULL ,
631 NULL , NULL , show_services ,
632 NULL },
633
634 { "service", create_service , disable_service , enable_service ,
635 select_service , set_service , show_service ,
636 remove_service },
637
638 { "protocols", NULL , NULL , NULL ,
639 NULL , NULL , show_protocols ,
640 NULL },
641
642 { "protocol", create_protocol , disable_protocol, enable_protocol ,
643 select_protocol , set_protocol , show_protocol ,
644 remove_protocol },
645
646 { "sets", NULL , NULL , NULL ,
647 NULL , NULL , show_sets ,
648 NULL },
649
650 { "set", create_set , NULL , NULL ,
651 select_set , set_set , show_set ,
652 remove_set }
653
654 };
655 #define N_NET_KEYS (sizeof(net_keys) / sizeof(net_keys[0]))
656
657
658 static int
659 findNetKey(char *key)
660 {
661 int i;
662
663 for (i = 0; i < (int)N_NET_KEYS; i++) {
664 if (strcmp(key, net_keys[i].key) == 0) {
665 return i;
666 }
667 }
668
669 return -1;
670 }
671
672
673 /* -------------------- */
674
675
676 __private_extern__
677 void
678 do_net_create(int argc, char **argv)
679 {
680 char *key;
681 int i;
682
683 key = argv[0];
684 argv++;
685 argc--;
686
687 i = findNetKey(key);
688 if (i < 0) {
689 SCPrint(TRUE, stderr, CFSTR("create what?\n"));
690 return;
691 }
692
693 if (*net_keys[i].create == NULL) {
694 SCPrint(TRUE, stderr, CFSTR("create what?\n"));
695 }
696
697 (*net_keys[i].create)(argc, argv);
698 return;
699 }
700
701
702 __private_extern__
703 void
704 do_net_disable(int argc, char **argv)
705 {
706 char *key;
707 int i;
708
709 key = argv[0];
710 argv++;
711 argc--;
712
713 i = findNetKey(key);
714 if (i < 0) {
715 SCPrint(TRUE, stderr, CFSTR("disable what?\n"));
716 return;
717 }
718
719 if (*net_keys[i].disable == NULL) {
720 SCPrint(TRUE, stderr, CFSTR("disable what?\n"));
721 }
722
723 (*net_keys[i].disable)(argc, argv);
724 return;
725 }
726
727
728 __private_extern__
729 void
730 do_net_enable(int argc, char **argv)
731 {
732 char *key;
733 int i;
734
735 key = argv[0];
736 argv++;
737 argc--;
738
739 i = findNetKey(key);
740 if (i < 0) {
741 SCPrint(TRUE, stderr, CFSTR("enable what?\n"));
742 return;
743 }
744
745 if (*net_keys[i].enable == NULL) {
746 SCPrint(TRUE, stderr, CFSTR("enable what?\n"));
747 }
748
749 (*net_keys[i].enable)(argc, argv);
750 return;
751 }
752
753
754 __private_extern__
755 void
756 do_net_remove(int argc, char **argv)
757 {
758 char *key;
759 int i;
760
761 key = argv[0];
762 argv++;
763 argc--;
764
765 i = findNetKey(key);
766 if (i < 0) {
767 SCPrint(TRUE, stderr, CFSTR("remove what?\n"));
768 return;
769 }
770
771 if (*net_keys[i].remove == NULL) {
772 SCPrint(TRUE, stderr, CFSTR("remove what?\n"));
773 }
774
775 (*net_keys[i].remove)(argc, argv);
776 return;
777 }
778
779
780 __private_extern__
781 void
782 do_net_select(int argc, char **argv)
783 {
784 char *key;
785 int i;
786
787 key = argv[0];
788 argv++;
789 argc--;
790
791 i = findNetKey(key);
792 if (i < 0) {
793 SCPrint(TRUE, stderr, CFSTR("select what?\n"));
794 return;
795 }
796
797 if (*net_keys[i].select == NULL) {
798 SCPrint(TRUE, stderr, CFSTR("select what?\n"));
799 }
800
801 (*net_keys[i].select)(argc, argv);
802 return;
803 }
804
805
806 __private_extern__
807 void
808 do_net_set(int argc, char **argv)
809 {
810 char *key;
811 int i;
812
813 key = argv[0];
814 argv++;
815 argc--;
816
817 i = findNetKey(key);
818 if (i < 0) {
819 SCPrint(TRUE, stderr, CFSTR("set what?\n"));
820 return;
821 }
822
823 (*net_keys[i].set)(argc, argv);
824 return;
825 }
826
827
828 __private_extern__
829 void
830 do_net_show(int argc, char **argv)
831 {
832 char *key;
833 int i;
834
835 key = argv[0];
836 argv++;
837 argc--;
838
839 i = findNetKey(key);
840 if (i < 0) {
841 SCPrint(TRUE, stderr, CFSTR("show what?\n"));
842 return;
843 }
844
845 (*net_keys[i].show)(argc, argv);
846 return;
847 }
848
849
850 __private_extern__
851 void
852 do_net_update(int argc, char **argv)
853 {
854 SCNetworkSetRef set;
855 Boolean setCreated = FALSE;
856 Boolean setUpdated = FALSE;
857
858 if (prefs == NULL) {
859 SCPrint(TRUE, stdout, CFSTR("network configuration not open\n"));
860 return;
861 }
862
863 if (net_set != NULL) {
864 set = CFRetain(net_set);
865 } else {
866 set = SCNetworkSetCopyCurrent(prefs);
867 if (set == NULL) {
868 CFBundleRef bundle;
869 Boolean ok;
870 CFArrayRef sets;
871 CFStringRef setName = NULL;
872
873 sets = SCNetworkSetCopyAll(prefs);
874 if (sets != NULL) {
875 CFIndex n;
876
877 n = CFArrayGetCount(sets);
878 CFRelease(sets);
879 if (n > 0) {
880 SCPrint(TRUE, stdout, CFSTR("no current set\n"));
881 return;
882 }
883 }
884
885 bundle = _SC_CFBundleGet();
886 if (bundle != NULL) {
887 setName = CFBundleCopyLocalizedString(bundle,
888 CFSTR("DEFAULT_SET_NAME"),
889 CFSTR("Automatic"),
890 NULL);
891 }
892 if (setName == NULL) {
893 setName = CFSTR("Automatic");
894 CFRetain(setName);
895 }
896
897 set = SCNetworkSetCreate(prefs);
898 if (set == NULL) {
899 SCPrint(TRUE, stdout,
900 CFSTR("could not initialize \"%@\": %s\n"),
901 setName,
902 SCErrorString(SCError()));
903 CFRelease(setName);
904 return;
905 }
906
907 (void) SCNetworkSetSetName(set, setName);
908
909 ok = SCNetworkSetSetCurrent(set);
910 if (!ok) {
911 SCPrint(TRUE, stdout,
912 CFSTR("could not initialize \"%@\": %s\n"),
913 setName,
914 SCErrorString(SCError()));
915 (void) SCNetworkSetRemove(set);
916 CFRelease(setName);
917 CFRelease(set);
918 return;
919 }
920
921 if (net_set != NULL) CFRelease(net_set);
922 net_set = set;
923
924 setCreated = TRUE;
925
926 CFRelease(setName);
927 CFRetain(set);
928 }
929 }
930
931 setUpdated = SCNetworkSetEstablishDefaultConfiguration(set);
932 if (setUpdated) {
933 CFStringRef setName;
934
935 _prefs_changed = TRUE;
936
937 setName = SCNetworkSetGetName(set);
938 if (setName != NULL) {
939 SCPrint(TRUE, stdout,
940 CFSTR("set \"%@\" (%@) %supdated\n"),
941 setName,
942 SCNetworkSetGetSetID(set),
943 setCreated ? "created, selected, and " : "");
944 } else {
945 SCPrint(TRUE, stdout,
946 CFSTR("set ID \"%@\" %supdated\n"),
947 SCNetworkSetGetSetID(set),
948 setCreated ? "created, selected, and " : "");
949 }
950 }
951
952 CFRelease(set);
953 return;
954 }
955
956
957 #include "SCPreferencesInternal.h"
958 #include <fcntl.h>
959 #include <unistd.h>
960 __private_extern__
961 void
962 do_net_snapshot(int argc, char **argv)
963 {
964 if (prefs == NULL) {
965 SCPrint(TRUE, stdout, CFSTR("network configuration not open\n"));
966 return;
967 }
968
969 if (prefs != NULL) {
970 SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs;
971
972 if (prefsPrivate->prefs != NULL) {
973 int fd;
974 static int n_snapshot = 0;
975 char *path;
976 CFDataRef xmlData;
977
978 asprintf(&path, "/tmp/prefs_snapshot_%d", n_snapshot++);
979 (void)unlink(path);
980 fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
981 free(path);
982 if (fd == -1) {
983 SCPrint(TRUE, stdout, CFSTR("could not write snapshot: open() failed : %s\n"), strerror(errno));
984 return;
985 }
986
987 xmlData = CFPropertyListCreateXMLData(NULL, prefsPrivate->prefs);
988 if (xmlData != NULL) {
989 (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
990 CFRelease(xmlData);
991 } else {
992 SCPrint(TRUE, stdout, CFSTR("could not write snapshot: CFPropertyListCreateXMLData() failed\n"));
993 }
994
995 (void) close(fd);
996 } else {
997 SCPrint(TRUE, stdout, CFSTR("prefs have not been accessed\n"));
998 }
999 }
1000
1001 return;
1002 }