From 998fb96f0bfdb893de0b10ddfcc3cd2655abe5d2 Mon Sep 17 00:00:00 2001 From: Michael Rash Date: Wed, 16 Dec 2015 18:59:10 -0800 Subject: [PATCH] promote argv handling functions to fko_util (avoids duplication across client and server) --- client/fwknop.c | 4 +- client/http_resolve_host.c | 2 +- client/utils.c | 93 -------------------------------------- client/utils.h | 2 - common/common.h | 4 -- common/fko_util.c | 84 ++++++++++++++++++++++++++++++++++ common/fko_util.h | 6 +++ server/extcmd.c | 4 +- server/utils.c | 93 -------------------------------------- server/utils.h | 3 -- 10 files changed, 95 insertions(+), 200 deletions(-) diff --git a/client/fwknop.c b/client/fwknop.c index 95e8d3b7..d88fdb4e 100644 --- a/client/fwknop.c +++ b/client/fwknop.c @@ -995,7 +995,7 @@ run_last_args(fko_cli_options_t *options, const char * const args_save_file) { FILE *args_file_ptr = NULL; int argc_new = 0, args_broken = 0; - char args_str[MAX_LINE_LEN] = {0}; + char args_str[MAX_ARGS_LINE_LEN] = {0}; char *argv_new[MAX_CMDLINE_ARGS]; /* should be way more than enough */ memset(argv_new, 0x0, sizeof(argv_new)); @@ -1014,7 +1014,7 @@ 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); - if(strtoargv(args_str, argv_new, &argc_new, options) != 1) + if(strtoargv(args_str, argv_new, &argc_new) != 1) { args_broken = 1; } diff --git a/client/http_resolve_host.c b/client/http_resolve_host.c index 0e509f13..e464de45 100644 --- a/client/http_resolve_host.c +++ b/client/http_resolve_host.c @@ -412,7 +412,7 @@ resolve_ip_https(fko_cli_options_t *options) #endif #if HAVE_EXECVPE - if(strtoargv(wget_ssl_cmd, wget_argv, &wget_argc, options) != 1) + if(strtoargv(wget_ssl_cmd, wget_argv, &wget_argc) != 1) { log_msg(LOG_VERBOSITY_ERROR, "Error converting wget cmd str to argv"); return(-1); diff --git a/client/utils.c b/client/utils.c index e800b5e0..a6db5852 100644 --- a/client/utils.c +++ b/client/utils.c @@ -288,97 +288,4 @@ proto_strtoint(const char *pr_str) return proto_int; } -static int -add_argv(char **argv_new, int *argc_new, - const char *new_arg, fko_cli_options_t *opts) -{ - int buf_size = 0; - - if(opts->verbose > 2) - log_msg(LOG_VERBOSITY_NORMAL, "[+] add_argv() + arg: %s", new_arg); - - buf_size = strlen(new_arg) + 1; - argv_new[*argc_new] = calloc(1, buf_size); - - if(argv_new[*argc_new] == NULL) - { - log_msg(LOG_VERBOSITY_ERROR, "[*] Memory allocation error."); - return 0; - } - strlcpy(argv_new[*argc_new], new_arg, buf_size); - - *argc_new += 1; - - if(*argc_new >= MAX_CMDLINE_ARGS-1) - { - log_msg(LOG_VERBOSITY_ERROR, "[*] max command line args exceeded."); - return 0; - } - - argv_new[*argc_new] = NULL; - - return 1; -} - -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; -} - -void -free_argv(char **argv_new, int *argc_new) -{ - int i; - - if(argv_new == NULL || *argv_new == NULL) - return; - - for (i=0; i < *argc_new; i++) - { - if(argv_new[i] == NULL) - break; - else - free(argv_new[i]); - } - return; -} - /***EOF***/ diff --git a/client/utils.h b/client/utils.h index c721a499..6866146b 100644 --- a/client/utils.h +++ b/client/utils.h @@ -60,7 +60,5 @@ int resolve_dst_addr(const char *dns_str, struct addrinfo *hints, char *ip_str, size_t ip_bufsize, fko_cli_options_t *opts); 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); -void free_argv(char **argv_new, int *argc_new); #endif /* UTILS_H */ diff --git a/common/common.h b/common/common.h index 37291360..05d97726 100644 --- a/common/common.h +++ b/common/common.h @@ -161,10 +161,6 @@ enum { #define MAX_KEY_LEN 128 #define MAX_B64_KEY_LEN 180 -/* Command line argument / argv handling -*/ -#define MAX_CMDLINE_ARGS 30 /*!< should be way more than enough */ - #if HAVE_LIBFIU #define MAX_FAULT_TAG_LEN 128 #endif diff --git a/common/fko_util.c b/common/fko_util.c index 32adca78..81722d94 100644 --- a/common/fko_util.c +++ b/common/fko_util.c @@ -655,6 +655,90 @@ void chop_spaces(char *str) return; } +static int +add_argv(char **argv_new, int *argc_new, const char *new_arg) +{ + int buf_size = 0; + + buf_size = strlen(new_arg) + 1; + argv_new[*argc_new] = calloc(1, buf_size); + + if(argv_new[*argc_new] == NULL) + return 0; + + strlcpy(argv_new[*argc_new], new_arg, buf_size); + + *argc_new += 1; + + if(*argc_new >= MAX_CMDLINE_ARGS-1) + return 0; + + argv_new[*argc_new] = NULL; + + return 1; +} + +int +strtoargv(const char * const args_str, char **argv_new, int *argc_new) +{ + int current_arg_ctr = 0, i; + char arg_tmp[MAX_ARGS_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) != 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) != 1) + { + free_argv(argv_new, argc_new); + return 0; + } + } + return 1; +} + +void +free_argv(char **argv_new, int *argc_new) +{ + int i; + + if(argv_new == NULL || *argv_new == NULL) + return; + + for (i=0; i < *argc_new; i++) + { + if(argv_new[i] == NULL) + break; + else + free(argv_new[i]); + } + return; +} + + /** * @brief Dump a FKO context to a buffer * diff --git a/common/fko_util.h b/common/fko_util.h index 4988770b..ad697293 100644 --- a/common/fko_util.h +++ b/common/fko_util.h @@ -33,6 +33,9 @@ #include "fko.h" +#define MAX_CMDLINE_ARGS 30 /*!< should be way more than enough */ +#define MAX_ARGS_LINE_LEN 1024 + /* Function prototypes */ int is_valid_encoded_msg_len(const int len); @@ -58,6 +61,9 @@ void chop_newline(char *str); void chop_char(char *str, const char chop); void chop_spaces(char *str); +int strtoargv(const char * const args_str, char **argv_new, int *argc_new); +void free_argv(char **argv_new, int *argc_new); + #if !HAVE_STRLCAT size_t strlcat(char *dst, const char *src, size_t siz); #endif diff --git a/server/extcmd.c b/server/extcmd.c index 5e003429..77cb13e0 100644 --- a/server/extcmd.c +++ b/server/extcmd.c @@ -148,7 +148,7 @@ _run_extcmd(uid_t uid, gid_t gid, const char *cmd, char *so_buf, */ memset(argv_new, 0x0, sizeof(argv_new)); - if(strtoargv(cmd, argv_new, &argc_new, opts) != 1) + if(strtoargv(cmd, argv_new, &argc_new) != 1) { log_msg(LOG_ERR, "run_extcmd(): Error converting cmd str to argv via strtoargv()"); @@ -598,7 +598,7 @@ int _run_extcmd_write(const char *cmd, const char *cmd_write, int *pid_status, */ memset(argv_new, 0x0, sizeof(argv_new)); - if(strtoargv(cmd, argv_new, &argc_new, opts) != 1) + if(strtoargv(cmd, argv_new, &argc_new) != 1) { log_msg(LOG_ERR, "run_extcmd_write(): Error converting cmd str to argv via strtoargv()"); diff --git a/server/utils.c b/server/utils.c index 95835ad9..4d63c381 100644 --- a/server/utils.c +++ b/server/utils.c @@ -263,99 +263,6 @@ is_digits(const char * const str) return 1; } -static int -add_argv(char **argv_new, int *argc_new, - const char *new_arg, const fko_srv_options_t * const opts) -{ - int buf_size = 0; - - if(opts->verbose > 3) - log_msg(LOG_INFO, "[+] add_argv() + arg: %s", new_arg); - - buf_size = strlen(new_arg) + 1; - argv_new[*argc_new] = calloc(1, buf_size); - - if(argv_new[*argc_new] == NULL) - { - log_msg(LOG_INFO, "[*] Memory allocation error."); - return 0; - } - strlcpy(argv_new[*argc_new], new_arg, buf_size); - - *argc_new += 1; - - if(*argc_new >= MAX_CMDLINE_ARGS-1) - { - log_msg(LOG_ERR, "[*] max command line args exceeded."); - return 0; - } - - argv_new[*argc_new] = NULL; - - return 1; -} - -int -strtoargv(const char * const args_str, char **argv_new, int *argc_new, - const fko_srv_options_t * const 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; -} - -void -free_argv(char **argv_new, int *argc_new) -{ - int i; - - if(argv_new == NULL || *argv_new == NULL) - return; - - for (i=0; i < *argc_new; i++) - { - if(argv_new[i] == NULL) - break; - else - free(argv_new[i]); - } - return; -} - void clean_exit(fko_srv_options_t *opts, unsigned int fw_cleanup_flag, unsigned int exit_status) { diff --git a/server/utils.h b/server/utils.h index c3dc2063..73a7cf2c 100644 --- a/server/utils.h +++ b/server/utils.h @@ -68,8 +68,5 @@ int is_valid_file(const char *path); int verify_file_perms_ownership(const char *file); void truncate_partial_line(char *str); int is_digits(const char * const str); -int strtoargv(const char * const args_str, char **argv_new, int *argc_new, - const fko_srv_options_t * const opts); -void free_argv(char **argv_new, int *argc_new); #endif /* UTILS_H */