Inspect/L7/SMTP.pm untested

filter: Statementstmt 0.0% 0 / 188 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 48 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 348 CC 40 SCAR 41.7
CC
40
Cov
0%
_in1_response
182.0
_in0_data
90.0
in
42.0
CRAP 64.7
1 ############################################################################
2 # finds SMTP traffic in tcp connection
3 ############################################################################
4 use strict;
5 use warnings;
6 package Net::Inspect::L7::SMTP;
7 use base 'Net::Inspect::Flow';
8 use Net::Inspect::Debug qw(:DEFAULT $DEBUG %TRACE);
9 use Hash::Util 'lock_ref_keys';
10 use Carp 'croak';
11 use Scalar::Util 'weaken';
12 use fields (
13 'replay', # collected and replayed in guess_protocol
14 'meta', # meta data from connection
15 'error', # connection has error like server sending data w/o request
16 'connid', # connection id
17 'offset', # offset in data stream
18 'handler', # handler sub for current state
19 'indata', # inside DATA section
20 'cmd', # list of open commands
21 );
22  
23  
24  
25 sub guess_protocol {
26 my ($self,$guess,$dir,$data,$eof,$time,$meta) = @_;
27  
28 if ($dir == 0) {
29 # data from client w/o greeting from server
30 debug("got data from client before getting greeting from server -> no SMTP");
31 $guess->detach($self);
32 return;
33 }
34  
35 my $rp = $self->{replay} ||= [];
36 push @$rp,[$data,$eof,$time];
37 my $buf = join('',map { $_->[0] } @$rp);
38  
39 my $eol = index($buf,"\n");
40 if ($eol == -1 && length($buf)>512 || $eol>512) {
41 # maximum length of reply line is 512 octets: RFC5321, 4.5.3.1.5.
42 debug("line to long -> no SMTP");
43 $guess->detach($self);
44 return;
45 } elsif ($eol == -1) {
46 # need more bytes
47 return;
48 }
49  
50 if ($buf !~ m{\A220[ -]}) {
51 debug("not an SMTP greeting -> no SMTP");
52 $guess->detach($self);
53 return;
54 }
55  
56 # looks like SMTP greeting
57 my $obj = $self->new_connection($meta);
58 # replay as one piece
59 my $n = $obj->in(1,$buf,$rp->[-1][1],$rp->[-1][2]);
60 undef $self->{replay};
61 $n += -length($buf) + length($data);
62 $n<=0 and die "object $obj did not consume alle replayed bytes";
63 debug("consumed $n of ".length($data)." bytes");
64 return ($obj,$n);
65 }
66  
67  
68 {
69 my $connid = 0;
70 sub syn { 1 }; # in case it is attached to Net::Inspect::Tcp
71 sub new_connection {
72 my ($self,$meta,@args) = @_;
73 my $obj = $self->new(@args);
74 $obj->{upper_flow} = $obj->{upper_flow}->new_connection($meta)
75 or return;
76 $obj->{meta} = $meta;
77 $obj->{connid} = ++$connid;
78 $obj->{offset} = [0,0];
79 $obj->{handler} = [ \&_in0_command, \&_in1_response ];
80 $obj->{cmd} = [ ':EXPECT-GREETING' ];
81 return $obj;
82 }
83 }
84  
85 sub in {
86 my ($self,$dir,$data,$eof,$time) = @_;
87 return 0 if $data eq '' && ! $eof;
88 defined($self->{error}) and return;
89  
90 $DEBUG && $self->xdebug("got %s bytes from %d, eof=%d",
91 ref($data) ? join(":",@$data): length($data),
92 $dir,$eof//0
93 );
94 my $bytes = 0;
95 while (1) {
96 my $sub = $self->{handler}[$dir];
97 my @arg;
98 ($sub,@arg) = @$sub if ref($sub) ne 'CODE';
99 my $n = $sub->($self,@arg,$data,$eof,$time) or last;
100 $bytes += $n;
101 substr($data,0,$n,'');
102 last if $data eq '';
103 }
104 if ($DEBUG && $bytes < length($data)) {
105 debug("unprocessed[%d]: %d/'%s'", $dir,
106 length($data)-$bytes,substr($data,$bytes));
107 }
108 return $bytes;
109 }
110  
111 sub offset {
112 my $self = shift;
113 return @{ $self->{offset} }[wantarray ? @_:$_[0]];
114 }
115  
116  
117 sub _in1_response {
118 my ($self,$data,$eof,$time) = @_;
119 my ($code,$eom);
120 while ($data =~m{\G([2345]\d\d)(?:\n|(-).*\n|\s.*\n)}gc) {
121 return $self->fatal('SMTP response line too long',0,$time)
122 if $+[0]-$-[0]>1024;
123  
124 if (!defined $code) {
125 $code = $1;
126 } elsif ($code != $1) {
127 return $self->fatal(
128 'mixed status in multiline SMTP response',0,$time);
129 }
130 if (!$2) {
131 $eom = pos($data);
132 last;
133 }
134 }
135  
136 return $self->fatal('SMTP response line too long',0,$time)
137 if length($data)-(pos($data)//0) > 1024;
138 return if !$eom;
139  
140 $self->{offset}[1] += $eom;
141  
142 my $cmd = pop @{$self->{cmd}};
143 return $self->fatal('SMTP response w/o command',0,$time)
144 if !defined $cmd;
145  
146 if ($cmd eq ':EXPECT-GREETING') {
147 $self->{upper_flow}->greeting(substr($data,0,$eom),$time);
148 } else {
149 my $resp = substr($data,0,$eom);
150 if ($code =~m{^3}) {
151 if ($cmd eq 'DATA') {
152 unshift @{$self->{cmd}}, \'DATA';
153 $self->{handler}[0] = \&_in0_data;
154 $self->{upper_flow}->response($resp,$time);
155 } elsif ($cmd eq 'AUTH' || ref($cmd) && $$cmd eq 'AUTH') {
156 unshift @{$self->{cmd}}, \'AUTH';
157 $self->{upper_flow}->response($resp,$time);
158 $self->{handler}[0] = \&_in0_auth;
159 } else {
160 return $self->fatal("$code response for $cmd",0,$time);
161 }
162 } else {
163 $self->{handler}[0] = \&_in0_command if ref $cmd;
164 $self->{upper_flow}->response($resp,$time);
165 }
166 }
167 return $eom;
168  
169  
170 # TODO
171 # check response to EHLO, i.e. allowed version used features
172 }
173  
174  
175 sub _in0_command {
176 my ($self,$data,$eof,$time) = @_;
177 $data =~m{^(\w+)(?:[ \t].*|\r|)\n}gc or do {
178 return $self->fatal("invalid SMTP command '$data'",0,$time)
179 if length($data)>1024 || $data =~m{\n};
180 return; # need more data
181 };
182  
183 my $cmd = uc($1);
184 $data = substr($data,0,pos($data));
185 $self->{offset}[1] += length($data);
186  
187 if ($cmd eq 'BDAT') {
188 my ($offset,$last) = $data =~m{^BDAT\s+(\d+)(\s+LAST)?\s+\z}i
189 or return $self->fatal("invalid BDAT syntax '$data'",0,$time);
190 $self->{handler}[0] = [ \&_in0_bdat, \$offset, $last ];
191 }
192 push @{$self->{cmd}}, $cmd;
193 $self->{upper_flow}->command($data);
194  
195 return length($data);
196 }
197  
198 sub _in0_data {
199 my ($self,$data,$eof,$time) = @_;
200 my $rx = $self->{indata}++ ? qr{^\.}m : qr{\n\.};
201  
202 return $self->fatal('no data in DATA') if $data eq '';
203 my $rxpre = $self->{indata}++ ? qr{(^)\.}m : qr{(\n)\.};
204 my $eom;
205 if ($data =~m{$rxpre\r?\n}gc) {
206 substr($data,pos($data)) = '';
207 $eom = 1;
208 } elsif ((my $pos = rindex(substr($data,-4),"\n")) != -1) {
209 substr($data,-4+$pos) = '';
210 }
211  
212 my $len = length($data);
213 if ($len) {
214 $data =~s{$rxpre}{$1}g;
215 $self->{upper_flow}->mail_data($data,$time) if $data ne '';
216 if ($eom) {
217 $self->{upper_flow}->mail_data('',$time);
218 $self->{handler}[0] = \&_in0_command;
219 }
220 }
221 return $len;
222 }
223  
224 sub _in0_bdat {
225 my ($self,$roffset,$last,$data,$eof,$time) = @_;
226 my $len = length($data);
227 if ($len <= $$roffset) {
228 $$roffset -= $len;
229 } else {
230 $len -= $$roffset;
231 $$roffset = 0;
232 substr($data,0,$len,'');
233 }
234  
235 $self->{upper_flow}->mail_data($data,$time) if $data ne '';
236 if ($$roffset == 0) {
237 $self->{upper_flow}->mail_data('',$time) if $last;
238 $self->{handler}[0] = \&_in0_command;
239 }
240 return $len;
241 }
242  
243 sub _in0_auth {
244 my ($self,$data,$eof,$time) = @_;
245 my $pos = index($data,"\n");
246 substr($data,$pos+1) = '' if $pos>=0;
247 return $self->fatal('SMTP AUTH response too long',0,$time)
248 if length($data)>1024;
249 return if $pos == -1;
250 $self->{upper_flow}->auth_data($data,$time);
251 return $pos+1;
252 }
253  
254 sub fatal {
255 my ($self,$reason,$dir,$time) = @_;
256 %TRACE && $self->xtrace($reason);
257 $self->{error} = $reason;
258 $self->{upper_flow}->fatal($dir,$reason,$time);
259 return;
260 }
261  
262 sub xtrace {
263 my $self = shift;
264 my $msg = shift;
265 $msg = "$$.$self->{connid} $msg";
266 unshift @_,$msg;
267 goto &trace;
268 }
269  
270 sub xdebug {
271 $DEBUG or return;
272 my $self = shift;
273 my $msg = shift;
274 $msg = "$$.$self->{connid} $msg";
275 unshift @_,$msg;
276 goto &debug;
277 }
278  
279  
280 1;
281  
282 __END__