def Base.connect(config={})
@@config = {}
@@config[:host] = config[:host] || @@host
@@config[:port] = config[:port] || @@port
if config[:base]
Base.class_eval "def Base.base\n'\#{config[:base]}'\nend\n"
end
@@config[:bind_format] = config[:bind_format] || @@bind_format
@@logger = config[:logger] || nil
if @@logger.nil?
@@logger = Log4r::Logger.new('activeldap')
@@logger.level = Log4r::OFF
Log4r::StderrOutputter.new 'console'
@@logger.add('console')
end
user = nil
password_block = nil
@@config[:allow_anonymous] = true
@@config[:try_sasl] = false
@@config[:user] = config[:user] || user
@@config[:allow_anonymous] = config[:allow_anonymous] if config.has_key? :allow_anonymous
@@config[:try_sasl] = config[:try_sasl]
@@config[:password_block] = config[:password_block] if config.has_key? :password_block
@@config[:user] = ENV['USER'] unless @@config[:user]
begin
@@conn = LDAP::SSLConn.new(@@config[:host], @@config[:port], true)
rescue
@@logger.warn "Warning: Failed to connect using TLS!"
begin
@@logger.warn "Warning: Attempting SSL connection . . ."
@@conn = LDAP::SSLConn.new(@@config[:host], @@config[:port], false)
@@schema = @@conn.schema() if @@schema.nil?
rescue
@@logger.warn "Warning: Attempting unencrypted connection . . ."
@@conn = LDAP::Conn.new(@@config[:host], @@config[:port])
end
end
@@logger.debug "Connected to #{@@config[:host]}:#{@@config[:port]}."
@@conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
do_bind
begin
@@schema = @@conn.schema() if @@schema.nil?
rescue => detail
raise ConnectionError, "#{detail.exception} - LDAP connection failure, or server does not support schema queries."
end
return true
end