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 / AuthLDAP.pm < prev    next >
Encoding:
Perl POD Document  |  2001-07-13  |  5.1 KB  |  188 lines

  1. package Apache::AuthLDAP;
  2.  
  3. use strict;
  4. use Apache::Constants ':common';
  5. use Net::LDAPapi;
  6.  
  7. $Apache::AuthLDAP::VERSION = '0.21';
  8.  
  9. sub handler
  10. {
  11.    my $r = shift;
  12.    my ($res, $sent_pwd) = $r->get_basic_auth_pw;
  13.    return $res if $res;
  14.  
  15.    my $name = $r->connection->user;
  16.  
  17.    my $basedn = $r->dir_config('BaseDN') || "";
  18.    my $ldapserver = $r->dir_config('LDAPServer') || "localhost";
  19.    my $ldapport = $r->dir_config('LDAPPort') || 389;
  20.    my $uidattr = $r->dir_config('UIDAttr') || "uid";
  21.  
  22.    if ($sent_pwd eq "")
  23.    {
  24.       $r->note_basic_auth_failure;
  25.       $r->log_reason("user $name: no password supplied",$r->uri);
  26.       return AUTH_REQUIRED;
  27.    }
  28.  
  29.    my $ld = new Net::LDAPapi($ldapserver,$ldapport);
  30.    if ($ld <= 0)
  31.    {
  32.       $r->note_basic_auth_failure;
  33.       $r->log_reason("user $name: LDAP Connection Failed",$r->uri);
  34.       return AUTH_REQUIRED;
  35.    }
  36.  
  37.    if ($ld->bind_s != LDAP_SUCCESS)
  38.    {
  39.       $r->note_basic_auth_failure;
  40.       $r->log_reason("user $name: LDAP Initial Bind Failed",$r->uri);
  41.       return AUTH_REQUIRED;
  42.    }
  43.  
  44.    my $filter = "$uidattr=$name";
  45.    my $status = $ld->search_s($basedn,LDAP_SCOPE_SUBTREE,$filter,["c"],1);
  46.    if ($status != LDAP_SUCCESS)
  47.    {
  48.       $r->note_basic_auth_failure;
  49.       $r->log_reason("user $name: ldap search failed",$r->uri);
  50.       $ld->unbind;
  51.       return AUTH_REQUIRED;
  52.    }
  53.  
  54.    if ($ld->count_entries != 1)
  55.    {
  56.       $r->note_basic_auth_failure;
  57.       $r->log_reason("user $name: username not found",$r->uri);
  58.       $ld->unbind;
  59.       return AUTH_REQUIRED;
  60.    }
  61.  
  62.    $ld->first_entry;
  63.  
  64.    my $dn = $ld->get_dn;
  65.  
  66.    $status = $ld->bind_s($dn,$sent_pwd);
  67.    if ($status == LDAP_SUCCESS)
  68.    {
  69.       $r->push_handlers(PerlAuthzHandler => \&authz);
  70.       $ld->unbind;
  71.       return OK;
  72.    }
  73.  
  74.    $ld->unbind;
  75.    $r->note_basic_auth_failure;
  76.    $r->log_reason("user $name: password mismatch", $r->uri);
  77.    return AUTH_REQUIRED;
  78. }
  79.  
  80. sub authz
  81. {
  82.    my $r = shift;
  83.    my $requires = $r->requires;
  84.    return OK unless $requires;
  85.  
  86.    my $name = $r->connection->user;
  87.  
  88.    my $basedn = $r->dir_config('BaseDN') || "";
  89.    my $ldapserver = $r->dir_config('LDAPServer') || "localhost";
  90.    my $ldapport = $r->dir_config('LDAPPort') || 389;
  91.    my $uidattr = $r->dir_config('UIDAttr') || "uid";
  92.  
  93.    for my $req (@$requires)
  94.    {
  95.       my ($require, @rest) = split /\s+/, $req->{requirement};
  96.  
  97.       if ($require eq "user")
  98.       {
  99.          return OK if grep $name eq $_, @rest;
  100.       } elsif ($require eq "valid-user")
  101.       {
  102.          return OK;
  103.       } else {
  104.          my $ld = new Net::LDAPapi($ldapserver,$ldapport);
  105.          $ld->bind_s;
  106.          my $filter = "(&(|($require=" . join(")($require=",@rest) .
  107.                "))($uidattr=$name))";
  108.          my $status = $ld->search_s($basedn,LDAP_SCOPE_SUBTREE,$filter,["c"],1);
  109.          if ($status != LDAP_SUCCESS)
  110.          {
  111.             $r->note_basic_auth_failure;
  112.             $r->log_reason("LDAP Lookup Failed",$r->uri);
  113.             $ld->unbind;
  114.             return AUTH_REQUIRED;
  115.          }
  116.          if ($ld->count_entries == 1)
  117.          {
  118.             $ld->unbind;
  119.             return OK;
  120.          }
  121.          $ld->unbind;
  122.       }
  123.    }
  124.  
  125.    $r->note_basic_auth_failure;
  126.    $r->log_reason("user $name: not authorized", $r->uri);
  127.    return AUTH_REQUIRED;
  128. }
  129.  
  130. 1;
  131. __END__
  132.  
  133. =head1 NAME
  134.  
  135. Apache::AuthLDAP - mod_perl LDAP Access Control and Authentication Module
  136.  
  137. =head1 SYNOPSIS
  138.  
  139.     <Directory /foo/bar>
  140.     # Authentication Realm and Type (only Basic supported)
  141.     AuthName "Foo Bar Authentication"
  142.     AuthType Basic
  143.  
  144.     # Any of the following variables can be set.  Defaults are listed
  145.     # to the right.
  146.     PerlSetVar BaseDN o=Foo,c=Bar        # Default:  Empty String ("")
  147.     PerlSetVar LDAPServer ldap.foo.com   # Default: localhost
  148.     PerlSetVar LDAPPort 389              # Default: 389 (standard LDAP port)
  149.     PerlSetVar UIDAttr uid               # Default: uid
  150.  
  151.     PerlAuthenHandler Apache::AuthLDAP
  152.  
  153.     # Require lines can be any of the following:
  154.     #
  155.     require valid-user             # Any Valid LDAP User
  156.     require user uid1 uid2 uid2    # Allow Any User in List
  157.     require ldapattrib val1 val2   # Allow Any User w/ Entry Containing
  158.                                    # Matching Attribute and Value
  159.     </Directory>
  160.  
  161.     These directives can also be used in a .htaccess file.
  162.  
  163. = head1 DESCRIPTION
  164.  
  165. This perl module is designed to work with mod_perl and my Net::LDAPapi
  166. module (http://www.wwa.com/~donley/).
  167.  
  168. This version of the module does not support access control based on
  169. LDAP groups, but the next release will.  It does support a handy access
  170. control based on attribute and value pairs.  This can be used to restrict
  171. access to people whose LDAP entries contain a given department number, etc...
  172.  
  173. I welcome feedback on this module and the Net::LDAPapi module.
  174.  
  175. =head1 AUTHOR
  176.  
  177. Clayton Donley <donley@wwa.com>
  178.  
  179. =head1 COPYRIGHT
  180.  
  181. Copyright (c) 1998 Clayton Donley
  182.  
  183. This library is free software; you can redistribute it and/or modify
  184. it under the same terms as Perl itself.
  185.  
  186. =cut
  187.  
  188.