From 7aa34a92f1ad1b98998770bb422a9d7a16797a9f Mon Sep 17 00:00:00 2001 From: Michael Rash Date: Tue, 30 Sep 2014 22:33:44 -0400 Subject: [PATCH] [client] add strtoargv() to easily get an argv array for passing to execvpe() --- client/fwknop.c | 27 +++++---------------- client/http_resolve_host.c | 48 +++++++------------------------------- client/utils.c | 43 ++++++++++++++++++++++++++++++++++ client/utils.h | 1 + 4 files changed, 59 insertions(+), 60 deletions(-) diff --git a/client/fwknop.c b/client/fwknop.c index 0afade3d..0aa20d12 100644 --- a/client/fwknop.c +++ b/client/fwknop.c @@ -982,13 +982,9 @@ static int run_last_args(fko_cli_options_t *options, const char * const args_save_file) { FILE *args_file_ptr = NULL; - - int current_arg_ctr = 0; - int argc_new = 0; + int argc_new = 0, args_broken = 0; int i = 0; - char args_str[MAX_LINE_LEN] = {0}; - char arg_tmp[MAX_LINE_LEN] = {0}; char *argv_new[MAX_CMDLINE_ARGS]; /* should be way more than enough */ memset(argv_new, 0x0, sizeof(argv_new)); @@ -1007,27 +1003,16 @@ run_last_args(fko_cli_options_t *options, const char * const args_save_file) args_str[MAX_LINE_LEN-1] = '\0'; if (options->verbose) log_msg(LOG_VERBOSITY_NORMAL, "Executing: %s", args_str); - for (i=0; i < (int)strlen(args_str); i++) + if(strtoargv(args_str, argv_new, &argc_new, options) != 1) { - if (!isspace(args_str[i])) - { - arg_tmp[current_arg_ctr] = args_str[i]; - current_arg_ctr++; - } - else - { - arg_tmp[current_arg_ctr] = '\0'; - if (add_argv(argv_new, &argc_new, arg_tmp, options) != 1) - { - fclose(args_file_ptr); - return 0; - } - current_arg_ctr = 0; - } + args_broken = 1; } } fclose(args_file_ptr); + if(args_broken) + return 0; + /* Reset the options index so we can run through them again. */ optind = 0; diff --git a/client/http_resolve_host.c b/client/http_resolve_host.c index 3f724721..e0a68015 100644 --- a/client/http_resolve_host.c +++ b/client/http_resolve_host.c @@ -313,7 +313,6 @@ resolve_ip_https(fko_cli_options_t *options) char wget_ssl_cmd[MAX_URL_PATH_LEN] = {0}; /* for verbose logging only */ char *wget_argv[MAX_CMDLINE_ARGS]; /* for execvpe() with no environment */ - char *output_args[4] = {"--secure-protocol=auto", "--quiet", "-O", "-"}; int wget_argc=0; int pipe_fd[2]; pid_t pid=0; @@ -326,21 +325,11 @@ resolve_ip_https(fko_cli_options_t *options) if(options->wget_bin != NULL) { strlcpy(wget_ssl_cmd, options->wget_bin, sizeof(wget_ssl_cmd)); - if (add_argv(wget_argv, &wget_argc, options->wget_bin, options) != 1) - { - free_argv(wget_argv, &wget_argc); - return -1; - } } else { #ifdef WGET_EXE strlcpy(wget_ssl_cmd, WGET_EXE, sizeof(wget_ssl_cmd)); - if (add_argv(wget_argv, &wget_argc, WGET_EXE, options) != 1) - { - free_argv(wget_argv, &wget_argc); - return -1; - } #else log_msg(LOG_VERBOSITY_ERROR, "[*] Use --wget-cmd to specify path to the wget command."); @@ -354,30 +343,12 @@ resolve_ip_https(fko_cli_options_t *options) { strlcat(wget_ssl_cmd, " -U ", sizeof(wget_ssl_cmd)); strlcat(wget_ssl_cmd, options->http_user_agent, sizeof(wget_ssl_cmd)); - if (add_argv(wget_argv, &wget_argc, "-U", options) != 1) - { - free_argv(wget_argv, &wget_argc); - return -1; - } - if (add_argv(wget_argv, &wget_argc, options->http_user_agent, options) != 1) - { - free_argv(wget_argv, &wget_argc); - return -1; - } } /* We collect the IP from wget's stdout */ strlcat(wget_ssl_cmd, " --secure-protocol=auto --quiet -O - ", sizeof(wget_ssl_cmd)); - for (i=0; i < 4; i++) - { - if (add_argv(wget_argv, &wget_argc, output_args[i], options) != 1) - { - free_argv(wget_argv, &wget_argc); - return -1; - } - } if(options->resolve_url != NULL) { @@ -396,22 +367,18 @@ resolve_ip_https(fko_cli_options_t *options) /* tack on the original URL to the wget command */ strlcat(wget_ssl_cmd, options->resolve_url, sizeof(wget_ssl_cmd)); - if (add_argv(wget_argv, &wget_argc, options->resolve_url, options) != 1) - { - free_argv(wget_argv, &wget_argc); - return -1; - } } else { /* tack on the default URL to the wget command */ strlcat(wget_ssl_cmd, WGET_RESOLVE_URL_SSL, sizeof(wget_ssl_cmd)); - if (add_argv(wget_argv, &wget_argc, WGET_RESOLVE_URL_SSL, options) != 1) - { - free_argv(wget_argv, &wget_argc); - return -1; - } + } + + if(strtoargv(wget_ssl_cmd, wget_argv, &wget_argc, options) != 1) + { + log_msg(LOG_VERBOSITY_ERROR, "Error converting wget cmd str to argv"); + return(-1); } /* We drive wget to resolve the external IP via SSL. This may not @@ -421,6 +388,7 @@ resolve_ip_https(fko_cli_options_t *options) if(pipe(pipe_fd) < 0) { log_msg(LOG_VERBOSITY_ERROR, "[*] pipe() error"); + free_argv(wget_argv, &wget_argc); return -1; } @@ -435,6 +403,7 @@ resolve_ip_https(fko_cli_options_t *options) else if(pid == -1) { log_msg(LOG_VERBOSITY_INFO, "[*] Could not fork() for wget."); + free_argv(wget_argv, &wget_argc); return -1; } @@ -452,6 +421,7 @@ resolve_ip_https(fko_cli_options_t *options) { log_msg(LOG_VERBOSITY_INFO, "[*] Could not fdopen() pipe output file descriptor."); + free_argv(wget_argv, &wget_argc); return -1; } fclose(output); diff --git a/client/utils.c b/client/utils.c index 7a38fbaa..b567ad0e 100644 --- a/client/utils.c +++ b/client/utils.c @@ -275,6 +275,49 @@ proto_strtoint(const char *pr_str) return proto_int; } +int +strtoargv(char *args_str, char **argv_new, int *argc_new, + fko_cli_options_t *opts) +{ + int current_arg_ctr = 0, i; + char arg_tmp[MAX_LINE_LEN] = {0}; + + for (i=0; i < (int)strlen(args_str); i++) + { + if (!isspace(args_str[i])) + { + arg_tmp[current_arg_ctr] = args_str[i]; + current_arg_ctr++; + } + else + { + if(current_arg_ctr > 0) + { + arg_tmp[current_arg_ctr] = '\0'; + if (add_argv(argv_new, argc_new, arg_tmp, opts) != 1) + { + free_argv(argv_new, argc_new); + return 0; + } + current_arg_ctr = 0; + } + } + } + + /* pick up the last argument in the string + */ + if(current_arg_ctr > 0) + { + arg_tmp[current_arg_ctr] = '\0'; + if (add_argv(argv_new, argc_new, arg_tmp, opts) != 1) + { + free_argv(argv_new, argc_new); + return 0; + } + } + return 1; +} + int add_argv(char **argv_new, int *argc_new, const char *new_arg, fko_cli_options_t *opts) diff --git a/client/utils.h b/client/utils.h index e747b0c5..773abb05 100644 --- a/client/utils.h +++ b/client/utils.h @@ -59,6 +59,7 @@ int verify_file_perms_ownership(const char *file); int resolve_dest_adr(const char *dns_str, struct addrinfo *hints, char *ip_str, size_t ip_bufsize); short proto_inttostr(int proto, char *proto_str, size_t proto_size); short proto_strtoint(const char *pr_str); +int strtoargv(char *args_str, char **argv_new, int *argc_new, fko_cli_options_t *opts); int add_argv(char **argv_new, int *argc_new, const char *new_arg, fko_cli_options_t *opts); void free_argv(char **argv_new, int *argc_new);