Debian Codegraph @

plugins/hosts_allow

raw
1 #!perl -w
2
3 =head1 NAME
4
5 hosts_allow - decide if a host is allowed to connect
6
7 =head1 DESCRIPTION
8
9 The B<hosts_allow> module decides before the SMTP-Greeting if a host is
10 allowed to connect. It checks for too many (running) connections from one
11 host (see -m/--max-from-ip options in qpsmtpd-forkserver) and the config
12 file I<hosts_allow>.
13
14 The plugin takes no config/plugin arguments.
15
16 This plugin only works with the forkserver and prefork deployment models. It
17 does not work with the tcpserver deployment model. See SEE ALSO below.
18
19 =head1 CONFIG
20
21 The I<hosts_allow> config file contains lines with two or three items. The
22 first is an IP address or a network/mask pair. The second is a (valid) return
23 code from Qpsmtpd::Constants. The last is a comment which will be returned to
24 the connecting client if the return code is DENY or DENYSOFT (and of course
25 DENY_DISCONNECT and DENYSOFT_DISCONNECT).
26
27 Example:
28
29 192.168.3.4 DECLINED
30 192.168.3.0/24 DENY Sorry, known spam only source
31
32 This would exclude 192.168.3.4 from the DENY of 192.168.3.0/24.
33
34 =head1 SEE ALSO
35
36 To get similar functionality for the tcpserver deployment model, use
37 tcpserver's -x feature. Create a tcp.smtp file with entries like this:
38
39 70.65.227.235:deny
40 183.7.90.207:deny
41 :allow
42
43 compile the tcp.smtp file like this:
44
45 /usr/local/bin/tcprules tcp.smtp.cdb tcp.smtp.tmp < tcp.smtp
46
47 and add the file to the chain of arguments to tcpserver in your run file.
48
49 See also: http://cr.yp.to/ucspi-tcp.html
50
51 =cut
52
53 use strict;
54 use warnings;
55
56 use Qpsmtpd::Constants;
57 use Socket;
58
59 sub hook_pre_connection {
60 my ($self, $transaction, %args) = @_;
61
62 # remote_ip => inet_ntoa($iaddr),
63 # remote_port => $port,
64 # local_ip => inet_ntoa($laddr),
65 # local_port => $lport,
66 # max_conn_ip => $MAXCONNIP,
67 # child_addrs => [values %childstatus],
68
69 my $remote = $args{remote_ip};
70 my $max = $args{max_conn_ip};
71 my $karma = $self->connection->notes('karma_history');
72
73 if ($max) {
74 my $num_conn = 1; # seed with current value
75 my $raddr = inet_aton($remote);
76 foreach my $rip (@{$args{child_addrs}}) {
77 ++$num_conn if (defined $rip && $rip eq $raddr);
78 }
79 $max = $self->karma_bump($karma, $max) if defined $karma;
80 if ($num_conn > $max) {
81 my $err_mess = "too many connections from $remote";
82 $self->log(LOGINFO, "fail: $err_mess ($num_conn > $max)");
83 return (DENYSOFT, "$err_mess, try again later");
84 }
85 }
86
87 my @r = $self->in_hosts_allow($remote);
88 return @r if scalar @r;
89
90 $self->log(LOGDEBUG, "pass");
91 return (DECLINED);
92 }
93
94 sub in_hosts_allow {
95 my $self = shift;
96 my $remote = shift;
97
98 foreach ($self->qp->config('hosts_allow')) {
99 s/^\s*//; # trim leading whitespace
100 my ($ipmask, $const, $message) = split /\s+/, $_, 3;
101 next unless defined $const;
102
103 my ($net, $mask) = split /\//, $ipmask, 2;
104 $mask = 32 if !defined $mask;
105 $mask = pack "B32", "1" x ($mask) . "0" x (32 - $mask);
106 if (join('.', unpack('C4', inet_aton($remote) & $mask)) eq $net) {
107 $const = Qpsmtpd::Constants::return_code($const) || DECLINED;
108 if ($const =~ /deny/i) {
109 $self->log(LOGINFO, "fail, $message");
110 }
111 $self->log(LOGDEBUG, "pass, $const, $message");
112 return ($const, $message);
113 }
114 }
115
116 return;
117 }
118
119 sub karma_bump {
120 my ($self, $karma, $max) = @_;
121
122 if ($karma > 5) {
123 $self->log(LOGDEBUG, "connect limit +3 for positive karma");
124 return $max + 3;
125 }
126 if ($karma <= 0) {
127 $self->log(LOGINFO, "connect limit 1, karma $karma");
128 return 1;
129 }
130 return $max;
131 }