diff --git a/lib/net/ldap/connection.rb b/lib/net/ldap/connection.rb index f8ba0b61..1cbcbb67 100644 --- a/lib/net/ldap/connection.rb +++ b/lib/net/ldap/connection.rb @@ -31,26 +31,27 @@ def socket_class=(socket_class) @socket_class = socket_class end - def prepare_socket(server) + def prepare_socket(server, timeout=nil) socket = server[:socket] encryption = server[:encryption] @conn = socket - setup_encryption encryption if encryption + setup_encryption(encryption, timeout) if encryption end def open_connection(server) hosts = server[:hosts] encryption = server[:encryption] + timeout = server[:connect_timeout] || DefaultConnectTimeout socket_opts = { - connect_timeout: server[:connect_timeout] || DefaultConnectTimeout, + connect_timeout: timeout, } errors = [] hosts.each do |host, port| begin - prepare_socket(server.merge(socket: @socket_class.new(host, port, socket_opts))) + prepare_socket(server.merge(socket: @socket_class.new(host, port, socket_opts)), timeout) return rescue Net::LDAP::Error, SocketError, SystemCallError, OpenSSL::SSL::SSLError => e @@ -76,7 +77,7 @@ def close end end - def self.wrap_with_ssl(io, tls_options = {}) + def self.wrap_with_ssl(io, tls_options = {}, timeout=nil) raise Net::LDAP::NoOpenSSLError, "OpenSSL is unavailable" unless Net::LDAP::HasOpenSSL ctx = OpenSSL::SSL::SSLContext.new @@ -86,7 +87,26 @@ def self.wrap_with_ssl(io, tls_options = {}) ctx.set_params(tls_options) unless tls_options.empty? conn = OpenSSL::SSL::SSLSocket.new(io, ctx) - conn.connect + + begin + if timeout + conn.connect_nonblock + else + conn.connect + end + rescue IO::WaitReadable + if IO.select([conn], nil, nil, timeout) + retry + else + raise Errno::ETIMEDOUT, "OpenSSL connection read timeout" + end + rescue IO::WaitWritable + if IO.select(nil, [conn], nil, timeout) + retry + else + raise Errno::ETIMEDOUT, "OpenSSL connection write timeout" + end + end # Doesn't work: # conn.sync_close = true @@ -123,11 +143,11 @@ def self.wrap_with_ssl(io, tls_options = {}) # communications, as with simple_tls. Thanks for Kouhei Sutou for # generously contributing the :start_tls path. #++ - def setup_encryption(args) + def setup_encryption(args, timeout=nil) args[:tls_options] ||= {} case args[:method] when :simple_tls - @conn = self.class.wrap_with_ssl(@conn, args[:tls_options]) + @conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout) # additional branches requiring server validation and peer certs, etc. # go here. when :start_tls @@ -144,7 +164,7 @@ def setup_encryption(args) end if pdu.result_code.zero? - @conn = self.class.wrap_with_ssl(@conn, args[:tls_options]) + @conn = self.class.wrap_with_ssl(@conn, args[:tls_options], timeout) else raise Net::LDAP::StartTLSError, "start_tls failed: #{pdu.result_code}" end diff --git a/test/test_ldap_connection.rb b/test/test_ldap_connection.rb index 6bb027ac..ba6289b3 100644 --- a/test/test_ldap_connection.rb +++ b/test/test_ldap_connection.rb @@ -291,7 +291,7 @@ def test_queued_read_setup_encryption_with_start_tls and_return(result2) mock.should_receive(:write) conn = Net::LDAP::Connection.new(:socket => mock) - flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, {}). + flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, {}, nil). and_return(mock) conn.next_msgid # simulates ongoing query