| 1 | package App::BCSSH::Command::ssh; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Sub::Quote; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use App::BCSSH::Message; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use App::BCSSH::Proxy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use App::BCSSH::Options; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use App::BCSSH::Util qw(find_mods rc_dir); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use JSON qw(decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use constant DEBUG => $ENV{BCSSH_DEBUG}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | with Options( | |||||||||||||||||||||||||||
| 12 | permute => 0, | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | 1 | with 'App::BCSSH::Help'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | has agent_path => ( is => 'ro', default => quote_sub q[ $ENV{SSH_AUTH_SOCK} ] ); | |||||||||||||||||||||||||||
| 17 | 1 | has host => ( is => 'ro', lazy => 1, default => quote_sub q{ $_[0]->find_host($_[0]->args) } ); | |||||||||||||||||||||||||||
| 18 | 1 | has auth => ( is => 'ro', arg_spec => 'auth!' ); | |||||||||||||||||||||||||||
| 19 | 1 | has auth_key => ( | |||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||
| 21 | lazy => 1, | ||||||||||||||||||||||||||||
| 22 | default => quote_sub q[ join '', map { chr(32+int(rand(96))) } (1..20) ], | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | 1 | has proxy => ( is => 'lazy' ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | has proxy_handlers => ( is => 'lazy' ); | |||||||||||||||||||||||||||
| 27 | 1 | has command_handlers => ( is => 'lazy' ); | |||||||||||||||||||||||||||
| 28 | 1 | has config => ( is => 'lazy' ); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub _build_config { | ||||||||||||||||||||||||||
|
Subroutine
_build_config: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 31 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 32 | 0 | ▶ | open my $fh, '<', rc_dir . '/config' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | or return {}; | ||||||||||||||||||||||||||||
| 34 | 0 | my $raw = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 35 | 0 | return decode_json($raw); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub run { | ||||||||||||||||||||||||||||
| 39 | my $self = shift; | ||||||||||||||||||||||||||||
| 40 | my $args = $self->args; | ||||||||||||||||||||||||||||
| 41 | my $host = $self->host; | ||||||||||||||||||||||||||||
| 42 | if (! $host) { | ||||||||||||||||||||||||||||
| 43 | exec 'ssh', @$args; | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | my $proxy = $self->proxy; | ||||||||||||||||||||||||||||
| 46 | $ENV{SSH_AUTH_SOCK} = $proxy->socket_path; | ||||||||||||||||||||||||||||
| 47 | if ($host && $self->auth) { | ||||||||||||||||||||||||||||
| 48 | $ENV{LC_BCSSH_AUTH} = $self->auth_key; | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | my $guard = $self->proxy_guard; | ||||||||||||||||||||||||||||
| 51 | # ssh closes all extra file descriptors, or this could use exec with a non-close-on-exec fd | ||||||||||||||||||||||||||||
| 52 | exit system('ssh', @$args); | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub _build_proxy_handlers { | ||||||||||||||||||||||||||
|
Subroutine
_build_proxy_handlers: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 56 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 57 | 0 | my $command_handlers = $self->command_handlers; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | my $host = $self->host; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ( !$host || $self->is_bcssh_agent($self->agent_path) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $host or $self->is_bcssh_agent($self->agent_path)
MC/DC0%0/2
not $host or $self->is_bcssh_agent($self->agent_path)
not $host
$self->is_bcssh_agent($self->agent_path)
Truth table0%0/3
not(A) or B
A= not $hostB= $self->is_bcssh_agent($self->agent_path)
| |||||||||||||||||||||||||||||
| 61 | 0 | return {}; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $auth_key = $self->auth && $self->auth_key; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->auth && $self->auth_key
MC/DC0%0/2
$self->auth && $self->auth_key
$self->auth
$self->auth_key
Truth table0%0/3
A && B
A= $self->authB= $self->auth_key
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | my %handlers = ( | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | (BCSSH_QUERY) => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 68 | 0 | my ($message, $send, $socket) = @_; | |||||||||||||||||||||||||||
| 69 | 0 | $send->(BCSSH_SUCCESS); | |||||||||||||||||||||||||||
| 70 | }, | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | (BCSSH_COMMAND) => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 72 | 0 | my ($message, $send, $socket) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my ($command, $key, $args) = split /\|/, $message, 3; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($auth_key && ! $auth_key ne $key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$auth_key and !$auth_key ne $key
MC/DC0%0/2
$auth_key and !$auth_key ne $key
$auth_key
!$auth_key ne $key
Truth table0%0/3
A and not(B)
A= $auth_keyB= !$auth_key ne $key
| |||||||||||||||||||||||||||||
| 76 | 0 | return $send->(BCSSH_FAILURE); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 0 | my $command_handler = $command_handlers->{$command}; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if (!$command_handler) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | return $send->(BCSSH_FAILURE); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 0 | $command_handler->($args, $send, $socket); | |||||||||||||||||||||||||||
| 83 | }, | ||||||||||||||||||||||||||||
| 84 | 0 | ); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | return \%handlers; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub _build_command_handlers { | ||||||||||||||||||||||||||
|
Subroutine
_build_command_handlers: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 90 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 91 | 0 | my $config = $self->config; | |||||||||||||||||||||||||||
| 92 | 0 | my %command_handlers; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | require App::BCSSH::Handler; | |||||||||||||||||||||||||||
| 95 | 0 | find_mods('App::BCSSH::Handler', 1); | |||||||||||||||||||||||||||
| 96 | 0 | my %handlers = App::BCSSH::Handler->handlers; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | for my $command ( keys %handlers ) { | |||||||||||||||||||||||||||
| 99 | 0 | my $handler = $handlers{$command}; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | my $handler_config = $config->{$command} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$config->{$command} || {}
MC/DC0%0/1
$config->{$command} || {}
$config->{$command}
Truth table0%0/2
A
A= $config->{$command}
| |||||||||||||||||||||||||||||
| 101 | 0 | $command_handlers{$command} | |||||||||||||||||||||||||||
| 102 | = $handler->new(%$handler_config, host => $self->host)->handler; | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 0 | return \%command_handlers; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub _build_proxy { | ||||||||||||||||||||||||||
|
Subroutine
_build_proxy: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 109 | 0 | return App::BCSSH::Proxy->new( | |||||||||||||||||||||||||||
| 110 | agent_path => $self->agent_path, | ||||||||||||||||||||||||||||
| 111 | handlers => $self->proxy_handlers, | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub proxy_guard { | ||||||||||||||||||||||||||
|
Subroutine
proxy_guard: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | 0 | my ($self, $child_cb) = @_; | |||||||||||||||||||||||||||
| 117 | 0 | my $proxy = $self->proxy; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | my $child = open my $fh, '|-'; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | if (!$child) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | chdir '/'; | |||||||||||||||||||||||||||
| 122 | 0 | $0 = 'bcssh proxy'; | |||||||||||||||||||||||||||
| 123 | 0 | unless (DEBUG) { | |||||||||||||||||||||||||||
| 124 | 0 | open STDOUT, '>', '/dev/null'; | |||||||||||||||||||||||||||
| 125 | 0 | open STDERR, '>', '/dev/null'; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | $proxy->proxy(\*STDIN); | |||||||||||||||||||||||||||
| 128 | 0 | exit; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 0 | return $fh; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub find_host { | ||||||||||||||||||||||||||
|
Subroutine
find_host: not called CC=14 SCAR=53.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 134 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 135 | 0 | my $args = shift; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | my %need_arg = map { $_ => 1} split //, 'bcDeFiLlmOopRS'; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | my $user; | |||||||||||||||||||||||||||
| 140 | my $host; | ||||||||||||||||||||||||||||
| 141 | 0 | my $port; | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | for (my $idx = 0; $idx < @$args; $idx++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | my $arg = $args->[$idx]; | |||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ($arg =~ /^-([bcDeFiLlmOopRS])(.*)/){ | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | my $val = length $2 ? $2 : $args->[++$idx]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ($1 eq 'l') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 147 | 0 | $user = $val; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | elsif ($1 eq 'p') { | ||||||||||||||||||||||||||||
| 150 | 0 | $port = $val; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | elsif ($arg =~ /^--/) { | ||||||||||||||||||||||||||||
| 154 | 0 | last; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | ▶ | elsif ($arg !~ /^-/ && !defined $host) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$arg =~ /^-/ || defined $host
MC/DC0%0/2
$arg =~ /^-/ || defined $host
$arg =~ /^-/
defined $host
Truth table0%0/3
A || B
A= $arg =~ /^-/B= defined $host
| |||||||||||||||||||||||||||||
| 157 | 0 | $host = $arg; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | return unless defined $host; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | my $target = ''; | |||||||||||||||||||||||||||
| 162 | 0 | ▶ | $target .= "$user@" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $user and not $host =~ /@/
MC/DC0%0/2
defined $user and not $host =~ /@/
defined $user
not $host =~ /@/
Truth table0%0/3
A and not(B)
A= defined $userB= not $host =~ /@/
| |||||||||||||||||||||||||||||
| 163 | if defined $user && $host !~ /@/; | ||||||||||||||||||||||||||||
| 164 | 0 | $target .= $host; | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | $host .= ":$port" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $port and not $host =~ /:/
MC/DC0%0/2
defined $port and not $host =~ /:/
defined $port
not $host =~ /:/
Truth table0%0/3
A and not(B)
A= defined $portB= not $host =~ /:/
| |||||||||||||||||||||||||||||
| 166 | if defined $port && $host !~ /:/; | ||||||||||||||||||||||||||||
| 167 | 0 | return $target; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | sub is_bcssh_agent { | ||||||||||||||||||||||||||
|
Subroutine
is_bcssh_agent: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 172 | 0 | return App::BCSSH::Message::ping(@_); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1 | 1; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||