blib/lib/Apache/AuthenLDAP.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 149 CC 14 SCAR 50.5
CC
14
Cov
0%
handler
210.0
BEGIN
2.0
BEGIN
2.0
CRAP 155.3
1 # $Id: AuthenLDAP.pm,v 1.14 2003/06/23 18:38:59 cgilmore Exp $
2 #
3 # Author : Jason Bodnar, Christian Gilmore
4 # Created On : Dec 08 12:04:00 CDT 1999
5 # Status : Functional
6 #
7 # PURPOSE
8 # LDAP User Authentication
9 #
10 ###############################################################################
11 #
12 # IBM Public License Version 1.0
13 #
14 # THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS IBM
15 # PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR
16 # DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF
17 # THIS AGREEMENT.
18 #
19 # 1. DEFINITIONS
20 #
21 # "Contribution" means:
22 #
23 # a) in the case of International Business Machines Corporation
24 # ("IBM"), the Original Program, and
25 #
26 # b) in the case of each Contributor,
27 #
28 # i) changes to the Program, and
29 #
30 # ii) additions to the Program;
31 #
32 # where such changes and/or additions to the Program originate from
33 # and are distributed by that particular Contributor. A Contribution
34 # 'originates' from a Contributor if it was added to the Program by
35 # such Contributor itself or anyone acting on such Contributor's
36 # behalf. Contributions do not include additions to the Program
37 # which: (i) are separate modules of software distributed in
38 # conjunction with the Program under their own license agreement,
39 # and (ii) are not derivative works of the Program.
40 #
41 # "Contributor" means IBM and any other entity that distributes the
42 # Program.
43 #
44 # "Licensed Patents " mean patent claims licensable by a Contributor
45 # which are necessarily infringed by the use or sale of its
46 # Contribution alone or when combined with the Program.
47 #
48 # "Original Program" means the original version of the software
49 # accompanying this Agreement as released by IBM, including source
50 # code, object code and documentation, if any.
51 #
52 # "Program" means the Original Program and Contributions.
53 #
54 # "Recipient" means anyone who receives the Program under this
55 # Agreement, including all Contributors.
56 #
57 # 2. GRANT OF RIGHTS
58 #
59 # a) Subject to the terms of this Agreement, each Contributor hereby
60 # grants Recipient a non-exclusive, worldwide, royalty-free
61 # copyright license to reproduce, prepare derivative works of,
62 # publicly display, publicly perform, distribute and sublicense the
63 # Contribution of such Contributor, if any, and such derivative
64 # works, in source code and object code form.
65 #
66 # b) Subject to the terms of this Agreement, each Contributor hereby
67 # grants Recipient a non-exclusive, worldwide, royalty-free patent
68 # license under Licensed Patents to make, use, sell, offer to sell,
69 # import and otherwise transfer the Contribution of such
70 # Contributor, if any, in source code and object code form. This
71 # patent license shall apply to the combination of the Contribution
72 # and the Program if, at the time the Contribution is added by the
73 # Contributor, such addition of the Contribution causes such
74 # combination to be covered by the Licensed Patents. The patent
75 # license shall not apply to any other combinations which include
76 # the Contribution. No hardware per se is licensed hereunder.
77 #
78 # c) Recipient understands that although each Contributor grants the
79 # licenses to its Contributions set forth herein, no assurances are
80 # provided by any Contributor that the Program does not infringe the
81 # patent or other intellectual property rights of any other entity.
82 # Each Contributor disclaims any liability to Recipient for claims
83 # brought by any other entity based on infringement of intellectual
84 # property rights or otherwise. As a condition to exercising the
85 # rights and licenses granted hereunder, each Recipient hereby
86 # assumes sole responsibility to secure any other intellectual
87 # property rights needed, if any. For example, if a third party
88 # patent license is required to allow Recipient to distribute the
89 # Program, it is Recipient's responsibility to acquire that license
90 # before distributing the Program.
91 #
92 # d) Each Contributor represents that to its knowledge it has
93 # sufficient copyright rights in its Contribution, if any, to grant
94 # the copyright license set forth in this Agreement.
95 #
96 # 3. REQUIREMENTS
97 #
98 # A Contributor may choose to distribute the Program in object code
99 # form under its own license agreement, provided that:
100 #
101 # a) it complies with the terms and conditions of this Agreement;
102 #
103 # and
104 #
105 # b) its license agreement:
106 #
107 # i) effectively disclaims on behalf of all Contributors all
108 # warranties and conditions, express and implied, including
109 # warranties or conditions of title and non-infringement, and
110 # implied warranties or conditions of merchantability and fitness
111 # for a particular purpose;
112 #
113 # ii) effectively excludes on behalf of all Contributors all
114 # liability for damages, including direct, indirect, special,
115 # incidental and consequential damages, such as lost profits;
116 # iii) states that any provisions which differ from this Agreement
117 # are offered by that Contributor alone and not by any other party;
118 # and
119 #
120 # iv) states that source code for the Program is available from such
121 # Contributor, and informs licensees how to obtain it in a
122 # reasonable manner on or through a medium customarily used for
123 # software exchange.
124 #
125 # When the Program is made available in source code form:
126 #
127 # a) it must be made available under this Agreement; and
128 #
129 # b) a copy of this Agreement must be included with each copy of the
130 # Program.
131 #
132 # Each Contributor must include the following in a conspicuous
133 # location in the Program:
134 #
135 # Copyright © {date here}, International Business Machines
136 # Corporation and others. All Rights Reserved.
137 #
138 # In addition, each Contributor must identify itself as the originator
139 # of its Contribution, if any, in a manner that reasonably allows
140 # subsequent Recipients to identify the originator of the
141 # Contribution.
142 #
143 # 4. COMMERCIAL DISTRIBUTION
144 #
145 # Commercial distributors of software may accept certain
146 # responsibilities with respect to end users, business partners and
147 # the like. While this license is intended to facilitate the
148 # commercial use of the Program, the Contributor who includes the
149 # Program in a commercial product offering should do so in a manner
150 # which does not create potential liability for other Contributors.
151 # Therefore, if a Contributor includes the Program in a commercial
152 # product offering, such Contributor ("Commercial Contributor") hereby
153 # agrees to defend and indemnify every other Contributor ("Indemnified
154 # Contributor") against any losses, damages and costs (collectively
155 # "Losses") arising from claims, lawsuits and other legal actions
156 # brought by a third party against the Indemnified Contributor to the
157 # extent caused by the acts or omissions of such Commercial
158 # Contributor in connection with its distribution of the Program in a
159 # commercial product offering. The obligations in this section do not
160 # apply to any claims or Losses relating to any actual or alleged
161 # intellectual property infringement. In order to qualify, an
162 # Indemnified Contributor must: a) promptly notify the Commercial
163 # Contributor in writing of such claim, and b) allow the Commercial
164 # Contributor to control, and cooperate with the Commercial
165 # Contributor in, the defense and any related settlement negotiations.
166 # The Indemnified Contributor may participate in any such claim at its
167 # own expense.
168 #
169 # For example, a Contributor might include the Program in a commercial
170 # product offering, Product X. That Contributor is then a Commercial
171 # Contributor. If that Commercial Contributor then makes performance
172 # claims, or offers warranties related to Product X, those performance
173 # claims and warranties are such Commercial Contributor's
174 # responsibility alone. Under this section, the Commercial Contributor
175 # would have to defend claims against the other Contributors related
176 # to those performance claims and warranties, and if a court requires
177 # any other Contributor to pay any damages as a result, the Commercial
178 # Contributor must pay those damages.
179 #
180 # 5. NO WARRANTY
181 #
182 # EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS
183 # PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
184 # ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION,
185 # ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
186 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient
187 # is solely responsible for determining the appropriateness of using
188 # and distributing the Program and assumes all risks associated with
189 # its exercise of rights under this Agreement, including but not
190 # limited to the risks and costs of program errors, compliance with
191 # applicable laws, damage to or loss of data, programs or equipment,
192 # and unavailability or interruption of operations.
193 #
194 # 6. DISCLAIMER OF LIABILITY
195 #
196 # EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT
197 # NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT,
198 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
199 # (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON
200 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
201 # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
202 # THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS
203 # GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
204 # DAMAGES.
205 #
206 # 7. GENERAL
207 #
208 # If any provision of this Agreement is invalid or unenforceable under
209 # applicable law, it shall not affect the validity or enforceability
210 # of the remainder of the terms of this Agreement, and without further
211 # action by the parties hereto, such provision shall be reformed to
212 # the minimum extent necessary to make such provision valid and
213 # enforceable.
214 #
215 # If Recipient institutes patent litigation against a Contributor with
216 # respect to a patent applicable to software (including a cross-claim
217 # or counterclaim in a lawsuit), then any patent licenses granted by
218 # that Contributor to such Recipient under this Agreement shall
219 # terminate as of the date such litigation is filed. In addition, If
220 # Recipient institutes patent litigation against any entity (including
221 # a cross-claim or counterclaim in a lawsuit) alleging that the
222 # Program itself (excluding combinations of the Program with other
223 # software or hardware) infringes such Recipient's patent(s), then
224 # such Recipient's rights granted under Section 2(b) shall terminate
225 # as of the date such litigation is filed.
226 #
227 # All Recipient's rights under this Agreement shall terminate if it
228 # fails to comply with any of the material terms or conditions of this
229 # Agreement and does not cure such failure in a reasonable period of
230 # time after becoming aware of such noncompliance. If all Recipient's
231 # rights under this Agreement terminate, Recipient agrees to cease use
232 # and distribution of the Program as soon as reasonably practicable.
233 # However, Recipient's obligations under this Agreement and any
234 # licenses granted by Recipient relating to the Program shall continue
235 # and survive.
236 #
237 # IBM may publish new versions (including revisions) of this Agreement
238 # from time to time. Each new version of the Agreement will be given a
239 # distinguishing version number. The Program (including Contributions)
240 # may always be distributed subject to the version of the Agreement
241 # under which it was received. In addition, after a new version of the
242 # Agreement is published, Contributor may elect to distribute the
243 # Program (including its Contributions) under the new version. No one
244 # other than IBM has the right to modify this Agreement. Except as
245 # expressly stated in Sections 2(a) and 2(b) above, Recipient receives
246 # no rights or licenses to the intellectual property of any
247 # Contributor under this Agreement, whether expressly, by implication,
248 # estoppel or otherwise. All rights in the Program not expressly
249 # granted under this Agreement are reserved.
250 #
251 # This Agreement is governed by the laws of the State of New York and
252 # the intellectual property laws of the United States of America. No
253 # party to this Agreement will bring a legal action under this
254 # Agreement more than one year after the cause of action arose. Each
255 # party waives its rights to a jury trial in any resulting litigation.
256 #
257 ###############################################################################
258  
259  
260 # Package name
261 package Apache::AuthenLDAP;
262  
263  
264 # Required packages
265 use strict;
266 use Apache::Constants qw(:common);
267 use Apache::Log qw();
268 use Date::Calc qw(Date_to_Days Today);
269 use Net::LDAP qw(:all);
270  
271  
272 # Global variables
273 $Apache::AuthenLDAP::VERSION = '1.00';
274  
275  
276 ###############################################################################
277 ###############################################################################
278 # handler: hook into Apache/mod_perl API
279 ###############################################################################
280 ###############################################################################
281 sub handler {
282 my $r = shift;
283 return OK unless $r->is_initial_req; # only the first internal request
284 my ($res, $sent_pwd) = $r->get_basic_auth_pw;
285 return $res if $res;
286  
287 my $name = $r->connection->user;
288 unless ($name) {
289 $r->note_basic_auth_failure;
290 $r->log_reason("no username supplied", $r->uri);
291 return AUTH_REQUIRED;
292 }
293  
294 my $cache_result = $r->notes('AuthenCache');
295 if ($cache_result eq 'hit') {
296 $r->log->debug("handler: upstream cache hit for username=$name");
297 return OK;
298 }
299  
300 my $basedn = $r->dir_config('AuthenBaseDN') || '';
301 my $ldapserver = $r->dir_config('AuthenLDAPServer') || "localhost";
302 my $ldapport = $r->dir_config('AuthenLDAPPort') || 389;
303 my $uidattrtype = $r->dir_config('AuthenUidAttrType') || "uid";
304  
305 my $expire = lc($r->dir_config('AuthenExpire')) || 'false';
306 my $exp_attrtype = $r->dir_config('AuthenExpireAttrType') ||
307 'passwordIsExpired';
308 my $exp_lastmodattrtype =
309 $r->dir_config('AuthenExpireLastModAttrType') ||
310 'passwordModifyTimestamp';
311 my $exp_time = $r->dir_config('AuthenExpireTime') ||
312 186;
313 my $exp_redirect = $r->dir_config('AuthenExpireRedirect') || '';
314  
315 $r->log->debug("handler: ",
316 "AuthenBaseDN - $basedn; LDAPServer - $ldapserver; ",
317 "LDAPPort - $ldapport; UiaDttrType - $uidattrtype; ",
318 "Expire - $expire; ExireAttrType - $exp_attrtype; ",
319 "ExpireLastModAttrType - $exp_lastmodattrtype; ",
320 "ExpireTime - $exp_time; ExpireRedirect - $exp_redirect");
321  
322 if ($sent_pwd eq "") {
323 $r->note_basic_auth_failure;
324 $r->log_reason("user $name: no password supplied", $r->uri);
325 return AUTH_REQUIRED;
326 }
327  
328 # Connect to the server
329 my $ld;
330 unless ($ld = new Net::LDAP($ldapserver, port => $ldapport)) {
331 $r->note_basic_auth_failure;
332 $r->log_reason("user $name: LDAP Connection Failed", $r->uri);
333 return SERVER_ERROR;
334 }
335  
336 # Bind anonymously
337 my $msg = $ld->bind;
338 unless ($msg->code == LDAP_SUCCESS) {
339 $r->note_basic_auth_failure;
340 $r->log_reason("user $name: LDAP Initial Bind Failed: " . $msg->code .
341 " " . $msg->error, $r->uri);
342 return SERVER_ERROR;
343 }
344  
345 # Create the filter and search
346 my $filter = "($uidattrtype=$name)";
347 $r->log->debug("handler: Using filter: $filter");
348 $msg = $ld->search(base => $basedn, filter => $filter);
349 unless ($msg->code == LDAP_SUCCESS) {
350 $r->note_basic_auth_failure;
351 $r->log_reason("user $name: ldap search operation failed: " .
352 $msg->code . " " . $msg->error, $r->uri);
353 return SERVER_ERROR;
354 }
355  
356 # Did we receive any entries
357 unless ($msg->count) {
358 $r->note_basic_auth_failure;
359 $r->log_reason("user $name: username not found",$r->uri);
360 return AUTH_REQUIRED;
361 }
362  
363 # Only want the first if we've received more than one
364 my $entry = $msg->first_entry;
365 my $dn = $entry->dn;
366  
367 # Bind as the user we're authenticating
368 $msg = $ld->bind($dn, password => $sent_pwd);
369 unless ($msg->code == LDAP_SUCCESS) {
370 $r->note_basic_auth_failure;
371 $r->log_reason("user $name: password mismatch", $r->uri);
372 return AUTH_REQUIRED;
373 }
374  
375 $ld->unbind;
376  
377 if ($expire eq 'true') {
378 # Is the password set to expired in LDAP?
379 if (($entry->get($exp_attrtype))[0] eq 'true') {
380 $r->log->debug("handler: password flag expired");
381 $r->custom_response(FORBIDDEN, "$exp_redirect");
382 return FORBIDDEN;
383 }
384  
385 # Has the password passed the age limit?
386 my ($modyear, $modmonth, $modday) =
387 (($entry->get($exp_lastmodattrtype))[0] =~ /(\d{4})(\d{2})(\d{2})/);
388 my ($year, $month, $day) = Today([time]);
389 if (Date_to_Days($year, $month, $day) -
390 Date_to_Days($modyear, $modmonth, $modday) > $exp_time) {
391 $r->log->debug("handler: password age expired");
392 $r->custom_response(FORBIDDEN, "$exp_redirect");
393 return FORBIDDEN;
394 }
395 }
396  
397 # Everything's A-OK
398 return OK;
399 }
400  
401 1;
402  
403 __END__