Skip to content

Commit

Permalink
Use correct has_arg when parsing command line options with getopt_long (
Browse files Browse the repository at this point in the history
#530)

* all enroll options require arguments. closes #528.
* all dump options require arguments. closes #503.
* check for required enroll options. closes #320.
* ensure at least one identity option is specified
  • Loading branch information
scareything committed Oct 19, 2022
1 parent 1298888 commit 91b276f
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions programs/ziti-edge-tunnel/ziti-edge-tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1670,8 +1670,8 @@ static struct option run_options[] = {
static struct option run_host_options[] = {
{ "identity", required_argument, NULL, 'i' },
{ "identity-dir", required_argument, NULL, 'I'},
{ "verbose", optional_argument, NULL, 'v'},
{ "refresh", optional_argument, NULL, 'r'},
{ "verbose", required_argument, NULL, 'v'},
{ "refresh", required_argument, NULL, 'r'},
};

#ifndef DEFAULT_DNS_CIDR
Expand All @@ -1686,6 +1686,7 @@ static int run_opts(int argc, char *argv[]) {

int c, option_index, errors = 0;
optind = 0;
bool identity_provided = false;

while ((c = getopt_long(argc, argv, "i:I:v:r:d:u:",
run_options, &option_index)) != -1) {
Expand All @@ -1694,10 +1695,12 @@ static int run_opts(int argc, char *argv[]) {
struct cfg_instance_s *inst = calloc(1, sizeof(struct cfg_instance_s));
inst->cfg = strdup(optarg);
LIST_INSERT_HEAD(&load_list, inst, _next);
identity_provided = true;
break;
}
case 'I':
config_dir = optarg;
identity_provided = true;
break;
case 'v':
setenv("ZITI_LOG", optarg, true);
Expand Down Expand Up @@ -1731,18 +1734,21 @@ static int run_host_opts(int argc, char *argv[]) {

int c, option_index, errors = 0;
optind = 0;
bool identity_provided = false;

while ((c = getopt_long(argc, argv, "i:I:v:r:",
run_options, &option_index)) != -1) {
run_host_options, &option_index)) != -1) {
switch (c) {
case 'i': {
struct cfg_instance_s *inst = calloc(1, sizeof(struct cfg_instance_s));
inst->cfg = strdup(optarg);
LIST_INSERT_HEAD(&load_list, inst, _next);
identity_provided = true;
break;
}
case 'I':
config_dir = optarg;
identity_provided = true;
break;
case 'v':
setenv("ZITI_LOG", optarg, true);
Expand All @@ -1757,7 +1763,7 @@ static int run_host_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
if (errors > 0 || !identity_provided) {
commandline_help(stderr);
exit(1);
}
Expand Down Expand Up @@ -1950,9 +1956,9 @@ static int parse_enroll_opts(int argc, char *argv[]) {
static struct option opts[] = {
{"jwt", required_argument, NULL, 'j'},
{"identity", required_argument, NULL, 'i'},
{"key", optional_argument, NULL, 'k'},
{"cert", optional_argument, NULL, 'c'},
{ "name", optional_argument, NULL, 'n'}
{"key", required_argument, NULL, 'k'},
{"cert", required_argument, NULL, 'c'},
{ "name", required_argument, NULL, 'n'}
};
int c, option_index, errors = 0;
optind = 0;
Expand Down Expand Up @@ -1982,7 +1988,7 @@ static int parse_enroll_opts(int argc, char *argv[]) {
}
}
}
if (errors > 0) {
if (errors > 0 || enroll_opts.jwt == NULL || enroll_opts.enroll_key == NULL) {
commandline_help(stderr);
exit(1);
}
Expand Down Expand Up @@ -2038,8 +2044,8 @@ static tunnel_command *cmd;

static int dump_opts(int argc, char *argv[]) {
static struct option opts[] = {
{"identity", optional_argument, NULL, 'i'},
{"dump_path", optional_argument, NULL, 'p'},
{"identity", required_argument, NULL, 'i'},
{"dump_path", required_argument, NULL, 'p'},
};
int c, option_index, errors = 0;
optind = 0;
Expand Down Expand Up @@ -2510,9 +2516,9 @@ static int set_log_level_opts(int argc, char *argv[]) {

static int update_tun_ip_opts(int argc, char *argv[]) {
static struct option opts[] = {
{"tunip", optional_argument, NULL, 't'},
{"prefixlength", optional_argument, NULL, 'p'},
{"addDNS", optional_argument, NULL, 'd'},
{"tunip", required_argument, NULL, 't'},
{"prefixlength", required_argument, NULL, 'p'},
{"addDNS", required_argument, NULL, 'd'},
};
int c, option_index, errors = 0;
optind = 0;
Expand Down

0 comments on commit 91b276f

Please sign in to comment.