lib/Mail/Pyzor/Client.pm

filter: Statementstmt 87.2% 89 / 102 Branchbran 53.8% 14 / 26 Conditioncond 50.0% 15 / 30 MC/DCmcdc 5.2% 1 / 19 Subroutinesub 94.7% 18 / 19 Podpod 100% 3 / 3 totaltotal 70.3% 140 / 199 CC 24 SCAR 18.0
CC
24
Cov
75%
_get_connection_or_die
12.0
_receive_packet
11.9
new
6.9
CRAP 6.0
« lib/Mail/Pyzor/SHA.pm ↑ Summary blib/lib/Mail/Pyzor.pm »
1 package Mail::Pyzor::Client;
2  
3 # Copyright 2018 cPanel, LLC.
4 # All rights reserved.
5 # http://cpanel.net
6 #
7 # <@LICENSE>
8 # Licensed to the Apache Software Foundation (ASF) under one or more
9 # contributor license agreements. See the NOTICE file distributed with
10 # this work for additional information regarding copyright ownership.
11 # The ASF licenses this file to you under the Apache License, Version 2.0
12 # (the "License"); you may not use this file except in compliance with
13 # the License. You may obtain a copy of the License at:
14 #
15 # http://www.apache.org/licenses/LICENSE-2.0
16 #
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22 # </@LICENSE>
23 #
24  
25 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 =encoding utf-8
29  
30 =head1 NAME
31  
32 Mail::Pyzor::Client - Pyzor client logic
33  
34 =head1 SYNOPSIS
35  
36 use Mail::Pyzor::Client ();
37 use Mail::Pyzor::Digest ();
38  
39 my $client = Mail::Pyzor::Client->new();
40  
41 my $digest = Mail::Pyzor::Digest::get( $msg );
42  
43 my $check_ref = $client->check($digest);
44 die $check_ref->{'Diag'} if $check_ref->{'Code'} ne '200';
45  
46 my $report_ref = $client->report($digest);
47 die $report_ref->{'Diag'} if $report_ref->{'Code'} ne '200';
48  
49 =head1 DESCRIPTION
50  
51 A bare-bones L<Pyzor|http://pyzor.org> client that currently only
52 implements the functionality needed for L<Mail::SpamAssassin>.
53  
54 =head1 PROTOCOL DETAILS
55  
56 The Pyzor protocol is not a published standard, and there appears to be
57 no meaningful public documentation. What follows is enough information,
58 largely gleaned through forum posts and reverse engineering, to facilitate
59 effective use of this module:
60  
61 Pyzor is an RPC-oriented, message-based protocol. Each message
62 is a simple dictionary of 7-bit ASCII keys and values. Server responses
63 always include at least the following:
64  
65 =over
66  
67 =item * C<Code> - Similar to HTTP status codes; anything besides C<200>
68 is an error.
69  
70 =item * C<Diag> - Similar to HTTP status reasons: a text description
71 of the status.
72  
73 =back
74  
75 (NB: There are additional standard response headers that are useful only for
76 the protocol itself and thus are not part of this module’s returns.)
77  
78 =head2 Reliability
79  
80 Pyzor uses UDP rather than TCP, so no message is guaranteed to reach its
81 destination. A transmission failure can happen in either the request or
82 the response; in either case, a timeout error will result. Such errors
83 are represented as thrown instances of L<Mail::Pyzor::X::Timeout>.
84  
85 =cut
86  
87 #----------------------------------------------------------------------
88  
89 2 our $VERSION = '0.06';
90  
91 2 our $DEFAULT_SERVER_HOST = 'public.pyzor.org';
92 2 our $DEFAULT_SERVER_PORT = 24441;
93 2 our $DEFAULT_USERNAME = 'anonymous';
94 2 our $DEFAULT_PASSWORD = '';
95 2 our $DEFAULT_OP_SPEC = '20,3,60,3';
96 2 our $PYZOR_PROTOCOL_VERSION = 2.1;
97 2 our $DEFAULT_TIMEOUT = 3.5;
98 2 our $READ_SIZE = 8192;
99  
100 2 ▶use Mail::Pyzor::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
101 2 ▶use IO::Socket::INET ();
Subroutine BEGIN: called CC=1 SCAR=0.0
102 2 ▶use IO::SigGuard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
103 2 ▶use Mail::Pyzor::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 2 my @hash_order = ( 'Op', 'Op-Digest', 'Op-Spec', 'Thread', 'PV', 'User', 'Time', 'Sig' );
106  
107 #----------------------------------------------------------------------
108  
109 =head1 CONSTRUCTOR
110  
111 =head2 new(%OPTS)
112  
113 Create a new pyzor client.
114  
115 =over 2
116  
117 =item Input
118  
119 %OPTS are (all optional):
120  
121 =over 3
122  
123 =item * C<server_host> - The pyzor server host to connect to (default is
124 C<public.pyzor.org>)
125  
126 =item * C<server_port> - The pyzor server port to connect to (default is
127 24441)
128  
129 =item * C<username> - The username to present to the pyzor server (default
130 is C<anonymous>)
131  
132 =item * C<password> - The password to present to the pyzor server (default
133 is empty)
134  
135 =item * C<timeout> - The maximum time, in seconds, to wait for a response
136 from the pyzor server (defeault is 3.5)
137  
138 =back
139  
140 =item Output
141  
142 =over 3
143  
144 Returns a L<Mail::Pyzor::Client> object.
145  
146 =back
147  
148 =back
149  
150 =cut
151  
152 5 ▶sub new {
Subroutine new: called CC=6 SCAR=19.3
Pod documented
153 5 my ( $class, %OPTS ) = @_;
154  
155 return bless {
156 '_server_host' => $OPTS{'server_host'} || $DEFAULT_SERVER_HOST,
157 '_server_port' => $OPTS{'server_port'} || $DEFAULT_SERVER_PORT,
158 '_username' => $OPTS{'username'} || $DEFAULT_USERNAME,
159 '_password' => $OPTS{'password'} || $DEFAULT_PASSWORD,
160 '_op_spec' => $DEFAULT_OP_SPEC,
161 5 ▶ '_timeout' => $OPTS{'timeout'} || $DEFAULT_TIMEOUT,
Condition66%10/15
$OPTS{'server_host'} || $Mail::Pyzor::Client::DEFAULT_SERVER_HOST
l!l&&r!l&&!r
140
$OPTS{'server_port'} || $Mail::Pyzor::Client::DEFAULT_SERVER_PORT
l!l&&r!l&&!r
140
$OPTS{'username'} || $Mail::Pyzor::Client::DEFAULT_USERNAME
l!l&&r!l&&!r
140
$OPTS{'password'} || $Mail::Pyzor::Client::DEFAULT_PASSWORD
l!l&&r!l&&!r
104
$OPTS{'timeout'} || $Mail::Pyzor::Client::DEFAULT_TIMEOUT
l!l&&r!l&&!r
140
MC/DC0%0/2
$OPTS{'server_host'} || $Mail::Pyzor::Client::DEFAULT_SERVER_HOST
$OPTS{'server_host'} $Mail::Pyzor::Client::DEFAULT_SERVER_HOST
MC/DC0%0/2
$OPTS{'server_port'} || $Mail::Pyzor::Client::DEFAULT_SERVER_PORT
$OPTS{'server_port'} $Mail::Pyzor::Client::DEFAULT_SERVER_PORT
MC/DC0%0/2
$OPTS{'username'} || $Mail::Pyzor::Client::DEFAULT_USERNAME
$OPTS{'username'} $Mail::Pyzor::Client::DEFAULT_USERNAME
MC/DC50%1/2
$OPTS{'password'} || $Mail::Pyzor::Client::DEFAULT_PASSWORD
$OPTS{'password'} $Mail::Pyzor::Client::DEFAULT_PASSWORD
MC/DC0%0/2
$OPTS{'timeout'} || $Mail::Pyzor::Client::DEFAULT_TIMEOUT
$OPTS{'timeout'} $Mail::Pyzor::Client::DEFAULT_TIMEOUT
Truth table66%2/3
A || B
A= $OPTS{'server_host'}B= $Mail::Pyzor::Client::DEFAULT_SERVER_HOST
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $OPTS{'server_port'}B= $Mail::Pyzor::Client::DEFAULT_SERVER_PORT
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $OPTS{'username'}B= $Mail::Pyzor::Client::DEFAULT_USERNAME
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $OPTS{'password'}B= $Mail::Pyzor::Client::DEFAULT_PASSWORD
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $OPTS{'timeout'}B= $Mail::Pyzor::Client::DEFAULT_TIMEOUT
ABresult
1X1
011
000
162 }, $class;
163 }
164  
165 #----------------------------------------------------------------------
166  
167 =head1 REQUEST METHODS
168  
169 =head2 report($digest)
170  
171 Report the digest of a spam message to the pyzor server. This function
172 will throw if a messaging failure or timeout happens.
173  
174 =over 2
175  
176 =item Input
177  
178 =over 3
179  
180 =item $digest C<SCALAR>
181  
182 The message digest to report, as given by
183 C<Mail::Pyzor::Digest::get()>.
184  
185 =back
186  
187 =item Output
188  
189 =over 3
190  
191 =item C<HASHREF>
192  
193 Returns a hashref of the standard attributes noted above.
194  
195 =back
196  
197 =back
198  
199 =cut
200  
201 2 ▶sub report {
Subroutine report: called CC=1 SCAR=0.0
Pod documented
202 2 my ( $self, $digest ) = @_;
203  
204 2 my $msg_ref = $self->_get_base_msg( 'report', $digest );
205  
206 1 $msg_ref->{'Op-Spec'} = $self->{'_op_spec'};
207  
208 1 return $self->_send_receive_msg($msg_ref);
209 }
210  
211 =head2 check($digest)
212  
213 Check the digest of a message to see if
214 the pyzor server has a report for it. This function
215 will throw if a messaging failure or timeout happens.
216  
217 =over 2
218  
219 =item Input
220  
221 =over 3
222  
223 =item $digest C<SCALAR>
224  
225 The message digest to check, as given by
226 C<Mail::Pyzor::Digest::get()>.
227  
228 =back
229  
230 =item Output
231  
232 =over 3
233  
234 =item C<HASHREF>
235  
236 Returns a hashref of the standard attributes noted above
237 as well as the following:
238  
239 =over
240  
241 =item * C<Count> - The number of reports the server has received
242 for the given digest.
243  
244 =item * C<WL-Count> - The number of whitelist requests the server has received
245 for the given digest.
246  
247 =back
248  
249 =back
250  
251 =back
252  
253 =cut
254  
255 2 ▶sub check {
Subroutine check: called CC=1 SCAR=0.0
Pod documented
256 2 my ( $self, $digest ) = @_;
257  
258 2 return $self->_send_receive_msg( $self->_get_base_msg( 'check', $digest ) );
259 }
260  
261 # ----------------------------------------
262  
263 2 ▶sub _send_receive_msg {
Subroutine _send_receive_msg: called CC=2 SCAR=7.0
264 2 my ( $self, $msg_ref ) = @_;
265  
266 2 ▶ my $thread_id = $msg_ref->{'Thread'} or die 'No thread ID?';
Branch: 1 branch
BranchTrueFalseTotal
my $thread_id = $msg_ref->{"Thread"} or die "No thread ID?" 2 0 2
267  
268 2 $self->_sign_msg($msg_ref);
269  
270 2 return $self->_do_send_receive(
271 $self->_generate_packet_from_message($msg_ref) . "\n\n",
272 $thread_id,
273 );
274 }
275  
276 5 ▶sub _get_base_msg {
Subroutine _get_base_msg: called CC=3 SCAR=11.0
277 5 my ( $self, $op, $digest ) = @_;
278  
279 5 ▶ die "Implementor error: op is required" if !$op;
Branch: 1 branch
BranchTrueFalseTotal
unless $op 1 4 5
280 4 ▶ die "error: digest is required" if !$digest;
Branch: 1 branch
BranchTrueFalseTotal
unless $digest 2 2 4
281  
282 return {
283 2 'User' => $self->{'_username'},
284 'PV' => $PYZOR_PROTOCOL_VERSION,
285 'Time' => time(),
286 'Op' => $op,
287 'Op-Digest' => $digest,
288 'Thread' => $self->_generate_thread_id()
289 };
290 }
291  
292 2 ▶sub _do_send_receive {
Subroutine _do_send_receive: called CC=2 SCAR=7.0
293 2 my ( $self, $packet, $thread_id ) = @_;
294  
295 2 my $sock = $self->_get_connection_or_die();
296  
297 2 $self->_send_packet( $sock, $packet );
298 2 my $response = $self->_receive_packet( $sock, $thread_id );
299  
300 2 my $resp_hr = { map { ( split(m{: }) )[ 0, 1 ] } split( m{\n}, $response ) };
301  
302 2 delete $resp_hr->{'Thread'};
303  
304 2 my $response_pv = delete $resp_hr->{'PV'};
305  
306 2 ▶ if ( $PYZOR_PROTOCOL_VERSION ne $response_pv ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Pyzor::Client::PYZOR_PROTOCOL_VERSION ne $response_pv) 0 2 2
307 0 warn "Unexpected protocol version ($response_pv) in Pyzor response!";
308 }
309  
310 2 return $resp_hr;
311 }
312  
313 2 ▶sub _receive_packet {
Subroutine _receive_packet: called CC=8 SCAR=24.8
314 2 my ( $self, $sock, $thread_id ) = @_;
315  
316 2 my $timeout = $self->{'_timeout'} * 1000;
317  
318 2 my $end_time = time + $self->{'_timeout'};
319  
320 2 $sock->blocking(0);
321 2 my $response = '';
322 2 my $rout = '';
323 2 my $rin = '';
324 2 vec( $rin, fileno($sock), 1 ) = 1;
325  
326 2 while (1) {
327 2 my $time_left = $end_time - time;
328  
329 2 ▶ if ( $time_left <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time_left <= 0) 0 2 2
330 0 die Mail::Pyzor::X->create( 'Timeout', "Did not receive a response from the pyzor server $self->{'_server_host'}:$self->{'_server_port'} for $self->{'_timeout'} seconds!" );
331 }
332  
333 2 my $bytes = IO::SigGuard::sysread( $sock, $response, $READ_SIZE, length $response );
334 2 ▶ if ( !defined($bytes) && !$!{'EAGAIN'} && !$!{'EWOULDBLOCK'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bytes or $!{"EAGAIN"} or $!{"EWOULDBLOCK"}) 0 2 2
Condition33%2/6
defined $bytes or $!{"EAGAIN"} or $!{"EWOULDBLOCK"}
l!l&&r!l&&!r
200
defined $bytes or $!{"EAGAIN"}
l!l&&r!l&&!r
200
MC/DC0%0/3
defined $bytes or $!{"EAGAIN"} or $!{"EWOULDBLOCK"}
defined $bytes $!{"EAGAIN"} $!{"EWOULDBLOCK"}
Truth table25%1/4
A or B or C
A= defined $bytesB= $!{"EAGAIN"}C= $!{"EWOULDBLOCK"}
ABCresult
1XX1
01X1
0011
0000
335 0 warn "read from socket: $!";
336 }
337  
338 2 ▶ if ( index( $response, "\n\n" ) > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($response, "\n\n") != -1) 2 0 2
339  
340 # Reject the response unless its thread ID matches what we sent.
341 # This prevents confusion among concurrent Pyzor reqeusts.
342 2 ▶ if ( index( $response, "\nThread: $thread_id\n" ) != -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($response, "\nThread: $thread_id\n") != -1) { } 2 0 2
343 2 last;
344 }
345 else {
346 0 $response = '';
347 }
348 }
349  
350 0 my $found = IO::SigGuard::select( $rout = $rin, undef, undef, $time_left );
351 0 ▶ warn "select(): $!" if $found == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $found == -1 0 0 0
352 }
353  
354 2 return $response;
355 }
356  
357 0 ▶sub _send_packet {
Subroutine _send_packet: not called CC=2 SCAR=17.9
358 0 my ( $self, $sock, $packet ) = @_;
359  
360 0 $sock->blocking(1);
361 0 ▶ IO::SigGuard::syswrite( $sock, $packet ) or warn "write to socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
IO::SigGuard::syswrite($sock, $packet) or warn "write to socket: $!" 0 0 0
362  
363 0 return;
364 }
365  
366 1 ▶sub _get_connection_or_die {
Subroutine _get_connection_or_die: called CC=6 SCAR=24.8
367 1 my ($self) = @_;
368  
369 # clear the socket if the PID changes
370 1 ▶ if ( defined $self->{'_sock_pid'} && $self->{'_sock_pid'} != $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_sock_pid"} and $self->{"_sock_pid"} != $$) 0 1 1
Condition33%1/3
defined $self->{"_sock_pid"} and $self->{"_sock_pid"} != $$
!ll&&!rl&&r
100
MC/DC0%0/2
defined $self->{"_sock_pid"} and $self->{"_sock_pid"} != $$
defined $self->{"_sock_pid"} $self->{"_sock_pid"} != $$
Truth table33%1/3
A and B
A= defined $self->{"_sock_pid"}B= $self->{"_sock_pid"} != $$
ABresult
0X0
100
111
371 0 undef $self->{'_sock_pid'};
372 0 undef $self->{'_sock'};
373 }
374  
375 1 ▶ $self->{'_sock_pid'} ||= $$;
Condition33%1/3
$self->{"_sock_pid"} ||= $$
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"_sock_pid"} ||= $$
$self->{"_sock_pid"} $$
Truth table33%1/3
A ||= B
A= $self->{"_sock_pid"}B= $$
ABresult
1X1
011
000
376 $self->{'_sock'} ||= IO::Socket::INET->new(
377 'PeerHost' => $self->{'_server_host'},
378 1 ▶ 'PeerPort' => $self->{'_server_port'},
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_sock"} ||= "IO::Socket::INET"->new("PeerHost", $self->{"_server_host"}, "PeerPort", $self->{"_server_port"}, "Proto", "udp") or die qq[Cannot connect to $self->{"_server_host"}:$self->{"_server_port"}: $@ $!] 0 1 1
Condition33%1/3
$self->{"_sock"} ||= "IO::Socket::INET"->new("PeerHost", $self->{"_server_host"}, "PeerPort", $self->{"_server_port"}, "Proto", "udp")
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"_sock"} ||= "IO::Socket::INET"->new("PeerHost", $self->{"_server_host"}, "PeerPort", $self->{"_server_port"}, "Proto", "udp")
$self->{"_sock"} "IO::Socket::INET"->new("PeerHost", $self->{"_server_host"}, "PeerPort", $self->{"_server_port"}, "Proto", "udp")
Truth table33%1/3
A ||= B
A= $self->{"_sock"}B= "IO::Socket::INET"->new("PeerHost", $self->{"_server_host"}, "PeerPort", $self->{"_server_port"}, "Proto", "udp")
ABresult
1X1
011
000
379 'Proto' => 'udp'
380 ) or die "Cannot connect to $self->{'_server_host'}:$self->{'_server_port'}: $@ $!";
381  
382 0 return $self->{'_sock'};
383 }
384  
385 2 ▶sub _sign_msg {
Subroutine _sign_msg: called CC=1 SCAR=0.0
386 2 my ( $self, $msg_ref ) = @_;
387  
388 $msg_ref->{'Sig'} = lc Mail::Pyzor::SHA::sha1_hex(
389 Mail::Pyzor::SHA::sha1( $self->_generate_packet_from_message($msg_ref) ) . #
390 ':' . #
391 2 $msg_ref->{'Time'} . #
392 ':' . #
393 $self->_get_user_pass_hash_key() #
394 );
395  
396 2 return 1;
397 }
398  
399 4 ▶sub _generate_packet_from_message {
Subroutine _generate_packet_from_message: called CC=1 SCAR=0.0
400 4 my ( $self, $msg_ref ) = @_;
401  
402 4 return join( "\n", map { "$_: $msg_ref->{$_}" } grep { length $msg_ref->{$_} } @hash_order );
403 }
404  
405 2 ▶sub _generate_thread_id {
Subroutine _generate_thread_id: called CC=2 SCAR=6.9
406 2 my $RAND_MAX = 2**16;
407 2 my $val = 0;
408 2 ▶ $val = int rand($RAND_MAX) while $val < 1024;
Branch: 1 branch
BranchTrueFalseTotal
$val < 1024 and do { $val = int rand $RAND_MAX } 2 2 4
409 2 return $val;
410 }
411  
412 2 ▶sub _get_user_pass_hash_key {
Subroutine _get_user_pass_hash_key: called CC=1 SCAR=0.0
413 2 my ($self) = @_;
414  
415 2 return lc Mail::Pyzor::SHA::sha1_hex( $self->{'_username'} . ':' . $self->{'_password'} );
416 }
417  
418 2 1;
« lib/Mail/Pyzor/SHA.pm ↑ Summary blib/lib/Mail/Pyzor.pm »