Browse Source

Add the -o, --output option

Allow to override the OUTFILE parameter in the configuration file
with a -o, --output option on the command line. Also detect if no
output file has been specified, either in the configuration file
or on the command line.
master
Damien Goutte-Gattat 6 years ago
parent
commit
6af8342be0
  1. 24
      src/gfsec-use.c

24
src/gfsec-use.c

@ -54,7 +54,9 @@ secret is destroyed when the command or the shell terminates.\n");
puts("\
-c, --config FILE Read configuration from the specified\n\
file.\n");
file.\n\
-o, --output FILE Write reconstructed secret in the specified\n\
file (override configuration file).\n");
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
@ -172,7 +174,7 @@ int
main(int argc, char **argv)
{
int c;
const char *cfg_file;
const char *cfg_file, *output_file;
char cfg_path[255];
gfsec_secret_config_t *cfg;
gfsec_share_t *share;
@ -183,14 +185,15 @@ main(int argc, char **argv)
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ "config", 1, NULL, 'c' },
{ "output", 1, NULL, 'o' },
{ NULL, 0, NULL, 0 }
};
setprogname(argv[0]);
cfg_file = NULL;
cfg_file = output_file = NULL;
have_shares = have_full = 0;
while ( (c = getopt_long(argc, argv, "hvc:", options, NULL)) != -1 ) {
while ( (c = getopt_long(argc, argv, "hvc:o:", options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
usage(EXIT_SUCCESS);
@ -207,6 +210,10 @@ main(int argc, char **argv)
case 'c':
cfg_file = optarg;
break;
case 'o':
output_file = optarg;
break;
}
}
@ -218,6 +225,9 @@ main(int argc, char **argv)
if ( ! (cfg = gfsec_read_config(cfg_path)) )
err(EXIT_FAILURE, "Cannot parse configuration file");
if ( ! output_file && ! (output_file = cfg->output_file) )
errx(EXIT_FAILURE, "No output file specified");
share = cfg->shares;
while ( share ) {
@ -242,7 +252,7 @@ main(int argc, char **argv)
}
printf("Using full data in %s\n", get_share_display_name(share));
if ( write_file(cfg->output_file, share->data, share->length) == -1 )
if ( write_file(output_file, share->data, share->length) == -1 )
err(EXIT_FAILURE, "Cannot write secret");
}
else if ( have_shares >= cfg->threshold ) {
@ -283,7 +293,7 @@ main(int argc, char **argv)
}
gfshare_ctx_dec_extract(ctx, secret);
if ( write_file(cfg->output_file, secret, size) == -1 )
if ( write_file(output_file, secret, size) == -1 )
err(EXIT_FAILURE, "Cannot write secret");
memset(secret, 0, size);
@ -328,7 +338,7 @@ main(int argc, char **argv)
printf("Removing secret.\n");
unlink(cfg->output_file);
unlink(output_file);
}
gfsec_destroy_config(cfg);

Loading…
Cancel
Save