| 1 | package Net::SSH::Any::Backend::Sshg3_Cmd; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | use Net::SSH::Any::Util qw(_first_defined _array_or_scalar_to_list $debug _debug _debugf _debug_dump); | ||
| 7 | use Net::SSH::Any::Constants qw(SSHA_CONNECTION_ERROR SSHA_CHANNEL_ERROR SSHA_REMOTE_CMD_ERROR); | ||
| 8 | |||
| 9 | use parent 'Net::SSH::Any::Backend::_Cmd'; | ||
| 10 | |||
| 11 | sub _validate_backend_opts { | ||
| 12 | my ($any, %be_opts) = @_; | ||
| 13 | $any->SUPER::_validate_backend_opts(%be_opts) or return; | ||
| 14 | |||
| 15 | defined $be_opts{host} or croak "host argument missing"; | ||
| 16 | |||
| 17 | $be_opts{local_sshg3_cmd} //= | ||
| 18 | $any->_find_cmd(sshg3 => undef, | ||
| 19 | { POSIX => 'tectia', | ||
| 20 | MSWin => 'SSH Communications Security\\SSH Tectia\\SSH Tectia Client' }) // return; | ||
| 21 | my $out = $any->_local_capture($be_opts{local_sshg3_cmd}, '-V'); | ||
| 22 | if ($?) { | ||
| 23 | $any->_set_error(SSHA_CONNECTION_ERROR, 'sshg3 not found or bad version, rc: ', ($? >> 8)); | ||
| 24 | return; | ||
| 25 | } | ||
| 26 | |||
| 27 | $be_opts{run_broker} //= 0; | ||
| 28 | |||
| 29 | if ($be_opts{run_broker}) { | ||
| 30 | $be_opts{local_ssh_broker_g3_cmd} //= | ||
| 31 | $any->_find_cmd('ssh-broker-g3', $be_opts{local_sshg3_cmd}, | ||
| 32 | { POSIX => 'tectia', | ||
| 33 | MSWin => 'SSH Communications Security\\SSH Tectia\\SSH Tectia Broker' }) // return; | ||
| 34 | } | ||
| 35 | |||
| 36 | $be_opts{local_sshg3_extra_args} = $any->_find_local_extra_args(sshg3 => \%be_opts); | ||
| 37 | $be_opts{local_ssh_broker_g3_extra_args} = $any->_find_local_extra_args(ssh_broker_g3 => \%be_opts); | ||
| 38 | |||
| 39 | my @auth_type; | ||
| 40 | if (defined $be_opts{password}) { | ||
| 41 | $any->{be_password_path} = # save it here to ensure it can be unlinked on destruction | ||
| 42 | $any->_os_create_secret_file("sshg3-pwd.txt", $be_opts{password}) // return; | ||
| 43 | push @auth_type, 'password'; | ||
| 44 | } | ||
| 45 | elsif (defined (my $key = $be_opts{key_path})) { | ||
| 46 | push @auth_type, 'publickey'; | ||
| 47 | croak "pubkey authentication not supported yet by Sshg3_Cmd backend"; | ||
| 48 | } | ||
| 49 | |||
| 50 | if (delete $be_opts{strict_host_key_checking}) { | ||
| 51 | $be_opts{hostkey_policy} = 'strict'; | ||
| 52 | } | ||
| 53 | else { | ||
| 54 | my $known_hosts_path = delete $be_opts{known_hosts_path}; | ||
| 55 | if (defined $known_hosts_path and $known_hosts_path eq '/dev/null') { | ||
| 56 | $be_opts{hostkey_policy} = 'advisory'; | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | $be_opts{hostkey_policy} = 'tofu'; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | # Work around bug on Tectia/Windows affecting only old Windows versions, apparently. | ||
| 64 | my ($os, $mayor, $minor) = $any->_os_version; | ||
| 65 | if ($os eq 'MSWin' and not $be_opts{exclusive}) { | ||
| 66 | $debug and $debug & 1024 and _debug "OS version is $os $mayor.$minor"; | ||
| 67 | if ($mayor < 6 or ($mayor == 6 and $minor < 1)) { # < Win7 | ||
| 68 | $be_opts{exclusive} //= 1; | ||
| 69 | $debug and $debug & 1024 and _debug($be_opts{exclusive} | ||
| 70 | ? "Exclusive mode enabled" | ||
| 71 | : "Exclusive mode disabled by user explicitly"); | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | $debug and $debug & 1024 and _debug "Leaving exclusive mode disabled"; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | $debug and $debug & 1024 and _debug_dump be_opts => \%be_opts; | ||
| 79 | |||
| 80 | $any->{be_opts} = \%be_opts; | ||
| 81 | $any->{be_auth_type} = join(',', @auth_type); | ||
| 82 | $any->{be_interactive_login} = 0; | ||
| 83 | |||
| 84 | 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _connect{ | ||
| 88 | my $any = shift; | ||
| 89 | my $be_opts = $any->{be_opts}; | ||
| 90 | |||
| 91 | if ($be_opts->{run_broker}) { | ||
| 92 | if (defined (my $broker = $be_opts->{local_ssh_broker_g3_cmd})) { | ||
| 93 | local $?; # ignore errors here; | ||
| 94 | my @args = @{$be_opts->{local_ssh_broker_g3_extra_args}}; | ||
| 95 | push @args, -a => $be_opts->{broker_address} | ||
| 96 | if defined $be_opts->{broker_address}; | ||
| 97 | system $broker $broker, @args; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | 1; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _make_cmd { | ||
| 104 | my ($any, $cmd_opts, $cmd) = @_; | ||
| 105 | my $be_opts = $any->{be_opts}; | ||
| 106 | |||
| 107 | # FIXME: the environment variable should only be set when running | ||
| 108 | # the sshg3 command, not globally! | ||
| 109 | if (defined (my $broker_address = $be_opts->{broker_address})) { | ||
| 110 | $any->_os_setenv(SSH_SECSH_BROKER => $broker_address); | ||
| 111 | } | ||
| 112 | |||
| 113 | my @args = ( $be_opts->{local_sshg3_cmd}, | ||
| 114 | @{$be_opts->{local_sshg3_extra_args}}, | ||
| 115 | '-B', '-enone', '-q', | ||
| 116 | "--hostkey-policy=$be_opts->{hostkey_policy}"); | ||
| 117 | |||
| 118 | push @args, '--exclusive' if $be_opts->{exclusive}; | ||
| 119 | push @args, "-l$be_opts->{user}" if defined $be_opts->{user}; | ||
| 120 | push @args, "-p$be_opts->{port}" if defined $be_opts->{port}; | ||
| 121 | push @args, "-Pfile://$any->{be_password_path}" if defined $any->{be_password_path}; | ||
| 122 | |||
| 123 | return (@args, | ||
| 124 | ( delete $cmd_opts->{subsystem} | ||
| 125 | ? (-s => $cmd, $be_opts->{host}) | ||
| 126 | : ($be_opts->{host}, $cmd))); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub DESTROY { | ||
| 130 | my $any = shift; | ||
| 131 | if (defined(my $password_path = $any->{be_password_path})) { | ||
| 132 | local $!; | ||
| 133 | unlink $password_path; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | 1; | ||
| 138 | |||
| 139 | __END__ |