Class: Dnsruby::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/Dnsruby/Config.rb

Overview

Description

The Config class determines the system configuration for DNS. In particular, it determines the nameserver to target queries to.

It also specifies whether and how the search list and default domain should be applied to queries, according to the following algorithm :

  • If the name is absolute, then it is used as is.

  • If the name is not absolute, then :

    If apply_domain is true, and ndots is greater than the number 
    of labels in the name, then the default domain is added to the name.
    
    If apply_search_list is true, then each member of the search list
    is appended to the name.
    

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Create a new Config with system default values



70
71
72
73
# File 'lib/Dnsruby/Config.rb', line 70

def initialize()
  @mutex = Mutex.new
  parse_config
end

Instance Attribute Details

#apply_domainObject

Should the default domain be applied?



49
50
51
# File 'lib/Dnsruby/Config.rb', line 49

def apply_domain
  @apply_domain
end

#apply_search_listObject

Should the search list be applied?



47
48
49
# File 'lib/Dnsruby/Config.rb', line 47

def apply_search_list
  @apply_search_list
end

#nameserverObject

The list of nameservers to query



45
46
47
# File 'lib/Dnsruby/Config.rb', line 45

def nameserver
  @nameserver
end

#ndotsObject

The minimum number of labels in the query name (if it is not absolute) before it is considered complete



51
52
53
# File 'lib/Dnsruby/Config.rb', line 51

def ndots
  @ndots
end

Class Method Details

.default_config_hash(filename = "/etc/resolv.conf") ⇒ Object

:nodoc: all



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/Dnsruby/Config.rb', line 286

def Config.default_config_hash(filename="/etc/resolv.conf") #:nodoc: all
  config_hash={}
  if File.exist? filename
    config_hash = Config.parse_resolv_conf(filename)
  else
    if (/java/ =~ RUBY_PLATFORM && !(filename=~/:/))
      # Problem with paths and Windows on JRuby - see if we can munge the drive...
      wd = Dir.getwd
      drive = wd.split(':')[0]
      if (drive.length==1)
        file = drive << ":" << filename
        if File.exist? file
          config_hash = Config.parse_resolv_conf(file)
        end
      end
    elsif /mswin32|cygwin|mingw|bccwin/ =~ RUBY_PLATFORM
      # @TODO@ Need to get windows domain sorted
      search, nameserver = Win32::Resolv.get_resolv_info
      #          config_hash[:domain] = domain if domain
      config_hash[:nameserver] = nameserver if nameserver
      config_hash[:search] = [search].flatten if search
    end
  end
  config_hash
end

.parse_resolv_conf(filename) ⇒ Object

:nodoc: all



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/Dnsruby/Config.rb', line 227

