Debian Codegraph @

plugins/qmail_deliverable

raw
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 qmail_deliverable - Check that the recipient address is deliverable
6
7 =head1 DESCRIPTION
8
9 See the description of Qmail::Deliverable.
10
11 This B<qpsmtpd plugin> uses the client/server interface and needs a running
12 qmail-deliverabled. If no connection can be made, deliverability is simply
13 assumed.
14
15 The modules LWP (libwww-perl) and HTTP::Daemon, available from CPAN, are
16 required for qmail-deliverabled and Qmail::Deliverable::Client.
17
18 =head1 CONFIGURATION
19
20 =over 4
21
22 =item server host:port
23
24 Hostname (or IP address), and port (both!) of the qmail-deliverabled server. If
25 none is specified, the default (127.0.0.1:8998) is used.
26
27 =item server smtproutes:host:port
28
29 If the specification is prepended by the literal text C<smtproutes:>, then for
30 recipient domains listed in your /var/qmail/control/smtproutes use their
31 respective hosts for the check. For other domains, the given host is used. The
32 port has to be the same across all servers.
33
34 Example:
35
36 qmail_deliverable server smtproutes:127.0.0.1:8998
37
38 Use "smtproutes:8998" (no second colon) to simply skip the deliverability
39 check for domains not listed in smtproutes.
40
41 =item vpopmail_ext [ 0 | 1 ]
42
43 Is vpopmail configured with the qmail-ext feature enabled? If so, this config
44 option must be enabled in order for [email protected] addresses to work.
45
46 Default: 0
47
48 =item reject
49
50 karma reject [ 0 | 1 | connect | naughty ]
51
52 I<0> will not reject any connections.
53
54 I<1> will reject naughty senders.
55
56 I<connect> is the most efficient setting.
57
58 To reject at any other connection hook, use the I<naughty> setting and the
59 B<naughty> plugin.
60
61 =back
62
63 =head1 CAVEATS
64
65 Given a null host in smtproutes, the normal MX lookup should be used. This
66 plugin does not do this, because we don't want to harrass arbitrary servers.
67
68 Connection failure is *faked* when there is no smtproute.
69
70 =head1 LEGAL
71
72 This software is released into the public domain, and does not come with
73 warranty or guarantee of any kind. Use it at your own risk.
74
75 =head1 AUTHOR
76
77 Juerd <#####@juerd.nl>
78
79 =head1 SEE ALSO
80
81 L<Qmail::Deliverable>, L<qmail-deliverabled>, L<Qmail::Deliverable::Client>
82
83 =cut
84
85 #################################
86 #################################
87
88 BEGIN {
89 use FindBin qw($Bin $Script);
90 if (not $INC{'Qpsmtpd.pm'}) {
91 my $dir = '$PLUGINS_DIRECTORY';
92 -d and $dir = $_ for qw(
93 /home/qpsmtpd/plugins
94 /home/smtp/qpsmtpd/plugins
95 /usr/local/qpsmtpd/plugins
96 /usr/local/share/qpsmtpd/plugins
97 /usr/share/qpsmtpd/plugins
98 );
99
100 my $file = "the 'plugins' configuration file";
101 -f and $file = $_ for qw(
102 /home/qpsmtpd/config/plugins
103 /home/smtp/qpsmtpd/config/plugins
104 /usr/local/qpsmtpd/config/plugins
105 /usr/local/etc/qpsmtpd/plugins
106 /etc/qpsmtpd/plugins
107 );
108
109 # "die" would print "BEGIN failed" garbage
110 print STDERR <<"END";
111
112 This is a plugin for qpsmtpd and should not be run manually.
113
114 To install the plugin:
115
116 ln -s $Bin/$Script $dir/
117
118 And add "$Script server 127.0.0.1:8998" to $file, before rcpt_ok.
119 For configuration instructions, read "man $Script"
120
121 (Paths may vary.)
122
123 END
124 exit 255;
125 }
126 }
127
128 #################################
129 #################################
130
131 use strict;
132 use warnings;
133
134 use Qpsmtpd::Constants;
135 use Qmail::Deliverable::Client qw(deliverable);
136
137 my %smtproutes;
138 my $shared_domain; # global variable to be closed over by the SERVER callback
139
140 sub register {
141 my ($self, $qp, @args) = @_;
142 if (@args % 2) {
143 $self->log(LOGWARN, "Odd number of arguments, using default config");
144 }
145 else {
146 my %args = @args;
147 if ($args{server} && $args{server} =~ /^smtproutes:/) {
148
149 my ($fallback, $port) = $args{server} =~ /:(?:(.*?):?)(\d+)/;
150
151 open my $fh, "/var/qmail/control/smtproutes"
152 or warn "Could not read smtproutes";
153 for (readline $fh) {
154 my ($domain, $mx) = /^(.*?) : \[? ( [^\]:\s]* )/x;
155 $smtproutes{$domain} = $mx;
156 }
157
158 $Qmail::Deliverable::Client::SERVER = sub {
159 my $server = _smtproute($shared_domain);
160 return "$server:$port" if defined $server;
161 return "$fallback:$port" if defined $fallback;
162 return;
163 };
164
165 }
166 elsif ($args{server}) {
167 $Qmail::Deliverable::Client::SERVER = $args{server};
168 }
169
170 if ($args{vpopmail_ext}) {
171 $Qmail::Deliverable::VPOPMAIL_EXT = $args{vpopmail_ext};
172 }
173 if ($args{reject}) {
174 $self->{_args}{reject} = $args{reject};
175 }
176 }
177 $self->register_hook("rcpt", "rcpt_handler");
178 }
179
180 sub rcpt_handler {
181 my ($self, $transaction, $rcpt) = @_;
182
183 return DECLINED if $self->is_immune(); # requires QP 0.90+
184
185 my $address = $rcpt->address;
186 $self->log(LOGDEBUG, "Checking deliverability for recipient '$address'");
187
188 $shared_domain = $rcpt->host;
189
190 my $rv = deliverable $address;
191
192 if (not defined $rv or not length $rv) {
193 $self->log(LOGWARN, "error (unknown) checking '$address'");
194 return DECLINED;
195 }
196
197 my $k = 0; # known status code
198 $self->log(LOGINFO, "error, permission failure"), $k++ if $rv == 0x11;
199 $self->log(LOGINFO, "pass, qmail-command in dot-qmail"), $k++
200 if $rv == 0x12;
201 $self->log(LOGINFO, "pass, bouncesaying with program"), $k++ if $rv == 0x13;
202 if ($rv == 0x14) {
203 my $s = $transaction->sender->address;
204 if (!$s || $s eq '<>') {
205 $self->adjust_karma(-1);
206 return (DENY, "mailing lists do not accept null senders");
207 };
208 $self->log(LOGINFO, "pass, ezmlm list");
209 $k++;
210 }
211 $self->log(LOGINFO, "Temporarily undeliverable: group/world writable"), $k++
212 if $rv == 0x21;
213 $self->log(LOGINFO, "Temporarily undeliverable: sticky home directory"),
214 $k++
215 if $rv == 0x22;
216 $self->log(LOGINFO, "error, $Qmail::Deliverable::Client::ERROR"), $k++
217 if $rv == 0x2f;
218 $self->log(LOGINFO, "pass, normal delivery"), $k++ if $rv == 0xf1;
219 $self->log(LOGINFO, "pass, vpopmail dir"), $k++ if $rv == 0xf2;
220 $self->log(LOGINFO, "pass, vpopmail alias"), $k++ if $rv == 0xf3;
221 $self->log(LOGINFO, "pass, vpopmail catchall"), $k++ if $rv == 0xf4;
222 $self->log(LOGINFO, "pass, vpopmail vuser"), $k++ if $rv == 0xf5;
223 $self->log(LOGINFO, "pass, vpopmail qmail-ext"), $k++ if $rv == 0xf6;
224 $self->log(LOGINFO, "error, SHOULD NOT HAPPEN"), $k++ if $rv == 0xfe;
225 $self->log(LOGINFO, "fail, address not local"), $k++ if $rv == 0xff;
226
227 if ($rv) {
228 $self->log(LOGINFO, sprintf("error, unknown: 0x%02x", $rv)) if not $k;
229 return DECLINED;
230 }
231
232 $self->adjust_karma(-1);
233 return $self->get_reject("Sorry, no mailbox by that name. qd (#5.1.1)");
234 }
235
236 sub _smtproute {
237 my ($domain) = @_;
238 my @parts = split /\./, $domain;
239 if (exists $smtproutes{$domain}) {
240 return undef if $smtproutes{$domain} eq "";
241 return $smtproutes{$domain};
242 }
243 for (reverse 1 .. @parts) {
244 my $wildcard = join "", map ".$_", @parts[-$_ .. -1];
245 if (exists $smtproutes{$wildcard}) {
246 return undef if $smtproutes{$wildcard} eq "";
247 return $smtproutes{$wildcard};
248 }
249 }
250 return undef if not exists $smtproutes{""};
251 return undef if $smtproutes{""} eq "";
252 return $smtproutes{""};
253 }
254