Authentication/Client.pm

filter: Statementstmt 92.8% 195 / 210 Branchbran 74.1% 46 / 62 Conditioncond 51.6% 16 / 31 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 82.2% 283 / 344 CC 49 SCAR 24.9
CC
49
Cov
85%
new
26.4
load_mail
12.8
r
9.3
CRAP 12.1
« Authentication/Metric/Grafana.pm ↑ Summary Authentication/Handler/TLS.pm »
1 package Mail::Milter::Authentication::Client;
2 113 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 113 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 113 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 113 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Client for connecting back to the authmilter server
7 113 our $VERSION = '4.20261116'; # VERSION
8 113 ▶use Mail::Milter::Authentication::Net::Milter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 113 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 113 ▶use Digest::MD5 qw{ md5_base64 };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 113 ▶use Email::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14  
15 33 ▶sub new {
Subroutine new: called CC=17 SCAR=32.7
Pod documented
16 33 my ( $class, $args ) = @_;
17  
18 33 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0330
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
19 33 my $self = {};
20  
21 33 my $config = get_config();
22 {
23 33 ▶ my $connection = $config->{'connection'} || die('No connection details given');
Condition50%1/2
$config->{'connection'} || die('No connection details given')
l!l
330
MC/DC0%0/1
$config->{'connection'} || die('No connection details given')
$config->{'connection'}
Truth table50%1/2
A
A= $config->{'connection'}
Aresult
11
00
24 33 $connection =~ /^([^:]+):([^:@]+)(?:@([^:@]+|\[[0-9a-f:\.]+\]))?$/;
25 33 my $type = $1;
26 33 my $path = $2;
27 33 ▶ my $host = $3 || q{};
Condition50%1/2
$3 || ''
l!l
033
MC/DC0%0/1
$3 || ''
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
28 33 ▶ if ( $type eq 'inet' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'inet') { } 0 33 33
elsif ($type eq 'unix') { } 33 0 33
29 0 $self->{'type'} = 'tcp';
30 0 $self->{'port'} = $path;
31 0 $self->{'path'} = $host;
32 }
33 elsif ( $type eq 'unix' ) {
34 33 $self->{'type'} = 'unix';
35 33 $self->{'port'} = 10;
36 33 $self->{'path'} = $path;
37 }
38 else {
39 0 die 'Invalid connection';
40 }
41 }
42  
43 33 ▶ if ( $config->{'protocol'} ne 'milter' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"protocol"} ne "milter") 0 33 33
44 0 die 'Client only works with milter protocol mode';
45 }
46  
47 33 $self->{'mailer_string'} = 'Testfix 1.00.0';
48 33 ▶ $self->{'mailer_name'} = $args->{'mailer_name'} || 'test.mta.example.com';
Condition50%1/2
$args->{'mailer_name'} || 'test.mta.example.com'
l!l
330
MC/DC0%0/1
$args->{'mailer_name'} || 'test.mta.example.com'
$args->{'mailer_name'}
Truth table50%1/2
A
A= $args->{'mailer_name'}
Aresult
11
00
49  
50 33 ▶ $self->{'connect_ip'} = $args->{'connect_ip'} || '66.111.4.147';
Condition50%1/2
$args->{'connect_ip'} || '66.111.4.147'
l!l
330
MC/DC0%0/1
$args->{'connect_ip'} || '66.111.4.147'
$args->{'connect_ip'}
Truth table50%1/2
A
A= $args->{'connect_ip'}
Aresult
11
00
51 33 ▶ $self->{'connect_name'} = $args->{'connect_name'} || 'test.example.com';
Condition50%1/2
$args->{'connect_name'} || 'test.example.com'
l!l
330
MC/DC0%0/1
$args->{'connect_name'} || 'test.example.com'
$args->{'connect_name'}
Truth table50%1/2
A
A= $args->{'connect_name'}
Aresult
11
00
52 33 ▶ $self->{'connect_port'} = $args->{'connect_port'} || '123456';
Condition50%1/2
$args->{'connect_port'} || '123456'
l!l
033
MC/DC0%0/1
$args->{'connect_port'} || '123456'
$args->{'connect_port'}
Truth table50%1/2
A
A= $args->{'connect_port'}
Aresult
11
00
53 33 ▶ $self->{'connect_type'} = $args->{'connect_type'} || 'tcp4';
Condition50%1/2
$args->{'connect_type'} || 'tcp4'
l!l
033
MC/DC0%0/1
$args->{'connect_type'} || 'tcp4'
$args->{'connect_type'}
Truth table50%1/2
A
A= $args->{'connect_type'}
Aresult
11
00
54  
55 33 ▶ $self->{'helo_host'} = $args->{'helo_host'} || 'test.host.example.com';
Condition50%1/2
$args->{'helo_host'} || 'test.host.example.com'
l!l
330
MC/DC0%0/1
$args->{'helo_host'} || 'test.host.example.com'
$args->{'helo_host'}
Truth table50%1/2
A
A= $args->{'helo_host'}
Aresult
11
00
56 33 ▶ $self->{'mail_from'} = $args->{'mail_from'} || '';
Condition100%2/2
$args->{'mail_from'} || ''
l!l
321
MC/DC100%1/1
$args->{'mail_from'} || ''
$args->{'mail_from'}
Truth table100%2/2
A
A= $args->{'mail_from'}
Aresult
11
00
57 33 ▶ $self->{'rcpt_to'} = $args->{'rcpt_to'} || 'test@to.example.com';
Condition50%1/2
$args->{'rcpt_to'} || 'test@to.example.com'
l!l
330
MC/DC0%0/1
$args->{'rcpt_to'} || 'test@to.example.com'
$args->{'rcpt_to'}
Truth table50%1/2
A
A= $args->{'rcpt_to'}
Aresult
11
00
58  
59 # Generate a unique Queue ID
60 33 $self->{'queue_id'} = md5_base64( "Authentication Milter Client $PID " . time() );
61  
62 33 $self->{'mail_file'} = $args->{'mail_file'};
63 33 $self->{'mail_data'} = $args->{'mail_data'};
64 33 ▶ if ( ! $self->{'mail_file'} && ! $self->{'mail_data'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"mail_file"} or $self->{"mail_data"}) 0 33 33
Condition33%1/3
$self->{"mail_file"} or $self->{"mail_data"}
l!l&&r!l&&!r
3300
MC/DC0%0/2
$self->{"mail_file"} or $self->{"mail_data"}
$self->{"mail_file"} $self->{"mail_data"}
Truth table33%1/3
A or B
A= $self->{"mail_file"}B= $self->{"mail_data"}
ABresult
1X1
011
000
65 0 die 'No mail file or data supplied';
66 }
67  
68 33 $self->{'testing'} = $args->{'testing'};
69  
70 33 $self->{'milter'} = Mail::Milter::Authentication::Net::Milter->new();
71  
72 33 bless($self,$class);
73 33 return $self;
74 }
75  
76  
77 628 ▶sub r { ## no critic [Subroutines::RequireArgUnpacking]
Subroutine r: called CC=9 SCAR=22.3
Pod documented
78 628 my $self = shift;
79 628 my @results = @_;
80 RESULT:
81 628 foreach my $result ( @results ) {
82 713 my $action = $result->{'action'};
83 713 ▶ if ( $action eq 'continue' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($action eq 'continue') { } 627 86 713
elsif ($action eq 'insert') { } 63 23 86
elsif ($action eq 'replace') { } 13 10 23
elsif ($action eq 'add') { } 9 1 10
elsif ($action eq 'reject') { } 1 0 1
84 627 next RESULT;
85 }
86 elsif ( $action eq 'insert' ) {
87 63 my $value = $result->{'value'};
88 63 my $header = $result->{'header'};
89 63 my $index = $result->{'index'};
90 63 $self->insert_header( $index, $header, $value );
91 # warn "INSERT HEADER $header at position $index\n$value\n\n";
92 }
93 elsif ( $action eq 'replace' ) {
94 13 my $value = $result->{'value'};
95 13 my $header = $result->{'header'};
96 13 my $index = $result->{'index'};
97 13 $self->replace_header( $index, $header, $value );
98 # warn "REPLACE HEADER $header at position $index\n$value\n\n";
99 }
100 elsif ( $action eq 'add' ) {
101 9 my $value = $result->{'value'};
102 9 my $header = $result->{'header'};
103 9 $self->add_header( $header, $value );
104 # warn "ADD HEADER $header\n$value\n\n";
105 }
106 elsif ( $action eq 'reject' ) {
107 1 ▶ my $value = $result->{'value'} || q{};
Condition50%1/2
$result->{'value'} || ''
l!l
10
MC/DC0%0/1
$result->{'value'} || ''
$result->{'value'}
Truth table50%1/2
A
A= $result->{'value'}
Aresult
11
00
108 1 $value =~ s/\0/ /g;
109 1 ▶ if ( $self->{'testing'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
110 1 $self->{'rejected'} = "Message rejected with code : $value";
111 }
112 else {
113 0 die "Message rejected with code : $value\n";
114 }
115 }
116 else {
117 0 warn "Unknown Action\n";
118 0 warn Dumper $result;
119 }
120 }
121 }
122  
123  
124 63 ▶sub insert_header {
Subroutine insert_header: called CC=3 SCAR=11.0
Pod documented
125 63 my ( $self, $index, $header, $value ) = @_;
126 63 my @process_header = @{ $self->{'header_pairs'} };
127 63 my @header_pairs;
128 63 my $i = 1;
129 63 ▶ while ( @process_header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@process_header) 716 63 779
130 716 my $key = shift @process_header;
131 716 my $evalue = shift @process_header;
132 716 ▶ if ( $i == $index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $index) 63 653 716
133 63 push @header_pairs, $header;
134 63 push @header_pairs, $value;
135 }
136 716 push @header_pairs, $key;
137 716 push @header_pairs, $evalue;
138 716 $i++;
139 }
140 63 $self->{'header_pairs'} = \@header_pairs;
141 }
142  
143  
144 13 ▶sub replace_header {
Subroutine replace_header: called CC=5 SCAR=16.2
Pod documented
145 13 my ( $self, $index, $header, $value ) = @_;
146  
147 13 my @process_header = @{ $self->{'header_pairs'} };
148 13 my @header_pairs;
149 13 my $i = 1;
150 13 ▶ while ( @process_header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@process_header) 276 13 289
151 276 my $key = shift @process_header;
152 276 my $evalue = shift @process_header;
153 276 ▶ if ( lc $key eq lc $header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $key eq lc $header) { } 62 214 276
154 62 ▶ if ( $i == $index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $index) { } 13 49 62
155 13 ▶ if ( $value eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq '') { } 13 0 13
156 # NOP
157 }
158 else {
159 0 push @header_pairs, $key;
160 0 push @header_pairs, $value;
161 }
162 }
163 else {
164 49 push @header_pairs, $key;
165 49 push @header_pairs, $evalue;
166 }
167 62 $i++;
168 }
169 else {
170 214 push @header_pairs, $key;
171 214 push @header_pairs, $evalue;
172 }
173 }
174 13 $self->{'header_pairs'} = \@header_pairs;
175 }
176  
177  
178 9 ▶sub add_header {
Subroutine add_header: called CC=1 SCAR=0.0
Pod documented
179 9 my ( $self, $header, $value ) = @_;
180 9 my @header_pairs = @{ $self->{'header_pairs'} };
181 9 push @header_pairs, $header;
182 9 push @header_pairs, $value;
183 9 $self->{'header_pairs'} = \@header_pairs;
184 }
185  
186  
187 33 ▶sub load_mail {
Subroutine load_mail: called CC=12 SCAR=25.5
Pod documented
188 33 my ( $self ) = @_;
189  
190 33 my $mail_data;
191 33 ▶ if ( $self->{'mail_file'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'mail_file'}) { } 33 0 33
elsif ($self->{'mail_data'}) { } 0 0 0
192 33 open my $inf, '<', $self->{'mail_file'};
193 33 my @mail_content = <$inf>;
194 33 close $inf;
195 33 $mail_data = join( q{}, @mail_content );
196 }
197 elsif ( $self->{'mail_data'} ) {
198 0 $mail_data = $self->{'mail_data'};
199 }
200  
201 33 my @header_pairs;
202 my @header_split;
203  
204 HEADERS:
205 33 foreach my $dataline ( split ( /\r?\n/, $mail_data ) ) {
206 # Handle transparency
207 1197 ▶ if ( $dataline =~ /^\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataline =~ /^\./u) 0 1197 1197
208 0 $dataline = substr( $dataline, 1 );
209 }
210 1197 ▶ if ( $dataline eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dataline eq "") 31 1166 1197
211 31 last HEADERS;
212 }
213 1166 push @header_split, $dataline;
214 }
215  
216 33 my $value = q{};
217 33 foreach my $header_line ( @header_split ) {
218 1166 ▶ if ( $header_line =~ /^\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_line =~ /^\s/u) { } 767 399 1166
219 767 $value .= "\r\n" . $header_line;
220 }
221 else {
222 399 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 366 33 399
223 366 my ( $hkey, $hvalue ) = split ( ':', $value, 2 );
224 366 ▶ $hvalue =~ s/^ // if defined $hvalue;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hvalue 366 0 366
225 366 push @header_pairs , $hkey;
226 366 push @header_pairs , $hvalue;
227 }
228 399 $value = $header_line;
229 }
230 }
231 33 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 33 0 33
232 33 my ( $hkey, $hvalue ) = split ( ':', $value, 2 );
233 33 ▶ $hvalue =~ s/^ // if defined $hvalue;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hvalue 33 0 33
234 33 push @header_pairs , $hkey;
235 33 push @header_pairs , $hvalue;
236 }
237  
238 33 my $message_object = Email::Simple->new( $mail_data );
239 33 $self->{'message_object'} = $message_object;
240 33 $self->{'header_pairs'} = \@header_pairs;
241 }
242  
243  
244 33 ▶sub process {
Subroutine process: called CC=6 SCAR=17.9
Pod documented
245 33 my ( $self ) = @_;
246  
247 33 $self->load_mail();
248 33 my $milter = $self->{'milter'};
249  
250 33 $milter->open( $self->{'path'}, $self->{'port'}, $self->{'type'} );
251 33 $milter->protocol_negotiation(
252 SMFIF_ADDHDRS => 1,
253 SMFIF_CHGBODY => 0,
254 SMFIF_ADDRCPT => 0,
255 SMFIF_DELRCPT => 0,
256 SMFIF_CHGHDRS => 1,
257 SMFIP_NOCONNECT => 0,
258 SMFIP_NOHELO => 0,
259 SMFIP_NOMAIL => 0,
260 SMFIP_NORCPT => 0,
261 SMFIP_NOBODY => 0,
262 SMFIP_NOHDRS => 0,
263 SMFIP_NOEOH => 0,
264 );
265  
266 $milter->send_macros(
267 'v' => $self->{'mailer_string'},
268 'j' => $self->{'mailer_name'},
269 33 '{daemon_name}' => $self->{'$mailer_name'},
270 );
271  
272 $self->r( $milter->send_connect(
273 $self->{'connect_name'},
274 $self->{'connect_type'},
275 $self->{'connect_port'},
276 33 $self->{'connect_ip'},
277 ));
278  
279 33 $self->r( $milter->send_helo( $self->{'helo_host'} ));
280  
281 $milter->send_macros(
282 '{mail_mailer}' => 'smtp',
283 '{mail_addr}' => $self->{'mail_from'},
284 33 '{mail_host}' => $self->{'helo_host'},
285 );
286 33 $self->r( $milter->send_mail_from( $self->{'mail_from'} ));
287  
288 $milter->send_macros(
289 '{rcpt_mailer}' => 'local',
290 '{rcpt_addr}' => $self->{'rcpt_to'},
291 33 '{rcpt_host}' => $self->{'helo_host'},
292 );
293 33 $self->r( $milter->send_rcpt_to( $self->{'rcpt_to'} ));
294  
295 33 my @process_header = @{ $self->{'header_pairs'} };
296 33 ▶ while ( @process_header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@process_header) 399 33 432
297 399 my $key = shift @process_header;
298 399 my $value = shift @process_header;
299 399 $self->r( $milter->send_header( $key, $value ));
300 }
301  
302 33 $milter->send_macros( 'i' => $self->{'queue_id'} );
303 33 $self->r( $milter->send_end_headers());
304  
305 33 my $body = $self->{'message_object'}->body();
306  
307 33 my $chunk_size = 50000;
308 33 ▶ while ($body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body) 31 33 64
309 31 my $body_chunk;
310 31 ▶ if (length($body) > $chunk_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body > $chunk_size) { } 0 31 31
311 0 $body_chunk = substr($body,0,$chunk_size);
312 0 $body = substr($body,$chunk_size);
313 }
314 else {
315 31 $body_chunk = $body;
316 31 $body = '';
317 }
318 31 $milter->send_macros( 'i' => $self->{'queue_id'} );
319 31 $self->r( $milter->send_body( $body_chunk ));
320 }
321  
322 33 $milter->send_macros( 'i' => $self->{'queue_id'} );
323 33 $self->r( $milter->send_end_body());
324  
325 33 $milter->send_abort();
326  
327 33 $milter->send_quit();
328  
329 33 my $header_string = q{};
330 {
331 33 my @process_header = @{ $self->{'header_pairs'} };
332 33 ▶ while ( @process_header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@process_header) 458 33 491
333 458 my $key = shift @process_header;
334 458 my $value = shift @process_header;
335 458 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 458 458
336 458 $header_string .= "$key: $value\015\012";
337 }
338 33 my $header_obj = Email::Simple::Header->new( $header_string );
339 33 $self->{'message_object'}->header_obj_set( $header_obj );
340 }
341  
342 33 $self->{'result'} = $self->{'message_object'}->as_string();
343 }
344  
345  
346 33 ▶sub result {
Subroutine result: called CC=3 SCAR=11.0
Pod documented
347 33 my ( $self ) = @_;
348 33 ▶ return $self->{'rejected'} if $self->{'rejected'} && $self->{'testing'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rejected"} and $self->{"testing"} 1 32 33
Condition66%2/3
$self->{"rejected"} and $self->{"testing"}
!ll&&!rl&&r
3201
MC/DC50%1/2
$self->{"rejected"} and $self->{"testing"}
$self->{"rejected"} $self->{"testing"}
Truth table66%2/3
A and B
A= $self->{"rejected"}B= $self->{"testing"}
ABresult
0X0
100
111
349 32 return $self->{'result'};
350 }
351  
352 113 1;
353  
354 __END__
« Authentication/Metric/Grafana.pm ↑ Summary Authentication/Handler/TLS.pm »