| 1 | package Mail::SRS::Daemon; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $SRSSOCKET); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use IO::Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Mail::SRS qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | @EXPORT_OK = qw($SRSSOCKET); | |||||||||||||||||||||||||||
| 15 | 1 | %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 16 | all => \@EXPORT_OK, | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | $SRSSOCKET = '/tmp/srsd'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=12 SCAR=38.4
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 23 | 0 | ▶ | my $args = ($#_ == 0) ? %{ (shift) } : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | my @secrets = ref($args->{Secret}) eq 'ARRAY' | ||||||||||||||||||||||||||||
| 26 | 0 | ? @{ $args->{Secret} } | |||||||||||||||||||||||||||
| 27 | 1 | ▶ | : [ $args->{Secret} ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | if (exists $args->{SecretFile} && defined $args->{SecretFile}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args->{"SecretFile"} and defined $args->{"SecretFile"}
MC/DC0%0/2
exists $args->{"SecretFile"} and defined $args->{"SecretFile"}
exists $args->{"SecretFile"}
defined $args->{"SecretFile"}
Truth table33%1/3
A and B
A= exists $args->{"SecretFile"}B= defined $args->{"SecretFile"}
| |||||||||||||||||||||||||||||
| 30 | 0 | my $secretfile = $args->{SecretFile}; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | die "Secret file $secretfile not readable" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | unless -r $secretfile; | ||||||||||||||||||||||||||||
| 33 | 0 | local *FH; | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | open(FH, "<$secretfile") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | or die "Cannot open $secretfile: $!"; | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | while (<FH>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | next unless /\S/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | next if /^#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | push(@secrets, $_); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 0 | close(FH); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | die "No secret or secretfile given. Use --secret or --secretfile, ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | "and ensure the secret file is not empty." | ||||||||||||||||||||||||||||
| 46 | unless @secrets; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Preserve the pertinent original arguments, mostly for fun. | ||||||||||||||||||||||||||||
| 49 | my $self = { | ||||||||||||||||||||||||||||
| 50 | Secret => $args->{Secret}, | ||||||||||||||||||||||||||||
| 51 | SecretFile => $args->{SecretFile}, | ||||||||||||||||||||||||||||
| 52 | 1 | }; | |||||||||||||||||||||||||||
| 53 | 1 | ▶ | $self->{Socket} = delete $args->{Socket} if exists $args->{Socket}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # An alternative pattern would be to inherit this, rather than | ||||||||||||||||||||||||||||
| 56 | # delegate to it. | ||||||||||||||||||||||||||||
| 57 | 1 | $args->{Secret} = \@secrets; | |||||||||||||||||||||||||||
| 58 | # All other args are passed on verbatim. | ||||||||||||||||||||||||||||
| 59 | 1 | my $srs = new Mail::SRS($args); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | $self->{Instance} = $srs; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | return bless $self, $class; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 68 | 0 | my $srs = $self->{Instance}; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | print STDERR "Starting SRS daemon in PID $$\n"; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # Until we decide that forward() and reverse() can die, this will | ||||||||||||||||||||||||||||
| 73 | # allow us to trap the error messages from those subroutines. | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | local $SIG{__WARN__} = sub { die @_; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | my $listen = $self->{Socket}; | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | unless ($listen) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | unlink($SRSSOCKET) if -e $SRSSOCKET; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $listen ||= new IO::Socket::UNIX( | ||||||||||||||||||||||||||
|
Condition0%0/3
$listen ||= "IO::Socket::UNIX"->new("Type", 1, "Local", $Mail::SRS::Daemon::SRSSOCKET, "Listen", 1)
MC/DC0%0/2
$listen ||= "IO::Socket::UNIX"->new("Type", 1, "Local", $Mail::SRS::Daemon::SRSSOCKET, "Listen", 1)
$listen
"IO::Socket::UNIX"->new("Type", 1, "Local", $Mail::SRS::Daemon::SRSSOCKET, "Listen", 1)
Truth table0%0/3
A ||= B
A= $listenB= "IO::Socket::UNIX"->new("Type", 1, "Local", $Mail::SRS::Daemon::SRSSOCKET, "Listen", 1)
| |||||||||||||||||||||||||||||
| 80 | Type => SOCK_STREAM, | ||||||||||||||||||||||||||||
| 81 | Local => $SRSSOCKET, | ||||||||||||||||||||||||||||
| 82 | Listen => 1, | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | die "Unable to create listen socket: $!" unless $listen; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my $select = new IO::Select(); | |||||||||||||||||||||||||||
| 88 | 0 | $select->add($listen); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | while (my @socks = $select->can_read) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | foreach my $sock (@socks) { | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($sock == $listen) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | # print "Accept on $sock\n"; | ||||||||||||||||||||||||||||
| 94 | 0 | $select->add($listen->accept()); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 0 | my $line = <$sock>; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (defined($line)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | chomp($line); | |||||||||||||||||||||||||||
| 100 | # print "Read '$line' on $sock\n"; | ||||||||||||||||||||||||||||
| 101 | 0 | my @args = split(/\s+/, $line); | |||||||||||||||||||||||||||
| 102 | 0 | my $cmd = uc shift @args; | |||||||||||||||||||||||||||
| 103 | 0 | eval { | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($cmd eq 'FORWARD') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 105 | 0 | $sock->print($srs->forward(@args), "\n"); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | elsif ($cmd eq 'REVERSE') { | ||||||||||||||||||||||||||||
| 108 | 0 | $sock->print($srs->reverse(@args), "\n"); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||
| 111 | 0 | die "Invalid command $cmd"; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | }; | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | $sock->print("ERROR: $@"); | |||||||||||||||||||||||||||
| 116 | 0 | $select->remove($sock); | |||||||||||||||||||||||||||
| 117 | 0 | $sock->close(); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Exim requires that we unconditionally close the socket | ||||||||||||||||||||||||||||
| 122 | # print "Close on $sock\n"; | ||||||||||||||||||||||||||||
| 123 | 0 | $select->remove($sock); | |||||||||||||||||||||||||||
| 124 | 0 | $sock->flush(); | |||||||||||||||||||||||||||
| 125 | 0 | $sock->close(); | |||||||||||||||||||||||||||
| 126 | 0 | undef $sock; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 0 | my @exc = $select->has_exception(0); | |||||||||||||||||||||||||||
| 130 | 0 | foreach my $sock (@exc) { | |||||||||||||||||||||||||||
| 131 | # print "Exception on $sock\n"; | ||||||||||||||||||||||||||||
| 132 | 0 | $select->remove($sock); | |||||||||||||||||||||||||||
| 133 | 0 | $sock->close(); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||