static void cmd_line(int argc, char *argv[]) { int optchar = 0; const char *program_name = NULL; struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {"prompt", no_argument, NULL, 'P'}, {"user", no_argument, NULL, 'u'}, {"role", no_argument, NULL, 'r'}, {"type", no_argument, NULL, 't'}, {"level", no_argument, NULL, 'l'}, /* compat. */ {"sensitivity", no_argument, NULL, 's'}, {"range", no_argument, NULL, 'm'}, {"clearance", no_argument, NULL, 'c'}, {"mls-range", no_argument, NULL, 'm'}, {"raw", no_argument, NULL, 'R'}, {"current", no_argument, NULL, 1}, {"self", no_argument, NULL, 1}, {"current-exec", no_argument, NULL, 2}, {"self-exec", no_argument, NULL, 2}, {"current-fs", no_argument, NULL, 3}, {"self-fs", no_argument, NULL, 3}, {"current-key", no_argument, NULL, 4}, {"self-key", no_argument, NULL, 4}, {"parent", no_argument, NULL, 5}, {"parent-exec", no_argument, NULL, 6}, {"parent-fs", no_argument, NULL, 7}, {"parent-key", no_argument, NULL, 8}, {"file", required_argument, NULL, 'f'}, {"link", required_argument, NULL, 'L'}, {"pid", required_argument, NULL, 'p'}, {"pid-exec", required_argument, NULL, 9}, {"pid-fs", required_argument, NULL, 10}, {"pid-key", required_argument, NULL, 11}, {NULL, 0, NULL, 0} }; int done = FALSE; program_name = opt_program_name(argv[0], SECON_CONF_PROG_NAME); while ((optchar = getopt_long(argc, argv, SECON_OPTS_GO, long_options, NULL)) != -1) { switch (optchar) { case '?': usage(program_name, EXIT_FAILURE); case 'h': usage(program_name, EXIT_SUCCESS); case 'V': fprintf(stdout, " %s version %s.\n", program_name, VERSION); exit(EXIT_SUCCESS); case 'u': done = TRUE; opts->disp_user = !opts->disp_user; break; case 'r': done = TRUE; opts->disp_role = !opts->disp_role; break; case 't': done = TRUE; opts->disp_type = !opts->disp_type; break; case 'l': done = TRUE; opts->disp_sen = !opts->disp_sen; break; case 's': done = TRUE; opts->disp_sen = !opts->disp_sen; break; case 'c': done = TRUE; opts->disp_clr = !opts->disp_clr; break; case 'm': done = TRUE; opts->disp_mlsr = !opts->disp_mlsr; break; case 'P': opts->disp_prompt = !opts->disp_prompt; break; case 'R': opts->disp_raw = !opts->disp_raw; break; case 1: opts->from_type = OPTS_FROM_CUR; break; case 2: opts->from_type = OPTS_FROM_CUREXE; break; case 3: opts->from_type = OPTS_FROM_CURFS; break; case 4: opts->from_type = OPTS_FROM_CURKEY; break; case 5: opts->from_type = OPTS_FROM_PROC; opts->f.pid = getppid(); break; case 6: opts->from_type = OPTS_FROM_PROCEXE; opts->f.pid = getppid(); break; case 7: opts->from_type = OPTS_FROM_PROCFS; opts->f.pid = getppid(); break; case 8: opts->from_type = OPTS_FROM_PROCKEY; opts->f.pid = getppid(); break; case 'f': opts->from_type = OPTS_FROM_FILE; opts->f.file = optarg; break; case 'L': opts->from_type = OPTS_FROM_LINK; opts->f.link = optarg; break; case 'p': opts->from_type = OPTS_FROM_PROC; opts->f.pid = atoi(optarg); break; case 9: opts->from_type = OPTS_FROM_PROCEXE; opts->f.pid = atoi(optarg); break; case 10: opts->from_type = OPTS_FROM_PROCFS; opts->f.pid = atoi(optarg); break; case 11: opts->from_type = OPTS_FROM_PROCKEY; opts->f.pid = atoi(optarg); break; default: assert(FALSE); } } if (!done) { /* defualt, if nothing specified */ opts->disp_user = TRUE; opts->disp_role = TRUE; opts->disp_type = TRUE; if (!opts->disp_prompt) { /* when displaying prompt, just output "normal" by default */ opts->disp_sen = TRUE; opts->disp_clr = TRUE; } opts->disp_mlsr = TRUE; } if (disp_none()) err(EXIT_FAILURE, " Nothing to display"); argc -= optind; argv += optind; if (!argc && (opts->from_type == OPTS_FROM_ARG) && !isatty(STDIN_FILENO)) opts->from_type = OPTS_FROM_STDIN; if (!argc && (opts->from_type == OPTS_FROM_ARG)) opts->from_type = OPTS_FROM_CUR; if (opts->from_type == OPTS_FROM_ARG) { opts->f.arg = argv[0]; if (xstreq(argv[0], "-")) opts->from_type = OPTS_FROM_STDIN; } else if (!is_selinux_enabled()) errx(EXIT_FAILURE, "SELinux is not enabled"); }
static void cl_cmd_line(int argc, char *argv[]) { char optchar = 0; const char *program_name = NULL; struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"clients", required_argument, NULL, 'c'}, {"debug", required_argument, NULL, 'd'}, {"execute", required_argument, NULL, 'e'}, {"host", required_argument, NULL, 'H'}, {"port", required_argument, NULL, 'P'}, {"nagle", optional_argument, NULL, 'n'}, {"timeout", required_argument, NULL, 't'}, {"version", no_argument, NULL, 'V'}, {NULL, 0, NULL, 0} }; Vstr_base *out = vstr_make_base(NULL); if (!out) errno = ENOMEM, err(EXIT_FAILURE, "command line"); program_name = opt_program_name(argv[0], "cntl"); while ((optchar = getopt_long(argc, argv, "c:de:hH:nP:Rt:V", long_options, NULL)) != -1) { switch (optchar) { case '?': usage(program_name, EXIT_FAILURE, ""); case 'h': usage(program_name, EXIT_SUCCESS, ""); case 'V': vstr_add_fmt(out, 0,"\ %s version 1.0.0, compiled on %s.\n\ Written by James Antill\n\ \n\ Uses Vstr string library.\n\ ", program_name, __DATE__); if (io_put_all(out, STDOUT_FILENO) == IO_FAIL) err(EXIT_FAILURE, "write"); exit (EXIT_SUCCESS); case 'c': server_clients = atoi(optarg); break; case 't': server_timeout = atoi(optarg); break; case 'd': vlg_debug(vlg); break; case 'e': /* use cmd line instead of stdin */ io_r_fd = -1; app_cstr_buf(io_r, optarg); app_cstr_buf(io_r, "\n"); break; case 'n': if (!optarg) { evnt_opt_nagle = !evnt_opt_nagle; } else if (!strcasecmp("true", optarg)) evnt_opt_nagle = TRUE; else if (!strcasecmp("1", optarg)) evnt_opt_nagle = TRUE; else if (!strcasecmp("false", optarg)) evnt_opt_nagle = FALSE; else if (!strcasecmp("0", optarg)) evnt_opt_nagle = FALSE; break; default: abort(); } } vstr_free_base(out); out = NULL; argc -= optind; argv += optind; if (argc != 1) usage(program_name, EXIT_FAILURE, ""); server_filename = argv[0]; }