123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- class Ldap
- DEFAULT_PORT = 389
- attr_reader :base_dn, :host, :port
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def initialize(config)
- @config = config
-
- connection
- end
-
-
-
-
-
-
-
- def preferences
- connection.search_root_dse.to_h
- end
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- def search(filter, base: nil, scope: nil, attributes: nil, &)
- base ||= base_dn
- scope ||= Net::LDAP::SearchScope_WholeSubtree
- connection.search(
- base: base,
- filter: filter,
- scope: scope,
- attributes: attributes,
- return_result: false,
- &
- )
- end
-
-
-
-
-
-
-
-
-
- def entries?(*)
- found = false
- search(*) do |_entry|
- found = true
- break
- end
- found
- end
-
-
-
-
-
-
-
-
-
- def count(*)
- counter = 0
- search(*) do |_entry|
- counter += 1
- end
- counter
- end
- def connection
- @connection ||= begin
- attributes_from_config
- binded_connection
- end
- end
- private
- def binded_connection
-
- ldap = Net::LDAP.new(connection_params)
-
- if @bind_user && @bind_pw
- ldap.auth @bind_user, @bind_pw
- end
- return ldap if ldap.bind
- result = ldap.get_operation_result
- raise Exceptions::UnprocessableEntity, "Can't bind to '#{@host}', #{result.code}, #{result.message}"
- rescue => e
- Rails.logger.error e
- raise Exceptions::UnprocessableEntity, "Can't connect to '#{@host}' on port '#{@port}', #{e}"
- end
- def connection_params
- params = {
- host: @host,
- port: @port,
- }
- if @encryption
- params[:encryption] = @encryption
- end
-
-
- if @host == 'bluepages.ibm.com'
- params[:force_no_page] = true
- end
- params
- end
- def attributes_from_config
-
- @host = @config[:host]
- @port = @config[:port]
- parse_host
- handle_ssl_config
- handle_bind_crendentials
- @base_dn = @config[:base_dn]
-
-
- @port ||= DEFAULT_PORT
- end
- def parse_host
- return if @host !~ %r{\A([^:]+):(.+?)\z}
- @host = $1
- @port = $2.to_i
- end
- def handle_ssl_config
- return if @config.fetch(:ssl, 'off').eql?('off')
- ssl_default_port = DEFAULT_PORT
- if @config[:ssl].eql?('ssl')
- ssl_default_port = 636
- @encryption = {
- method: :simple_tls,
- }
- else
- @encryption = {
- method: :start_tls,
- }
- end
- @port ||= @config.fetch(:port, ssl_default_port)
- if @config[:ssl_verify]
- Certificate::ApplySSLCertificates.ensure_fresh_ssl_context
- @encryption[:tls_options] = OpenSSL::SSL::SSLContext::DEFAULT_PARAMS
- return
- end
- @encryption[:tls_options] = {
- verify_mode: OpenSSL::SSL::VERIFY_NONE
- }
- end
- def handle_bind_crendentials
- @bind_user = @config[:bind_user]
- @bind_pw = @config[:bind_pw]
- end
- end
|