From 61a369b85e51e0d54e1651e4ff8c11fe4815a0c0 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Thu, 16 May 2019 21:35:16 +0100 Subject: Move default configuration file location to $HOME/.blabouncer/ and create a default file automatically if it doesn't exist. --- blabouncer.c | 9 ++++++- config.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ config.h | 7 +++++ 3 files changed, 102 insertions(+), 1 deletion(-) 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); diff --git a/config.c b/config.c index af30910..a5d9c2f 100644 --- a/config.c +++ b/config.c @@ -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); +} diff --git a/config.h b/config.h index 3e96c19..4043f36 100644 --- a/config.h +++ b/config.h @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include #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 -- cgit v1.2.3