Class: Dnsruby::ZoneReader

Inherits:
Object
  • Object
show all
Defined in:
lib/dnsruby/zone_reader.rb

Defined Under Namespace

Classes: ParseException

Instance Method Summary collapse

Constructor Details

#initialize(origin, soa_minimum = nil, soa_ttl = nil) ⇒ ZoneReader

Create a new ZoneReader. The zone origin is required. If the desired SOA minimum and TTL are passed in, then they are used as default values.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dnsruby/zone_reader.rb', line 27

def initialize(origin, soa_minimum = nil, soa_ttl = nil)
  origin_name = parse_zone_name(origin)
  @origin = Name.new(origin_name.labels, true).to_s(true)
  @soa_ttl = soa_ttl
  @default_ttl = nil
  if (soa_minimum && !@last_explicit_ttl)
    @last_explicit_ttl = soa_minimum
  else
    @last_explicit_ttl = 0
  end
  @last_explicit_class = Classes.new("IN")
  @last_name = nil
  @continued_line = nil
  @group_depth = 0
  @in_quoted_section = false
end

Instance Method Details

#get_ttl(ttl_text_in) ⇒ Object

Get the TTL in seconds from the m, h, d, w format



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/dnsruby/zone_reader.rb', line 463

def get_ttl(ttl_text_in)
  #  If no letter afterwards, then in seconds already
  #  Could be e.g. "3d4h12m" - unclear if "4h5w" is legal - best assume it is
  #  So, search out each letter in the string, and get the number before it.
  ttl_text = ttl_text_in.downcase
  index = ttl_text.index(/[whdms]/)
  if (!index)
    return ttl_text.to_i
  end
  last_index = -1
  total = 0
  while (index)
    letter = ttl_text[index]
    number = ttl_text[last_index + 1, index-last_index-1].to_i
    new_number = 0
    case letter
    when "s"
      new_number = number
    when "m"
      new_number = number * 60
    when "h"
      new_number = number * 3600
    when "d"
      new_number = number * 86400
    when "w"
      new_number = number * 604800
    end
    total += new_number

    last_index = index
    index = ttl_text.index(/[whdms]/, last_index + 1)
  end
  total += ttl_text[last_index + 1..].to_i
  return total
end

#normalise_line(line, do_prefix_hack = false) ⇒ Object

Take a line from the input zone file, and return the normalised form do_prefix_hack should always be false



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/dnsruby/zone_reader.rb', line 212

def normalise_line(line, do_prefix_hack = false)
  # Direct callers historically accepted unmatched grouping, just like
  # RR.create. process_line has already scanned and checked its input.
  unless @scanned_line && line.equal?(@scanned_line[0]) && line == @scanned_line[1]
    single_quoted_start = if line.include?("'")
      Presentation.single_quoted_start(line, owner_omitted: line.start_with?(' ', "\t"))
    end
    line, = Presentation.scan(line, allow_unbalanced: true, single_quoted_from: single_quoted_start)
  end
  normalise_scanned_line(line, do_prefix_hack)
end

#prefix_for_rrset_order(type, type_was) ⇒ Object

This method is included only for OpenDNSSEC support. It should not be used otherwise. Frig the RR type so that NSEC records appear last in the RRSets. Also make sure that DNSKEYs come first (so we have a key to verify the RRSet with!).



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/dnsruby/zone_reader.rb', line 515

def prefix_for_rrset_order(type, type_was) # :nodoc: all
  #  Now make sure that NSEC(3) RRs go to the back of the list
  if ['NSEC', 'NSEC3'].include?type.string
    if (type_was == Types::RRSIG)
      #  Get the RRSIG first
      type_string = "ZZ" + type.string
    else
      type_string = "ZZZ" + type.string
    end
  elsif type == Types::DNSKEY
    type_string = "0" + type.string
  elsif type == Types::NS
    #  Make sure that we see the NS records first so we know the delegation status
    type_string = "1" + type.string
  else
    type_string = type.string
  end
  return type_string
end

#process_file(source) ⇒ Object

Takes a filename string, or any type of IO object, and attempts to load a zone. Returns a list of RRs if successful, nil otherwise.



46
47
48
49
50
51
52
53
54
# File 'lib/dnsruby/zone_reader.rb', line 46

def process_file(source)
  if source.is_a?(String)
    File.open(source) do |file|
      process_io(file)
    end
  else
    process_io(source)
  end
end

#process_io(io) ⇒ Object

Iterate over each line in a IO object, and process it. Returns a list of RRs if successful, nil otherwise.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dnsruby/zone_reader.rb', line 58

def process_io(io)
  zone = nil
  io.each do |line|
    begin
      ret = process_line(line)
      if (ret)
        rr = RR.create(ret)
        if (!zone)
          zone = []
        end
        zone.push(rr)
      end
    rescue Exception
      raise ParseException.new("Error reading line #{io.lineno} of #{io.inspect} : [#{line}]")
    end
  end
  if @continued_line
    raise ParseException.new("Unclosed parenthesis at end of #{io.inspect}")
  end
  return zone
