@@ -83,16 +83,6 @@ func (ls *Source) sanitizedUserDN(username string) (string, bool) {
8383
8484func (ls * Source ) findUserDN (l * ldap.Conn , name string ) (string , bool ) {
8585 log .Trace ("Search for LDAP user: %s" , name )
86- if ls .BindDN != "" && ls .BindPassword != "" {
87- err := l .Bind (ls .BindDN , ls .BindPassword )
88- if err != nil {
89- log .Debug ("Failed to bind as BindDN[%s]: %v" , ls .BindDN , err )
90- return "" , false
91- }
92- log .Trace ("Bound as BindDN %s" , ls .BindDN )
93- } else {
94- log .Trace ("Proceeding with anonymous LDAP search." )
95- }
9686
9787 // A search for the user.
9888 userFilter , ok := ls .sanitizedUserQuery (name )
@@ -203,20 +193,48 @@ func (ls *Source) SearchEntry(name, passwd string, directBind bool) *SearchResul
203193
204194 var ok bool
205195 userDN , ok = ls .sanitizedUserDN (name )
196+
206197 if ! ok {
207198 return nil
208199 }
200+
201+ err = bindUser (l , userDN , passwd )
202+ if err != nil {
203+ return nil
204+ }
205+
206+ if ls .UserBase != "" {
207+ // not everyone has a CN compatible with input name so we need to find
208+ // the real userDN in that case
209+
210+ userDN , ok = ls .findUserDN (l , name )
211+ if ! ok {
212+ return nil
213+ }
214+ }
209215 } else {
210216 log .Trace ("LDAP will use BindDN." )
211217
212218 var found bool
219+
220+ if ls .BindDN != "" && ls .BindPassword != "" {
221+ err := l .Bind (ls .BindDN , ls .BindPassword )
222+ if err != nil {
223+ log .Debug ("Failed to bind as BindDN[%s]: %v" , ls .BindDN , err )
224+ return nil
225+ }
226+ log .Trace ("Bound as BindDN %s" , ls .BindDN )
227+ } else {
228+ log .Trace ("Proceeding with anonymous LDAP search." )
229+ }
230+
213231 userDN , found = ls .findUserDN (l , name )
214232 if ! found {
215233 return nil
216234 }
217235 }
218236
219- if directBind || ! ls .AttributesInBind {
237+ if ! ls .AttributesInBind {
220238 // binds user (checking password) before looking-up attributes in user context
221239 err = bindUser (l , userDN , passwd )
222240 if err != nil {
0 commit comments