-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubmission
executable file
·380 lines (337 loc) · 10.9 KB
/
Submission
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/perl
# $Id: Submission,v 1.42 2008/07/28 15:31:27 fujiwara Exp $
#
# Submission: UTF8SMTP Submission daemon which supports downgrading.
#
# draft-ietf-eai-smtp-11.txt
# draft-ietf-eai-utf8headers-09.txt
# draft-ietf-eai-downgrade-06.txt
#
# Author: Kazunori Fujiwara
# Contact: <[email protected]>, <[email protected]>
#
# Copyright and License are at the end of this file.
# Documentation is avaiable by "perldoc"
require v5.8.8;
my $progname_path = $0;
BEGIN {
use lib '/home/eai/lib';
};
use Carp;
use strict;
use Socket;
use Getopt::Std;
use UTF8SMTP::CONFIG;
use UTF8SMTP::MIME;
use UTF8SMTP::SMTP;
use UTF8SMTP::Downgrading;
my %option = (
enable_eai => 0,
enable_8bit => 0,
enable_smtpauth => 0,
enable_startssl => 0,
debug => 0,
Myname => "localhost",
MyAuthDomain => "localhost",
LogCategory => "daemon",
workdir => "/tmp/",
);
if (!defined(%option = parse_config_file($progname_path, '', %option))) {
exit 1;
}
&set_datetime_tz($option{TimeZone}, $option{TimeZoneOffset});
if ($option{enable_starttls}) {
use IO::Socket::SSL; # qw(debug4);
}
if (defined($ENV{RELAYCLIENT})) {
$option{enable_smtpauth} = 0;
}
my $authen;
if ($option{enable_smtpauth}) {
use HTTPD::Authen;
use MIME::Base64;
my @textpasswd = (DBType => 'Text', DB => $option{Passwordfile}, Server => 'apache');
$authen = new HTTPD::Authen (@textpasswd);
}
my (@recipient);
my ($mailfrom);
my $socket = getpeername STDIN;
my $src;
my $filename;
my $srchost;
if (!defined($socket)) {
# debug;
$src = 'stdin [0.0.0.0]';
} else {
my ($port, $iaddr) = sockaddr_in($socket);
my $name = gethostbyaddr($iaddr, AF_INET);
my $ipaddr = inet_ntoa($iaddr);
$src = "$name [$ipaddr]";
}
$|=1;
&logmsg("request from $src");
my $intls = 0;
my $in = *STDIN;
my $out = *STDOUT;
my $authenticated = '';
my $reply;
&reply($out,"220 $option{Myname} ESMTP");
while($_ = &readline($in)) {
chomp;
chop if (/\r$/);
&logmsg("command: $_") if (!/^AUTH/i);
if (/^helo$/i || /^helo\s+(\S+)$/i) {
&reply($out, '250 '.$option{Myname});
$srchost = $1;
} elsif (/^ehlo$/i || /^ehlo\s+(\S+)$/i) {
$srchost = $1;
my @data;
push @data, "250-$option{Myname}";
push @data, "250-UTF8SMTP" if ($option{enable_eai});
push @data, "250-8BITMIME" if ($option{enable_8bit});
push @data, "250-STARTTLS" if ($option{enable_starttls} && $intls == 0);
push @data, "250-AUTH PLAIN" if ($option{enable_smtpauth});
push @data, "250 HELP";
&reply($out,@data);
} elsif (/^auth(|\s.*)$/i && $option{enable_smtpauth}) {
my $param = $1;
my $method = '';
if ($param =~ /^\s*(\S+)(|\s+(\S.*))$/) {
$method = $1;
$method =~ y/A-Z/a-z/;
$param = $3;
}
if ($method eq 'plain') {
if ($param eq '') {
&reply($out, "334");
last if (!($param = &readline($in)));
}
if ($authenticated = &smtpauth_check($param)) {
&reply($out, "235 Authentication succeed as $authenticated");
&logmsg("Authenticated as $authenticated");
} else {
&reply($out, "535 Authentication failed");
&logmsg("Authentication failed");
}
} else {
&reply($out, "504 Unsupported authentication mechanism");
}
} elsif (/^mail\s+from:\s*(\S+.*)\s*$/i) {
if ($option{enable_smtpauth} && $authenticated eq '') {
&reply($out, "530 Authentication required");
next;
}
if (!defined($mailfrom)) {
$mailfrom = &parse_mailfrom($1);
if ($authenticated ne '') {
my @param = $mailfrom->{param};
for (my $i = 0; $i <= $#param; $i++) {
if ($param[$i] =~ /^AUTH=/i) {
$param[$i] = '';
}
}
}
@recipient = ();
if (defined($mailfrom)) {
&reply($out, "250 ok");
} else {
&reply($out, "501 syntax error");
$mailfrom = undef;
}
} else {
&reply($out, "501 duplicate mail from");
}
} elsif (/^rcpt\s+to:\s*(\S+.*)\s*$/i) {
if (defined($mailfrom)) {
my $rcptto = &parse_mailfrom($1);
if (!defined($rcptto)) {
&reply($out ,"501 syntax error");
} else {
push @recipient, $rcptto;
&reply($out, "250 ok");
}
} else {
&reply($out, "501 MAIL FROM REQUIRED");
}
} elsif ($#recipient >= 0 && /^data$/i) {
&reply($out, "354 go ahead");
$filename = $option{workdir}.sprintf('/incoming-%d.%d', time. $$);
open(W, ">$filename") || logmsg_die "cannot write $filename";
while($_ = &readline($in)) {
chomp;
chop if (/\r$/);
last if ($_ eq ".");
print W $_, "\n";
}
close(W);
&logmsg_die("disconnected from client") if ($_ ne ".");
my $smtpfrom = "$srchost (".$src.($authenticated ne ''? " smtpauth=$authenticated":"").")";
my @error = &send_messages($filename, $option{Myname}, $smtpfrom, $mailfrom, @recipient);
unlink($filename);
my @msg;
if ($#error < 0) {
push @msg, "250 ok";
} else {
foreach my $l (@error) {
push @msg, "554-".$l;
logmsg($l);
}
push @msg, "554 Transaction failed";
}
$mailfrom = undef;
&reply($out, @msg);
} elsif (/^rset/i) {
@recipient = ();
$mailfrom = undef;
&reply($out, "250 ok");
} elsif (/^noop$/i) {
&reply($out, "250 ok");
} elsif (/^help$/i) {
&reply($out, "214 JPRS eai prototype page: http://eai.dnslab.jp/");
} elsif (/^quit/i) {
&reply($out, "221 $option{Myname}");
exit 0;
} elsif (/^STARTTLS$/i && $option{enable_starttls}) {
&reply($out,"220 Go ahead");
my $fd = IO::Socket::SSL->new_from_fd(fileno(STDIN),
SSL_server => 1,
Timeout => 120,
SSL_startHandshake => 1,
SSL_verify_mode => 0x00,
SSL_key_file => $option{ssl_key},
SSL_cert_file => $option{ssl_cert},
SSL_version => "SSLv23 TLSv1");
if ($fd) {
$in = $fd;
$out = $fd;
$intls = 1;
$src .= ' /TLS';
} else {
&logmsg("IO::Socket::SSL new_from_fd: ".IO::Socket::SSL::errstr());
&reply($out, "454 TLS not available due to temporary reason");
exit 0;
}
} else {
&reply($out, "501 syntax error: $_");
}
}
&logmsg("closed by client: $src");
exit 0;
sub reply
{
my ($fd, @str) = @_;
my $crlf = "\r\n";
my $str = join($crlf, @str).$crlf;
print $out $str;
&logmsg(join("\\r\\n", @str)) if ($option{debug} > 0);
}
sub smtpauth_check
{
my $param = shift;
my $fail = '';
chomp $param;
my $decoded = MIME::Base64::decode($param);
my @param = split(/\0/, $decoded);
return $fail if ($#param ne 2 || $param[1] eq '' || $param[2] eq '');
my ($user, $dom) = split(/\@/, $param[1]);
$dom =~ y/A-Z/a-z/;
if ($option{MyAuthDomain} eq $dom || $dom eq '') {
if ($authen->check($user, $param[2])) {
return $param[1];
}
}
return $fail;
}
=head1 NAME
Submission - UTF8SMTP Submission daemon which supports downgrading
=head1 DESCRIPTION
Submission is an implimentation of draft-ietf-eai-smtp,
draft-ietf-eai-utf8headers, draft-ietf-eai-downgrade.
Submission supports SMTP AUTH (PLAIN only) and STARTTLS.
Submission shares password database with Webmail client.
If SMTP AUTH is enabled, all submission request needs authenticated by
SMTP AUTH. But setting "RELAYCLIENT" environment value bypass SMTP AUTH.
=head1 USAGE
Submission is invoked by inetd.
Put "submission stream tcp nowait nobody /Path-To-/Submission" to
inetd.conf.
=head2 mail.conf
Write a configuration file and put it to "/Path-To-/mail.conf".
It contains colon sparated field name and field value.
The field name field is composed of the field name and the program name.
Two fields are separated by '@'.
Program name sub field may be omitted.
Don't include useless spaces or comments.
The line starts with '#' character is treated as comment line.
The configuration file is shared with SMTPreceiver, Submission,
POP3d, mail.cgi.
(default value)
enable_eai: responds UTF8SMTP or not (0)
enable_8bit: responds 8BITMIME or not (0)
MaildirPath: Maildir base directory (.)
LogCategory: syslog category (daemon)
workdir: Work directory (writable) (.)
Myname: EHLO name (localhost)
TimeZone: Timezone used in Received ()
TimeZoneOffset: Timezone offset (0)
enable_startssl: Enable STARTSSL (0)
enable_smtpauth: Enable SMTP AUTH PLAIN (0)
Passwordfile: Apache style passwd file (none)
ssl_cert: TLS Certificate file (none)
ssl_key: TLS Private Key file (none)
MyAuthDomain: Domainname used in SMTP AUTH (localhost)
Example:
enable_8bit: 1
enable_eai: 1
enable_smtpauth@Submission: 1
MaildirPath: /home/eai/maildirs
Passwordfile: /home/webmail/data/passwd
LogCategory: local0
ssl_cert@Submission: /home/eai/bin/cert.pem
ssl_key@Submission: /home/eai/bin/private.pem
TimeZone: +0900
TimeZoneOffset: 32400
=head1 Bugs
This program may have many problems.
If you find any problem, please contact to the author.
=head1 Author
Kazunori Fujiwara <[email protected]> <[email protected]>
=head1 Copyright and License
Copyright(c) 2007,2008 Japan Registry Services Co., Ltd.
All rights reserved. By using this file, you agree to the
terms and conditions set forth bellow.
LICENSE TERMS AND CONDITIONS
The following License Terms and Conditions apply, unless a different
license is obtained from Japan Registry Service Co., Ltd. ("JPRS"),
a Japanese corporation, Chiyoda First Bldg. East 13F,
3-8-1 Nishi-Kanda, Chiyoda-ku, Tokyo 101-0065, Japan.
1. Use, Modification and Redistribution (including distribution
of any modified or derived work) in source and/or binary forms
is permitted under this License Terms and Conditions.
2. Redistribution of source code must retain the copyright notices
as they appear in each source code file, this License Terms and
Conditions.
3. Redistribution in binary form must reproduce the Copyright
Notice, this License Terms and Conditions, in the documentation
and/or other materials provided with the distribution.
For the purposes of binary distribution the "Copyright Notice"
refers to the following language:
"Copyright(c) 2007,2008 Japan Registry Service Co., Ltd.
All rights reserved."
4. The name of JPRS may not be used to endorse or promote products
derived from this Software without specific prior written
approval of JPRS.
5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED
BY JPRS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL JPRS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGES.
=cut