Commit cbd283e0 authored by venaas's avatar venaas Committed by venaas

implemented config file stack; to be used for include support

git-svn-id: https://svn.testnett.uninett.no/radsecproxy/trunk@198 e88ac4ed-0b26-0410-9574-a7f39faa03bf
parent ddddde8f
......@@ -46,6 +46,51 @@ char *strtokenquote(char *s, char **token, char *del, char *quote, char *comment
return t + 1;
}
FILE *pushgconffile(struct gconffile **cf, const char *path) {
int i;
struct gconffile *newcf;
FILE *f;
f = fopen(path, "r");
if (!f) {
debug(DBG_INFO, "could not read config file %s", path);
return NULL;
}
if (!*cf) {
newcf = malloc(sizeof(struct gconffile) * 2);
if (!newcf)
debugx(1, DBG_ERR, "malloc failed");
newcf[1].path = NULL;
} else {
for (i = 0; (*cf)[i].path; i++);
newcf = realloc(*cf, sizeof(struct gconffile) * (i + 2));
if (!newcf)
debugx(1, DBG_ERR, "malloc failed");
memmove(newcf + 1, newcf, sizeof(struct gconffile) * (i + 1));
}
newcf[0].file = f;
newcf[0].path = stringcopy(path, 0);
*cf = newcf;
return f;
}
FILE *popgconffile(struct gconffile **cf) {
int i;
if (!*cf)
return NULL;
for (i = 0; (*cf)[i].path; i++);
if (i && (*cf)[0].file)
fclose((*cf)[0].file);
if (i < 2) {
free(*cf);
*cf = NULL;
return NULL;
}
memmove(*cf, *cf + 1, sizeof(struct gconffile) * i);
return (*cf)[0].file;
}
/* Parses config with following syntax:
* One of these:
* option-name value
......
......@@ -2,4 +2,11 @@
#define CONF_CBK 2
#define CONF_MSTR 3
struct gconffile {
char *path;
FILE *file;
};
void getgenericconfig(FILE *f, char *block, ...);
FILE *pushgconffile(struct gconffile **cf, const char *path);
FILE *popgconffile(struct gconffile **cf);
......@@ -2431,28 +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;
return cf;
}
int addmatchcertattr(struct clsrvconf *conf, char *matchcertattr) {
......@@ -2689,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();
......@@ -2723,7 +2726,7 @@ void getmainconfig(const char *configfile) {
"TLS", CONF_CBK, conftls_cb,
NULL
);
fclose(f);
popgconffile(&cfs);
tlsfree();
if (loglevel) {
......
......@@ -50,7 +50,7 @@ void err(char *format, ...) {
}
#endif
char *stringcopy(char *s, int len) {
char *stringcopy(const char *s, int len) {
char *r;
if (!len)
len = strlen(s);
......
#include <sys/socket.h>
char *stringcopy(char *s, int len);
char *stringcopy(const char *s, int len);
char *addr2string(struct sockaddr *addr, socklen_t len);
void printfchars(char *prefixfmt, char *prefix, char *charfmt, char *chars, int len);
int connectport(int type, char *host, char *port);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment