HEX

Warning: set_time_limit() [function.set-time-limit]: Cannot set time limit - prohibited by configuration in /home/u547966/brikov.ru/www/wp-content/plugins/admin-menu-editor/menu-editor.php on line 745
Server: Apache
System: Linux 4.19.0-0.bpo.9-amd64 x86_64 at red40
User: u547966 (5490)
PHP: 5.3.29-mh2
Disabled: syslog, dl, popen, proc_open, proc_nice, proc_get_status, proc_close, proc_terminate, posix_mkfifo, chown, chgrp, accelerator_reset, opcache_reset, accelerator_get_status, opcache_get_status, pcntl_alarm, pcntl_fork, pcntl_waitpid, pcntl_wait, pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, pcntl_wifcontinued, pcntl_wexitstatus, pcntl_wtermsig, pcntl_wstopsig, pcntl_signal, pcntl_signal_dispatch, pcntl_get_last_error, pcntl_strerror, pcntl_sigprocmask, pcntl_sigwaitinfo, pcntl_sigtimedwait, pcntl_exec, pcntl_getpriority, pcntl_setpriority
Upload Files
File: //var/cache/puppet/lib/puppet_x/ldapquery.rb
# Class: PuppetX::LDAPquery
#

module PuppetX
  class LDAPquery
    attr_reader :content

    def initialize(
      filter,
      attributes = [],
      base = Puppet[:ldapbase],
      scope = 'sub'
    )
      @filter = filter
      @attributes = attributes
      @base = base

      return unless scope

      if scope == 'sub'
        @scope = Net::LDAP::SearchScope_WholeSubtree
      elsif scope == 'base'
        @scope = Net::LDAP::SearchScope_BaseObject
      elsif scope == 'single'
        @scope = Net::LDAP::SearchScope_SingleLevel
      else
        raise Puppet::ParseError, 'Received param "scope" not one of ["sub","base","single"]'
      end
    end

    def ldap_config
      # Load the configuration variables from Puppet
      required_vars = [
        :ldapserver,
        :ldapport
      ]

      required_vars.each do |r|
        unless Puppet[r]
          raise Puppet::ParseError, "Missing required setting '#{r}' in puppet.conf"
        end
      end

      host = Puppet[:ldapserver]
      port = Puppet[:ldapport]

      if Puppet[:ldapuser] && Puppet[:ldappassword]
        user     = Puppet[:ldapuser]
        password = Puppet[:ldappassword]
      end

      tls = Puppet[:ldaptls]
      ca_file = "#{Puppet[:confdir]}/ldap_ca.pem"

      # TODO if not exists ldap_ca.pem fail

      conf = {
        host: host,
        port: port
      }

      if (user != '') && (password != '')
        conf[:auth] = {
          method: :simple,
          username: user,
          password: password
        }
      end

      if tls
        conf[:encryption] = {
          method: :simple_tls,
          tls_options: { ca_file: ca_file }
        }
      end

      conf
    end

    def entries
      # Query the LDAP server for attributes using the filter
      #
      # Returns: An array of Net::LDAP::Entry objects
      conf = ldap_config

      start_time = Time.now
      ldap = Net::LDAP.new(conf)

      search_args = {
        base: @base,
        attributes: @attributes,
        scope: @scope,
        time: 10
      }

      if @filter && !@filter.empty?
        ldapfilter = Net::LDAP::Filter.construct(@filter)
        search_args[:filter] = ldapfilter
      end

      entries = []

      begin
        ldap.search(search_args) do |entry|
          entries << entry
        end
        end_time = Time.now
        time_delta = format('%.3f', end_time - start_time)

        Puppet.debug("ldapquery(): Searching #{@base} for #{@attributes} using #{@filter} took #{time_delta} seconds and returned #{entries.length} results")
        return entries
      rescue Net::LDAP::LdapError => e
        Puppet.debug("There was an error searching LDAP #{e.message}")
        Puppet.debug('Returning false')
        return false
      end
    end

    def parse_entries
      data = []
      entries.each do |entry|
        entry_data = {}
        entry.each do |attribute, values|
          attr = attribute.to_s
          value_data = []
          Array(values).flatten.each do |v|
            value_data << v.chomp
          end
          entry_data[attr] = value_data
        end
        data << entry_data
      end
      Puppet.debug(data)
      data
    end

    def results
      parse_entries
    end
  end
end