Browse Source

Add the gfsec_write_config function

Add a function to dump a secret object into a configuration file
which could later be parsed by the gfsec_read_config function.
develop
Damien Goutte-Gattat 5 years ago
parent
commit
45f78de66f
  1. 48
      src/secretcfg.c

48
src/secretcfg.c

@ -394,3 +394,51 @@ gfsec_read_config(gfsec_secret_t **cfg,
return rc;
}
/**
* Write a configuration file for the specified secret object.
*
* @param cfg The secret object to write a configuration for.
* @param filename The location of the file to write.
*
* @return
* - 0 if successful;
* - GFSEC_ERR_INVALID_CALL if an invalid pointer was passed;
* - GFSEC_ERR_SYSTEM_ERROR if an I/O error occured.
*/
int
gfsec_write_config(gfsec_secret_t *cfg, const char *filename)
{
FILE *f;
char buffer[33];
unsigned n, m;
static char *schemes[] = { "file://", "uuid://", "label://", "mtp://" };
if ( ! cfg || ! filename )
return GFSEC_ERR_INVALID_CALL;
if ( ! (f = fopen(filename, "w")) )
return GFSEC_ERR_SYSTEM_ERROR;
fprintf(f, "OUTFILE=%s\n", cfg->filename);
fprintf(f, "MINSHARES=%u\n", cfg->threshold);
for ( n = 0; n < cfg->n_shares; n++ ) {
gfsec_share_t *share = cfg->shares[n];
for ( m = 0; m < 32; m++ )
sprintf(buffer + (m*2), "%02x", share->hash[m]);
fprintf(f, "URI=%s%s%s?sha256=%s%s\n",
schemes[share->scheme],
share->authority ? share->authority : "",
share->path,
buffer,
gfsec_share_is_full(share) ? "&share=no" : "");
}
fclose(f);
return 0;
}
Loading…
Cancel
Save