diff options
author | Luke Bratch <luke@bratch.co.uk> | 2019-05-12 20:56:55 +0100 |
---|---|---|
committer | Luke Bratch <luke@bratch.co.uk> | 2019-05-12 20:56:55 +0100 |
commit | e18e670f43f0e7b126f209c040126aa02511fa32 (patch) | |
tree | 798e297304c0ce051847359ebf32af63503b67c8 /config.c | |
parent | 34836ab2c9124e17d80bd8e8a1601398297391a8 (diff) |
Completely rewrite configuration file reading to remove lots of duplicated code and to simplify things. Alter everything that called the old functions.
Diffstat (limited to 'config.c')
-rw-r--r-- | config.c | 259 |
1 files changed, 80 insertions, 179 deletions
@@ -1,227 +1,128 @@ #include "config.h" -// TODO - Multiple functions here (at least readnames(), replayseconds() and checkpassword()) have the file opening code, rewrite. +// Sets 'dest' to the value of the configuration option with name +// 'confname' from configuration file 'filename'. +// Returns 1 for success or 0 for error/failure. +int getconfstr(char *confname, char *filename, char* dest) { + FILE *fp; + char *ret; + char str[MAXCHAR]; -// TODO - Can isconf() and getconf() just be merged into one function? + // Set strings to zero-length to begin + dest[0] = '\0'; -// Check to see if a line is the configuration option specified by -// checking to see if str starts with conf followed by a space, a tab, -// or an equals sign. -// Returns the length of the name of the requested option if found, or -// zero if not. -long int isconf(char *str, char *conf) { - char *ret; - char substr[MAXCHAR]; - long int len = strlen(conf); + // Length of requested configuration option name + long int namelen = strlen(confname); - // Ensure the next character is an equals sign, a space, or a tab - if (str[len] != '=' && str[len] != ' ' && str[len] != ' ') { - return 0; + fp = fopen(filename, "r"); + + if (fp == NULL) { + printf("error: could not open configuration file '%s'.\n", filename); + exit(1); } + // Loop through the whole file, looking for the requested configuration option + while (fgets(str, MAXCHAR, fp) != NULL) { + char substr[MAXCHAR]; - strncpy(substr, str, len); - substr[len] = '\0'; + // Check if the next character after the length of the requested option + // name is an equals sign, a space, or a tab + if (str[namelen] != '=' && str[namelen] != ' ' && str[namelen] != ' ') { + // If it isn't this can't have been our option + continue; + } + // Copy the number of characters that the requested option name is long + // to a temporary string + strncpy(substr, str, namelen); + substr[namelen] = '\0'; - if (!(ret = strstr(substr, conf)) == 0) { - return len; + // If the resulting temporary string contains the requested option name, + // we have found our configuration option and it is in the current 'str' + if (!(ret = strstr(substr, confname)) == 0) { + break; + } } - return 0; -} + // If we got here, then either we found the configuration option or we ran out of file + + // If str is NULL then we ran out of file + if (ret == NULL) { + printf("Error reading configuration option '%s', did not find it in configuration file '%s'.\n", confname, filename); + return 0; + } -// Extracts the value of a configuration option from a string. Expects -// to receive the string and the number of characters long that the -// configuration option name is. -void getconf(char *str, long int start) { - long int len = strlen(str); long int pos; - char conf[MAXCHAR]; + char conf[MAXCHAR]; // Temporary string to build configuration value in - for (long int i = start; i < len; i++) { + // Starting from the end of the option name, find the position of the start of the configuration value + // (including its double quotes) by skipping over everything that isn't an equals sign, a space, or a tab + for (size_t i = namelen; i < strlen(str); i++) { if (str[i] == '=' || str[i] == ' ' || str[i] == ' ') { continue; } else { + // Record current/final position in string pos = i; break; } } - strncpy(conf, str + pos, len - pos - 1); // Copy remainder to new string and lop off the newline - conf[len - pos - 1] = '\0'; // Null terminate + strncpy(conf, str + pos, strlen(str) - pos - 1); // Copy remainder to new string and lop off the newline + conf[strlen(str) - pos - 1] = '\0'; // Null terminate // Check for start and end quotes if (conf[0] != '"' || conf[strlen(conf) - 1] != '"') { - printf("Error reading configuration option, not enclosed in double quotes!\n"); + printf("Error reading configuration option '%s', not enclosed in double quotes in configuration file '%s'!\n", confname, filename); exit(1); } - strncpy(str, conf + 1, strlen(conf) - 2); // Copy result back to original string without quotes - str[strlen(conf) - 2] = '\0'; // Null terminate -} - -int readnames(char *nick, char *username, char *realname) { - FILE *fp; - char str[MAXCHAR]; - char* filename = "blabouncer.conf"; + strncpy(dest, conf + 1, strlen(conf) - 2); // Copy result to destination string without quotes + dest[strlen(conf) - 2] = '\0'; // Null terminate - // Set all strings to zero-length to begin - nick[0] = '\0'; - username[0] = '\0'; - realname[0] = '\0'; - - 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, "nick"))) { - getconf(str, len); - strncpy(nick, str, strlen(str)); - nick[strlen(str)] = '\0'; - printf("Nick is: '%s', length '%ld'.\n", nick, strlen(nick)); - } else if ((len = isconf(str, "username"))) { - getconf(str, len); - strncpy(username, str, strlen(str)); - username[strlen(str)] = '\0'; - printf("Username is: '%s', length '%ld'.\n", username, strlen(username)); - } else if ((len = isconf(str, "realname"))) { - getconf(str, len); - strncpy(realname, str, strlen(str)); - realname[strlen(str)] = '\0'; - printf("Real name is: '%s', length '%ld'.\n", realname, strlen(realname)); - } - } - - // Ensure we have everything required - if (!strlen(nick) || !strlen(username) || !strlen(realname)) { - printf("Failed to read nick and/or username and/or real name from configuration file.\n"); - exit(1); - } + printf("getconfstr(): returning '%s'.\n", dest); + // Close fine and return success fclose(fp); return 1; } -// Return the replayseconds configuration option -// (How many seconds of replay should be sent to connecting clients) -int confreplayseconds() { - FILE *fp; - char str[MAXCHAR]; - char* filename = "blabouncer.conf"; - - char secondsstr[MAXCHAR]; - int seconds; - - fp = fopen(filename, "r"); - - if (fp == NULL) { - printf("error: could not open configuration file '%s'.\n", filename); +// Returns the value of the configuration option with name +// 'confname' from configuration file 'filename'. +int getconfint(char *confname, char *filename) { + char result[MAXCHAR]; + if (!getconfstr(confname, filename, result)) { + printf("getconfint(): error getting configuration option '%s' from configuration file '%s'.\n", confname, filename); + // TODO - Do something useful here instead of exiting exit(1); } - while (fgets(str, MAXCHAR, fp) != NULL) { - long int len; - if ((len = isconf(str, "replayseconds"))) { - getconf(str, len); - strncpy(secondsstr, str, strlen(str)); - secondsstr[strlen(str)] = '\0'; - printf("secondsstr is: '%s', length '%ld'.\n", secondsstr, strlen(secondsstr)); - seconds = strtol(secondsstr, NULL, 10); // Convert resulting string to an integer, base 10 - printf("seconds is: '%d'.\n", seconds); - } - } - - fclose(fp); - return seconds; + return strtol(result, NULL, 10); // Convert resulting string to an integer, base 10 } // Check the password provided in the string 'str' against what is in -// the config file. +// the configuration file 'filename'. // 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); -} +int checkpassword(char *password, char *filename) { + char confpassword[MAXCHAR]; -int confclientport(char *clientport) { - FILE *fp; - char str[MAXCHAR]; - char* filename = "blabouncer.conf"; - - // Set all strings to zero-length to begin - clientport[0] = '\0'; - - 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, "clientport"))) { - getconf(str, len); - strncpy(clientport, str, strlen(str)); - clientport[strlen(str)] = '\0'; - printf("clientport is: '%s', length '%ld'.\n", clientport, strlen(clientport)); - } + if (!getconfstr("password", filename, confpassword)) { + printf("checkpassword(): error getting configuration option 'password' from configuration file '%s'.\n", filename); + return 0; } - // Ensure we have everything required - if (!strlen(clientport)) { - printf("Failed to read clientport from configuration file.\n"); - exit(1); + // Ensure passwords are the same length + if (strlen(password) != strlen(confpassword)) { + printf("Password length mismatch!\n"); + return 0; } - - int portint = strtol(clientport, NULL, 10); // Convert resulting string to an integer, base 10 - // Ensure port is valid - if (portint < 1 || portint > 65535) { - printf("Invalid clientport value in configuration file.\n"); - exit(1); + // Ensure passwords match + if (strncmp(password, confpassword, strlen(password)) == 0) { + printf("confpassword matches password.\n"); + return 1; + } else { + printf("confpassword does NOT match password!\n"); + return 0; } - fclose(fp); - return 1; + printf("checkpassword(): unexpectedly got to end of function, quitting.\n"); + exit(1); } |