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 / Rhosts_RSA.pm < prev    next >
Encoding:
Perl POD Document  |  2003-12-03  |  4.7 KB  |  155 lines

  1. # $Id: Rhosts_RSA.pm,v 1.13 2003/12/03 15:35:21 autarch Exp $
  2.  
  3. package Net::SSH::Perl::Auth::Rhosts_RSA;
  4.  
  5. use strict;
  6.  
  7. use Net::SSH::Perl::Constants qw(
  8.     SSH_SMSG_FAILURE
  9.     SSH_SMSG_SUCCESS
  10.     SSH_CMSG_AUTH_RHOSTS_RSA
  11.     SSH_SMSG_AUTH_RSA_CHALLENGE
  12.     SSH_CMSG_AUTH_RSA_RESPONSE );
  13.  
  14. use Net::SSH::Perl::Util qw( :rsa _load_private_key );
  15. use Net::SSH::Perl::Packet;
  16. use Net::SSH::Perl::Auth;
  17. use base qw( Net::SSH::Perl::Auth );
  18.  
  19. use Scalar::Util qw(weaken);
  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.         $private_key = _load_private_key("/etc/ssh_host_key");
  42.     };
  43.     $ssh->debug("Rhosts with RSA authentication failed: Can't load private host key."),
  44.         return 0 if $@;
  45.  
  46.     my $user = $ssh->config->get('user');
  47.     $packet = $ssh->packet_start(SSH_CMSG_AUTH_RHOSTS_RSA);
  48.     $packet->put_str($user);
  49.     $packet->put_int32($private_key->{rsa}{bits});
  50.     $packet->put_mp_int($private_key->{rsa}{e});
  51.     $packet->put_mp_int($private_key->{rsa}{n});
  52.     $packet->send;
  53.  
  54.     $packet = Net::SSH::Perl::Packet->read($ssh);
  55.     my $type = $packet->type;
  56.     if ($type == SSH_SMSG_FAILURE) {
  57.         $ssh->debug("Server refused our rhosts authentication or host key.");
  58.         return 0;
  59.     }
  60.  
  61.     if ($type != SSH_SMSG_AUTH_RSA_CHALLENGE) {
  62.         $ssh->fatal_disconnect("Protocol error during RSA authentication: $type");
  63.     }
  64.     my $challenge = $packet->get_mp_int;
  65.  
  66.     $ssh->debug("Received RSA challenge for host key from server.");
  67.  
  68.     _respond_to_rsa_challenge($ssh, $challenge, $private_key);
  69.  
  70.     $packet = Net::SSH::Perl::Packet->read($ssh);
  71.     $type = $packet->type;
  72.     if ($type == SSH_SMSG_SUCCESS) {
  73.         $ssh->debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server.");
  74.         return 1;
  75.     }
  76.     elsif ($type != SSH_SMSG_FAILURE) {
  77.         $ssh->fatal_disconnect("Protocol error waiting RSA auth response: $type");
  78.     }
  79.  
  80.     $ssh->debug("Rhosts or /hosts.equiv with RSA host authentication refused.");
  81.     return 0;
  82. }
  83.  
  84. 1;
  85. __END__
  86.  
  87. =head1 NAME
  88.  
  89. Net::SSH::Perl::Auth::Rhosts_RSA - Perform Rhosts-RSA authentication
  90.  
  91. =head1 SYNOPSIS
  92.  
  93.     use Net::SSH::Perl::Auth;
  94.     my $auth = Net::SSH::Perl::Auth->new('Rhosts_RSA', $ssh);
  95.     print "Valid auth" if $auth->authenticate;
  96.  
  97. =head1 DESCRIPTION
  98.  
  99. I<Net::SSH::Perl::Auth::Rhosts_RSA> performs Rhosts with RSA
  100. authentication with a remote sshd server. This is standard
  101. Rhosts authentication, plus a challenge-response phase where
  102. the server RSA-authenticates the client based on its host
  103. key. When you create a new Rhosts_RSA auth object, you give
  104. it an I<$ssh> object, which should contain an open connection
  105. to an ssh daemon, as well as any data that the authentication
  106. module needs to proceed. In this case, the I<$ssh> object
  107. must contain the name of the user trying to open the connection.
  108.  
  109. Note that the sshd server will require two things from your
  110. client:
  111.  
  112. =over 4
  113.  
  114. =item 1. Privileged Port
  115.  
  116. sshd will require your client to be running on a privileged port
  117. (below 1024); this will, in turn, likely require your client to be
  118. running as root. If your client is not running on a privileged port,
  119. the Rhosts-RSA authentication request will be denied.
  120.  
  121. If you're running as root, I<Net::SSH::Perl> should
  122. automatically detect that and try to start up on a privileged
  123. port. If for some reason that isn't happening, take a look at
  124. the I<Net::SSH::Perl> docs.
  125.  
  126. =item 2. Private Host Key
  127.  
  128. In order to do RSA-authentication on your host key, your client
  129. must be able to read the host key. This will likely be
  130. impossible unless you're running as root, because the private
  131. host key file (F</etc/ssh_host_key>) is readable only by root.
  132.  
  133. =back
  134.  
  135. With that aside, to use Rhosts-RSA authentication the client
  136. sends a request to the server to authenticate it, including
  137. the name of the user trying to authenticate, as well as the
  138. public parts of the host key. The server first ensures that
  139. the host can be authenticated using standard Rhosts
  140. authentication (I<shosts.equiv>, I<hosts.equiv>, etc.).
  141. If the client passes this test, the server sends an encrypted
  142. challenge to the client. The client must decrypt this
  143. challenge using its private host key, then respond to the
  144. server with its response.
  145.  
  146. Once the response has been sent, the server responds with
  147. success or failure.
  148.  
  149. =head1 AUTHOR & COPYRIGHTS
  150.  
  151. Please see the Net::SSH::Perl manpage for author, copyright,
  152. and license information.
  153.  
  154. =cut
  155.