From effcd419f3a36c35048faf2a0b6c27b3a1b8919d Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Fri, 6 Sep 2013 10:41:39 +0100
Subject: [PATCH] Bug 10842: Fixed bug in Auth_with_ldap

A bug in Auth_with_ldap was preventing ldapuser binds from
taking priority over anonymous binds when auth_by_bind was
set and an ldapuser and ldappassowrd were also set.
---
 C4/Auth_with_ldap.pm | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/C4/Auth_with_ldap.pm b/C4/Auth_with_ldap.pm
index 6143c9f..5e7a7d7 100644
--- a/C4/Auth_with_ldap.pm
+++ b/C4/Auth_with_ldap.pm
@@ -112,10 +112,10 @@ sub checkpw_ldap {
     my $userldapentry;
 
   if ( $ldap->{auth_by_bind} ) {
-    # Perform an anonymous bind
-    my $res = $db->bind;
+    # Perform initial bind (anonymous or otherwise)
+    my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword);
     if ( $res->code ) {
-      $debug and warn "Anonymous LDAP bind failed: ". description($res);
+      $debug and warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res);
       return 0;
     }
 
@@ -123,7 +123,7 @@ sub checkpw_ldap {
     my $search = search_method($db, $userid) or return 0;   # warnings are in the sub
     $userldapentry = $search->shift_entry;
 
-    # Perform a LDAP bind for the given username using the matched DN
+    # Perform a LDAP (re)bind for the given username using the matched DN
     $res = $db->bind( $userldapentry->dn, password => $password );
     if ( $res->code ) {
       $debug and warn "LDAP bind failed as kohauser $userid: ". description($res);
-- 
1.8.1.2