| 1 | package Net::SSH::Perl::Auth::Rhosts_RSA; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Net::SSH::Perl::Constants qw( | ||
| 7 | SSH_SMSG_FAILURE | ||
| 8 | SSH_SMSG_SUCCESS | ||
| 9 | SSH_CMSG_AUTH_RHOSTS_RSA | ||
| 10 | SSH_SMSG_AUTH_RSA_CHALLENGE | ||
| 11 | SSH_CMSG_AUTH_RSA_RESPONSE ); | ||
| 12 | |||
| 13 | use Net::SSH::Perl::Util qw( :rsa _load_private_key ); | ||
| 14 | use Net::SSH::Perl::Packet; | ||
| 15 | use Net::SSH::Perl::Auth; | ||
| 16 | use base qw( Net::SSH::Perl::Auth ); | ||
| 17 | |||
| 18 | use Scalar::Util qw(weaken); | ||
| 19 | use File::Spec::Functions qw( catfile ); | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | my $ssh = shift; | ||
| 24 | my $auth = bless { ssh => $ssh }, $class; | ||
| 25 | weaken $auth->{ssh}; | ||
| 26 | $auth; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub authenticate { | ||
| 30 | my $auth = shift; | ||
| 31 | my($packet); | ||
| 32 | my $ssh = $auth->{ssh}; | ||
| 33 | |||
| 34 | $ssh->debug("Rhosts-RSA authentication is disabled by the client."), return | ||
| 35 | unless $ssh->config->get('auth_rhosts_rsa'); | ||
| 36 | |||
| 37 | $ssh->debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); | ||
| 38 | |||
| 39 | my $private_key; | ||
| 40 | eval { | ||
| 41 | my $ssh_host_key = $^O eq 'MSWin32' | ||
| 42 | ? catfile($ENV{WINDIR}, 'ssh_host_key') | ||
| 43 | : '/etc/ssh_host_key'; | ||
| 44 | $private_key = _load_private_key($ssh_host_key); | ||
| 45 | }; | ||
| 46 | $ssh->debug("Rhosts with RSA authentication failed: Can't load private host key."), | ||
| 47 | return 0 if $@; | ||
| 48 | |||
| 49 | my $user = $ssh->config->get('user'); | ||
| 50 | $packet = $ssh->packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); | ||
| 51 | $packet->put_str($user); | ||
| 52 | $packet->put_int32($private_key->{rsa}{bits}); | ||
| 53 | $packet->put_mp_int($private_key->{rsa}{e}); | ||
| 54 | $packet->put_mp_int($private_key->{rsa}{n}); | ||
| 55 | $packet->send; | ||
| 56 | |||
| 57 | $packet = Net::SSH::Perl::Packet->read($ssh); | ||
| 58 | my $type = $packet->type; | ||
| 59 | if ($type == SSH_SMSG_FAILURE) { | ||
| 60 | $ssh->debug("Server refused our rhosts authentication or host key."); | ||
| 61 | return 0; | ||
| 62 | } | ||
| 63 | |||
| 64 | if ($type != SSH_SMSG_AUTH_RSA_CHALLENGE) { | ||
| 65 | $ssh->fatal_disconnect("Protocol error during RSA authentication: $type"); | ||
| 66 | } | ||
| 67 | my $challenge = $packet->get_mp_int; | ||
| 68 | |||
| 69 | $ssh->debug("Received RSA challenge for host key from server."); | ||
| 70 | |||
| 71 | _respond_to_rsa_challenge($ssh, $challenge, $private_key); | ||
| 72 | |||
| 73 | $packet = Net::SSH::Perl::Packet->read($ssh); | ||
| 74 | $type = $packet->type; | ||
| 75 | if ($type == SSH_SMSG_SUCCESS) { | ||
| 76 | $ssh->debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); | ||
| 77 | return 1; | ||
| 78 | } | ||
| 79 | elsif ($type != SSH_SMSG_FAILURE) { | ||
| 80 | $ssh->fatal_disconnect("Protocol error waiting RSA auth response: $type"); | ||
| 81 | } | ||
| 82 | |||
| 83 | $ssh->debug("Rhosts or /hosts.equiv with RSA host authentication refused."); | ||
| 84 | return 0; | ||
| 85 | } | ||
| 86 | |||
| 87 | 1; | ||
| 88 | __END__ |