/* ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- */ static void nc_select(int argc, char **argv) { SCNetworkSetRef current_set; int exit_code = 1; SCNetworkServiceRef service = NULL; Boolean status; do_prefs_init(); /* initialization */ do_prefs_open(0, NULL); /* open default prefs */ current_set = SCNetworkSetCopyCurrent(prefs); if (current_set == NULL) { SCPrint(TRUE, stderr, CFSTR("No current location\n"), SCErrorString(SCError())); goto done; } service = nc_copy_service_from_arguments(argc, argv, current_set); if (service == NULL) { SCPrint(TRUE, stderr, CFSTR("No service\n")); goto done; } #if !TARGET_OS_IPHONE status = SCNetworkServiceSetEnabled(service, TRUE); if (!status) { SCPrint(TRUE, stderr, CFSTR("Unable to enable service: %s\n"), SCErrorString(SCError())); goto done; } #else status = SCNetworkSetSetSelectedVPNService(current_set, service); if (!status) { SCPrint(TRUE, stderr, CFSTR("Unable to select service: %s\n"), SCErrorString(SCError())); goto done; } #endif _prefs_save(); exit_code = 0; done: my_CFRelease(&service); my_CFRelease(¤t_set); _prefs_close(); exit(exit_code); }
static void nc_set_application_url(CFStringRef subtype, CFStringRef directory) { CFURLRef directory_url = NULL; CFDataRef directory_url_data = NULL; CFStringRef vpnprefpath = NULL; char *path = NULL; CFIndex path_len = 0; if (subtype == NULL || directory == NULL) { goto done; } directory_url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, directory, kCFURLPOSIXPathStyle, FALSE); if (directory_url == NULL) { SCPrint(TRUE, stderr, CFSTR("CFURLCreateWithFileSystemPath failed\n")); goto done; } directory_url_data = CFURLCreateBookmarkData(NULL, directory_url, 0, 0, 0, 0); if (directory_url_data == NULL) { SCPrint(TRUE, stderr, CFSTR("CFURLCreateBookmarkData failed\n")); goto done; } vpnprefpath = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@%@"), PREF_PREFIX, subtype, PREF_SUFFIX ); if (vpnprefpath == NULL) { SCPrint(TRUE, stderr, CFSTR("CFStringCreateWithFormat failed\n")); goto done; } path_len = CFStringGetLength(vpnprefpath) + 1; path = malloc(path_len); if (path == NULL) { goto done; } if (!CFStringGetCString(vpnprefpath, path, path_len, kCFStringEncodingASCII)) { SCPrint(TRUE, stderr, CFSTR("CFStringGetCString failed\n")); goto done; } do_prefs_init(); /* initialization */ do_prefs_open(1, &path); /* open prefs */ if (!SCPreferencesSetValue(prefs, CFSTR("ApplicationURL"), directory_url_data)) { SCPrint(TRUE, stderr, CFSTR("SCPreferencesSetValue ApplicationURL failed, %s\n"), SCErrorString(SCError())); goto done; } _prefs_save(); done: my_CFRelease(&directory_url); my_CFRelease(&directory_url_data); my_CFRelease(&vpnprefpath); if (path) { free(path); } _prefs_close(); exit(0); }
/* ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- */ static void nc_show(int argc, char **argv) { SCNetworkServiceRef service = NULL; SCDynamicStoreRef store = NULL; int exit_code = 1; CFStringRef serviceID = NULL; CFStringRef iftype = NULL; CFStringRef ifsubtype = NULL; CFStringRef type_entity_key = NULL; CFStringRef subtype_entity_key = NULL; CFDictionaryRef type_entity_dict = NULL; CFDictionaryRef subtype_entity_dict = NULL; CFStringRef vpnprefpath = NULL; #if !TARGET_OS_IPHONE CFDataRef bookmarkData = NULL; CFURLRef directory = NULL; Boolean isStale = FALSE; char *path = NULL; CFIndex path_len = 0; #endif service = nc_copy_service_from_arguments(argc, argv, NULL); if (service == NULL) { SCPrint(TRUE, stderr, CFSTR("No service\n")); exit(exit_code); } serviceID = SCNetworkServiceGetServiceID(service); nc_get_service_type_and_subtype(service, &iftype, &ifsubtype); if (!CFEqual(iftype, kSCEntNetPPP) && !CFEqual(iftype, kSCEntNetIPSec) && !CFEqual(iftype, kSCEntNetVPN)) { SCPrint(TRUE, stderr, CFSTR("Not a connection oriented service: %@\n"), serviceID); goto done; } type_entity_key = SCDynamicStoreKeyCreateNetworkServiceEntity(NULL, kSCDynamicStoreDomainSetup, serviceID, iftype); nc_print_VPN_service(service); #if !TARGET_OS_IPHONE vpnprefpath = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@%@"), PREF_PREFIX, ifsubtype, PREF_SUFFIX); if (vpnprefpath == NULL) { goto skipURL; } path_len = CFStringGetLength(vpnprefpath) + 1; path = malloc(path_len); if (path == NULL) { goto skipURL; } if (!CFStringGetCString(vpnprefpath, path, path_len, kCFStringEncodingASCII)) { SCPrint(TRUE, stderr, CFSTR("CFStringGetCString failed\n")); goto done; } do_prefs_init(); /* initialization */ do_prefs_open(1, &path); /* open prefs */ bookmarkData = SCPreferencesGetValue(prefs, CFSTR("ApplicationURL")); if (bookmarkData == NULL) { goto skipURL; } directory = CFURLCreateByResolvingBookmarkData(kCFAllocatorDefault, bookmarkData, 0, NULL, NULL, &isStale, NULL); if (directory == NULL) { goto skipURL; } SCPrint(TRUE, stdout, CFSTR("ApplicationURL: %@\n"), directory); skipURL: #endif store = SCDynamicStoreCreate(NULL, CFSTR("scutil --nc"), NULL, NULL); if (store == NULL) { SCPrint(TRUE, stderr, CFSTR("Unable to create dynamic store: %s\n"), SCErrorString(SCError())); goto done; } type_entity_dict = SCDynamicStoreCopyValue(store, type_entity_key); if (!type_entity_dict) { SCPrint(TRUE, stderr, CFSTR("No \"%@\" configuration available\n"), iftype); } else { SCPrint(TRUE, stdout, CFSTR("%@ %@\n"), iftype, type_entity_dict); } if (ifsubtype) { subtype_entity_key = SCDynamicStoreKeyCreateNetworkServiceEntity(NULL, kSCDynamicStoreDomainSetup, serviceID, ifsubtype); subtype_entity_dict = SCDynamicStoreCopyValue(store, subtype_entity_key); if (!subtype_entity_dict) { // } else { SCPrint(TRUE, stdout, CFSTR("%@ %@\n"), ifsubtype, subtype_entity_dict); } } exit_code = 0; done: my_CFRelease(&type_entity_key); my_CFRelease(&type_entity_dict); my_CFRelease(&subtype_entity_key); my_CFRelease(&subtype_entity_dict); my_CFRelease(&store); my_CFRelease(&service); my_CFRelease(&vpnprefpath); _prefs_close(); exit(exit_code); }
int main(int argc, char * const argv[]) { Boolean doDNS = FALSE; Boolean doNet = FALSE; Boolean doNWI = FALSE; Boolean doPrefs = FALSE; Boolean doProxy = FALSE; Boolean doReach = FALSE; Boolean doSnap = FALSE; char *get = NULL; char *log = NULL; extern int optind; int opt; int opti; const char *prog = argv[0]; char *renew = NULL; char *set = NULL; char *nc_cmd = NULL; InputRef src; int timeout = 15; /* default timeout (in seconds) */ char *wait = NULL; Boolean watch = FALSE; int xStore = 0; /* non dynamic store command line options */ /* process any arguments */ while ((opt = getopt_long(argc, argv, "dDvprt:w:W", longopts, &opti)) != -1) switch(opt) { case 'd': _sc_debug = TRUE; _sc_log = FALSE; /* enable framework logging */ break; case 'D': doDispatch = TRUE; break; case 'v': _sc_verbose = TRUE; _sc_log = FALSE; /* enable framework logging */ break; case 'p': enablePrivateAPI = TRUE; break; case 'r': doReach = TRUE; xStore++; break; case 't': timeout = atoi(optarg); break; case 'w': wait = optarg; xStore++; break; case 'W': watch = TRUE; break; case 0: if (strcmp(longopts[opti].name, "dns") == 0) { doDNS = TRUE; xStore++; } else if (strcmp(longopts[opti].name, "get") == 0) { get = optarg; xStore++; } else if (strcmp(longopts[opti].name, "nc") == 0) { nc_cmd = optarg; xStore++; } else if (strcmp(longopts[opti].name, "net") == 0) { doNet = TRUE; xStore++; } else if (strcmp(longopts[opti].name, "nwi") == 0) { doNWI = TRUE; xStore++; } else if (strcmp(longopts[opti].name, "prefs") == 0) { doPrefs = TRUE; xStore++; } else if (strcmp(longopts[opti].name, "proxy") == 0) { doProxy = TRUE; xStore++; } else if (strcmp(longopts[opti].name, "renew") == 0) { renew = optarg; xStore++; } else if (strcmp(longopts[opti].name, "set") == 0) { set = optarg; xStore++; } else if (strcmp(longopts[opti].name, "snapshot") == 0) { doSnap = TRUE; xStore++; } else if (strcmp(longopts[opti].name, "log") == 0) { log = optarg; xStore++; } else if (strcmp(longopts[opti].name, "user") == 0) { username = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingUTF8); } else if (strcmp(longopts[opti].name, "password") == 0) { password = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingUTF8); } else if (strcmp(longopts[opti].name, "secret") == 0) { sharedsecret = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingUTF8); } break; case '?': default : usage(prog); } argc -= optind; argv += optind; if (xStore > 1) { // if we are attempting to process more than one type of request usage(prog); } /* are we checking (or watching) the reachability of a host/address */ if (doReach) { if (argc < 1) { usage(prog); } if (watch) { do_watchReachability(argc, (char **)argv); } else { do_checkReachability(argc, (char **)argv); } /* NOT REACHED */ } /* are we waiting on the presense of a dynamic store key */ if (wait) { do_wait(wait, timeout); /* NOT REACHED */ } /* are we looking up the DNS configuration */ if (doDNS) { do_showDNSConfiguration(argc, (char **)argv); /* NOT REACHED */ } if (doNWI) { do_nwi(argc, (char**)argv); /* NOT REACHED */ } if (doSnap) { if (!enablePrivateAPI #if !TARGET_IPHONE_SIMULATOR || (geteuid() != 0) #endif // !TARGET_IPHONE_SIMULATOR ) { usage(prog); } do_open(0, NULL); /* open the dynamic store */ do_snapshot(argc, (char**)argv); exit(0); } /* are we looking up a preference value */ if (get) { if (argc != 2) { if (findPref(get) < 0) { usage(prog); } } else { /* need to go back one argument * for the filename */ argc++; argv--; } do_getPref(get, argc, (char **)argv); /* NOT REACHED */ } /* are we looking up the proxy configuration */ if (doProxy) { do_showProxyConfiguration(argc, (char **)argv); /* NOT REACHED */ } /* are we changing a preference value */ if (set) { if (findPref(set) < 0) { usage(prog); } do_setPref(set, argc, (char **)argv); /* NOT REACHED */ } /* verbose log */ if (log != NULL) { if (strcasecmp(log, "IPMonitor")) { usage(prog); } do_log(log, argc, (char * *)argv); /* NOT REACHED */ } /* network connection commands */ if (nc_cmd) { if (find_nc_cmd(nc_cmd) < 0) { usage(prog); } do_nc_cmd(nc_cmd, argc, (char **)argv, watch); /* NOT REACHED */ } if (doNet) { /* if we are going to be managing the network configuration */ commands = (cmdInfo *)commands_net; nCommands = nCommands_net; if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) { usage(prog); } do_net_init(); /* initialization */ do_net_open(argc, (char **)argv); /* open prefs */ } else if (doPrefs) { /* if we are going to be managing the network configuration */ commands = (cmdInfo *)commands_prefs; nCommands = nCommands_prefs; do_dictInit(0, NULL); /* start with an empty dictionary */ do_prefs_init(); /* initialization */ do_prefs_open(argc, (char **)argv); /* open prefs */ } else { /* if we are going to be managing the dynamic store */ commands = (cmdInfo *)commands_store; nCommands = nCommands_store; do_dictInit(0, NULL); /* start with an empty dictionary */ do_open(0, NULL); /* open the dynamic store */ } /* are we trying to renew a DHCP lease */ if (renew != NULL) { do_renew(renew); /* NOT REACHED */ } /* allocate command input stream */ src = (InputRef)CFAllocatorAllocate(NULL, sizeof(Input), 0); src->fp = stdin; src->el = NULL; src->h = NULL; if (isatty(fileno(src->fp))) { int editmode = 1; HistEvent ev; struct termios t; if (tcgetattr(fileno(src->fp), &t) != -1) { if ((t.c_lflag & ECHO) == 0) { editmode = 0; } } src->el = el_init(prog, src->fp, stdout, stderr); src->h = history_init(); (void)history(src->h, &ev, H_SETSIZE, INT_MAX); el_set(src->el, EL_HIST, history, src->h); if (!editmode) { el_set(src->el, EL_EDITMODE, 0); } el_set(src->el, EL_EDITOR, "emacs"); el_set(src->el, EL_PROMPT, prompt); el_source(src->el, NULL); if ((el_get(src->el, EL_EDITMODE, &editmode) != -1) && editmode != 0) { el_set(src->el, EL_SIGNAL, 1); } else { history_end(src->h); src->h = NULL; el_end(src->el); src->el = NULL; } } while (TRUE) { Boolean ok; ok = process_line(src); if (!ok) { break; } } /* close the socket, free resources */ if (src->h) history_end(src->h); if (src->el) el_end(src->el); (void)fclose(src->fp); CFAllocatorDeallocate(NULL, src); exit (EX_OK); // insure the process exit status is 0 return 0; // ...and make main fit the ANSI spec. }