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