From 4be1e8fd5e352a54e87a81979d5ad303efdcbffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Ter=C3=A4s?= Date: Tue, 14 Sep 2010 13:06:29 +0300 Subject: auth-ip: print information in shell compatible format so we can dump information from http cgi-bin scripts. --- addr.c | 21 +++++++++++++++++++++ addr.h | 1 + blob.c | 12 ++++++++++++ blob.h | 1 + squark-auth-ip.c | 51 +++++++++++++++++++++++++++------------------------ 5 files changed, 62 insertions(+), 24 deletions(-) diff --git a/addr.c b/addr.c index 1dddaff..47013f2 100644 --- a/addr.c +++ b/addr.c @@ -1,3 +1,4 @@ +#include #include #include "addr.h" @@ -51,3 +52,23 @@ blob_t addr_get_hostaddr_blob(const sockaddr_any *addr) return BLOB_NULL; } } + +void addr_push_hostaddr(blob_t *b, const sockaddr_any *addr) +{ + char buf[64]; + blob_t f; + unsigned int t; + + switch (addr->any.sa_family) { + case AF_INET: + t = ntohl(addr->ipv4.sin_addr.s_addr); + f.ptr = buf; + f.len = sprintf(buf, "%d.%d.%d.%d", + (t ) & 0xff, (t >> 8) & 0xff, + (t >> 16) & 0xff, (t >> 24) & 0xff); + break; + default: + return; + } + blob_push(b, f); +} diff --git a/addr.h b/addr.h index 00e1833..452d14b 100644 --- a/addr.h +++ b/addr.h @@ -14,6 +14,7 @@ sockaddr_any *addr_parse(blob_t text, sockaddr_any *addr); unsigned long addr_hash(const sockaddr_any *addr); const char *addr_print(const sockaddr_any *addr); blob_t addr_get_hostaddr_blob(const sockaddr_any *addr); +void addr_push_hostaddr(blob_t *b, const sockaddr_any *addr); static inline void addr_copy(sockaddr_any *dst, const sockaddr_any *src) { diff --git a/blob.c b/blob.c index f2a80f6..2c5dd17 100644 --- a/blob.c +++ b/blob.c @@ -1,3 +1,4 @@ +#include #include #include @@ -208,6 +209,17 @@ void blob_push_uint(blob_t *to, unsigned int value, int radix) blob_push(to, BLOB_PTR_PTR(ptr+1, &buf[sizeof(buf)-1])); } +void blob_push_ctime(blob_t *to, time_t t) +{ + char buf[128]; + blob_t b; + + ctime_r(&t, buf); + b = BLOB_STRLEN(buf); + b.len--; + blob_push(to, b); +} + void blob_push_hexdump(blob_t *to, blob_t binary) { char *d; diff --git a/blob.h b/blob.h index 0c10ca6..76afed7 100644 --- a/blob.h +++ b/blob.h @@ -45,6 +45,7 @@ void blob_push(blob_t *b, blob_t d); void blob_push_lower(blob_t *b, blob_t d); void blob_push_byte(blob_t *b, unsigned char byte); void blob_push_uint(blob_t *to, unsigned int value, int radix); +void blob_push_ctime(blob_t *to, time_t t); void blob_push_hexdump(blob_t *to, blob_t binary); void blob_push_urldecode(blob_t *to, blob_t url); void blob_push_urlencode(blob_t *to, blob_t url); diff --git a/squark-auth-ip.c b/squark-auth-ip.c index 602969b..1346a2e 100644 --- a/squark-auth-ip.c +++ b/squark-auth-ip.c @@ -101,6 +101,13 @@ static void read_input(void) } while (b.len); } +#define DUMPPAR(b, name, fn) \ + do { \ + blob_push(b, BLOB_STR("squark_" name "='")); \ + fn; \ + blob_push(b, BLOB_STR("'; ")); \ + } while (0) + int main(int argc, char **argv) { int opt; @@ -141,7 +148,6 @@ int main(int argc, char **argv) if (running < 0) { struct authdb_entry entry; void *token; - char buf1[64], buf2[64], buf3[64]; if (ipaddr.any.sa_family == AF_UNSPEC) { fprintf(stderr, "IP-address not specified\n"); @@ -168,30 +174,27 @@ int main(int argc, char **argv) if (authdb_check_login(token, &entry, username, now)) authdb_commit_override(token, &entry, now); break; - case DO_PRINT: - fprintf(stdout, - "IP-address: %s\n" - "Username: %s\n" - "MAC-address: %02x:%02x:%02x:%02x:%02x:%02x\n" - "Soft block mask: %016llx\n" - "Hard block mask: %016llx\n" - "Login time: %s" - "Last activity time: %s" - "Override time: %s", - ip.ptr, - entry.p.login_name, - entry.p.mac_address[0], - entry.p.mac_address[1], - entry.p.mac_address[2], - entry.p.mac_address[3], - entry.p.mac_address[4], - entry.p.mac_address[5], - entry.p.block_categories, - entry.p.hard_block_categories, - entry.p.login_time ? ctime_r(&entry.p.login_time, buf1) : "\n", - entry.last_activity_time ? ctime_r(&entry.last_activity_time, buf2) : "\n", - entry.override_time ? ctime_r(&entry.override_time, buf3) : "\n"); + case DO_PRINT: { + char buf[512]; + blob_t b = BLOB_BUF(buf); + + DUMPPAR(&b, "ip_address", + addr_push_hostaddr(&b, &ipaddr)); + DUMPPAR(&b, "username", + blob_push(&b, BLOB_BUF(entry.p.login_name))); + DUMPPAR(&b, "mac_address", + blob_push_hexdump(&b, BLOB_BUF(entry.p.mac_address))); + DUMPPAR(&b, "login_time", + blob_push_ctime(&b, entry.p.login_time)); + DUMPPAR(&b, "activity_time", + blob_push_ctime(&b, entry.last_activity_time)); + DUMPPAR(&b, "override_time", + blob_push_ctime(&b, entry.override_time)); + blob_push(&b, BLOB_STR("\n")); + b = blob_pushed(BLOB_BUF(buf), b); + fwrite(b.ptr, b.len, 1, stdout); break; + } case DO_LOGOUT: if (authdb_check_login(token, &entry, username, now)) authdb_commit_logout(token); -- cgit v1.2.3