blib/lib/Apache/AuthLDAP.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 143 CC 14 SCAR 39.9
CC
14
Cov
0%
handler
72.0
authz
56.0
BEGIN
2.0
CRAP 54.1
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__