end

#process_line(line, do_prefix_hack = false) ⇒ Object

Process the next line of the file Returns a string representing the normalised line.



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
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dnsruby/zone_reader.rb', line 82

def process_line(line, do_prefix_hack = false)
  if !@continued_line
    @in_quoted_section = false
    @single_quoted_start = if /['(]/.match?(line)
      Presentation.single_quoted_start(line, owner_omitted: line.start_with?(' ', "\t"))
    end
  end
  previous_quote = @in_quoted_section
  line, @group_depth, @in_quoted_section =
    Presentation.scan(line, @group_depth, @in_quoted_section,
      single_quoted_from: @continued_line && @single_quoted_start ? 0 : @single_quoted_start)

  if @continued_line
    # The scanner keeps literal newlines inside quotes and turns layout
    # newlines into spaces. Do not strip trailing quoted data. Callers of
    # process_line may also supply lines without a terminating newline.
    separator = previous_quote ? (@continued_line.end_with?("\n") ? '' : "\n") : ' '
    line = @continued_line + separator + line
  end
  if @group_depth.positive?
    @continued_line = line
    return nil
  end
  @continued_line = nil
  return nil if line.strip.empty?

  if (line.index("$ORIGIN") == 0)
    @origin = absolute_name(Presentation.tokens(line)[1]).to_s(true) # $ORIGIN <domain-name>
    #                 print "Setting $ORIGIN to #{@origin}\n"
    return nil
  end
  if (line.index("$TTL") == 0)
    @default_ttl = get_ttl(line.split()[1].strip) #  $TTL <ttl>
    #                 print "Setting $TTL to #{ttl}\n"
    return nil
  end
  line += "\n"

  # Preserve the public normalization hook for subclasses and prepends.
  # Only unchanged input has already been scanned. Overrides may replace
  # or mutate the String before calling super, introducing new syntax.
  previous_scanned_line = @scanned_line
  begin
    @scanned_line = [line, line.dup]
    normalise_line(line, do_prefix_hack)
  ensure
    @scanned_line = previous_scanned_line
  end
end

#process_quotes(section) ⇒ Object



200
201
202
203
204
205
206
207
208
# File 'lib/dnsruby/zone_reader.rb', line 200

def process_quotes(section)
  #  Look through the section of text and set the @in_quoted_section
  #  as it should be at the end of the given section
  last_index = 0
  while (next_index = section.index("\"", last_index + 1))
    @in_quoted_section = !@in_quoted_section
    last_index = next_index
  end
end

#replace_soa_ttl_fields(line) ⇒ Object



499
500
501
502
503
504
505
506
507
508
# File 'lib/dnsruby/zone_reader.rb', line 499

def replace_soa_ttl_fields(line)
  #  Replace any fields which evaluate to 0
  split = line.split
  4.times {|i|
    x = i + 7
    split[x].strip!
    split[x] = get_ttl(split[x]).to_s
  }
  return split.join(" ") + "\n"
end

#strip_comments(line) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/dnsruby/zone_reader.rb', line 132

def strip_comments(line)
  last_index = 0
  #  Are we currently in a quoted section?
  #  Does a quoted section begin or end in this line?
  #  Are there any semi-colons?
  #  Ary any of the semi-colons inside a quoted section?
  #  Handle escape characters
  if (line.index"\\")
    return strip_comments_meticulously(line)
  end
  while (next_index = line.index(";", last_index + 1))
    #  Have there been any quotes since we last looked?
    process_quotes(line[last_index, next_index - last_index])

    #  Now use @in_quoted_section to work out if the ';' terminates the line
    if (!@in_quoted_section)
      return line[0,next_index]
    end

    last_index = next_index
  end
  #  Check out the quote situation to the end of the line
  process_quotes(line[last_index, line.length-1])

  return line
end

#strip_comments_meticulously(line) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/dnsruby/zone_reader.rb', line 159

def strip_comments_meticulously(line)
  #  We have escape characters in the text. Go through it character by
  #  character and work out what's escaped and quoted and what's not
  escaped = false
  quoted = false
  pos = 0
  line.each_char {|c|
    if (c == "\\")
      if (!escaped)
        escaped = true
      else
        escaped = false
      end
    else
      if (escaped)
        if (c >= "0" && c <= "9") # rfc 1035 5.1 \DDD
          pos = pos + 2
        end
        escaped = false
        next
      else
        if (c == "\"")
          if (quoted)
            quoted = false
          else
            quoted = true
          end
        else
          if (c == ";")
            if (!quoted)
              return line[0, pos+1]
            end
          end
        end
      end
    end
    pos +=1
  }
  return line
end