#include "config.h" // TODO - Multiple functions here (at least readnames(), replayseconds() and checkpassword()) have the file opening code, rewrite. // TODO - Can isconf() and getconf() just be merged into one function? // 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); // Ensure the next character is an equals sign, a space, or a tab if (str[len] != '=' && str[len] != ' ' && str[len] != ' ') { return 0; } strncpy(substr, str, len); substr[len] = '\0'; if (!(ret = strstr(substr, conf)) == 0) { return len; } 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]; for (long int i = start; i < len; i++) { if (str[i] == '=' || str[i] == ' ' || str[i] == ' ') { continue; } else { 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 // Check for start and end quotes if (conf[0] != '"' || conf[strlen(conf) - 1] != '"') { printf("Error reading configuration option, not enclosed in double quotes!\n"); 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"; // 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); } 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); 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; } // 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); } 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)); } } // Ensure we have everything required if (!strlen(clientport)) { printf("Failed to read clientport from configuration file.\n"); exit(1); } 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); } fclose(fp); return 1; }