Browse Source

Add first draft of the gfsec-split program

Add the gfsec-split program to split a secret into shares,
dispatch the shares to specific locations, and write a
configuration file allowing to reconstitute the secret using
the gfsec-use program.
master
Damien Goutte-Gattat 5 years ago
parent
commit
92a8ec08f5
  1. 20
      src/Makefile.am
  2. 210
      src/gfsec-split.c

20
src/Makefile.am

@ -1,11 +1,15 @@
bin_PROGRAMS = gfsec-use
gfsec_use_SOURCES = gfsec-use.c util.c util.h secret.c secret.h \
secretcfg.c secretcfg.h gfsec-errors.h \
schemes.h scheme-module.h scheme-module.c \
scheme-libmtp.c scheme-libmtp.h \
scheme-gio.c scheme-gio.h \
scheme-file.c scheme-file.h
bin_PROGRAMS = gfsec-use gfsec-split
common_sources = util.c util.h secret.c secret.h \
secretcfg.c secretcfg.h gfsec-errors.h \
schemes.h scheme-module.c scheme-module.h \
scheme-libmtp.c scheme-libmtp.h \
scheme-gio.c scheme-gio.h \
scheme-file.c scheme-file.h
gfsec_use_SOURCES = gfsec-use.c $(common_sources)
gfsec_split_SOURCES = gfsec-split.c $(common_sources)
AM_CPPFLAGS = -I$(top_srcdir)/lib $(LIBGFSHARE_CFLAGS) $(LIBMTP_CFLAGS) $(GIO_CFLAGS)
AM_LDFLAGS = -L$(top_builddir)/lib $(LIBGFSHARE_LIBS) $(LIBMTP_LIBS) $(GIO_LIBS)

210
src/gfsec-split.c

@ -0,0 +1,210 @@
/*
* gfsecret - Secret sharing tools
* Copyright (C) 2016 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <err.h>
#include <gcrypt.h>
#include "util.h"
#include "secretcfg.h"
#include "scheme-module.h"
static void
usage(int status)
{
puts("Usage: gfsec-split [options] file\n\
Split the specified file for later use with gfsec-use.\n");
puts("Options:\n\
-h, --help Display this help message.\n\
-v, --version Display the version message.\n");
puts("\
-n, --threshold N Specify the minimal number of shares\n\
required to re-assemble the secret.\n\
Default is 2.\n\
-s, --share URI Add a share.\n");
puts("\
-c, --config FILE Write configuration to the specified\n\
file. Default is the basename of the\n\
splitted file in gfsecret's configuration\n\
directory.\n");
puts("\
-k, --keep Do not delete the original file.\n");
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
exit(status);
}
static void
info(void)
{
printf("\
gfsec-split (%s %s)\n\
Copyright (C) 2016 Damien Goutte-Gattat\n\
\n\
This program is released under the GNU General Public License.\n\
See the COPYING file or <http://www.gnu.org/licenses/gpl.html>.\n\
", PACKAGE_NAME, VERSION);
exit(EXIT_SUCCESS);
}
static unsigned
get_uinteger_or_die(const char *arg)
{
unsigned val;
char *endptr;
errno = 0;
val = strtoul(arg, &endptr, 10);
if ( errno != 0 || endptr == arg )
errx(EXIT_FAILURE, "Invalid argument, unsigned integer expected: %s", arg);
return val;
}
int
main(int argc, char **argv)
{
int c;
unsigned threshold, keep_original;
char *output_base, *config_path;
gfsec_secret_t *cfg;
struct option options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ "treshold", 1, NULL, 'n' },
{ "share", 1, NULL, 's' },
{ "config", 1, NULL, 'c' },
{ "keep", 0, NULL, 'k' },
{ NULL, 0, NULL, 0 }
};
setprogname(argv[0]);
threshold = 2;
keep_original = 0;
output_base = config_path = NULL;
if ( ! (cfg = gfsec_secret_new()) )
err(EXIT_FAILURE, "Cannot create secret");
while ( (c = getopt_long(argc, argv, "hvn:s:c:k",
options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
usage(EXIT_SUCCESS);
break;
case '?':
usage(EXIT_FAILURE);
break;
case 'v':
info();
break;
case 'n':
threshold = get_uinteger_or_die(optarg);
break;
case 's':
if ( (c = gfsec_parse_uri(optarg, cfg, 1)) != 0 )
errx(EXIT_FAILURE, "Cannot add share: %s", gfsec_error_string(c));
break;
case 'c':
config_path = optarg;
break;
case 'k':
keep_original = 1;
break;
}
}
gfsec_scheme_module_init();
if ( ! gcry_check_version(GCRYPT_VERSION) )
errx(EXIT_FAILURE, "libgcrypt version mismatch");
gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
if ( optind >= argc )
errx(EXIT_FAILURE, "Missing secret file");
if ( ! output_base && ! (output_base = get_file_basename(argv[optind])) )
err(EXIT_FAILURE, "No output basename");
if ( ! config_path ) {
char *base;
if ( ! (base = get_file_basename(argv[optind])) )
err(EXIT_FAILURE, "Cannot get basename");
if ( asprintf(&config_path, "%s/gfsecret/%s.conf",
getenv("XDG_CONFIG_HOME"), base) == -1 )
err(EXIT_FAILURE, "Cannot construct a pathname for the configuration file");
free(base);
}
if ( (c = gfsec_secret_set_secret_file(cfg, argv[optind])) != 0 )
errx(EXIT_FAILURE, "Cannot set secret: %s", gfsec_error_string(c));
if ( (c = gfsec_secret_split(cfg, threshold)) != 0 )
errx(EXIT_FAILURE, "Cannot split secret: %s", gfsec_error_string(c));
for ( c = 0; c < cfg->n_shares; c++ ) {
gfsec_share_t *share;
share = cfg->shares[c];
if ( gfsec_scheme_module_put_file(share->scheme, share->authority,
share->path, share->data, share->len)
!= GFSEC_SCHEME_STATUS_SUCCESS )
errx(EXIT_FAILURE, "Cannot write share");
}
if ( (c = gfsec_write_config(cfg, config_path)) != 0 )
errx(EXIT_FAILURE, "Cannot write configuration file: %s",
gfsec_error_string(c));
gfsec_secret_free(cfg);
if ( ! keep_original )
unlink(argv[optind]);
return EXIT_SUCCESS;
}
Loading…
Cancel
Save