def Config.parse_resolv_conf(filename) #:nodoc: all
  nameserver = []
  search = nil
  domain = nil
  ndots = 1
  open(filename) {|f|
    f.each {|line|
      line.sub!(/[#;].*/, '')
      keyword, *args = line.split(/\s+/)
      args.each { |arg|
        arg.untaint
      }
      next unless keyword
      case keyword
      when 'nameserver'
        nameserver += args
      when 'domain'
        next if args.empty?
        domain = args[0]
        #            if search == nil
        #              search = []
        #            end
        #            search.push(args[0])
      when 'search'
        next if args.empty?
        if search == nil
          search = []
        end
        args.each {|a| search.push(a)}
      when 'options'
        args.each {|arg|
          case arg
          when /\Andots:(\d+)\z/
            ndots = $1.to_i
          end
        }
      end
    }
  }
  return { :nameserver => nameserver, :domain => domain, :search => search, :ndots => ndots }
end

.resetObject

Reset the config to default values



75
76
77
78
# File 'lib/Dnsruby/Config.rb', line 75

def Config.reset
  c = Config.new
  c.parse_config
end

.resolve_server(ns) ⇒ Object

:nodoc: all



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/Dnsruby/Config.rb', line 201

def Config.resolve_server(ns) #:nodoc: all
  # Sanity check server
  # If it's an IP address, then use that for server
  # If it's a name, then we'll need to resolve it first
  server=ns
  begin
    addr = IPv4.create(ns)
    server = ns
  rescue Exception 
    begin
      addr=IPv6.create(ns)
      server = ns
    rescue Exception
      begin
        # try to resolve server to address
        addr = TCPSocket.gethostbyname(ns)[3] # @TODO@ Replace this with Dnsruby call when lookups work
        server = addr
      rescue Exception => e
        TheLog.error("Can't make sense of nameserver : #{server}, exception : #{e}")
        raise ArgumentError.new("Can't make sense of nameserver : #{server}, exception : #{e}")
      end
    end
  end
  return server
end

Instance Method Details

#add_nameserver(ns) ⇒ Object

Add a nameserver to the list of nameservers.

Can take either a single String or an array of Strings. The new nameservers are added at a higher priority.



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/Dnsruby/Config.rb', line 180

def add_nameserver(ns)
  if (ns.kind_of?String) 
    ns=[ns]
  end
  check_ns(ns)
  ns.reverse_each do |n|
    if (!@nameserver.include?(n))
      self.nameserver=[n]+@nameserver
    end
  end
end

#check_ns(ns) ⇒ Object

:nodoc: all



166
167
168
169
170
171
172
173
174
# File 'lib/Dnsruby/Config.rb', line 166

def check_ns(ns) #:nodoc: all
  if !ns.kind_of?(Array) ||
      !ns.all? {|n| (String === n || IPv4 === n || IPv6 === n)}
    raise ArgumentError.new("invalid nameserver config: #{ns.inspect}")
  end    
  ns.each_index do |i|
    ns[i]=Config.resolve_server(ns[i])
  end
end

#domainObject

Return the default domain



322
323
324
325
326
327
# File 'lib/Dnsruby/Config.rb', line 322

def domain
  if (@domain==nil)
    return nil
  end
  return Name.create(@domain).to_s
end

#domain=(dom) ⇒ Object

Set the default domain



122
123
124
125
126
127
128
129
130
131
# File 'lib/Dnsruby/Config.rb', line 122

def domain=(dom)
  if (dom)
    if !dom.kind_of?(String)
      raise ArgumentError.new("invalid domain config: #{@domain.inspect}")
    end
    @domain = Name::Label.split(dom)
  else
    @domain=nil
  end
end

#generate_candidates(name) ⇒ Object

:nodoc: all



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/Dnsruby/Config.rb', line 337

def generate_candidates(name) #:nodoc: all
  candidates = []
  name = Name.create(name)
  if name.absolute?
    candidates = [name]
  else
    if (@apply_domain)
      if @ndots > name.length - 1
        candidates.push(Name.create(name.to_a+@domain))
      end
    end
    if (!@apply_search_list)
      candidates.push(Name.create(name.to_a))
    else
      if @ndots <= name.length - 1
        candidates.push(Name.create(name.to_a))
      end
      candidates.concat(@search.map {|domain| Name.create(name.to_a + domain)})
      if (name.length == 1)
        candidates.concat([Name.create(name.to_a)])
      end
    end          
  end
  return candidates
end

#inspectObject

:nodoc: all



269
270
271
# File 'lib/Dnsruby/Config.rb', line 269

def inspect #:nodoc: all
  to_s
end

#parse_config(config_info = nil) ⇒ Object

:nodoc: all



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/Dnsruby/Config.rb', line 80

def parse_config(config_info=nil) #:nodoc: all
  @mutex.synchronize {
    ns = []
    @nameserver = []
    @domain, s, @search = nil
    dom=""
    nd = 1
    @ndots = 1
    @apply_search_list = true
    @apply_domain = true
    config_hash = Config.default_config_hash
    case config_info
    when nil
    when String
      config_hash.merge!(Config.parse_resolv_conf(config_info))
    when Hash
      config_hash.merge!(config_info.dup)
      if String === config_hash[:nameserver]
        config_hash[:nameserver] = [config_hash[:nameserver]]
      end
      if String === config_hash[:search]
        config_hash[:search] = [config_hash[:search]]
      end
    else
      raise ArgumentError.new("invalid resolv configuration: #{@config_info.inspect}")
    end
    ns = config_hash[:nameserver] if config_hash.include? :nameserver
    s = config_hash[:search] if config_hash.include? :search
    nd = config_hash[:ndots] if config_hash.include? :ndots
    @apply_search_list = config_hash[:apply_search_list] if config_hash.include? :apply_search_list
    @apply_domain= config_hash[:apply_domain] if config_hash.include? :apply_domain
    dom = config_hash[:domain] if config_hash.include? :domain
    
    send("nameserver=",ns)        
    send("search=",s)        
    send("ndots=",nd)
    send("domain=",dom)        
  }
  TheLog.info(to_s)
end

#searchObject

Return the search path



313
314
315
316
317
318
319
# File 'lib/Dnsruby/Config.rb', line 313

def search
  search = []
  @search.each do |s|
    search.push(Name.new(s).to_s)
  end
  return search
end

#search=(s) ⇒ Object

Set the default search path



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/Dnsruby/Config.rb', line 142

def search=(s)
  @search=s
  if @search
    if @search.class == Array
      @search = @search.map {|arg| Name::Label.split(arg) }
    else
      raise ArgumentError.new("invalid search config: search must be an array!")
    end
  else
    hostname = Socket.gethostname
    if /\./ =~ hostname
      @search = [Name::Label.split($')]
    else
      @search = [[]]
    end
  end
  
  if !@search.kind_of?(Array) ||
      #              [email protected]? {|ls| ls.all? {|l| Label::Str === l } }
    !@search.all? {|ls| ls.all? {|l| Name::Label === l } }
    raise ArgumentError.new("invalid search config: #{@search.inspect}")
  end
end

#set_config_info(config_info) ⇒ Object

Set the config. Parameter can be :

  • A String containing the name of the config file to load

    e.g. /etc/resolv.conf
    
  • A hash with the following elements :

    nameserver (String)
    domain (String)
    search (String)
    ndots (Fixnum)
    

This method should not normally be called by client code.



65
66
67
# File 'lib/Dnsruby/Config.rb', line 65

def set_config_info(config_info)
  parse_config(config_info)
end

#single?Boolean

:nodoc: all

Returns:

  • (Boolean)


329
330
331
332
333
334
335
# File 'lib/Dnsruby/Config.rb', line 329

def single? #:nodoc: all
  if @nameserver.length == 1
    return @nameserver[0]
  else
    return nil
  end
end

#to_sObject



273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/Dnsruby/Config.rb', line 273

def to_s
  ret = "Config - nameservers : "
  @nameserver.each {|n| ret += n.to_s + ", "}
  domain_string="empty"
  if (@domain!=nil)
    domain_string=@domain.to_s
  end
  ret += " domain : #{domain_string}, search : "
  search.each {|s| ret += s + ", " }
  ret += " ndots : #{@ndots}"
  return ret
end