blib/lib/Net/SSH/Perl/Util/Hosts.pm

filter: Statementstmt 18.1% 28 / 154 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 69.2% 9 / 13 Podpod n/a totaltotal 12.2% 37 / 303 SCAR 79.2
CC
61
Cov
10%
_remove_host_from_hostfile
462.0
_add_host_to_hostfile
235.5
_all_keys_for_host
210.0
CRAP 2747.1
« blib/lib/Net/SSH/Perl/SSH2.pm ↑ Summary blib/lib/Net/SSH/Perl/SSH1.pm »
1 package Net::SSH::Perl::Util::Hosts;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Net::SSH::Perl::Constants qw( :hosts );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Crypt::Misc qw( encode_b64 decode_b64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Crypt::Mac::HMAC qw( hmac );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use constant SALT_LEN => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub _check_host_in_hostfile {
15 0 ▶ my($host, $port, $hostfile, $key) = @_;
Subroutine _check_host_in_hostfile: not called CC=13 SCAR=52.0
16 0 my $key_class = ref($key);
17  
18 0 ▶ if (defined $port && $port != 22) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port and $port != 22) 0 0 0
Condition0%0/3
defined $port and $port != 22
!ll&&!rl&&r
000
MC/DC0%0/2
defined $port and $port != 22
defined $port $port != 22
Truth table0%0/3
A and B
A= defined $portB= $port != 22
ABresult
0X0
100
111
19 0 $host = "[$host]:$port";
20 }
21  
22 # ssh returns HOST_NEW if the host file can't be opened
23 0 ▶ open my $fh, '<', $hostfile or return HOST_NEW;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $hostfile or return &HOST_NEW() 0 0 0
24 0 local($_, $/);
25 0 $/ = "\n";
26 0 my $status = HOST_NEW;
27 0 ▶ HOST: while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
28 0 chomp;
29 0 my ($hosts, $keyblob) = split /\s+/, $_, 2;
30 0 ▶ next unless $keyblob;
Branch: 1 branch
BranchTrueFalseTotal
unless $keyblob 0 0 0
31 0 my $fkey;
32 ## Trap errors for any potentially unsupported key types
33 0 eval {
34 0 $fkey = $key_class->extract_public($keyblob);
35 };
36 0 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
37  
38 0 my $checkhost = $host;
39  
40 ## Check for hashed entries
41 0 ▶ if (index($hosts, '|') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($hosts, "|") == 0) 0 0 0
42 0 ▶ if ($hosts !~ /^\|1\|(.+?)\|/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hosts =~ /^\|1\|(.+?)\|/) 0 0 0
43 0 warn qq{Cannot parse line $. of $hostfile\n};
44 0 next;
45 }
46 0 my $salt = $1;
47  
48 0 my $rawsalt = decode_b64($salt);
49 0 my $hash = encode_b64(hmac('SHA1',$rawsalt,$host));
50 0 $checkhost = "|1|$salt|$hash";
51 }
52  
53 0 for my $h (split /,/, $hosts) {
54 0 ▶ if ($h eq $checkhost && $key->ssh_name eq $fkey->ssh_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h eq $checkhost and $key->ssh_name eq $fkey->ssh_name) 0 0 0
Condition0%0/3
$h eq $checkhost and $key->ssh_name eq $fkey->ssh_name
!ll&&!rl&&r
000
MC/DC0%0/2
$h eq $checkhost and $key->ssh_name eq $fkey->ssh_name
$h eq $checkhost $key->ssh_name eq $fkey->ssh_name
Truth table0%0/3
A and B
A= $h eq $checkhostB= $key->ssh_name eq $fkey->ssh_name
ABresult
0X0
100
111
55 0 ▶ $status = $key->equal($fkey) ? HOST_OK : HOST_CHANGED;
Branch: 1 branch
BranchTrueFalseTotal
$key->equal($fkey) ? : 0 0 0
56 last HOST
57 0 }
58 }
59 }
60 0 close $fh;
61 0 $status;
62 }
63  
64 sub _all_keys_for_host {
65 0 ▶ my($host, $port, $hostfile) = @_;
Subroutine _all_keys_for_host: not called CC=14 SCAR=53.5
66 0 my $ip;
67 0 ▶ if ($host =~ /[a-zA-Z]+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /[a-zA-Z]+/) 0 0 0
68 0 $ip = inet_ntoa(inet_aton($host));
69 }
70 0 ▶ if (defined $port && $port != 22) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port and $port != 22) 0 0 0
Condition0%0/3
defined $port and $port != 22
!ll&&!rl&&r
000
MC/DC0%0/2
defined $port and $port != 22
defined $port $port != 22
Truth table0%0/3
A and B
A= defined $portB= $port != 22
ABresult
0X0
100
111
71 0 $host = "[$host]:$port";
72 0 $ip = "[$ip]:$port";
73 }
74  
75 0 ▶ open my $fh, '<', $hostfile or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $hostfile or return 0 0 0 0
76 0 local($_, $/);
77 0 $/ = "\n";
78 0 my @keys;
79 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
80 0 chomp;
81 0 my ($hosts, $keyblob) = split /\s+/, $_, 2;
82 0 my @hosts_to_check = ($host);
83 0 ▶ push @hosts_to_check, $ip if $ip;
Branch: 1 branch
BranchTrueFalseTotal
if $ip 0 0 0
84  
85 0 foreach my $checkhost (@hosts_to_check) {
86 ## Check for hashed entries
87 0 ▶ if (index($hosts, '|') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($hosts, "|") == 0) 0 0 0
88 0 ▶ if ($hosts !~ /^\|1\|(.+?)\|/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hosts =~ /^\|1\|(.+?)\|/) 0 0 0
89 0 warn qq{Cannot parse line $. of $hostfile\n};
90 next
91 0 }
92 0 my $salt = $1;
93  
94 0 my $rawsalt = decode_b64($salt);
95 0 my $hash = encode_b64(hmac('SHA1',$rawsalt,$host));
96 0 $checkhost = "|1|$salt|$hash";
97 }
98 0 for my $h (split /,/, $hosts) {
99 0 ▶ if ($h eq $checkhost) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h eq $checkhost) 0 0 0
100 0 my $fkey;
101 0 eval { $fkey = Net::SSH::Perl::Key->extract_public($keyblob) };
102 0 ▶ push @keys, $fkey if $fkey;
Branch: 1 branch
BranchTrueFalseTotal
if $fkey 0 0 0
103 }
104 }
105 }
106 }
107 0 close $fh;
108 0 ▶ return wantarray ? @keys : \@keys
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
109 }
110  
111 sub _add_host_to_hostfile {
112 0 ▶ my($host, $port, $hostfile, $key, $hash_flag) = @_;
Subroutine _add_host_to_hostfile: not called CC=16 SCAR=54.6
113 0 ▶ unless (-e $hostfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $hostfile) 0 0 0
114 0 require File::Basename;
115 0 my $dir = File::Basename::dirname($hostfile);
116 0 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
117 0 require File::Path;
118 0 ▶ File::Path::mkpath([ $dir ])
Branch: 1 branch
BranchTrueFalseTotal
File::Path::mkpath([$dir]) or die "Can't create directory ${dir}: $!" 0 0 0
119 or die "Can't create directory $dir: $!";
120 }
121 }
122  
123 0 my $ip;
124 0 ▶ if ($host =~ /[a-zA-Z]+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /[a-zA-Z]+/) 0 0 0
125 0 $ip = inet_ntoa(inet_aton($host));
126 0 ▶ $ip = "[$ip]:$port" if $ip && defined $port && $port != 22;
Branch: 1 branch
BranchTrueFalseTotal
if $ip and defined $port and $port != 22 0 0 0
Condition0%0/6
$ip and defined $port and $port != 22
!ll&&!rl&&r
000
$ip and defined $port
!ll&&!rl&&r
000
MC/DC0%0/3
$ip and defined $port and $port != 22
$ip defined $port $port != 22
Truth table0%0/4
A and B and C
A= $ipB= defined $portC= $port != 22
ABCresult
0XX0
10X0
1100
1111
127 }
128 0 ▶ $host = "[$host]:$port" if defined $port && $port != 22;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port and $port != 22 0 0 0
Condition0%0/3
defined $port and $port != 22
!ll&&!rl&&r
000
MC/DC0%0/2
defined $port and $port != 22
defined $port $port != 22
Truth table0%0/3
A and B
A= defined $portB= $port != 22
ABresult
0X0
100
111
129  
130 0 my $data;
131 0 ▶ open my $fh, '>>', $hostfile or croak "Can't write to $hostfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $hostfile or croak("Can't write to ${hostfile}: $!") 0 0 0
132 0 ▶ if ($hash_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_flag) { } 0 0 0
133 5 ▶ use Crypt::PRNG qw( random_bytes );
Subroutine BEGIN: called CC=1 SCAR=0.0
134 0 my @entries = ($host);
135 0 ▶ push @entries, $ip if $ip;
Branch: 1 branch
BranchTrueFalseTotal
if $ip 0 0 0
136 0 foreach my $entry (@entries) {
137 0 my $rawsalt = random_bytes(SALT_LEN);
138 0 my $salt = encode_b64($rawsalt);
139 0 my $hash = encode_b64(hmac('SHA1', $rawsalt, $entry));
140 0 $data .= join(' ', "|1|$salt|$hash", $key->dump_public, "\n");
141 }
142 }
143 else {
144 0 ▶ $host = "$host,$ip" if $ip;
Branch: 1 branch
BranchTrueFalseTotal
if $ip 0 0 0
145 0 $data = join(' ', $host, $key->dump_public, "\n");
146 }
147 0 print $fh $data;
148 0 ▶ close $fh or croak "Can't close $hostfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Can't close ${hostfile}: $!") 0 0 0
149 }
150  
151 sub _remove_host_from_hostfile {
152 0 ▶ my($host, $port, $hostfile, $key) = @_;
Subroutine _remove_host_from_hostfile: not called CC=21 SCAR=61.4
153 0 ▶ return unless -e $hostfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $hostfile 0 0 0
154  
155 0 my $ip;
156 0 ▶ if ($host =~ /[a-zA-Z]+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host =~ /[a-zA-Z]+/) 0 0 0
157 0 $ip = inet_ntoa(inet_aton($host));
158 0 ▶ $ip = "[$ip]:$port" if $ip && defined $port && $port != 22;
Branch: 1 branch
BranchTrueFalseTotal
if $ip and defined $port and $port != 22 0 0 0
Condition0%0/6
$ip and defined $port and $port != 22
!ll&&!rl&&r
000
$ip and defined $port
!ll&&!rl&&r
000
MC/DC0%0/3
$ip and defined $port and $port != 22
$ip defined $port $port != 22
Truth table0%0/4
A and B and C
A= $ipB= defined $portC= $port != 22
ABCresult
0XX0
10X0
1100
1111
159 }
160 0 ▶ $host = "[$host]:$port" if defined $port && $port != 22;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port and $port != 22 0 0 0
Condition0%0/3
defined $port and $port != 22
!ll&&!rl&&r
000
MC/DC0%0/2
defined $port and $port != 22
defined $port $port != 22
Truth table0%0/3
A and B
A= defined $portB= $port != 22
ABresult
0X0
100
111
161  
162 0 ▶ open my $fh, '<', $hostfile or croak "Can't open $hostfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $hostfile or croak("Can't open ${hostfile}: $!") 0 0 0
163 0 ▶ open my $fhw, '>', "$hostfile.new" or croak "Can't open $hostfile.new for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fhw, ">", "$hostfile.new" or croak("Can't open $hostfile.new for writing: $!") 0 0 0
164  
165 0 ▶ LINE: while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
166 0 chomp;
167 0 my ($hosts, $keyblob) = split /\s+/, $_, 2;
168 0 my $fkey;
169 ## Trap errors for any potentially unsupported key types
170 0 eval {
171 0 $fkey = Net::SSH::Perl::Key->extract_public($keyblob);
172 };
173 # keep it if we don't know what it is
174 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
175 0 print $fhw $_,"\n";
176 0 next LINE;
177 }
178  
179 0 my @hosts_to_check = ($host);
180 0 ▶ push @hosts_to_check, $ip if $ip;
Branch: 1 branch
BranchTrueFalseTotal
if $ip 0 0 0
181  
182 0 foreach my $checkhost (@hosts_to_check) {
183 ## Check for hashed entries
184 0 ▶ if (index($hosts, '|') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($hosts, "|") == 0) 0 0 0
185 0 ▶ if ($hosts !~ /^\|1\|(.+?)\|/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hosts =~ /^\|1\|(.+?)\|/) 0 0 0
186 0 warn qq{Cannot parse line $. of $hostfile\n};
187 0 next;
188 }
189 0 my $salt = $1;
190  
191 0 my $rawsalt = decode_b64($salt);
192 0 my $hash = encode_b64(hmac('SHA1',$rawsalt,$checkhost));
193 0 $checkhost = "|1|$salt|$hash";
194 }
195  
196 0 for my $h (split /,/, $hosts) {
197 0 ▶ if ($h eq $checkhost && $key->equal($fkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h eq $checkhost and $key->equal($fkey)) 0 0 0
Condition0%0/3
$h eq $checkhost and $key->equal($fkey)
!ll&&!rl&&r
000
MC/DC0%0/2
$h eq $checkhost and $key->equal($fkey)
$h eq $checkhost $key->equal($fkey)
Truth table0%0/3
A and B
A= $h eq $checkhostB= $key->equal($fkey)
ABresult
0X0
100
111
198 0 next LINE;
199 }
200 }
201 }
202 0 print $fhw $_,"\n";
203 }
204 0 ▶ close $fhw or croak "Can't close $hostfile.new: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fhw or croak("Can't close $hostfile.new: $!") 0 0 0
205 0 ▶ close $fh or croak "Can't close $hostfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Can't close ${hostfile}: $!") 0 0 0
206 0 rename "$hostfile.new", $hostfile;
207 }
208  
209 5 1;
« blib/lib/Net/SSH/Perl/SSH2.pm ↑ Summary blib/lib/Net/SSH/Perl/SSH1.pm »