blib/lib/Net/EPP/Server.pm

filter: Statementstmt 26.7% 93 / 348 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 51.7% 29 / 56 Podpod 38.1% 8 / 21 totaltotal 21.8% 130 / 594 CC 76 SCAR 44.9
CC
76
Cov
19%
process_frame
380.0
run_command
132.0
generate_response
110.0
CRAP 89.2
↑ Summary
1 package Net::EPP::Server;
2 # ABSTRACT: A simple EPP server implementation.
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Crypt::OpenSSL::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Digest::SHA qw(sha512_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Slurp qw(write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use List::Util qw(any none);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Mozilla::CA;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Net::EPP 0.27;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Net::EPP::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Net::EPP::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Net::EPP::ResponseCodes;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use No::Worries::DN qw(dn_parse);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Socket6;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Time::HiRes qw(ualarm);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use base qw(Net::Server::PreFork);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use open qw(:encoding(utf8));
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use feature qw(say state);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use vars qw($VERSION %MESSAGES $HELLO);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 our $VERSION = '0.10';
34  
35 1 our %MESSAGES = (
36 1000 => 'Command completed successfully.',
37 1001 => 'Command completed successfully; action pending.',
38 1300 => 'Command completed successfully; no messages.',
39 1301 => 'Command completed successfully; ack to dequeue.',
40 1500 => 'Command completed successfully; ending session.',
41 2000 => 'Unknown command.',
42 2001 => 'Command syntax error.',
43 2002 => 'Command use error.',
44 2003 => 'Required parameter missing.',
45 2004 => 'Parameter value range error.',
46 2005 => 'Parameter value syntax error.',
47 2100 => 'Unimplemented protocol version.',
48 2101 => 'Unimplemented command.',
49 2102 => 'Unimplemented option.',
50 2103 => 'Unimplemented extension.',
51 2104 => 'Billing failure.',
52 2105 => 'Object is not eligible for renewal.',
53 2106 => 'Object is not eligible for transfer.',
54 2200 => 'Authentication error.',
55 2201 => 'Authorization error.',
56 2202 => 'Invalid authorization information.',
57 2300 => 'Object pending transfer.',
58 2301 => 'Object not pending transfer.',
59 2302 => 'Object exists.',
60 2303 => 'Object does not exist.',
61 2304 => 'Object status prohibits operation.',
62 2305 => 'Object association prohibits operation.',
63 2306 => 'Parameter value policy error.',
64 2307 => 'Unimplemented object service.',
65 2308 => 'Data management policy violation.',
66 2400 => 'Command failed.',
67 2500 => 'Command failed; server closing connection.',
68 2501 => 'Authentication error; server closing connection.',
69 2502 => 'Session limit exceeded; server closing connection.',
70 );
71  
72 1 $HELLO = XML::LibXML::Document->new;
73 1 $HELLO->setDocumentElement($HELLO->createElementNS($Net::EPP::Frame::EPP_URN, 'epp'));
74 1 $HELLO->documentElement->appendChild($HELLO->createElement('hello'));
75  
76  
77 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
78 0 my $package = shift;
79  
80 0 return bless($package->SUPER::new, $package);
81 }
82  
83  
84 0 ▶sub run {
Subroutine run: not called CC=7 SCAR=40.3
Pod documented
85 0 my ($self, %args) = @_;
86  
87 0 ▶ $args{'host'} ||= 'localhost';
Condition0%0/2
$args{"host"} ||= "localhost"
l!l
00
MC/DC0%0/1
$args{"host"} ||= "localhost"
$args{"host"}
Truth table0%0/2
A
A= $args{"host"}
Aresult
11
00
88 0 ▶ $args{'port'} ||= 7000;
Condition0%0/2
$args{"port"} ||= 7000
l!l
00
MC/DC0%0/1
$args{"port"} ||= 7000
$args{"port"}
Truth table0%0/2
A
A= $args{"port"}
Aresult
11
00
89 0 ▶ $args{'proto'} ||= 'ssl';
Condition0%0/2
$args{"proto"} ||= "ssl"
l!l
00
MC/DC0%0/1
$args{"proto"} ||= "ssl"
$args{"proto"}
Truth table0%0/2
A
A= $args{"proto"}
Aresult
11
00
90  
91 $self->{'epp'} = {
92 'handlers' => delete($args{'handlers'}) || {},
93 'timeout' => delete($args{'timeout'}) || 30,
94 'client_ca_file' => delete($args{'client_ca_file'}),
95 'xsd_file' => delete($args{'xsd_file'}),
96 0 ▶ 'log_dir' => delete($args{'log_dir'}),
Condition0%0/4
delete $args{'handlers'} || {}
l!l
00
delete $args{'timeout'} || 30
l!l
00
MC/DC0%0/1
delete $args{'handlers'} || {}
delete $args{'handlers'}
MC/DC0%0/1
delete $args{'timeout'} || 30
delete $args{'timeout'}
Truth table0%0/2
A
A= delete $args{'handlers'}
Aresult
11
00
Truth table0%0/2
A
A= delete $args{'timeout'}
Aresult
11
00
97 };
98  
99 0 ▶ if ($self->{'epp'}->{'client_ca_file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"epp"}{"client_ca_file"}) 0 0 0
100 0 $args{'SSL_verify_mode'} = SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
101 0 $args{'SSL_client_ca_file'} = $self->{'epp'}->{'client_ca_file'};
102 }
103  
104 0 return $self->SUPER::run(%args);
105 }
106  
107 #
108 # This method is called when a new connection is received. It sends the
109 # <greeting> to the client, then enters the main loop.
110 #
111 0 ▶sub process_request {
Subroutine process_request: not called CC=1 SCAR=6.9
Pod documented
112 0 my ($self, $socket) = @_;
113  
114 0 $self->send_frame($socket, $self->generate_greeting);
115  
116 0 $self->main_loop($socket);
117  
118 0 $socket->flush;
119 0 $socket->close;
120 }
121  
122 #
123 # This method initialises the session, and calls main_loop_iteration() in a
124 # loop. That method returns the result code, and the loop will terminate if the
125 # code indicates that it should.
126 #
127 0 ▶sub main_loop {
Subroutine main_loop: not called CC=3 SCAR=24.8
Pod undocumented
128 0 my ($self, $socket) = @_;
129  
130 0 my $session = $self->init_session($socket);
131  
132 0 while (1) {
133 0 my $code = $self->main_loop_iteration($socket, $session);
134  
135 0 ▶ last if (OK_BYE == $code || $code >= COMMAND_FAILED_BYE);
Branch: 1 branch
BranchTrueFalseTotal
if 1500 == $code or $code >= 2500 0 0 0
Condition0%0/3
1500 == $code or $code >= 2500
l!l&&r!l&&!r
000
MC/DC0%0/2
1500 == $code or $code >= 2500
1500 == $code $code >= 2500
Truth table0%0/3
A or B
A= 1500 == $codeB= $code >= 2500
ABresult
1X1
011
000
136 }
137 }
138  
139 #
140 # This method initialises a new session
141 #
142 0 ▶sub init_session {
Subroutine init_session: not called CC=3 SCAR=24.8
Pod undocumented
143 0 my ($self, $socket) = @_;
144  
145 0 ▶ my $session = {
Branch: 1 branch
BranchTrueFalseTotal
4 == length $socket->peeraddr ? : 0 0 0
146 'session_id' => $self->generate_svTRID,
147 'remote_addr' => inet_ntop(4 == length($socket->peeraddr) ? AF_INET : AF_INET6, $socket->peeraddr),
148 'remote_port' => $socket->peerport,
149 'counter' => 0,
150 };
151  
152 0 ▶ if ($socket->peer_certificate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket->peer_certificate) 0 0 0
153 0 $session->{'client_cert'} = {
154 'issuer' => dn_to_hashref($socket->peer_certificate('issuer')),
155 'subject' => dn_to_hashref($socket->peer_certificate('subject')),
156 'common_name' => $socket->peer_certificate('commonName'),
157 };
158 };
159  
160 0 return $session;
161 }
162  
163 #
164 # this function wraps No::Worries::DN::dn_parse() and returns a hashref instead
165 # of an array
166 #
167 0 ▶sub dn_to_hashref {
Subroutine dn_to_hashref: not called CC=2 SCAR=17.9
Pod undocumented
168 0 my $ref = {};
169  
170 0 foreach (@{dn_parse(shift)}) {
171 0 my ($k, $v) = split(/=/, $_, 2);
172 0 $ref->{$k} = $v;
173 }
174  
175 0 return $ref;
176 }
177  
178 #
179 # This method reads a frame from the client, passes it to process_frame(),
180 # sends the response back to the client, and returns the result code back to
181 # main_loop().
182 #
183 0 ▶sub main_loop_iteration {
Subroutine main_loop_iteration: not called CC=3 SCAR=24.8
Pod undocumented
184 0 my ($self, $socket, $session) = @_;
185  
186 0 my $xml = $self->get_frame($socket);
187  
188 0 ▶ return COMMAND_FAILED_BYE if (!$xml);
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
189  
190 0 my $response = $self->process_frame($xml, $session);
191  
192 0 $self->send_frame($socket, $response);
193  
194 0 $session->{'counter'}++;
195  
196 0 $self->write_log($session, $xml, $response);
197  
198 0 ▶ if ('greeting' eq $response->documentElement->firstChild->localName) {
Branch: 1 branch
BranchTrueFalseTotal
if ('greeting' eq $response->documentElement->firstChild->localName) { } 0 0 0
199 0 return OK;
200  
201 } else {
202 0 return $response->getElementsByTagName('result')->item(0)->getAttribute('code');
203  
204 }
205 }
206  
207 #
208 # write the command and response to the log
209 #
210 0 ▶sub write_log {
Subroutine write_log: not called CC=2 SCAR=17.9
Pod undocumented
211 0 my ($self, $session, $command, $response) = @_;
212  
213 0 ▶ return unless exists($self->{'epp'}->{'log_dir'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"epp"}{"log_dir"} 0 0 0
214  
215 my $dir = File::Spec->catdir(
216 abs_path($self->{'epp'}->{'log_dir'}),
217 0 $session->{'session_id'}
218 );
219  
220 0 make_path($dir, { mode => 0700});
221  
222 0 write_file(File::Spec->catfile($dir, sprintf('%016u-command.xml', $session->{'counter'})), $command);
223 0 write_file(File::Spec->catfile($dir, sprintf('%016u-response.xml', $session->{'counter'})), $response);
224 }
225  
226 #
227 # This method is a wrapper around Net::EPP::Protocol->get_frame() which
228 # implements a timeout and exception handler.
229 #
230 0 ▶sub get_frame {
Subroutine get_frame: not called CC=2 SCAR=17.9
Pod undocumented
231 0 my ($self, $socket) = @_;
232  
233 0 my $xml;
234  
235 0 eval {
236 0 ▶ local $SIG{ALRM} = sub { die("ALARM\n") };
Subroutine __ANON__: not called CC=1 SCAR=6.9
237  
238 0 ualarm(1000 * 1000 * ($self->{'epp'}->{'timeout'}));
239  
240 0 $xml = Net::EPP::Protocol->get_frame($socket);
241  
242 0 ualarm(0);
243 };
244  
245 0 ▶ return ($@ ? undef : $xml);
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
246 }
247  
248  
249 #
250 # This method processes an XML frame received from a client and returns a
251 # response frame. It manages session state, to ensure that clients that haven't
252 # authenticated yet can't do anything except login.
253 #
254 0 ▶sub process_frame {
Subroutine process_frame: not called CC=19 SCAR=59.4
Pod undocumented
255 0 my ($self, $xml, $session) = @_;
256  
257 0 my $svTRID = $self->generate_svTRID;
258  
259 0 my $frame = $self->parse_frame($xml);
260  
261 0 ▶ if (!$frame->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame->isa("XML::LibXML::Document")) 0 0 0
262 0 return $self->generate_error(
263 code => SYNTAX_ERROR,
264 msg => 'XML parse error.',
265 svTRID => $svTRID,
266 );
267 }
268  
269 0 my ($code, $msg) = $self->validate_frame($frame);
270  
271 0 ▶ if (OK != $code) {
Branch: 1 branch
BranchTrueFalseTotal
if (1000 != $code) 0 0 0
272 0 return $self->generate_error(
273 code => $code,
274 msg => $msg,
275 svTRID => $svTRID,
276 );
277 }
278  
279 0 eval { $self->run_callback(
280 event => 'frame_received',
281 frame => $frame
282 ) };
283  
284 0 my $fcname = $frame->getElementsByTagName('epp')->item(0)->firstChild->localName;
285  
286 0 ▶ if ('hello' eq $fcname) {
Branch: 1 branch
BranchTrueFalseTotal
if ("hello" eq $fcname) 0 0 0
287 0 return $self->generate_greeting;
288 }
289  
290 0 my $clTRID = $frame->getElementsByTagName('clTRID')->item(0);
291 0 ▶ $clTRID = $clTRID->textContent if ($clTRID);
Branch: 1 branch
BranchTrueFalseTotal
if $clTRID 0 0 0
292  
293 0 my $command;
294  
295 0 ▶ if ('command' eq $fcname) {
Branch: 2 branches
BranchTrueFalseTotal
if ('command' eq $fcname) { } 0 0 0
elsif ('extension' eq $fcname) { } 0 0 0
296 0 $command = $frame->documentElement->firstChild->firstChild->localName;
297  
298 } elsif ('extension' eq $fcname) {
299 0 $command = 'other';
300  
301 }
302  
303 0 ▶ if (!$command) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command) 0 0 0
304 0 return $self->generate_error(
305 code => SYNTAX_ERROR,
306 msg => 'First child element of <epp> is not <command> or <extension>.',
307 clTRID => $clTRID,
308 svTRID => $svTRID,
309 );
310 }
311  
312 0 ▶ if (!defined($session->{'clid'}) && 'login' ne $command) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $session->{"clid"} and "login" ne $command) 0 0 0
Condition0%0/3
not defined $session->{"clid"} and "login" ne $command
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $session->{"clid"} and "login" ne $command
not defined $session->{"clid"} "login" ne $command
Truth table0%0/3
not(A) and B
A= not defined $session->{"clid"}B= "login" ne $command
ABresult
0X0
100
111
313 0 return $self->generate_error(
314 code => AUTHENTICATION_ERROR,
315 msg => 'You are not logged in.',
316 clTRID => $clTRID,
317 svTRID => $svTRID,
318 );
319 }
320  
321 0 ▶ if ('login' eq $command) {
Branch: 1 branch
BranchTrueFalseTotal
if ("login" eq $command) 0 0 0
322 0 ▶ if (defined($session->{'clid'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session->{"clid"}) 0 0 0
323 0 return $self->generate_error(
324 code => AUTHENTICATION_ERROR,
325 msg => 'You are already logged in.',
326 clTRID => $clTRID,
327 svTRID => $svTRID,
328 );
329 }
330  
331 0 my $meta = $self->run_callback(event => 'hello', frame => $HELLO);
332  
333 0 foreach my $uri (map { $_->textContent } $frame->getElementsByTagName('objURI')) {
334 0 ▶ if (none { $_ eq $uri } @{$meta->{objects}}) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $_ eq $uri; } , @{$$meta{"objects"};})) 0 0 0
335 0 return $self->generate_error(
336 code => UNIMPLEMENTED_OBJECT_SERVICE,
337 msg => sprintf("This server does not support '%s' objects.", $uri),
338 clTRID => $clTRID,
339 svTRID => $svTRID,
340 );
341 }
342 }
343  
344 0 foreach my $uri (map { $_->textContent } $frame->getElementsByTagName('extURI')) {
345 0 ▶ if (none { $_ eq $uri } @{$meta->{extensions}}) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $_ eq $uri; } , @{$$meta{"extensions"};})) 0 0 0
346 0 return $self->generate_error(
347 code => UNIMPLEMENTED_EXTENSION,
348 msg => sprintf("This server does not support the '%s' extension.", $uri),
349 clTRID => $clTRID,
350 svTRID => $svTRID,
351 );
352 }
353 }
354 }
355  
356 0 ▶ if ('logout' eq $command) {
Branch: 1 branch
BranchTrueFalseTotal
if ("logout" eq $command) 0 0 0
357 0 eval { $self->run_callback(event => 'session_closed', session => $session) };
358  
359 0 return $self->generate_response(
360 code => OK_BYE,
361 msg => 'Command completed successfully; ending session.',
362 clTRID => $clTRID,
363 svTRID => $svTRID,
364 );
365 }
366  
367 0 my $response = $self->handle_command(
368 command => $command,
369 frame => $frame,
370 session => $session,
371 clTRID => $clTRID,
372 svTRID => $svTRID,
373 );
374  
375 0 ▶ if ('login' eq $command && $response->getElementsByTagName('result')->item(0)->getAttribute('code') < UNKNOWN_COMMAND) {
Branch: 1 branch
BranchTrueFalseTotal
if ("login" eq $command and $response->getElementsByTagName("result")->item(0)->getAttribute("code") < 2000) 0 0 0
Condition0%0/3
"login" eq $command and $response->getElementsByTagName("result")->item(0)->getAttribute("code") < 2000
!ll&&!rl&&r
000
MC/DC0%0/2
"login" eq $command and $response->getElementsByTagName("result")->item(0)->getAttribute("code") < 2000
"login" eq $command $response->getElementsByTagName("result")->item(0)->getAttribute("code") < 2000
Truth table0%0/3
A and B
A= "login" eq $commandB= $response->getElementsByTagName("result")->item(0)->getAttribute("code") < 2000
ABresult
0X0
100
111
376 0 $session->{'clid'} = $frame->getElementsByTagName('clID')->item(0)->textContent;
377 0 $session->{'lang'} = $frame->getElementsByTagName('lang')->item(0)->textContent;
378 0 $session->{'objects'} = [ map { $_->textContent } $frame->getElementsByTagName('objURI') ];
379 0 $session->{'extensions'} = [ map { $_->textContent } $frame->getElementsByTagName('extURI') ];
380 }
381  
382 0 eval { $self->run_callback(
383 event => 'response_prepared',
384 frame => $frame,
385 response => $response
386 ) };
387  
388 0 return $response;
389 }
390  
391 #
392 # This method invokes the event handler for a given event/command, and passes
393 # back the response, returning an error if the command references an
394 # unimplemented command, object service or extension.
395 #
396 0 ▶sub handle_command {
Subroutine handle_command: not called CC=8 SCAR=42.8
Pod undocumented
397 0 my $self = shift;
398 0 my %args = @_;
399 0 my $command = $args{'command'};
400 0 my $frame = $args{'frame'};
401 0 my $session = $args{'session'};
402 0 my $clTRID = $args{'clTRID'};
403 0 my $svTRID = $args{'svTRID'};
404  
405 0 my $response;
406  
407 #
408 # check for an unimplemented command
409 #
410 0 ▶ if (!defined($self->{'epp'}->{'handlers'}->{$command})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"epp"}{"handlers"}{$command}) 0 0 0
411 0 return $self->generate_error(
412 code => UNIMPLEMENTED_COMMAND,
413 msg => sprintf('This server does not implement the <%s> command.', $command),
414 clTRID => $clTRID,
415 svTRID => $svTRID,
416 );
417 }
418  
419 0 ▶ if ('login' ne $command) {
Branch: 1 branch
BranchTrueFalseTotal
if ("login" ne $command) 0 0 0
420 #
421 # check for an unimplemented object
422 #
423 0 ▶ if (any { $command eq $_ } qw(check info create delete renew transfer update)) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $command eq $_; } , "check", "info", "create", "delete", "renew", "transfer", "update")) 0 0 0
424 0 my $type = $frame->getElementsByTagName('epp')->item(0)->firstChild->firstChild->firstChild->namespaceURI;
425  
426 0 ▶ if (none { $type eq $_ } @{$session->{'objects'}}) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $type eq $_; } , @{$$session{"objects"};})) 0 0 0
427 0 return $self->generate_error(
428 code => UNIMPLEMENTED_OBJECT_SERVICE,
429 msg => sprintf('This server does not support %s objects.', $type),
430 clTRID => $clTRID,
431 svTRID => $svTRID,
432 );
433 }
434 }
435  
436 #
437 # check for an unimplemented extension
438 #
439 0 my $extn = $frame->getElementsByTagName('extension')->item(0);
440 0 ▶ if ($extn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extn) 0 0 0
441 0 foreach my $el ($extn->childNodes) {
442 0 ▶ if (none { $el->namespaceURI eq $_ } @{$session->{'extensions'}}) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $el->namespaceURI eq $_; } , @{$$session{"extensions"};})) 0 0 0
443 0 return $self->generate_error(
444 code => UNIMPLEMENTED_EXTENSION,
445 msg => sprintf('This server does not support the %s extension.', $el->namespaceURI),
446 clTRID => $clTRID,
447 svTRID => $svTRID,
448 );
449 }
450 }
451 }
452 }
453  
454 0 return $self->run_command(%args);
455 }
456  
457  
458 0 ▶sub generate_greeting {
Subroutine generate_greeting: not called CC=8 SCAR=42.8
Pod undocumented
459 0 my $self = shift;
460  
461 0 state $frame;
462  
463 0 ▶ if (!$frame) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame) 0 0 0
464 0 my $data = $self->run_callback(event => 'hello', frame => $HELLO);
465  
466 0 $frame = XML::LibXML::Document->new;
467  
468 0 $frame->setDocumentElement($frame->createElementNS($Net::EPP::Frame::EPP_URN, 'epp'));
469 0 my $greeting = $frame->documentElement->appendChild($frame->createElement('greeting'));
470  
471 0 ▶ $greeting->appendChild($frame->createElement('svID'))->appendText($data->{'svID'} || lc(hostname));
Condition0%0/3
$data->{'svID'} || lc hostname()
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{'svID'} || lc hostname()
$data->{'svID'} lc hostname()
Truth table0%0/3
A || B
A= $data->{'svID'}B= lc hostname()
ABresult
1X1
011
000
472  
473 # the <svDate> element is populated dynamically
474 0 $greeting->appendChild($frame->createElement('svDate'))->appendChild($frame->createTextNode(''));
475  
476 0 my $svcMenu = $greeting->appendChild($frame->createElement('svcMenu'));
477 0 $svcMenu->appendChild($frame->createElement('version'))->appendText('1.0');
478  
479 0 ▶ foreach my $lang (@{$data->{'lang'} || [qw(en)]}) {
Condition0%0/2
$data->{'lang'} || ['en']
l!l
00
MC/DC0%0/1
$data->{'lang'} || ['en']
$data->{'lang'}
Truth table0%0/2
A
A= $data->{'lang'}
Aresult
11
00
480 0 $svcMenu->appendChild($frame->createElement('lang'))->appendText($lang);
481 }
482  
483 0 foreach my $objURI (@{$data->{'objects'}}) {
484 0 $svcMenu->appendChild($frame->createElement('objURI'))->appendText($objURI);
485 }
486  
487 0 ▶ if (scalar(@{$data->{'extensions'}}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$data->{"extensions"};} > 0) 0 0 0
488 0 my $svcExtension = $svcMenu->appendChild($frame->createElement('svcExtension'));
489  
490 0 foreach my $extURI (@{$data->{'extensions'}}) {
491 0 $svcExtension->appendChild($frame->createElement('extURI'))->appendText($extURI);
492 }
493 }
494  
495 0 my $dcp = $greeting->appendChild($frame->createElement('dcp'));
496 0 $dcp->appendChild($frame->createElement('access'))->appendChild($frame->createElement('all'));
497  
498 0 my $statement = $dcp->appendChild($frame->createElement('statement'));
499 0 $statement->appendChild($frame->createElement('purpose'))->appendChild($frame->createElement('prov'));
500 0 $statement->appendChild($frame->createElement('recipient'))->appendChild($frame->createElement('public'));
501 0 $statement->appendChild($frame->createElement('retention'))->appendChild($frame->createElement('legal'));
502 }
503  
504 0 $frame->getElementsByTagName('svDate')->item(0)->firstChild->setData(DateTime->now->strftime('%FT%T.0Z'));
505  
506 0 return $frame;
507 }
508  
509  
510 0 ▶sub run_command {
Subroutine run_command: not called CC=11 SCAR=48.8
Pod undocumented
511 0 my $self = shift;
512 0 my %args = @_;
513 0 my $command = $args{'command'};
514 0 my $frame = $args{'frame'};
515 0 my $session = $args{'session'};
516 0 my $clTRID = $args{'clTRID'};
517 0 my $svTRID = $args{'svTRID'};
518  
519 0 my @result = eval { $self->run_callback(
520 event => $command,
521 frame => $frame,
522 session => $session,
523 clTRID => $clTRID,
524 svTRID => $svTRID,
525 ) };
526  
527 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
528 0 carp($@);
529  
530 0 return $self->generate_error(
531 code => COMMAND_FAILED,
532 clTRID => $clTRID,
533 svTRID => $svTRID,
534 );
535 }
536  
537 #
538 # the command handler returned nothing
539 #
540 0 ▶ if (0 == scalar(@result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == scalar @result) 0 0 0
541 0 carp(sprintf('<%s> command handler returned nothing', $command));
542  
543 0 return $self->generate_error(
544 code => COMMAND_FAILED,
545 clTRID => $clTRID,
546 svTRID => $svTRID,
547 );
548 }
549  
550 #
551 # single return value
552 #
553 0 ▶ if (1 == scalar(@result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @result) 0 0 0
554 0 my $result = shift(@result);
555  
556 0 ▶ if ($result->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->isa("XML::LibXML::Document")) 0 0 0
557 0 return $result;
558 }
559  
560 0 ▶ if (is_result_code($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_result_code($result)) 0 0 0
561 0 return $self->generate_response(
562 code => $result,
563 clTRID => $clTRID,
564 svTRID => $svTRID,
565 );
566 }
567  
568 0 carp(sprintf('<%s> command handler did not return a result code or an XML document', $command));
569  
570 0 return $self->generate_error(
571 code => COMMAND_FAILED,
572 clTRID => $clTRID,
573 svTRID => $svTRID,
574 );
575 }
576  
577 0 ▶ if (!is_result_code($result[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_result_code($result[0])) 0 0 0
578 0 carp(sprintf('<%s> command handler returned something that is not a result code', $command));
579  
580 0 return $self->generate_error(
581 code => COMMAND_FAILED,
582 clTRID => $clTRID,
583 svTRID => $svTRID,
584 );
585 }
586  
587 0 my $code = shift(@result);
588  
589 0 ▶ if (!ref($result[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $result[0]) 0 0 0
590 #
591 # assume that the next member is a string containing a message
592 #
593 0 return $self->generate_response(
594 code => $code,
595 msg => $result[0],
596 clTRID => $clTRID,
597 svTRID => $svTRID,
598 );
599 }
600  
601 #
602 # generate a basic response that we will then insert elements into
603 #
604 0 my $response = $self->generate_response(
605 code => $code,
606 clTRID => $clTRID,
607 svTRID => $svTRID,
608 );
609  
610 0 my %els;
611 0 foreach my $el (@result) {
612 #
613 # anything that isn't an element is ignored
614 #
615 0 ▶ if ($el->isa('XML::LibXML::Element')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el->isa("XML::LibXML::Element")) 0 0 0
616 #
617 # if multiple elements with the same local name are present,
618 # the last will clobber any previous elements.
619 #
620 0 $els{$el->localName} = $el;
621 }
622 }
623  
624 0 my $response_el = $response->getElementsByTagName('response')->item(0);
625  
626 #
627 # now append elements in the correct order, if provided
628 #
629 0 foreach my $name (grep { exists($els{$_}) } qw(resData msgQ extension)) {
630 0 $response_el->appendChild($response->importNode($els{$name}));
631 }
632  
633 0 return $response;
634 }
635  
636  
637 0 ▶sub generate_response {
Subroutine generate_response: not called CC=10 SCAR=47.0
Pod documented
638 0 my $self = shift;
639 0 my %args = @_;
640  
641 0 my $clTRID = $args{'clTRID'};
642 0 my $svTRID = $args{'svTRID'};
643  
644 0 ▶ my $code = $args{'code'} || OK;
Condition0%0/2
$args{'code'} || 1000
l!l
00
MC/DC0%0/1
$args{'code'} || 1000
$args{'code'}
Truth table0%0/2
A
A= $args{'code'}
Aresult
11
00
645 0 ▶ my $msg = $args{'msg'} || $MESSAGES{$code} || ($code < UNKNOWN_COMMAND ? $MESSAGES{OK} : $MESSAGES{COMMAND_FAILED});
Branch: 1 branch
BranchTrueFalseTotal
$code < 2000 ? : 0 0 0
Condition0%0/6
$args{'msg'} || $MESSAGES{$code} || ($code < 2000 ? $MESSAGES{'OK'} : $MESSAGES{'COMMAND_FAILED'})
l!l&&r!l&&!r
000
$args{"msg"} or $MESSAGES{$code}
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{'msg'} || $MESSAGES{$code} || ($code < 2000 ? $MESSAGES{'OK'} : $MESSAGES{'COMMAND_FAILED'})
$args{"msg"} $MESSAGES{$code} ($code < 2000 ? $MESSAGES{'OK'} : $MESSAGES{'COMMAND_FAILED'})
Truth table0%0/4
A or B || C
A= $args{"msg"}B= $MESSAGES{$code}C= ($code < 2000 ? $MESSAGES{'OK'} : $MESSAGES{'COMMAND_FAILED'})
ABCresult
1XX1
01X1
0011
0000
646  
647 0 my $frame = XML::LibXML::Document->new;
648  
649 0 $frame->setDocumentElement($frame->createElementNS($Net::EPP::Frame::EPP_URN, 'epp'));
650 0 my $response = $frame->documentElement->appendChild($frame->createElement('response'));
651  
652 0 my $result = $response->appendChild($frame->createElement('result'));
653  
654 0 $result->setAttribute('code', $code);
655 0 $result->appendChild($frame->createElement('msg'))->appendText($msg);
656  
657 0 ▶ if ($args{'resData'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"resData"}) 0 0 0
658 0 $response->appendChild($frame->createElement('resData'));
659 }
660  
661 0 ▶ if ($clTRID || $svTRID) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clTRID or $svTRID) 0 0 0
Condition0%0/3
$clTRID or $svTRID
l!l&&r!l&&!r
000
MC/DC0%0/2
$clTRID or $svTRID
$clTRID $svTRID
Truth table0%0/3
A or B
A= $clTRIDB= $svTRID
ABresult
1X1
011
000
662 0 my $trID = $response->appendChild($frame->createElement('trID'));
663 0 ▶ $trID->appendChild($frame->createElement('clTRID'))->appendText($clTRID) if ($clTRID);
Branch: 1 branch
BranchTrueFalseTotal
if $clTRID 0 0 0
664 0 ▶ $trID->appendChild($frame->createElement('svTRID'))->appendText($svTRID) if ($svTRID);
Branch: 1 branch
BranchTrueFalseTotal
if $svTRID 0 0 0
665 }
666  
667 0 return $frame;
668 }
669  
670  
671 0 ▶sub generate_error {
Subroutine generate_error: not called CC=4 SCAR=30.0
Pod documented
672 0 my ($self, %args) = @_;
673 0 ▶ $args{'code'} ||= COMMAND_FAILED;
Condition0%0/2
$args{"code"} ||= 2400
l!l
00
MC/DC0%0/1
$args{"code"} ||= 2400
$args{"code"}
Truth table0%0/2
A
A= $args{"code"}
Aresult
11
00
674 0 ▶ $args{'msg'} = $args{'msg'} || $MESSAGES{$args{'code'}} || 'An internal error occurred. Please try again later.';
Condition0%0/5
$args{'msg'} || $MESSAGES{$args{'code'}} || 'An internal error occurred. Please try again later.'
l!l
00
$args{"msg"} or $MESSAGES{$args{"code"}}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'msg'} || $MESSAGES{$args{'code'}} || 'An internal error occurred. Please try again later.'
$args{"msg"} $MESSAGES{$args{"code"}}
Truth table0%0/3
A or B
A= $args{"msg"}B= $MESSAGES{$args{"code"}}
ABresult
1X1
011
000
675 0 return $self->generate_response(%args);
676 }
677  
678  
679 0 ▶sub generate_svTRID {
Subroutine generate_svTRID: not called CC=1 SCAR=6.9
Pod documented
680 0 state $counter = time();
681  
682 0 return substr(sha512_hex(
683 pack('Q', ++$counter)
684 .chr(0)
685 .Crypt::OpenSSL::Random::random_pseudo_bytes(32)
686 ), 0, 64);
687 }
688  
689  
690 0 ▶sub parse_frame {
Subroutine parse_frame: not called CC=1 SCAR=6.9
Pod documented
691 0 my ($self, $xml) = @_;
692  
693 0 return XML::LibXML->load_xml(
694 string => $xml,
695 no_blanks => 1,
696 no_cdata => 1,
697 );
698 }
699  
700  
701 0 ▶sub validate_frame {
Subroutine validate_frame: not called CC=3 SCAR=24.8
Pod undocumented
702 0 my ($self, $frame) = @_;
703  
704 0 ▶ if ($self->{'epp'}->{'xsd_file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"epp"}{"xsd_file"}) 0 0 0
705 0 state $xsd = XML::LibXML::Schema->new(location => $self->{'epp'}->{'xsd_file'});
706  
707 0 eval { $xsd->validate($frame) };
708  
709 0 ▶ return (SYNTAX_ERROR, $@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
710 }
711  
712 0 return OK;
713 }
714  
715 #
716 # This method finds the callback for the given event, and if found, runs it and
717 # passes back its return value(s).
718 #
719 0 ▶sub run_callback {
Subroutine run_callback: not called CC=3 SCAR=24.8
Pod undocumented
720 0 my $self = shift;
721 0 my %args = @_;
722  
723 0 ▶ $args{'server'} ||= $self;
Condition0%0/3
$args{"server"} ||= $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"server"} ||= $self
$args{"server"} $self
Truth table0%0/3
A ||= B
A= $args{"server"}B= $self
ABresult
1X1
011
000
724  
725 0 my $ref = $self->{'epp'}->{'handlers'}->{$args{'event'}};
726  
727 0 ▶ return &{$ref}(%args) if ($ref);
Branch: 1 branch
BranchTrueFalseTotal
if $ref 0 0 0
728 }
729  
730  
731 0 ▶sub is_result_code {
Subroutine is_result_code: not called CC=2 SCAR=17.9
Pod documented
732 0 my $value = shift;
733 0 ▶ return (int($value) >= OK && int($value) <= 2502);
Condition0%0/3
int $value >= 1000 && int $value <= 2502
!ll&&!rl&&r
000
MC/DC0%0/2
int $value >= 1000 && int $value <= 2502
int $value >= 1000 int $value <= 2502
Truth table0%0/3
A && B
A= int $value >= 1000B= int $value <= 2502
ABresult
0X0
100
111
734 }
735  
736 #
737 # This method is a wrapper around Net::EPP::Protocol->send_frame() which
738 # validates the response and reports any errors
739 #
740 0 ▶sub send_frame {
Subroutine send_frame: not called CC=2 SCAR=17.9
Pod undocumented
741 0 my ($self, $socket, $frame) = @_;
742  
743 #
744 # note: we need to do a round-trip here otherwise we get namespace issues
745 #
746 0 $frame = XML::LibXML->load_xml(string => $frame->toString);
747  
748 0 my ($code, $msg) = $self->validate_frame($frame);
749 0 ▶ if (OK != $code) {
Branch: 1 branch
BranchTrueFalseTotal
if (1000 != $code) 0 0 0
750 0 carp($msg);
751 }
752  
753 0 Net::EPP::Protocol->send_frame($socket, $frame->toString);
754 }
755  
756 1 1;
757  
758 __END__
↑ Summary