Debian Codegraph @

plugins/rhsbl

raw
1 #!perl -w
2
3 =head1 NAME
4
5 rhsbl - handle RHSBL lookups
6
7 =head1 DESCRIPTION
8
9 Pluging that checks the host part of the sender's address against a
10 configurable set of RBL services.
11
12 =head1 CONFIGURATION
13
14 This plugin reads the lists to use from the rhsbl_zones configuration
15 file. Normal domain based dns blocking lists ("RBLs") which contain TXT
16 records are specified simply as:
17
18 dsn.rfc-ignorant.org
19
20 To configure RBL services which do not contain TXT records in the DNS,
21 but only A records, specify, after a whitespace, your own error message
22 to return in the SMTP conversation e.g.
23
24 abuse.rfc-ignorant.org does not support abuse@domain
25
26 =cut
27
28 use strict;
29 use warnings;
30
31 use Qpsmtpd::Constants;
32
33 sub register {
34 my ($self, $qp) = (shift, shift);
35
36 if (@_ == 1) {
37 $self->legacy_positional_args(@_);
38 }
39 else {
40 $self->{_args} = {@_};
41 }
42
43 $self->{_args}{reject} = 1 if !defined $self->{_args}{reject};
44 $self->{_args}{reject_type} ||= 'perm';
45 }
46
47 sub legacy_positional_args {
48 my ($self, $denial) = @_;
49
50 if (defined $denial && $denial =~ /^disconnect$/i) {
51 $self->{_args}{reject_type} = 'disconnect';
52 }
53 else {
54 $self->{_args}{reject_type} = 'perm';
55 }
56 }
57
58 sub hook_mail {
59 my ($self, $transaction, $sender, %param) = @_;
60
61 return DECLINED if $self->is_immune();
62
63 if ($sender->format eq '<>') {
64 $self->log(LOGINFO, 'pass, null sender');
65 return DECLINED;
66 }
67
68 my %rhsbl_zones = $self->populate_zones() or return DECLINED;
69
70 my $res = $self->init_resolver();
71
72 my @hosts = $sender->host;
73 for my $host (@hosts) {
74 for my $rhsbl (keys %rhsbl_zones) {
75 my $query;
76
77 # fix to find TXT records, if the rhsbl_zones line doesn't have second field
78 if (defined($rhsbl_zones{$rhsbl})) {
79 $self->log(LOGDEBUG, "Checking $host.$rhsbl for A record");
80 $query = $res->query("$host.$rhsbl");
81 }
82 else {
83 $self->log(LOGDEBUG, "Checking $host.$rhsbl for TXT record");
84 $query = $res->query("$host.$rhsbl", 'TXT');
85 }
86
87 if (!$query) {
88 if ($res->errorstring ne 'NXDOMAIN') {
89 $self->log(LOGCRIT, "query failed: ", $res->errorstring);
90 }
91 next;
92 }
93
94 my $result;
95 foreach my $rr ($query->answer) {
96 $self->log(LOGDEBUG,
97 'got an ' . $rr->type . ' record ' . $rr->name);
98 if ($rr->type eq 'A') {
99 $self->log(LOGDEBUG,
100 "A record found for $result with IP " . $rr->address);
101 $result = $rr->name;
102 }
103 elsif ($rr->type eq 'TXT') {
104 $result = $rr->txtdata;
105 $self->log(LOGDEBUG, "TXT record found: " . $rr->txtdata);
106 }
107
108 next if !$result;
109
110 $self->log(LOGINFO, "fail, $result");
111
112 if ($transaction->sender) {
113 my $host = $transaction->sender->host;
114 if ($result =~ /^$host\./) {
115 return $self->get_reject(
116 "Mail from $host rejected because it $result");
117 }
118 }
119
120 my $hello = $self->qp->connection->hello_host;
121 return $self->get_reject(
122 "Mail from HELO $hello rejected because it $result");
123 }
124 }
125 }
126
127 $self->log(LOGINFO, "pass");
128 return DECLINED;
129 }
130
131 sub populate_zones {
132 my $self = shift;
133
134 my %rhsbl_zones =
135 map { (split /\s+/, $_, 2)[0, 1] } $self->qp->config('rhsbl_zones');
136
137 if (!keys %rhsbl_zones) {
138 $self->log(LOGINFO, 'pass, no zones');
139 return;
140 }
141
142 return %rhsbl_zones;
143 }
144