home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / ChallengeResponse.pm < prev    next >
Encoding:
Perl POD Document  |  2003-12-03  |  2.0 KB  |  72 lines

  1. # $Id: ChallengeResponse.pm,v 1.3 2003/12/03 15:35:21 autarch Exp $
  2.  
  3. package Net::SSH::Perl::Auth::ChallengeResponse;
  4. use strict;
  5.  
  6. use Net::SSH::Perl::Constants qw(
  7.     SSH_CMSG_AUTH_TIS
  8.     SSH_SMSG_AUTH_TIS_CHALLENGE
  9.     SSH_CMSG_AUTH_TIS_RESPONSE
  10.     SSH_SMSG_SUCCESS
  11.     SSH_SMSG_FAILURE );
  12.  
  13. use Net::SSH::Perl::Packet;
  14. use Net::SSH::Perl::Util qw( _read_passphrase );
  15. use Net::SSH::Perl::Auth;
  16. use base qw( Net::SSH::Perl::Auth );
  17.  
  18. use Scalar::Util qw(weaken);
  19.  
  20. sub new {
  21.     my $class = shift;
  22.     my $ssh = shift;
  23.     my $auth = bless { ssh => $ssh }, $class;
  24.     weaken $auth->{ssh};
  25.     $auth->enabled( $ssh->config->get('auth_ch_res') );
  26.     $auth;
  27. }
  28.  
  29. sub enabled {
  30.     my $auth = shift;
  31.     $auth->{enabled} = shift if @_;
  32.     $auth->{enabled};
  33. }
  34.  
  35. sub authenticate {
  36.     my $auth = shift;
  37.     my $ssh = $auth->{ssh};
  38.     $ssh->debug("Password authentication is disabled by the client."), return
  39.         unless $auth->enabled;
  40.  
  41.     $ssh->debug("Doing challenge response authentication.");
  42.     for (1..$ssh->config->get('number_of_password_prompts')) {
  43.         my $packet = $ssh->packet_start(SSH_CMSG_AUTH_TIS);
  44.         $packet->send;
  45.  
  46.         $packet = Net::SSH::Perl::Packet->read($ssh);
  47.         my $type = $packet->type;
  48.         $ssh->fatal_disconnect("Protocol error in AUTH_TIS")
  49.             unless $type == SSH_SMSG_FAILURE ||
  50.                    $type == SSH_SMSG_AUTH_TIS_CHALLENGE;
  51.  
  52.         $ssh->debug("No challenge presented."), return
  53.             unless $type == SSH_SMSG_AUTH_TIS_CHALLENGE;
  54.  
  55.         my $challenge = $packet->get_str;
  56.         my $response = _read_passphrase($challenge);
  57.         $packet = $ssh->packet_start(SSH_CMSG_AUTH_TIS_RESPONSE);
  58.         $packet->put_str($response);
  59.         $packet->send;
  60.  
  61.         $packet = Net::SSH::Perl::Packet->read($ssh);
  62.         return 1 if $packet->type == SSH_SMSG_SUCCESS;
  63.  
  64.         $ssh->fatal_disconnect("Protocol error to AUTH_TIS response")
  65.             unless $packet->type == SSH_SMSG_FAILURE;
  66.     }
  67.  
  68.     return 0;
  69. }
  70.  
  71. 1;
  72.