Browse Source

Use fixed-size buffer for the configuration filename.

For consistency with gfsec-use.
master
Damien Goutte-Gattat 4 months ago
parent
commit
5f244c45d1
  1. 56
      src/gfsec-split.c

56
src/gfsec-split.c

@ -213,37 +213,50 @@ interactive_loop(gfsec_secret_t *cfg)
errx(EXIT_FAILURE, _("Split cancelled"));
}
static void
transform_config_pathname(char **cfg, const char *secret)
static int
transform_config_pathname(char *cfg, size_t len, const char *secret)
{
char *base = NULL;
int rc = 0;
if ( ! *cfg )
base = get_file_basename(secret);
else if ( ! strchr(*cfg, '/') && ! strchr(*cfg, '.') )
base = xstrdup(*cfg);
else if ( ! strchr(cfg, '/') && ! strchr(cfg, '.') )
base = xstrdup(cfg);
if ( base ) {
const char *env_info;
char *config_dir = NULL;
char config_dir[255];
if ( (env_info = getenv("XDG_CONFIG_HOME")) && *env_info)
xasprintf(&config_dir, "%s/gfsecret", env_info);
else if ( (env_info = getenv("HOME")) && *env_info )
xasprintf(&config_dir, "%s/.config/gfsecret", env_info);
rc = snprintf(config_dir, sizeof(config_dir), "%s/gfsecret", env_info);
else if ( (env_info = getenv("HOME")) && *env_info)
rc = snprintf(config_dir, sizeof(config_dir), "%s/.config/gfsecret", env_info);
else {
rc = -1;
errno = ENOENT;
}
if ( config_dir ) {
if ( makedir(config_dir, 0700) == -1 )
err(EXIT_FAILURE, _("Cannot create configuration directory"));
if ( rc != -1 && (unsigned)rc >= sizeof(config_dir) ) {
rc = -1;
errno = ENAMETOOLONG;
}
if ( rc != -1 )
rc = makedir(config_dir, 0700);
if ( rc != -1 )
rc = snprintf(cfg, len, "%s/%s.conf", config_dir, base);
xasprintf(cfg, "%s/%s.conf", config_dir, base);
free(config_dir);
if ( rc != -1 && (unsigned)rc >= len ) {
rc = -1;
errno = ENAMETOOLONG;
}
else
xasprintf(cfg, "%s.conf", base);
free(base);
}
return rc;
}
static unsigned
@ -265,7 +278,8 @@ main(int argc, char **argv)
{
int c, list_mode, interactive;
unsigned threshold, keep_original;
char *config_path, *secret_file, *output_file;
char *secret_file, *output_file;
char config_path[255];
gfsec_secret_t *cfg;
struct option options[] = {
@ -288,7 +302,8 @@ main(int argc, char **argv)
setprogname(argv[0]);
threshold = 2;
keep_original = list_mode = interactive = 0;
config_path = output_file = NULL;
output_file = NULL;
config_path[0] = '\0';
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
@ -327,7 +342,9 @@ main(int argc, char **argv)
break;
case 'c':
config_path = optarg;
if ( strlen(optarg) >= sizeof(config_path) - 1 )
errx(EXIT_FAILURE, _("Path too long: %s"), optarg);
strcpy(config_path, optarg);
break;
case 'k':
@ -392,7 +409,8 @@ main(int argc, char **argv)
errx(EXIT_FAILURE, _("Missing secret file"));
secret_file = argv[optind++];
transform_config_pathname(&config_path, secret_file);
if ( transform_config_pathname(config_path, sizeof(config_path), secret_file) == -1 )
err(EXIT_FAILURE, _("Cannot create configuration file"));
if ( file_exists(config_path) != -1 ) {
warn(_("Configuration file %s already exists"), config_path);
if ( ! ask_confirmation(_("Overwrite (y/N)?")) )

Loading…
Cancel
Save