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 / Auth.pm < prev    next >
Encoding:
Perl POD Document  |  2001-07-03  |  5.7 KB  |  234 lines

  1. # $Id: Auth.pm,v 1.8 2001/07/03 07:04:52 btrott Exp $
  2.  
  3. package Net::SSH::Perl::Auth;
  4.  
  5. use strict;
  6. use Carp qw( croak );
  7.  
  8. use vars qw( %AUTH %AUTH_REVERSE @AUTH_ORDER %SUPPORTED );
  9. BEGIN {
  10.     %AUTH = (
  11.         Rhosts       => 1,
  12.         RSA          => 2,
  13.         Password     => 3,
  14.         Rhosts_RSA   => 4,
  15.         ChallengeResponse => 5,
  16.         Kerberos     => 6,
  17.         Kerberos_TGT => 7,
  18.     );
  19.     %AUTH_REVERSE = reverse %AUTH;
  20.  
  21.     @AUTH_ORDER = qw( 7 6 1 4 2 5 3 );
  22. }
  23.  
  24. sub _determine_supported {
  25.     for my $auth (keys %AUTH) {
  26.         my $pack = sprintf "%s::%s", __PACKAGE__, $auth;
  27.         eval "use $pack";
  28.         $SUPPORTED{$AUTH{$auth}}++ unless $@;
  29.     }
  30. }
  31.  
  32. sub new {
  33.     my $class = shift;
  34.     my $type = shift;
  35.     my $auth_class = join '::', __PACKAGE__, $type;
  36.     (my $lib = $auth_class . ".pm") =~ s!::!/!g;
  37.     require $lib;
  38.     $auth_class->new(@_);
  39. }
  40.  
  41. ## For SSH2: mgr is Net::SSH::Perl::AuthMgr object.
  42. sub mgr {
  43.     my $auth = shift;
  44.     $auth->{mgr} = shift if @_;
  45.     $auth->{mgr};
  46. }
  47.  
  48. sub id {
  49.     my $this = shift;
  50.     my $type;
  51.     if (my $class = ref $this) {
  52.         my $pack = __PACKAGE__;
  53.         ($type = $class) =~ s/^${pack}:://;
  54.     }
  55.     else {
  56.         $type = $this;
  57.     }
  58.     $AUTH{$type};
  59. }
  60.  
  61. sub name {
  62.     my $this = shift;
  63.     my $name;
  64.     if (my $class = ref $this) {
  65.         my $pack = __PACKAGE__;
  66.         ($name = $class) =~ s/^${pack}:://;
  67.     }
  68.     else {
  69.         $name = $AUTH_REVERSE{$this};
  70.     }
  71.     $name;
  72. }
  73.  
  74. sub mask {
  75.     my $mask = 0;
  76.     $mask |= (1<<$_) for keys %SUPPORTED;
  77.     $mask;
  78. }
  79.  
  80. sub supported {
  81.     unless (keys %SUPPORTED) {
  82.         _determine_supported();
  83.     }
  84.     return [ keys %SUPPORTED ] unless @_;
  85.     my $id = shift;
  86.     return $id == 0 || exists $SUPPORTED{$id} unless @_;
  87.     my $ssupp = shift;
  88.     mask() & $ssupp & (1 << $id);
  89. }
  90.  
  91. sub auth_order { \@AUTH_ORDER }
  92.  
  93. sub authenticate { 0 }
  94.  
  95. 1;
  96. __END__
  97.  
  98. =head1 NAME
  99.  
  100. Net::SSH::Perl::Auth - Base authentication class, plus utility methods
  101.  
  102. =head1 SYNOPSIS
  103.  
  104.    use Net::SSH::Perl::Cipher;
  105.  
  106.    # Get list of supported authentication IDs.
  107.    my $supported = Net::SSH::Perl::Auth::supported();
  108.  
  109.    # Translate an auth name into an ID.
  110.    my $id = Net::SSH::Perl::Auth::id($name);
  111.  
  112.    # Translate an auth ID into a name.
  113.    my $name = Net::SSH::Perl::Auth::name($id);
  114.  
  115.    # Get the order in which auth methods are tested.
  116.    my $order = Net::SSH::Perl::Auth::order();
  117.  
  118. =head1 DESCRIPTION
  119.  
  120. I<Net::SSH::Perl::Auth> provides a base class for each of
  121. the authentication method classes. In addition, it defines
  122. a set of utility methods that can be called either as
  123. functions or object methods.
  124.  
  125. =head1 UTILITY METHODS
  126.  
  127. =head2 supported( [ $auth_id [, $server_supports ] ])
  128.  
  129. Without arguments, returns a reference to an array of
  130. auth methods supported by I<Net::SSH::Perl>. These are methods
  131. that have working Net::SSH::Perl::Auth:: implementations,
  132. essentially.
  133.  
  134. With one argument I<$auth_id>, returns a true value if
  135. that auth method is supported by I<Net::SSH::Perl>, and
  136. false otherwise.
  137.  
  138. With two arguments, I<$auth_id> and I<$server_supports>,
  139. returns true if the auth represented by I<$auth_id>
  140. is supported both by I<Net::SSH::Perl> and by the sshd
  141. server. The list of methods supported by the server
  142. should be in I<$server_supports>, a bit mask sent
  143. from the server during the session identification
  144. phase.
  145.  
  146. Can be called either as a non-exported function, i.e.
  147.  
  148.     my $i_support = Net::SSH::Perl::Auth::supported();
  149.  
  150. or as an object method of a I<Net::SSH::Perl::Auth>
  151. object, or an object of a subclass (in which case
  152. the first argument should be I<$server_supports>,
  153. not the I<$auth_id>):
  154.  
  155.     if ($auth->supported($server_supports)) {
  156.         print "Server supports auth method $auth";
  157.     }
  158.  
  159. =head2 id( [ $auth_name ] )
  160.  
  161. Translates an auth method name into an ID (suitable
  162. for sending to the sshd server, for example).
  163.  
  164. If given I<$auth_name> translates that name into
  165. the corresponding ID. If called as an object method,
  166. translates the object's auth class name into the
  167. ID.
  168.  
  169. =head2 name( [ $auth_id ] )
  170.  
  171. Translates an auth method ID into a name.
  172.  
  173. If given I<$auth_id> translates that ID into the
  174. corresponding name. If called as an object method,
  175. returns the (stripped) object's auth class name;
  176. for example, if the object were of type
  177. I<Net::SSH::Perl::Auth::Rhosts>, I<name> would return
  178. I<Rhosts>.
  179.  
  180. =head2 auth_order()
  181.  
  182. Returns a reference to an array containing auth method
  183. IDs. These IDs describe the order in which authentication
  184. should be tested against the server. So, for example, if
  185. the array listed (2, 4, 3), then the client should test:
  186. RSA, then Rhosts-RSA, then Password authentication.
  187.  
  188. =head1 AUTH USAGE
  189.  
  190. =head2 Net::SSH::Perl::Auth->new($auth_name, $ssh)
  191.  
  192. Instantiates a new auth object of the type
  193. I<$auth_name>, and gives it the I<Net::SSH::Perl>
  194. object I<$ssh>, which should contain an open
  195. connetion to an sshd server.
  196.  
  197. Returns the auth object, which will be blessed into
  198. the actual auth subclass.
  199.  
  200. =head2 $valid = $auth->authenticate()
  201.  
  202. Talks to the sshd server to authenticate the user;
  203. if valid, returns true, and if invalid, returns
  204. false.
  205.  
  206. =head1 AUTH DEVELOPMENT
  207.  
  208. Classes implementing an authentication method must implement
  209. the following two methods:
  210.  
  211. =over 4
  212.  
  213. =item * $class->new($ssh)
  214.  
  215. Given a I<Net::SSH::Perl> object I<$ssh>, should construct a
  216. new auth object and bless it into I<$class>, presumably.
  217.  
  218. =item * $class->authenticate()
  219.  
  220. Authenticate the current user with the remote daemon. This
  221. requires following the messaging protocol defined for your
  222. authentication method. All of the data you need--user name,
  223. password (if required), etc.--should be in the I<$ssh>
  224. object.
  225.  
  226. Returns 1 if the authentication is successful, 0 otherwise.
  227.  
  228. =head1 AUTHOR & COPYRIGHTS
  229.  
  230. Please see the Net::SSH::Perl manpage for author, copyright,
  231. and license information.
  232.  
  233. =cut
  234.