Debian Codegraph @

lib/Qpsmtpd/Postfix.pm

raw
1 package Qpsmtpd::Postfix;
2
3 =head1 NAME
4
5 Qpsmtpd::Postfix - postfix queueing support for qpsmtpd
6
7 =head2 DESCRIPTION
8
9 This package implements the protocol Postfix servers use to communicate
10 with each other. See src/global/rec_type.h in the postfix source for
11 details.
12
13 =cut
14
15 use strict;
16 use IO::Socket::UNIX;
17 use IO::Socket::INET;
18 use vars qw(@ISA);
19 @ISA = qw(IO::Socket::UNIX);
20
21 my %rec_types;
22
23 sub init {
24 my ($self) = @_;
25
26 %rec_types = (
27 REC_TYPE_SIZE => 'C', # first record, created by cleanup
28 REC_TYPE_TIME => 'T', # time stamp, required
29 REC_TYPE_FULL => 'F', # full name, optional
30 REC_TYPE_INSP => 'I', # inspector transport
31 REC_TYPE_FILT => 'L', # loop filter transport
32 REC_TYPE_FROM => 'S', # sender, required
33 REC_TYPE_DONE => 'D', # delivered recipient, optional
34 REC_TYPE_RCPT => 'R', # todo recipient, optional
35 REC_TYPE_ORCP => 'O', # original recipient, optional
36 REC_TYPE_WARN => 'W', # warning message time
37 REC_TYPE_ATTR => 'A', # named attribute for extensions
38
39 REC_TYPE_MESG => 'M', # start message records
40
41 REC_TYPE_CONT => 'L', # long data record
42 REC_TYPE_NORM => 'N', # normal data record
43
44 REC_TYPE_XTRA => 'X', # start extracted records
45
46 REC_TYPE_RRTO => 'r', # return-receipt, from headers
47 REC_TYPE_ERTO => 'e', # errors-to, from headers
48 REC_TYPE_PRIO => 'P', # priority
49 REC_TYPE_VERP => 'V', # VERP delimiters
50
51 REC_TYPE_END => 'E', # terminator, required
52
53 );
54
55 }
56
57 sub print_rec {
58 my ($self, $type, @list) = @_;
59
60 die "unknown record type" unless ($rec_types{$type});
61 $self->print($rec_types{$type});
62
63 # the length is a little endian base-128 number where each
64 # byte except the last has the high bit set:
65 my $s = "@list";
66 my $ln = length($s);
67 while ($ln >= 0x80) {
68 my $lnl = $ln & 0x7F;
69 $ln >>= 7;
70 $self->print(chr($lnl | 0x80));
71 }
72 $self->print(chr($ln));
73
74 $self->print($s);
75 }
76
77 sub print_rec_size {
78 my ($self, $content_size, $data_offset, $rcpt_count) = @_;
79
80 my $s =
81 sprintf("%15ld %15ld %15ld", $content_size, $data_offset, $rcpt_count);
82 $self->print_rec('REC_TYPE_SIZE', $s);
83 }
84
85 sub print_rec_time {
86 my ($self, $time) = @_;
87
88 $time = time() unless (defined($time));
89
90 my $s = sprintf("%d", $time);
91 $self->print_rec('REC_TYPE_TIME', $s);
92 }
93
94 sub open_cleanup {
95 my ($class, $socket) = @_;
96
97 my $self;
98 if ($socket =~ m#^(/.+)#) {
99 $socket = $1; # un-taint socket path
100 $self = IO::Socket::UNIX->new(Type => SOCK_STREAM,
101 Peer => $socket)
102 if $socket;
103
104 }
105 elsif ($socket =~ /(.*):(\d+)/) {
106 my ($host, $port) = ($1, $2); # un-taint address and port
107 $self = IO::Socket::INET->new(
108 Proto => 'tcp',
109 PeerAddr => $host,
110 PeerPort => $port
111 )
112 if $host and $port;
113 }
114 unless (ref $self) {
115 warn "Couldn't open \"$socket\": $!";
116 return;
117 }
118
119 # allow buffered writes
120 $self->autoflush(0);
121 bless($self, $class);
122 $self->init();
123 return $self;
124 }
125
126 sub print_attr {
127 my ($self, @kv) = @_;
128 for (@kv) {
129 $self->print("$_\0");
130 }
131 $self->print("\0");
132 }
133
134 sub get_attr {
135 my ($self) = @_;
136 local $/ = "\0";
137 my %kv;
138 for (; ;) {
139 my $k = $self->getline;
140 chomp($k);
141 last unless ($k);
142 my $v = $self->getline;
143 chomp($v);
144 $kv{$k} = $v;
145 }
146 return %kv;
147 }
148
149 =head2 print_msg_line($line)
150
151 print one line of a message to cleanup.
152
153 This removes any linefeed characters from the end of the line
154 and splits the line across several records if it is longer than
155 1024 chars.
156
157 =cut
158
159 sub print_msg_line {
160 my ($self, $line) = @_;
161
162 $line =~ s/\r?\n$//s;
163
164 # split into 1k chunks.
165 while (length($line) > 1024) {
166 my $s = substr($line, 0, 1024);
167 $line = substr($line, 1024);
168 $self->print_rec('REC_TYPE_CONT', $s);
169 }
170 $self->print_rec('REC_TYPE_NORM', $line);
171 }
172
173 =head2 inject_mail($transaction)
174
175 (class method) inject mail in $transaction into postfix queue via cleanup.
176 $transaction is supposed to be a Qpsmtpd::Transaction object.
177
178 =cut
179
180 sub inject_mail {
181 my ($class, $transaction) = @_;
182
183 my @sockets = @{$transaction->notes('postfix-queue-sockets')
184 // ['/var/spool/postfix/public/cleanup']};
185 my $strm;
186 $strm = $class->open_cleanup($_) and last for @sockets;
187 die "Unable to open any cleanup sockets!" unless $strm;
188
189 my %at = $strm->get_attr;
190 my $qid = $at{queue_id};
191 print STDERR "qid=$qid\n";
192 $strm->print_attr('flags' => $transaction->notes('postfix-queue-flags'));
193 $strm->print_rec_time();
194 $strm->print_rec('REC_TYPE_FROM', $transaction->sender->address || "");
195 for (map { $_->address } $transaction->recipients) {
196 $strm->print_rec('REC_TYPE_RCPT', $_);
197 }
198
199 # add an empty message length record.
200 # cleanup is supposed to understand that.
201 # see src/pickup/pickup.c
202 $strm->print_rec('REC_TYPE_MESG', "");
203
204 # a received header has already been added in SMTP.pm
205 # so we can just copy the message:
206
207 my $hdr = $transaction->header->as_string;
208 for (split(/\r?\n/, $hdr)) {
209 print STDERR "hdr: $_\n";
210 $strm->print_msg_line($_);
211 }
212 $transaction->body_resetpos;
213 while (my $line = $transaction->body_getline) {
214
215 # print STDERR "body: $line\n";
216 $strm->print_msg_line($line);
217 }
218
219 # finish it.
220 $strm->print_rec('REC_TYPE_XTRA', "");
221 $strm->print_rec('REC_TYPE_END', "");
222 $strm->flush();
223 %at = $strm->get_attr;
224 my $status = $at{status};
225 my $reason = $at{reason};
226 $strm->close();
227 return wantarray ? ($status, $qid, $reason || "") : $status;
228 }
229
230 1;
231
232 # vim:sw=2