From ab31159090e427a495f4ad77a6291923ab939160 Mon Sep 17 00:00:00 2001 From: laforge Date: Tue, 12 Sep 2000 14:29:36 +0000 Subject: added example config file bugfixes in conffile.c ulogd_log still prints obsolete \n cosmetic fixes --- ulogd/conffile.c | 27 +++++++++++++--- ulogd/conffile.h | 9 ++++-- ulogd/extensions/ulogd_BASE.c | 7 ++--- ulogd/extensions/ulogd_OPRINT.c | 24 ++++++++------ ulogd/include/ulogd/ulogd.h | 14 ++++++--- ulogd/ulogd.c | 70 +++++++++++++++++++---------------------- ulogd/ulogd.conf | 9 ++++++ 7 files changed, 96 insertions(+), 64 deletions(-) create mode 100644 ulogd/ulogd.conf diff --git a/ulogd/conffile.c b/ulogd/conffile.c index 10a537d..987a5d8 100644 --- a/ulogd/conffile.c +++ b/ulogd/conffile.c @@ -1,7 +1,7 @@ /* config file parser functions * (C) 2000 by Harald Welte * - * $Id: conffile.c,v 1.3 2000/09/09 18:35:26 laforge Exp $ + * $Id: conffile.c,v 1.4 2000/09/09 21:55:46 laforge Exp $ * * This code is distributed under the terms of GNU GPL */ @@ -20,14 +20,16 @@ static config_entry_t *config = NULL; config_entry_t *config_errce = NULL; +static char *fname = NULL; + static char *get_word(const char *string) { int len; char *word, *space; - space = strchr(string, ' '); + space = strrchr(string, ' '); if (!space) { - space = strchr(string, '\t'); + space = strrchr(string, '\t'); if (!space) return NULL; } @@ -41,7 +43,7 @@ static char *get_word(const char *string) strncpy(word, string, len); - if (*(word + len) == '\n') +// if (*(word + len) == '\n') *(word + len) = '\0'; return word; @@ -79,7 +81,22 @@ int config_register_key(config_entry_t *ce) return 0; } -int config_parse_file(const char *fname, int final) +int config_register_file(const char *file) +{ + /* FIXME: stat of file */ + if (fname) + return 1; + + fname = (char *) malloc(strlen(file)+1); + if (!fname) + return -ERROOM; + + strcpy(fname, file); + + return 0; +} + +int config_parse_file(int final) { FILE *cfile; char *line, *word, *args; diff --git a/ulogd/conffile.h b/ulogd/conffile.h index 6b04695..976bd44 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.3 2000/09/09 18:35:26 laforge Exp $ + * $Id: conffile.h,v 1.4 2000/09/09 21:55:46 laforge Exp $ * * This code is distributed under the terms of GNU GPL */ @@ -55,9 +55,12 @@ typedef struct config_entry { /* if an error occurs, config_errce is set to the erroneous ce */ config_entry_t *config_errce; -/* parse the config file "fname", presume all config keys are registered +/* tell us the name of the config file */ +int config_register_file(const char *file); + +/* parse the config file , presume all config keys are registered * if final==1 */ -int config_parse_file(const char *fname, int final); +int config_parse_file(int final); /* register a linked list of config entries */ int config_register_key(config_entry_t *ce); diff --git a/ulogd/extensions/ulogd_BASE.c b/ulogd/extensions/ulogd_BASE.c index e0b1e1f..290ad0b 100644 --- a/ulogd/extensions/ulogd_BASE.c +++ b/ulogd/extensions/ulogd_BASE.c @@ -1,11 +1,11 @@ -/* ulogd_MAC.c, Version $Revision: 1.2 $ +/* ulogd_MAC.c, Version $Revision: 1.3 $ * * ulogd logging interpreter for MAC addresses, TIME, etc. * * (C) 2000 by Harald Welte * This software is released under the terms of GNU GPL * - * $Id: ulogd_BASE.c,v 1.2 2000/08/02 12:15:44 laforge Exp $ + * $Id: ulogd_BASE.c,v 1.3 2000/08/14 08:28:24 laforge Exp $ * */ @@ -25,8 +25,7 @@ ulog_iret_t *_interp_mac(ulog_packet_msg_t *pkt) char *buf; ulog_iret_t *ret; - if (pkt->mac_len) - { + if (pkt->mac_len) { buf = (char *) malloc(3 * pkt->mac_len + 1); *buf = 0; diff --git a/ulogd/extensions/ulogd_OPRINT.c b/ulogd/extensions/ulogd_OPRINT.c index 19fbd7b..2d8e608 100644 --- a/ulogd/extensions/ulogd_OPRINT.c +++ b/ulogd/extensions/ulogd_OPRINT.c @@ -1,18 +1,19 @@ -/* ulogd_MAC.c, Version $Revision: 1.1 $ +/* ulogd_MAC.c, Version $Revision: 1.2 $ * * ulogd output target for logging to a file * * (C) 2000 by Harald Welte * This software is released under the terms of GNU GPL * - * $Id: ulogd_OPRINT.c,v 1.1 2000/08/02 12:16:00 laforge Exp $ + * $Id: ulogd_OPRINT.c,v 1.2 2000/08/14 08:28:24 laforge Exp $ * */ #include #include #include -#include +#include "ulogd.h" +#include "conffile.h" #define NIPQUAD(addr) \ ((unsigned char *)&addr)[0], \ @@ -26,8 +27,6 @@ ((unsigned char *)&addr)[1], \ ((unsigned char *)&addr)[0] -#define ULOGD_OPRINT_FILE "/var/log/ulogd.pktlog" - static FILE *of = NULL; int _output_print(ulog_iret_t *res) @@ -35,8 +34,7 @@ int _output_print(ulog_iret_t *res) ulog_iret_t *ret; fprintf(of, "===>PACKET BOUNDARY\n"); - for (ret = res; ret; ret = ret->next) - { + for (ret = res; ret; ret = ret->next) { fprintf(of,"%s=", ret->key); switch (ret->type) { case ULOGD_RET_STRING: @@ -74,12 +72,12 @@ int _output_print(ulog_iret_t *res) } static ulog_output_t base_op[] = { - { NULL, "print.console", &_output_print }, + { NULL, "print", &_output_print }, { NULL, "", NULL }, }; -void _base_reg_op(void) +static void _base_reg_op(void) { ulog_output_t *op = base_op; ulog_output_t *p; @@ -88,12 +86,18 @@ void _base_reg_op(void) register_output(p); } +static config_entry_t outf_ce = { NULL, "dumpfile", CONFIG_TYPE_STRING, + CONFIG_OPT_NONE, 0, + { string: "/var/log/ulogd.pktlog" } }; void _init(void) { #ifdef DEBUG of = stdout; #else - of = fopen(ULOGD_OPRINT_FILE, "a"); + config_register_key(&outf_ce); + config_parse_file(0); + + of = fopen(outf_ce.u.string, "a"); if (!of) { ulogd_error("ulogd_OPRINT: can't open PKTLOG: %s\n", strerror(errno)); exit(2); diff --git a/ulogd/include/ulogd/ulogd.h b/ulogd/include/ulogd/ulogd.h index 31beb29..24986b0 100644 --- a/ulogd/include/ulogd/ulogd.h +++ b/ulogd/include/ulogd/ulogd.h @@ -1,6 +1,6 @@ #ifndef _ULOGD_H #define _ULOGD_H -/* ulogd, Version $Revision: 1.4 $ +/* ulogd, Version $Revision: 1.6 $ * * first try of a logging daemon for my netfilter ULOG target * for the linux 2.4 netfilter subsystem. @@ -9,7 +9,7 @@ * * this code is released under the terms of GNU GPL * - * $Id: ulogd.h,v 1.4 2000/08/14 08:28:24 laforge Exp $ + * $Id: ulogd.h,v 1.6 2000/09/12 13:43:34 laforge Exp $ */ #include @@ -40,10 +40,13 @@ #define ULOGD_RET_OTHER 0xffff - +/* maximum length of ulogd key */ #define ULOGD_MAX_KEYLEN 32 -#define ulogd_error(format, args...) ulogd_log(8, format, ## args) +#define ULOGD_DEBUG 1 +#define ULOGD_NOTICE 5 +#define ULOGD_ERROR 8 + extern FILE *logfile; @@ -94,4 +97,7 @@ ulog_iret_t *alloc_ret(const u_int16_t type, const char*); /* write a message to the daemons' logfile */ void ulogd_log(int level, const char *message, ...); +/* backwards compatibility */ +#define ulogd_error(format, args...) ulogd_log(ULOGD_ERROR, format, ## args) + #endif diff --git a/ulogd/ulogd.c b/ulogd/ulogd.c index 1e8d255..a91be2c 100644 --- a/ulogd/ulogd.c +++ b/ulogd/ulogd.c @@ -1,4 +1,4 @@ -/* ulogd, Version $Revision: 1.8 $ +/* ulogd, Version $Revision: 1.9 $ * * 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.8 2000/09/09 21:55:46 laforge Exp $ + * $Id: ulogd.c,v 1.9 2000/09/12 13:43:34 laforge Exp $ */ #include @@ -77,7 +77,7 @@ void register_interpreter(ulog_interpreter_t *me) me->name); exit(1); } - DEBUGP("registering interpreter `%s'\n", me->name); + ulogd_log(ULOGD_NOTICE, "registering interpreter `%s'\n", me->name); me->next = ulogd_interpreters; ulogd_interpreters = me; } @@ -103,7 +103,7 @@ void register_output(ulog_output_t *me) me->name); exit(1); } - DEBUGP("registering output `%s'\n", me->name); + ulogd_log(ULOGD_NOTICE, "registering output `%s'\n", me->name); me->next = ulogd_outputs; ulogd_outputs = me; } @@ -145,14 +145,20 @@ void ulogd_log(int level, const char *format, ...) char *timestr; va_list ap; time_t tm; + FILE *outfd; + + if (logfile) + outfd = logfile; + else + outfd = stderr; va_start(ap, format); tm = time(NULL); timestr = ctime(&tm); - fprintf(logfile, "%s <%1.1d>", timestr, level); + fprintf(outfd, "%s <%1.1d>", timestr, level); - vfprintf(logfile, format, ap); + vfprintf(outfd, format, ap); va_end(ap); } /* this should pass the result(s) to one or more registered output plugins, @@ -161,8 +167,7 @@ static void propagate_results(ulog_iret_t *ret) { ulog_output_t *p; - for (p = ulogd_outputs; p; p = p->next) - { + for (p = ulogd_outputs; p; p = p->next) { (*p->output)(ret); } } @@ -219,8 +224,7 @@ static void load_plugins(char *dir) static int logfile_open(const char *name) { logfile = fopen(name, "a"); - if (!logfile) - { + if (!logfile) { fprintf(stderr, "ERROR: unable to open logfile %s: %s\n", name, strerror(errno)); exit(2); @@ -228,34 +232,29 @@ static int logfile_open(const char *name) return 0; } -static int parse_conffile(char *file, int final) +static int parse_conffile(int final) { int err; - FILE *outfd; - err = config_parse_file(file, final); - - if (logfile) - outfd = logfile; - else - outfd = stderr; + err = config_parse_file(final); switch(err) { case 0: return 0; break; case -ERROPEN: - fprintf(outfd, "ERROR: unable to open configfile: %s\n", + ulogd_error("ERROR: unable to open configfile: %s\n", ULOGD_CONFIGFILE); break; case -ERRMAND: - fprintf(outfd, "ERROR: mandatory option not found\n"); + ulogd_error("ERROR: mandatory option not found\n"); break; case -ERRMULT: - fprintf(outfd, "ERROR: option occurred more than once\n"); + ulogd_error("ERROR: option occurred more than once\n"); break; case -ERRUNKN: - fprintf(outfd, "ERROR: unknown config key\n"); + ulogd_error("ERROR: unknown config key, %s\n", + config_errce->key); break; } return 1; @@ -275,14 +274,17 @@ static config_entry_t nlgroup_ce = { NULL, "nlgroup", CONFIG_TYPE_INT, { value: ULOGD_NLGROUP_DEFAULT } }; static int init_conffile(char *file) { - /* linke them together */ + if (config_register_file(file)) + return 1; + + /* link them together */ logf_ce.next = &pldir_ce; pldir_ce.next = &nlgroup_ce; config_register_key(&logf_ce); /* parse config file the first time (for logfile name, ...) */ - return parse_conffile(file, 0); + return parse_conffile(0); } int main(int argc, char* argv[]) @@ -292,19 +294,15 @@ int main(int argc, char* argv[]) size_t len; ulog_packet_msg_t *upkt; - if (init_conffile(ULOGD_CONFIGFILE)) - { - DEBUGP("ERROR during init_configfile\n"); + if (init_conffile(ULOGD_CONFIGFILE)) { 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)) - { + if (parse_conffile(1)) { ulogd_error("ERROR during second parse_conffile\n"); exit(1); } @@ -314,16 +312,14 @@ int main(int argc, char* argv[]) /* create ipulog handle */ h = ipulog_create_handle(ipulog_group2gmask(nlgroup_ce.u.value)); - if (!h) - { + if (!h) { /* if some error occurrs, print it to stderr */ ipulog_perror(NULL); exit(1); } #ifndef DEBUG - if (!fork()) - { + if (!fork()) { fclose(stdout); fclose(stderr); @@ -331,8 +327,7 @@ int main(int argc, char* argv[]) /* endless loop receiving packets and handling them over to * handle_packet */ - while(1) - { + while(1) { len = ipulog_read(h, buf, MYBUFSIZ, 1); upkt = ipulog_get_packet(buf); DEBUGP("==> packet received\n"); @@ -344,8 +339,7 @@ int main(int argc, char* argv[]) free(buf); fclose(logfile); #ifndef DEBUG - } else - { + } else { exit(0); } #endif diff --git a/ulogd/ulogd.conf b/ulogd/ulogd.conf new file mode 100644 index 0000000..27f0394 --- /dev/null +++ b/ulogd/ulogd.conf @@ -0,0 +1,9 @@ +# which netlink multicast group ulogd should listen to +nlgroup 32 + +# logfile +logfile /var/log/ulogd.log + +# dumpfile for ulogd_OPRINT.so +dumpfile /var/log/ulogd.pktlog + -- cgit v1.2.3