Debian Codegraph @

plugins/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 It also supports the Postfix XCLIENT extension.
13
14 =head1 CONFIG
15
16 It takes one required parameter, the IP address or hostname to forward to.
17
18 queue/smtp-forward 10.2.2.2
19
20 Optionally you can also add a port:
21
22 queue/smtp-forward 10.2.2.2 9025
23
24 And a flag:
25
26 queue/smtp-forward 10.2.2.2 9025 xclient
27
28 =cut
29
30 use Net::SMTP;
31 use Net::Cmd qw//;
32
33 sub init {
34 my ($self, $qp, @args) = @_;
35
36 if (@args <= 0) {
37 die "No SMTP server specified in smtp-forward config";
38 };
39
40 if ($args[0] =~ /^([\.\w_-]+)$/) {
41 $self->{_smtp_server} = $1;
42 }
43 else {
44 die "Bad data in smtp server: $args[0]";
45 }
46
47 $self->{_smtp_port} = 25;
48 if (@args > 1 and $args[1] =~ /^(\d+)$/) {
49 $self->{_smtp_port} = $1;
50 }
51
52 for (my $i = 2; $i < @args; $i++) {
53 if ($args[$i] !~ /^(\w+)$/) {
54 $self->log(LOGWARN, "WARNING: Rejecting invalid flag");
55 next;
56 }
57 my $flag = lc($1);
58 $self->log(LOGWARN, "WARNING: Unknown flag $flag") unless $flag eq 'xclient';
59 $self->{_flags}{$flag} = 1;
60 }
61 }
62
63 sub hook_queue {
64 my ($self, $transaction) = @_;
65
66 $self->log(LOGINFO,
67 "forwarding to $self->{_smtp_server}:$self->{_smtp_port}");
68 my $smtp = Net::SMTP->new(
69 $self->{_smtp_server},
70 Port => $self->{_smtp_port},
71 Timeout => 60,
72 Hello => $self->qp->config("me"),
73 ) || die $!;
74
75
76 my $xcret = $self->xclient($smtp);
77 return(DECLINED, $xcret) if defined $xcret;
78
79 $smtp->mail($transaction->sender->address || "")
80 or return (DECLINED, "Unable to queue message ($!)");
81 for ($transaction->recipients) {
82 $smtp->to($_->address)
83 or return (DECLINED, "Unable to queue message ($!)");
84 }
85 $smtp->data() or return (DECLINED, "Unable to queue message ($!)");
86 $smtp->datasend($transaction->header->as_string)
87 or return (DECLINED, "Unable to queue message ($!)");
88 $transaction->body_resetpos;
89 while (my $line = $transaction->body_getline) {
90 $smtp->datasend($line)
91 or return (DECLINED, "Unable to queue message ($!)");
92 }
93 $smtp->dataend() or return (DECLINED, "Unable to queue message ($!)");
94 my $qid = $smtp->message();
95 my @list = split(' ', $qid);
96 $qid = pop(@list);
97
98 $smtp->quit() or return (DECLINED, "Unable to queue message ($!)");
99 $self->log(LOGINFO, "finished queueing");
100 return (OK, "queued as $qid");
101 }
102
103 sub xclient {
104 my ($self, $smtp) = @_;
105
106 return unless $self->{_flags}{xclient};
107
108 my $parts = $smtp->supports('XCLIENT');
109 if (!defined($parts)) { # what parts do they want?
110 return "Unable to queue message (Server does not advertise XCLIENT support)";
111 };
112
113 my %haveparts;
114 for my $part (split(/\s+/, $parts)) {
115 next unless $part =~ /^(\w+)$/;
116 $haveparts{uc($part)} = 1;
117 }
118
119 my $conn = $self->qp->connection;
120 my @rparts;
121
122 if ($haveparts{NAME}) {
123 my $name = $conn->remote_host || '[UNAVAILABLE]';
124 $name = '[UNAVAILABLE]' if ($name eq 'Unknown');
125 push(@rparts, "NAME=$name");
126 }
127
128 if ($haveparts{ADDR}) {
129 my $ip = $conn->remote_ip;
130 push(@rparts, "ADDR=$ip");
131 }
132
133 if ($haveparts{PORT}) {
134 my $port = $conn->remote_port;
135 push(@rparts, "PORT=$port");
136 }
137
138 my $hello_name = $self->connection->hello_host;
139 $hello_name ||= '[UNAVAILABLE]';
140 if ($haveparts{HELO}) {
141 push(@rparts, "HELO=$hello_name");
142 }
143
144 my $hello = $conn->hello;
145 if ($haveparts{PROTO} && defined($hello)) {
146 my $proto = (uc($hello) eq 'EHLO') ? 'ESMTP' : 'SMTP';
147 push(@rparts, "PROTO=$proto");
148 }
149
150 while (scalar(@rparts)) {
151 my @items;
152 my $cursz = 0;
153 while (defined(my $item = $rparts[0])) {
154 my $len = length($item);
155 last if ($cursz + $len > 500);
156 $cursz += $len;
157 push(@items, shift @rparts);
158 }
159
160 last unless @items;
161 if ($smtp->command('XCLIENT', @items)->response() != Net::Cmd::CMD_OK) {
162 return "Unable to queue message (XCLIENT failed)";
163 }
164 }
165
166 $smtp->hello($hello_name) or return "Unable to queue message (HELLO after XCLIENT failed)";
167 return;
168 }