2012-04-29 10:35:59 +02:00
|
|
|
#!perl -w
|
2012-04-08 02:11:16 +02:00
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
rcpt_ok
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
2013-04-24 22:31:00 +02:00
|
|
|
Validate that we accept mail for a recipient using a qmail rcpthosts file
|
2012-04-08 02:11:16 +02:00
|
|
|
|
2012-05-11 07:50:04 +02:00
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
2013-04-24 22:31:00 +02:00
|
|
|
Check the envelope recipient hostname and determine if we accept mail to that host.
|
2012-05-11 07:50:04 +02:00
|
|
|
|
|
|
|
This is functionally identical to qmail's rcpthosts implementation, consulting
|
|
|
|
both rcpthosts and morercpthosts.cdb.
|
|
|
|
|
|
|
|
=head1 CONFIGURATION
|
|
|
|
|
2013-04-24 22:31:00 +02:00
|
|
|
It should be configured as the _LAST_ recipient plugin!
|
2012-04-08 02:11:16 +02:00
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2012-05-11 07:50:04 +02:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Qpsmtpd::Constants;
|
2006-01-25 03:59:31 +01:00
|
|
|
use Qpsmtpd::DSN;
|
2004-09-22 18:01:16 +02:00
|
|
|
|
2005-07-07 06:17:39 +02:00
|
|
|
sub hook_rcpt {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $transaction, $recipient, %param) = @_;
|
2004-09-22 18:01:16 +02:00
|
|
|
|
2014-09-18 03:28:51 +02:00
|
|
|
return OK if $self->is_immune(); # relay_client or whitelist
|
2013-04-24 22:31:00 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
# Allow 'no @' addresses for 'postmaster' and 'abuse'
|
|
|
|
# qmail-smtpd will do this for all users without a domain, but we'll
|
|
|
|
# be a bit more picky. Maybe that's a bad idea.
|
2014-09-18 03:28:51 +02:00
|
|
|
my $host = $self->get_rcpt_host($recipient) or return OK;
|
2012-05-11 07:50:04 +02:00
|
|
|
|
2014-09-18 03:28:51 +02:00
|
|
|
return OK if $self->is_in_rcpthosts($host);
|
|
|
|
return OK if $self->is_in_morercpthosts($host);
|
2012-05-11 07:50:04 +02:00
|
|
|
|
|
|
|
# default of relaying_denied is obviously DENY,
|
2006-01-25 03:59:31 +01:00
|
|
|
# we use the default "Relaying denied" message...
|
|
|
|
return Qpsmtpd::DSN->relaying_denied();
|
2004-09-22 18:01:16 +02:00
|
|
|
}
|
2012-05-11 07:50:04 +02:00
|
|
|
|
|
|
|
sub is_in_rcpthosts {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $host) = @_;
|
2012-05-11 07:50:04 +02:00
|
|
|
|
|
|
|
my @rcpt_hosts = ($self->qp->config('me'), $self->qp->config('rcpthosts'));
|
|
|
|
|
|
|
|
# Check if this recipient host is allowed
|
|
|
|
for my $allowed (@rcpt_hosts) {
|
|
|
|
$allowed =~ s/^\s*(\S+)/$1/;
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($host eq lc $allowed) {
|
|
|
|
$self->log(LOGINFO, "pass: $host in rcpthosts");
|
2012-05-11 07:50:04 +02:00
|
|
|
return 1;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-11 07:50:04 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if (substr($allowed, 0, 1) eq '.' and $host =~ m/\Q$allowed\E$/i) {
|
|
|
|
$self->log(LOGINFO, "pass: $host in rcpthosts as $allowed");
|
2012-05-11 07:50:04 +02:00
|
|
|
return 1;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-11 07:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-11 07:50:04 +02:00
|
|
|
|
|
|
|
sub is_in_morercpthosts {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $host) = @_;
|
2012-05-11 07:50:04 +02:00
|
|
|
|
|
|
|
my $more_rcpt_hosts = $self->qp->config('morercpthosts', 'map');
|
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if (exists $more_rcpt_hosts->{$host}) {
|
|
|
|
$self->log(LOGINFO, "pass: $host found in morercpthosts");
|
2012-05-11 07:50:04 +02:00
|
|
|
return 1;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-11 07:50:04 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
$self->log(LOGINFO, "fail: $host not in morercpthosts");
|
2012-05-11 07:50:04 +02:00
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-11 07:50:04 +02:00
|
|
|
|
|
|
|
sub get_rcpt_host {
|
2013-04-21 06:50:39 +02:00
|
|
|
my ($self, $recipient) = @_;
|
2012-05-11 07:50:04 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
return if !$recipient; # Qpsmtpd::Address couldn't parse the recipient
|
2012-05-11 07:50:04 +02:00
|
|
|
|
2013-04-21 06:50:39 +02:00
|
|
|
if ($recipient->host) {
|
2012-05-11 07:50:04 +02:00
|
|
|
return lc $recipient->host;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-11 07:50:04 +02:00
|
|
|
|
|
|
|
# no host portion exists
|
|
|
|
my $user = $recipient->user or return;
|
2013-04-21 06:50:39 +02:00
|
|
|
if (lc $user eq 'postmaster' || lc $user eq 'abuse') {
|
2012-05-11 07:50:04 +02:00
|
|
|
return $self->qp->config('me');
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-11 07:50:04 +02:00
|
|
|
return;
|
2013-04-21 06:50:39 +02:00
|
|
|
}
|
2012-05-11 07:50:04 +02:00
|
|
|
|