home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / SMTP_auth.pm < prev    next >
Encoding:
Perl POD Document  |  2003-08-07  |  5.1 KB  |  202 lines

  1. # Net::SMTP_auth.pm
  2. #
  3. # alex pleiner 2001, 2003, zeitform Internet Dienste
  4. # thanks to Graham Barr <gbarr@pobox.com> for Net::SMTP
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the same terms as Perl itself.
  7.  
  8. # Net::SMTP_auth is a small extension to G. Barr's Net::SMTP
  9. # to authenticate to an SMTP server using one of the AUTH
  10. # methods provided by Authen::SASL (see RFC2554 for details).
  11. # This module can be expanded and is a very first implementation.
  12.  
  13. package Net::SMTP_auth;
  14.  
  15. require 5.001;
  16.  
  17. use strict;
  18. use vars qw($VERSION @ISA);
  19. use Socket 1.3;
  20. use Carp;
  21. use IO::Socket;
  22. use Net::Cmd;
  23. use Net::Config;
  24. use Net::SMTP;
  25. use MIME::Base64;
  26. use Digest::HMAC_MD5 qw(hmac_md5_hex);
  27. use Authen::SASL;
  28.  
  29. $VERSION = "0.07";
  30.  
  31. @ISA = qw(Net::SMTP);
  32.  
  33. # all other method taken from Net::SMTP
  34.  
  35. sub auth_types {
  36.   @_ == 1 or croak 'usage: $pop3->auth_types()';
  37.   my $me = shift;
  38.  
  39.   if (exists ${*$me}{'net_smtp_esmtp'}) {
  40.  
  41.     my $esmtp = ${*$me}{'net_smtp_esmtp'};
  42.  
  43.     if(exists $esmtp->{AUTH}) {
  44.       return wantarray ? split(/\s+/, $esmtp->{AUTH}) : $esmtp->{AUTH};
  45.     }
  46.   }
  47.  
  48.   return;
  49. }
  50.  
  51.  
  52. sub auth {
  53.   @_ == 4 or croak 'usage: $smtp->auth( AUTH, USER, PASS )';
  54.   my ($me, $auth, $user, $pass) = @_;
  55.  
  56.   my $sasl = Authen::SASL->new(
  57.                    mechanism => uc($auth),
  58.                    callback => {
  59.                         authname => $user,
  60.                         user     => $user,
  61.                         pass     => $pass,
  62.                        },
  63.                   );
  64.   return unless $sasl;
  65.   my $host = ${*$me}{'net_smtp_host'};
  66.   my $conn = $sasl->client_new("smtp", $host);#, "noplaintext noanonymous");
  67.  
  68.   $me->_AUTH($auth) or return;
  69.  
  70.   if ( $me->code() == 334 ) {
  71.  
  72.     if (my $initial = $conn->client_start)
  73.       {
  74.     $me->command(encode_base64($initial, ''))->response();
  75.     return 1 if $me->code() == 235;
  76.       }
  77.  
  78.     while ( $me->code() == 334 )
  79.       {
  80.     my $message = decode_base64($me->message());
  81.     my $return = $conn->client_step($message);
  82.     $me->command(encode_base64($return, ''))->response();
  83.     return 1 if $me->code() == 235;
  84.     return   if $me->code() == 535;
  85.       }
  86.  
  87.   }
  88. }
  89.  
  90.  
  91. sub _AUTH { shift->command("AUTH", @_)->response()  == CMD_MORE }
  92.  
  93. 1;
  94.  
  95.  
  96. __END__
  97.  
  98. =head1 NAME
  99.  
  100. Net::SMTP_auth - Simple Mail Transfer Protocol Client with AUTHentication
  101.  
  102. =head1 SYNOPSIS
  103.  
  104.     use Net::SMTP_auth;
  105.  
  106.     # Constructors
  107.     $smtp = Net::SMTP_auth->new('mailhost');
  108.     $smtp = Net::SMTP_auth->new('mailhost', Timeout => 60);
  109.  
  110. =head1 DESCRIPTION
  111.  
  112. This module implements a client interface to the SMTP and ESMTP
  113. protocol AUTH service extension, enabling a perl5 application to talk 
  114. to and authenticate against SMTP servers. This documentation assumes 
  115. that you are familiar with the concepts of the SMTP protocol described 
  116. in RFC821 and with the AUTH service extension described in RFC2554.
  117.  
  118. A new Net::SMTP_auth object must be created with the I<new> method. Once
  119. this has been done, all SMTP commands are accessed through this object.
  120.  
  121. The Net::SMTP_auth class is a subclass of Net::SMTP, which itself is
  122. a subclass of Net::Cmd and IO::Socket::INET.
  123.  
  124. =head1 EXAMPLES
  125.  
  126. This example authenticates via CRAM-MD5 and sends a small message to 
  127. the postmaster at the SMTP server known as mailhost:
  128.  
  129.     #!/usr/bin/perl -w
  130.  
  131.     use Net::SMTP_auth;
  132.  
  133.     $smtp = Net::SMTP_auth->new('mailhost');
  134.     $smtp->auth('CRAM-MD5', 'user', 'password');
  135.  
  136.     $smtp->mail($ENV{USER});
  137.     $smtp->to('postmaster');
  138.  
  139.     $smtp->data();
  140.     $smtp->datasend("To: postmaster\n");
  141.     $smtp->datasend("\n");
  142.     $smtp->datasend("A simple test message\n");
  143.     $smtp->dataend();
  144.  
  145.     $smtp->quit;
  146.  
  147. =head1 CONSTRUCTOR
  148.  
  149. =over 4
  150.  
  151. =item new Net::SMTP_auth [ HOST, ] [ OPTIONS ]
  152.  
  153. This is the constructor for a new Net::SMTP_auth object. It is
  154. taken from Net::SMTP as all other methods (except I<auth> and 
  155. I<auth_types>) are, too.
  156.  
  157. =head1 METHODS
  158.  
  159. Unless otherwise stated all methods return either a I<true> or I<false>
  160. value, with I<true> meaning that the operation was a success. When a method
  161. states that it returns a value, failure will be returned as I<undef> or an
  162. empty list.
  163.  
  164. =over 4
  165.  
  166. =item auth_types ()
  167.  
  168. Returns the AUTH methods supported by the server as an array or in a 
  169. space separated string. This string is exacly the line given by the SMTP 
  170. server after the C<EHLO> command containing the keyword C<AUTH>.
  171.  
  172. =item auth ( AUTH, USER, PASSWORD )
  173.  
  174. Authenticates the user C<USER> via the authentication method C<AUTH>
  175. and the password C<PASSWORD>. Returns I<true> if successful and I<false>
  176. if the authentication failed. Remember that the connection is not closed
  177. if the authentication fails. You may issue a different authentication 
  178. attempt. If you once are successfully authenticated, you cannot send
  179. the C<AUTH> command again.
  180.  
  181. =back
  182.  
  183. =head1 SEE ALSO
  184.  
  185. L<Net::SMTP> and L<Net::Cmd>
  186.  
  187. =head1 AUTHOR
  188.  
  189. Alex Pleiner <alex@zeitform.de>, zeitform Internet Dienste.
  190. Thanks to Graham Barr <gbarr@pobox.com> for Net::SMTP.
  191.  
  192. =head1 COPYRIGHT
  193.  
  194. Copyright (c) 2001, 2003 zeitform Internet Dienste. All rights reserved.
  195. This program is free software; you can redistribute it and/or modify
  196. it under the same terms as Perl itself.
  197.  
  198. =cut
  199.  
  200.  
  201.  
  202.