]> git.saurik.com Git - apple/configd.git/blobdiff - scutil.tproj/net.c
configd-802.40.13.tar.gz
[apple/configd.git] / scutil.tproj / net.c
index 31ff83161d839206d430a2586ff45da6c9fbdf06..db88330e4d6160ce44cc88a5f1998735bbeac640 100644 (file)
@@ -1,15 +1,15 @@
 /*
- * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
+ * Copyright (c) 2004-2007, 2009-2011, 2014 Apple Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
- * 
+ *
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
  * compliance with the License. Please obtain a copy of the License at
  * http://www.opensource.apple.com/apsl/ and read it before using this
  * file.
- * 
+ *
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
@@ -17,7 +17,7 @@
  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  * Please see the License for the specific language governing rights and
  * limitations under the License.
- * 
+ *
  * @APPLE_LICENSE_HEADER_END@
  */
 
@@ -61,7 +61,7 @@ __private_extern__ CFNumberRef                        CFNumberRef_1   = NULL;
 
 
 __private_extern__
-CFNumberRef
+CF_RETURNS_RETAINED CFNumberRef
 _copy_number(const char *arg)
 {
        int     val;
@@ -251,10 +251,12 @@ _process_options(optionsRef options, int nOptions, int argc, char **argv, CFMuta
 
                                if        ((strcasecmp(argv[0], "disable") == 0) ||
                                           (strcasecmp(argv[0], "no"     ) == 0) ||
+                                          (strcasecmp(argv[0], "off"    ) == 0) ||
                                           (strcasecmp(argv[0], "0"      ) == 0)) {
                                        CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), CFNumberRef_0);
                                } else if ((strcasecmp(argv[0], "enable") == 0) ||
                                           (strcasecmp(argv[0], "yes"   ) == 0) ||
+                                          (strcasecmp(argv[0], "on"   ) == 0) ||
                                           (strcasecmp(argv[0], "1"     ) == 0)) {
                                        CFDictionarySetValue(newConfiguration, *(options[optionIndex].key), CFNumberRef_1);
                                } else {
@@ -401,11 +403,20 @@ _show_entity(CFDictionaryRef entity, CFStringRef prefix)
                key   = CFArrayGetValueAtIndex(sorted, i);
                value = CFDictionaryGetValue(entity, key);
                if (isA_CFArray(value)) {
-                       CFStringRef     str;
+                       CFIndex         i;
+                       CFIndex         n       = CFArrayGetCount(value);
+
+                       SCPrint(TRUE, stdout, CFSTR("%@    %@ = ("), prefix, key);
+                       for (i = 0; i < n; i++) {
+                               CFTypeRef       val;
 
-                       str = CFStringCreateByCombiningStrings(NULL, value, CFSTR(", "));
-                       SCPrint(TRUE, stdout, CFSTR("%@    %@ = (%@)\n"), prefix, key, str);
-                       CFRelease(str);
+                               val = CFArrayGetValueAtIndex(value, i);
+                               SCPrint(TRUE, stdout,
+                                       CFSTR("%s%@"),
+                                       (i > 0) ? ", " : "",
+                                       val);
+                       }
+                       SCPrint(TRUE, stdout, CFSTR(")\n"));
                } else {
                        SCPrint(TRUE, stdout, CFSTR("%@    %@ = %@\n"), prefix, key, value);
                }
@@ -681,8 +692,9 @@ do_net_create(int argc, char **argv)
                return;
        }
 
-       if (*net_keys[i].create == NULL) {
+       if (net_keys[i].create == NULL) {
                SCPrint(TRUE, stderr, CFSTR("create what?\n"));
+               return;
        }
 
        (*net_keys[i].create)(argc, argv);
@@ -707,8 +719,9 @@ do_net_disable(int argc, char **argv)
                return;
        }
 
-       if (*net_keys[i].disable == NULL) {
+       if (net_keys[i].disable == NULL) {
                SCPrint(TRUE, stderr, CFSTR("disable what?\n"));
+               return;
        }
 
        (*net_keys[i].disable)(argc, argv);
@@ -733,8 +746,9 @@ do_net_enable(int argc, char **argv)
                return;
        }
 
-       if (*net_keys[i].enable == NULL) {
+       if (net_keys[i].enable == NULL) {
                SCPrint(TRUE, stderr, CFSTR("enable what?\n"));
+               return;
        }
 
        (*net_keys[i].enable)(argc, argv);
@@ -742,6 +756,124 @@ do_net_enable(int argc, char **argv)
 }
 
 
