Browse Source

Add the gfsec_error_string function

master
Damien Goutte-Gattat 5 years ago
parent
commit
ee382525e7
  1. 44
      src/gfsec-use.c
  2. 66
      src/secret.c
  3. 3
      src/secret.h

44
src/gfsec-use.c

@ -159,10 +159,9 @@ get_share_data(gfsec_share_t *share)
if ( rc == 0 ) {
gfsec_share_get_uri(share, buffer, sizeof(buffer));
if ( gfsec_share_set_data(share, data, len) != 0 ) {
warnx("Incorrect hash value for share %s\n", buffer);
if ( (rc = gfsec_share_set_data(share, data, len)) != 0 ) {
warnx("Cannot set data for share %s: %s", buffer, gfsec_error_string(rc));
free(data);
rc = -1;
}
else
printf("Found share data in %s\n", buffer);
@ -229,32 +228,11 @@ main(int argc, char **argv)
if ( get_config_file(cfg_file, cfg_path, sizeof(cfg_path)) == -1 )
err(EXIT_FAILURE, "Cannot find configuration file");
switch ( gfsec_read_config(&cfg, cfg_path, &u) ) {
case 0:
break;
case GFSEC_ERR_SYSTEM_ERROR:
err(EXIT_FAILURE, "Cannot parse configuration file");
break;
case GFSEC_ERR_CONFIG_LINE_TOO_LONG:
errx(EXIT_FAILURE, "%s: line %u: Line too long", cfg_path, u);
break;
case GFSEC_ERR_CONFIG_INVALID_URI:
errx(EXIT_FAILURE, "%s: line %u: Invalid URI", cfg_path, u);
break;
case GFSEC_ERR_CONFIG_INVALID_HASH:
errx(EXIT_FAILURE, "%s: line %u: Invalid hash", cfg_path, u);
break;
case GFSEC_ERR_CONFIG_UNKNOWN_SCHEME:
errx(EXIT_FAILURE, "%s: line %u: Unknown scheme", cfg_path, u);
break;
case GFSEC_ERR_CONFIG_INVALID_THRESHOLD:
errx(EXIT_FAILURE, "%s: line %u: Invalid threshold", cfg_path, u);
break;
}
if ( gfsec_read_config(&cfg, cfg_path, &u) != 0 ) {
errx(EXIT_FAILURE, "Cannot parse configuration file (error at line %u)", u);
if ( (c = gfsec_read_config(&cfg, cfg_path, &u)) != 0 ) {
if ( c == GFSEC_ERR_SYSTEM_ERROR )
err(EXIT_FAILURE, "Cannot read configuration file");
else
errx(EXIT_FAILURE, "%s (line %u): %s", cfg_path, u, gfsec_error_string(c));
}
if ( ! output_file && ! (output_file = cfg->filename) )
@ -263,11 +241,11 @@ main(int argc, char **argv)
for ( u = 0; u < cfg->n_shares; u++ )
get_share_data(cfg->shares[u]);
if ( gfsec_secret_can_combine(cfg) != 0 )
errx(EXIT_FAILURE, "Cannot reconstitute secret");
if ( (c = gfsec_secret_can_combine(cfg)) != 0 )
errx(EXIT_FAILURE, "Cannot reconstitute secret: %s", gfsec_error_string(c));
if ( gfsec_secret_combine(cfg) != 0 )
errx(EXIT_FAILURE, "Error while reconsituting secret");
if ( (c = gfsec_secret_combine(cfg)) != 0 )
errx(EXIT_FAILURE, "Error while reconstituting secret: %s", gfsec_error_string(c));
if ( write_file(output_file, cfg->data, cfg->len) == -1 )
err(EXIT_FAILURE, "Cannot write secret");

66
src/secret.c

@ -404,3 +404,69 @@ gfsec_secret_free(gfsec_secret_t *secret)
free(secret);
}
const char *
gfsec_error_string(int err)
{
const char *msg;
switch ( err ) {
case 0:
msg = "Success";
break;
case GFSEC_ERR_SYSTEM_ERROR:
msg = strerror(errno);
break;
case GFSEC_ERR_INVALID_CALL:
msg = "A function was improperly called";
break;
case GFSEC_ERR_NOT_ENOUGH_SHARES:
msg = "Not enough shares available to reconstitute the secret";
break;
case GFSEC_ERR_TOO_MANY_SHARES:
msg = "Too many shares";
break;
case GFSEC_ERR_SHARE_NOT_AVAILABLE:
msg = "A share is not available";
break;
case GFSEC_ERR_INVALID_LENGTH:
msg = "Shares have inconsistent lengths";
break;
case GFSEC_ERR_INVALID_SHARE:
msg = "Checksum failed for share data";
break;
case GFSEC_ERR_CONFIG_LINE_TOO_LONG:
msg = "Line too long";
break;
case GFSEC_ERR_CONFIG_INVALID_URI:
msg = "Invalid share URI";
break;
case GFSEC_ERR_CONFIG_INVALID_HASH:
msg = "Invalid hash value";
break;
case GFSEC_ERR_CONFIG_UNKNOWN_SCHEME:
msg = "Unknown URI scheme";
break;
case GFSEC_ERR_CONFIG_INVALID_THRESHOLD:
msg = "Invalid threshold";
break;
default: /* Should not happen. */
msg = "Unknown error";
break;
}
return msg;
}

3
src/secret.h

@ -119,6 +119,9 @@ gfsec_secret_split(gfsec_secret_t *, unsigned char);
void
gfsec_secret_free(gfsec_secret_t *);
const char *
gfsec_error_string(int);
#ifdef __cplusplus
}
#endif

Loading…
Cancel
Save