blib/lib/Apache/TransLDAP.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 105 CC 9 SCAR 40.0
CC
9
Cov
0%
handler
90.0
BEGIN
2.0
BEGIN
2.0
CRAP 54.8
1 package Apache::TransLDAP;
2 package Apache::TransLDAP;
3  
4 use strict;
5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
6  
7 require Exporter;
8 require AutoLoader;
9 use Apache::Constants ':common';
10 use Net::LDAPapi;
11  
12 @ISA = qw(Exporter AutoLoader);
13 # Items to export into callers namespace by default. Note: do not export
14 # names by default without a very good reason. Use EXPORT_OK instead.
15 # Do not simply export all your public functions/methods/constants.
16 @EXPORT = qw(
17  
18 );
19 $VERSION = '0.20';
20  
21 sub handler
22 {
23 my $r = shift @_;
24  
25 my $URI = $r->uri;
26 my $USERDIR = $r->dir_config("UserDir") || "/users/";
27 my $LDAPSERVER = $r->dir_config("LDAPServer");
28 my $LDAPPORT = $r->dir_config("LDAPPort") || 389;
29 my $LDAPBASE = $r->dir_config("LDAPBase");
30 my $UIDATTR = $r->dir_config("UIDAttr") || "uid";
31 my $URIATTR = $r->dir_config("URIAttr") || "labeleduri";
32  
33 if ($URI =~ /^$USERDIR/)
34 {
35 $URI =~ s/$USERDIR//;
36 my $user;
37 if ($URI =~ /\//)
38 {
39 $URI =~ s/^(.*)\///;
40 $user = $1;
41 } else {
42 $URI =~ s/^(.*)$//;
43 $user = $1;
44 }
45  
46 if (!$user)
47 {
48 return DECLINED;
49 }
50  
51 my $ldap = new Net::LDAPapi($LDAPSERVER,$LDAPPORT);
52 $ldap->bind_s;
53  
54 my $filter = "($UIDATTR=$user)";
55  
56 my @attrs = ($URIATTR);
57  
58 if ($ldap->search_s($LDAPBASE,LDAP_SCOPE_SUBTREE,$filter,\@attrs,0)
59 != LDAP_SUCCESS)
60 {
61 $r->warn("Search Failed");
62 $ldap->unbind;
63 return DECLINED;
64 }
65  
66 if (!$ldap->first_entry)
67 {
68 $r->warn("No First Entry");
69 $ldap->unbind;
70 return DECLINED;
71 }
72  
73 my @uris = $ldap->get_values($URIATTR);
74  
75 $ldap->unbind;
76  
77 if ($#uris < 0)
78 {
79 $r->warn("No labeledURIs");
80 return DECLINED;
81 }
82  
83 if ($uris[0] =~ /\/$/)
84 {
85 $URI = $uris[0] . $URI;
86 } else {
87 $URI = $uris[0];
88 }
89  
90 $r->uri($URI);
91  
92 if ($r->args)
93 {
94 $URI .= "?" . $r->args;
95 }
96  
97 $r->content_type("text/html");
98 $r->header_out(Location=>$URI);
99 $r->header_out(URI=>$URI);
100 $r->status(301);
101 return (301);
102 }
103 return DECLINED;
104 }
105 1;
106 __END__