=head1 NAME whitelist_soft - whitelist override for other qpsmtpd plugins =head1 DESCRIPTION The whitelist_soft plugin allows selected hosts or senders or recipients to be whitelisted as exceptions to later plugin processing. It is a more conservative variant of Devin Carraway's 'whitelist' plugin. =head1 CONFIGURATION To enable the plugin, add it to the ~qpsmtpd/config/plugins file as usual. It should precede any plugins whose rejections you wish to override. Several configuration files are supported, corresponding to different parts of the SMTP conversation: =over 4 =item whitelisthosts Any IP address (or start-anchored fragment thereof) listed in the whitelisthosts file is exempted from any further validation during 'connect', and can be selectively exempted at other stages by plugins testing for a 'whitelisthost' connection note. Similarly, if the environment variable $WHITELISTCLIENT is set (which can be done by tcpserver), the connection will be exempt from further 'connect' validation, and the host can be selectively exempted by other plugins testing for a 'whitelistclient' connection note. =item whitelisthelo Any host that issues a HELO matching an entry in whitelisthelo will be exempted from further validation at the 'helo' stage. Subsequent plugins can test for a 'whitelisthelo' connection note. Note that this does not actually amount to an authentication in any meaningful sense. =item whitelistsenders If the envelope sender of a mail (that which is sent as the MAIL FROM) matches an entry in whitelistsenders, or if the hostname component matches, the mail will be exempted from any further validation within the 'mail' stage. Subsequent plugins can test for this exemption as a 'whitelistsender' transaction note. =item whitelistrcpt If any recipient of a mail (that sent as the RCPT TO) matches an entry from whitelistrcpt, or if the hostname component matches, no further validation will be required for this recipient. Subsequent plugins can test for this exemption using a 'whitelistrcpt' transaction note, which holds the count of whitelisted recipients. =head1 BUGS Whitelist lookups are all O(n) linear scans of configuration files, even though they're all associative lookups. Something should be done about this when CDB/DB/GDBM configs are supported. =head1 AUTHOR Based on the 'whitelist' plugin by Devin Carraway . Modified by Gavin Carr to not inherit whitelisting across hooks, but use per-hook whitelist notes instead. This is a more conservative approach e.g. whitelisting an IP will not automatically allow relaying from that IP. =cut sub register { my ($self, $qp) = @_; $self->register_hook("connect", "connect_handler"); $self->register_hook("helo", "helo_handler"); $self->register_hook("ehlo", "helo_handler"); $self->register_hook("mail", "mail_handler"); $self->register_hook("rcpt", "rcpt_handler"); } sub connect_handler { my ($self, $transaction) = @_; my $ip = $self->qp->connection->remote_ip || return (DECLINED); # From tcpserver if (exists $ENV{WHITELISTCLIENT}) { $self->qp->connection->notes('whitelistclient', 1); $self->log(2,"Host $ip is a whitelisted client"); return OK; } for my $h ($self->qp->config('whitelisthosts')) { if ($h eq $ip or $ip =~ /^\Q$h\E/) { $self->qp->connection->notes('whitelisthost', 1); $self->log(2,"Host $ip is a whitelisted host"); return OK; } } return DECLINED; } sub helo_handler { my ($self, $transaction, $helo) = @_; for my $h ($self->qp->config('whitelisthelo')) { if ($helo and lc $h eq lc $helo) { $self->qp->connection->notes('whitelisthelo', 1); $self->log(2,"HELO host $helo in whitelisthelo"); return OK; } } return DECLINED; } sub mail_handler { my ($self, $transaction, $sender) = @_; return DECLINED if $sender->format eq '<>'; my $addr = lc $sender->address or return DECLINED; my $host = lc $sender->host or return DECLINED; for my $h ($self->qp->config('whitelistsenders')) { next unless $h; $h = lc $h; if ($addr eq $h or $host eq $h) { $transaction->notes('whitelistsender', 1); $self->log(2,"Envelope sender $addr in whitelistsenders"); return OK; } } return DECLINED; } sub rcpt_handler { my ($self, $transaction, $rcpt) = @_; my $addr = lc $rcpt->address or return DECLINED; my $host = lc $rcpt->host or return DECLINED; for my $h ($self->qp->config('whitelistrcpt')) { next unless $h; $h = lc $h; if ($addr eq $h or $host eq $h) { my $note = $transaction->notes('whitelistrcpt'); $transaction->notes('whitelistrcpt', ++$note); $self->log(2,"Recipient $addr in whitelistrcpt"); return OK; } } return DECLINED; } # tag: modified whitelisting plugin with more conservative per-hook behaviour