| 1 | package Net::SSH::Any::Test::Backend::OpenSSH_Daemon; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Net::SSH::Any; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Net::SSH::Any::Constants qw(SSHA_BACKEND_ERROR); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use parent 'Net::SSH::Any::Test::Backend::_Daemon'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | sub _validate_backend_opts { | ||||||||||||||||||||||||||||
|
Subroutine
_validate_backend_opts: called CC=4 SCAR=15.5
| |||||||||||||||||||||||||||||||
| 12 | 1 | my $tssh = shift; | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | $tssh->SUPER::_validate_backend_opts or return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 1 | my $opts = $tssh->{current_opts}; | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | $opts->{sshd_config_file} //= $tssh->_backend_wfile('sshd_config'); | ||||||||||||||||||||||||||||
|
Condition33%1/3
$opts->{"sshd_config_file"} //= $tssh->_backend_wfile("sshd_config")
MC/DC0%0/2
$opts->{"sshd_config_file"} //= $tssh->_backend_wfile("sshd_config")
$opts->{"sshd_config_file"}
$tssh->_backend_wfile("sshd_config")
Truth table33%1/3
A //= B
A= $opts->{"sshd_config_file"}B= $tssh->_backend_wfile("sshd_config")
| |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||
| 18 | # ssh and sshd are resolved here so that they can be used as | ||||||||||||||||||||||||||||||
| 19 | # friends by any other commands | ||||||||||||||||||||||||||||||
| 20 | 1 | $tssh->_be_find_cmd({relaxed => 1}, 'ssh'); | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | $tssh->_be_find_cmd('sshd') // return; | ||||||||||||||||||||||||||||
|
Condition50%1/2
$tssh->_be_find_cmd("sshd") // (return)
MC/DC0%0/1
$tssh->_be_find_cmd("sshd") // (return)
$tssh->_be_find_cmd("sshd")
Truth table50%1/2
A
A= $tssh->_be_find_cmd("sshd")
| |||||||||||||||||||||||||||||||
| 22 | 0 | 1; | |||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub _create_key { | ||||||||||||||||||||||||||||
|
Subroutine
_create_key: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||
| 26 | 0 | my ($tssh, $path) = @_; | |||||||||||||||||||||||||||||
| 27 | 0 | my $path_pub = "$path.pub"; | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | -f $path and -f $path_pub and return 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $path and -f $path_pub
MC/DC0%0/2
-f $path and -f $path_pub
-f $path
-f $path_pub
Truth table0%0/3
A and B
A= -f $pathB= -f $path_pub
| |||||||||||||||||||||||||||||||
| 29 | 0 | my $tmppath = join('.', $path, $$, int(rand(9999999))); | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if ($tssh->_run_cmd({}, 'ssh-keygen', -t => 'rsa', -b => 1024, -f => $tmppath, -P => '')) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 31 | 0 | unlink $path; | |||||||||||||||||||||||||||||
| 32 | 0 | unlink $path_pub; | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if (rename $tmppath, $path and | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
rename $tmppath, $path and rename "$tmppath.pub", $path_pub
MC/DC0%0/2
rename $tmppath, $path and rename "$tmppath.pub", $path_pub
rename $tmppath, $path
rename "$tmppath.pub", $path_pub
Truth table0%0/3
A and B
A= rename $tmppath, $pathB= rename "$tmppath.pub", $path_pub
| |||||||||||||||||||||||||||||||
| 34 | rename "$tmppath.pub", $path_pub) { | ||||||||||||||||||||||||||||||
| 35 | 0 | $tssh->_log("key generated $path"); | |||||||||||||||||||||||||||||
| 36 | 0 | return 1; | |||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||
| 39 | 0 | $tssh->_set_error(SSHA_BACKEND_ERROR, "key generation failed"); | |||||||||||||||||||||||||||||
| 40 | 0 | return; | |||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | sub _be_find_cmd { | ||||||||||||||||||||||||||||
|
Subroutine
_be_find_cmd: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||
| 44 | 2 | my $tssh = shift; | |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | my $find_opts = (ref($_[0]) ? shift : {}); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 46 | 2 | my ($name, $friend, $app, $default) = @_; | |||||||||||||||||||||||||||||
| 47 | 2 | my $opts = $tssh->{current_opts}; | |||||||||||||||||||||||||||||
| 48 | $tssh->SUPER::_be_find_cmd($find_opts, | ||||||||||||||||||||||||||||||
| 49 | $name, | ||||||||||||||||||||||||||||||
| 50 | $friend // $opts->{local_ssh_cmd}, | ||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | $app // { POSIX => 'OpenSSH', MSWin => 'Cygwin' }, | ||||||||||||||||||||||||||||
|
Condition60%3/5
$friend // $opts->{"local_ssh_cmd"}
$app // {"POSIX", "OpenSSH", "MSWin", "Cygwin"}
MC/DC50%1/2
$friend // $opts->{"local_ssh_cmd"}
$friend
$opts->{"local_ssh_cmd"}
MC/DC0%0/1
$app // {"POSIX", "OpenSSH", "MSWin", "Cygwin"}
$app
Truth table66%2/3
A // B
A= $friendB= $opts->{"local_ssh_cmd"}
Truth table50%1/2
A
A= $app
| |||||||||||||||||||||||||||||||
| 52 | $default); | ||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub _user_key_path_quoted { | ||||||||||||||||||||||||||||
|
Subroutine
_user_key_path_quoted: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 56 | 0 | my $tssh = shift; | |||||||||||||||||||||||||||||
| 57 | 0 | my $key = $tssh->_os_unix_path($tssh->{current_opts}{user_key_path}); | |||||||||||||||||||||||||||||
| 58 | 0 | $tssh->_log("user_key_path: $tssh->{current_opts}{user_key_path}, unix path: $key"); | |||||||||||||||||||||||||||||
| 59 | 0 | $key =~ s/%/%%/g; | |||||||||||||||||||||||||||||
| 60 | 0 | $key; | |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub _escape_config { | ||||||||||||||||||||||||||||
|
Subroutine
_escape_config: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 64 | 0 | my ($tssh, $v) = @_; | |||||||||||||||||||||||||||||
| 65 | 0 | $v =~ s/([\\\s])/\\$1/g; | |||||||||||||||||||||||||||||
| 66 | 0 | return $v; | |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub _write_config { | ||||||||||||||||||||||||||||
|
Subroutine
_write_config: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||
| 70 | 0 | my $tssh = shift; | |||||||||||||||||||||||||||||
| 71 | 0 | my $fn = $tssh->{current_opts}{sshd_config_file}; | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | -f $fn and return 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (open my $fn, '>', $fn) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | while (@_) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 75 | 0 | my $k = $tssh->_escape_config(shift); | |||||||||||||||||||||||||||||
| 76 | 0 | my $v = $tssh->_escape_config(shift); | |||||||||||||||||||||||||||||
| 77 | 0 | print $fn "$k=$v\n"; | |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | close $fn and return 1 | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||
| 81 | 0 | $tssh->_set_error(SSHA_BACKEND_ERROR, "unable to create sshd configuration file at '$fn': $!"); | |||||||||||||||||||||||||||||
| 82 | () | ||||||||||||||||||||||||||||||
| 83 | 0 | } | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub _override_config { | ||||||||||||||||||||||||||||
|
Subroutine
_override_config: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||
| 86 | 0 | my $tssh = shift; | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my %override = %{ $tssh->{current_opts}{override_config} // {} }; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$tssh->{"current_opts"}{"override_config"} // {}
MC/DC0%0/1
$tssh->{"current_opts"}{"override_config"} // {}
$tssh->{"current_opts"}{"override_config"}
Truth table0%0/2
A
A= $tssh->{"current_opts"}{"override_config"}
| |||||||||||||||||||||||||||||||
| 88 | 0 | my @cfg; | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | while (@_) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 90 | 0 | my $k = shift; | |||||||||||||||||||||||||||||
| 91 | 0 | my $v = shift; | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if (exists $override{$k}) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 93 | 0 | $v = delete $override{$k}; | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | next unless defined $v; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||
| 96 | 0 | push @cfg, $k, $v; | |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||
| 98 | 0 | (@cfg, %override); | |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _start_and_check { | ||||||||||||||||||||||||||||
|
Subroutine
_start_and_check: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||
| 102 | 0 | my $tssh = shift; | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $tssh->_create_all_keys or return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | 0 | my $opts = $tssh->{current_opts}; | |||||||||||||||||||||||||||||
| 107 | 0 | my $port = $opts->{port}; | |||||||||||||||||||||||||||||
| 108 | 0 | my $sftp_server = $tssh->_be_find_cmd({relaxed => 1}, 'sftp-server'); | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | 0 | my $user = $opts->{user}; | |||||||||||||||||||||||||||||
| 111 | 0 | $user =~ s/\s/?/g; | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||
| 113 | 0 | my @cfg = $tssh->_override_config( HostKey => $tssh->_os_unix_path($opts->{host_key_path}), | |||||||||||||||||||||||||||||
| 114 | AuthorizedKeysFile => $tssh->_user_key_path_quoted . ".pub", | ||||||||||||||||||||||||||||||
| 115 | AllowUsers => $user, # only user running the script can log in | ||||||||||||||||||||||||||||||
| 116 | AllowTcpForwarding => 'yes', | ||||||||||||||||||||||||||||||
| 117 | GatewayPorts => 'no', # bind port forwarder listener to localhost only | ||||||||||||||||||||||||||||||
| 118 | ChallengeResponseAuthentication => 'no', | ||||||||||||||||||||||||||||||
| 119 | PasswordAuthentication => 'no', | ||||||||||||||||||||||||||||||
| 120 | Port => $port, | ||||||||||||||||||||||||||||||
| 121 | ListenAddress => "localhost:$port", | ||||||||||||||||||||||||||||||
| 122 | LogLevel => 'INFO', | ||||||||||||||||||||||||||||||
| 123 | PermitRootLogin => 'yes', | ||||||||||||||||||||||||||||||
| 124 | PidFile => $tssh->_os_unix_path($tssh->_backend_wfile('sshd.pid')), | ||||||||||||||||||||||||||||||
| 125 | PrintLastLog => 'no', | ||||||||||||||||||||||||||||||
| 126 | PrintMotd => 'no', | ||||||||||||||||||||||||||||||
| 127 | UseDNS => 'no', | ||||||||||||||||||||||||||||||
| 128 | StrictModes => 'no', | ||||||||||||||||||||||||||||||
| 129 | UsePrivilegeSeparation => 'no', | ||||||||||||||||||||||||||||||
| 130 | Subsystem => "sftp $sftp_server" ); | ||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | $tssh->_write_config(@cfg) or return; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | 0 | $tssh->_log("Starting sshd at localhost:$port"); | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | 0 | my @cmd; | |||||||||||||||||||||||||||||
| 136 | my @sshd_args = ( '-D', # no daemon | ||||||||||||||||||||||||||||||
| 137 | '-e', # send output to STDEE | ||||||||||||||||||||||||||||||
| 138 | 0 | '-f', $tssh->_os_unix_path($opts->{sshd_config_file}) ); | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 141 | 0 | my $sshd_cmd = $tssh->_os_unix_path($tssh->_be_find_cmd('sshd')); | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my $bash_subcmd = $tssh->_quote_args({shell => 'MSCmd'}, exec => $sshd_cmd, @sshd_args) // | ||||||||||||||||||||||||||||
|
Condition0%0/2
$tssh->_quote_args({"shell", "MSCmd"}, "exec", $sshd_cmd, @sshd_args) // (return)
MC/DC0%0/1
$tssh->_quote_args({"shell", "MSCmd"}, "exec", $sshd_cmd, @sshd_args) // (return)
$tssh->_quote_args({"shell", "MSCmd"}, "exec", $sshd_cmd, @sshd_args)
Truth table0%0/2
A
A= $tssh->_quote_args({"shell", "MSCmd"}, "exec", $sshd_cmd, @sshd_args)
| |||||||||||||||||||||||||||||||
| 143 | return; | ||||||||||||||||||||||||||||||
| 144 | 0 | @cmd = ('bash', '--login', '-c', $bash_subcmd); | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||||
| 147 | 0 | @cmd = ('sshd', @sshd_args) | |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | $tssh->{daemon_proc} = $tssh->_run_cmd({out_name => 'sshd', | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 151 | async => 1 }, | ||||||||||||||||||||||||||||||
| 152 | @cmd) or return; | ||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | $tssh->_check_daemon_and_set_uri and return 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 155 | 0 | $tssh->_stop; | |||||||||||||||||||||||||||||
| 156 | () | ||||||||||||||||||||||||||||||
| 157 | 0 | } | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub _daemon_name { 'sshd' } | ||||||||||||||||||||||||||||
|
Subroutine
_daemon_name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||
| 161 | 1 | 1; | |||||||||||||||||||||||||||||