From 5b2b627c33a071f8420836f75c5b92897e49d68a Mon Sep 17 00:00:00 2001 From: laforge Date: Sat, 9 Sep 2000 18:35:26 +0000 Subject: config file added --- ulogd/Makefile | 9 +++-- ulogd/conffile.c | 15 ++++++--- ulogd/conffile.h | 9 ++--- ulogd/ulogd.c | 101 +++++++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 105 insertions(+), 29 deletions(-) (limited to 'ulogd') diff --git a/ulogd/Makefile b/ulogd/Makefile index 2358c04..22489b3 100644 --- a/ulogd/Makefile +++ b/ulogd/Makefile @@ -19,10 +19,13 @@ $(SHARED_LIBS): %.so: %_sh.o ld -shared -o $@ $< %_sh.o: %.c - gcc $(SH_CFLAGS) -o $@ -c $< + $(CC) $(SH_CFLAGS) -o $@ -c $< -ulogd: ulogd.c $(LIBIPULOG) ulogd.h - $(CC) $(CFLAGS) -rdynamic -ldl -i ulogd.c $(LIBIPULOG)/libipulog.a -o ulogd +conffile.o: conffile.c + $(CC) $(CFLAGS) -c $< -o $@ + +ulogd: ulogd.c $(LIBIPULOG) ulogd.h conffile.o + $(CC) $(CFLAGS) -rdynamic -ldl -i ulogd.c conffile.o $(LIBIPULOG)/libipulog.a -o ulogd clean: rm -f ulogd extensions/*.o extensions/*.so diff --git a/ulogd/conffile.c b/ulogd/conffile.c index 8227c67..2baf8f8 100644 --- a/ulogd/conffile.c +++ b/ulogd/conffile.c @@ -1,27 +1,30 @@ /* config file parser functions * (C) 2000 by Harald Welte * - * $Id: conffile.c,v 1.1 2000/09/09 08:36:05 laforge Exp $ + * $Id: conffile.c,v 1.2 2000/09/09 18:27:23 laforge Exp $ * * This code is distributed under the terms of GNU GPL */ #include #include +#include #include "conffile.h" #ifdef DEBUG_CONF #define DEBUGC(format, args...) fprintf(stderr, format ## args) #else -#define DEBUGC +#define DEBUGC(format, args...) #endif static config_entry_t *config = NULL; +config_entry_t *config_errce = NULL; static char *get_word(const char *string) { int len; char *word, *space; space = strchr(string, ' '); + if (!space) { space = strchr(string, '\t'); if (!space) @@ -88,7 +91,8 @@ int config_parse_file(const char *fname, int final) word = get_word(line); if (!word) continue; - + + /* if we do the final parse and word is not a config key */ if (final && !config_iskey(word)) { err = -ERRUNKN; config_errce = ce; @@ -96,6 +100,7 @@ int config_parse_file(const char *fname, int final) } args = line + strlen(word) + 1; + *(args + strlen(args) - 1 ) = '\0'; for (ce = config; ce; ce = ce->next) { if (strcmp(ce->key, word)) { @@ -112,8 +117,8 @@ int config_parse_file(const char *fname, int final) ce->hit++; switch (ce->type) { case CONFIG_TYPE_STRING: - if (strlen(args) < - CONFIG_VAL_STRING_LEN) { + if (strlen(args) < + CONFIG_VAL_STRING_LEN ) { strcpy(ce->u.string, args); } break; diff --git a/ulogd/conffile.h b/ulogd/conffile.h index 773c932..8429bbb 100644 --- a/ulogd/conffile.h +++ b/ulogd/conffile.h @@ -1,7 +1,7 @@ /* config file parser functions * (C) 2000 by Harald Welte * - * $Id: conffile.h,v 1.1 2000/09/09 08:36:05 laforge Exp $ + * $Id: conffile.h,v 1.2 2000/09/09 18:27:23 laforge Exp $ * * This code is distributed under the terms of GNU GPL */ @@ -17,7 +17,7 @@ enum { ERROOM, /* out of memory */ ERRMULT, /* non-multiple option occured more than once */ ERRMAND, /* mandatory option not found */ - ERRUNKN, /* unknown key */ + ERRUNKN, /* unknown config key */ }; /* maximum line lenght of config file entries */ @@ -25,8 +25,9 @@ enum { /* maximum lenght of config key name */ #define CONFIG_KEY_LEN 30 -#define CONFIG_VAL_STRING_LEN 225 +/* maximum lenght of string config value */ +#define CONFIG_VAL_STRING_LEN 225 /* valid config types */ #define CONFIG_TYPE_INT 0x0001 @@ -51,7 +52,7 @@ typedef struct config_entry { } config_entry_t; /* if an error occurs, config_errce is set to the erroneous ce */ -config_entry_t *config_errce = NULL; +config_entry_t *config_errce; int config_parse_file(const char *fname, int final); int config_register_key(config_entry_t *ce); diff --git a/ulogd/ulogd.c b/ulogd/ulogd.c index d917a3f..5c24083 100644 --- a/ulogd/ulogd.c +++ b/ulogd/ulogd.c @@ -1,4 +1,4 @@ -/* ulogd, Version $Revision: 1.5 $ +/* ulogd, Version $Revision: 1.6 $ * * first try of a logging daemon for my netfilter ULOG target * for the linux 2.4 netfilter subsystem. @@ -7,7 +7,7 @@ * * this code is released under the terms of GNU GPL * - * $Id: ulogd.c,v 1.5 2000/08/11 09:56:48 laforge Exp $ + * $Id: ulogd.c,v 1.6 2000/08/14 08:28:24 laforge Exp $ */ #include @@ -17,26 +17,30 @@ #include #include #include +#include "conffile.h" #include "ulogd.h" #define MYBUFSIZ 2048 - -#define DEBUGP ulogd_error +#ifdef DEBUG +#define DEBUGP(format, args...) fprintf(stderr, format, ## args) +#else +#define DEBUGP(format, args...) +#endif #ifndef ULOGD_PLUGIN_DIR #define ULOGD_PLUGIN_DIR "/usr/local/lib/ulogd" #endif -#ifndef ULOGD_LOGFILE -#define ULOGD_LOGFILE "/var/log/ulogd.log" +#ifndef ULOGD_CONFIGFILE +#define ULOGD_CONFIGFILE "/etc/ulogd.conf" #endif #ifndef ULOGD_NLGROUP #define ULOGD_NLGROUP 32 #endif -FILE *logfile; +FILE *logfile = NULL; /* linked list for all registered interpreters */ static ulog_interpreter_t *ulogd_interpreters; @@ -45,7 +49,7 @@ static ulog_interpreter_t *ulogd_interpreters; static ulog_output_t *ulogd_outputs; /* try to lookup a registered interpreter for a given name */ -ulog_interpreter_t *find_interpreter(const char *name) +static ulog_interpreter_t *find_interpreter(const char *name) { ulog_interpreter_t *ptr; @@ -72,7 +76,7 @@ void register_interpreter(ulog_interpreter_t *me) } /* try to lookup a registered output plugin for a given name */ -ulog_output_t *find_output(const char *name) +static ulog_output_t *find_output(const char *name) { ulog_output_t *ptr; @@ -132,7 +136,7 @@ void free_ret(ulog_iret_t *ret) /* this should pass the result(s) to one or more registered output plugins, * but is currently only printing them out */ -void propagate_results(ulog_iret_t *ret) +static void propagate_results(ulog_iret_t *ret) { ulog_output_t *p; @@ -144,7 +148,7 @@ void propagate_results(ulog_iret_t *ret) /* call all registered interpreters and hand the results over to * propagate_results */ -void handle_packet(ulog_packet_msg_t *pkt) +static void handle_packet(ulog_packet_msg_t *pkt) { ulog_interpreter_t *ptr; ulog_iret_t *ret, *b; @@ -167,13 +171,13 @@ void handle_packet(ulog_packet_msg_t *pkt) } /* silly plugin loader to dlopen() all available plugins */ -void load_plugins(void) +static void load_plugins(char *dir) { DIR *ldir; struct dirent *dent; char *fname; - ldir = opendir(ULOGD_PLUGIN_DIR); + ldir = opendir(dir); if (ldir) { fname = (char *) malloc(NAME_MAX + strlen(ULOGD_PLUGIN_DIR) + 3); @@ -187,11 +191,11 @@ void load_plugins(void) } free(fname); } else - ulogd_error("no plugin directory\n"); + ulogd_error("no plugin directory: %s\n", dir); } -int logfile_open(const char *name) +static int logfile_open(const char *name) { logfile = fopen(name, "a"); if (!logfile) @@ -202,6 +206,55 @@ int logfile_open(const char *name) return 0; } +static int parse_conffile(char *file, int final) +{ + int err; + FILE *outfd; + + err = config_parse_file(file, final); + + if (logfile) + outfd = logfile; + else + outfd = stderr; + + switch(err) { + case 0: + return 0; + break; + case -ERROPEN: + fprintf(outfd, "ERROR: unable to open configfile: %s\n", + ULOGD_CONFIGFILE); + break; + case -ERRMAND: + fprintf(outfd, "ERROR: mandatory option not found\n"); + break; + case -ERRMULT: + fprintf(outfd, "ERROR: option occurred more than once\n"); + break; + case -ERRUNKN: + fprintf(outfd, "ERROR: unknown config key\n"); + break; + } + return 1; + +} + +static config_entry_t logf_ce = { NULL, "logfile", CONFIG_TYPE_STRING, + CONFIG_OPT_MANDATORY, 0, { "" } }; + +static config_entry_t pldir_ce = { NULL, "plugindir", CONFIG_TYPE_STRING, + CONFIG_OPT_MANDATORY, 0, { "" } }; + +static int init_conffile(char *file) +{ + config_register_key(&logf_ce); + config_register_key(&pldir_ce); + + /* parse config file the first time (for logfile name, ...) */ + return parse_conffile(file, 0); +} + int main(int argc, char* argv[]) { struct ipulog_handle *h; @@ -209,9 +262,23 @@ int main(int argc, char* argv[]) size_t len; ulog_packet_msg_t *upkt; - logfile_open(ULOGD_LOGFILE); - load_plugins(); + if (init_conffile(ULOGD_CONFIGFILE)) + { + DEBUGP("ERROR during init_configfile\n"); + exit(1); + } + + logfile_open(logf_ce.u.string); + load_plugins(pldir_ce.u.string); + + /* parse config file the second time (for plugin options) */ + if (parse_conffile(ULOGD_CONFIGFILE, 1)) + { + ulogd_error("ERROR during second parse_conffile\n"); + exit(1); + } + /* allocate a receive buffer */ buf = (unsigned char *) malloc(MYBUFSIZ); -- cgit v1.2.3