blib/lib/Mail/SMTP/Honeypot.pm

filter: Statementstmt 52.2% 282 / 540 Branchbran 36.6% 121 / 330 Conditioncond 20.0% 21 / 105 MC/DCmcdc 12.2% 7 / 57 Subroutinesub 63.3% 38 / 60 Podpod 5.4% 2 / 37 totaltotal 41.7% 471 / 1129 CC 214 SCAR 52.2
CC
214
Cov
43%
daemon
756.0
dns_rcv
600.0
check_config
312.0
CRAP 184.2
↑ Summary recurse2txt »
1 #!/usr/bin/perl
2 package Mail::SMTP::Honeypot;
3  
4 # Notes to curious readers:
5 #
6 # This module was cobbled together in a couple of days out of another
7 # project that is neatly partitioned into pieces that have a good
8 # organization. Everything from there was pretty much dumped in this one
9 # file. Sorry 'bout that ;-)
10 #
11 # I was a lot more interested in having it work quickly than making it neat.
12 #
13 # Michael
14 #
15 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 #use diagnostics;
17 #use lib qw(blib lib);
18  
19 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Net::DNS::Codes qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
21 T_PTR
22 C_IN
23 BITS_QUERY
24 RD
25 NS_PACKETSZ
26 HFIXEDSZ
27 QUERY
28 NOERROR
29 NXDOMAIN
30 SERVFAIL
31 7 );
32 7 ▶use Net::NBsocket qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
33 open_udpNB
34 open_listenNB
35 accept_NB
36 inet_aton
37 inet_ntoa
38 sockaddr_in
39 set_so_linger
40 7 );
41 7 ▶use Net::DNS::ToolKit qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
42 gethead
43 newhead
44 get_ns
45 7 );
46 #use Net::DNS::ToolKit::Debug qw(
47 # print_head
48 # print_buf
49 #);
50 7 ▶use Net::DNS::ToolKit::RR;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 7 ▶use POSIX qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
52 EINTR
53 EWOULDBLOCK
54 7 );
55 7 ▶use Proc::PidUtil qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
56 if_run_exit
57 is_running
58 get_script_name
59 make_pidfile
60 zap_pidfile
61 7 );
62 7 ▶use Sys::Hostname::FQDN qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
63 fqdn
64 7 );
65 7 ▶use Unix::Syslog qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
66 :macros
67 openlog
68 syslog
69 7 );
70 7 ▶use vars qw($VERSION @EXPORT @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
71 7 require Exporter;
72  
73 7 @ISA = qw(Exporter);
74  
75 7 $VERSION = do { my @r = (q$Revision: 0.11 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
76  
77 7 @EXPORT = qw(
78 run_honeypot
79 );
80  
81  
82 # private file scoped variables
83  
84 7 my($me,$threads,$dns,$dnshost,$dnsport,$dnsaddr,$deny,$hostname,$laddr,
85 $port,$delay,$config,$syslog,$verbose,$DNStimeout,$maxthreads,$maxcmds,
86 $LOG,$DNSfileno,$disconnect,%Commands,$unique,$log_facility,%subref
87 );
88 7 my $CRLF = "\r\n";
89  
90 7 my @IDarray = ('a'..'z','A'..'Z',(0..9));
91  
92 =head1 NAME
93  
94 Mail::SMTP::Honeypot -- Dummy mail server
95  
96 =head1 SYNOPSIS
97  
98 use Mail::SMTP::Honeypot;
99  
100 run_honeypot($config)
101  
102 =head1 DESCRIPTION
103  
104 B<Mail::SMTP::Honeypot> is a perl module that appears to provide all the
105 functionality of a standard SMTP server except that when the targeted
106 command state is detected (default DATA), it terminates the connection with
107 a temporary failure and the response:
108  
109 421 Service not available, closing transmission channel
110  
111 The purpose of this module is to provide a spam sink on a tertiary MX host.
112 The module daemon is run on an MX host with a very high priority number
113 specified in it's DNS record. i.e.
114  
115 some_mail_domain.com IN MX 9999 lastmx.servicedomain.com.
116  
117 Since many spammers target this mail server in the hope that its
118 configuration and/or security is not as strong or well maintained as the
119 primary mail host for a domain. In the off chance that a real message is
120 sent to the server, the TEMPORARY failure code will simply make the sending
121 host retry later -- probably with the lower priority numbered host.
122 Meanwhile, the server target by the spam source has its resources consumed
123 by B<honeypot>.
124  
125 Honeypot does not spawn children and holds only a small reference to each
126 thread that it holds to a client, thus consuming minimal resources. It can
127 produce logs useful in analyzing the spam traffic to your site. Using it
128 with a detach in CONN mode is adequate for triggering a companion spam
129 program such as Mail::SpamCannibal while consuming minimum host resources.
130 At our site, we simply run B<honeypot> on the same host as our secondary MX
131 but on a different IP address.
132  
133 Honeypot provides various levels of connection and transaction logging that
134 can be set in the configuration.
135  
136 A delay may be inserted between the receipt of each command and the response
137 from the server daemon to slow down the sending client.
138  
139 =head1 CONFIGURATION
140  
141 Edit the B<rc.honeypot.pl> file to change or set the following:
142  
143 my $config = {
144  
145 # specify the directory for the pid file for this daemon
146 # [required]
147 #
148 piddir => '/var/run',
149  
150 # deny at command state, one of:
151 # CONN EHLO HELO MAIL RCPT DATA
152 # defaults to DATA if not specified
153 # [optional]
154 # deny => 'DATA',
155  
156  
157 # specify the local domain name, defaults to local hostname.
158 # this is probably not what you want if you use virtual IP's
159 # and have a real mail client on the same host. so...
160 # specify the host 'answerback name' here.
161 # [optional]
162 #
163 # hostname => 'my.host.name.com',
164  
165 # specify the IP address to bind the listening port
166 # defaults to ALL interfaces (INADDR_ANY)
167 # [optional]
168 #
169 # ip_address => '1.2.3.4',
170  
171 # listen port -- default 25
172 # this is useful for debugging purposes
173 # [optional]
174 #
175 # port => 25,
176  
177 ## NOTE: see Concurrent Daemon Operation in the
178 ## documentation for setup where another
179 ## mail daemon is running on the same host.
180
181 # specify the response delay after connect or upon
182 # receipt of an smtp command from the client
183 #
184 # NOTE: if a response is not received
185 # from the client in this time
186 # period, the smptdeny daemon will
187 # issue a 421 response and disconnect
188 # [optional] default 10 seconds
189 #
190 # delay => 10,
191  
192 # syslog facility, one of:
193 # LOG_KERN LOG_USER LOG_MAIL LOG_DAEMON
194 # LOG_AUTH LOG_SYSLOG LOG_LPR LOG_NEWS
195 # LOG_UUCP LOG_CRON LOG_AUTHPRIV LOG_FTP
196 # LOG_LOCAL0 LOG_LOCAL1 LOG_LOCAL2 LOG_LOCAL3
197 # LOG_LOCAL4 LOG_LOCAL5 LOG_LOCAL6 LOG_LOCAL7
198 #
199 # You should not need to change this
200 #
201 # log_facility => 'LOG_MAIL',
202  
203 # syslog log level or (none), one of:
204 # STDERR LOG_EMERG LOG_ALERT LOG_CRIT LOG_ERR
205 # LOG_WARNING LOG_NOTICE LOG_INFO LOG_DEBUG
206 #
207 # NOTE: the command line -d flag overrides
208 # this and sets the level to STDERR
209 # [optional]
210 #
211 syslog => 'LOG_WARNING',
212  
213 # log verbosity
214 # 0 connect only
215 # 1 + To: & From:
216 # 2 + bad commands
217 # 3 + trace execution
218 # 4 + deep trace with sub names
219 # [optional]
220 #
221 verbose => 0,
222  
223 # DNS host, if you do not have a resolver
224 # on your host or for debugging
225 # default: as returned by your resolver for local dns
226 # [optional]
227 # dnshost => 'use.default',
228  
229 # DNS port, useful for debugging
230 # [optional] default 53
231 #
232 # dnsport => 53,
233  
234 # timeout for DNS PTR queries
235 # [optional] default: use 'delay' above
236 #
237 # DNStimeout => 10,
238  
239 # maximum number of connected clients
240 # [optional] default 100
241 #
242 # maxthreads => 100,
243  
244 # maximum number of commands per client
245 # [optional] default 100
246 #
247 # maxcmds => 100,
248  
249 # disconnect the remote after this much time
250 # [optional] default 300 seconds
251 #
252 # disconnect => 300,
253  
254 };
255  
256 =head1 OPERATION
257  
258 Launch the daemon with the command:
259  
260 rc.honeypot.pl [-d] [start | stop | restart]
261  
262 The '-d' flag, this overides the config settings and
263 reports logging to STDERR
264  
265 On some systems it may be necessary to wrap a shell script around
266 rc.honeypot.pl if the path for perl is not in scope during boot.
267  
268 #!/bin/sh
269 #
270 # shell script 'rc.honeypot'
271 #
272 /path/to/rc.honeypot.pl $*
273  
274 A sample shell script is included in the distribution as B<rc.honeypot>
275  
276 NOTE: suggest you test your configuration as follows...
277  
278 Set: verbose => 3,
279 delay => 5,
280  
281 ./rc.honeypot -d start
282  
283 Connect to the daemon from a host not on the same subnet and watch the
284 output from daemon to verify proper operation.
285  
286 Correct the configuration values and ENJOY!
287  
288 =head2 Standalone Operation
289  
290 For operation on a host where B<Mail::SMTP::Honeypot> is the only SMTP
291 daemon, the default configuration will work for most installations.
292  
293 =head2 Concurrent Daemon Operation
294  
295 To operate B<Mail::SMTP::Honeypot> concurrently with another mail daemon on
296 the same host you must do the following:
297  
298 =item B<1)> add a virtual IP address for the daemon to answer.
299 The IP address in the rc.honeypot.pl config section should be left
300 commented out so that the daemon will bind to INADDR_ANY.
301  
302 In your startup sequence, execute the following: (example for Linux)
303  
304 #/bin/sh
305 #
306 # Edit for your setup.
307 NETMASK="255.255.255.0" # REPLACE with YOUR netmask!
308 NETWORK="5.6.7.0" # REPLACE with YOUR network address!
309 BROADCAST="5.6.7.255" # REPLACE with YOUR broadcast address
310 # assign a virtual IP address
311 IPADDR="5.6.7.8"
312  
313 # assign ethernet device
314 DEVICE="eth0" # REPLACE with your external device
315 LUN="0"
316  
317 # Note: the "real" IP address has no LUN
318 # virtual IP's are assigned LUN's starting with '0'
319 #
320 # i.e. host IP = 5.6.7.1 eth0
321 # virtIP 5.6.7.8 LUN 0 eth0:0
322 # virtIP 5.6.7.9 LUN 1 eth0:1
323  
324 IFACE=${DEVICE}:${LUN}
325 /sbin/ifconfig ${IFACE} ${IPADDR} broadcast ${BROADCAST} netmask ${NETMASK}
326 /sbin/route add ${IPADDR} dev ${IFACE}
327 echo Configuring $IFACE as $IPADDR
328  
329 =item B<2)> run the honeypot daemon on an unused port.
330  
331 Select a high port number that will not interfere with normail operation of
332 the host SMTP daemon or other services on the host.
333  
334 i.e. in the config section of rc.honeypot.pl
335  
336 port => 10025,
337  
338 =item B<3)> add packet filter rules to redirect queries.
339  
340 This example is for IPTABLES on Linux. Similar rules would apply for other
341 filter packages.
342  
343 # allowed chain for TCP connections
344 iptables -N allowed
345 iptables -A allowed -p tcp --syn -j ACCEPT
346 iptables -A allowed -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
347 iptables -A allowed -p tcp -j DROP
348  
349 # drop all external packets target on honeypot daemon
350 iptables -t nat -A PREROUTING -p tcp -s 0/0 --dport 10025 -j DROP
351 iptables -t nat -A PREROUTING -p tcp -d 5.6.7.8 --dport 25 -j REDIRECT --to-port 10025
352 # alternate DNAT statement
353 # iptables -t nat -a PREROUTING -p tcp -d 5.6.7.8 --dport 25 -j DNAT --to 5.6.7.8:10025
354  
355 ## if you are running SpamCannibal, add this rule to capture IP's of connecting hosts
356 ## iptables -A INPUT -p tcp -i eth0 --dport 10025 -j QUEUE
357  
358 # allow the internal port to connect
359 iptables -A INPUT -p tcp -s 0/0 --dport 10025 -j allowed
360  
361 =head1 EXPORTS
362  
363 Only one function is exported by Honeypot.pm. This function is called in the
364 rc.honeypot.pl.sample script to launch the B<honeypot> daemon.
365  
366 =over 4
367  
368 =item * run_honeypot($config); # with @ARGV
369  
370 Launch the honeypot daemon.
371  
372 input: config hash
373 returns: nothing (exits)
374  
375 =back
376  
377 =cut
378  
379 0 ▶sub run_honeypot {
Subroutine run_honeypot: not called CC=3 SCAR=24.8
Pod documented
380 0 ▶ die "arg 1 of run_honeypot must be a hash\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 0 0
381 unless ref $_[0] eq 'HASH';
382 0 my $pidfile = &check_run; # check for another running daemon
383 0 &check_config; # verify that config array is correct and populated
384 0 clean_child(); # double fork a child
385 0 ▶ make_pidfile($pidfile,$$)
Branch: 1 branch
BranchTrueFalseTotal
make_pidfile($pidfile, $$) or die "could not make pidfile '${pidfile}' for ${$}\n" 0 0 0
386 or die "could not make pidfile '$pidfile' for $$\n";
387 0 init_all();
388 0 syslog_config();
389 0 goto &daemon;
390 }
391  
392 0 ▶sub usage { # tested by hand
Subroutine usage: not called CC=3 SCAR=24.8
Pod undocumented
393 0 ▶ $me = $0 unless $me;
Branch: 1 branch
BranchTrueFalseTotal
unless $me 0 0 0
394 0 ▶ print STDERR $_[0],"\n" if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 0 0
395 0 print STDERR qq|
396 Syntax: $me start
397 $me stop
398 $me restart
399 $me status
400  
401 -d switch may be added to
402 redirect logging to STDERR
403  
404 |;
405 0 exit 1;
406 }
407  
408 0 ▶sub kill_job {
Subroutine kill_job: not called CC=2 SCAR=17.9
Pod undocumented
409 0 my($pidfile) = @_;
410 0 my $pid = is_running($pidfile);
411 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
412 0 kill 15, $pid;
413 0 $pid = 0;
414 0 sleep 1;
415 } else {
416 0 print STDERR "$me: not running\n";
417 }
418 }
419  
420 # return true on good number
421 7 ▶sub vld_num { # t => vld_num.t
Subroutine vld_num: called CC=2 SCAR=6.9
Pod undocumented
422 7 my $num = shift;
423 7 ▶ return $num =~ /\d/ && $num !~ /[\D\s]/;
Condition100%3/3
$num =~ /\d/ && !($num =~ /[\D\s]/)
!ll&&!rl&&r
322
MC/DC100%2/2
$num =~ /\d/ && !($num =~ /[\D\s]/)
$num =~ /\d/ !($num =~ /[\D\s]/)
Truth table100%3/3
A && not(B)
A= $num =~ /\d/B= !($num =~ /[\D\s]/)
ABresult
0X0
100
111
424 }
425  
426 0 ▶sub bad_config {
Subroutine bad_config: not called CC=1 SCAR=6.9
Pod undocumented
427 0 my($msg) = @_;
428 0 print STDERR 'config: ',$msg,"\n";
429 0 exit 1;
430 }
431  
432 #=item * $sub_name = who();
433 #
434 #Returns the name of the calling subroutine without the package name.
435 #
436 #=cut
437  
438 2 ▶sub who {
Subroutine who: called CC=1 SCAR=0.0
Pod undocumented
439 2 (caller(1))[3] =~ /[^:]+$/;
440 2 return $& . ': ';
441 }
442  
443 # returns $pidfile if successful or exits
444 #
445 0 ▶sub check_run {
Subroutine check_run: not called CC=14 SCAR=53.5
Pod undocumented
446 0 my($c) = @_;
447 0 ▶ unless ($c->{piddir} && -d $c->{piddir} && -w $c->{piddir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->{"piddir"} and -d $c->{"piddir"} and -w $c->{"piddir"}) 0 0 0
Condition0%0/6
$c->{"piddir"} and -d $c->{"piddir"} and -w $c->{"piddir"}
!ll&&!rl&&r
000
$c->{"piddir"} and -d $c->{"piddir"}
!ll&&!rl&&r
000
MC/DC0%0/3
$c->{"piddir"} and -d $c->{"piddir"} and -w $c->{"piddir"}
$c->{"piddir"} -d $c->{"piddir"} -w $c->{"piddir"}
Truth table0%0/4
A and B and C
A= $c->{"piddir"}B= -d $c->{"piddir"}C= -w $c->{"piddir"}
ABCresult
0XX0
10X0
1100
1111
448 0 print STDERR "pid directory not specified or not writable\n";
449 0 exit 0;
450 }
451 0 $me = get_script_name();
452 0 $0 = $me;
453
454 0 ▶ usage('missing command argument(s)') if
Branch: 1 branch
BranchTrueFalseTotal
if grep(($_ eq "-d"), @ARGV) and @ARGV < 2 or @ARGV < 1 0 0 0
Condition0%0/6
grep(($_ eq "-d"), @ARGV) and @ARGV < 2 or @ARGV < 1
l!l&&r!l&&!r
000
grep(($_ eq "-d"), @ARGV) and @ARGV < 2
!ll&&!rl&&r
000
MC/DC0%0/3
grep(($_ eq "-d"), @ARGV) and @ARGV < 2 or @ARGV < 1
grep(($_ eq "-d"), @ARGV) @ARGV < 2 @ARGV < 1
Truth table0%0/5
A and B or C
A= grep(($_ eq "-d"), @ARGV)B= @ARGV < 2C= @ARGV < 1
ABCresult
11X1
0X11
1011
0X00
1000
455 (grep($_ eq '-d',@ARGV) && @ARGV < 2) ||
456 @ARGV < 1;
457 0 my $pidfile = $c->{piddir} .'/'. $me . '.pid';
458 0 foreach(@ARGV) {
459 0 ▶ if ($_ eq 'start') {
Branch: 5 branches
BranchTrueFalseTotal
if ($_ eq 'start') { } 0 0 0
elsif ($_ eq 'stop') { } 0 0 0
elsif ($_ eq 'restart') { } 0 0 0
elsif ($_ eq 'status') { } 0 0 0
elsif ($_ eq '-d') { } 0 0 0
460 0 if_run_exit($c->{piddir},"already running\n");
461 0 last;
462 }
463 elsif ($_ eq 'stop') {
464 0 kill_job($pidfile);
465 0 exit 0;
466 }
467 elsif ($_ eq 'restart') {
468 0 kill_job($pidfile);
469 0 last;
470 }
471 elsif ($_ eq 'status') {
472 0 my $pid = is_running($pidfile);
473 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
474 0 print STDERR "$pid $me is running\n";
475 } else {
476 0 print STDERR "$me not running\n";
477 }
478 0 last;
479 }
480 elsif ($_ eq '-d') {
481 0 $c->{syslog} = 'STDERR';
482 }
483 else {
484 0 usage("unknown command argument '$_'\n");
485 }
486 }
487  
488 0 return $pidfile;
489 }
490  
491 ### validate and set configuration defaults
492 #
493 4 ▶sub check_config {
Subroutine check_config: called CC=34 SCAR=57.4
Pod undocumented
494 # ip-address
495 4 my($c) = @_;;
496 4 ▶ if ($c->{ip_address}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'ip_address'}) { } 0 4 4
497 bad_config("bad IP address '$c->{ip_address}'")
498 0 ▶ unless $laddr = inet_aton($c->{ip_address});
Branch: 1 branch
BranchTrueFalseTotal
unless $laddr = inet_aton($c->{"ip_address"}) 0 0 0
499 } else {
500 4 $laddr = Socket::INADDR_ANY;
501 }
502 # port number
503 4 ▶ if ($port = $c->{port}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port = $c->{'port'}) { } 0 4 4
504 0 ▶ bad_config("bad port number '$port'")
Branch: 1 branch
BranchTrueFalseTotal
unless vld_num($port) 0 0 0
505 unless vld_num($port);
506 } else {
507 4 $port = 25;
508 }
509 # delay
510 4 ▶ if ($delay = $c->{delay}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delay = $c->{'delay'}) { } 0 4 4
511 0 ▶ bad_config("invalid delay '$delay'")
Branch: 1 branch
BranchTrueFalseTotal
unless vld_num($delay) 0 0 0
512 unless vld_num($delay);
513 } else {
514 4 $delay = 10;
515 }
516 # deny
517 4 ▶ if ($deny = $c->{deny}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deny = $c->{'deny'}) { } 0 4 4
518 0 $deny = uc $deny;
519 0 ▶ bad_config("invalid deny state '$deny'")
Branch: 1 branch
BranchTrueFalseTotal
unless $deny =~ /^(?:CONN|EHLO|HELO|MAIL|RCPT|DATA)$/ 0 0 0
520 unless $deny =~ /^(?:CONN|EHLO|HELO|MAIL|RCPT|DATA)$/;
521 0 ▶ $deny = 'HELO|EHLO' if $deny =~ /HELO|EHLO/;
Branch: 1 branch
BranchTrueFalseTotal
if $deny =~ /HELO|EHLO/ 0 0 0
522 } else {
523 4 $deny = 'DATA';
524 }
525 # hostname
526 4 ▶ $hostname = $c->{hostname} || fqdn();
Condition66%2/3
$c->{'hostname'} || fqdn()
l!l&&r!l&&!r
220
MC/DC0%0/2
$c->{'hostname'} || fqdn()
$c->{'hostname'} fqdn()
Truth table66%2/3
A || B
A= $c->{'hostname'}B= fqdn()
ABresult
1X1
011
000
527 # syslog
528 4 ▶ if ($log_facility = $c->{log_facility}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log_facility = $c->{'log_facility'}) { } 0 4 4
529 0 $log_facility = uc $log_facility;
530 0 ▶ bad_config("invalid log facility '$log_facility'")
Branch: 1 branch
BranchTrueFalseTotal
unless $log_facility =~ /^(?:LOG_KERN|LOG_USER|LOG_MAIL|LOG_DAEMON|LOG_AUTH|LOG_SYSLOG|LOG_LPR|LOG_NEWS|LOG_UUCP|LOG_CRON|LOG_AUTHPRIV|LOG_FTP|LOG_LOCAL0|LOG_LOCAL1|LOG_LOCAL2|LOG_LOCAL3|LOG_LOCAL4|LOG_LOCAL5|LOG_LOCAL6|LOG_LOCAL7)$/ 0 0 0
531 unless $log_facility =~ /^(?:LOG_KERN|LOG_USER|LOG_MAIL|LOG_DAEMON|LOG_AUTH|LOG_SYSLOG|LOG_LPR|LOG_NEWS|LOG_UUCP|LOG_CRON|LOG_AUTHPRIV|LOG_FTP|LOG_LOCAL0|LOG_LOCAL1|LOG_LOCAL2|LOG_LOCAL3|LOG_LOCAL4|LOG_LOCAL5|LOG_LOCAL6|LOG_LOCAL7)$/;
532 } else {
533 4 $log_facility = 'LOG_MAIL';
534 }
535 4 ▶ if ($syslog = $c->{syslog}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($syslog = $c->{"syslog"}) 0 4 4
536 0 $syslog = uc $syslog;
537 0 ▶ bad_config("invalid log request '$syslog'")
Branch: 1 branch
BranchTrueFalseTotal
unless $syslog =~ /^(?:STDERR|LOG_EMERG|LOG_ALERT|LOG_CRIT|LOG_ERR|LOG_WARNING|LOG_NOTICE|LOG_INFO|LOG_DEBUG)$/ 0 0 0
538 unless $syslog =~ /^(?:STDERR|LOG_EMERG|LOG_ALERT|LOG_CRIT|LOG_ERR|LOG_WARNING|LOG_NOTICE|LOG_INFO|LOG_DEBUG)$/;
539 }
540 # verbose
541 4 ▶ if ($verbose = $c->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose = $c->{'verbose'}) { } 0 4 4
542 0 ▶ bad_config("invalid verbosity '$verbose'")
Branch: 1 branch
BranchTrueFalseTotal
unless vld_num($verbose) and $verbose > 0 0 0 0
Condition0%0/3
vld_num($verbose) and $verbose > 0
!ll&&!rl&&r
000
MC/DC0%0/2
vld_num($verbose) and $verbose > 0
vld_num($verbose) $verbose > 0
Truth table0%0/3
A and B
A= vld_num($verbose)B= $verbose > 0
ABresult
0X0
100
111
543 unless vld_num($verbose) && $verbose > 0;
544 ########### DEEP TRACE CODE #############
545 0 ▶ if ($verbose > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose > 3) 0 0 0
546 0 foreach(sort keys %Mail::SMTP::Honeypot::) {
547 0 my $subref = \&{"Mail::SMTP::Honeypot::$_"};
548 0 $Mail::SMTP::Honeypot::{$_} =~ /[^:]+$/;
549 0 $subref{$subref} = $&;
550 }
551 }
552 ########### END DEEP TRACE CODE #############
553 } else {
554 4 $verbose = 0;
555 }
556 # dns host
557 4 ▶ if ($_ = $c->{dnshost}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = $c->{'dnshost'}) { } 0 4 4
558 0 ▶ bad_config("invalid dns hostname '$dnshost'")
Branch: 1 branch
BranchTrueFalseTotal
unless $dnshost = inet_aton($_) 0 0 0
559 unless ($dnshost = inet_aton($_));
560 } else {
561 4 $dnshost = get_ns();
562 }
563 # dns port
564 4 ▶ if ($dnsport = $c->{dnsport}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dnsport = $c->{'dnsport'}) { } 0 4 4
565 0 ▶ bad_config("invalid dns port number '$dnsport'")
Branch: 1 branch
BranchTrueFalseTotal
unless vld_num($dnsport) 0 0 0
566 unless vld_num($dnsport);
567 } else {
568 4 $dnsport = 53;
569 }
570 4 ▶ if ($dnshost) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dnshost) 4 0 4
571 4 $dnsaddr = sockaddr_in($dnsport,$dnshost);
572 }
573 # DNStimeout
574 4 ▶ if ($DNStimeout = $c->{DNStimeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DNStimeout = $c->{'DNStimeout'}) { } 0 4 4
575 0 ▶ bad_config("invalid DNS timeout '$DNStimeout'")
Branch: 1 branch
BranchTrueFalseTotal
unless vld_num($DNStimeout) and $DNStimeout >= $delay 0 0 0
Condition0%0/3
vld_num($DNStimeout) and $DNStimeout >= $delay
!ll&&!rl&&r
000
MC/DC0%0/2
vld_num($DNStimeout) and $DNStimeout >= $delay
vld_num($DNStimeout) $DNStimeout >= $delay
Truth table0%0/3
A and B
A= vld_num($DNStimeout)B= $DNStimeout >= $delay
ABresult
0X0
100
111
576 unless vld_num($DNStimeout) && $DNStimeout >= $delay;
577 } else {
578 4 $DNStimeout = $delay;
579 }
580 # maxthreads
581 4 ▶ if ($maxthreads = $c->{maxthreads}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxthreads = $c->{'maxthreads'}) { } 0 4 4
582 0 ▶ bad_config("invalid maximum client count '$maxthreads'")
Branch: 1 branch
BranchTrueFalseTotal
unless vld_num($maxthreads) 0 0 0
583 unless vld_num($maxthreads);
584 } else {
585 4 $maxthreads = 100;
586 }
587 # maxcmds
588 4 ▶ if ($maxcmds = $c->{maxcmds}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxcmds = $c->{'maxcmds'}) { } 0 4 4
589 0 ▶ bad_config("invalid maximum client count '$maxcmds'")
Branch: 1 branch
BranchTrueFalseTotal
unless vld_num($maxcmds) 0 0 0
590 unless vld_num($maxcmds);
591 } else {
592 4 $maxcmds = 100;
593 }
594 # disconnect
595 4 ▶ if ($disconnect = $c->{disconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disconnect = $c->{'disconnect'}) { } 0 4 4
596 0 ▶ bad_config("invalid maximum client count '$disconnect'")
Branch: 1 branch
BranchTrueFalseTotal
unless vld_num($disconnect) 0 0 0
597 unless vld_num($disconnect);
598 } else {
599 4 $disconnect = 300;
600 }
601 }
602  
603 0 ▶sub clean_child() {
Subroutine clean_child: not called CC=5 SCAR=34.0
Pod undocumented
604 0 my $pid = fork;
605 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
606 0 waitpid($pid,0);
607 0 exit 0;
608 }
609  
610 0 chdir '/'; # allow root dismount
611 0 ▶ open STDIN, '/dev/null' or die "Can't dup STDIN to /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "/dev/null" or die "Can't dup STDIN to /dev/null: $!" 0 0 0
612 0 ▶ open STDOUT, '>/dev/null' or die "Can't dup STDOUT to /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">/dev/null" or die "Can't dup STDOUT to /dev/null: $!" 0 0 0
613  
614 0 ▶ exit 0 if $pid = fork; # double fork to release instantiating terminal
Branch: 1 branch
BranchTrueFalseTotal
if $pid = fork 0 0 0
615 }
616  
617 4 ▶sub _trace {
Subroutine _trace: called CC=2 SCAR=6.9
618 4 ▶ return (wantarray) ? (\$threads,\$dns) : \$threads;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 1 4
619 }
620  
621 0 ▶sub init_all() {
Subroutine init_all: not called CC=2 SCAR=17.9
Pod undocumented
622 0 $unique = $$ -1;
623 0 $threads = {}; # thread hash
624 0 ▶ die "could not open DNS socket\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $dns = open_udpNB() 0 0 0
625 unless ($dns = open_udpNB());
626 0 $DNSfileno = fileno($dns);
627 0 $threads->{$DNSfileno} = {
628 sock => $dns,
629 alarm => 0,
630 # name => '4.3.2.1.in-addr.arpa',
631 # read => \&dns_rcv
632 };
633 0 $dns = {}; # dns transaction hash
634 }
635  
636 0 ▶sub my_dump {
Subroutine my_dump: not called CC=4 SCAR=30.0
Pod undocumented
637 0 my %names;
638 0 local *pref = __PACKAGE__ . '::';
639 0 foreach(keys %{*pref}) {
640 0 $names{'*'.$_} = \&{*pref->{$_}};
641 }
642 0 my @d = (
643 $threads => 'threads',
644 $dns => 'dns',
645 );
646 0 ▶ for ($_=0;$_<@d;$_+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ < @d) 0 0 0
647 0 my $d = new Data::Dumper([$d[$_]],[$d[$_+1]]);
648 0 $d->Seen(\%names);
649 0 @_ = split(/\n/,$d->Dump);
650 0 foreach(@_) {
651 0 logit($_ ."\n");
652 }
653 }
654 }
655  
656 0 ▶sub daemon {
Subroutine daemon: not called CC=27 SCAR=66.3
Pod documented
657 0 ▶ unless ($syslog && $syslog eq 'STDERR') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($syslog and $syslog eq "STDERR") 0 0 0
Condition0%0/3
$syslog and $syslog eq "STDERR"
!ll&&!rl&&r
000
MC/DC0%0/2
$syslog and $syslog eq "STDERR"
$syslog $syslog eq "STDERR"
Truth table0%0/3
A and B
A= $syslogB= $syslog eq "STDERR"
ABresult
0X0
100
111
658 0 ▶ open STDERR, '>/dev/null' or die "Can't dup STDERR to /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">/dev/null" or die "Can't dup STDERR to /dev/null: $!" 0 0 0
659 }
660  
661 # initialization complete, log start up message
662 0 logit('Initiated...');
663  
664 0 my $run = 1;
665 0 ▶ local $SIG{TERM} = sub {$run = 0};
Subroutine __ANON__: not called CC=1 SCAR=6.9
666 0 local $SIG{USR1} = \&my_dump;
667 0 local $SIG{PIPE} = 'IGNORE';
668  
669 0 my $then = time;
670 0 my $sock = open_listenNB($port,$laddr);
671 0 ▶ die "could not open listen socket on port $port\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
672 unless $sock;
673 0 my $fileno = fileno($sock);
674 0 my $go_listen = $threads->{$fileno} = {
675 sock => $sock,
676 alarm => 0,
677 read => \&newthread,
678 # next => \&next thing to do
679 };
680 0 my($rin,$win,$rout,$wout,$delta,$nfound);
681 0 ▶ while($run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run) 0 0 0
682 0 $win = $rin = '';
683 0 $threads->{$DNSfileno}->{read} = \&dns_rcv; # always armed
684 0 foreach(grep(!/\D/,keys %$threads)) { # each thread key
685 0 ▶ vec($rin,$_,1) = 1 if $threads->{$_}->{read}; # set read selects
Branch: 1 branch
BranchTrueFalseTotal
if $threads->{$_}{"read"} 0 0 0
686 0 ▶ vec($win,$_,1) = 1 if $threads->{$_}->{write}; # set write selects
Branch: 1 branch
BranchTrueFalseTotal
if $threads->{$_}{"write"} 0 0 0
687 }
688 0 $go_listen->{read} = \&newthread; # re-arm listner if it was busy
689 0 $nfound = select($rout=$rin,$wout=$win,undef,1); # tick each second
690  
691 0 ▶ if ($nfound > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nfound > 0) { } 0 0 0
elsif ($delta = ($_ = time) - $then) { } 0 0 0
692 0 ▶ do_thread($wout,'write') if $wout;
Branch: 1 branch
BranchTrueFalseTotal
if $wout 0 0 0
693 0 ▶ do_thread($rout,'read') if $rout;
Branch: 1 branch
BranchTrueFalseTotal
if $rout 0 0 0
694 }
695 elsif ($delta = ($_ = time) - $then) { # timer = next second or more
696 0 $then = $_;
697 0 my @threads = keys %$threads;
698 0 foreach(@threads) { # each receive thread
699 0 ▶ next unless exists $threads->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $threads->{$_} 0 0 0
700 0 my $tptr = $threads->{$_};
701 0 ▶ if ($tptr->{alarm} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($tptr->{"alarm"} and $tptr->{"alarm"} + $delay < $then) 0 0 0
Condition0%0/3
$tptr->{"alarm"} and $tptr->{"alarm"} + $delay < $then
!ll&&!rl&&r
000
MC/DC0%0/2
$tptr->{"alarm"} and $tptr->{"alarm"} + $delay < $then
$tptr->{"alarm"} $tptr->{"alarm"} + $delay < $then
Truth table0%0/3
A and B
A= $tptr->{"alarm"}B= $tptr->{"alarm"} + $delay < $then
ABresult
0X0
100
111
702 ($tptr->{alarm} + $delay) < $then) {
703 0 $tptr->{alarm} = time + $disconnect - $delay;
704 0 my($logtxt,$go);
705 0 ▶ if ($tptr->{tout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tptr->{'tout'}) { } 0 0 0
706 0 $go = $tptr->{tout};
707 0 $logtxt = 'tout ';
708 } else {
709 0 $go = $tptr->{next};
710 0 $logtxt = 'next ';
711 }
712 0 ▶ if ($verbose > 3) { # deep trace
Branch: 2 branches
BranchTrueFalseTotal
if ($verbose > 3) { } 0 0 0
elsif ($verbose > 2) { } 0 0 0
713 $logtxt = &who ."delay ended for '$_' $logtxt => ".
714 0 ▶ (exists $subref{$go}) ? $subref{$go} : 'sub ref not defined';
Branch: 1 branch
BranchTrueFalseTotal
&who . "delay ended for '${_}' $logtxt => " . exists $subref{$go} ? : 0 0 0
715 0 logit($logtxt);
716 }
717 elsif ($verbose > 2) {
718 0 logit(&who ."delay ended for '$_'\n");
719 }
720 0 $go->($_);
721 0 ▶ if (exists $threads->{$_} && ! $threads->{$_}->{tout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $threads->{$_} and not $threads->{$_}{"tout"}) 0 0 0
Condition0%0/3
exists $threads->{$_} and not $threads->{$_}{"tout"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $threads->{$_} and not $threads->{$_}{"tout"}
exists $threads->{$_} not $threads->{$_}{"tout"}
Truth table0%0/3
A and not(B)
A= exists $threads->{$_}B= not $threads->{$_}{"tout"}
ABresult
0X0
100
111
722 0 $threads->{$_}->{tout} = \&terminate
723 }
724 0 last;
725 }
726 }
727 0 foreach(keys %$dns) { # each dns thread
728 0 ▶ if ($dns->{$_}->{alarm} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($dns->{$_}{"alarm"} and $dns->{$_}{"alarm"} + $delay < $then) 0 0 0
Condition0%0/3
$dns->{$_}{"alarm"} and $dns->{$_}{"alarm"} + $delay < $then
!ll&&!rl&&r
000
MC/DC0%0/2
$dns->{$_}{"alarm"} and $dns->{$_}{"alarm"} + $delay < $then
$dns->{$_}{"alarm"} $dns->{$_}{"alarm"} + $delay < $then
Truth table0%0/3
A and B
A= $dns->{$_}{"alarm"}B= $dns->{$_}{"alarm"} + $delay < $then
ABresult
0X0
100
111
729 ($dns->{$_}->{alarm} + $delay) < $then) {
730 0 ▶ logit(&who ."dns ended for id $_ for $dns->{$_}->{fileno}\n") unless $verbose < 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 3 0 0 0
731 0 delete $dns->{$_};
732 0 last; # only do one per check for efficiancy
733 }
734 }
735 }
736 }
737 0 &close_all;
738 0 logit('Exiting...');
739 0 closelog();
740 0 exit 0;
741 }
742  
743 #
744 # execute a thread based on what the select routine returns
745 # sort used for testing only
746 #
747  
748 10 ▶sub do_thread { # t => do_thread.t
Subroutine do_thread: called CC=13 SCAR=28.1
Pod undocumented
749 10 my($vec,$op,$sort) = @_;
750 10 ▶ logit(&who . $op) unless $verbose < 3; # trace each thread
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 3 0 10 10
751 10 my @threads; # use array in case we decide not to use 'goto' at return of this subr
752 10 ▶ if ($sort) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort) { } 5 5 10
753 5 @threads = sort {$a <=> $b} grep(!/\D/,keys %$threads); # each numeric thread key
754 } else {
755 5 @threads = grep(!/\D/,keys %$threads);
756 }
757 10 foreach (@threads) { # or if re-entering after read with a deleted thread
758 38 ▶ next unless exists $threads->{$_} && $threads->{$_}; # skip killed threads
Branch: 1 branch
BranchTrueFalseTotal
unless exists $threads->{$_} and $threads->{$_} 5 33 38
Condition66%2/3
exists $threads->{$_} and $threads->{$_}
!ll&&!rl&&r
0533
MC/DC50%1/2
exists $threads->{$_} and $threads->{$_}
exists $threads->{$_} $threads->{$_}
Truth table66%2/3
A and B
A= exists $threads->{$_}B= $threads->{$_}
ABresult
0X0
100
111
759 33 ▶ next unless vec($vec,$_,1); # skip inactive threads
Branch: 1 branch
BranchTrueFalseTotal
unless vec $vec, $_, 1 16 17 33
760 17 ▶ next unless $threads->{$_}->{$op};
Branch: 1 branch
BranchTrueFalseTotal
unless $threads->{$_}{$op} 12 5 17
761 5 my $go = $threads->{$_}->{$op};
762 5 $threads->{$_}->{$op} = undef; # clear vector
763 5 ▶ next unless ref $go; # ignore blank vectors
Branch: 1 branch
BranchTrueFalseTotal
unless ref $go 0 5 5
764 5 @_ = ($_);
765 5 ▶ if ($verbose > 3) { # deep trace
Branch: 2 branches
BranchTrueFalseTotal
if ($verbose > 3) { } 0 5 5
elsif ($verbose > 2) { } 0 5 5
766 0 ▶ my $exsub = (exists $subref{$go}) ? $subref{$go} : 'sub ref not found';
Branch: 1 branch
BranchTrueFalseTotal
exists $subref{$go} ? : 0 0 0
767 0 logit(&who ."exec $op for '$_' => $exsub\n");
768 }
769 elsif ($verbose > 2) {
770 0 ▶ logit(&who ."executing $op for '$_'\n") unless $verbose < 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 3 0 0 0
771 }
772 5 goto $go; # do it and return
773 }
774 }
775  
776 2 ▶sub writesock { # t => new_rw_sock.t
Subroutine writesock: called CC=10 SCAR=24.7
Pod undocumented
777 2 my($fileno) = @_;
778 2 my $tptr = $threads->{$fileno};
779 2 my $bytes = length($tptr->{wargs}) - $tptr->{woff};
780 2 $! = 9;
781 my $wrote = syswrite( $tptr->{sock},
782 $tptr->{wargs},
783 $bytes,
784 $tptr->{woff},
785 2 ▶ ) if fileno($tptr->{sock}); # closed filehandles return false
Branch: 1 branch
BranchTrueFalseTotal
if fileno $tptr->{"sock"} 1 1 2
786 2 my $logtxt = &who . $fileno .' ';
787 2 ▶ if (defined $wrote) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wrote) { } 1 1 2
788 1 $logtxt .= $wrote;
789 }
790 else {
791 1 $logtxt .= 'sock error: '. $!;
792 }
793 2 ▶ logit($logtxt) unless $verbose < 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 3 0 2 2
794 2 ▶ if (defined $wrote) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $wrote) { } 1 1 2
elsif (sockerror($! || 9)) { } 1 0 1
795 1 $tptr->{woff} += $wrote;
796 1 ▶ if ($tptr->{woff} == $bytes) { # if complete
Branch: 1 branch
BranchTrueFalseTotal
if ($tptr->{"woff"} == $bytes) 1 0 1
797 1 my $go = $tptr->{next};
798 1 ▶ unless ($verbose < 4) { # deep trace
Branch: 1 branch
BranchTrueFalseTotal
unless ($verbose < 4) 0 1 1
799 0 ▶ my $exsub = (exists $subref{$go}) ? $subref{$go} : 'sub ref not found';
Branch: 1 branch
BranchTrueFalseTotal
exists $subref{$go} ? : 0 0 0
800 0 logit(&who ."next => $exsub for '$fileno'\n");
801 }
802 1 goto $go; # goto the next link
803 }
804 ▶ } elsif (sockerror($! || 9)) { # default to bad file descriptor
Condition50%1/2
$! || 9
l!l
10
MC/DC0%0/1
$! || 9
$!
Truth table50%1/2
A
A= $!
Aresult
11
00
805 1 goto &removethread; # remove thread if there was an error
806 }
807 0 $tptr->{write} = \&writesock; # restore write pointer
808 }
809  
810 3 ▶sub _readsock { # t => new_rw_sock.t
Subroutine _readsock: called CC=4 SCAR=15.6
811 3 my($fileno) = @_;
812 3 my $tptr = $threads->{$fileno};
813 my $bytes = sysread( $tptr->{sock},
814 $tptr->{rargs},
815 2048, # limit reads, data is mostly limited to 2048
816 $tptr->{roff}
817 3 ▶ ) if fileno($tptr->{sock}); # closed filehandles return false
Branch: 1 branch
BranchTrueFalseTotal
if fileno $tptr->{"sock"} 3 0 3
818 3 ▶ logit(&who . $fileno .' '. ((defined $bytes) ? $bytes : 'error '. $!))
Branch: 2 branches
BranchTrueFalseTotal
unless $verbose < 3 0 3 3
defined $bytes ? : 0 0 0
819 unless $verbose < 3; # trace
820 3 return($tptr,$bytes);
821 }
822
823 3 ▶sub readsock { # t => new_rw_sock.t
Subroutine readsock: called CC=7 SCAR=21.8
Pod undocumented
824 3 my ($tptr,$bytes) = &_readsock;
825 3 ▶ if (defined $bytes) { # returns undef on error
Branch: 2 branches
BranchTrueFalseTotal
if (defined $bytes) { } 2 1 3
elsif (sockerror($! || 9)) { } 0 1 1
826 2 ▶ goto &removethread
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes 1 1 2
827 unless $bytes; # EOF
828 1 $tptr->{alarm} = time; # renew timeout
829 1 $tptr->{roff} += $bytes; # bytes read
830 1 my $go = $tptr->{next};
831 1 ▶ unless ($verbose < 4) { # deep trace
Branch: 1 branch
BranchTrueFalseTotal
unless ($verbose < 4) 0 1 1
832 0 ▶ my $exsub = (exists $subref{$go}) ? $subref{$go} : 'sub ref not found';
Branch: 1 branch
BranchTrueFalseTotal
exists $subref{$go} ? : 0 0 0
833 0 logit(&who ."next => $exsub for '$_[0]'\n");
834 }
835 1 goto $go;
836 ▶ } elsif (sockerror($! || 9)) { # default to bad file descriptor
Condition50%1/2
$! || 9
l!l
10
MC/DC0%0/1
$! || 9
$!
Truth table50%1/2
A
A= $!
Aresult
11
00
837 0 goto &removethread; # detected fatal condition
838 }
839 # probably never get to here
840 1 $tptr->{read} = \&readsock; # restore read pointer
841 }
842  
843 #
844 # input: error code
845 # returns: true if error, else false
846 #
847 6 ▶sub sockerror { # t => sockerror.t
Subroutine sockerror: called CC=4 SCAR=13.9
Pod undocumented
848 6 my($err) = @_;
849 6 ▶ return ($err == EINTR || # don't die for interrupts
Branch: 2 branches
BranchTrueFalseTotal
$err == 4 || $err == 11 ? : 3 3 6
$err ? : 2 1 3
Condition100%3/3
$err == 4 or $err == 11
l!l&&r!l&&!r
123
MC/DC100%2/2
$err == 4 or $err == 11
$err == 4 $err == 11
Truth table100%3/3
A or B
A= $err == 4B= $err == 11
ABresult
1X1
011
000
850 $err == EWOULDBLOCK) # or while waiting
851 ? 0
852 : ($err) ? 1 : 0;
853 }
854  
855 # remove a thread, closing the socket
856 #
857 # input: threads pointer, fileno
858 # returns: nothing
859 #
860 3 ▶sub removethread { # t => removethread.t
Subroutine removethread: called CC=3 SCAR=11.3
Pod undocumented
861 3 my($fileno) = @_;
862 3 ▶ logit(&who . $fileno) unless $verbose < 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 3 0 3 3
863 3 my $sock = $threads->{$fileno}->{sock};
864 3 delete $threads->{$fileno};
865 3 ▶ close $sock if $sock; # don't attempt close on non-existent sock
Branch: 1 branch
BranchTrueFalseTotal
if $sock 3 0 3
866 }
867  
868 0 ▶sub close_all {
Subroutine close_all: not called CC=2 SCAR=17.9
Pod undocumented
869 0 foreach(keys %$threads) {
870 0 removethread($_);
871 }
872 }
873  
874 2 ▶sub newthread { # t => new_rw_sock.t
Subroutine newthread: called CC=6 SCAR=20.1
Pod undocumented
875 2 my($listner) = @_;
876 2 ▶ if ((keys %$threads) > $maxthreads) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$threads > $maxthreads) 0 2 2
877 0 ▶ logit(&who . "thread pool full\n") unless $verbose < 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 2 0 0 0
878 0 return;
879 }
880 2 $threads->{$listner}->{read} = \&newthread; # restore vector
881 2 my($sock,$netaddr) = accept_NB($threads->{$listner}->{sock});
882 2 ▶ return unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 1 1 2
883 1 my $ipaddr = inet_ntoa($netaddr);
884 1 ▶ unless ($ipaddr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ipaddr) 0 1 1
885 0 close $sock;
886 0 return;
887 }
888 1 set_so_linger($sock,30); # set linger to 30 seconds, just in case
889 1 my $fileno = fileno($sock);
890 1 $threads->{$fileno} = {
891 alarm => 1,
892 # cmdcnt => 0, # number of allowed commands
893 # conlog => 0, # connection logged
894 # domain => '', # claims to be this domain
895 ipaddr => $ipaddr, # dot quad
896 # lastc => 'CONN', # last connection state
897 name => '', # smtp host name
898 # next => \&sub, # next sub to exec
899 # proto => 'SMTP', # protocol
900 # read => \&sub, # read sub to exec
901 # rargs => '', # read string
902 # roff => 0, # length
903 sock => $sock, # socket
904 # write => \&sub, # write sub to exec
905 # wargs => ''. # string to write
906 # woff => 0, # offset into write string
907 };
908 1 ▶ if ($deny eq 'CONN') {
Branch: 1 branch
BranchTrueFalseTotal
if ($deny eq 'CONN') { } 0 1 1
909 0 $threads->{$fileno}->{next} = \&terminate;
910 } else {
911 1 dns_send($fileno,$ipaddr); # initiate a PTR lookup
912 1 @{$threads->{$fileno}}{qw(
913 alarm
914 cmdcnt
915 cok
916 domain
917 lastc
918 name
919 next
920 proto
921 wargs
922 )} = (
923 time, # alarm
924 0, # cmdcnt
925 0, # cok
926 '', # domain
927 'CONN', # lastc
928 '', # name
929 \&connOK, # next
930 'SMTP', # proto
931 '220 '. $hostname .' service ready'. $CRLF,
932 );
933 }
934 }
935  
936 0 ▶sub connOK {
Subroutine connOK: not called CC=1 SCAR=6.9
Pod undocumented
937 0 my($fileno) = @_;
938 0 my $tptr = $threads->{$fileno};
939 0 $tptr->{cok} = 1; # flag that says this is done
940 0 logit('honeypot connect '. $tptr->{name} .'['. $tptr->{ipaddr} .']');
941 0 $tptr->{woff} = 0;
942 0 $tptr->{next} = \&readSMTP;
943 0 $tptr->{tout} = \&write_delay;
944 # $tptr->{alarm} = use previous value
945 }
946  
947 0 ▶sub terminate { # t => parseSMTP.t
Subroutine terminate: not called CC=2 SCAR=17.9
Pod undocumented
948 0 my($fileno) = @_;
949 0 $threads->{$fileno}->{wargs} = '421 Service not available, closing transmission channel'. $CRLF;
950 0 ▶ logit(&who ."sent terminate for '$fileno'\n") unless $verbose < 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 3 0 0 0
951 0 write_rearm($fileno,\&removethread,1); # immediate terminate
952 }
953  
954 # implementation from rfc 2821
955 #
956 # STATE: allowed commands
957 #
958 # initial: HELO, EHLO, NOOP, HELP, VRFY, RSET, and QUIT
959 #
960 # HELO/EHLO MAIL, HELO, EHLO, NOOP, HELP, VRFY, RSET, and QUIT
961 #
962 # MAIL RCPT, HELO, EHLO, NOOP, HELP, VRFY, RSET, and QUIT
963 #
964 # RCPT RCPT, DATA, EHLO, NOOP, HELP, VRFY, RSET, and QUIT
965 #
966 # DATA {data} .
967  
968 7 %Commands = (
969 EHLO => \&_EHLO,
970 HELO => \&_HELO,
971 MAIL => \&_MAIL,
972 RCPT => \&_RCPT,
973 DATA => \&terminate,
974 RSET => \&_RSET,
975 VRFY => \&_VRFY,
976 HELP => \&_HELP,
977 NOOP => \&_NOOP,
978 QUIT => \&_QUIT,
979 SEND => \&notimp,
980 SOML => \&notimp,
981 SAML => \&notimp,
982 EXPN => \&notimp,
983 TURN => \&notimp,
984 );
985  
986 21 ▶sub parseSMTP { # t => parseSMTP.t
Subroutine parseSMTP: called CC=19 SCAR=29.6
Pod undocumented
987 21 my($fileno) = @_;
988 21 my $tptr = $threads->{$fileno};
989 21 ▶ goto &terminate if ++$tptr->{cmdcnt} > $maxcmds;
Branch: 1 branch
BranchTrueFalseTotal
if ++$tptr->{"cmdcnt"} > $maxcmds 0 21 21
990 21 my $newc = '';
991 21 my $smtp_args = '';
992 21 ▶ if ($tptr->{rargs} =~ /^\s*([a-zA-Z]{4})\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tptr->{"rargs"} =~ /^\s*([a-zA-Z]{4})\b/) 18 3 21
993 18 $newc = uc $1;
994 18 $smtp_args = lc $';
995 }
996 21 my $lastc = $tptr->{lastc};
997 21 $tptr->{wargs} = ''; # error text
998 21 ▶ unless ($newc) {
Branch: 6 branches
BranchTrueFalseTotal
if ($newc) { } 18 3 21
if (do { not exists $Commands{$newc} }) { } 1 17 18
elsif ($tptr->{'roff'} > 512) { } 1 16 17
elsif ($lastc =~ /(?:CONN|HELO|EHLO)/) { } 7 9 16
elsif ($lastc eq 'MAIL') { } 8 1 9
elsif ($lastc eq 'RCPT') { } 1 0 1
999 3 $tptr->{rargs} =~ s/[^[\w .-]//g;
1000 3 $tptr->{wargs} = '500 5.5.1 Command unrecognized "'. $tptr->{rargs} .'"';
1001 }
1002 elsif (! exists $Commands{$newc}) {
1003 1 $tptr->{wargs} = '500 5.5.1 Command unrecognized "'. $1 .'"';
1004 }
1005 elsif ($tptr->{roff} > 512) { # rfc2821 4.5.3.1
1006 1 $tptr->{wargs} = '500 5.5.4 Command line too long';
1007 }
1008 elsif ($lastc =~ /(?:CONN|HELO|EHLO)/) {
1009 7 ▶ if ($newc eq 'RCPT') {
Branch: 2 branches
BranchTrueFalseTotal
if ($newc eq 'RCPT') { } 2 5 7
elsif ($newc eq 'DATA') { } 2 3 5
1010 2 $tptr->{wargs} = '503 5.0.0 Need MAIL before RCPT';
1011 }
1012 elsif ($newc eq 'DATA') {
1013 2 $tptr->{wargs} = '503 5.0.0 Need MAIL command';
1014 }
1015 }
1016 elsif ($lastc eq 'MAIL') {
1017 8 ▶ if ($newc eq 'MAIL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($newc eq 'MAIL') { } 1 7 8
elsif ($newc eq 'DATA') { } 1 6 7
1018 1 $tptr->{wargs} = '503 5.5.0 Sender already specified';
1019 }
1020 elsif ($newc eq 'DATA') {
1021 1 $tptr->{wargs} = '503 5.0.0 Need RCPT before DATA';
1022 }
1023 }
1024 elsif ($lastc eq 'RCPT') {
1025 1 ▶ if ($newc eq 'MAIL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($newc eq "MAIL") 0 1 1
1026 0 $tptr->{wargs} = '503 5.5.0 Sender already specified';
1027 }
1028 }
1029  
1030 21 ▶ if ($tptr->{wargs}) { # if there is an error
Branch: 2 branches
BranchTrueFalseTotal
if ($tptr->{'wargs'}) { } 11 10 21
elsif ($newc eq $deny) { } 1 9 10
1031 11 ▶ logit(&who ."$newc ". $tptr->{wargs}) unless $verbose < 2; # more log info
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 2 0 11 11
1032 11 $tptr->{wargs} .= $CRLF;
1033 11 write_rearm($fileno,\&readSMTP); # send error and return to this routine
1034 } elsif ($newc eq $deny) {
1035 1 $tptr->{alarm} = time;
1036 1 $tptr->{next} = \&terminate;
1037 1 ▶ logit(&who .'deny '. $newc . $smtp_args) unless $verbose < 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 3 0 1 1
1038 } else { # else
1039 9 ▶ logit(&who . $newc . $smtp_args) unless $verbose < 3; # trace success
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 3 0 9 9
1040 9 $Commands{$newc}->($fileno,$smtp_args,$tptr); # execute the command
1041 }
1042 }
1043  
1044 #
1045 # input: to or from,
1046 # string [to/from: garbage junk email@addy.sufx more junk]
1047 # returns: (error text on error)
1048 # (name,domain) on match
1049 # or (name,{defined+false}) if 'postmaster' by itself
1050  
1051 8 ▶sub xtract_to_from { # t => parseSMTP.t
Subroutine xtract_to_from: called CC=14 SCAR=31.0
Pod undocumented
1052 8 my $match = lc shift; # 'to' or 'from' or 'vrfy'
1053 8 my $string = lc shift; # input string
1054 8 ▶ my $what = ($string =~ /[^\s:]+/) # must have some characters
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /[^\s:]+/ ? : 8 0 8
1055 ? $& : '';
1056 8 ▶ $string = $' || ''; # remainder of string
Condition50%1/2
$' || ''
l!l
80
MC/DC0%0/1
$' || ''
$'
Truth table50%1/2
A
A= $'
Aresult
11
00
1057 8 ▶ return ('501 5.5.2 Syntax error in parameters scanning "'. $what .'"'. $CRLF)
Branch: 1 branch
BranchTrueFalseTotal
unless $what eq $match and $string =~ /^:/ 0 8 8
Condition33%1/3
$what eq $match and $string =~ /^:/
!ll&&!rl&&r
008
MC/DC0%0/2
$what eq $match and $string =~ /^:/
$what eq $match $string =~ /^:/
Truth table33%1/3
A and B
A= $what eq $matchB= $string =~ /^:/
ABresult
0X0
100
111
1058 unless $what eq $match && $string =~ /^:/; # return error if 'to / from' does not match
1059 # or is not terminated with colon
1060 8 $string = $'; # snip off colon
1061 8 ▶ if ($string =~ /([\w\.-]+)@([\w\-]+\.[\w\.-]+)/) { # if email addy found
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /([\w\.-]+)@([\w\-]+\.[\w\.-]+)/) 3 5 8
1062 3 ▶ return ('500 5.5.4 User name too long'. $CRLF)
Branch: 1 branch
BranchTrueFalseTotal
if length $1 > 64 0 3 3
1063 if length($1) > 64; # rfc2821 4.3.5.1
1064 3 ▶ return ('500 5.5.4 Domain name too long'. $CRLF)
Branch: 1 branch
BranchTrueFalseTotal
if length $2 > 255 0 3 3
1065 if length($2) > 255;
1066 3 return ($1,$2);
1067 }
1068 5 ▶ return ('postmaster','')
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\s*<?postmaster>?\s*$/ and $match =~ /to|vrfy/ 0 5 5
Condition33%1/3
$string =~ /^\s*<?postmaster>?\s*$/ and $match =~ /to|vrfy/
!ll&&!rl&&r
500
MC/DC0%0/2
$string =~ /^\s*<?postmaster>?\s*$/ and $match =~ /to|vrfy/
$string =~ /^\s*<?postmaster>?\s*$/ $match =~ /to|vrfy/
Truth table33%1/3
A and B
A= $string =~ /^\s*<?postmaster>?\s*$/B= $match =~ /to|vrfy/
ABresult
0X0
100
111
1069 if $string =~ /^\s*<?postmaster>?\s*$/ && $match =~/to|vrfy/;
1070 5 ▶ return ('','')
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\s*<\s*>/ and $match =~ /from/ 0 5 5
Condition33%1/3
$string =~ /^\s*<\s*>/ and $match =~ /from/
!ll&&!rl&&r
500
MC/DC0%0/2
$string =~ /^\s*<\s*>/ and $match =~ /from/
$string =~ /^\s*<\s*>/ $match =~ /from/
Truth table33%1/3
A and B
A= $string =~ /^\s*<\s*>/B= $match =~ /from/
ABresult
0X0
100
111
1071 if $string =~ /^\s*<\s*>/ && $match =~/from/; # error message returned to ME
1072  
1073 # figure what kind or error to report
1074 5 $string =~ s/^\s+//; # waste leading spaces
1075 5 @_ = split(/\s+/,$string);
1076 5 ▶ return ('555 5.5.4 "'. $_[1] .'" parameter unrecognized'. $CRLF)
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 5 5
1077 if @_ > 1; # error if there are unknown parameters
1078 5 ▶ return ('553 5.5.4 Domain name required for address "'. $_[0] .'"'. $CRLF)
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 5 0 5
1079 if $_[0];
1080 0 return ('501 5.0.0 Argument required'. $CRLF);
1081 }
1082  
1083 #
1084 # SMTP commands
1085 #
1086 # HELO & EHLO
1087 #
1088  
1089 2 ▶sub _EHLO { # t => commands.t
Subroutine _EHLO: called CC=1 SCAR=0.0
1090 2 push @_, 1;
1091 2 goto &_HELO;
1092 }
1093  
1094 5 ▶sub _HELO { # t => commands.t
Subroutine _HELO: called CC=3 SCAR=11.0
1095 5 my($fileno,$smtp_args,$tptr,$is_EHLO) = @_;
1096 5 ▶ $tptr->{domain} = ($smtp_args =~ /[\w\.-]+/)
Branch: 1 branch
BranchTrueFalseTotal
$smtp_args =~ /[\w\.-]+/ ? : 2 3 5
1097 ? $& : 'nobody';
1098 # S: 250 hostname ready for {domain}
1099 # (ehlo) 250 HELP
1100 5 my $wargs = $hostname . ' ready for '. $tptr->{domain} .' ('. $tptr->{name} .'['. $tptr->{ipaddr} .'])'. $CRLF;
1101 5 ▶ if ($is_EHLO) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_EHLO) { } 2 3 5
1102 $tptr->{wargs} =
1103 2 '250-'. $wargs .
1104 '250 HELP'. $CRLF;
1105 2 $tptr->{lastc} = 'EHLO';
1106 2 $tptr->{proto} = 'ESMTP';
1107 } else {
1108 $tptr->{wargs} =
1109 3 '250 '. $wargs;
1110 3 $tptr->{lastc} = 'HELO';
1111 }
1112 5 write_rearm($fileno,\&readSMTP);
1113 }
1114  
1115 #
1116 # MAIL
1117 #
1118 # no attempt is made to verify the sender envelope address since
1119 # it is so easy to forge an address that will validate somewhere
1120 #
1121 3 ▶sub _MAIL { # t => commands.t
Subroutine _MAIL: called CC=3 SCAR=11.0
1122 3 my($fileno,$smtp_args,$tptr) = @_;
1123 3 my($name,$domain) = xtract_to_from('from',$smtp_args);
1124 3 ▶ unless (defined $domain) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain) { } 2 1 3
1125 1 $tptr->{wargs} = $name; # 'name' contains the error message when 'domain' is undefined
1126 } else {
1127 2 $tptr->{lastc} = 'MAIL';
1128 2 $tptr->{wargs} = '250 2.1.0 OK'. $CRLF;
1129 2 $tptr->{from} = $name .'@'. $domain;
1130 2 $tptr->{msgid} = uniquemsgid();
1131 2 ▶ logit($tptr->{msgid}.': from=<'. $tptr->{from} .'>, relay='. $tptr->{domain}.' ('. $tptr->{name} .'['. $tptr->{ipaddr} .'])')
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 1 0 2 2
1132 unless $verbose < 1;
1133 }
1134 3 write_rearm($fileno,\&readSMTP);
1135 }
1136  
1137 #
1138 # RCPT && VRFY
1139 #
1140 2 ▶sub _RCPT { # t => commands.t
Subroutine _RCPT: called CC=1 SCAR=0.0
1141 2 push @_, 1;
1142 2 goto &_VRFY;
1143 }
1144  
1145 5 ▶sub _VRFY { # t => commands.t
Subroutine _VRFY: called CC=7 SCAR=19.7
1146 5 my($fileno,$smtp_args,$tptr,$is_rcpt) = @_;
1147 5 my($name,$domain);
1148 5 ▶ if ($is_rcpt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_rcpt) { } 2 3 5
1149 2 ($name,$domain) = xtract_to_from('to',$smtp_args);
1150 } else {
1151 3 ($name,$domain) = xtract_to_from('vrfy','vrfy:'. $smtp_args);
1152 }
1153 5 ▶ if (defined $domain) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain) { } 1 4 5
1154 1 ▶ my $to = ($domain) ? $name .'@'. $domain : $name .'@'. $hostname; # postmaster is by itself without attached domain
Branch: 1 branch
BranchTrueFalseTotal
$domain ? : 1 0 1
1155 1 ▶ $tptr->{lastc} = ($is_rcpt) ? 'RCPT' : 'VRFY';
Branch: 1 branch
BranchTrueFalseTotal
$is_rcpt ? : 0 1 1
1156 1 $tptr->{wargs} = '250 2.1.5 OK'. $CRLF;
1157 1 ▶ $is_rcpt = ($is_rcpt) ? 'rcpt' : 'vrfy';
Branch: 1 branch
BranchTrueFalseTotal
$is_rcpt ? : 0 1 1
1158 1 ▶ logit($tptr->{msgid}.': '. $is_rcpt .'=<'. $to .'>, relay='. $tptr->{domain}.' ('. $tptr->{name} .'['. $tptr->{ipaddr} .'])')
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 1 0 1 1
1159 unless $verbose < 1;
1160 }
1161 else {
1162 4 $tptr->{wargs} = $name; # this is really the error string from xtract_to_from
1163 }
1164 5 write_rearm($fileno,\&readSMTP);
1165 }
1166  
1167 #
1168 # RSET
1169 #
1170 1 ▶sub _RSET { # t => commands.t
Subroutine _RSET: called CC=1 SCAR=0.0
1171 1 my($fileno,$smtp_args,$tptr) = @_;
1172 1 $tptr->{wargs} = '250 2.0.0 OK'. $CRLF;
1173 1 goto &soft_reset;
1174 }
1175  
1176 #
1177 # HELP
1178 #
1179 2 ▶sub _HELP { # t => commands.t
Subroutine _HELP: called CC=1 SCAR=0.0
1180 2 my($fileno,$smtp_args,$tptr) = @_;
1181 $tptr->{wargs} =
1182 2 '214-2.0.0 Commands supported are'. $CRLF .
1183 '214-2.0.0 HELO EHLO MAIL RCPT DATA'. $CRLF .
1184 '214 2.0.0 RSET VRFY HELP NOOP QUIT'. $CRLF;
1185 2 write_rearm($fileno,\&readSMTP);
1186 }
1187  
1188 #
1189 # NOOP
1190 #
1191 2 ▶sub _NOOP { # t => commands.t
Subroutine _NOOP: called CC=1 SCAR=0.0
1192 2 my($fileno,$smtp_args,$tptr) = @_;
1193 2 $tptr->{wargs} = '250 2.0.0 OK'. $CRLF;
1194 2 write_rearm($fileno,\&readSMTP);
1195 }
1196  
1197 #
1198 # QUIT
1199 #
1200 2 ▶sub _QUIT { # t => commands.t
Subroutine _QUIT: called CC=1 SCAR=0.0
1201 2 my($fileno,$smtp_args,$tptr) = @_;
1202 2 $threads->{$fileno}->{wargs} = '221 2.0.0 '. $hostname .' closing connection'. $CRLF;
1203 2 write_rearm($fileno,\&removethread);
1204 }
1205  
1206 #
1207 # DATA
1208 # this is where we disconnect
1209 #
1210 ### REPLACED BY TERMINATE
1211 #
1212 #sub _DATA {
1213 # my($fileno) = @_;
1214 # my $tptr = $threads->{$fileno};
1215 # $tptr->{woff} = 0;
1216 # $tptr->{next} = \&terminate;
1217 # $tptr->{tout} = 0;
1218 # $tptr->{alarm} = time; # wait 'delay'
1219 #}
1220  
1221 #
1222 # notimp
1223 #
1224 5 ▶sub notimp { # t => parseSMTP.t
Subroutine notimp: called CC=1 SCAR=0.0
Pod undocumented
1225 5 my($fileno,$smtp_args,$tptr) = @_;
1226 5 $tptr->{wargs} = '502 5.5.1 Command not implemented'. $CRLF;
1227 5 write_rearm($fileno,\&readSMTP);
1228 }
1229  
1230 2 ▶sub soft_reset { # t => commands.t
Subroutine soft_reset: called CC=4 SCAR=13.9
Pod undocumented
1231 2 my($fileno) = @_;
1232 2 my $tptr = $threads->{$fileno};
1233 2 my $wargs = $tptr->{wargs};
1234 2 my $ipaddr = $tptr->{ipaddr};
1235 2 ▶ my $name = $tptr->{name} || '';
Condition100%2/2
$tptr->{'name'} || ''
l!l
11
MC/DC100%1/1
$tptr->{'name'} || ''
$tptr->{'name'}
Truth table100%2/2
A
A= $tptr->{'name'}
Aresult
11
00
1236 2 $tptr = clear_bufs($fileno);
1237 2 $tptr->{lastc} = 'CONN';
1238 2 $tptr->{proto} = 'SMTP';
1239 2 ▶ $tptr->{wargs} = $wargs || '554 5.3.5 unknown mailer error'. $CRLF;
Condition50%1/2
$wargs || '554 5.3.5 unknown mailer error' . $CRLF
l!l
20
MC/DC0%0/1
$wargs || '554 5.3.5 unknown mailer error' . $CRLF
$wargs
Truth table50%1/2
A
A= $wargs
Aresult
11
00
1240 2 $tptr->{ipaddr} = $ipaddr;
1241 2 $tptr->{name} = $name;
1242 2 ▶ logit(&who . $tptr->{wargs}) unless $verbose < 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 2 0 2 2
1243 2 write_rearm($fileno,\&readSMTP);
1244 }
1245  
1246 0 ▶sub readSMTP {
Subroutine readSMTP: not called CC=1 SCAR=6.9
Pod undocumented
1247 0 my($fileno) = @_;
1248 0 my $tptr = $threads->{$fileno};
1249 0 $tptr->{alarm} = time;
1250 0 $tptr->{tout} = \&readRestore;
1251 }
1252  
1253 0 ▶sub readRestore {
Subroutine readRestore: not called CC=1 SCAR=6.9
Pod undocumented
1254 0 my($fileno) = @_;
1255 0 my $tptr = $threads->{$fileno};
1256 0 $tptr->{read} = \&readsock;
1257 0 $tptr->{roff} = 0;
1258 0 $tptr->{next} = \&parseSMTP;
1259 0 $tptr->{alarm} = time + $disconnect - $delay; # five minute timeout
1260 0 $tptr->{tout} = \&terminate;
1261 }
1262  
1263 # return buffers to the 'ehlo,helo' state
1264 #
1265 # input: threads, fileno
1266 # returns: $threads->{$fileno}
1267 #
1268 5 ▶sub clear_bufs { # t => commands.t
Subroutine clear_bufs: called CC=3 SCAR=11.0
Pod undocumented
1269 5 my($fileno) = @_;
1270 5 my($sock,$domain,$proto) = @{$threads->{$fileno}}{qw(
1271 sock domain proto)};
1272 5 delete $threads->{$fileno}; # clean all buffers in the thread
1273 5 ▶ my $lastc = ($domain)
Branch: 2 branches
BranchTrueFalseTotal
$domain ? : 3 2 5
$proto eq 'ESMTP' ? : 1 2 3
1274 ? ($proto eq 'ESMTP') ? 'EHLO' : 'HELO'
1275 : 'CONN';
1276 5 my $tptr = $threads->{$fileno} = {};
1277 5 @{$tptr}{qw(
1278 sock domain proto lastc)} = # restore only those that are needed
1279 ($sock,$domain,$proto,$lastc);
1280 5 return $tptr;
1281 }
1282  
1283 0 ▶sub write_rearm { # t => parseSMTP.t
Subroutine write_rearm: not called CC=2 SCAR=17.9
Pod undocumented
1284 0 my($fileno,$next,$immediate) = @_;
1285 0 my $tptr = $threads->{$fileno};
1286 0 $tptr->{woff} = 0;
1287 0 $tptr->{next} = $next;
1288 0 ▶ goto &write_delay
Branch: 1 branch
BranchTrueFalseTotal
if $immediate 0 0 0
1289 if $immediate;
1290 0 $tptr->{tout} = \&write_delay;
1291 0 $tptr->{alarm} = time; # wait 'delay'
1292 }
1293  
1294 0 ▶sub write_delay {
Subroutine write_delay: not called CC=1 SCAR=6.9
Pod undocumented
1295 0 my($fileno) = @_;
1296 0 my $tptr = $threads->{$fileno};
1297 0 $tptr->{tout} = \&terminate;
1298 0 $tptr->{write} = \&writesock;
1299 0 $tptr->{alarm} = time; # kill thread if we can't write
1300 }
1301  
1302 #=item * syslog_config();
1303 #
1304 #Configure Unix logging.
1305 #
1306 # NOTE, logging must be initiated by the caller
1307 #
1308 # input: none
1309 # output: none
1310 #
1311 #=cut
1312  
1313 0 ▶sub syslog_config {
Subroutine syslog_config: not called CC=3 SCAR=24.8
Pod undocumented
1314 0 ▶ if ($syslog && $syslog ne 'STDERR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($syslog and $syslog ne "STDERR") 0 0 0
Condition0%0/3
$syslog and $syslog ne "STDERR"
!ll&&!rl&&r
000
MC/DC0%0/2
$syslog and $syslog ne "STDERR"
$syslog $syslog ne "STDERR"
Truth table0%0/3
A and B
A= $syslogB= $syslog ne "STDERR"
ABresult
0X0
100
111
1315 0 openlog($me, LOG_PID(), eval "$log_facility");
1316 0 $LOG = eval "$syslog"; # save LOGlevel for everyone
1317 }
1318 }
1319  
1320 #=item * logit($msg);
1321 #
1322 #Log a message.
1323 #
1324 # input: message string
1325 # output: none
1326 #
1327 #=cut
1328  
1329 0 ▶sub logit {
Subroutine logit: not called CC=3 SCAR=24.8
Pod undocumented
1330 0 my($msg) = @_;
1331 0 ▶ return unless $syslog;
Branch: 1 branch
BranchTrueFalseTotal
unless $syslog 0 0 0
1332 0 $msg .= "\n";
1333 0 $msg =~ s/[\r\n]+/\n/g;
1334 0 ▶ if ($syslog eq 'STDERR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($syslog eq 'STDERR') { } 0 0 0
1335 0 print STDERR $msg;
1336 }
1337 else {
1338 0 syslog($LOG,"%s",$msg);
1339 }
1340 }
1341  
1342 #=item * closelog();
1343 #
1344 #Close the syslog facility if it has been opened
1345 #
1346 # input: none
1347 # returns: none
1348 #
1349 #=cut
1350  
1351 0 ▶sub closelog {
Subroutine closelog: not called CC=3 SCAR=24.8
Pod undocumented
1352 0 local $^W = 0; # no warnings;
1353 0 ▶ &Unix::Syslog::closelog
Branch: 1 branch
BranchTrueFalseTotal
if $syslog and $syslog ne "STDERR" 0 0 0
Condition0%0/3
$syslog and $syslog ne "STDERR"
!ll&&!rl&&r
000
MC/DC0%0/2
$syslog and $syslog ne "STDERR"
$syslog $syslog ne "STDERR"
Truth table0%0/3
A and B
A= $syslogB= $syslog ne "STDERR"
ABresult
0X0
100
111
1354 if $syslog && $syslog ne 'STDERR';
1355 }
1356  
1357 44 ▶sub get_unique { # t => uniquemsgid.t
Subroutine get_unique: called CC=3 SCAR=11.0
Pod undocumented
1358 44 my($seed) = @_;
1359 44 ▶ $unique = $seed if $seed;
Branch: 1 branch
BranchTrueFalseTotal
if $seed 14 30 44
1360 44 ▶ $unique = 1 if ++$unique > 65535;
Branch: 1 branch
BranchTrueFalseTotal
if ++$unique > 65535 2 42 44
1361 44 return $unique; # return an ascending number or the PID if just invoked
1362 }
1363  
1364 #=item * $msgid = uniquemsgid($seed);
1365 #
1366 #Uses 'time' as a seed (standard) unless specified. Returns an email-safe
1367 #alphanumeric string based on the time (or seed), the pid of the caller and a
1368 #random number. Guaranteed to be unique for multiple daemons with less than
1369 #65k new reqests per second.
1370 #
1371 # input: [optional seed] or [default 'time']
1372 # returns: string of the form: 'bbnPCFUDYctT'
1373 #
1374 #=cut
1375  
1376 30 ▶sub uniquemsgid { # t => uniquemsgid.t
Subroutine uniquemsgid: called CC=3 SCAR=11.0
Pod undocumented
1377 30 ▶ my $t = shift || time;
Condition100%2/2
shift() || time
l!l
273
MC/DC100%1/1
shift() || time
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1378 30 my $q = sprintf("%010u",($$ << 16) + get_unique());
1379 30 my @serial = ();
1380 30 foreach(0..5) {
1381 180 my $x = $t % 62;
1382 180 my $y = $q % 62;
1383 180 unshift @serial,$IDarray[$x],$IDarray[$y];
1384 180 $t = int $t/62;
1385 180 $q = int $q/62;
1386 }
1387 30 return join('',@serial);
1388 }
1389  
1390 0 ▶sub dns_send { # tested by hand
Subroutine dns_send: not called CC=5 SCAR=34.0
Pod undocumented
1391 0 ▶ return unless $dnsaddr && $DNSfileno; # skip if no DNS present
Branch: 1 branch
BranchTrueFalseTotal
unless $dnsaddr and $DNSfileno 0 0 0
Condition0%0/3
$dnsaddr and $DNSfileno
!ll&&!rl&&r
000
MC/DC0%0/2
$dnsaddr and $DNSfileno
$dnsaddr $DNSfileno
Truth table0%0/3
A and B
A= $dnsaddrB= $DNSfileno
ABresult
0X0
100
111
1392 0 my($fileno,$ipaddr) = @_;
1393 0 my $id = get_unique();
1394 0 ▶ logit(&who . $ipaddr ." $fileno id $id")
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 3 0 0 0
1395 unless $verbose < 3;
1396 0 my @ip = split(/\./,$ipaddr);
1397 0 @_ = reverse @ip;
1398 0 my $name = join('.',@_,'in-addr.arpa');
1399 0 my $buffer;
1400 0 my $offset = newhead(\$buffer,
1401 $id,
1402 BITS_QUERY | RD, # query, recursion desired
1403 1,0,0,0, # one question
1404 );
1405 0 my ($get,$put,$parse) = new Net::DNS::ToolKit::RR;
1406 0 $offset = $put->Question(\$buffer,$offset,$name,T_PTR,C_IN);
1407 0 ▶ return unless $buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer 0 0 0
1408 0 $dns->{$id} = {
1409 fileno => $fileno,
1410 alarm => time,
1411 name => $name,
1412 };
1413 0 $threads->{$fileno}->{id} = $id; # mark original thread with this ID
1414 # UDP may not block
1415 send(
1416 $threads->{$DNSfileno}->{sock},
1417 0 $buffer,0,
1418 $dnsaddr);
1419 }
1420  
1421 0 ▶sub dns_rcv { # tested by hand
Subroutine dns_rcv: not called CC=24 SCAR=64.0
Pod undocumented
1422 0 my($fileno) = @_;
1423 0 my $tptr = $threads->{$fileno};
1424 0 my $msg;
1425 0 my $sender = recv($tptr->{sock},$msg,NS_PACKETSZ,0);
1426 0 ▶ return undef unless $sender; # no message received
Branch: 1 branch
BranchTrueFalseTotal
unless $sender 0 0 0
1427 0 ▶ return undef if length($msg) < HFIXEDSZ; # short message
Branch: 1 branch
BranchTrueFalseTotal
if length $msg < HFIXEDSZ() 0 0 0
1428 0 my ($off,$id,$qr,$opcode,$aa,$tc,$rd,$ra,$mbz,$ad,$cd,$rcode,
1429 $qdcount,$ancount,$nscount,$arcount)
1430 = gethead(\$msg);
1431 return undef unless
1432 $tc == 0 &&
1433 $qr == 1 &&
1434 $opcode == QUERY &&
1435 ($rcode == NOERROR || $rcode == NXDOMAIN || $rcode == SERVFAIL) &&
1436 $qdcount == 1 &&
1437 0 ▶ exists $dns->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $tc == 0 and $qr == 1 and $opcode == QUERY() and $rcode == NOERROR() || $rcode == NXDOMAIN() || $rcode == SERVFAIL() and $qdcount == 1 and exists $dns->{$id} 0 0 0
Condition0%0/21
$tc == 0 and $qr == 1 and $opcode == QUERY() and $rcode == NOERROR() || $rcode == NXDOMAIN() || $rcode == SERVFAIL() and $qdcount == 1 and exists $dns->{$id}
!ll&&!rl&&r
000
$tc == 0 and $qr == 1 and $opcode == QUERY() and $rcode == NOERROR() || $rcode == NXDOMAIN() || $rcode == SERVFAIL() and $qdcount == 1
!ll&&!rl&&r
000
$tc == 0 and $qr == 1 and $opcode == QUERY() and $rcode == NOERROR() || $rcode == NXDOMAIN() || $rcode == SERVFAIL()
!ll&&!rl&&r
000
$tc == 0 and $qr == 1 and $opcode == QUERY()
!ll&&!rl&&r
000
$tc == 0 and $qr == 1
!ll&&!rl&&r
000
$rcode == NOERROR() or $rcode == NXDOMAIN() or $rcode == SERVFAIL()
l!l&&r!l&&!r
000
$rcode == NOERROR() or $rcode == NXDOMAIN()
l!l&&r!l&&!r
000
MC/DC0%0/8
$tc == 0 and $qr == 1 and $opcode == QUERY() and $rcode == NOERROR() || $rcode == NXDOMAIN() || $rcode == SERVFAIL() and $qdcount == 1 and exists $dns->{$id}
$tc == 0 $qr == 1 $opcode == QUERY() $rcode == NOERROR() $rcode == NXDOMAIN() $rcode == SERVFAIL() $qdcount == 1 exists $dns->{$id}
Truth table0%0/13
A and B and C and D or E or F and G and H
A= $tc == 0B= $qr == 1C= $opcode == QUERY()D= $rcode == NOERROR()E= $rcode == NXDOMAIN()F= $rcode == SERVFAIL()G= $qdcount == 1H= exists $dns->{$id}
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
111000XX0
1111XX0X0
11101X0X0
1110010X0
1111XX100
11101X100
111001100
1111XX111
11101X111
111001111
1438 0 my $pfno = $dns->{$id}->{fileno}; # originating thread pointer
1439 0 my $pname = $dns->{$id}->{name};
1440 0 delete $dns->{$id}; # remove dns query thread
1441 return undef
1442 0 ▶ unless exists $threads->{$pfno};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $threads->{$pfno} 0 0 0
1443 0 ▶ return undef unless length($msg) > HFIXEDSZ; # no message
Branch: 1 branch
BranchTrueFalseTotal
unless length $msg > HFIXEDSZ() 0 0 0
1444 0 my ($get,$put,$parse) = new Net::DNS::ToolKit::RR;
1445 0 my($name,$t,$type,$class,$ttl,$rdl,@rdata);
1446 0 ($off,$name,$type,$class) = $get->Question(\$msg,$off);
1447 0 my $lname = lc $name;
1448 0 ▶ if ( $ancount &&
Branch: 1 branch
BranchTrueFalseTotal
if ($ancount and $rcode == &NOERROR and $lname eq $pname and $type == T_PTR() and $class == C_IN()) 0 0 0
Condition0%0/12
$ancount and $rcode == &NOERROR and $lname eq $pname and $type == T_PTR() and $class == C_IN()
!ll&&!rl&&r
000
$ancount and $rcode == &NOERROR and $lname eq $pname and $type == T_PTR()
!ll&&!rl&&r
000
$ancount and $rcode == &NOERROR and $lname eq $pname
!ll&&!rl&&r
000
$ancount and $rcode == &NOERROR
!ll&&!rl&&r
000
MC/DC0%0/5
$ancount and $rcode == &NOERROR and $lname eq $pname and $type == T_PTR() and $class == C_IN()
$ancount $rcode == &NOERROR $lname eq $pname $type == T_PTR() $class == C_IN()
Truth table0%0/6
A and B and C and D and E
A= $ancountB= $rcode == &NOERRORC= $lname eq $pnameD= $type == T_PTR()E= $class == C_IN()
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
1449 $rcode == &NOERROR &&
1450 $lname eq $pname &&
1451 $type == T_PTR &&
1452 $class == C_IN
1453 ) {
1454 0 foreach(0..$ancount -1) {
1455 0 ($off,$name,$t,$class,$ttl,$rdl,@rdata) = $get->next(\$msg,$off);
1456 0 ▶ last if $t == T_PTR;
Branch: 1 branch
BranchTrueFalseTotal
if $t == T_PTR() 0 0 0
1457 }
1458 }
1459 0 ($name) = @rdata;
1460 0 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 0 0 0
1461 0 $threads->{$pfno}->{name} = $name .' ';
1462 0 ▶ logit(&who ."$pfno rDNS $rdata[0]") unless $verbose < 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 3 0 0 0
1463 } else {
1464 0 $threads->{$pfno}->{name} = '';
1465 0 ▶ logit(&who ."$pfno rDNS missing") unless $verbose < 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose < 3 0 0 0
1466 }
1467 0 ▶ connOK($pfno) unless $threads->{$pfno}->{cok}; # log connection, continue
Branch: 1 branch
BranchTrueFalseTotal
unless $threads->{$pfno}{"cok"} 0 0 0
1468 }
1469  
1470 =head1 COPYRIGHT
1471  
1472 Copyright 2004 - 2014, Michael Robinton <michael@bizsystems.com>
1473  
1474 This program is free software; you can redistribute it and/or modify
1475 it under the terms of the GNU General Public License (except as noted
1476 otherwise in individuals sub modules) published by
1477 the Free Software Foundation; either version 2 of the License, or
1478 (at your option) any later version.
1479  
1480 This program is distributed in the hope that it will be useful,
1481 but WITHOUT ANY WARRANTY; without even the implied warranty of
1482 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1483 GNU General Public License for more details.
1484  
1485 You should have received a copy of the GNU General Public License
1486 along with this program; if not, write to the Free Software
1487 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1488  
1489 =head1 AUTHOR
1490  
1491 Michael Robinton <michael@bizsystems.com>
1492  
1493 =head1 SEE ALSO
1494  
1495 L<Mail::SpamCannibal> on CPAN or spamcannibal.org
1496  
1497 =cut
1498  
1499 7 1;
↑ Summary recurse2txt »