| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
dns_whitelist_soft - dns-based whitelist override for other qpsmtpd plugins |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
The dns_whitelist_soft plugin allows selected host to be whitelisted as |
| 10 |
exceptions to later plugin processing. It is strongly based on the original |
| 11 |
dnsbl plugin as well as Gavin Carr's original whitelist_soft plugin. It is |
| 12 |
most suitable for multisite installations, so that the whitelist is stored |
| 13 |
in one location and available from all. |
| 14 |
|
| 15 |
=head1 CONFIGURATION |
| 16 |
|
| 17 |
To enable the plugin, add it to the ~qpsmtpd/config/plugins file as usual. |
| 18 |
It should precede any plugins whose rejections you wish to override. You may |
| 19 |
have to alter those plugins to check the appropriate notes field. |
| 20 |
|
| 21 |
Several configuration files are supported, corresponding to different |
| 22 |
parts of the SMTP conversation: |
| 23 |
|
| 24 |
=over 4 |
| 25 |
|
| 26 |
=item whitelist_zones |
| 27 |
|
| 28 |
Any IP address listed in the whitelist_zones file is queried using |
| 29 |
the connecting MTA's IP address. Any A or TXT answer means that the |
| 30 |
remote HOST address can be selectively exempted at other stages by plugins |
| 31 |
testing for a 'whitelisthost' connection note. |
| 32 |
|
| 33 |
=back |
| 34 |
|
| 35 |
NOTE: other 'connect' hooks will continue to fire (e.g. dnsbl), since the DNS |
| 36 |
queries happen in the background. This plugin's 'rcpt_handler' retrieves |
| 37 |
the results of the query and sets the connection note if found. |
| 38 |
|
| 39 |
If you switch to qpsmtpd-async and to the async version of this plugin, then |
| 40 |
the 'whitelisthost' connection note will be available to the other 'connect' |
| 41 |
hooks, see the documentation of the async plugin. |
| 42 |
|
| 43 |
=head1 AUTHOR |
| 44 |
|
| 45 |
John Peacock <[email protected]> |
| 46 |
|
| 47 |
Based on the 'whitelist_soft' plugin by Gavin Carr <[email protected]>, |
| 48 |
based on the 'whitelist' plugin by Devin Carraway <[email protected]>. |
| 49 |
|
| 50 |
=cut |
| 51 |
|
| 52 |
use strict; |
| 53 |
use warnings; |
| 54 |
|
| 55 |
use Qpsmtpd::Constants; |
| 56 |
|
| 57 |
sub register { |
| 58 |
my ($self, $qp) = (shift, shift); |
| 59 |
$self->log(LOGERROR, "Bad arguments") if @_ % 2; |
| 60 |
$self->{_args} = {@_}; |
| 61 |
} |
| 62 |
|
| 63 |
sub hook_connect { |
| 64 |
my ($self, $transaction) = @_; |
| 65 |
|
| 66 |
my $remote_ip = $self->qp->connection->remote_ip; |
| 67 |
|
| 68 |
my %whitelist_zones = |
| 69 |
map { (split /\s+/, $_, 2)[0, 1] } $self->qp->config('whitelist_zones'); |
| 70 |
|
| 71 |
return DECLINED unless %whitelist_zones; |
| 72 |
|
| 73 |
my $reversed_ip = join('.', reverse(split(/\./, $remote_ip))); |
| 74 |
|
| 75 |
# we queue these lookups in the background and just fetch the |
| 76 |
# results in the first rcpt handler |
| 77 |
|
| 78 |
my $res = new Net::DNS::Resolver; |
| 79 |
my $sel = IO::Select->new(); |
| 80 |
|
| 81 |
for my $dnsbl (keys %whitelist_zones) { |
| 82 |
$self->log(LOGDEBUG, "Checking $reversed_ip.$dnsbl in the background"); |
| 83 |
$sel->add($res->bgsend("$reversed_ip.$dnsbl", 'TXT')); |
| 84 |
} |
| 85 |
|
| 86 |
$self->connection->notes('whitelist_sockets', $sel); |
| 87 |
return DECLINED; |
| 88 |
} |
| 89 |
|
| 90 |
sub process_sockets { |
| 91 |
my ($self) = @_; |
| 92 |
|
| 93 |
my $conn = $self->connection; |
| 94 |
|
| 95 |
return $conn->notes('whitelisthost') if $conn->notes('whitelisthost'); |
| 96 |
|
| 97 |
my $res = new Net::DNS::Resolver; |
| 98 |
my $sel = $conn->notes('whitelist_sockets') or return ''; |
| 99 |
|
| 100 |
$self->log(LOGDEBUG, "waiting for whitelist dns"); |
| 101 |
|
| 102 |
# don't wait more than 4 seconds here |
| 103 |
my @ready = $sel->can_read(4); |
| 104 |
|
| 105 |
$self->log(LOGDEBUG, |
| 106 |
"done waiting for whitelist dns, got ", |
| 107 |
scalar @ready, |
| 108 |
" answers ..."); |
| 109 |
return '' unless @ready; |
| 110 |
|
| 111 |
my $result; |
| 112 |
|
| 113 |
for my $socket (@ready) { |
| 114 |
my $query = $res->bgread($socket); |
| 115 |
$sel->remove($socket); |
| 116 |
undef $socket; |
| 117 |
|
| 118 |
my $whitelist; |
| 119 |
|
| 120 |
if ($query) { |
| 121 |
my $a_record = 0; |
| 122 |
foreach my $rr ($query->answer) { |
| 123 |
$a_record = 1 if $rr->type eq 'A'; |
| 124 |
my $name = $rr->name; |
| 125 |
($whitelist) = ($name =~ m/(?:\d+\.){4}(.*)/) unless $whitelist; |
| 126 |
$whitelist = $name unless $whitelist; |
| 127 |
$self->log(LOGDEBUG, 'name ', $rr->name); |
| 128 |
next unless $rr->type eq 'TXT'; |
| 129 |
$self->log(LOGDEBUG, "got txt record"); |
| 130 |
$result = $rr->txtdata and last; |
| 131 |
} |
| 132 |
$a_record and $result = "Blocked by $whitelist"; |
| 133 |
} |
| 134 |
else { |
| 135 |
$self->log(LOGERROR, "$whitelist query failed: ", $res->errorstring) |
| 136 |
if $res->errorstring ne "NXDOMAIN"; |
| 137 |
} |
| 138 |
|
| 139 |
if ($result) { |
| 140 |
|
| 141 |
# kill any other pending I/O |
| 142 |
$conn->notes('whitelist_sockets', undef); |
| 143 |
return $conn->notes('whitelisthost', $result); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if ($sel->count) { |
| 148 |
|
| 149 |
# loop around if we have dns blacklists left to see results from |
| 150 |
return $self->process_sockets(); |
| 151 |
} |
| 152 |
|
| 153 |
# er, the following code doesn't make much sense anymore... |
| 154 |
|
| 155 |
# if there was more to read; then forget it |
| 156 |
$conn->notes('whitelist_sockets', undef); |
| 157 |
|
| 158 |
return $conn->notes('whitelisthost', $result); |
| 159 |
} |
| 160 |
|
| 161 |
sub hook_rcpt { |
| 162 |
my ($self, $transaction, $rcpt, %param) = @_; |
| 163 |
my $ip = $self->qp->connection->remote_ip or return (DECLINED); |
| 164 |
my $note = $self->process_sockets; |
| 165 |
if ($note) { |
| 166 |
$self->log(LOGNOTICE, "Host $ip is whitelisted: $note"); |
| 167 |
} |
| 168 |
return DECLINED; |
| 169 |
} |
| 170 |
|