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 / AuthPerLDAP.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-13  |  4.6 KB  |  171 lines

  1. package Apache::AuthPerLDAP;
  2.  
  3. use mod_perl;
  4. use Apache::Constants qw(OK AUTH_REQUIRED);
  5. use Mozilla::LDAP::Conn;
  6.  
  7. use strict;
  8.  
  9. $Apache::AuthPerLDAP::VERSION = '0.5';
  10.  
  11. sub handler {
  12.     my $r = shift;
  13.     my ($result, $password) = $r->get_basic_auth_pw;
  14.     return $result if $result;
  15.  
  16.     my $username = $r->connection->user;
  17.  
  18.     my $basedn = $r->dir_config('BaseDN') || "";
  19.     my $ldapserver = $r->dir_config('LDAPServer') || "localhost";
  20.     my $ldapport = $r->dir_config('LDAPPort') || 389;
  21.     my $uidattr = $r->dir_config('UIDAttr') || "uid";
  22.  
  23.     if ($password eq "") {
  24.         $r->note_basic_auth_failure;
  25.         $r->log_reason("user $username: no password supplied",$r->uri);
  26.         return AUTH_REQUIRED;
  27.     }
  28.  
  29.     my $conn = new Mozilla::LDAP::Conn({ "host" => $ldapserver, 
  30.                                          "port" => $ldapport} );
  31.     unless($conn) {
  32.         $r->note_basic_auth_failure;
  33.         $r->log_reason("user $username: LDAP Connection Failed",$r->uri);
  34.         return AUTH_REQUIRED;
  35.     }
  36. #
  37. # Attempt to find the user using as user attribute the value of UIDAttr
  38. #
  39.     my $entry = $conn->search($basedn, "SUB", "($uidattr=$username)", 0, ($uidattr));
  40.  
  41.     unless ($entry) {
  42.         $r->note_basic_auth_failure;
  43.         $r->log_reason("user $username: username not found",$r->uri);
  44.         return AUTH_REQUIRED;
  45.     }
  46.  
  47. # Found username in LDAP database, get its DN
  48.  
  49.     my $dn = $entry->getDN();
  50.  
  51. #
  52. # Try to rebind with the users DN and password.
  53. #
  54.  
  55.     unless (($dn ne "") && ($conn->simpleAuth($dn, $password))) {
  56.         $r->note_basic_auth_failure;
  57.         $r->log_reason("user $username: invalid password", $r->uri);
  58.         return AUTH_REQUIRED;
  59.     }
  60.  
  61. return OK;
  62.  
  63. } # End of handler()
  64.  
  65. 1;
  66.  
  67. __END__
  68.  
  69. =head1 NAME
  70.  
  71. Apache::AuthPerLDAP - mod_perl PerLDAP Authentication Module
  72.  
  73. =head1 SYNOPSIS
  74.  
  75.     <Directory /foo/bar>
  76.     # Authentication Realm and Type (only Basic supported)
  77.     AuthName "Foo Bar Authentication"
  78.     AuthType Basic
  79.  
  80.     # Any of the following variables can be set.
  81.     # Defaults are listed to the right.
  82.     PerlSetVar BaseDN o=Foo,c=Bar        # Default: ""  (empty String)
  83.     PerlSetVar LDAPServer ldap.foo.com   # Default: localhost
  84.     PerlSetVar LDAPPort 389              # Default: 389 (standard LDAP port)
  85.     PerlSetVar UIDAttr uid               # Default: uid
  86.     require valid-user
  87.  
  88.     PerlAuthenHandler Apache::AuthPerLDAP
  89.  
  90.     </Directory>
  91.  
  92.     These directives can also be used in a .htaccess file.
  93.  
  94. =head1 DESCRIPTION
  95.  
  96. AuthPerLDAP provides Basic Authentication, with username/password
  97. lookups against an LDAP server, using Netscape's PerLDAP kit.
  98.  
  99. It is heavily based on Clayton Donley's Apache::AuthLDAP module,
  100. but uses the newer Netscape PerLDAP (Mozilla::LDAP), which in turn
  101. uses the Netscape Directory SDK for C. Thus Donley's original
  102. Net::LDAPapi module and library is no longer required.
  103.  
  104. It requires mod_perl and PerLDAP (v1.2 or later).
  105. Building mod_perl with:
  106.  
  107. perl Makefile.PL PERL_AUTHEN=1 PERL_STACKED_HANDLERS=1 PERL_GET_SET_HANDLERS
  108.  
  109. works for me. If this module is the only Apache/Perl module you are going to use,
  110. you probably don't need anything but the PERL_AUTHEN hook enabled.
  111.  
  112. Unlike Donley's Apache::AuthLDAP module, AuthPerLDAP is only used for
  113. authentication, and thus only supports the require-user directive.
  114. If a user enters the correct username and password, the authentication
  115. is considered to be OK.
  116.  
  117. =head1 TODO
  118.  
  119. =over 4
  120.  
  121. =item *
  122.  
  123. Find out more about these messages in the error_log:
  124. "child pid 5244 exit signal Segmentation Fault (11)"
  125.  
  126. =item *
  127.  
  128. Further testing.
  129.  
  130. =item *
  131.  
  132. More detailed documentation.
  133.  
  134. =item *
  135.  
  136. Some examples of how to setup and use this module.
  137.  
  138. =back
  139.  
  140. =head1 CREDITS
  141.  
  142. Apache::AuthPerLDAP is greatly inspired by the original Apache::AuthLDAP
  143. written by Clayton Donley.
  144.  
  145. Adoption to PerLDAP was done by reading the PerLDAP source and documentation
  146. provided by Netscape Corp. and Leif Hedstrom, found at www.perldap.org.
  147.  
  148. The new book published by O'Reilly & Associates, and authored by Lincoln Stein
  149. and Doug MacEachern helped clarify many mod_perl issues I previously had
  150. problems with: "Writing Apache Modules with Perl and C" (www.modperl.com).
  151.  
  152. Andreas K. Sorensen provided usefull Perl wisdom during debugging.
  153.  
  154. =head1 AUTHOR
  155.  
  156. Henrik Strom <henrik@computer.org>
  157.  
  158. =head1 COPYRIGHT
  159.  
  160. Copyright (c) 1999 Henrik Strom
  161.  
  162. This library is free software; you can redistribute it and/or modify
  163. it under the same terms as Perl itself.
  164.  
  165. =head1 SEE ALSO
  166.  
  167. mod_perl(1), I<Mozilla::LDAP::Conn>, I<Apache::AuthenCache>.
  168.  
  169. =cut
  170.  
  171.