From a1277ab45a9d2bab9ca28baf05f978bf8066d928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Ter=C3=A4s?= Date: Tue, 14 Jun 2011 11:18:06 +0300 Subject: filter: support filter-only and track-only modes --- src/squark-filter.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/squark-filter.c b/src/squark-filter.c index 22c8800..db8fb38 100644 --- a/src/squark-filter.c +++ b/src/squark-filter.c @@ -26,11 +26,17 @@ #define FILTER_OVERRIDE_TIMEOUT (15*60) +#define MODE_NOAUTH 0 +#define MODE_TRACK 1 +#define MODE_CAPTIVE 2 + static struct sqdb db; static struct authdb adb; static struct authdb_config adbc; static int running = 1; +static int mode = MODE_CAPTIVE; + static const blob_t dash = BLOB_STR_INIT("-"); static const blob_t space = BLOB_STR_INIT(" "); static const blob_t slash = BLOB_STR_INIT("/"); @@ -401,12 +407,22 @@ static void read_input(struct sqdb *db) else category = 0; - token = authdb_get(&adb, &addr, &entry, 1); - if (authdb_check_login(token, &entry, username, now, &adbc)) { + if (mode == MODE_NOAUTH) { auth_ok = 1; - username = BLOB_STRLEN(entry.p.login_name); } else { - auth_ok = 0; + token = authdb_get(&adb, &addr, &entry, 1); + if (authdb_check_login(token, &entry, username, now, &adbc)) { + auth_ok = 1; + username = BLOB_STRLEN(entry.p.login_name); + } else if (mode == MODE_TRACK) { + auth_ok = 1; + authdb_clear_entry(&entry); + memcpy(entry.p.login_name, username.ptr, username.len); + authdb_commit_login(token, &entry, now, &adbc); + } else { + /* mode == MODE_CAPTIVE, and not authenticated */ + auth_ok = 0; + } } if (!auth_ok) { @@ -429,25 +445,61 @@ static void read_input(struct sqdb *db) } while (b.len); } +static int usage(const char *progname) +{ + fprintf(stderr, + "Usage: %s [-m {captive|track|nauth}]\n" + "\n" + "Program arguments:\n" + " -m Select authentication mode:\n" + " captive captive portal authentication enforced\n" + " track squid authentication is tracked if available\n" + " noauth authentication infromation is ignored (filter only)\n", + progname); + return 1; +} + int main(int argc, char **argv) { - int rc = 1; + int rc = 1, opt; + + while ((opt = getopt(argc, argv, "hm:")) != -1) { + switch (opt) { + case 'm': + if (!strcmp(optarg, "captive")) + mode = MODE_CAPTIVE; + else if (!strcmp(optarg, "track")) + mode = MODE_TRACK; + else if (!strcmp(optarg, "noauth")) + mode = MODE_NOAUTH; + else + return usage(argv[0]); + break; + case 'h': + default: + return usage(argv[0]); + } + } 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 (mode != MODE_NOAUTH) { + if (authdb_open(&adb, &adbc, &db) < 0) { + fprintf(stderr, "Failed to initialize authdb\n"); + goto err_adb; + } } while (running) read_input(&db); rc = 0; - authdb_close(&adb); + if (mode != MODE_NOAUTH) + authdb_close(&adb); err_adb: sqdb_close(&db); err_sqdb: -- cgit v1.2.3