Browse Source

Do not use fixed-size buffer for config pathname.

Use a dynamically allocated buffer to store the full pathname to the
configuration file.
master
Damien Goutte-Gattat 4 months ago
parent
commit
d16b0d9dd7
  1. 51
      src/gfsec-split.c
  2. 44
      src/gfsec-use.c

51
src/gfsec-split.c

@ -214,34 +214,36 @@ interactive_loop(gfsec_secret_t *cfg)
errx(EXIT_FAILURE, _("Split cancelled"));
}
static int
transform_config_pathname(char *cfg, size_t len, const char *secret)
static char *
get_config_path(const char *config_file, const char *secret)
{
char *base = NULL;
int rc = 0;
char *config_path = NULL;
if ( ! *cfg )
if ( ! config_file )
base = get_file_basename(secret);
else if ( ! strchr(cfg, '/') && ! strchr(cfg, '.') )
base = xstrdup(cfg);
else if ( ! strchr(config_file, '/') && ! strchr(config_file, '.') )
base = (char *)config_file;
if ( base ) {
rc = snprintf(cfg, len, "%s/gfsecret", xdg->config_home);
if ( (unsigned)rc >= len - 6 - strlen(base) ) {
rc = -1;
errno = ENAMETOOLONG;
}
char *config_dir;
if ( rc != -1 )
rc = makedir(cfg, 0700);
xasprintf(&config_dir, "%s/gfsecret", xdg->config_home);
if ( makedir(config_dir, 0700) != -1 )
xasprintf(&config_path, "%s/%s.conf", config_dir, base);
if ( rc != -1 )
snprintf(cfg, len, "%s/gfsecret/%s.conf", xdg->config_home, base);
free(base);
free(config_dir);
if ( ! config_file )
free(base);
}
else {
/* Use the original path, but still make it into a dynamically
* allocated buffer so that we can inconditionally free the
* buffer at the end. */
config_path = xstrdup(config_file);
}
return rc;
return config_path;
}
static unsigned
@ -263,8 +265,7 @@ main(int argc, char **argv)
{
int c, list_mode, interactive;
unsigned threshold, keep_original;
char *secret_file, *output_file;
char config_path[255];
char *config_path, *secret_file, *output_file;
gfsec_secret_t *cfg;
struct option options[] = {
@ -287,8 +288,7 @@ main(int argc, char **argv)
setprogname(argv[0]);
threshold = 2;
keep_original = list_mode = interactive = 0;
output_file = NULL;
config_path[0] = '\0';
config_path = output_file = NULL;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
@ -329,9 +329,7 @@ main(int argc, char **argv)
break;
case 'c':
if ( strlen(optarg) >= sizeof(config_path) - 1 )
errx(EXIT_FAILURE, _("Path too long: %s"), optarg);
strcpy(config_path, optarg);
config_path = optarg;
break;
case 'k':
@ -396,7 +394,7 @@ main(int argc, char **argv)
errx(EXIT_FAILURE, _("Missing secret file"));
secret_file = argv[optind++];
if ( transform_config_pathname(config_path, sizeof(config_path), secret_file) == -1 )
if ( ! (config_path = get_config_path(config_path, secret_file)) )
err(EXIT_FAILURE, _("Cannot create configuration file"));
if ( file_exists(config_path) != -1 ) {
warn(_("Configuration file %s already exists"), config_path);
@ -446,6 +444,7 @@ main(int argc, char **argv)
}
gfsec_secret_free(cfg);
free(config_path);
return EXIT_SUCCESS;
}

44
src/gfsec-use.c

@ -35,6 +35,7 @@
#include <gcrypt.h>
#include <xmem.h>
#include <xdg.h>
#include "util.h"
@ -89,39 +90,19 @@ See the COPYING file or <http://www.gnu.org/licenses/gpl.html>.\n\
exit(EXIT_SUCCESS);
}
static int
get_config_file_in_dir(const char *filename, char *buffer, size_t len)
{
int rc = 0;
rc = snprintf(buffer, len, "%s/gfsecret/%s.conf", xdg->config_home, filename);
if ( (unsigned)rc >= len ) {
rc = -1;
errno = ENAMETOOLONG;
}
return rc;
}
static int
get_config_file(const char *filename, char *buffer, size_t len)
static char *
get_config_path(const char *filename)
{
int rc;
char *path;
if ( ! filename )
rc = get_config_file_in_dir("default", buffer, len);
xasprintf(&path, "%s/gfsecret.conf/default.conf", xdg->config_home);
else if ( file_exists(filename) == -1 )
rc = get_config_file_in_dir(filename, buffer, len);
else if ( strlen(filename) < len ) {
rc = 0;
strcpy(buffer, filename);
}
else {
rc = -1;
errno = ENAMETOOLONG;
}
xasprintf(&path, "%s/gfsecret.conf/%s.conf", xdg->config_home, filename);
else
path = xstrdup(filename);
return rc;
return path;
}
static int
@ -168,7 +149,7 @@ main(int argc, char **argv)
{
int c, keep;
const char *cfg_file, *output_file, *restore_cmd, *delete_cmd;
char cfg_path[255];
char *cfg_path;
gfsec_secret_t *cfg;
pid_t pid;
unsigned u;
@ -240,9 +221,7 @@ main(int argc, char **argv)
gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
if ( get_config_file(cfg_file, cfg_path, sizeof(cfg_path)) == -1 )
err(EXIT_FAILURE, _("Cannot find configuration file"));
cfg_path = get_config_path(cfg_file);
if ( (c = gfsec_read_config(&cfg, cfg_path, &u)) != 0 ) {
if ( c == GFSEC_ERR_SYSTEM_ERROR )
err(EXIT_FAILURE, _("Cannot read configuration file"));
@ -335,6 +314,7 @@ main(int argc, char **argv)
}
gfsec_secret_free(cfg);
free(cfg_path);
return EXIT_SUCCESS;
}

Loading…
Cancel
Save