summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Beck <beck@cvs.openbsd.org>2006-12-09 17:13:08 +0000
committerBob Beck <beck@cvs.openbsd.org>2006-12-09 17:13:08 +0000
commit2330caeb5ddbb29407b3c9a050e95b729b1d0db8 (patch)
treefceab723f8245f9460c7a7002c4ff6714feaf076
parent231b6827bc3d1097cd240ac26cb0e5cb0f482e3f (diff)
make spamdb take multiple add/delete arguments, thus making it
much easier to do bulk add/deletes without opening and locking the database for each one. testing by scads of people on tech@ Ok deraadt@ jmc@
-rw-r--r--usr.sbin/spamdb/spamdb.851
-rw-r--r--usr.sbin/spamdb/spamdb.c62
2 files changed, 54 insertions, 59 deletions
diff --git a/usr.sbin/spamdb/spamdb.8 b/usr.sbin/spamdb/spamdb.8
index 467878644e7..7c3b8682e4e 100644
--- a/usr.sbin/spamdb/spamdb.8
+++ b/usr.sbin/spamdb/spamdb.8
@@ -1,4 +1,4 @@
-.\" $OpenBSD: spamdb.8,v 1.8 2005/10/07 21:41:24 jmc Exp $
+.\" $OpenBSD: spamdb.8,v 1.9 2006/12/09 17:13:07 beck Exp $
.\"
.\" Copyright (c) 2004 Bob Beck. All rights reserved.
.\"
@@ -22,9 +22,8 @@
.Nd spamd database tool
.Sh SYNOPSIS
.Nm spamdb
-.Op Fl Tt
-.Op Fl a Ar key
-.Op Fl d Ar key
+.Op Fl adTt
+.Op Ar keys ...
.Sh DESCRIPTION
.Nm
manipulates the spamd database in
@@ -35,22 +34,22 @@ greylisting.
.Pp
The options are as follows:
.Bl -tag -width Ds
-.It Fl a Ar key
-Add or update an entry for
-.Ar key .
-This can be used to whitelist an IP address
+.It Fl a
+Add or update the entries for
+.Ar keys .
+This can be used to whitelist one or more IP addresses
(i.e. circumvent the greylisting process altogether)
-by adding it to the spamd database as a WHITE entry.
-If the
-.Ar key
-specified matches an entry already in the spamd database,
+by adding all IP addresses as keys to the spamd database for WHITE entries.
+If any
+.Ar keys
+specified match entries already in the spamd database,
.Nm
updates the entry's time last seen to now.
-.It Fl d Ar key
-Delete an entry for
-.Ar key .
+.It Fl d
+Delete entries for
+.Ar keys .
.It Fl T
-Add or delete the key as a SPAMTRAP entry.
+Add or delete the keys as SPAMTRAP entries.
See the
.Sx GREYTRAPPING
section of
@@ -62,7 +61,7 @@ or
.Fl d
option.
.It Fl t
-Add or delete the key as a TRAPPED entry.
+Add or delete the keys as TRAPPED entries.
See the
.Sx GREYTRAPPING
section of
@@ -77,14 +76,14 @@ option.
.Pp
If adding or deleting a SPAMTRAP address
.Pq Fl T ,
-.Ar key
-should be specified as an email address:
+.Ar keys
+should be specified as email addresses:
.Pp
.Dl <spamtrap@mydomain.org>
.Pp
Otherwise
-.Ar key
-should be a numerical IP address.
+.Ar keys
+must be numerical IP addresses.
.Ss DATABASE OUTPUT FORMAT
If invoked without any arguments,
.Nm
@@ -108,25 +107,25 @@ For TRAPPED entries the format is:
where
.Em type
will be TRAPPED,
-.Em ip
+.Em IP
will be the IP address blacklisted due to hitting a spamtrap, and
.Em expire
will be when the IP is due to be removed from the blacklist.
.Pp
For GREY or WHITE entries, the format is:
.Pp
-.Dl type|source ip|from|to|first|pass|expire|block|pass
+.Dl type|source IP|from|to|first|pass|expire|block|pass
.Pp
The fields are as follows:
.Pp
-.Bl -tag -width "source ip" -offset indent -compact
+.Bl -tag -width "source IP" -offset indent -compact
.It type
.Em WHITE
if whitelisted or
.Em GREY
if greylisted
-.It source ip
-IP address the connection originated from
+.It source IP
+IP aaddress the connection originated fro
.It from
envelope-from address for
.Em GREY
diff --git a/usr.sbin/spamdb/spamdb.c b/usr.sbin/spamdb/spamdb.c
index 1a14039dd56..903b7702bbc 100644
--- a/usr.sbin/spamdb/spamdb.c
+++ b/usr.sbin/spamdb/spamdb.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: spamdb.c,v 1.16 2006/04/12 12:53:04 dhill Exp $ */
+/* $OpenBSD: spamdb.c,v 1.17 2006/12/09 17:13:07 beck Exp $ */
/*
* Copyright (c) 2004 Bob Beck. All rights reserved.
@@ -37,30 +37,24 @@
#define TRAPHIT 1
#define SPAMTRAP 2
-int dblist(const char *);
-int dbupdate(const char *, char *, int, int);
+int dblist(DB *);
+int dbupdate(DB *, char *, int, int);
int
-dbupdate(const char *dbname, char *ip, int add, int type)
+dbupdate(DB *db, char *ip, int add, int type)
{
- BTREEINFO btreeinfo;
DBT dbk, dbd;
- DB *db;
struct gdata gd;
time_t now;
int r;
struct addrinfo hints, *res;
now = time(NULL);
- memset(&btreeinfo, 0, sizeof(btreeinfo));
- db = dbopen(dbname, O_EXLOCK|O_RDWR, 0600, DB_BTREE, &btreeinfo);
- if (db == NULL)
- err(1, "cannot open %s for writing", dbname);
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
hints.ai_flags = AI_NUMERICHOST;
- if (type == TRAPHIT || type == WHITE) {
+ if (add && (type == TRAPHIT || type == WHITE)) {
if (getaddrinfo(ip, NULL, &hints, &res) != 0) {
warnx("invalid ip address %s", ip);
goto bad;
@@ -168,29 +162,19 @@ dbupdate(const char *dbname, char *ip, int add, int type)
}
}
}
- db->close(db);
- db = NULL;
return (0);
bad:
- db->close(db);
- db = NULL;
return (1);
}
int
-dblist(const char *dbname)
+dblist(DB *db)
{
- BTREEINFO btreeinfo;
DBT dbk, dbd;
- DB *db;
struct gdata gd;
int r;
/* walk db, list in text format */
- memset(&btreeinfo, 0, sizeof(btreeinfo));
- db = dbopen(dbname, O_EXLOCK|O_RDONLY, 0600, DB_BTREE, &btreeinfo);
- if (db == NULL)
- err(1, "cannot open %s for reading", dbname);
memset(&dbk, 0, sizeof(dbk));
memset(&dbd, 0, sizeof(dbd));
for (r = db->seq(db, &dbk, &dbd, R_FIRST); !r;
@@ -259,7 +243,7 @@ extern char *__progname;
static int
usage(void)
{
- fprintf(stderr, "usage: %s [-Tt] [-a key] [-d key]\n", __progname);
+ fprintf(stderr, "usage: %s [-Tt] [-ad] key [key ...]\n", __progname);
exit(1);
/* NOTREACHED */
}
@@ -267,18 +251,17 @@ usage(void)
int
main(int argc, char **argv)
{
- int ch, action = 0, type = WHITE;
- char *ip = NULL;
+ int i, ch, action = 0, type = WHITE, r = 0;
+ BTREEINFO btreeinfo;
+ DB *db;
- while ((ch = getopt(argc, argv, "a:d:tT")) != -1) {
+ while ((ch = getopt(argc, argv, "adtT")) != -1) {
switch (ch) {
case 'a':
action = 1;
- ip = optarg;
break;
case 'd':
action = 2;
- ip = optarg;
break;
case 't':
type = TRAPHIT;
@@ -291,17 +274,30 @@ main(int argc, char **argv)
break;
}
}
+ argc -= optind;
+ argv += optind;
+
+ memset(&btreeinfo, 0, sizeof(btreeinfo));
+ btreeinfo.cachesize = 8192 * 128;
+ db = dbopen(PATH_SPAMD_DB, O_EXLOCK|O_RDWR, 0600, DB_BTREE,
+ &btreeinfo);
+ if (db == NULL)
+ err(1, "cannot open %s for writing", PATH_SPAMD_DB);
switch (action) {
case 0:
- return dblist(PATH_SPAMD_DB);
+ return dblist(db);
case 1:
- return dbupdate(PATH_SPAMD_DB, ip, 1, type);
+ for (i=0; i<argc; i++)
+ r += dbupdate(db, argv[i], 1, type);
+ break;
case 2:
- return dbupdate(PATH_SPAMD_DB, ip, 0, type);
+ for (i=0; i<argc; i++)
+ r += dbupdate(db, argv[i], 0, type);
+ break;
default:
errx(-1, "bad action");
}
- /* NOTREACHED */
- return (0);
+ db->close(db);
+ return (r);
}