blib/lib/Mail/Pyzor/Client.pm untested

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