module Ucert::Utils

Public Instance Methods

file_2_list(f,lc=false) click to toggle source

Load entries from a text file and return an array

# File lib/ucert/utils/utils.rb, line 16
def file_2_list(f,lc=false)
        puts "Loading records from file: #{f}" if @verbose
        begin
                list=Array.new
                file = File.open(f, "r")
                file.each_line do |line|
                        line=line.chomp.strip
                        next if line.nil?
                        next if line.empty?
                        next if line =~ /^\s*#/
                        line=line.downcase if lc==true
                        list.push(line.chomp.strip)
                end
                file.close
                return list
        rescue => ee
                puts "Exception on method #{__method__} for file #{f}: #{ee}" if @verbose
                return nil
        end
end
is_fqdn?(host) click to toggle source

Simple test a host string format. Return true if it contains a valid internet domain sub-string. Note: Don't be confused with another method 'valid_dns_record?', which is a stricter and time-consuming test on the DNS server for a resolvable internet host.

# File lib/ucert/utils/utils.rb, line 155
def is_fqdn? (host)
        puts "Validate the host-name format is valid: #{host}" if @verbose
        begin
                return false if is_ip?(host) or is_url?(host)
                domain=get_domain_root(host)
                if domain.nil?
                        return false
                elsif is_domain_root?(domain)
                        return true
                else
                        return false
                end
        rescue => ee
                puts "Exception on method is_fqdn? for #{host}: #{ee}" if @verbose
                return false
        end
end
Also aliased as: is_host?
is_host?(host)
Alias for: is_fqdn?
is_ip?(ip) click to toggle source

Test if it's a legitimate IP4 address

# File lib/ucert/utils/utils.rb, line 110
def is_ip? (ip)
        puts "Validate the IP format is valid: #{ip}" if @verbose
        begin
                ip=ip.strip
                raise "This is an URL: #{ip}" if is_url?(ip)
                if ip =~ /\d+\.\d+\.\d+.\d+/ and ip !~ /\/\d+/
  octs=ip.split('.')
                        return false unless octs.size == 4
  return false if octs[0].to_i == 0
                        octs.map { |x| return false unless x.to_i >=0 and x.to_i <=255 }
                else
                        return false
                end
                puts "Confirmed as a valid IP: #{ip}" if @verbose
                return true
        rescue => ee
                puts "Exception on method is_ip? for #{ip}: #{ee}" if @verbose
                return false
        end
end
Also aliased as: is_valid_ip?
is_url?(url) click to toggle source

Simple sanity check on a 'claimed' URL string.

# File lib/ucert/utils/utils.rb, line 133
def is_url?(url)
        puts "Validate the URL format is valid: #{url}" if @verbose
        begin
                if url =~ /(http|https)\:\/\/((.)+)/i
                        host=$2.split('/')[0]
                        host=host.split(':')[0]
                        if is_ip?(host) or is_fqdn?(host)
                                return true
                        else
                                return false
                        end
                else
                        puts "Unknown URL format: #{url}" if @verbose
                        return false
                end
        rescue => ee
                puts "Exception on method #{__method__}: #{ee}" if @verbose
                return false
        end
end
is_valid_ip?(ip)
Alias for: is_ip?
list_2_file(list,file) click to toggle source

Save an array into a file

# File lib/ucert/utils/utils.rb, line 38
def list_2_file (list,file)
        puts "Save list #{list} to plain file #{file}" if @verbose
        begin
                f = File.open(file, "w")
                list.map do |ent|
                        #ent.strip!
                        # Append the unix line break
                        f.write("#{ent}\n")
                end
                f.close
        rescue => ee
                puts "Exception on method #{__method__} for file #{file}: #{ee}" if @verbose
                return nil
        end
end
load_known_user_map_from_file(f_users) click to toggle source

Load known user map from the local cache file, in order to speed up the DN foreign key lookup process

# File lib/ucert/utils/utils.rb, line 91
def load_known_user_map_from_file(f_users)
  puts "Loading knonw users from local cache file: #{f_users}" if @verbose
  begin
    my_users=Hash.new
    f=File.open(f_users, 'r')
    f.each do |line|
      next if line =~ /^\#/
      entry=line.chomp.split('|')
      my_users.merge!({entry[0]=>entry[1]})
    end
    return my_users
    puts "Done loading local user map file: #{f_users}" if @verbose
  rescue => ee
    puts "Exception on method #{__method__}: #{ee}"
    return Hash.new
  end
end
nslookup(hostname) click to toggle source

perform simple DNS txt record lookup

# File lib/ucert/utils/utils.rb, line 175
def nslookup (hostname)
  puts "Perform simple DNS TXT Record lookup for host: #{hostname}" if @verbose
  begin
                    ips=Array.new
                    if is_ip?(hostname)
                            puts "No change - same IP is returned. " if @verbose
                            return hostname.strip
                    else
                            ips=Resolv.getaddresses(hostname)
                            if (ips.empty?) then
                                    puts "Failed to resolve #{hostname}" if @verbose
                                    return nil
                            else
                                    puts "IP found: #{ips.first}" if @verbose
                                    return ips.first.strip
                            end
                    end
            rescue => ee
                    puts "Exception on method host_2_ip for host #{hostname}: #{ee}" if @verbose
                    return nil
            end
end
search_ad(name) click to toggle source

Search AD Store for a specific person, return the AD DN record as the output if found

# File lib/ucert/utils/utils.rb, line 55
def search_ad (name)
  begin
    puts "Search in ad_tracker for user: #{name}" if @verbose
    k=Ucert::AdTracker.new
    search_result=k.ad_search_by_text(name, "person")
    k=nil
    puts "Found: #{search_result}" if @verbose
    return search_result
  rescue => ee
    puts "Exception on method #{__method__}: #{ee}"
  end
end
update_dn(tracker,dn) click to toggle source

Perform AD lookup to detect the DN record change; track the change in the ad_delta file and return the new DN value

# File lib/ucert/utils/utils.rb, line 69
def update_dn(tracker,dn)
    begin
      puts "Perform AD tracker lookup for possible change of DN: #{dn}" if @verbose
      return if dn.nil?
      puts "Additional logic for case of DN update: #{dn}" if @verbose
      old_dn = dn
      cn=tracker.extract_first_cn(dn)
      dn=tracker.ad_search_by_text(cn,"person")
      return if dn.nil?
      # write the change to the ad_delta file
      timestamp = Time.now
      f = File.open(Ucert.ad_delta,'a')
      f.write "# old_dn|dn - tracked by the #{self.class} class #{__method__} method at: #{timestamp}\n"
      f.write "#{old_dn}|#{dn}\n"
      f.close
      return dn
    rescue => ee
      puts "Exception on method #{__method__}: #{ee}"
    end
end