diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2004-03-01 17:03:11 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2004-03-01 17:03:11 +0000 |
commit | 5e10182ce2fa51d51f190e0eafd4b0d1a2358da9 (patch) | |
tree | edcdb8411aaebfbd3ab2d0770612def8eba26141 /libexec | |
parent | 0597ddfc164be74586215c528a55cb929c83b0fd (diff) |
uset inet_pton(3) instead of home grown address validator; some other cleanup
ok beck@
Diffstat (limited to 'libexec')
-rw-r--r-- | libexec/spamd/grey.c | 66 | ||||
-rw-r--r-- | libexec/spamlogd/spamlogd.c | 25 |
2 files changed, 23 insertions, 68 deletions
diff --git a/libexec/spamd/grey.c b/libexec/spamd/grey.c index 1015b25e226..5aeeb01b1d3 100644 --- a/libexec/spamd/grey.c +++ b/libexec/spamd/grey.c @@ -1,4 +1,4 @@ -/* $OpenBSD: grey.c,v 1.6 2004/02/28 00:03:59 beck Exp $ */ +/* $OpenBSD: grey.c,v 1.7 2004/03/01 17:03:10 otto Exp $ */ /* * Copyright (c) 2004 Bob Beck. All rights reserved. @@ -49,36 +49,6 @@ size_t whitecount, whitealloc; char **whitelist; int pfdev; -/* borrowed from dhartmei.. */ -int -address_valid_v4(const char *a) -{ - if (!*a) - return (0); - while (*a) - if ((*a >= '0' && *a <= '9') || *a == '.') - a++; - else - return (0); - return (1); -} - -int -address_valid_v6(const char *a) -{ - if (!*a) - return (0); - while (*a) - if ((*a >= '0' && *a <= '9') || - (*a >= 'a' && *a <= 'f') || - (*a >= 'A' && *a <= 'F') || - *a == ':') - a++; - else - return (0); - return (1); -} - static char *pargv[11]= { "pfctl", "-p", "/dev/pf", "-q", "-t", "spamd-white", "-T", "replace", "-f" "-", NULL @@ -150,26 +120,25 @@ configure_pf(char **addrs, int count) int addwhiteaddr(char *addr) { - struct in_addr ia; + struct in_addr ia; + struct in6_addr ia6; - if (address_valid_v4(addr)) { - if (inet_aton(addr, &ia) == 1) { - if (whitecount == whitealloc) { - char **tmp; + if (inet_pton(AF_INET, addr, &ia) == 1) { + if (whitecount == whitealloc) { + char **tmp; - tmp = realloc(whitelist, - (whitealloc + 1024) * sizeof(char **)); - if (tmp == NULL) - return(-1); - whitelist = tmp; - whitealloc += 1024; - } - whitelist[whitecount] = strdup(addr); - if (whitelist[whitecount] == NULL) + tmp = realloc(whitelist, + (whitealloc + 1024) * sizeof(char *)); + if (tmp == NULL) return(-1); - whitecount++; + whitelist = tmp; + whitealloc += 1024; } - } else if (address_valid_v6(addr)) { + whitelist[whitecount] = strdup(addr); + if (whitelist[whitecount] == NULL) + return(-1); + whitecount++; + } else if (inet_pton(AF_INET6, addr, &ia6) == 1) { /* XXX deal with v6 later */ return(-1); } else @@ -359,6 +328,7 @@ greyreader(void) char ip[32], from[MAX_MAIL], to[MAX_MAIL], *buf; size_t len; int state; + struct in_addr ia; state = 0; if (grey == NULL) @@ -377,7 +347,7 @@ greyreader(void) if (strncmp(buf, "IP:", 3) != 0) break; strlcpy(ip, buf+3, sizeof(ip)); - if (address_valid_v4(ip)) + if (inet_pton(AF_INET, ip, &ia) == 1) state = 1; else state = 0; diff --git a/libexec/spamlogd/spamlogd.c b/libexec/spamlogd/spamlogd.c index a25676caccc..07a7e118d65 100644 --- a/libexec/spamlogd/spamlogd.c +++ b/libexec/spamlogd/spamlogd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: spamlogd.c,v 1.3 2004/02/27 18:25:49 beck Exp $ */ +/* $OpenBSD: spamlogd.c,v 1.4 2004/03/01 17:03:10 otto Exp $ */ /* * Copyright (c) 2004 Bob Beck. All rights reserved. @@ -52,20 +52,6 @@ char **whitelist; int inbound; /* do we only whitelist inbound smtp? */ int pfdev; -/* borrowed from dhartmei.. */ -static int -address_valid_v4(const char *a) -{ - if (!*a) - return (0); - while (*a) - if ((*a >= '0' && *a <= '9') || *a == '.') - a++; - else - return (0); - return (1); -} - int dbupdate(char *dbname, char *ip) { @@ -75,13 +61,14 @@ dbupdate(char *dbname, char *ip) struct gdata gd; time_t now; int r; + struct in_addr ia; now = time(NULL); memset(&btreeinfo, 0, sizeof(btreeinfo)); db = dbopen(dbname, O_EXLOCK|O_RDWR, 0600, DB_BTREE, &btreeinfo); if (db == NULL) return(-1); - if (!address_valid_v4(ip)) { + if (inet_pton(AF_INET, ip, &ia) != 1) { syslog_r(LOG_NOTICE, &sdata, "invalid ip address %s", ip); goto bad; } @@ -276,10 +263,8 @@ main(int argc, char **argv) if (cp != NULL) dbupdate(PATH_SPAMD_DB, cp); - if (lbuf != NULL) { - free(lbuf); - lbuf = NULL; - } + free(lbuf); + lbuf = NULL; } exit(0); } |