Module: Ucert::Utils

Instance Method Summary collapse

Instance Method Details

#file_2_list(f, lc = false) ⇒ Object

Load entries from a text file and return an array



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Boolean Also known as: is_host?

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.

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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

#is_ip?(ip) ⇒ Boolean Also known as: is_valid_ip?

Test if it’s a legitimate IP4 address

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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

#is_url?(url) ⇒ Boolean

Simple sanity check on a ‘claimed’ URL string.

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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

#list_2_file(list, file) ⇒ Object

Save an array into a file



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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) ⇒ Object

perform simple DNS txt record lookup



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# 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) ⇒ Object

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



55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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