From be427ded4ed91db215eb2af31ff41a3b989c61ac Mon Sep 17 00:00:00 2001 From: venaas Date: Thu, 8 Feb 2007 10:30:24 +0000 Subject: improved config code git-svn-id: https://svn.testnett.uninett.no/radsecproxy/trunk@53 e88ac4ed-0b26-0410-9574-a7f39faa03bf --- radsecproxy.c | 44 ++++++++++++++++++++++++++++++-------------- radsecproxy.h | 4 ++++ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/radsecproxy.c b/radsecproxy.c index 742c6b6..85fb401 100644 --- a/radsecproxy.c +++ b/radsecproxy.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -1592,22 +1593,41 @@ char *parserealmlist(char *s, struct server *server) { return p; } +FILE *openconfigfile(const char *filename) { + FILE *f; + char pathname[100], *base; + + f = fopen(filename, "r"); + if (f) { + printf("reading config file %s\n", filename); + return f; + } + + if (strlen(filename) + 1 <= sizeof(pathname)) { + // basename() might modify the string + strcpy(pathname, filename); + base = basename(pathname); + f = fopen(base, "r"); + } + + if (!f) + err("could not read config file %s nor %s\n", filename, base); + + printf("reading config file %s\n", base); + return f; +} + /* exactly one argument must be non-NULL */ void getconfig(const char *serverfile, const char *clientfile) { FILE *f; char line[1024]; char *p, *field, **r; - const char *file; struct client *client; struct server *server; struct peer *peer; int i, count, *ucount, *tcount; - file = serverfile ? serverfile : clientfile; - f = fopen(file, "r"); - if (!f) - errx("getconfig failed to open %s for reading", file); - printf("opening file %s for reading\n", file); + f = openconfigfile(serverfile ? serverfile : clientfile); if (serverfile) { ucount = &server_udp_count; tcount = &server_tls_count; @@ -1742,11 +1762,7 @@ void getmainconfig(const char *configfile) { char line[1024]; char *p, *opt, *endopt, *val, *endval; - printf("opening file %s for reading\n", configfile); - f = fopen(configfile, "r"); - if (!f) - errx("getmainconfig failed to open %s for reading", configfile); - + f = openconfigfile(configfile); memset(&options, 0, sizeof(options)); while (fgets(line, 1024, f)) { @@ -1828,9 +1844,9 @@ int main(int argc, char **argv) { int i; // parseargs(argc, argv); - getmainconfig("radsecproxy.conf"); - getconfig("servers.conf", NULL); - getconfig(NULL, "clients.conf"); + getmainconfig(CONFIG_MAIN); + getconfig(CONFIG_SERVERS, NULL); + getconfig(NULL, CONFIG_CLIENTS); // pthread_attr_init(&joinable); // pthread_attr_setdetachstate(&joinable, PTHREAD_CREATE_JOINABLE); diff --git a/radsecproxy.h b/radsecproxy.h index 53c772a..dd67844 100644 --- a/radsecproxy.h +++ b/radsecproxy.h @@ -12,6 +12,10 @@ sizeof(struct sockaddr_in) : \ sizeof(struct sockaddr_in6)) +#define CONFIG_MAIN "/etc/radsecproxy/radsecproxy.conf" +#define CONFIG_SERVERS "/etc/radsecproxy/servers.conf" +#define CONFIG_CLIENTS "/etc/radsecproxy/clients.conf" + /* MAX_REQUESTS must be 256 due to Radius' 8 bit ID field */ #define MAX_REQUESTS 256 #define DEFAULT_TLS_SECRET "mysecret" -- cgit v1.1