Skip to content

Commit d483a34

Browse files
author
blackhedd
committed
Added a patch by Kouhei Sutou which supports start_tls encryption.
1 parent ae298d2 commit d483a34

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
* Added some support for SNMP data-handling.
2929
* Extended support for server-reported error messages. This was provisionally
3030
added to Net::LDAP#add, and eventually will be added to other methods.
31+
* Belatedly added a patch contributed by Kouhei Sutou last October.
32+
The patch adds start_tls support.
3133

3234

3335
== Net::LDAP 0.0.4: August 15, 2006

lib/net/ber.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ module BERParser
124124
:constructed => {
125125
16 => :array,
126126
17 => :array
127+
},
128+
:context_specific => {
129+
:primitive => {
130+
10 => :integer
131+
}
127132
}
128133
}
129134
})

lib/net/ldap.rb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ class LdapError < Exception; end
321321
DefaultAuth = {:method => :anonymous}
322322
DefaultTreebase = "dc=com"
323323

324+
StartTlsOid = "1.3.6.1.4.1.1466.20037"
324325

325326
ResultStrings = {
326327
0 => "Success",
@@ -473,8 +474,9 @@ def authenticate username, password
473474
# unencrypted connections.]</i>
474475
#
475476
def encryption args
476-
if args == :simple_tls
477-
args = {:method => :simple_tls}
477+
case args
478+
when :simple_tls, :start_tls
479+
args = {:method => args}
478480
end
479481
@encryption = args
480482
end
@@ -1114,6 +1116,11 @@ def initialize server
11141116
# OBSERVE: WE REPLACE the value of @conn, which is presumed to be a connected
11151117
# TCPsocket object.
11161118
#
1119+
# The start_tls method is supported by many servers over the standard LDAP port.
1120+
# It does not require an alternative port for encrypted communications, as with
1121+
# simple_tls.
1122+
# Thanks for Kouhei Sutou for generously contributing the :start_tls path.
1123+
#
11171124
def setup_encryption args
11181125
case args[:method]
11191126
when :simple_tls
@@ -1123,6 +1130,24 @@ def setup_encryption args
11231130
@conn.connect
11241131
@conn.sync_close = true
11251132
# additional branches requiring server validation and peer certs, etc. go here.
1133+
when :start_tls
1134+
raise LdapError.new("openssl unavailable") unless $net_ldap_openssl_available
1135+
msgid = next_msgid.to_ber
1136+
request = [StartTlsOid.to_ber].to_ber_appsequence( Net::LdapPdu::ExtendedRequest )
1137+
request_pkt = [msgid, request].to_ber_sequence
1138+
@conn.write request_pkt
1139+
be = @conn.read_ber(AsnSyntax)
1140+
raise LdapError.new("no start_tls result") if be.nil?
1141+
pdu = Net::LdapPdu.new(be)
1142+
raise LdapError.new("no start_tls result") if pdu.nil?
1143+
if pdu.result_code.zero?
1144+
ctx = OpenSSL::SSL::SSLContext.new
1145+
@conn = OpenSSL::SSL::SSLSocket.new(@conn, ctx)
1146+
@conn.connect
1147+
@conn.sync_close = true
1148+
else
1149+
raise LdapError.new("start_tls failed: #{pdu.result_code}")
1150+
end
11261151
else
11271152
raise LdapError.new( "unsupported encryption method #{args[:method]}" )
11281153
end

lib/net/ldap/pdu.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class LdapPdu
4747
DeleteResponse = 11
4848
ModifyRDNResponse = 13
4949
SearchResultReferral = 19
50+
ExtendedRequest = 23
51+
ExtendedResponse = 24
5052

5153
attr_reader :msg_id, :app_tag
5254
attr_reader :search_dn, :search_attributes, :search_entry
@@ -114,6 +116,8 @@ def initialize ber_object
114116
parse_bind_request ber_object[1]
115117
when UnbindRequest
116118
parse_unbind_request ber_object[1]
119+
when ExtendedResponse
120+
parse_ldap_result ber_object[1]
117121
else
118122
raise LdapPduError.new( "unknown pdu-type: #{@app_tag}" )
119123
end

0 commit comments

Comments
 (0)