#include "config.h" // 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]; int found = 0; // Have we found the configuration option? // Set strings to zero-length to begin dest[0] = '\0'; // Length of requested configuration option name long int namelen = strlen(confname); 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]; // 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 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) { found = 1; break; } } // If we got here, then either we found the configuration option or we ran out of file if (!found) { printf("Error reading configuration option '%s', did not find it in configuration file '%s'.\n", confname, filename); return 0; } long int pos; char conf[MAXCHAR]; // Temporary string to build configuration value in // 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, 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 '%s', not enclosed in double quotes in configuration file '%s'!\n", confname, filename); exit(1); } strncpy(dest, conf + 1, strlen(conf) - 2); // Copy result to destination string without quotes dest[strlen(conf) - 2] = '\0'; // Null terminate printf("getconfstr(): returning '%s'.\n", dest); // Close fine and return success fclose(fp); return 1; } // 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); } 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 configuration file 'filename'. // Return 0 for password mismatch, or 1 for password match. int checkpassword(char *password, char *filename) { char confpassword[MAXCHAR]; if (!getconfstr("password", filename, confpassword)) { printf("checkpassword(): error getting configuration option 'password' from configuration file '%s'.\n", filename); return 0; } // Ensure passwords are the same length if (strlen(password) != strlen(confpassword)) { printf("Password length mismatch!\n"); return 0; } // 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; } printf("checkpassword(): unexpectedly got to end of function, quitting.\n"); exit(1); }