Debian Codegraph @

plugins/async/queue/smtp-forward

raw
1 #!perl -w
2
3 =head1 NAME
4
5 smtp-forward
6
7 =head1 DESCRIPTION
8
9 This plugin forwards the mail via SMTP to a specified server, rather than
10 delivering the email locally.
11
12 =head1 CONFIG
13
14 It takes one required parameter, the IP address or hostname to forward to.
15
16 async/queue/smtp-forward 10.2.2.2
17
18 Optionally you can also add a port:
19
20 async/queue/smtp-forward 10.2.2.2 9025
21
22 =cut
23
24 use Qpsmtpd::Constants;
25
26 sub register {
27 my ($self, $qp) = @_;
28
29 $self->register_hook(queue => "start_queue");
30 $self->register_hook(queue => "finish_queue");
31 }
32
33 sub init {
34 my ($self, $qp, @args) = @_;
35
36 if (@args > 0) {
37 if ($args[0] =~ /^([\.\w_-]+)$/) {
38 $self->{_smtp_server} = $1;
39 }
40 else {
41 die "Bad data in smtp server: $args[0]";
42 }
43 $self->{_smtp_port} = 25;
44 if (@args > 1 and $args[1] =~ /^(\d+)$/) {
45 $self->{_smtp_port} = $1;
46 }
47
48 $self->log(LOGWARN, "WARNING: Ignoring additional arguments.")
49 if (@args > 2);
50 }
51 else {
52 die("No SMTP server specified in smtp-forward config");
53 }
54
55 }
56
57 sub start_queue {
58 my ($self, $transaction) = @_;
59
60 my $qp = $self->qp;
61 my $SERVER = $self->{_smtp_server};
62 my $PORT = $self->{_smtp_port};
63 $self->log(LOGINFO, "forwarding to $SERVER:$PORT");
64
65 $transaction->notes(
66 'async_sender',
67 AsyncSMTPSender->new(
68 $SERVER, $PORT, $qp, $self, $transaction
69 )
70 );
71
72 return YIELD;
73 }
74
75 sub finish_queue {
76 my ($self, $transaction) = @_;
77
78 my $sender = $transaction->notes('async_sender');
79 $transaction->notes('async_sender', undef);
80
81 my ($rc, $msg) = $sender->results;
82
83 return $rc, $msg;
84 }
85
86 package AsyncSMTPSender;
87
88 use IO::Socket;
89
90 use base qw(Danga::Socket);
91 use fields qw(
92 qp
93 pkg
94 tran
95 state
96 rcode
97 rmsg
98 buf
99 command
100 resp
101 to
102 );
103
104 use constant ST_CONNECTING => 0;
105 use constant ST_CONNECTED => 1;
106 use constant ST_COMMANDS => 2;
107 use constant ST_DATA => 3;
108
109 use Qpsmtpd::Constants;
110
111 sub new {
112 my ($self, $server, $port, $qp, $pkg, $transaction) = @_;
113 $self = fields::new($self) unless ref $self;
114
115 my $sock = IO::Socket::INET->new(
116 PeerAddr => $server,
117 PeerPort => $port,
118 Blocking => 0,
119 )
120 or die "Error connecting to server $server:$port : $!\n";
121
122 IO::Handle::blocking($sock, 0);
123 binmode($sock, ':raw');
124
125 $self->{qp} = $qp;
126 $self->{pkg} = $pkg;
127 $self->{tran} = $transaction;
128 $self->{state} = ST_CONNECTING;
129 $self->{rcode} = DECLINED;
130 $self->{command} = 'connect';
131 $self->{buf} = '';
132 $self->{resp} = [];
133
134 # copy the recipients so we can pop them off one by one
135 $self->{to} = [$transaction->recipients];
136
137 $self->SUPER::new($sock);
138
139 # Watch for write first, this is when the TCP session is established.
140 $self->watch_write(1);
141
142 return $self;
143 }
144
145 sub results {
146 my AsyncSMTPSender $self = shift;
147 return ($self->{rcode}, $self->{rmsg});
148 }
149
150 sub log {
151 my AsyncSMTPSender $self = shift;
152 $self->{qp}->log(@_);
153 }
154
155 sub cont {
156 my AsyncSMTPSender $self = shift;
157 $self->{qp}->run_continuation;
158 }
159
160 sub command {
161 my AsyncSMTPSender $self = shift;
162 my ($command, $params) = @_;
163 $params ||= '';
164
165 $self->log(LOGDEBUG, ">> $command $params");
166
167 $self->write( ($command =~ m/ / ? "$command:" : $command)
168 . ($params ? " $params" : "")
169 . "\r\n");
170 $self->watch_read(1);
171 $self->{command} = ($command =~ /(\S+)/)[0];
172 }
173
174 sub handle_response {
175 my AsyncSMTPSender $self = shift;
176
177 my $method = "cmd_" . lc($self->{command});
178
179 $self->$method(@_);
180 }
181
182 sub cmd_connect {
183 my AsyncSMTPSender $self = shift;
184 my ($code, $response) = @_;
185
186 if ($code != 220) {
187 $self->{rmsg} = "Error on connect: @$response";
188 $self->close;
189 $self->cont;
190 }
191 else {
192 my $host = $self->{qp}->config('me');
193 print "HELOing with $host\n";
194 $self->command((join '', @$response) =~ m/ ESMTP/ ? "EHLO" : "HELO",
195 $host);
196 }
197 }
198
199 sub cmd_helo {
200 my AsyncSMTPSender $self = shift;
201 my ($code, $response) = @_;
202
203 if ($code != 250) {
204 $self->{rmsg} = "Error on HELO: @$response";
205 $self->close;
206 $self->cont;
207 }
208 else {
209 $self->command("MAIL", "FROM:" . $self->{tran}->sender->format);
210 }
211 }
212
213 sub cmd_ehlo {
214 my AsyncSMTPSender $self = shift;
215 my ($code, $response) = @_;
216
217 if ($code != 250) {
218 $self->{rmsg} = "Error on EHLO: @$response";
219 $self->close;
220 $self->cont;
221 }
222 else {
223 $self->command("MAIL", "FROM:" . $self->{tran}->sender->format);
224 }
225 }
226
227 sub cmd_mail {
228 my AsyncSMTPSender $self = shift;
229 my ($code, $response) = @_;
230
231 if ($code != 250) {
232 $self->{rmsg} = "Error on MAIL FROM: @$response";
233 $self->close;
234 $self->cont;
235 }
236 else {
237 $self->command("RCPT", "TO:" . shift(@{$self->{to}})->format);
238 }
239 }
240
241 sub cmd_rcpt {
242 my AsyncSMTPSender $self = shift;
243 my ($code, $response) = @_;
244
245 if ($code != 250) {
246 $self->{rmsg} = "Error on RCPT TO: @$response";
247 $self->close;
248 $self->cont;
249 }
250 else {
251 if (@{$self->{to}}) {
252 $self->command("RCPT", "TO:" . shift(@{$self->{to}})->format);
253 }
254 else {
255 $self->command("DATA");
256 }
257 }
258 }
259
260 sub cmd_data {
261 my AsyncSMTPSender $self = shift;
262 my ($code, $response) = @_;
263
264 if ($code != 354) {
265 $self->{rmsg} = "Error on DATA: @$response";
266 $self->close;
267 $self->cont;
268 }
269 else {
270 # $self->{state} = ST_DATA;
271 $self->datasend($self->{tran}->header->as_string);
272 $self->{tran}->body_resetpos;
273 my $write_buf = '';
274 while (my $line = $self->{tran}->body_getline) {
275 $line =~ s/\r?\n/\r\n/;
276 $write_buf .= $line;
277 if (length($write_buf) >= 131072) { # 128KB, arbitrary value
278 $self->log(LOGDEBUG, ">> $write_buf");
279 $self->datasend($write_buf);
280 $write_buf = '';
281 }
282 }
283 if (length($write_buf)) {
284 $self->log(LOGDEBUG, ">> $write_buf");
285 $self->datasend($write_buf);
286 }
287 $self->write(".\r\n");
288 $self->{command} = "DATAEND";
289 }
290 }
291
292 sub cmd_dataend {
293 my AsyncSMTPSender $self = shift;
294 my ($code, $response) = @_;
295
296 if ($code != 250) {
297 $self->{rmsg} = "Error after DATA: @$response";
298 $self->close;
299 $self->cont;
300 }
301 else {
302 $self->command("QUIT");
303 }
304 }
305
306 sub cmd_quit {
307 my AsyncSMTPSender $self = shift;
308 my ($code, $response) = @_;
309
310 $self->{rcode} = OK;
311 $self->{rmsg} = "Queued!";
312 $self->close;
313 $self->cont;
314 }
315
316 sub datasend {
317 my AsyncSMTPSender $self = shift;
318 my ($data) = @_;
319 $data =~ s/^\./../mg;
320 $self->write(\$data);
321 }
322
323 sub event_read {
324 my AsyncSMTPSender $self = shift;
325
326 if ($self->{state} == ST_CONNECTED) {
327 $self->{state} = ST_COMMANDS;
328 }
329
330 if ($self->{state} == ST_COMMANDS) {
331 my $in = $self->read(1024);
332 if (!$in) {
333
334 # XXX: connection closed
335 $self->close("lost connection");
336 return;
337 }
338
339 my @lines = split /\r?\n/, $self->{buf} . $$in, -1;
340 $self->{buf} = delete $lines[-1];
341
342 for (@lines) {
343 if (my ($code, $cont, $rest) = /^([0-9]{3})([ -])(.*)/) {
344 $self->log(LOGDEBUG, "<< $code$cont$rest");
345 push @{$self->{resp}}, $rest;
346
347 if ($cont eq ' ') {
348 $self->handle_response($code, $self->{resp});
349 $self->{resp} = [];
350 }
351 }
352 else {
353 $self->log(LOGERROR, "Unrecognised SMTP response line: $_");
354 $self->{rmsg} = "Error from upstream SMTP server";
355 $self->close;
356 $self->cont;
357 }
358 }
359 }
360 else {
361 $self->log(LOGERROR, "SMTP Session occurred out of order");
362 $self->close;
363 $self->cont;
364 }
365 }
366
367 sub event_write {
368 my AsyncSMTPSender $self = shift;
369
370 if ($self->{state} == ST_CONNECTING) {
371 $self->watch_write(0);
372 $self->{state} = ST_CONNECTED;
373 $self->watch_read(1);
374 }
375 elsif (0 && $self->{state} == ST_DATA) {
376
377 # send more data
378 if (my $line = $self->{tran}->body_getline) {
379 $self->log(LOGDEBUG, ">> $line");
380 $line =~ s/\r?\n/\r\n/;
381 $self->datasend($line);
382 }
383 else {
384 # no more data.
385 $self->log(LOGINFO, "No more data");
386 $self->watch_write(0);
387 $self->{state} = ST_COMMANDS;
388 }
389 }
390 else {
391 $self->write(undef);
392 }
393 }
394
395 sub event_err {
396 my ($self) = @_;
397 eval { $self->read(1); }; # gives us the correct error in errno
398 $self->{rmsg} = "Read error from remote server: $!";
399
400 #print "lost connection: $!\n";
401 $self->close;
402 $self->cont;
403 }
404
405 sub event_hup {
406 my ($self) = @_;
407 eval { $self->read(1); }; # gives us the correct error in errno
408 $self->{rmsg} = "HUP error from remote server: $!";
409
410 #print "lost connection: $!\n";
411 $self->close;
412 $self->cont;
413 }