| 1 | package Net::SSH::Perl::Auth::KeyboardInteractive; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Exporter qw(import); | ||
| 7 | use Net::SSH::Perl::Constants qw(:msg2); | ||
| 8 | use Net::SSH::Perl::Util qw(_prompt); | ||
| 9 | use Scalar::Util qw(weaken); | ||
| 10 | |||
| 11 | use base qw(Net::SSH::Perl::Auth); | ||
| 12 | |||
| 13 | our @EXPORT_OK = qw(add_kbdint_handler del_kbdint_handler); | ||
| 14 | |||
| 15 | sub add_kbdint_handler { | ||
| 16 | my ($ssh, $handles, $handler, $pos) = @_; | ||
| 17 | my $handlers = $ssh->config->get('auth_kbd_interactive_handlers') || []; | ||
| 18 | $pos = @$handlers unless (defined $pos); | ||
| 19 | splice @$handlers, $pos, 0, {'handler' => $handler, 'handles' => $handles}; | ||
| 20 | $ssh->config->set('auth_kbd_interactive_handlers', $handlers); | ||
| 21 | return $pos; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub del_kbdint_handler { | ||
| 25 | my ($ssh, $pos) = @_; | ||
| 26 | my $handlers = $ssh->config->get('auth_kbd_interactive_handlers') or return; | ||
| 27 | $pos = -1 unless (defined $pos); | ||
| 28 | splice @$handlers, $pos, 1; | ||
| 29 | return $pos; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _dispatch { | ||
| 33 | my ($auth, $name, $instruction, $lang, $prompts) = @_; | ||
| 34 | foreach (@{$auth->{'_handlers'}}) { | ||
| 35 | return $_->{'handler'} | ||
| 36 | if ($_->{'handles'}($auth, $name, $instruction, $lang, $prompts)); | ||
| 37 | } | ||
| 38 | return; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub new { | ||
| 42 | my ($class, $ssh) = @_; | ||
| 43 | my $auth = bless {'ssh' => $ssh}, $class; | ||
| 44 | weaken($auth->{'ssh'}); | ||
| 45 | my $handlers = $ssh->config->get('auth_kbd_interactive_handlers') || []; | ||
| 46 | @{$auth->{'_handlers'}} = map { {%$_} } @$handlers; # 1 level deep copy | ||
| 47 | $auth->enabled($ssh->config->get('auth_kbd_interactive')); | ||
| 48 | return $auth; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub enabled { | ||
| 52 | my $self = shift; | ||
| 53 | $self->{'enabled'} = shift if (@_); | ||
| 54 | return $self->{'enabled'}; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub add_handler { | ||
| 58 | my ($self, $handles, $handler, $pos) = @_; | ||
| 59 | $pos = @{$self->{'_handlers'}} unless (defined $pos); | ||
| 60 | splice @{$self->{'_handlers'}}, $pos, 0, | ||
| 61 | { 'handler' => $handler, 'handles' => $handles }; | ||
| 62 | return $pos; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub del_handler { | ||
| 66 | my ($self, $pos) = @_; | ||
| 67 | return unless (@{$self->{'_handlers'}}); | ||
| 68 | $pos = -1 unless (defined $pos); | ||
| 69 | splice @{$self->{'_handlers'}}, $pos, 1; | ||
| 70 | return $pos; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub authenticate { | ||
| 74 | my $self = shift; | ||
| 75 | my $ssh = $self->{'ssh'}; | ||
| 76 | |||
| 77 | return unless ($self->enabled); | ||
| 78 | |||
| 79 | $self->mgr->register_handler(SSH2_MSG_USERAUTH_INFO_REQUEST, sub { | ||
| 80 | my ($amgr, $pkt) = @_; | ||
| 81 | $ssh->debug('auth keyboard-interactive: rcvd info req'); | ||
| 82 | my ($name, $instruction, $lang, $n_prompts) = | ||
| 83 | ($pkt->get_str, $pkt->get_str, $pkt->get_str, $pkt->get_int32); | ||
| 84 | $ssh->debug("auth info req: name='$name' instruction='$instruction' " . | ||
| 85 | "language='$lang' prompts=$n_prompts"); | ||
| 86 | my @prompts = map { [$pkt->get_str, $pkt->get_int8] } (1 .. $n_prompts); | ||
| 87 | my $r_pkt = $ssh->packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE); | ||
| 88 | $r_pkt->put_int32($n_prompts); | ||
| 89 | if (my $h = _dispatch($self, $name, $instruction, $lang, \@prompts)) { | ||
| 90 | $r_pkt->put_str(&$h($self, $name, $instruction, $lang, @$_)) | ||
| 91 | for (@prompts); | ||
| 92 | } elsif ($ssh->config->get('interactive')) { | ||
| 93 | print map { s/[[:cntrl:]]+//g; "$_\n" } grep { length } | ||
| 94 | ($name, $instruction); | ||
| 95 | $r_pkt->put_str(_prompt($_->[0], undef, $_->[1])) for (@prompts); | ||
| 96 | } elsif ($n_prompts != 0) { | ||
| 97 | $ssh->debug('auth keyboard-interactive: cannot handle info req'); | ||
| 98 | # We're not in interactive mode so we can't prompt anybody | ||
| 99 | # and we don't have a handler for this request, so we mark | ||
| 100 | # ourselves disabled, and abandon the current info request | ||
| 101 | # by sending a new, "none" method, auth request. | ||
| 102 | $self->enabled(0); | ||
| 103 | $r_pkt = $ssh->packet_start(SSH2_MSG_USERAUTH_REQUEST); | ||
| 104 | $r_pkt->put_str($ssh->config->get('user')); # user | ||
| 105 | $r_pkt->put_str('ssh-connection'); # service name | ||
| 106 | $r_pkt->put_str('none'); # method name | ||
| 107 | } | ||
| 108 | $r_pkt->send; | ||
| 109 | }); | ||
| 110 | |||
| 111 | $ssh->debug('auth keyboard-interactive: send req'); | ||
| 112 | my $packet = $ssh->packet_start(SSH2_MSG_USERAUTH_REQUEST); | ||
| 113 | $packet->put_str($ssh->config->get('user')); # user | ||
| 114 | $packet->put_str('ssh-connection'); # service name | ||
| 115 | $packet->put_str('keyboard-interactive'); # method name | ||
| 116 | $packet->put_str(''); # language | ||
| 117 | $packet->put_str(''); # submethods | ||
| 118 | $packet->send; | ||
| 119 | |||
| 120 | return 1; | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; |