Debian Codegraph @

plugins/rcpt_ok

raw
1 #!perl -w
2
3 =head1 NAME
4
5 rcpt_ok
6
7 =head1 SYNOPSIS
8
9 Validate that we accept mail for a recipient using a qmail rcpthosts file
10
11 =head1 DESCRIPTION
12
13 Check the envelope recipient hostname and determine if we accept mail to that host.
14
15 This is functionally identical to qmail's rcpthosts implementation, consulting
16 both rcpthosts and morercpthosts.cdb.
17
18 =head1 CONFIGURATION
19
20 It should be configured as the _LAST_ recipient plugin!
21
22 =cut
23
24 use strict;
25 use warnings;
26
27 use Qpsmtpd::Constants;
28 use Qpsmtpd::DSN;
29
30 sub hook_rcpt {
31 my ($self, $transaction, $recipient, %param) = @_;
32
33 return (OK) if $self->is_immune(); # relay_client or whitelist
34
35 # Allow 'no @' addresses for 'postmaster' and 'abuse'
36 # qmail-smtpd will do this for all users without a domain, but we'll
37 # be a bit more picky. Maybe that's a bad idea.
38 my $host = $self->get_rcpt_host($recipient) or return (OK);
39
40 return (OK) if $self->is_in_rcpthosts($host);
41 return (OK) if $self->is_in_morercpthosts($host);
42
43 # default of relaying_denied is obviously DENY,
44 # we use the default "Relaying denied" message...
45 return Qpsmtpd::DSN->relaying_denied();
46 }
47
48 sub is_in_rcpthosts {
49 my ($self, $host) = @_;
50
51 my @rcpt_hosts = ($self->qp->config('me'), $self->qp->config('rcpthosts'));
52
53 # Check if this recipient host is allowed
54 for my $allowed (@rcpt_hosts) {
55 $allowed =~ s/^\s*(\S+)/$1/;
56 if ($host eq lc $allowed) {
57 $self->log(LOGINFO, "pass: $host in rcpthosts");
58 return 1;
59 }
60
61 if (substr($allowed, 0, 1) eq '.' and $host =~ m/\Q$allowed\E$/i) {
62 $self->log(LOGINFO, "pass: $host in rcpthosts as $allowed");
63 return 1;
64 }
65 }
66
67 return;
68 }
69
70 sub is_in_morercpthosts {
71 my ($self, $host) = @_;
72
73 my $more_rcpt_hosts = $self->qp->config('morercpthosts', 'map');
74
75 if (exists $more_rcpt_hosts->{$host}) {
76 $self->log(LOGINFO, "pass: $host found in morercpthosts");
77 return 1;
78 }
79
80 $self->log(LOGINFO, "fail: $host not in morercpthosts");
81 return;
82 }
83
84 sub get_rcpt_host {
85 my ($self, $recipient) = @_;
86
87 return if !$recipient; # Qpsmtpd::Address couldn't parse the recipient
88
89 if ($recipient->host) {
90 return lc $recipient->host;
91 }
92
93 # no host portion exists
94 my $user = $recipient->user or return;
95 if (lc $user eq 'postmaster' || lc $user eq 'abuse') {
96 return $self->qp->config('me');
97 }
98 return;
99 }
100