blib/lib/POE/Component/Server/NSCA.pm

filter: Statementstmt 85.9% 293 / 341 Branchbran 47.4% 37 / 78 Conditioncond 40.0% 4 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 95.5% 64 / 67 Podpod 100% 6 / 6 totaltotal 79.5% 404 / 508 SCAR 44.8
CC
55
Cov
78%
_decrypt_mcrypt
42.0
unregister
30.0
spawn
10.8
CRAP 87.8
↑ Summary
1 package POE::Component::Server::NSCA;
2 4 $POE::Component::Server::NSCA::VERSION = '0.10';
3 #ABSTRACT: a POE Component that implements NSCA daemon functionality
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Net::Netmask;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Math::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Stream);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use constant MAX_INPUT_BUFFER => 2048 ; # /* max size of most buffers we use */
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use constant MAX_HOST_ADDRESS_LENGTH => 256 ; # /* max size of a host address */
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use constant MAX_HOSTNAME_LENGTH => 64 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use constant MAX_DESCRIPTION_LENGTH => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use constant OLD_PLUGINOUTPUT_LENGTH => 512;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use constant MAX_PLUGINOUTPUT_LENGTH => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use constant MAX_PASSWORD_LENGTH => 512;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use constant TRANSMITTED_IV_SIZE => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use constant SIZEOF_U_INT32_T => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use constant SIZEOF_INT16_T => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use constant SIZEOF_INIT_PACKET => TRANSMITTED_IV_SIZE + SIZEOF_U_INT32_T;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 4 ▶use constant PROBABLY_ALIGNMENT_ISSUE => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 4 ▶use constant SIZEOF_DATA_PACKET => SIZEOF_INT16_T + SIZEOF_U_INT32_T + SIZEOF_U_INT32_T + SIZEOF_INT16_T + MAX_HOSTNAME_LENGTH + MAX_DESCRIPTION_LENGTH + MAX_PLUGINOUTPUT_LENGTH + PROBABLY_ALIGNMENT_ISSUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use constant SIZEOF_OLD_PACKET => SIZEOF_INT16_T + SIZEOF_U_INT32_T + SIZEOF_U_INT32_T + SIZEOF_INT16_T + MAX_HOSTNAME_LENGTH + MAX_DESCRIPTION_LENGTH + OLD_PLUGINOUTPUT_LENGTH + PROBABLY_ALIGNMENT_ISSUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 4 ▶use constant ENCRYPT_NONE => 0 ; # /* no encryption */
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use constant ENCRYPT_XOR => 1 ; # /* not really encrypted, just obfuscated */
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use constant ENCRYPT_DES => 2 ; # /* DES */
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use constant ENCRYPT_3DES => 3 ; # /* 3DES or Triple DES */
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use constant ENCRYPT_CAST128 => 4 ; # /* CAST-128 */
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 ▶use constant ENCRYPT_CAST256 => 5 ; # /* CAST-256 */
Subroutine BEGIN: called CC=1 SCAR=0.0
36 4 ▶use constant ENCRYPT_XTEA => 6 ; # /* xTEA */
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use constant ENCRYPT_3WAY => 7 ; # /* 3-WAY */
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use constant ENCRYPT_BLOWFISH => 8 ; # /* SKIPJACK */
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use constant ENCRYPT_TWOFISH => 9 ; # /* TWOFISH */
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use constant ENCRYPT_LOKI97 => 10 ; # /* LOKI97 */
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use constant ENCRYPT_RC2 => 11 ; # /* RC2 */
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use constant ENCRYPT_ARCFOUR => 12 ; # /* RC4 */
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use constant ENCRYPT_RC6 => 13 ; # /* RC6 */ ; # /* UNUSED */
Subroutine BEGIN: called CC=1 SCAR=0.0
44 4 ▶use constant ENCRYPT_RIJNDAEL128 => 14 ; # /* RIJNDAEL-128 */
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 ▶use constant ENCRYPT_RIJNDAEL192 => 15 ; # /* RIJNDAEL-192 */
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶use constant ENCRYPT_RIJNDAEL256 => 16 ; # /* RIJNDAEL-256 */
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶use constant ENCRYPT_MARS => 17 ; # /* MARS */ ; # /* UNUSED */
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶use constant ENCRYPT_PANAMA => 18 ; # /* PANAMA */ ; # /* UNUSED */
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 ▶use constant ENCRYPT_WAKE => 19 ; # /* WAKE */
Subroutine BEGIN: called CC=1 SCAR=0.0
50 4 ▶use constant ENCRYPT_SERPENT => 20 ; # /* SERPENT */
Subroutine BEGIN: called CC=1 SCAR=0.0
51 4 ▶use constant ENCRYPT_IDEA => 21 ; # /* IDEA */ ; # /* UNUSED */
Subroutine BEGIN: called CC=1 SCAR=0.0
52 4 ▶use constant ENCRYPT_ENIGMA => 22 ; # /* ENIGMA (Unix crypt) */
Subroutine BEGIN: called CC=1 SCAR=0.0
53 4 ▶use constant ENCRYPT_GOST => 23 ; # /* GOST */
Subroutine BEGIN: called CC=1 SCAR=0.0
54 4 ▶use constant ENCRYPT_SAFER64 => 24 ; # /* SAFER-sk64 */
Subroutine BEGIN: called CC=1 SCAR=0.0
55 4 ▶use constant ENCRYPT_SAFER128 => 25 ; # /* SAFER-sk128 */
Subroutine BEGIN: called CC=1 SCAR=0.0
56 4 ▶use constant ENCRYPT_SAFERPLUS => 26 ; # /* SAFER+ */
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 4 my $HAVE_MCRYPT = 0;
59 4 eval {
60 4 require Mcrypt;
61 0 $HAVE_MCRYPT++;
62 };
63  
64 # Lookups for loading.
65 4 my %mcrypts = ( ENCRYPT_NONE, "none",
66 ENCRYPT_XOR, "xor",
67 ENCRYPT_DES, "DES",
68 ENCRYPT_3DES, "3DES",
69 ENCRYPT_CAST128, "CAST_128",
70 ENCRYPT_CAST256, "CAST-256",
71 ENCRYPT_XTEA, "XTEA",
72 ENCRYPT_3WAY, "THREEWAY",
73 ENCRYPT_BLOWFISH, "BLOWFISH",
74 ENCRYPT_TWOFISH, "TWOFISH",
75 ENCRYPT_LOKI97, "LOKI97",
76 ENCRYPT_RC2, "RC2",
77 ENCRYPT_ARCFOUR, "ARCFOUR",
78 ENCRYPT_RC6, "RC6",
79 ENCRYPT_RIJNDAEL128, "RIJNDAEL_128",
80 ENCRYPT_RIJNDAEL192, "RIJNDAEL_192",
81 ENCRYPT_RIJNDAEL256, "RIJNDAEL_256",
82 ENCRYPT_MARS, "MARS",
83 ENCRYPT_PANAMA, "PANAMA",
84 ENCRYPT_WAKE, "WAKE",
85 ENCRYPT_SERPENT, "SERPENT",
86 ENCRYPT_IDEA, "IDEA",
87 ENCRYPT_ENIGMA, "ENGIMA",
88 ENCRYPT_GOST, "GOST",
89 ENCRYPT_SAFER64, "SAFER_SK64",
90 ENCRYPT_SAFER128, "SAFER_SK128",
91 ENCRYPT_SAFERPLUS, "SAFERPLUS",
92 );
93  
94 sub spawn {
95 3 ▶ my $package = shift;
Subroutine spawn: called CC=10 SCAR=23.8
Pod documented
96 3 my %opts = @_;
97 3 $opts{lc $_} = delete $opts{$_} for keys %opts;
98 croak "$package requires a 'password' argument\n"
99 3 ▶ unless $opts{password};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"password"} 0 3 3
100 croak "$package requires an 'encryption' argument\n"
101 3 ▶ unless defined $opts{encryption};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"encryption"} 0 3 3
102 croak "'encryption' argument must be a valid numeric\n"
103 3 ▶ unless defined $mcrypts{ $opts{encryption} };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mcrypts{$opts{"encryption"}} 0 3 3
104 3 my $options = delete $opts{options};
105 3 ▶ my $access = delete $opts{access} || [ Net::Netmask->new('any') ];
Condition50%1/2
delete $opts{'access'} || ['Net::Netmask'->new('any')]
l!l
03
MC/DC0%0/1
delete $opts{'access'} || ['Net::Netmask'->new('any')]
delete $opts{'access'}
Truth table50%1/2
A
A= delete $opts{'access'}
Aresult
11
00
106 3 ▶ $access = [ ] unless ref $access eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $access eq "ARRAY" 0 3 3
107 3 foreach my $acl ( @$access ) {
108 3 ▶ next unless $acl->isa('Net::Netmask');
Branch: 1 branch
BranchTrueFalseTotal
unless $acl->isa("Net::Netmask") 0 3 3
109 3 push @{ $opts{access} }, $acl;
110 }
111 3 my $self = bless \%opts, $package;
112 3 ▶ $self->{session_id} = POE::Session->create(
Branch: 1 branch
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 1 2 3
113 object_states => [
114 $self => { shutdown => '_shutdown',
115 },
116 $self => [qw(
117 _start
118 _accept_client
119 _accept_failed
120 _conn_input
121 _conn_error
122 _conn_alarm
123 register
124 unregister
125 )],
126 ],
127 heap => $self,
128 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
129 )->ID();
130 3 return $self;
131 }
132  
133 sub shutdown {
134 3 ▶ my $self = shift;
Subroutine shutdown: called CC=1 SCAR=0.0
Pod documented
135 3 $poe_kernel->post( $self->{session_id}, 'shutdown' );
136 }
137  
138 sub session_id {
139 2 ▶ return $_[0]->{session_id};
Subroutine session_id: called CC=1 SCAR=0.0
Pod documented
140 }
141  
142 sub getsockname {
143 3 ▶ return unless $_[0]->{listener};
Subroutine getsockname: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"listener"} 0 3 3
144 3 return $_[0]->{listener}->getsockname();
145 }
146  
147 sub _start {
148 3 ▶ my ($kernel,$self,$sender) = @_[KERNEL,OBJECT,SENDER];
Subroutine _start: called CC=4 SCAR=14.8
149 3 $self->{session_id} = $_[SESSION]->ID();
150 3 ▶ if ( $self->{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'alias'}) { } 0 3 3
151 0 $kernel->alias_set( $self->{alias} );
152 }
153 else {
154 3 $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ );
155 }
156 #$self->{filter} = POE::Filter::Block->new( BlockSize => SIZEOF_DATA_PACKET );
157 3 $self->{filter} = POE::Filter::Stream->new();
158 $self->{listener} = POE::Wheel::SocketFactory->new(
159 ( defined $self->{address} ? ( BindAddress => $self->{address} ) : () ),
160 3 ▶ ( defined $self->{port} ? ( BindPort => $self->{port} ) : ( BindPort => 5667 ) ),
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'address'} ? : 3 0 3
defined $self->{'port'} ? : 3 0 3
161 SuccessEvent => '_accept_client',
162 FailureEvent => '_accept_failed',
163 SocketDomain => AF_INET, # Sets the socket() domain
164 SocketType => SOCK_STREAM, # Sets the socket() type
165 SocketProtocol => 'tcp', # Sets the socket() protocol
166 Reuse => 'on', # Lets the port be reused
167 );
168 3 return;
169 }
170  
171 sub register {
172 2 ▶ my ($kernel,$self,$sender) = @_[KERNEL,OBJECT,SENDER];
Subroutine register: called CC=6 SCAR=21.7
Pod documented
173 2 my $sender_id = $sender->ID();
174 2 my %args;
175 2 ▶ if ( ref $_[ARG0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[10] eq 'HASH') { } 2 0 2
elsif (ref $_[10] eq 'ARRAY') { } 0 0 0
176 2 %args = %{ $_[ARG0] };
177 }
178 elsif ( ref $_[ARG0] eq 'ARRAY' ) {
179 0 %args = @{ $_[ARG0] };
180 }
181 else {
182 0 %args = @_[ARG0..$#_];
183 }
184 2 $args{lc $_} = delete $args{$_} for keys %args;
185 2 ▶ unless ( $args{event} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"event"}) 0 2 2
186 0 warn "No 'event' argument supplied\n";
187 0 return;
188 }
189 2 ▶ if ( defined $self->{sessions}->{ $sender_id } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'sessions'}{$sender_id}) { } 0 2 2
190 0 $self->{sessions}->{ $sender_id } = \%args;
191 }
192 else {
193 2 $self->{sessions}->{ $sender_id } = \%args;
194 2 $kernel->refcount_increment( $sender_id, __PACKAGE__ );
195 }
196 2 return;
197 }
198  
199 sub unregister {
200 0 ▶ my ($kernel,$self,$sender) = @_[KERNEL,OBJECT,SENDER];
Subroutine unregister: not called CC=5 SCAR=34.0
Pod documented
201 0 my $sender_id = $sender->ID();
202 0 my %args;
203 0 ▶ if ( ref $_[ARG0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[10] eq 'HASH') { } 0 0 0
elsif (ref $_[10] eq 'ARRAY') { } 0 0 0
204 0 %args = %{ $_[ARG0] };
205 }
206 elsif ( ref $_[ARG0] eq 'ARRAY' ) {
207 0 %args = @{ $_[ARG0] };
208 }
209 else {
210 0 %args = @_[ARG0..$#_];
211 }
212 0 $args{lc $_} = delete $args{$_} for keys %args;
213 0 my $data = delete $self->{sessions}->{ $sender_id };
214 0 ▶ $kernel->refcount_decrement( $sender_id, __PACKAGE__ ) if $data;
Branch: 1 branch
BranchTrueFalseTotal
if $data 0 0 0
215 0 return;
216 }
217  
218 sub _shutdown {
219 3 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _shutdown: called CC=4 SCAR=13.9
220 3 delete $self->{listener};
221 3 delete $self->{clients};
222 3 $kernel->refcount_decrement( $_, __PACKAGE__ ) for keys %{ $self->{sessions} };
223 3 $kernel->alarm_remove_all();
224 3 $kernel->alias_remove( $_ ) for $kernel->alias_list();
225 3 ▶ $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 3 0 3
226 3 return;
227 }
228  
229 sub _accept_failed {
230 0 ▶ my ($kernel,$self,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,OBJECT,ARG0..ARG3];
Subroutine _accept_failed: not called CC=1 SCAR=6.9
231 0 warn "Listener: $wheel_id generated $operation error $errnum: $errstr\n";
232 0 delete $self->{listener};
233 0 $kernel->yield( '_shutdown' );
234 0 return;
235 }
236  
237 sub _accept_client {
238 3 ▶ my ($kernel,$self,$socket,$peeraddr,$peerport) = @_[KERNEL,OBJECT,ARG0..ARG2];
Subroutine _accept_client: called CC=4 SCAR=14.0
239 3 my $sockaddr = inet_ntoa( ( unpack_sockaddr_in ( CORE::getsockname $socket ) )[1] );
240 3 my $sockport = ( unpack_sockaddr_in ( CORE::getsockname $socket ) )[0];
241 3 $peeraddr = inet_ntoa( $peeraddr );
242  
243 3 ▶ return unless grep { $_->match( $peeraddr ) } @{ $self->{access} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->match($peeraddr);} @{$$self{"access"};} 0 3 3
244  
245 my $wheel = POE::Wheel::ReadWrite->new(
246 Handle => $socket,
247 Filter => $self->{filter},
248 3 InputEvent => '_conn_input',
249 ErrorEvent => '_conn_error',
250 FlushedEvent => '_conn_flushed',
251 );
252  
253 3 ▶ return unless $wheel;
Branch: 1 branch
BranchTrueFalseTotal
unless $wheel 0 3 3
254  
255 3 my $id = $wheel->ID();
256 3 my $time = time();
257 3 my $iv = join '', random_uniform_integer(128,0,9);
258 3 my $init_packet = $iv . pack 'N', $time;
259 3 $self->{clients}->{ $id } =
260 {
261 wheel => $wheel,
262 peeraddr => $peeraddr,
263 peerport => $peerport,
264 sockaddr => $sockaddr,
265 sockport => $sockport,
266 ts => $time,
267 iv => $iv,
268 };
269 3 ▶ $self->{clients}->{ $id }->{alarm} = $kernel->delay_set( '_conn_alarm', $self->{time_out} || 60, $id );
Condition50%1/2
$self->{'time_out'} || 60
l!l
30
MC/DC0%0/1
$self->{'time_out'} || 60
$self->{'time_out'}
Truth table50%1/2
A
A= $self->{'time_out'}
Aresult
11
00
270 3 $wheel->put( $init_packet );
271 3 return;
272 }
273  
274 sub _conn_exists {
275 5 ▶ my ($self,$wheel_id) = @_;
Subroutine _conn_exists: called CC=3 SCAR=12.5
276 5 ▶ return 0 unless $wheel_id and defined $self->{clients}->{ $wheel_id };
Branch: 1 branch
BranchTrueFalseTotal
unless $wheel_id and defined $self->{"clients"}{$wheel_id} 0 5 5
Condition33%1/3
$wheel_id and defined $self->{"clients"}{$wheel_id}
!ll&&!rl&&r
005
MC/DC0%0/2
$wheel_id and defined $self->{"clients"}{$wheel_id}
$wheel_id defined $self->{"clients"}{$wheel_id}
Truth table33%1/3
A and B
A= $wheel_idB= defined $self->{"clients"}{$wheel_id}
ABresult
0X0
100
111
277 5 return 1;
278 }
279  
280 sub _conn_error {
281 2 ▶ my ($self,$errstr,$id) = @_[OBJECT,ARG2,ARG3];
Subroutine _conn_error: called CC=2 SCAR=7.0
282 2 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 2 2
283 2 delete $self->{clients}->{ $id };
284 2 return;
285 }
286  
287 sub _conn_alarm {
288 1 ▶ my ($kernel,$self,$id) = @_[KERNEL,OBJECT,ARG0];
Subroutine _conn_alarm: called CC=2 SCAR=7.0
289 1 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 1 1
290 1 delete $self->{clients}->{ $id };
291 1 return;
292 }
293  
294 sub _conn_input {
295 2 ▶ my ($kernel,$self,$packet,$id) = @_[KERNEL,OBJECT,ARG0,ARG1];
Subroutine _conn_input: called CC=10 SCAR=23.4
296 2 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 2 2
297 2 my $client = $self->{clients}->{ $id };
298 2 $kernel->alarm_remove( delete $client->{alarm} );
299 2 my $data_packet_length = SIZEOF_DATA_PACKET;
300 2 ▶ if ( length( $packet ) == SIZEOF_OLD_PACKET ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $packet == 720) 2 0 2
301 2 $data_packet_length = SIZEOF_OLD_PACKET;
302 }
303 2 my $input = _decrypt( $packet, $self->{encryption}, $client->{iv}, $self->{password}, $data_packet_length );
304 2 ▶ return unless $input; # something wrong with the decryption
Branch: 1 branch
BranchTrueFalseTotal
unless $input 0 2 2
305 2 my $version = unpack 'n', substr $input, 0, 4;
306 2 ▶ return unless $version == 3 or $client->{version_already_checked}; # Wrong version received
Branch: 1 branch
BranchTrueFalseTotal
unless $version == 3 or $client->{"version_already_checked"} 0 2 2
Condition33%1/3
$version == 3 or $client->{"version_already_checked"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$version == 3 or $client->{"version_already_checked"}
$version == 3 $client->{"version_already_checked"}
Truth table33%1/3
A or B
A= $version == 3B= $client->{"version_already_checked"}
ABresult
1X1
011
000
307 2 $client->{version_already_checked} = 1;
308 2 my $crc32 = unpack 'N', substr $input, 4, 4;
309 2 my $ts = unpack 'N', substr $input, 8, 4;
310 2 my $rc = unpack 'n', substr $input, 12, 2;
311 2 my $firstbit = substr $input, 0, 4;
312 2 my $secondbit = substr $input, 8;
313 2 my $checksum = _calculate_crc32( $firstbit . pack('N', 0) . $secondbit );
314 2 my @data = unpack 'a[64]a[128]a[512]', substr $input, 14;
315 2 s/\000.*$// for @data;
316 2 my $result = {
317 version => $version,
318 crc32 => $crc32,
319 checksum => $checksum,
320 return_code => $rc,
321 timestamp => $ts,
322 };
323 2 $result->{$_} = shift @data for qw(host_name svc_description plugin_output);
324 2 $result->{$_} = $client->{$_} for qw(peeraddr peerport sockaddr sockport ts iv);
325 $kernel->post( $_, $self->{sessions}->{$_}->{event}, $result, $self->{sessions}->{$_}->{context} )
326 2 for keys %{ $self->{sessions} };
327 2 return;
328 }
329  
330 #/* calculates the CRC 32 value for a buffer */
331 sub _calculate_crc32 {
332 2 ▶ my $string = shift;
Subroutine _calculate_crc32: called CC=2 SCAR=6.9
333  
334 2 my $crc32_table = _generate_crc32_table();
335 2 my $crc = 0xFFFFFFFF;
336  
337 2 foreach my $tchar (split(//, $string)) {
338 1440 my $char = ord($tchar);
339 1440 $crc = (($crc >> 8) & 0x00FFFFFF) ^ $crc32_table->[($crc ^ $char) & 0xFF];
340 }
341  
342 2 return ($crc ^ 0xFFFFFFFF);
343 }
344  
345 #/* build the crc table - must be called before calculating the crc value */
346 sub _generate_crc32_table {
347 2 ▶ my $crc32_table = [];
Subroutine _generate_crc32_table: called CC=4 SCAR=13.9
348 2 my $poly = 0xEDB88320;
349  
350 2 ▶ for (my $i = 0; $i < 256; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 256) 512 2 514
351 512 my $crc = $i;
352 512 ▶ for (my $j = 8; $j > 0; $j--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j > 0) 4096 512 4608
353 4096 ▶ if ($crc & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crc & 1) { } 2048 2048 4096
354 2048 $crc = ($crc >> 1) ^ $poly;
355 }
356 else {
357 2048 $crc = ($crc >> 1);
358 }
359 }
360 512 $crc32_table->[$i] = $crc;
361 }
362 2 return $crc32_table;
363 }
364  
365 # central switchboard for encryption methods.
366 sub _decrypt {
367 2 ▶ my ($data_packet_string, $encryption_method, $iv_salt, $password, $data_packet_length) = @_;
Subroutine _decrypt: called CC=3 SCAR=11.2
368  
369 2 my $crypted;
370 2 ▶ if ($encryption_method == ENCRYPT_NONE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($encryption_method == 0) { } 1 1 2
elsif ($encryption_method == 1) { } 1 0 1
371 1 $crypted = $data_packet_string;
372 }
373 elsif ($encryption_method == ENCRYPT_XOR) {
374 1 $crypted = _decrypt_xor($data_packet_string, $iv_salt, $password, $data_packet_length);
375 }
376 else {
377 0 $crypted = _decrypt_mcrypt( $data_packet_string, $encryption_method, $iv_salt, $password );
378 }
379 2 return $crypted;
380 }
381  
382 sub _decrypt_xor {
383 1 ▶ my ($data_packet_string, $iv_salt, $password, $data_packet_length) = @_;
Subroutine _decrypt_xor: called CC=3 SCAR=11.0
384  
385 1 my @out = split(//, $data_packet_string);
386 1 my @salt_iv = split(//, $iv_salt);
387 1 my @salt_pw = split(//, $password);
388  
389 1 my $y = 0;
390 1 my $x = 0;
391  
392 #/* rotate over IV we received from the server... */
393 1 ▶ while ($y < $data_packet_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $data_packet_length) 720 1 721
394 #/* keep rotating over IV */
395 720 $out[$y] = $out[$y] ^ $salt_iv[$x % scalar(@salt_iv)];
396  
397 720 $y++;
398 720 $x++;
399 }
400  
401 #/* rotate over password... */
402 1 $y=0;
403 1 $x=0;
404 1 ▶ while ($y < $data_packet_length){
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $data_packet_length) 720 1 721
405 #/* keep rotating over password */
406 720 $out[$y] = $out[$y] ^ $salt_pw[$x % scalar(@salt_pw)];
407  
408 720 $y++;
409 720 $x++;
410 }
411 1 return join '', @out;
412 }
413  
414 sub _decrypt_mcrypt {
415 0 ▶ my ( $data_packet_string, $encryption_method, $iv_salt, $password ) = @_;
Subroutine _decrypt_mcrypt: not called CC=6 SCAR=37.4
416 0 my $crypted;
417 0 my $evalok = 0;
418 0 ▶ if( $HAVE_MCRYPT ){
Branch: 1 branch
BranchTrueFalseTotal
if ($HAVE_MCRYPT) { } 0 0 0
419 # Initialise the routine
420 0 ▶ if( defined( $mcrypts{$encryption_method} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mcrypts{$encryption_method}) 0 0 0
421 # Load the routine.
422 0 my $routine = $mcrypts{$encryption_method};
423 0 eval {
424 # This sometimes dies with 'mcrypt is not of type MCRYPT'.
425 0 my $td = Mcrypt->new( algorithm => &{\&{"Mcrypt::".$routine}}(), mode => Mcrypt::CFB(), verbose => 0 );
426 0 my $key = $password;
427 0 my $iv = substr $iv_salt, 0, $td->{IV_SIZE};
428 0 ▶ if( defined( $td ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $td) 0 0 0
429 0 $td->init($key, $iv);
430 0 ▶ for (my $i = 0; $i < length( $data_packet_string ); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $data_packet_string) 0 0 0
431 0 $crypted .= $td->decrypt( substr $data_packet_string, 0+$i, 1 );
432 }
433 0 $td->end();
434 }
435 0 $evalok++;
436 };
437 0 ▶ warn "$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
438 }
439 } else {
440 0 warn "Mcrypt module missing\n";
441 }
442 0 return $crypted;
443 }
444  
445 4 'Hon beiriant goes at hun ar ddeg';
446  
447 __END__
↑ Summary