Browse Source

Add specific error codes for configuration reading

Replace the GFSEC_ERR_INVALID_CONFIG error code by a set of more
specific error codes.
develop
Damien Goutte-Gattat 5 years ago
parent
commit
7c86674074
  1. 6
      src/secret.h
  2. 22
      src/secretcfg.c

6
src/secret.h

@ -26,7 +26,11 @@
#define GFSEC_ERR_SHARE_NOT_AVAILABLE -5
#define GFSEC_ERR_INVALID_LENGTH -6
#define GFSEC_ERR_INVALID_SHARE -7
#define GFSEC_ERR_INVALID_CONFIG -8
#define GFSEC_ERR_CONFIG_LINE_TOO_LONG -9
#define GFSEC_ERR_CONFIG_INVALID_URI -10
#define GFSEC_ERR_CONFIG_INVALID_HASH -11
#define GFSEC_ERR_CONFIG_UNKNOWN_SCHEME -12
#define GFSEC_ERR_CONFIG_INVALID_THRESHOLD -13
#define GFSEC_SHARE_NUMBER_FULL 0
#define GFSEC_SHARE_NUMBER_AUTOASSIGN 256

22
src/secretcfg.c

@ -53,7 +53,7 @@ get_line(FILE *f, char *buffer, size_t len)
if ( n >= len - 1 ) { /* Line too long */
while ( (c = fgetc(f)) != '\n' && c != EOF ) ; /* Discard line */
return GFSEC_ERR_INVALID_CONFIG;
return GFSEC_ERR_CONFIG_LINE_TOO_LONG;
}
buffer[n++] = (char) c;
@ -87,7 +87,7 @@ parse_authority(const char **uri, char **authority)
return 0;
}
else
return GFSEC_ERR_INVALID_CONFIG;
return GFSEC_ERR_CONFIG_INVALID_URI;
}
/**
@ -135,7 +135,7 @@ parse_sha256(const char *hex, size_t len, unsigned char **sha256)
unsigned n;
if ( len != 64 )
return GFSEC_ERR_INVALID_CONFIG;
return GFSEC_ERR_CONFIG_INVALID_HASH;
if ( ! (*sha256 = malloc(32)) )
return GFSEC_ERR_SYSTEM_ERROR;
@ -155,7 +155,7 @@ parse_sha256(const char *hex, size_t len, unsigned char **sha256)
/* Invalid character in the hash. */
free(*sha256);
*sha256 = NULL;
return GFSEC_ERR_INVALID_CONFIG;
return GFSEC_ERR_CONFIG_INVALID_HASH;
}
if ( n % 2 == 0 )
@ -188,7 +188,7 @@ parse_parameter(const char **uri, gfsec_share_t *share)
return 0; /* Silently ignore terminal delimiter. */
if ( ! (eq = strchr(*uri, '=')) )
return GFSEC_ERR_INVALID_CONFIG;
return GFSEC_ERR_CONFIG_INVALID_URI;
if ( strncmp(*uri, "share", eq - *uri) == 0 ) {
amp = strchrnul(++eq, '&');
@ -264,7 +264,7 @@ parse_uri(const char *uri, gfsec_secret_t *secret)
else if ( strncmp(p, "mtp://", 6) == 0 && (p += 6) )
share->scheme = GFSEC_SCHEME_MTP;
else
rc = GFSEC_ERR_INVALID_CONFIG;
rc = GFSEC_ERR_CONFIG_UNKNOWN_SCHEME;
if ( rc == 0 )
rc = parse_authority(&p, &(share->authority));
@ -277,7 +277,7 @@ parse_uri(const char *uri, gfsec_secret_t *secret)
if ( rc == 0 && ! gfsec_share_is_full(share) ) {
if ( (share->number = get_share_number_from_path(share->path)) == 0 )
rc = GFSEC_ERR_INVALID_CONFIG;
rc = GFSEC_ERR_CONFIG_INVALID_URI;
}
if ( rc == 0 )
@ -297,10 +297,10 @@ parse_uri(const char *uri, gfsec_secret_t *secret)
* - GFSEC_ERR_INVALID_CALL if an invalid pointer was passed;
* - GFSEC_ERR_SYSTEM_ERROR if an error occured when reading
* the file or allocating memory (check errno for details);
* - GFSEC_ERR_INVALID_CONFIG if the configuration file does
* not meet the expected syntax;
* - GFSEC_ERR_TOO_MANY_SHARES if the configuration describes
* a secret with too many shares (more than 255).
* a secret with too many shares (more than 255);
* - one of the GFSEC_ERR_CONFIG_* error codes if the syntax
* of the configuration file is invalid.
*/
int
gfsec_read_config(gfsec_secret_t **cfg, const char *filename)
@ -343,7 +343,7 @@ gfsec_read_config(gfsec_secret_t **cfg, const char *filename)
l = strtoul(buffer + 10, &endptr, 10);
if ( *endptr != '\0' || l < 2 || l > 255 )
rc = GFSEC_ERR_INVALID_CONFIG;
rc = GFSEC_ERR_CONFIG_INVALID_THRESHOLD;
else
(*cfg)->threshold = (unsigned char) l;
}

Loading…
Cancel
Save