blib/lib/OpenVAS/OMP.pm

filter: Statementstmt 44.5% 45 / 101 Branchbran 10.5% 4 / 38 Conditioncond 18.4% 7 / 38 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 61.1% 11 / 18 Podpod 100% 4 / 4 totaltotal 31.8% 71 / 223 CC 34 SCAR 34.6
CC
34
Cov
32%
_authenticate
90.0
_readSock
56.0
new
20.2
CRAP 31.8
↑ Summary
1 #!/usr/bin/perl
2 #
3 # Filename: OMP.pm
4 # Description: Pure-Perl interface to the OpenVAS Management Protocol
5 # Creator: Winfried Neessen <wn@neessen.net>
6 #
7 # $Id$
8 #
9 # Last modified: [ 2013-06-12 12:13:43 ]
10  
11 ## This is the OpenVAS::OMP package {{{
12 package OpenVAS::OMP;
13  
14 ### Global modules {{{
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use XML::Simple qw( :strict );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 # }}}
21  
22 ### Global variables {{{
23 1 our $VERSION = '0.04';
24 # }}}
25  
26 ### Constants definitions {{{
27 1 ▶use constant CHUNK_SIZE => 8192;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant CONN_TIMEOUT => 30;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use constant DEFAULT_PORT => 9390;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use constant DEFAULT_HOST => 'localhost';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use constant DEFAULT_SSL_VERIFY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 # }}}
33  
34 #### Public methods
35  
36 ### Module constructor // new() {{{
37 sub new
38 1 ▶{
Subroutine new: called CC=12 SCAR=30.1
Pod documented
39  
40 ## Create class and read arguments
41 1 my ( $class, %args ) = @_;
42 1 my $self = bless {}, $class;
43  
44 ## Read arguments
45 1 ▶ $self->{ 'host' } = delete( $args{ 'host' } ) || DEFAULT_HOST;
Condition50%1/2
delete $args{'host'} || 'localhost'
l!l
10
MC/DC0%0/1
delete $args{'host'} || 'localhost'
delete $args{'host'}
Truth table50%1/2
A
A= delete $args{'host'}
Aresult
11
00
46 1 ▶ $self->{ 'timeout' } = delete( $args{ 'timeout' } ) || CONN_TIMEOUT;
Condition50%1/2
delete $args{'timeout'} || 30
l!l
01
MC/DC0%0/1
delete $args{'timeout'} || 30
delete $args{'timeout'}
Truth table50%1/2
A
A= delete $args{'timeout'}
Aresult
11
00
47 1 ▶ $self->{ 'port' } = delete( $args{ 'port' } ) || DEFAULT_PORT;
Condition50%1/2
delete $args{'port'} || 9390
l!l
01
MC/DC0%0/1
delete $args{'port'} || 9390
delete $args{'port'}
Truth table50%1/2
A
A= delete $args{'port'}
Aresult
11
00
48 1 ▶ $self->{ 'ssl_verify' } = delete( $args{ 'ssl_verify' } ) || DEFAULT_SSL_VERIFY;
Condition50%1/2
delete $args{'ssl_verify'} || 0
l!l
10
MC/DC0%0/1
delete $args{'ssl_verify'} || 0
delete $args{'ssl_verify'}
Truth table50%1/2
A
A= delete $args{'ssl_verify'}
Aresult
11
00
49 1 $self->{ 'username' } = delete( $args{ 'username' } );
50 1 $self->{ 'password' } = delete( $args{ 'password' } );
51  
52 ## Check for mantatory arguments
53 croak( 'No host argument given' )
54 1 ▶ unless( defined( $self->{ 'host' } ) and $self->{ 'host' } ne '' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"host"} and $self->{"host"} ne "" 0 1 1
Condition33%1/3
defined $self->{"host"} and $self->{"host"} ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->{"host"} and $self->{"host"} ne ""
defined $self->{"host"} $self->{"host"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"host"}B= $self->{"host"} ne ""
ABresult
0X0
100
111
55 croak( 'No port argument given' )
56 1 ▶ unless( defined( $self->{ 'port' } ) and $self->{ 'port' } ne '' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"port"} and $self->{"port"} ne "" 0 1 1
Condition33%1/3
defined $self->{"port"} and $self->{"port"} ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->{"port"} and $self->{"port"} ne ""
defined $self->{"port"} $self->{"port"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"port"}B= $self->{"port"} ne ""
ABresult
0X0
100
111
57 croak( 'Unsupported value "' . $self->{ 'ssl_verify' } . '" for argument "ssl_verify".' )
58 1 ▶ if( $self->{ 'ssl_verify' } < 0 or $self->{ 'ssl_verify' } > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ssl_verify"} < 0 or $self->{"ssl_verify"} > 1 0 1 1
Condition33%1/3
$self->{"ssl_verify"} < 0 or $self->{"ssl_verify"} > 1
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"ssl_verify"} < 0 or $self->{"ssl_verify"} > 1
$self->{"ssl_verify"} < 0 $self->{"ssl_verify"} > 1
Truth table33%1/3
A or B
A= $self->{"ssl_verify"} < 0B= $self->{"ssl_verify"} > 1
ABresult
1X1
011
000
59  
60 ## Warn about unrecognized arguments
61 0 ▶ carp( "Unrecognized arguments: @{ [ sort keys %args ] }" ) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 1 1
62  
63 ## Return object
64 1 return $self;
65  
66 }
67 # }}}
68  
69 ### Send a request (hashref) to the OMP and return a XML hashref (or raw XML) // commandHash() {{{
70 sub commandHash
71 0 ▶{
Subroutine commandHash: not called CC=3 SCAR=24.8
Pod documented
72  
73 ## Get object and arguments
74 0 my ( $self, $cmdHash, $raw ) = @_;
75  
76 ## cmdHash needs to be a hash/hashref
77 0 ▶ croak( 'Method "commandHash()" requires the first argument to be a hash/hashref' )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cmdHash eq "HASH" 0 0 0
78 unless( ref( $cmdHash ) eq 'HASH' );
79  
80 ## Convert command hash to XML
81 0 my $cmdXML = XMLout( $cmdHash, NoEscape => 0, SuppressEmpty => 1, KeepRoot => 1, KeyAttr => 'command' );
82  
83 ## Send commandXML to server
84 0 my $response = $self->_sendSock( $cmdXML );
85  
86 ## Close socket connection
87 0 $self->{ 'socket' }->close();
88  
89 ## Return RAW or hashref version
90 0 ▶ if( defined( $raw ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw) { } 0 0 0
91 {
92 0 return $response;
93 } else {
94 0 return XMLin( $response, ForceArray => 1, KeyAttr => 'command_response' );
95 }
96  
97 }
98 # }}}
99  
100 ### Send a request (pure XML) to the OMP and return a XML hashref (or raw XML) // commandXML() {{{
101 sub commandXML
102 0 ▶{
Subroutine commandXML: not called CC=4 SCAR=30.0
Pod documented
103  
104 ## Get object and arguments
105 0 my ( $self, $cmdXML, $raw ) = @_;
106  
107 ## cmdHash needs to be a hash/hashref
108 0 ▶ croak( 'Method "commandXML()" requires the first argument to be a hash/hashref' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmdXML and ref $cmdXML eq "" 0 0 0
Condition0%0/3
defined $cmdXML and ref $cmdXML eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cmdXML and ref $cmdXML eq ""
defined $cmdXML ref $cmdXML eq ""
Truth table0%0/3
A and B
A= defined $cmdXMLB= ref $cmdXML eq ""
ABresult
0X0
100
111
109 unless( defined( $cmdXML ) and ref( $cmdXML ) eq '' );
110  
111 ## Send commandXML to server
112 0 my $response = $self->_sendSock( $cmdXML );
113  
114 ## Close socket connection
115 0 $self->{ 'socket' }->close();
116  
117 ## Return RAW or hashref version
118 0 ▶ if( defined( $raw ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw) { } 0 0 0
119 {
120 0 return $response;
121 } else {
122 0 return XMLin( $response, ForceArray => 1, KeyAttr => 'command_response' );
123 }
124  
125 }
126 # }}}
127  
128 ### Request version string from OMP server // getVersion() {{{
129 sub getVersion
130 0 ▶{
Subroutine getVersion: not called CC=3 SCAR=24.8
Pod documented
131  
132 ## Get object
133 0 my $self = shift;
134  
135 ## Connect and authenticate with OMP server
136 0 $self->_connect();
137  
138 ## Send commandXML to server
139 0 $self->{ 'socket' }->syswrite( '<get_version/>' );
140 0 my $response = XMLin( $self->_readSock, ForceArray => 1, KeyAttr => 'command_response' );
141  
142 ## Check respone
143 croak( 'getVersion failed: ' . $response->{ 'status_text' } )
144 0 ▶ unless( defined( $response->{ 'status' } ) and $response->{ 'status' } eq '200' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response->{"status"} and $response->{"status"} eq 200 0 0 0
Condition0%0/3
defined $response->{"status"} and $response->{"status"} eq 200
!ll&&!rl&&r
000
MC/DC0%0/2
defined $response->{"status"} and $response->{"status"} eq 200
defined $response->{"status"} $response->{"status"} eq 200
Truth table0%0/3
A and B
A= defined $response->{"status"}B= $response->{"status"} eq 200
ABresult
0X0
100
111
145
146 ## Return response
147 0 print STDERR 'OMP server version: ' . $response->{ 'version' }->[0] . "\n";
148 0 return $response->{ 'version' }->[0];
149  
150 }
151 # }}}
152  
153  
154 #### Private methods
155  
156 ### Initiate a SSL connection with the server // _connect() {{{
157 sub _connect
158 0 ▶{
Subroutine _connect: not called CC=2 SCAR=17.9
159  
160 ## Get object
161 0 my $self = shift;
162  
163 ## Create a SSL socket
164 my $socket = IO::Socket::SSL->new
165 (
166  
167 PeerHost => $self->{ 'host' },
168 PeerPort => $self->{ 'port' },
169  
170 Timeout => $self->{ 'timeout' },
171 Proto => 'tcp',
172  
173 SSL_verify_mode => $self->{ 'ssl_verify' },
174
175 0 ▶ ) or croak( 'Unable to connect to host ' . $self->{ 'host' } . ':' . $self->{ 'port' } . ': ' . &IO::Socket::SSL::errstr );
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::SSL"->new("PeerHost", $self->{"host"}, "PeerPort", $self->{"port"}, "Timeout", $self->{"timeout"}, "Proto", "tcp", "SSL_verify_mode", $self->{"ssl_verify"}) or croak('Unable to connect to host ' . $self->{"host"} . ':' . $self->{"port"} . ': ' . &IO::Socket::SSL::errstr) 0 0 0
176  
177 ## Reference socket in the object
178 0 $self->{ 'socket' } = \*$socket;
179  
180 }
181 # }}}
182  
183 ### Authenticate with the OMP server // _authenticate() {{{
184 sub _authenticate
185 0 ▶{
Subroutine _authenticate: not called CC=9 SCAR=45.0
186  
187 ## Get object
188 0 my $self = shift;
189  
190 ## Make sure the everything required is available
191 croak( 'Not connected with the OMP server' )
192 0 ▶ unless( defined( $self->{ 'socket' } ) and ref( $self->{ 'socket' } ) eq 'IO::Socket::SSL' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"socket"} and ref $self->{"socket"} eq "IO::Socket::SSL" 0 0 0
Condition0%0/3
defined $self->{"socket"} and ref $self->{"socket"} eq "IO::Socket::SSL"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"socket"} and ref $self->{"socket"} eq "IO::Socket::SSL"
defined $self->{"socket"} ref $self->{"socket"} eq "IO::Socket::SSL"
Truth table0%0/3
A and B
A= defined $self->{"socket"}B= ref $self->{"socket"} eq "IO::Socket::SSL"
ABresult
0X0
100
111
193 croak( 'Username or password not provided' )
194 0 ▶ unless( defined( $self->{ 'username' } ) and defined( $self->{ 'password' } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"username"} and defined $self->{"password"} 0 0 0
Condition0%0/3
defined $self->{"username"} and defined $self->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"username"} and defined $self->{"password"}
defined $self->{"username"} defined $self->{"password"}
Truth table0%0/3
A and B
A= defined $self->{"username"}B= defined $self->{"password"}
ABresult
0X0
100
111
195  
196 ## Generate XML authentication string
197 0 my ( $auth, $authXML );
198 0 $auth->{ 'authenticate' }->{ 'credentials' } = [ { 'username' => [ $self->{ 'username' } ], 'password' => [ $self->{ 'password' } ] } ];
199 0 $authXML = XMLout( $auth, NoEscape => 0, SuppressEmpty => 1, KeepRoot => 1, KeyAttr => 'authenticate' );
200  
201 ## Send authentication string to OMP server and read response
202 0 $self->{ 'socket' }->syswrite( $authXML );
203 0 my $response = $self->_readSock;
204  
205 ## Check repsonse
206 0 my $authResponse = XMLin( $response, ForceArray => 1, KeyAttr => 'authenticate_response' );
207 0 ▶ if( defined( $authResponse->{ 'status' } ) and $authResponse->{ 'status' } eq '200' )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $authResponse->{'status'} and $authResponse->{'status'} eq '200') { } 0 0 0
elsif (defined $authResponse->{'status'} and $authResponse->{'status'} ne '200') { } 0 0 0
Condition0%0/3
defined $authResponse->{"status"} and $authResponse->{"status"} eq 200
!ll&&!rl&&r
000
MC/DC0%0/2
defined $authResponse->{"status"} and $authResponse->{"status"} eq 200
defined $authResponse->{"status"} $authResponse->{"status"} eq 200
Truth table0%0/3
A and B
A= defined $authResponse->{"status"}B= $authResponse->{"status"} eq 200
ABresult
0X0
100
111
208 {
209 0 return 1;
210 }
211 ▶ elsif( defined( $authResponse->{ 'status' } ) and $authResponse->{ 'status' } ne '200' )
Condition0%0/3
defined $authResponse->{"status"} and $authResponse->{"status"} ne 200
!ll&&!rl&&r
000
MC/DC0%0/2
defined $authResponse->{"status"} and $authResponse->{"status"} ne 200
defined $authResponse->{"status"} $authResponse->{"status"} ne 200
Truth table0%0/3
A and B
A= defined $authResponse->{"status"}B= $authResponse->{"status"} ne 200
ABresult
0X0
100
111
212 {
213 0 carp( 'Error: ' . $authResponse->{ 'status_text' } );
214 0 return undef;
215 }
216 else
217 {
218 0 carp( 'Unexpected failure.' );
219 0 return undef;
220 }
221  
222 }
223 # }}}
224  
225 ### Send date to socket // _sendSock() {{{
226 sub _sendSock
227 0 ▶{
Subroutine _sendSock: not called CC=1 SCAR=6.9
228  
229 ## Get object
230 0 my ( $self, $cmdXML ) = @_;
231  
232 ## Connect and authenticate with OMP server
233 0 $self->_connect();
234 0 $self->_authenticate();
235  
236 ## Send commandXML to server
237 0 $self->{ 'socket' }->syswrite( $cmdXML );
238 0 my $response = $self->_readSock;
239  
240 ## Return the server response
241 0 return $response;
242  
243 }
244 # }}}
245  
246 ### Read from socket // _readSock() {{{
247 sub _readSock
248 0 ▶{
Subroutine _readSock: not called CC=7 SCAR=40.3
249  
250 ## Get object
251 0 my $self = shift;
252  
253 ## Make sure we are connected to the OMP server
254 croak( 'Not connected with the OMP server' )
255 0 ▶ unless( defined( $self->{ 'socket' } ) and ref( $self->{ 'socket' } ) eq 'IO::Socket::SSL' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"socket"} and ref $self->{"socket"} eq "IO::Socket::SSL" 0 0 0
Condition0%0/3
defined $self->{"socket"} and ref $self->{"socket"} eq "IO::Socket::SSL"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"socket"} and ref $self->{"socket"} eq "IO::Socket::SSL"
defined $self->{"socket"} ref $self->{"socket"} eq "IO::Socket::SSL"
Truth table0%0/3
A and B
A= defined $self->{"socket"}B= ref $self->{"socket"} eq "IO::Socket::SSL"
ABresult
0X0
100
111
256  
257 ## Read from socket
258 0 my ( $length, $response );
259 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$length and do { do { $length = $self->{"socket"}->sysread(my $buffer, 8192); undef $length if $length == 0; undef $length if $length < 8192; $response .= $buffer if defined $buffer } } 0 0 0
260  
261 0 $length = $self->{ 'socket' }->sysread( my $buffer, CHUNK_SIZE );
262 0 ▶ undef $length if( $length == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $length == 0 0 0 0
263 0 ▶ undef $length if( $length < CHUNK_SIZE );
Branch: 1 branch
BranchTrueFalseTotal
if $length < 8192 0 0 0
264  
265 0 ▶ $response .= $buffer if defined( $buffer );
Branch: 1 branch
BranchTrueFalseTotal
if defined $buffer 0 0 0
266  
267 } while ( $length );
268  
269 ## Return the response
270 0 return $response;
271  
272 }
273 # }}}
274  
275  
276  
277 1 1;
278 __END__
↑ Summary