diff options
author | Luke Bratch <luke@bratch.co.uk> | 2019-05-16 21:35:16 +0100 |
---|---|---|
committer | Luke Bratch <luke@bratch.co.uk> | 2019-05-16 21:35:16 +0100 |
commit | 61a369b85e51e0d54e1651e4ff8c11fe4815a0c0 (patch) | |
tree | 37273e4881e917b7402b66d563acc2f1359b7a08 | |
parent | 968cee422ab1d61b4234127892d75f0497d8d8c2 (diff) |
Move default configuration file location to $HOME/.blabouncer/ and create a default file automatically if it doesn't exist.
-rw-r--r-- | blabouncer.c | 9 | ||||
-rw-r--r-- | config.c | 87 | ||||
-rw-r--r-- | config.h | 7 |
3 files changed, 102 insertions, 1 deletions
diff --git a/blabouncer.c b/blabouncer.c index 59cf8c1..8ed8d72 100644 --- a/blabouncer.c +++ b/blabouncer.c @@ -1375,7 +1375,14 @@ int main(int argc, char *argv[]) { exit(1); } else { // If none provided, set to default - strcpy(settings.conffile, "blabouncer.conf"); + snprintf(settings.conffile, PATH_MAX, "%s/.blabouncer/blabouncer.conf", getenv("HOME")); + // Since this is the default, it might not exist yet, so let's check... + struct stat st = {0}; + if (stat(settings.conffile, &st) == -1) { + // ...it doesn't exist, so let's create it + printf("Creating default configuration file '%s'.\n", settings.conffile); + createconfigfile(settings.conffile); + } } printf("Using configuration file '%s'.\n", settings.conffile); @@ -127,3 +127,90 @@ int checkpassword(char *password, char *filename) { printf("checkpassword(): unexpectedly got to end of function, quitting.\n"); exit(1); } + +// Create the default configuration file. +// Return 1 on success, 0 on failure. +int createconfigfile(char *filename) { + char *dirtmp; + char *dir; + dirtmp = strdup(filename); + dir = strdup(dirname(dirtmp)); + + // Make sure the parent directory exists + struct stat st = {0}; + if (stat(dir, &st) == -1) { + if (mkdir(dir, 0700)) { + printf("Error creating config directory '%s'.\n", dir); + exit(1); + } else { + printf("Created config directory '%s'.\n", dir); + } + } + + FILE *fp; +// char line[MAXCHAR]; + + fp = fopen(filename, "a"); + + if (fp == NULL) { + printf("error: could not open default configuration file '%s' for writing.\n", filename); + exit(1); + } + + // Prepare the string + char *string = + "# blabouncer configuration file\n" + "# Entries must be in the form:\n" + "# option name, space, equals sign, space, double quote, option value, double quote\n" + "# e.g.\n" + "# realname = \"Mr Bla Bouncer\"\n" + "\n" + "nick = \"blabounce\"\n" + "username = \"blabounce\"\n" + "realname = \"Mr Bla Bouncer\"\n" + "\n" + "# How many seconds of replay log should be sent to connecting clients\n" + "replayseconds = \"7200\"\n" + "\n" + "# Connect password clients must provided to connect\n" + "password = \"bananas\"\n" + "\n" + "# Port the bouncer should listen on\n" + "clientport = \"1234\"\n" + "\n" + "# Enable TLS for clients connecting to the bouncer (\"1\" for yes or \"0\" for no)\n" + "# If \"0\" then certfile and keyfile need not be set\n" + "clienttls = \"1\"\n" + "\n" + "# Enable TLS for the bouncer connecting to the IRC server (\"1\" for yes or \"0\" for no)\n" + "servertls = \"1\"\n" + "\n" + "# Real IRC server the bouncer connects to\n" + "ircserver = \"irc.blatech.net\"\n" + "\n" + "# Real IRC server port\n" + "ircserverport = \"6697\"\n" + "\n" + "# Certificate file\n" + "# If clienttls = \"0\" then this need not be set\n" + "certfile = \"cert.pem\"\n" + "\n" + "# Certificate key file\n" + "# If clienttls = \"0\" then this need not be set\n" + "keyfile = \"key.pem\"\n" + "\n" + "# Base directory (defaults to $HOME/.blabouncer/)\n" + "# Things such as the logs directory will be placed below this\n" + "#basedir = \"/home/foo/.blabouncer/\"\n"; + + // Write complete string to file + if ((fprintf(fp, string)) < 0) { + printf("error: could not write to replay log file.\n"); + exit(1); + } + + fclose(fp); + return 0; + +exit(1); +} @@ -4,6 +4,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <libgen.h> +#include <sys/stat.h> +#include <limits.h> #define MAXCHAR 1000 @@ -22,4 +25,8 @@ int getconfint(char *confname, char *filename); // Return 0 for password mismatch, or 1 for password match. int checkpassword(char *password, char *filename); +// Create the default configuration file. +// Return 1 on success, 0 on failure. +int createconfigfile(char *filename); + #endif |