Browse Source

Report errors when reading configuration file

master
Damien Goutte-Gattat 5 years ago
parent
commit
33b0a8d1cc
  1. 29
      src/gfsec-use.c
  2. 10
      src/secretcfg.c
  3. 2
      src/secretcfg.h

29
src/gfsec-use.c

@ -229,8 +229,33 @@ main(int argc, char **argv)
if ( get_config_file(cfg_file, cfg_path, sizeof(cfg_path)) == -1 )
err(EXIT_FAILURE, "Cannot find configuration file");
if ( gfsec_read_config(&cfg, cfg_path) != 0 )
errx(EXIT_FAILURE, "Cannot parse 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 ( ! output_file && ! (output_file = cfg->filename) )
errx(EXIT_FAILURE, "No output file specified");

10
src/secretcfg.c

@ -303,7 +303,9 @@ parse_uri(const char *uri, gfsec_secret_t *secret)
* of the configuration file is invalid.
*/
int
gfsec_read_config(gfsec_secret_t **cfg, const char *filename)
gfsec_read_config(gfsec_secret_t **cfg,
const char *filename,
unsigned int *line)
{
FILE *f;
char buffer[MAX_LINE_LENGTH];
@ -322,10 +324,16 @@ gfsec_read_config(gfsec_secret_t **cfg, const char *filename)
(*cfg)->threshold = 2;
if ( line )
*line = 0;
rc = 0;
while ( rc >= 0 ) {
ssize_t n;
if ( line )
*line += 1;
n = get_line(f, buffer, sizeof(buffer));
if ( n == GFSEC_ERR_SYSTEM_ERROR && feof(f) )
break; /* End of file. */

2
src/secretcfg.h

@ -26,7 +26,7 @@ extern "C" {
#endif
int
gfsec_read_config(gfsec_secret_t **, const char *);
gfsec_read_config(gfsec_secret_t **, const char *, unsigned int *);
#ifdef __cplusplus
}

Loading…
Cancel
Save