Debian Codegraph @

plugins/relay

raw
1 #!perl -w
2
3 =head1 SYNOPSIS
4
5 relay - control whether relaying is permitted
6
7 =head1 DESCRIPTION
8
9 relay - check the following places to see if relaying is allowed:
10
11 I<$ENV{RELAYCLIENT}>
12
13 I<config/norelayclients>, I<config/relayclients>, I<config/morerelayclients>
14
15 The search order is as shown and cascades until a match is found or the list
16 is exhausted.
17
18 Note that I<norelayclients> is the first file checked. A match there will
19 override matches in the subsequent files.
20
21 =head1 CONFIG
22
23 Enable this plugin by adding it to config/plugins above the rcpt_* plugins
24
25 # other plugins...
26
27 relay
28
29 # rcpt_* go here
30
31 =head2 relayclients
32
33 A list of IP addresses that are permitted to relay mail through this server.
34
35 Each line in I<relayclients> is one of:
36 - a full IP address
37
38 - partial IP address terminated by a dot or colon for matching whole networks
39 192.168.42.
40 2001:db8:e431:ae06:
41 ...
42
43 - a network/mask, aka a CIDR block
44 10.1.0.0/24
45 2001:db8:e431:ae06::/64
46 ...
47
48 =head2 morerelayclients
49
50 Additional IP addresses that are permitted to relay. The syntax of the config
51 file is identical to I<relayclients> except that CIDR (net/mask) entries are
52 not supported. If you have many (>50) IPs allowed to relay, most should likely
53 be listed in I<morerelayclients> where lookups are faster.
54
55
56 =head2 norelayclients
57
58 I<norelayclients> allows specific clients, such as a mail gateway, to be denied
59 relaying, even though they would be allowed by I<relayclients>. This is most
60 useful when a block of IPs is allowed in relayclients, but several IPs need to
61 be excluded.
62
63 The file format is the same as morerelayclients.
64
65 =head2 RELAY ONLY
66
67 The relay only option restricts connections to only clients that have relay
68 permission. All other connections are denied during the RCPT phase of the
69 SMTP conversation.
70
71 This option is useful when a server is used as the smart relay host for
72 internal users and external/authenticated users, but should not be considered
73 a normal inbound MX server.
74
75 It should be configured to be run before other RCPT hooks! Only clients that
76 have authenticated or are listed in the relayclient file will be allowed to
77 send mail.
78
79 To enable relay only mode, set the B<only> option to any true value in
80 I<config/plugins> as shown:
81
82 relay only 1
83
84 =head1 AUTHOR
85
86 2012 - Matt Simerson - Merged check_relay, check_norelay, and relayonly
87
88 2006 - relay_only - John Peackock
89
90 2005 - check_norelay - Copyright Gordon Rowell <[email protected]>
91
92 2002 - check_relay - Ask Bjorn Hansen
93
94 =head1 LICENSE
95
96 This software is free software and may be distributed under the same
97 terms as qpsmtpd itself.
98
99 =cut
100
101 use strict;
102 use warnings;
103
104 use Qpsmtpd::Constants;
105 use Net::IP qw(:PROC);
106
107 sub register {
108 my ($self, $qp) = (shift, shift);
109 $self->log(LOGERROR, "Bad arguments") if @_ % 2;
110 $self->{_args} = {@_};
111
112 if ($self->{_args}{only}) {
113 $self->register_hook('rcpt', 'relay_only');
114 }
115 }
116
117 sub is_in_norelayclients {
118 my $self = shift;
119
120 my %no_relay_clients = map { $_ => 1 } $self->qp->config('norelayclients');
121
122 my $ip = $self->qp->connection->remote_ip;
123
124 while ($ip) {
125 if (exists $no_relay_clients{$ip}) {
126 $self->log(LOGINFO, "$ip in norelayclients");
127 return 1;
128 }
129 $ip =~ s/(\d|\w)+(:|\.)?$// or last; # strip off another octet
130 }
131
132 $self->log(LOGDEBUG, "no match in norelayclients");
133 return;
134 }
135
136 sub populate_relayclients {
137 my $self = shift;
138
139 foreach ($self->qp->config('relayclients')) {
140 my ($network, $netmask) = ip_splitprefix($_);
141 if ($netmask) {
142 push @{$self->{_cidr_blocks}}, $_;
143 next;
144 }
145 $self->{_octets}{$_} = 1; # no prefix, split
146 }
147 }
148
149 sub is_in_cidr_block {
150 my $self = shift;
151
152 my $ip = $self->qp->connection->remote_ip or do {
153 $self->log(LOGINFO, "err, no remote_ip?");
154 return;
155 };
156 my $cversion = ip_get_version($ip);
157 for (@{$self->{_cidr_blocks}}) {
158 my ($network, $mask) = ip_splitprefix($_); # split IP & CIDR range
159 my $rversion = ip_get_version($network); # get IP version (4 vs 6)
160 my ($begin, $end) = ip_normalize($_, $rversion); # get pool start/end
161
162 # expand the client address (zero pad it) before converting to binary
163 my $bin_ip = ip_iptobin(ip_expand_address($ip, $cversion), $cversion)
164 or next;
165
166 next if !$begin || !$end; # probably not a netmask entry
167
168 if ( ip_bincomp($bin_ip, 'gt', ip_iptobin($begin, $rversion))
169 && ip_bincomp($bin_ip, 'lt', ip_iptobin($end, $rversion)))
170 {
171 $self->log(LOGINFO, "pass, cidr match ($ip)");
172 return 1;
173 }
174 }
175
176 $self->log(LOGDEBUG, "no cidr match");
177 return;
178 }
179
180 sub is_octet_match {
181 my $self = shift;
182
183 my $ip = $self->qp->connection->remote_ip;
184
185 if ($ip eq '::1') {
186 $self->log(LOGINFO, "pass, octet matched localhost ($ip)");
187 return 1;
188 }
189
190 my $more_relay_clients = $self->qp->config('morerelayclients', 'map');
191
192 my $ipv6 = $ip =~ /:/ ? 1 : 0;
193
194 if ($ipv6 && $ip =~ /::/) { # IPv6 compressed notation
195 $ip = Net::IP::ip_expand_address($ip, 6);
196 }
197
198 while ($ip) {
199 if (exists $self->{_octets}{$ip}) {
200 $self->log(LOGINFO, "pass, octet match in relayclients ($ip)");
201 return 1;
202 }
203
204 if (exists $more_relay_clients->{$ip}) {
205 $self->log(LOGINFO, "pass, octet match in morerelayclients ($ip)");
206 return 1;
207 }
208
209 # added IPv6 support (Michael Holzt - 2012-11-14)
210 if ($ipv6) {
211 $ip =~ s/[0-9a-f]:?$//; # strip off another nibble
212 chop $ip if ':' eq substr($ip, -1, 1);
213 }
214 else {
215 $ip =~ s/\d+\.?$// or last; # strip off another 8 bits
216 }
217 }
218
219 $self->log(LOGDEBUG, "no octet match");
220 return;
221 }
222
223 sub hook_connect {
224 my ($self, $transaction) = @_;
225
226 if ($self->is_in_norelayclients()) {
227 $self->qp->connection->relay_client(0);
228 delete $ENV{RELAYCLIENT};
229 $self->log(LOGINFO, "fail, disabled by norelayclients");
230 return (DECLINED);
231 }
232
233 if ($ENV{RELAYCLIENT}) {
234 $self->qp->connection->relay_client(1);
235 $self->log(LOGINFO, "pass, enabled by env");
236 return (DECLINED);
237 }
238
239 $self->populate_relayclients();
240
241 # 95586 (connect) relay: pass, octet match in relayclients (127.0.0.)
242
243 if ($self->is_in_cidr_block() || $self->is_octet_match()) {
244 $self->qp->connection->relay_client(1);
245 return (DECLINED);
246 }
247
248 $self->log(LOGINFO, "skip, no match");
249 return (DECLINED);
250 }
251
252 sub relay_only {
253 my $self = shift;
254 if ($self->qp->connection->relay_client) {
255 return (OK);
256 }
257 return (DENY);
258 }
259