Debian Codegraph @

plugins/dnsbl

raw
1 #!perl -w
2
3 =head1 NAME
4
5 dnsbl - handle DNS BlackList lookups
6
7 =head1 DESCRIPTION
8
9 Plugin that checks the IP address of the incoming connection against
10 a configurable set of RBL services.
11
12 =head1 USAGE
13
14 Add the following line to the config/plugins file:
15
16 dnsbl
17
18 The following options are also availble:
19
20 =head2 reject [ 0 | 1 | naughty ]
21
22 dnsbl reject 0 <- do not reject
23
24 dnsbl reject 1 <- reject
25
26 dnsbl reject naughty <- See perldoc plugins/naughty
27
28 Also, when I<reject naughty> is set, DNS queries are processed during connect.
29
30 =head2 reject_type [ temp | perm | disconnect ]
31
32 Default: perm
33
34 To immediately drop the connection (since some blacklisted servers attempt
35 multiple sends per session), set I<reject_type disconnect>. In most cases,
36 an IP address that is listed should not be given the opportunity to begin a
37 new transaction, since even the most volatile blacklists will return the same
38 answer for a short period of time (the minimum DNS cache period).
39
40 =head2 loglevel
41
42 dnsbl [loglevel -1]
43
44 Adjust the quantity of logging for this plugin. See docs/logging.pod
45
46 =head1 CONFIG FILES
47
48 This plugin uses the following configuration files. All are optional. Not
49 specifying dnsbl_zones is like not using the plugin at all.
50
51 =head2 dnsbl_zones
52
53 Normal ip based dns blocking lists ("RBLs") which contain TXT records are
54 specified simply as:
55
56 relays.ordb.org
57 spamsources.fabel.dk
58
59 To configure RBL services which do not contain TXT records in the DNS,
60 but only A records (e.g. the RBL+ at http://www.mail-abuse.org), specify your
61 own error message to return in the SMTP conversation after a colon e.g.
62
63 rbl-plus.mail-abuse.org:You are listed at - http://http://www.mail-abuse.org/cgi-bin/lookup?%IP%
64
65 The string %IP% will be replaced with the IP address of incoming connection.
66 Thus a fully specified file could be:
67
68 sbl-xbl.spamhaus.org
69 list.dsbl.org
70 rbl-plus.mail-abuse.ja.net:Listed by rbl-plus.mail-abuse.ja.net - see <URL:http://www.mail-abuse.org/cgi-bin/lookup?%IP%>
71 relays.ordb.org
72
73 =head2 dnsbl_allow
74
75 List of allowed ip addresses that bypass RBL checking. Format is one entry per line,
76 with either a full IP address or a truncated IP address with a period at the end.
77 For example:
78
79 192.168.1.1
80 172.16.33.
81
82 NB the environment variable RBLSMTPD is considered before this file is
83 referenced. See below.
84
85 =head2 dnsbl_rejectmsg
86
87 A textual message that is sent to the sender on an RBL failure. The TXT record
88 from the RBL list is also sent, but this file can be used to indicate what
89 action the sender should take.
90
91 For example:
92
93 If you think you have been blocked in error, then please forward
94 this entire error message to your ISP so that they can fix their problems.
95 The next line often contains a URL that can be visited for more information.
96
97 =head1 Environment Variables
98
99 =head2 RBLSMTPD
100
101 The environment variable RBLSMTPD is supported and mimics the behaviour of
102 Dan Bernstein's rblsmtpd. The exception to this is the '-' char at the
103 start of RBLSMTPD which is used to force a hard error in Dan's rblsmtpd.
104 NB I don't really see the benefit
105 of using a soft error for a site in an RBL list. This just complicates
106 things as it takes 7 days (or whatever default period) before a user
107 gets an error email back. In the meantime they are complaining that their
108 emails are being "lost" :(
109
110 =over 4
111
112 =item RBLSMTPD is set and non-empty
113
114 The contents are used as the SMTP conversation error.
115 Use this for forcibly blocking sites you don't like
116
117 =item RBLSMTPD is set, but empty
118
119 In this case no RBL checks are made.
120 This can be used for local addresses.
121
122 =item RBLSMTPD is not set
123
124 All RBL checks will be made.
125 This is the setting for remote sites that you want to check against RBL.
126
127 =back
128
129 =head1 Revisions
130
131 See: https://github.com/smtpd/qpsmtpd/commits/master/plugins/dnsbl
132
133 =cut
134
135 sub register {
136 my ($self, $qp) = (shift, shift);
137
138 if (@_ % 2) {
139 $self->{_args}{reject_type} = shift; # backwards compatibility
140 }
141 else {
142 $self->{_args} = {@_};
143 }
144
145 # explicitly state legacy reject behavior
146 if (!defined $self->{_args}{reject_type}) {
147 $self->{_args}{reject_type} = 'perm';
148 }
149 if (!defined $self->{_args}{reject}) {
150 $self->{_args}{reject} = 1;
151 }
152 }
153
154 sub hook_connect {
155 my ($self, $transaction) = @_;
156
157 # perform RBLSMTPD checks to mimic DJB's rblsmtpd
158 # RBLSMTPD being non-empty means it contains the failure message to return
159 if (defined $ENV{'RBLSMTPD'} && $ENV{'RBLSMTPD'} ne '') {
160 my $reject = $self->{_args}{reject};
161 return $self->return_env_message() if $reject && $reject eq 'connect';
162 }
163
164 return DECLINED if $self->is_immune();
165 return DECLINED if $self->is_set_rblsmtpd();
166 return DECLINED if $self->ip_whitelisted();
167
168 my $dnsbl_zones = $self->get_dnsbl_zones() or return DECLINED;
169 my $resolv = $self->get_resolver() or return DECLINED;
170
171 for my $dnsbl (keys %$dnsbl_zones) {
172
173 my $query = $self->get_query($dnsbl) or do {
174 if ($resolv->errorstring ne 'NXDOMAIN') {
175 $self->log(LOGERROR, "$dnsbl query failed: ",
176 $resolv->errorstring);
177 }
178 next;
179 };
180
181 my $a_record = 0;
182 my $result;
183 foreach my $rr ($query->answer) {
184 if ($rr->type eq 'A') {
185 $result = $rr->name;
186 $self->log(LOGDEBUG,
187 "found A for $result with IP " . $rr->address);
188 }
189 elsif ($rr->type eq 'TXT') {
190 $self->log(LOGDEBUG, "found TXT, " . $rr->txtdata);
191 $result = $rr->txtdata;
192 }
193
194 next if !$result;
195
196 $self->adjust_karma(-1);
197
198 if (!$dnsbl) { ($dnsbl) = ($result =~ m/(?:\d+\.){4}(.*)/); }
199 if (!$dnsbl) { $dnsbl = $result; }
200
201 if ($a_record) {
202 if (defined $dnsbl_zones->{$dnsbl}) {
203 my $smtp_msg = $dnsbl_zones->{$dnsbl};
204 my $remote_ip = $self->qp->connection->remote_ip;
205 $smtp_msg =~ s/%IP%/$remote_ip/g;
206 return $self->get_reject($smtp_msg, $dnsbl);
207 }
208 return $self->get_reject("Blocked by $dnsbl");
209 }
210
211 return $self->get_reject($result, $dnsbl);
212 }
213 }
214
215 $self->log(LOGINFO, 'pass');
216 return DECLINED;
217 }
218
219 sub get_dnsbl_zones {
220 my $self = shift;
221
222 my %dnsbl_zones =
223 map { (split /:/, $_, 2)[0, 1] } $self->qp->config('dnsbl_zones');
224 if (!%dnsbl_zones) {
225 $self->log(LOGDEBUG, "skip, no zones");
226 return;
227 }
228
229 $self->{_dnsbl}{zones} = \%dnsbl_zones;
230 return \%dnsbl_zones;
231 }
232
233 sub get_query {
234 my ($self, $dnsbl) = @_;
235
236 my $remote_ip = $self->qp->connection->remote_ip;
237 my $reversed_ip = join('.', reverse(split(/\./, $remote_ip)));
238
239 # fix to find A records, if the dnsbl_zones line has a second field 20/1/04 ++msp
240 if (defined $self->{_dnsbl}{zones}{$dnsbl}) {
241 $self->log(LOGDEBUG, "Checking $reversed_ip.$dnsbl for A record");
242 return $self->{_resolver}->query("$reversed_ip.$dnsbl");
243 }
244
245 $self->log(LOGDEBUG, "Checking $reversed_ip.$dnsbl for TXT record");
246 return $self->{_resolver}->query("$reversed_ip.$dnsbl", 'TXT');
247 }
248
249 sub is_set_rblsmtpd {
250 my $self = shift;
251
252 my $remote_ip = $self->qp->connection->remote_ip;
253
254 if (!defined $ENV{'RBLSMTPD'}) {
255 $self->log(LOGDEBUG, "RBLSMTPD not set for $remote_ip");
256 return;
257 }
258
259 if ($ENV{'RBLSMTPD'} ne '') {
260 $self->log(LOGINFO, "RBLSMTPD=\"$ENV{'RBLSMTPD'}\" for $remote_ip");
261 return $ENV{'RBLSMTPD'};
262 }
263
264 $self->log(LOGINFO, "RBLSMTPD set, but empty for $remote_ip");
265 return 1; # don't return empty string, it evaluates to false
266 }
267
268 sub ip_whitelisted {
269 my ($self) = @_;
270
271 my $remote_ip = $self->qp->connection->remote_ip;
272
273 return grep {
274 s/\.?$/./;
275 $_ eq substr($remote_ip . '.', 0, length $_)
276 } $self->qp->config('dnsbl_allow');
277 }
278
279 sub return_env_message {
280 my $self = shift;
281 my $result = $ENV{'RBLSMTPD'};
282 my $remote_ip = $self->qp->connection->remote_ip;
283 $result =~ s/%IP%/$remote_ip/g;
284 my $msg = $self->qp->config('dnsbl_rejectmsg');
285 $self->log(LOGINFO, "fail, $msg");
286 return ($self->get_reject_type(), join(' ', $msg, $result));
287 }
288
289 sub hook_rcpt {
290 my ($self, $transaction, $rcpt, %param) = @_;
291
292 if ($rcpt->user =~ /^(?:postmaster|abuse|mailer-daemon|root)$/i) {
293 $self->log(LOGWARN,
294 "skip, don't blacklist special account: " . $rcpt->user);
295
296 # clear the naughty connection note here, if desired.
297 $self->is_naughty(0);
298 }
299
300 return DECLINED;
301 }
302
303 sub get_resolver {
304 my $self = shift;
305 return $self->{_resolver} if $self->{_resolver};
306 $self->log(LOGDEBUG, "initializing Net::DNS::Resolver");
307 $self->{_resolver} = Net::DNS::Resolver->new(dnsrch => 0);
308 my $timeout = $self->{_args}{timeout} || 30;
309 $self->{_resolver}->tcp_timeout($timeout);
310 $self->{_resolver}->udp_timeout($timeout);
311 return $self->{_resolver};
312 }
313