#!/usr/bin/perl -w
use DBI;
use strict;
# Version: 0.01
#
# Programmer: Tim Freeman <tim at fungible.com>
my $conffile = "/etc/mail/relaydelay.conf";
my $verbose;
my $run_as_daemon;
my $log_file;
my $milter_filter_name;
my $milter_socket_connection;
my $maximum_milter_threads;
my $relaydelay_pid_file;
my $delay_mail_secs;
my $auto_record_life_secs;
my $update_record_life;
my $update_record_life_secs;
my $check_wildcard_relay_ip;
my $check_wildcard_rcpt_to;
my $tempfail_messages_after_data_phase;
my $do_relay_lookup_by_subnet;
my $enable_relay_name_updates;
my $check_envelope_address_format;
my $pass_mail_when_db_unavail;
my $reverse_mail_tracking;
my $reverse_mail_life_secs;
my $database_type;
my $database_name;
my $database_host;
my $database_port;
my $database_user;
my $database_pass;
sub usage () {
die << "EOM";
Usage: $0 [[-whitelist|-blacklist] yyyy-mm-dd hh:mm:ss|-delete] < list
Add the IP addresses listed in one of
the .txt files that comes with
relaydelay to the database.
Reads $conffile for database passwords.
With -whitelist, the given IP\'s are
whitelisted until the given date.
With -blacklist, the given IP\'s are blacklisted until the given date.
With -delete, the given IP\'s are
deleted from the database. Do this
first if you want to change the dates on the whitelists or the
blacklists, or you accidentally blacklisted your whitelist.
For example:
$0 -whitelist 9999-12-31 23:59:59 <
whitelist_ip.txt
EOM
}
# Unfortunately, this strategy for
reading the configuration file
# requires that each perl statement in the config file is on one line.
sub readconf () {
open (INFILE, "<$conffile") || die "Can't read $conffile:
$!";
for (;;) {
my $line = <INFILE>;
last unless defined $line;
eval $line;
if ($@ ne '') {
die "Error evaluating this line from $conffile:\n$line\n$@" ;
}
}
close (INFILE) || die "Can't close $conffile: $!";
}
sub docmd ($$) {
my ($dbh, $cmd) = @_;
print "Doing $cmd\n" if $verbose;
$dbh->do($cmd);
}
sub main () {
my $foundarg = 0;
if (0 == @ARGV || $ARGV[0] =~ m!help!i) {
usage ();
}
readconf ();
my $dsn = "DBI:$database_type:database=$database_name:".
"host=$database_host:port=$database_port";
my $dbh = DBI->connect($dsn, $database_user, $database_pass,
{ PrintError => 0, RaiseError => 1 });
die "$DBI::errstr\n" unless($dbh);
for (;;) {
my $line = <STDIN>;
last unless defined $line;
chomp $line;
if ($line !~ m!^\s*(\d+(\.\d+)*)?\s*(\#.*)?$!) {
die "Syntax error at $line";
}
my $ip = $1;
if (! $ip) {
print "Skipping blank line $line\n" if $verbose;
next;
}
print "Found ip $ip.\n" if $verbose;
my $didsomething = 0;
if ($ARGV[0] eq "-delete") {
$didsomething = 1;
docmd ($dbh, "delete from relaytofrom where relay_ip = '$ip'");
} else {
usage () unless 3 == @ARGV;
my $date = "$ARGV[1] $ARGV[2]";
if ($ARGV[0] eq "-blacklist") {
$didsomething = 1;
docmd ($dbh,
"insert into relaytofrom ".
"(block_expires, record_expires, create_time, relay_ip) ".
"values ('$date', '$date', now(), '$ip')");
} elsif ($ARGV[0] eq "-whitelist") {
$didsomething = 1;
docmd ($dbh,
"insert into relaytofrom ".
"(relay_ip, record_expires, create_time) ".
"values ('$ip', '$date', now())");
}
}
if (!$didsomething) {
usage ();
}
}
$dbh->disconnect();
}
main ();