-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDowngrade
executable file
·257 lines (227 loc) · 6.95 KB
/
Downgrade
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
#!/usr/bin/perl
# Downgrade: UTF8SMTP Downgrading/displaying downgraded message
#
# draft-ietf-eai-downgrade-06.txt
# draft-fujiwara-eai-downgraded-display-00.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 5.8.8;
BEGIN {
use lib '/home/eai/lib';
};
use strict;
use Getopt::Std;
use UTF8SMTP::MIME;
use UTF8SMTP::SMTP;
use UTF8SMTP::Downgrading;
my @error;
my (%opts);
##############################################################################
sub do_folding_only
{
my ($fh, $fhout, $fherr, $mode) = @_;
my ($input, $orig, $l);
my ($fh, $fhout) = @_;
&downgrading_init();
getheaderfromfile($fh, 2);
while(($orig = getheaderfromfile($fh, 0)) ne '') {
last if ($orig eq '');
$input = &unfolding($orig);
if ($mode) {
print $fhout &folding($input), "\n";
} else {
print $fhout $input, "\n";
}
}
print $fhout "\n";
while(<$fh>) {
print $fhout $_;
};
if ($#error ne 0) {
foreach my $l (@error) {
print $fherr "ERROR: ", $l, "\n";
}
}
}
###############################################################################
#
# Main
#
###############################################################################
sub usage
{
print
"$0 -[DE7T012FUs] [-m B|Q|A] [-f envelope_from] [-t envelope_to] [-d Number]
-D: Downgrade
-E: Downgrade with Envelope information (extended syntax)
-7: Downgrade with 7bit transport (without 8BITMIME)
-T: Test if it contains non_ascii character in header fields
-0: MIME decoding
-1: Displaying Technique 1
-2: Displaying technique 2
-F: Folding
-U: Unfolding
-m: Mime encode mode (B, Q, A) A=Auto
-6: Decode base64 encoded message or text
-s: silent
-q: Supress envelope information
-d: debug level
perldoc $0 shows a document of this program.
";
exit 0;
}
getopts('6sqDE7T012FUNf:t:m:d:', \%opts);
&main;
sub main
{
my $flag;
my $in;
my (@envelope, @downgraded);
my ($state, $body8bit, @message, $errorref);
my $option = 0;
if ($#ARGV >= 0 && -f $ARGV[0]) {
open($in, $ARGV[0]) || die "cannot open $ARGV[0]";
} else {
$in = *STDIN;
}
if ($opts{6}) {
$option |= &decoding_decode_base64;
}
if ($opts{N}) {
$option |= &decoding_nofolding;
}
if (defined($opts{m})) {
if ($opts{m} =~ /^[BQA]$/) {
&set_mime_encoding($opts{m});
} else {
&usage;
}
}
if ($opts{E}) {
while(<$in>) {
chomp;
next if (/^#/ || /^$/);
if (/^DATA(|[ \t]+.*)$/i) {
last;
} elsif (/^(Mail[ \t]+from|Rcpt[ \t]+To):[ \t]*([^ \t]+.*)$/i) {
my ($name, $value) = ($1, $2);
my $addr = &parse_mailfrom($value);
my ($e, $d) = &smtp_envelope_generate($name, $addr, 1);
push @downgraded, $d if ($d ne '');
push @envelope, $e;
} else {
print STDERR "UnknownCommand: $_\n";
}
}
}
if (defined($opts{f})) {
my $addr = &parse_mailfrom($opts{f});
my ($e, $d) = &smtp_envelope_generate('Mail From', $addr, 1);
push @downgraded, $d if ($d ne '');
push @envelope, $e;
}
if (defined($opts{t})) {
my $addr = &parse_mailfrom($opts{t});
my ($e, $d) = &smtp_envelope_generate('Rcpt To', $addr, 1);
push @downgraded, $d if ($d ne '');
push @envelope, $e;
}
if ($opts{D}||$opts{E}||$opts{7}) {
if (!$opts{q} && $#envelope >= 0) {
for my $l (@envelope) {
print $l, "\n";
}
for my $l (@downgraded) {
print $l;
}
}
($state, $body8bit, $errorref, @message) =
&downgrading($in, $opts{7}?&downgrading_7bit:&downgrading_8bit);
} elsif ($opts{0}) {
($state, $errorref, @message) = &decoding_downgraded_message($in,&decoding_mime | $option);
} elsif ($opts{1}) {
($state, $errorref, @message) = &decoding_downgraded_message($in,&decoding_technique_1 | $option);
} elsif ($opts{2}) {
($state, $errorref, @message) = &decoding_downgraded_message($in,&decoding_technique_2 | $option);
} else {
&usage;
}
foreach my $l (@message) {
print $l;
}
foreach my $l (@$errorref) {
print "ERROR: $l\n";
}
exit 0;
}
=head1 NAME
Downgrade - UTF8SMTP Downgrade/Downgraded Display
=head1 DESCRIPTION
Downgrade is an implimentation of draft-ietf-eai-downgrade,
draft-ietf-eai-downgraded-display.
=head1 USAGE
Downgrade -[DE7T012FUsN] [-m B|Q|A] [-f envelope_from] [-t envelope_to] [-d Number]
-D: Downgrade
-E: Downgrade with Envelope information (extended syntax)
-7: Downgrade with 7bit transport (without 8BITMIME)
-T: Test if it contains non_ascii character in header fields
-0: MIME decoding
-1: Displaying Technique 1
-2: Displaying technique 2
-F: Folding
-U: Unfolding
-N: No folding output
-m: Mime encode mode (B, Q, A) A=Auto
-s: silent
-q: Supress envelope information
-d: debug level
TBD
=head1 Bugs
This program may have many problems.
If you find any problem, please contact to the author.
Non-ASCII check is achived by /[^000-177]/.
=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