+static void
+do_net_migrate_perform(int argc, char **argv)
+{
+       char * sourceConfiguration = NULL;
+       char * targetConfiguration = NULL;
+       char * currentConfiguration = NULL;
+       CFStringRef str = NULL;
+       CFURLRef sourceConfigurationURL = NULL;
+       CFURLRef targetConfigurationURL = NULL;
+       CFURLRef currentConfigurationURL = NULL;
+       CFArrayRef migrationFiles = NULL;
+
+       sourceConfiguration = argv[0];
+       targetConfiguration = argv[1];
+
+       if (argc == 3) {
+               currentConfiguration = argv[2];
+       }
+
+       SCPrint(_sc_debug, stdout, CFSTR("sourceConfiguration: %s\ntargetConfiguration: %s\ncurrentConfiguration: %s\n"),
+               sourceConfiguration, targetConfiguration, (currentConfiguration != NULL) ? currentConfiguration : "<current system>" );
+
+       str = CFStringCreateWithCString(NULL, sourceConfiguration, kCFStringEncodingUTF8);
+       sourceConfigurationURL = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, TRUE);
+       CFRelease(str);
+
+       str = CFStringCreateWithCString(NULL, targetConfiguration, kCFStringEncodingUTF8);
+       targetConfigurationURL = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, TRUE);
+       CFRelease(str);
+
+       if (currentConfiguration != NULL) {
+               str = CFStringCreateWithCString(NULL, currentConfiguration, kCFStringEncodingUTF8);
+               currentConfigurationURL = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, TRUE);
+               CFRelease(str);
+       }
+
+       migrationFiles = _SCNetworkConfigurationPerformMigration(sourceConfigurationURL, currentConfigurationURL, targetConfigurationURL, NULL);
+
+       if (migrationFiles != NULL) {
+               SCPrint(TRUE, stdout, CFSTR("Migration Successful: %@ \n"), migrationFiles);
+       }
+       else {
+               SCPrint(TRUE, stdout, CFSTR("Migration Unsuccessful \n"));
+       }
+
+       if (sourceConfigurationURL != NULL) {
+               CFRelease(sourceConfigurationURL);
+       }
+       if (targetConfigurationURL != NULL) {
+               CFRelease(targetConfigurationURL);
+       }
+       if (currentConfigurationURL != NULL) {
+               CFRelease(currentConfigurationURL);
+       }
+       if (migrationFiles != NULL) {
+               CFRelease(migrationFiles);
+       }
+}
+
+
+static void
+do_net_migrate_validate(int argc, char **argv)
+{
+       char *configuration = NULL;
+       CFURLRef configurationURL = NULL;
+       char *expectedConfiguration = NULL;
+       CFURLRef expectedConfigurationURL = NULL;
+       Boolean isValid = FALSE;
+       CFStringRef str = NULL;
+       
+       configuration = argv[0];
+       str = CFStringCreateWithCString(NULL, configuration, kCFStringEncodingUTF8);
+       configurationURL = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, TRUE);
+       CFRelease(str);
+       
+       expectedConfiguration = argv[1];
+       str = CFStringCreateWithCString(NULL, expectedConfiguration, kCFStringEncodingUTF8);
+       expectedConfigurationURL = CFURLCreateWithFileSystemPath(NULL, str, kCFURLPOSIXPathStyle, TRUE);
+       CFRelease(str);
+       
+       isValid = _SCNetworkMigrationAreConfigurationsIdentical(configurationURL, expectedConfigurationURL);
+
+       SCPrint(TRUE, stdout, CFSTR("Configuration at location %s %s\n"), configuration, isValid ? "is valid" : "is NOT valid");
+
+       if (configurationURL != NULL) {
+               CFRelease(configurationURL);
+       }
+       if (expectedConfigurationURL != NULL) {
+               CFRelease(expectedConfigurationURL);
+       }
+}
+
+
+__private_extern__
+void
+do_net_migrate(int argc, char **argv)
+{
+       char *key;
+       SCPrint(TRUE, stdout, CFSTR("do_net_migrate called, %d\n"), argc);
+
+       key = argv[0];
+       argv++;
+       argc--;
+
+       if (strncmp(key, "perform", strlen(key)) == 0) {
+               do_net_migrate_perform(argc, argv);
+       }
+       else if (strncmp(key, "validate", strlen(key)) == 0) {
+               do_net_migrate_validate(argc, argv);
+       }
+       else {
+               SCPrint(TRUE, stderr, CFSTR("migrate what?\n"));
+               return;
+       }
+
+}
+
+
 __private_extern__
 void
 do_net_remove(int argc, char **argv)
@@ -759,8 +891,9 @@ do_net_remove(int argc, char **argv)
                return;
        }
 
-       if (*net_keys[i].remove == NULL) {
+       if (net_keys[i].remove == NULL) {
                SCPrint(TRUE, stderr, CFSTR("remove what?\n"));
+               return;
        }
 
        (*net_keys[i].remove)(argc, argv);
@@ -787,6 +920,7 @@ do_net_select(int argc, char **argv)
 
        if (*net_keys[i].select == NULL) {
                SCPrint(TRUE, stderr, CFSTR("select what?\n"));
+               return;
        }
 
        (*net_keys[i].select)(argc, argv);
@@ -867,6 +1001,7 @@ do_net_update(int argc, char **argv)
 
                                n = CFArrayGetCount(sets);
                                CFRelease(sets);
+                               sets = NULL;
                                if (n > 0) {
                                        SCPrint(TRUE, stdout, CFSTR("no current set\n"));
                                        return;
@@ -913,7 +1048,6 @@ do_net_update(int argc, char **argv)
                        net_set = set;
 
                        setCreated = TRUE;
-                       setUpdated = TRUE;
 
                        CFRelease(setName);
                        CFRetain(set);
@@ -976,12 +1110,12 @@ do_net_snapshot(int argc, char **argv)
                                return;
                        }
 
-                       xmlData = CFPropertyListCreateXMLData(NULL, prefsPrivate->prefs);
+                       xmlData = CFPropertyListCreateData(NULL, prefsPrivate->prefs, kCFPropertyListXMLFormat_v1_0, 0, NULL);
                        if (xmlData != NULL) {
                                (void) write(fd, CFDataGetBytePtr(xmlData), CFDataGetLength(xmlData));
                                CFRelease(xmlData);
                        } else {
-                               SCPrint(TRUE, stdout, CFSTR("could not write snapshot: CFPropertyListCreateXMLData() failed\n"));
+                               SCPrint(TRUE, stdout, CFSTR("could not write snapshot: CFPropertyListCreateData() failed\n"));
                        }
 
                        (void) close(fd);