예제 #1
0
/* -----------------------------------------------------------------------------
----------------------------------------------------------------------------- */
void
do_nc_cmd(char *cmd, int argc, char **argv, Boolean watch)
{
	int	i;

	i = find_nc_cmd(cmd);
	if (i >= 0) {
		nc_func	func;

		func = nc_cmds[i].func;
		if (watch) {
			if (func == nc_status) {
				func = nc_watch;
			} else if (func == nc_ondemand) {
				ondemandwatch = TRUE;
			}
		}
		(*func)(argc, argv);
	}
	return;
}
예제 #2
0
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.
}