implemented config file stack; to be used for include support
[radsecproxy.git] / radsecproxy.c
index 7a6e4c8..aa01bb9 100644 (file)
@@ -48,6 +48,8 @@
 #include <openssl/x509v3.h>
 #include "debug.h"
 #include "list.h"
+#include "util.h"
+#include "gconfig.h"
 #include "radsecproxy.h"
 
 static struct options options;
@@ -2429,203 +2431,29 @@ void addrealm(char *value, char **servers, char *message) {
     debug(DBG_DBG, "addrealm: added realm %s", value);
 }
 
-FILE *openconfigfile(const char *filename) {
+struct gconffile *openconfigfile(const char *filename) {
     FILE *f;
     char pathname[100], *base = NULL;
+    struct gconffile *cf = NULL;
     
-    f = fopen(filename, "r");
+    f = pushgconffile(&cf, filename);
     if (f) {
        debug(DBG_DBG, "reading config file %s", filename);
-       return f;
+       return cf;
     }
 
     if (strlen(filename) + 1 <= sizeof(pathname)) {
        /* basename() might modify the string */
        strcpy(pathname, filename);
        base = basename(pathname);
-       f = fopen(base, "r");
+       f = pushgconffile(&cf, base);
     }
 
     if (!f)
        debugx(1, DBG_ERR, "could not read config file %s nor %s\n%s", filename, base, strerror(errno));
     
     debug(DBG_DBG, "reading config file %s", base);
-    return f;
-}
-
-/* returns NULL on error, where to continue parsing if token and ok. E.g. "" will return token with empty string */
-char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment) {
-    char *t = s, *q, *r;
-
-    if (!t || !token || !del)
-       return NULL;
-    while (*t && strchr(del, *t))
-       t++;
-    if (!*t || (comment && strchr(comment, *t))) {
-       *token = NULL;
-       return t + 1; /* needs to be non-NULL, but value doesn't matter */
-    }
-    if (quote && (q = strchr(quote, *t))) {
-       t++;
-       r = t;
-       while (*t && *t != *q)
-           t++;
-       if (!*t || (t[1] && !strchr(del, t[1])))
-           return NULL;
-       *t = '\0';
-       *token = r;
-       return t + 1;
-    }
-    *token = t;
-    t++;
-    while (*t && !strchr(del, *t))
-       t++;
-    *t = '\0';
-    return t + 1;
-}
-
-/* Parses config with following syntax:
- * One of these:
- * option-name value
- * option-name = value
- * Or:
- * option-name value {
- *     option-name [=] value
- *     ...
- * }
- */
-void getgeneralconfig(FILE *f, char *block, ...) {
-    va_list ap;
-    char line[1024];
-    /* initialise lots of stuff to avoid stupid compiler warnings */
-    char *tokens[3], *s, *opt = NULL, *val = NULL, *word, *optval, **str = NULL, ***mstr = NULL;
-    int type = 0, tcount, conftype = 0, n;
-    void (*cbk)(FILE *, char *, char *, char *) = NULL;
-       
-    while (fgets(line, 1024, f)) {
-       s = line;
-       for (tcount = 0; tcount < 3; tcount++) {
-           s = strtokenquote(s, &tokens[tcount], " \t\r\n", "\"'", tcount ? NULL : "#");
-           if (!s)
-               debugx(1, DBG_ERR, "Syntax error in line starting with: %s", line);
-           if (!tokens[tcount])
-               break;
-       }
-       if (!tcount || **tokens == '#')
-           continue;
-
-       if (**tokens == '}') {
-           if (block)
-               return;
-           debugx(1, DBG_ERR, "configuration error, found } with no matching {");
-       }
-           
-       switch (tcount) {
-       case 2:
-           opt = tokens[0];
-           val = tokens[1];
-           conftype = CONF_STR;
-           break;
-       case 3:
-           if (tokens[1][0] == '=' && tokens[1][1] == '\0') {
-               opt = tokens[0];
-               val = tokens[2];
-               conftype = CONF_STR;
-               break;
-           }
-           if (tokens[2][0] == '{' && tokens[2][1] == '\0') {
-               opt = tokens[0];
-               val = tokens[1];
-               conftype = CONF_CBK;
-               break;
-           }
-           /* fall through */
-       default:
-           if (block)
-               debugx(1, DBG_ERR, "configuration error in block %s, line starting with %s", block, tokens[0]);
-           debugx(1, DBG_ERR, "configuration error, syntax error in line starting with %s", tokens[0]);
-       }
-
-       if (!*val)
-           debugx(1, DBG_ERR, "configuration error, option %s needs a non-empty value", opt);
-       
-       va_start(ap, block);
-       while ((word = va_arg(ap, char *))) {
-           type = va_arg(ap, int);
-           switch (type) {
-           case CONF_STR:
-               str = va_arg(ap, char **);
-               if (!str)
-                   debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
-               break;
-           case CONF_MSTR:
-               mstr = va_arg(ap, char ***);
-               if (!mstr)
-                   debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
-               break;
-           case CONF_CBK:
-               cbk = va_arg(ap, void (*)(FILE *, char *, char *, char *));
-               break;
-           default:
-               debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
-           }
-           if (!strcasecmp(opt, word))
-               break;
-       }
-       va_end(ap);
-       
-       if (!word) {
-           if (block)
-               debugx(1, DBG_ERR, "configuration error in block %s, unknown option %s", block, opt);
-           debugx(1, DBG_ERR, "configuration error, unknown option %s", opt);
-       }
-
-       if (((type == CONF_STR || type == CONF_MSTR) && conftype != CONF_STR) ||
-           (type == CONF_CBK && conftype != CONF_CBK)) {
-           if (block)
-               debugx(1, DBG_ERR, "configuration error in block %s, wrong syntax for option %s", block, opt);
-           debugx(1, DBG_ERR, "configuration error, wrong syntax for option %s", opt);
-       }
-       
-       switch (type) {
-       case CONF_STR:
-           if (block)
-               debug(DBG_DBG, "getgeneralconfig: block %s: %s = %s", block, opt, val);
-           else 
-               debug(DBG_DBG, "getgeneralconfig: %s = %s", opt, val);
-           if (*str)
-               debugx(1, DBG_ERR, "configuration error, option %s already set to %s", opt, *str);
-           *str = stringcopy(val, 0);
-           if (!*str)
-               debugx(1, DBG_ERR, "malloc failed");
-           break;
-       case CONF_MSTR:
-           if (block)
-               debug(DBG_DBG, "getgeneralconfig: block %s: %s = %s", block, opt, val);
-           else 
-               debug(DBG_DBG, "getgeneralconfig: %s = %s", opt, val);
-           if (*mstr)
-               for (n = 0; (*mstr)[n]; n++);
-           else
-               n = 0;
-           *mstr = realloc(*mstr, sizeof(char *) * (n + 2));
-           if (!*mstr)
-               debugx(1, DBG_ERR, "malloc failed");
-           (*mstr)[n] = stringcopy(val, 0);
-           (*mstr)[n + 1] = NULL;
-           break;
-       case CONF_CBK:
-           optval = malloc(strlen(opt) + strlen(val) + 2);
-           if (!optval)
-               debugx(1, DBG_ERR, "malloc failed");
-           sprintf(optval, "%s %s", opt, val);
-           cbk(f, optval, opt, val);
-           free(optval);
-           break;
-       default:
-           debugx(1, DBG_ERR, "getgeneralconfig: internal parameter error");
-       }
-    }
+    return cf;
 }
 
 int addmatchcertattr(struct clsrvconf *conf, char *matchcertattr) {
@@ -2706,7 +2534,7 @@ void confclient_cb(FILE *f, char *block, char *opt, char *val) {
        debugx(1, DBG_ERR, "malloc failed");
     memset(conf, 0, sizeof(struct clsrvconf));
     
-    getgeneralconfig(f, block,
+    getgenericconfig(f, block,
                     "type", CONF_STR, &type,
                     "host", CONF_STR, &conf->host,
                     "secret", CONF_STR, &conf->secret,
@@ -2766,7 +2594,7 @@ void confserver_cb(FILE *f, char *block, char *opt, char *val) {
        debugx(1, DBG_ERR, "malloc failed");
     memset(conf, 0, sizeof(struct clsrvconf));
     
-    getgeneralconfig(f, block,
+    getgenericconfig(f, block,
                     "type", CONF_STR, &type,
                     "host", CONF_STR, &conf->host,
                     "port", CONF_STR, &conf->port,
@@ -2827,7 +2655,7 @@ void confrealm_cb(FILE *f, char *block, char *opt, char *val) {
     
     debug(DBG_DBG, "confrealm_cb called for %s", block);
     
-    getgeneralconfig(f, block,
+    getgenericconfig(f, block,
                     "server", CONF_MSTR, &servers,
                     "ReplyMessage", CONF_STR, &msg,
                     NULL
@@ -2842,7 +2670,7 @@ void conftls_cb(FILE *f, char *block, char *opt, char *val) {
     
     debug(DBG_DBG, "conftls_cb called for %s", block);
     
-    getgeneralconfig(f, block,
+    getgenericconfig(f, block,
                     "CACertificateFile", CONF_STR, &cacertfile,
                     "CACertificatePath", CONF_STR, &cacertpath,
                     "CertificateFile", CONF_STR, &certfile,
@@ -2862,8 +2690,10 @@ void conftls_cb(FILE *f, char *block, char *opt, char *val) {
 void getmainconfig(const char *configfile) {
     FILE *f;
     char *loglevel = NULL;
+    struct gconffile *cfs;
 
-    f = openconfigfile(configfile);
+    cfs = openconfigfile(configfile);
+    f = cfs->file;
     memset(&options, 0, sizeof(options));
     
     clconfs = list_create();
@@ -2882,7 +2712,7 @@ void getmainconfig(const char *configfile) {
     if (!tlsconfs)
        debugx(1, DBG_ERR, "malloc failed");    
  
-    getgeneralconfig(f, NULL,
+    getgenericconfig(f, NULL,
                     "ListenUDP", CONF_STR, &options.listenudp,
                     "ListenTCP", CONF_STR, &options.listentcp,
                     "ListenAccountingUDP", CONF_STR, &options.listenaccudp,
@@ -2896,7 +2726,7 @@ void getmainconfig(const char *configfile) {
                     "TLS", CONF_CBK, conftls_cb,
                     NULL
                     );
-    fclose(f);
+    popgconffile(&cfs);
     tlsfree();
     
     if (loglevel) {
@@ -2907,10 +2737,10 @@ void getmainconfig(const char *configfile) {
     }
 }
 
-void getargs(int argc, char **argv, uint8_t *foreground, uint8_t *loglevel, char **configfile) {
+void getargs(int argc, char **argv, uint8_t *foreground, uint8_t *pretend, uint8_t *loglevel, char **configfile) {
     int c;
 
-    while ((c = getopt(argc, argv, "c:d:fv")) != -1) {
+    while ((c = getopt(argc, argv, "c:d:fpv")) != -1) {
        switch (c) {
        case 'c':
            *configfile = optarg;
@@ -2923,6 +2753,9 @@ void getargs(int argc, char **argv, uint8_t *foreground, uint8_t *loglevel, char
        case 'f':
            *foreground = 1;
            break;
+       case 'p':
+           *pretend = 1;
+           break;
        case 'v':
                debugx(0, DBG_ERR, "radsecproxy revision $Rev$");
        default:
@@ -2933,19 +2766,19 @@ void getargs(int argc, char **argv, uint8_t *foreground, uint8_t *loglevel, char
        return;
 
  usage:
-    debug(DBG_ERR, "Usage:\n%s [ -c configfile ] [ -d debuglevel ] [ -f ] [ -v ]", argv[0]);
+    debug(DBG_ERR, "Usage:\n%s [ -c configfile ] [ -d debuglevel ] [ -f ] [ -p ] [ -v ]", argv[0]);
     exit(1);
 }
 
 int main(int argc, char **argv) {
     pthread_t udpserverth, udpaccserverth, udpclient4rdth, udpclient6rdth;
     struct list_node *entry;
-    uint8_t foreground = 0, loglevel = 0;
+    uint8_t foreground = 0, pretend = 0, loglevel = 0;
     char *configfile = NULL;
     
     debug_init("radsecproxy");
     debug_set_level(DEBUG_LEVEL);
-    getargs(argc, argv, &foreground, &loglevel, &configfile);
+    getargs(argc, argv, &foreground, &pretend, &loglevel, &configfile);
     if (loglevel)
        debug_set_level(loglevel);
     getmainconfig(configfile ? configfile : CONFIG_MAIN);
@@ -2961,6 +2794,9 @@ int main(int argc, char **argv) {
        debug_set_destination(options.logdestination);
     }
 
+    if (pretend)
+       debugx(0, DBG_ERR, "All OK so far; exiting since only pretending");
+    
     if (!list_first(clconfs))
        debugx(1, DBG_ERR, "No clients configured, nothing to do, exiting");
     if (!list_first(srvconfs))