[client] add strtoargv() to easily get an argv array for passing to execvpe()

This commit is contained in:
Michael Rash 2014-09-30 22:33:44 -04:00
parent bf3319e0ba
commit 7aa34a92f1
4 changed files with 59 additions and 60 deletions

View File

@ -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;

View File

@ -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 <path> 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);

View File

@ -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)

View File

@ -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);