| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
whitelist - whitelist override for other qpsmtpd plugins |
| 6 |
|
| 7 |
|
| 8 |
=head1 DESCRIPTION |
| 9 |
|
| 10 |
The B<whitelist> plugin allows selected hosts or senders or recipients |
| 11 |
to be whitelisted as exceptions to later plugin processing. It is a more |
| 12 |
conservative variant of Devin Carraway's 'whitelist' plugin. |
| 13 |
|
| 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 you might wish to whitelist for. |
| 19 |
|
| 20 |
Several configuration files are supported, corresponding to different |
| 21 |
parts of the SMTP conversation: |
| 22 |
|
| 23 |
=over 4 |
| 24 |
|
| 25 |
=item whitelisthosts |
| 26 |
|
| 27 |
Any IP address (or start-anchored fragment thereof) listed in the |
| 28 |
whitelisthosts file is exempted from any further validation during |
| 29 |
'connect', and can be selectively exempted at other stages by |
| 30 |
plugins testing for a 'whitelisthost' connection note. |
| 31 |
|
| 32 |
Similarly, if the environment variable $WHITELISTCLIENT is set |
| 33 |
(which can be done by tcpserver), the connection will be exempt from |
| 34 |
further 'connect' validation, and the host can be selectively |
| 35 |
exempted by other plugins testing for a 'whitelistclient' connection |
| 36 |
note. |
| 37 |
|
| 38 |
=item whitelisthelo |
| 39 |
|
| 40 |
Any host that issues a HELO matching an entry in whitelisthelo will |
| 41 |
be exempted from further validation at the 'helo' stage. Subsequent |
| 42 |
plugins can test for a 'whitelisthelo' connection note. Note that |
| 43 |
this does not actually amount to an authentication in any meaningful |
| 44 |
sense. |
| 45 |
|
| 46 |
=item whitelistsenders |
| 47 |
|
| 48 |
If the envelope sender of a mail (that which is sent as the MAIL FROM) |
| 49 |
matches an entry in whitelistsenders, or if the hostname component |
| 50 |
matches, the mail will be exempted from any further validation within |
| 51 |
the 'mail' stage. Subsequent plugins can test for this exemption as a |
| 52 |
'whitelistsender' transaction note. |
| 53 |
|
| 54 |
=item whitelistrcpt |
| 55 |
|
| 56 |
If any recipient of a mail (that sent as the RCPT TO) matches an |
| 57 |
entry from whitelistrcpt, or if the hostname component matches, no |
| 58 |
further validation will be required for this recipient. Subsequent |
| 59 |
plugins can test for this exemption using a 'whitelistrcpt' |
| 60 |
transaction note, which holds the count of whitelisted recipients. |
| 61 |
|
| 62 |
=back |
| 63 |
|
| 64 |
whitelist_soft also supports per-recipient whitelisting when using |
| 65 |
the per_user_config plugin. To enable the per-recipient behaviour |
| 66 |
(delaying all whitelisting until the rcpt part of the smtp |
| 67 |
conversation, and using per-recipient whitelist configs, if |
| 68 |
available), pass a true 'per_recipient' argument in the |
| 69 |
config/plugins invocation i.e. |
| 70 |
|
| 71 |
whitelist_soft per_recipient 1 |
| 72 |
|
| 73 |
By default global and per-recipient whitelists are merged; to turn off |
| 74 |
the merge behaviour pass a false 'merge' argument in the config/plugins |
| 75 |
invocation i.e. |
| 76 |
|
| 77 |
whitelist_soft per_recipient 1 merge 0 |
| 78 |
|
| 79 |
|
| 80 |
=head1 BUGS |
| 81 |
|
| 82 |
Whitelist lookups are all O(n) linear scans of configuration files, even |
| 83 |
though they're all associative lookups. Something should be done about |
| 84 |
this when CDB/DB/GDBM configs are supported. |
| 85 |
|
| 86 |
|
| 87 |
=head1 AUTHOR |
| 88 |
|
| 89 |
Based on the 'whitelist' plugin by Devin Carraway <[email protected]>. |
| 90 |
|
| 91 |
Modified by Gavin Carr <[email protected]> to not inherit |
| 92 |
whitelisting across hooks, but use per-hook whitelist notes instead. |
| 93 |
This is a more conservative approach e.g. whitelisting an IP will not |
| 94 |
automatically allow relaying from that IP. |
| 95 |
|
| 96 |
=cut |
| 97 |
|
| 98 |
use strict; |
| 99 |
use warnings; |
| 100 |
|
| 101 |
use Qpsmtpd::Constants; |
| 102 |
|
| 103 |
my $VERSION = 0.02; |
| 104 |
|
| 105 |
# Default is to merge whitelists in per_recipient mode |
| 106 |
my %MERGE = (merge => 1); |
| 107 |
|
| 108 |
sub register { |
| 109 |
my ($self, $qp, %arg) = @_; |
| 110 |
|
| 111 |
$self->{_per_recipient} = 1 if $arg{per_recipient}; |
| 112 |
$MERGE{merge} = $arg{merge} if defined $arg{merge}; |
| 113 |
|
| 114 |
# Normal mode - whitelist per hook |
| 115 |
unless ($arg{per_recipient}) { |
| 116 |
$self->register_hook("connect", "check_host"); |
| 117 |
$self->register_hook("helo", "check_helo"); |
| 118 |
$self->register_hook("ehlo", "check_helo"); |
| 119 |
$self->register_hook("mail", "check_sender"); |
| 120 |
$self->register_hook("rcpt", "check_rcpt"); |
| 121 |
} |
| 122 |
|
| 123 |
# Per recipient mode - defer all whitelisting to rcpt hook |
| 124 |
else { |
| 125 |
$self->register_hook("rcpt", "check_host"); |
| 126 |
$self->register_hook("helo", "helo_helper"); |
| 127 |
$self->register_hook("ehlo", "helo_helper"); |
| 128 |
$self->register_hook("rcpt", "check_helo"); |
| 129 |
$self->register_hook("rcpt", "check_sender"); |
| 130 |
$self->register_hook("rcpt", "check_rcpt"); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
sub check_host { |
| 135 |
my ($self, $transaction, $rcpt) = @_; |
| 136 |
my $ip = $self->qp->connection->remote_ip || return (DECLINED); |
| 137 |
|
| 138 |
# From tcpserver |
| 139 |
if (exists $ENV{WHITELISTCLIENT}) { |
| 140 |
$self->qp->connection->notes('whitelistclient', 1); |
| 141 |
$self->log(2, "pass, is whitelisted client"); |
| 142 |
$self->adjust_karma(5); |
| 143 |
return OK; |
| 144 |
} |
| 145 |
|
| 146 |
my $config_arg = $self->{_per_recipient} ? {rcpt => $rcpt, %MERGE} : {}; |
| 147 |
for my $h ($self->qp->config('whitelisthosts', $config_arg)) { |
| 148 |
if ($h eq $ip or $ip =~ /^\Q$h\E/) { |
| 149 |
$self->qp->connection->notes('whitelisthost', 1); |
| 150 |
$self->log(2, "pass, is a whitelisted host"); |
| 151 |
$self->adjust_karma(5); |
| 152 |
return OK; |
| 153 |
} |
| 154 |
} |
| 155 |
$self->log(LOGDEBUG, "skip: $ip is not whitelisted"); |
| 156 |
return DECLINED; |
| 157 |
} |
| 158 |
|
| 159 |
sub helo_helper { |
| 160 |
my ($self, $transaction, $helo) = @_; |
| 161 |
$self->{_whitelist_soft_helo} = $helo; |
| 162 |
return DECLINED; |
| 163 |
} |
| 164 |
|
| 165 |
sub check_helo { |
| 166 |
my ($self, $transaction, $helo) = @_; |
| 167 |
|
| 168 |
# If per_recipient will be rcpt hook, and helo actually rcpt |
| 169 |
my $config_arg = {}; |
| 170 |
if ($self->{_per_recipient}) { |
| 171 |
$config_arg = {rcpt => $helo, %MERGE}; |
| 172 |
$helo = $self->{_whitelist_soft_helo}; |
| 173 |
} |
| 174 |
|
| 175 |
for my $h ($self->qp->config('whitelisthelo', $config_arg)) { |
| 176 |
if ($helo and lc $h eq lc $helo) { |
| 177 |
$self->qp->connection->notes('whitelisthelo', 1); |
| 178 |
$self->log(2, "helo host $helo in whitelisthelo"); |
| 179 |
return OK; |
| 180 |
} |
| 181 |
} |
| 182 |
return DECLINED; |
| 183 |
} |
| 184 |
|
| 185 |
sub check_sender { |
| 186 |
my ($self, $transaction, $sender) = @_; |
| 187 |
|
| 188 |
# If per_recipient will be rcpt hook, and sender actually rcpt |
| 189 |
my $config_arg = {}; |
| 190 |
if ($self->{_per_recipient}) { |
| 191 |
$config_arg = {rcpt => $sender, %MERGE}; |
| 192 |
$sender = $transaction->sender; |
| 193 |
} |
| 194 |
|
| 195 |
return DECLINED if $sender->format eq '<>'; |
| 196 |
my $addr = lc $sender->address or return DECLINED; |
| 197 |
my $host = lc $sender->host or return DECLINED; |
| 198 |
|
| 199 |
for my $h ($self->qp->config('whitelistsenders', $config_arg)) { |
| 200 |
next unless $h; |
| 201 |
$h = lc $h; |
| 202 |
|
| 203 |
if ($addr eq $h or $host eq $h) { |
| 204 |
$transaction->notes('whitelistsender', 1); |
| 205 |
$self->log(2, "envelope sender $addr in whitelistsenders"); |
| 206 |
return OK; |
| 207 |
} |
| 208 |
} |
| 209 |
return DECLINED; |
| 210 |
} |
| 211 |
|
| 212 |
sub check_rcpt { |
| 213 |
my ($self, $transaction, $rcpt) = @_; |
| 214 |
|
| 215 |
my $addr = lc $rcpt->address or return DECLINED; |
| 216 |
my $host = lc $rcpt->host or return DECLINED; |
| 217 |
|
| 218 |
my $config_arg = $self->{_per_recipient} ? {rcpt => $rcpt, %MERGE} : {}; |
| 219 |
for my $h ($self->qp->config('whitelistrcpt', $config_arg)) { |
| 220 |
next unless $h; |
| 221 |
$h = lc $h; |
| 222 |
|
| 223 |
if ($addr eq $h or $host eq $h) { |
| 224 |
my $note = $transaction->notes('whitelistrcpt'); |
| 225 |
$transaction->notes('whitelistrcpt', ++$note); |
| 226 |
$self->log(2, "recipient $addr in whitelistrcpt"); |
| 227 |
return OK; |
| 228 |
} |
| 229 |
} |
| 230 |
return DECLINED; |
| 231 |
} |
| 232 |
|