From cb2db2f1c759c60849b66dabf8ee03c9cdfb2dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Ter=C3=A4s?= Date: Sun, 7 Nov 2010 01:37:06 +0200 Subject: all: check error for sqdb_open and adb_open so we don't crash on startup if essential files are missing. fixes #454 --- src/Makefile | 8 +++++--- src/squark-auth-ip.c | 23 +++++++++++++++++++---- src/squark-auth-snmp.c | 25 ++++++++++++++++++++----- src/squark-filter.c | 20 +++++++++++++++++--- 4 files changed, 61 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 5863414..eb24877 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,14 +2,16 @@ progs-y += squark-filter squark-auth-snmp squark-auth-ip shlibs-y += squarkdb.so scripts-y += sqdb-build.lua -common-objs += filterdb.o authdb.o blob.o addr.o +common-objs += filterdb.o authdb.o blob.o addr.o config.o squark-filter-objs += squark-filter.o $(common-objs) squark-auth-snmp-objs += squark-auth-snmp.o $(common-objs) squark-auth-ip-objs += squark-auth-ip.o $(common-objs) squarkdb.so-objs += lua-squarkdb.o filterdb.o blob.o -CFLAGS += -DSQUARK_VERSION=\"$(FULL_VERSION)\" +CFLAGS_config.o += -DSQUARK_VERSION=\"$(FULL_VERSION)\" \ + -DCONFDIR=\"$(CONFDIR)\" \ + -DLIBDIR=\"$(LIBDIR)\" LIBS += -lrt CFLAGS_lua-squarkdb.o += $(shell pkg-config --cflags lua5.1) @@ -22,4 +24,4 @@ install: $(INSTALL) $(addprefix $(obj)/,$(progs-y) $(scripts-y)) $(DESTDIR)$(BINDIR) $(INSTALLDIR) $(DESTDIR)$(LUALIBDIR) - $(INSTALL) $(addprefix $(obj)/,$(shlibs-y)) $(DESTDIR)$(LUALIBDIR) \ No newline at end of file + $(INSTALL) $(addprefix $(obj)/,$(shlibs-y)) $(DESTDIR)$(LUALIBDIR) diff --git a/src/squark-auth-ip.c b/src/squark-auth-ip.c index 3cdea0b..08adca9 100644 --- a/src/squark-auth-ip.c +++ b/src/squark-auth-ip.c @@ -14,6 +14,7 @@ #include #include +#include "config.h" #include "blob.h" #include "authdb.h" #include "filterdb.h" @@ -113,12 +114,15 @@ static void read_input(void) int main(int argc, char **argv) { - int opt; + int opt, rc = 1; sockaddr_any ipaddr = { .any.sa_family = AF_UNSPEC }; blob_t ip = BLOB_NULL, username = BLOB_NULL; - while ((opt = getopt(argc, argv, "i:u:olpL")) != -1) { + while ((opt = getopt(argc, argv, "Vi:u:olpL")) != -1) { switch (opt) { + case 'V': + fprintf(stderr, "squark-auth-ip %s\n", squark_version); + return 0; case 'i': ip = BLOB_STRLEN(optarg); if (!addr_parse(ip, &ipaddr)) { @@ -146,8 +150,15 @@ int main(int argc, char **argv) } now = time(NULL); - sqdb_open(&db, "/var/lib/squark/squark.db"); - authdb_open(&adb, &adbc, &db); + if (sqdb_open(&db, squark_dbname) < 0) { + fprintf(stderr, "%s: failed to open squarkdb\n", + squark_dbname); + goto err_sqdb; + } + if (authdb_open(&adb, &adbc, &db) < 0) { + fprintf(stderr, "Failed to initialize authdb\n"); + goto err_adb; + } if (running < 0) { struct authdb_entry entry; @@ -212,7 +223,11 @@ int main(int argc, char **argv) while (running) read_input(); } + rc = 0; authdb_close(&adb); +err_adb: sqdb_close(&db); +err_sqdb: + return rc; } diff --git a/src/squark-auth-snmp.c b/src/squark-auth-snmp.c index aa45c73..40ef6b5 100644 --- a/src/squark-auth-snmp.c +++ b/src/squark-auth-snmp.c @@ -26,6 +26,7 @@ #include #include +#include "config.h" #include "blob.h" #include "addr.h" #include "authdb.h" @@ -1063,13 +1064,16 @@ int main(int argc, char **argv) struct timeval timeout; sockaddr_any addr; fd_set fdset; - int opt, fds, block, i; + int opt, fds, block, i, rc = 1; setenv("MIBS", "", 1); init_snmp("squark-auth"); - while ((opt = getopt(argc, argv, "c:r:i:R:v:f:T:K")) != -1) { + while ((opt = getopt(argc, argv, "Vc:r:i:R:v:f:T:K")) != -1) { switch (opt) { + case 'V': + fprintf(stderr, "squark-auth-snmp %s\n", squark_version); + return 0; case 'c': snmp_community = optarg; break; @@ -1104,8 +1108,15 @@ int main(int argc, char **argv) return 1; } - sqdb_open(&db, "/var/lib/squark/squark.db"); - authdb_open(&adb, &adbc, &db); + if (sqdb_open(&db, squark_dbname) < 0) { + fprintf(stderr, "%s: failed to open squarkdb\n", + squark_dbname); + goto err_sqdb; + } + if (authdb_open(&adb, &adbc, &db) < 0) { + fprintf(stderr, "Failed to initialize authdb\n"); + goto err_adb; + } if (l2_root == NULL) l2_root = l3_root; @@ -1145,8 +1156,12 @@ int main(int argc, char **argv) } else snmp_timeout(); } + rc = 0; + authdb_close(&adb); +err_adb: sqdb_close(&db); +err_sqdb: - return 0; + return rc; } diff --git a/src/squark-filter.c b/src/squark-filter.c index 14515cf..567201a 100644 --- a/src/squark-filter.c +++ b/src/squark-filter.c @@ -18,6 +18,7 @@ #include +#include "config.h" #include "blob.h" #include "addr.h" #include "filterdb.h" @@ -422,12 +423,25 @@ static void read_input(struct sqdb *db) int main(int argc, char **argv) { - sqdb_open(&db, "/var/lib/squark/squark.db"); - authdb_open(&adb, &adbc, &db); + int rc = 1; + + if (sqdb_open(&db, squark_dbname) < 0) { + fprintf(stderr, "%s: failed to open squarkdb\n", + squark_dbname); + goto err_sqdb; + } + if (authdb_open(&adb, &adbc, &db) < 0) { + fprintf(stderr, "Failed to initialize authdb\n"); + goto err_adb; + } while (running) read_input(&db); + rc = 0; - sqdb_close(&db); authdb_close(&adb); +err_adb: + sqdb_close(&db); +err_sqdb: + return rc; } -- cgit v1.2.3