Browse Source

Move hash check in the get_share_data function

Avoid cluttering of main by moving the hash value computation and
comparison into the get_share_data function. If the comparison is
invalid, return as if we did not find any data.
develop
Damien Goutte-Gattat 5 years ago
parent
commit
619c4bdaa2
  1. 55
      src/gfsec-use.c

55
src/gfsec-use.c

@ -125,7 +125,7 @@ get_config_file(const char *filename, char *buffer, size_t len)
}
static int
get_share_data(gfsec_share_t *share)
get_share_data(gfsec_share_t *share, gcry_md_hd_t md)
{
int rc = -1;
@ -154,6 +154,21 @@ get_share_data(gfsec_share_t *share)
break;
}
if ( rc == 0 && share->sha256 ) {
unsigned char *md_val;
gcry_md_write(md, share->data, share->length);
md_val = gcry_md_read(md, 0);
if ( memcmp(share->sha256, md_val, 32) != 0 ) {
free(share->data);
share->data = NULL;
rc = -1;
}
gcry_md_reset(md);
}
return rc;
}
@ -171,24 +186,6 @@ get_share_display_name(gfsec_share_t *share)
return buffer;
}
static int
check_share(gfsec_share_t *share, gcry_md_hd_t md)
{
unsigned char *md_val;
int rc;
if ( ! share->sha256 )
return 0;
gcry_md_write(md, share->data, share->length);
md_val = gcry_md_read(md, 0);
rc = memcmp(share->sha256, md_val, 32) == 0 ? 0 : -1;
gcry_md_reset(md);
return rc;
}
int
main(int argc, char **argv)
{
@ -260,21 +257,13 @@ main(int argc, char **argv)
share = cfg->shares;
while ( share ) {
if ( get_share_data(share) == 0 ) {
if ( check_share(share, md) == -1 ) {
warnx("Share data in %s does not hash to the expected value",
get_share_display_name(share));
free(share->data);
share->data = NULL;
}
else {
if ( (share->flags & GFSEC_SHARE_FLAGS_FULL) > 0 )
have_full = 1;
else
have_shares += 1;
if ( get_share_data(share, md) == 0 ) {
if ( (share->flags & GFSEC_SHARE_FLAGS_FULL) > 0 )
have_full = 1;
else
have_shares += 1;
printf("Found share data in %s\n", get_share_display_name(share));
}
printf("Found share data in %s\n", get_share_display_name(share));
}
share = share->next;

Loading…
Cancel
Save