summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlaforge <laforge>2001-10-02 16:39:17 +0000
committerlaforge <laforge>2001-10-02 16:39:17 +0000
commita43bcc8b4e138435618394e19b222af6df84a51a (patch)
tree63f9c415dfbfbe4a4f6f22c935d5f83c3fb45237
parent69cf8cb0eded79a9b8fda0edf8175497c39bceb2 (diff)
Added Jakab Laszlo's support for PostgreSQL
-rw-r--r--Changes1
-rw-r--r--Makefile.in9
-rw-r--r--Rules.make.in6
-rw-r--r--configure.in55
-rw-r--r--pgsql/Makefile.in30
-rw-r--r--pgsql/ulogd_PGSQL.c370
-rw-r--r--ulogd.conf15
7 files changed, 480 insertions, 6 deletions
diff --git a/Changes b/Changes
index df04f5f..13237c1 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,6 @@
Version 0.98
- Fix MAC address printing if there is none (by Andrej Ota)
+- Add PostgreSQL support by Jakab Laszlo
Version 0.97
- added error handling after ipulog_read() to prevent endless loops
diff --git a/Makefile.in b/Makefile.in
index 609ac55..a0f353f 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -10,6 +10,11 @@ else
SUBDIRS+=mysql
endif
+ifeq (x@PGSQLINCLUDES@,x)
+else
+SUBDIRS+=pgsql
+endif
+
# Normally You should not need to change anything below
all: recurse ulogd
@@ -35,8 +40,8 @@ distclean: clean
install: all
@for d in $(SUBDIRS); do if ! make -C $$d $@; then exit 1; fi; done
- @INSTALL@ -m 755 ulogd $(DESTDIR)@sbindir@/ulogd
- @INSTALL@ -m 600 ulogd.conf $(DESTDIR)@sysconfdir@/ulogd.conf
+ @INSTALL@ -D -m 755 ulogd $(DESTDIR)@sbindir@/ulogd
+ @INSTALL@ -D -m 600 ulogd.conf $(DESTDIR)@sysconfdir@/ulogd.conf
doc:
$(MAKE) -C $@
diff --git a/Rules.make.in b/Rules.make.in
index 1be476e..c8f7468 100644
--- a/Rules.make.in
+++ b/Rules.make.in
@@ -20,7 +20,7 @@ CFLAGS+=-DULOGD_CONFIGFILE=\"$(ULOGD_CONFIGFILE)\"
#CFLAGS+=$(INCIPULOG) $(INCCONFFILE)
CFLAGS+=-I/usr/src/linux/include
#CFLAGS+=@DEFS@
-#CFLAGS+=-g -DDEBUG -DDEBUG_MYSQL
+#CFLAGS+=-g -DDEBUG -DDEBUG_MYSQL -DDEBUG_PGSQL
LIBS=@LIBS@
@@ -33,3 +33,7 @@ ULOGD_SL:=BASE OPRINT PWSNIFF LOGEMU
MYSQL_CFLAGS=-I@MYSQLINCLUDES@ @EXTRA_MYSQL_DEF@
MYSQL_LDFLAGS=@DATABASE_LIB_DIR@ @DATABASE_LIB@
+# postgreSQL output support
+#ULOGD_SL+=PGSQL
+PGSQL_CFLAGS=-I@PGSQLINCLUDES@
+PGSQL_LDFLAGS=@DATABASE_LIB_DIR@ @DATABASE_LIB@
diff --git a/configure.in b/configure.in
index fc4bb73..63303e2 100644
--- a/configure.in
+++ b/configure.in
@@ -108,6 +108,7 @@ else
fi
])
+
dnl
dnl Check whether the user wants log IP-addresses as strings rather
dnl than as unsigned long-integers to his MySQL-database. Since this
@@ -123,6 +124,56 @@ AC_ARG_WITH(mysql-log-ip-as-string,
AC_MSG_WARN(the use of --with-mysql-log-ip-as-string is discouraged)
])
+
+dnl
+dnl test for PostgreSQL
+dnl
+AC_ARG_WITH(pgsql,
+ --with-pgsql=<directory> pgsql installed in <directoty>,[
+if test $withval != yes
+then
+ dir=$withval
+else
+ dir="/usr/local"
+fi
+pgsqldir=""
+AC_MSG_CHECKING(for PGSQL files)
+for d in $dir /usr /usr/local /usr/local/pgsql /opt/pgsql /opt/packages/pgsql
+do
+ if test -f $d/lib/pgsql/libpq.so
+ then
+ AC_MSG_RESULT(found pgsql in $d)
+ pgsqldir=$d
+ break
+ fi
+ if test -f $d/lib/libpq.so
+ then
+ AC_MSG_RESULT(found pgsql in $d)
+ pgsqldir=$d
+ break
+ fi
+done
+
+if test x$pgsqldir = x
+then
+ AC_MSG_WARN(PGSQL backend not used)
+else
+ AC_DEFINE(HAVE_PGSQL)
+ PGSQLINCLUDES=${pgsqldir}/include
+ PGSQLLIBS=${pgsqldir}/lib
+
+ DATABASE_DIR="${DATABASE_DIR} pgsql"
+
+ DATABASE_LIB="${DATABASE_LIB} -lpq "
+ DATABASE_LIB_DIR="${DATABASE_LIB_DIR} -L${PGSQLLIBS} "
+
+ DB_DEF="${DB_DEF} -DHAVE_PGSQL "
+
+ AC_SUBST(PGSQLINCLUDES)
+
+fi
+])
+
AC_SUBST(DATABASE_DIR)
AC_SUBST(DATABASE_LIB)
AC_SUBST(DATABASE_LIB_DIR)
@@ -132,6 +183,6 @@ AC_SUBST(EXTRA_MYSQL_DEF)
AC_SUBST(DATABASE_DRIVERS)
AM_CONDITIONAL(HAVE_MYSQL, test x$mysqldir != x)
+AM_CONDITIONAL(HAVE_PGSQL, test x$pgsqldir != x)
-
-AC_OUTPUT(extensions/Makefile doc/Makefile conffile/Makefile libipulog/Makefile mysql/Makefile Makefile Rules.make)
+AC_OUTPUT(extensions/Makefile doc/Makefile conffile/Makefile libipulog/Makefile mysql/Makefile pgsql/Makefile Makefile Rules.make)
diff --git a/pgsql/Makefile.in b/pgsql/Makefile.in
new file mode 100644
index 0000000..2f65173
--- /dev/null
+++ b/pgsql/Makefile.in
@@ -0,0 +1,30 @@
+#
+
+# Normally You should not need to change anything below
+#
+include @top_srcdir@/Rules.make
+
+CFLAGS+=-I@top_srcdir@ -I@top_srcdir@/libipulog/include -I@top_srcdir@/conffile
+SH_CFLAGS:=$(CFLAGS) -fPIC
+
+SHARED_LIBS=ulogd_PGSQL.so
+
+all: $(SHARED_LIBS)
+
+distrib:
+
+$(SHARED_LIBS): %.so: %_sh.o
+ ld -shared $(PGSQL_LDFLAGS) -o $@ $<
+
+%_sh.o: %.c
+ $(CC) $(PGSQL_CFLAGS) $(SH_CFLAGS) -o $@ -c $<
+
+clean:
+ rm -f $(SHARED_LIBS) *.o
+
+distclean:
+ rm -f Makefile
+
+install: all
+ @INSTALL@ -m 755 -d $(DESTDIR)$(ULOGD_LIB_PATH)
+ @INSTALL@ -m 755 *.so $(DESTDIR)$(ULOGD_LIB_PATH)
diff --git a/pgsql/ulogd_PGSQL.c b/pgsql/ulogd_PGSQL.c
new file mode 100644
index 0000000..a54a5ed
--- /dev/null
+++ b/pgsql/ulogd_PGSQL.c
@@ -0,0 +1,370 @@
+/* ulogd_PGSQL.c, Version 1.1
+ *
+ * ulogd output plugin for logging to a PGSQL database
+ *
+ * (C) 2000 by Harald Welte <laforge@gnumonks.org>
+ * This software is distributed under the terms of GNU GPL
+ *
+ * This plugin is based on the MySQL plugin made by Harald Welte.
+ * The support PostgreSQL were made by Jakab Laszlo.
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <ulogd.h>
+#include <libpq-fe.h>
+#include "ulogd.h"
+#include "conffile.h"
+
+#ifdef DEBUG_PQSQL
+#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
+#else
+#define DEBUGP(x, args...)
+#endif
+
+struct _field {
+ char name[ULOGD_MAX_KEYLEN];
+ unsigned int id;
+ struct _field *next;
+};
+
+/* the database handle we are using */
+static PGconn *dbh;
+
+/* a linked list of the fields the table has */
+static struct _field *fields;
+
+/* buffer for our insert statement */
+static char *stmt;
+
+/* pointer to the beginning of the "VALUES" part */
+static char *stmt_val;
+
+/* pointer to current inser position in statement */
+static char *stmt_ins;
+
+/* our configuration directives */
+static config_entry_t db_ce = { NULL, "pgsqldb", CONFIG_TYPE_STRING,
+ CONFIG_OPT_MANDATORY, 0,
+ { } };
+
+static config_entry_t host_ce = { &db_ce, "pgsqlhost", CONFIG_TYPE_STRING,
+ CONFIG_OPT_MANDATORY, 0,
+ { } };
+
+static config_entry_t user_ce = { &host_ce, "pgsqluser", CONFIG_TYPE_STRING,
+ CONFIG_OPT_MANDATORY, 0,
+ { } };
+
+static config_entry_t pass_ce = { &user_ce, "pgsqlpass", CONFIG_TYPE_STRING,
+ CONFIG_OPT_MANDATORY, 0,
+ { } };
+
+static config_entry_t table_ce = { &pass_ce, "pgsqltable", CONFIG_TYPE_STRING,
+ CONFIG_OPT_MANDATORY, 0,
+ { } };
+
+/* is the given string a field in our table? */
+static int is_field(const char *name)
+{
+ struct _field *f;
+
+ for (f = fields; f; f = f->next) {
+ if (!strcmp(f->name, name))
+ return 1;
+ }
+ return 0;
+}
+
+/* our main output function, called by ulogd */
+static int _pgsql_output(ulog_iret_t *result)
+{
+ struct _field *f;
+ ulog_iret_t *res;
+ PGresult *pgres;
+
+ char *tmpstr;
+
+ stmt_ins = stmt_val;
+
+ for (f = fields; f; f = f->next) {
+ res = keyh_getres(f->id);
+
+ if (!res) {
+ ulogd_log(ULOGD_NOTICE,
+ "no result for %s ?!?\n", f->name);
+ }
+
+ if (!res || !IS_VALID((*res))) {
+ /* no result, we have to fake something */
+ sprintf(stmt_ins, "NULL,");
+ stmt_ins = stmt + strlen(stmt);
+ continue;
+ }
+
+ switch (res->type) {
+ case ULOGD_RET_INT8:
+ sprintf(stmt_ins, "%d,", res->value.i8);
+ break;
+ case ULOGD_RET_INT16:
+ sprintf(stmt_ins, "%d,", res->value.i16);
+ break;
+ case ULOGD_RET_INT32:
+ sprintf(stmt_ins, "%d,", res->value.i32);
+ break;
+ case ULOGD_RET_INT64:
+ sprintf(stmt_ins, "%ld,", res->value.i64);
+ break;
+ case ULOGD_RET_UINT8:
+ sprintf(stmt_ins, "%u,", res->value.ui8);
+ break;
+ case ULOGD_RET_UINT16:
+ sprintf(stmt_ins, "%u,", res->value.ui16);
+ break;
+ case ULOGD_RET_IPADDR:
+/* #ifdef IP_AS_STRING
+ *stmt_ins++ = '\'';
+ tmpstr = inet_ntoa(ntohl(res->value.ui32));
+#ifdef OLD_PGSQL
+ pgsql_escape_string(stmt_ins, tmpstr,
+ strlen(tmpstr));
+#else
+ pgsql_real_escape_string(dbh, stmt_ins,
+ tmpstr,
+ strlen(tmpstr));
+#endif /* OLD_PGSQL */
+/*
+ stmt_ins = stmt + strlen(stmt);
+ sprintf(stmt_ins, "',");
+ break;
+#endif /* IP_AS_STRING */
+ /* EVIL: fallthrough when logging IP as
+ * u_int32_t */
+
+ case ULOGD_RET_UINT32:
+ sprintf(stmt_ins, "%u,", res->value.ui32);
+ break;
+ case ULOGD_RET_UINT64:
+ sprintf(stmt_ins, "%lu,", res->value.ui64);
+ break;
+ case ULOGD_RET_BOOL:
+ sprintf(stmt_ins, "1,");
+ break;
+ case ULOGD_RET_STRING:
+ *stmt_ins++ = '\'';
+/* Change by Jaki
+#ifdef OLD_PGSQL
+ pgsql_escape_string(stmt_ins, res->value.ptr,
+ strlen(res->value.ptr));
+#else
+ pgsql_real_escape_string(dbh, stmt_ins,
+ res->value.ptr, strlen(res->value.ptr));
+#endif
+*End Change by Jaki
+*/
+ strcpy(stmt_ins,"\'");
+ strcpy(stmt_ins,res->value.ptr); /* Added by Jaki */
+ stmt_ins = stmt + strlen(stmt);
+ sprintf(stmt_ins, "',");
+ /* sprintf(stmt_ins, "'%s',", res->value.ptr); */
+ break;
+ default:
+ ulogd_log(ULOGD_NOTICE,
+ "unknown type %d for %s\n",
+ res->type, res->key);
+ break;
+ }
+ stmt_ins = stmt + strlen(stmt);
+ }
+ *(stmt_ins - 1) = ')';
+ DEBUGP("stmt=#%s#\n", stmt);
+
+ /* now we have created our statement, insert it */
+ /* Added code by Jaki */
+ pgres = PQexec(dbh, stmt);
+ if(!pgres || PQresultStatus(pgres) != PGRES_COMMAND_OK) {
+ ulogd_log(ULOGD_ERROR, "sql error during insert: %s\n",
+ PQresultErrorMessage(pgres));
+ return 1;
+ }
+
+ return 0;
+}
+
+#define PGSQL_INSERTTEMPL "insert into X (Y) values (Z)"
+#define PGSQL_VALSIZE 100
+
+/* create the static part of our insert statement */
+static int _pgsql_createstmt(void)
+{
+ struct _field *f;
+ unsigned int size;
+ char buf[ULOGD_MAX_KEYLEN];
+ char *underscore;
+
+ if (stmt) {
+ ulogd_log(ULOGD_NOTICE, "createstmt called, but stmt"
+ " already existing\n");
+ return 1;
+ }
+
+ /* caclulate the size for the insert statement */
+ size = strlen(PGSQL_INSERTTEMPL) + strlen(table_ce.u.string);
+
+ for (f = fields; f; f = f->next) {
+ /* we need space for the key and a comma, as well as
+ * enough space for the values */
+ size += strlen(f->name) + 1 + PGSQL_VALSIZE;
+ }
+
+ ulogd_log(ULOGD_DEBUG, "allocating %u bytes for statement\n", size);
+
+ stmt = (char *) malloc(size);
+
+ if (!stmt) {
+ ulogd_log(ULOGD_ERROR, "OOM!\n");
+ return 1;
+ }
+
+ sprintf(stmt, "insert into %s (", table_ce.u.string);
+ stmt_val = stmt + strlen(stmt);
+
+ for (f = fields; f; f = f->next) {
+ strncpy(buf, f->name, ULOGD_MAX_KEYLEN);
+ while (underscore = strchr(buf, '.'))
+ *underscore = '_';
+ sprintf(stmt_val, "%s,", buf);
+ stmt_val = stmt + strlen(stmt);
+ }
+ *(stmt_val - 1) = ')';
+
+ sprintf(stmt_val, " values (");
+ stmt_val = stmt + strlen(stmt);
+
+ ulogd_log(ULOGD_DEBUG, "stmt='%s'\n", stmt);
+
+ return 0;
+}
+
+/* find out which columns the table has */
+static int _pgsql_get_columns(const char *table)
+{
+ PGresult *result;
+ char buf[ULOGD_MAX_KEYLEN];
+ char pgbuf[256];
+ char *underscore;
+ struct _field *f;
+ int id;
+ int intaux;
+ char *sql;
+
+ if (!dbh)
+ return 1;
+
+ strcpy(pgbuf, "SELECT a.attname FROM pg_class c, pg_attribute a WHERE c.relname ='");
+ strncat(pgbuf, table, strlen(table));
+ strcat(pgbuf, "' AND a.attnum>0 AND a.attrelid=c.oid ORDER BY a.attnum");
+ sql = (char *) malloc(strlen(pgbuf));
+ sql = pgbuf;
+ ulogd_log(ULOGD_DEBUG, sql);
+ result = PQexec(dbh, sql);
+ if(!result) {
+ ulogd_log(ULOGD_DEBUG, "\n result false");
+ return 1;
+ }
+ if (PQresultStatus(result) != PGRES_TUPLES_OK) {
+ ulogd_log(ULOGD_DEBUG, "\n pres_command_not_ok");
+ return 1;
+ }
+ for (intaux=0; intaux<PQntuples(result); intaux++) {
+
+ /* replace all underscores with dots */
+ strncpy(buf, PQgetvalue(result, intaux, 0), ULOGD_MAX_KEYLEN);
+ while (underscore = strchr(buf, '_'))
+ *underscore = '.';
+
+ DEBUGP("field '%s' found: ", buf);
+
+ if (!(id = keyh_getid(buf))) {
+ DEBUGP(" no keyid!\n");
+ continue;
+ }
+
+ DEBUGP("keyid %u\n", id);
+
+ /* prepend it to the linked list */
+ f = (struct _field *) malloc(sizeof *f);
+ if (!f) {
+ ulogd_log(ULOGD_ERROR, "OOM!\n");
+ return 1;
+ }
+ strncpy(f->name, buf, ULOGD_MAX_KEYLEN);
+ f->id = id;
+ f->next = fields;
+ fields = f;
+ }
+
+ PQclear(result);
+ return 0;
+}
+
+int exit_nicely(PGconn *conn)
+{
+ PQfinish(conn);
+ return 0;;
+}
+
+/* make connection and select database */
+static int _pgsql_open_db(char *server, char *user, char *pass, char *db)
+{
+ char connstr[80];
+ char * sql;
+
+ strcpy(connstr, " host=");
+ strcat(connstr, server);
+ strcat(connstr, " dbname=");
+ strcat(connstr, db);
+ strcat(connstr, " user=");
+ strcat(connstr, user);
+ strcat(connstr, " password=");
+ strcat(connstr, pass);
+ sql = (char *) malloc(strlen(connstr));
+
+ sql = connstr;
+ dbh = PQconnectdb(sql);
+ if (PQstatus(dbh)!=CONNECTION_OK) {
+ exit_nicely(dbh);
+ return 1;
+ }
+
+
+ return 0;
+}
+
+
+static ulog_output_t _pgsql_plugin = { NULL, "pgsql", &_pgsql_output, NULL };
+
+void _init(void)
+{
+ /* register our configfile options here */
+ config_register_key(&table_ce);
+
+ /* have the opts parsed */
+ config_parse_file(0);
+
+ if (_pgsql_open_db(host_ce.u.string, user_ce.u.string,
+ pass_ce.u.string, db_ce.u.string)) {
+ ulogd_log(ULOGD_ERROR, "can't establish database connection\n");
+ return;
+ }
+
+ /* read the fieldnames to know which values to insert */
+ if (_pgsql_get_columns(table_ce.u.string)) {
+ ulogd_log(ULOGD_ERROR, "unable to get pgsql columns\n");
+ return;
+ }
+ _pgsql_createstmt();
+ register_output(&_pgsql_plugin);
+
+}
diff --git a/ulogd.conf b/ulogd.conf
index 9fd72d9..a4d1fc1 100644
--- a/ulogd.conf
+++ b/ulogd.conf
@@ -1,5 +1,5 @@
# Example configuration for ulogd
-# $Id$
+# $Id: ulogd.conf,v 1.5 2001/05/20 14:44:37 laforge Exp $
#
######################################################################
@@ -63,3 +63,16 @@ mysqldb ulogd
mysqlhost localhost
# load the plugin (remove the '#' if you want to enable it)
#plugin /usr/local/lib/ulogd/ulogd_MYSQL.so
+
+
+#
+# ulogd_PGSQL.so: optional logging into a PostgreSQL database
+#
+# database information
+pgsqltable ulog
+pgsqlpass
+pgsqluser postgres
+pgsqldb ulogd
+pgsqlhost localhost
+#load the plugin (remove the '#' if you want to enable it)
+#plugin /usr/local/lib/ulogd/ulogd_PGSQL.so