@@ -250,130 +250,11 @@ def next_msgid
250250 def bind ( auth )
251251 instrument "bind.net_ldap_connection" do |payload |
252252 payload [ :method ] = meth = auth [ :method ]
253- if [ :simple , :anonymous , :anon ] . include? ( meth )
254- bind_simple auth
255- elsif meth == :sasl
256- bind_sasl ( auth )
257- elsif meth == :gss_spnego
258- bind_gss_spnego ( auth )
259- else
260- raise Net ::LDAP ::AuthMethodUnsupportedError , "Unsupported auth method (#{ meth } )"
261- end
262- end
263- end
264-
265- #--
266- # Implements a simple user/psw authentication. Accessed by calling #bind
267- # with a method of :simple or :anonymous.
268- #++
269- def bind_simple ( auth )
270- user , psw = if auth [ :method ] == :simple
271- [ auth [ :username ] || auth [ :dn ] , auth [ :password ] ]
272- else
273- [ "" , "" ]
274- end
275-
276- raise Net ::LDAP ::BindingInformationInvalidError , "Invalid binding information" unless ( user && psw )
277-
278- message_id = next_msgid
279- request = [
280- LdapVersion . to_ber , user . to_ber ,
281- psw . to_ber_contextspecific ( 0 )
282- ] . to_ber_appsequence ( Net ::LDAP ::PDU ::BindRequest )
283-
284- write ( request , nil , message_id )
285- pdu = queued_read ( message_id )
286-
287- if !pdu || pdu . app_tag != Net ::LDAP ::PDU ::BindResult
288- raise Net ::LDAP ::NoBindResultError , "no bind result"
253+ adapter = Net ::LDAP ::AuthAdapter [ meth ]
254+ adapter . new ( self ) . bind ( auth )
289255 end
290-
291- pdu
292256 end
293257
294- #--
295- # Required parameters: :mechanism, :initial_credential and
296- # :challenge_response
297- #
298- # Mechanism is a string value that will be passed in the SASL-packet's
299- # "mechanism" field.
300- #
301- # Initial credential is most likely a string. It's passed in the initial
302- # BindRequest that goes to the server. In some protocols, it may be empty.
303- #
304- # Challenge-response is a Ruby proc that takes a single parameter and
305- # returns an object that will typically be a string. The
306- # challenge-response block is called when the server returns a
307- # BindResponse with a result code of 14 (saslBindInProgress). The
308- # challenge-response block receives a parameter containing the data
309- # returned by the server in the saslServerCreds field of the LDAP
310- # BindResponse packet. The challenge-response block may be called multiple
311- # times during the course of a SASL authentication, and each time it must
312- # return a value that will be passed back to the server as the credential
313- # data in the next BindRequest packet.
314- #++
315- def bind_sasl ( auth )
316- mech , cred , chall = auth [ :mechanism ] , auth [ :initial_credential ] ,
317- auth [ :challenge_response ]
318- raise Net ::LDAP ::BindingInformationInvalidError , "Invalid binding information" unless ( mech && cred && chall )
319-
320- message_id = next_msgid
321-
322- n = 0
323- loop {
324- sasl = [ mech . to_ber , cred . to_ber ] . to_ber_contextspecific ( 3 )
325- request = [
326- LdapVersion . to_ber , "" . to_ber , sasl
327- ] . to_ber_appsequence ( Net ::LDAP ::PDU ::BindRequest )
328-
329- write ( request , nil , message_id )
330- pdu = queued_read ( message_id )
331-
332- if !pdu || pdu . app_tag != Net ::LDAP ::PDU ::BindResult
333- raise Net ::LDAP ::NoBindResultError , "no bind result"
334- end
335-
336- return pdu unless pdu . result_code == Net ::LDAP ::ResultCodeSaslBindInProgress
337- raise Net ::LDAP ::SASLChallengeOverflowError , "sasl-challenge overflow" if ( ( n += 1 ) > MaxSaslChallenges )
338-
339- cred = chall . call ( pdu . result_server_sasl_creds )
340- }
341-
342- raise Net ::LDAP ::SASLChallengeOverflowError , "why are we here?"
343- end
344- private :bind_sasl
345-
346- #--
347- # PROVISIONAL, only for testing SASL implementations. DON'T USE THIS YET.
348- # Uses Kohei Kajimoto's Ruby/NTLM. We have to find a clean way to
349- # integrate it without introducing an external dependency.
350- #
351- # This authentication method is accessed by calling #bind with a :method
352- # parameter of :gss_spnego. It requires :username and :password
353- # attributes, just like the :simple authentication method. It performs a
354- # GSS-SPNEGO authentication with the server, which is presumed to be a
355- # Microsoft Active Directory.
356- #++
357- def bind_gss_spnego ( auth )
358- require 'ntlm'
359-
360- user , psw = [ auth [ :username ] || auth [ :dn ] , auth [ :password ] ]
361- raise Net ::LDAP ::BindingInformationInvalidError , "Invalid binding information" unless ( user && psw )
362-
363- nego = proc { |challenge |
364- t2_msg = NTLM ::Message . parse ( challenge )
365- t3_msg = t2_msg . response ( { :user => user , :password => psw } ,
366- { :ntlmv2 => true } )
367- t3_msg . serialize
368- }
369-
370- bind_sasl ( :method => :sasl , :mechanism => "GSS-SPNEGO" ,
371- :initial_credential => NTLM ::Message ::Type1 . new . serialize ,
372- :challenge_response => nego )
373- end
374- private :bind_gss_spnego
375-
376-
377258 #--
378259 # Allow the caller to specify a sort control
379260 #
0 commit comments