diff options
| author | Luke Bratch <luke@bratch.co.uk> | 2019-05-11 22:11:42 +0100 | 
|---|---|---|
| committer | Luke Bratch <luke@bratch.co.uk> | 2019-05-11 22:11:42 +0100 | 
| commit | 34d410dc82e05f4255ec23a9deaff212b7903955 (patch) | |
| tree | 75f70464ca07411c85f79263af5cd55867ad9993 /config.c | |
| parent | 231656bf59da65036b171ce5ccc06eeadd64a05a (diff) | |
Implement authentication in the form of the bouncer having a configurable server password
Diffstat (limited to 'config.c')
| -rw-r--r-- | config.c | 47 | 
1 files changed, 46 insertions, 1 deletions
| @@ -1,6 +1,6 @@  #include "config.h" -// TODO - Multiple functions here (at least readnames() and relayseconds() have the file opening code, rewrite. +// TODO - Multiple functions here (at least readnames(), relayseconds() and checkpassword()) have the file opening code, rewrite.  // TODO - Can isconf() and getconf() just be merged into one function? @@ -136,5 +136,50 @@ int confrelayseconds() {      }    } +  fclose(fp);    return seconds;  } + +// Check the password provided in the string 'str' against what is in +// the config file. +// Return 0 for password mismatch, or 1 for password match. +int checkpassword(char *password) { +  FILE *fp; +  char str[MAXCHAR]; +  char* filename = "blabouncer.conf"; + +  fp = fopen(filename, "r"); + +  if (fp == NULL) { +    printf("error: could not open configuration file '%s'.\n", filename); +    exit(1); +  } + +  while (fgets(str, MAXCHAR, fp) != NULL) { +    long int len; +    if ((len = isconf(str, "password"))) { +      getconf(str, len); +      printf("confpassword is: '%s', length '%ld'.\n", str, strlen(str)); +      // Ensure password are the same length +      if (strlen(str) != strlen(password)) { +        printf("Password length mismatch!\n"); +        fclose(fp); +        return 0; +      } +      // Ensure passwords match +      if (strncmp(str, password, strlen(password)) == 0) { +        printf("confpassword matches password.\n"); +        fclose(fp); +        return 1; +      } else { +        printf("confpassword does NOT match password!\n"); +        fclose(fp); +        return 0; +      } +    } +  } + +  fclose(fp); +  printf("No password read from configuration file, quitting.\n"); +  exit(1); +} | 
