File: axfr.rb

package info (click to toggle)
dnsruby 1.73.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,548 kB
  • sloc: ruby: 17,966; makefile: 3
file content (197 lines) | stat: -rwxr-xr-x 5,595 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
120
121
122
123
124
125
126
127
128
129
130
131
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
158
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
#! /usr/bin/env ruby

# --
# Copyright 2007 Nominet UK
# 

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ++

# = NAME
# 
# axfr - Perform a DNS zone transfer
# 
# = SYNOPSIS
# 
# axfr [ -fqs ] [ -D directory ] [ @nameserver ] zone
# 
# = DESCRIPTION
# 
# axfr performs a DNS zone transfer, prints each record to the standard
# output, and stores the zone to a file.  If the zone has already been
# stored in a file, axfr will read the file instead of performing a
# zone transfer.
# 
# Zones will be stored in a directory hierarchy.  For example, the
# zone transfer for foo.bar.com will be stored in the file
#  HOME/.dns-zones/com/bar/foo/axfr.  The directory can be changed
#  with the B<-D> option.
# 
#  This programs requires that the Storable module be installed.
# 
# = OPTIONS
# 
#    * -f     Force a zone transfer, even if the zone has already been stored
#    in a file.
# 
#    * -q    Be quiet -- don't print the records from the zone.
# 
#    * -s    Perform a zone transfer if the SOA serial number on the nameserver
#    is different than the serial number in the zone file.
# 
#    * -D directory   Store zone files under I<directory> instead of the default directory (see "FILES")
# 
#    * nameserver    Query nameserver instead of the default nameserver.
# 
# = FILES
# 
#  * ${HOME}/.dns-zones   Default directory for storing zone files.
# 
# = AUTHOR
# 
#      Michael Fuhr <mike@fuhr.org>
# 

unless (1..2).include?(ARGV.length)
  puts "Usage: #{$0} [ -fqs ] [ -D directory ] [ @nameserver ] zone"
  exit(-1)
end


require 'getoptLong'
require 'dnsruby'


# ------------------------------------------------------------------------------
# Read any command-line options and check syntax.
# ------------------------------------------------------------------------------

# getopts("fqsD:");
opts = GetoptLong.new(["-f", GetoptLong::NO_ARGUMENT],
  ["-q", GetoptLong::NO_ARGUMENT],
  ["-D", GetoptLong::REQUIRED_ARGUMENT],
  ["-s", GetoptLong::NO_ARGUMENT])

opt_q = false
opt_f = false
opt_s = false
opt_d = nil
opts.each do |opt, arg|
  case opt
  when '-q'
    opt_q=true
  when '-f'
    opt_f = true
  when '-s'
    opt_s = true
  when '-D'
    opt_d = arg
  end
end

# ------------------------------------------------------------------------------
#  Get the nameserver (if specified) and set up the zone transfer directory
#  hierarchy.
# ------------------------------------------------------------------------------

nameserver = (ARGV[0] =~ /^@/) ? ARGV.shift : ''
nameserver = nameserver.sub(/^@/, '')
resolver = nameserver ? Dnsruby::Resolver.new(nameserver) : Dnsruby::Resolver.new

zone = ARGV.shift
basedir = opt_d || File.join((ENV['HOME'] || ''), '.dns-zones')
zonedir = zone.split(/\./).reverse.join("/")
zonefile = File.join(basedir, zonedir, 'axfr')

#  Don't worry about the 0777 permissions here - the current umask setting
#  will be applied.
# NOTE: mkdir will raise an error on failure so I don't think 'or' works here.
unless FileTest.directory?(basedir)
  Dir.mkdir(basedir, 0777) or raise RuntimeError, "can't mkdir #{basedir}: #{$!}\n"
end

dir = basedir
# NOTE: What are we doing here?  Could this be replaced by mkdir_p?
zonedir.split('/').each do |subdir|
  dir += '/' + subdir
  unless FileTest.directory?(dir)
    Dir.mkdir(dir, 0777) or raise RuntimeError, "can't mkdir #{dir}: #{$!}\n"
  end
end

# ------------------------------------------------------------------------------
#  Get the zone.
# ------------------------------------------------------------------------------

if FileTest.exist?(zonefile) && !opt_f
  zoneref = Marshal.load(File.open(zonefile))
  if zoneref.nil?
    raise RuntimeError, "couldn't retrieve zone from #{zonefile}: #{$!}\n"
  end

  # ----------------------------------------------------------------------
  #  Check the SOA serial number if desired.
  # ----------------------------------------------------------------------

  if opt_s
    serial_file = serial_zone = nil

    zoneref.each do |rr|
      if (rr.type == 'SOA')
        serial_file = rr.serial
        break
      end
    end
    if serial_file.nil?
      raise RuntimeError,  "no SOA in #{zonefile}\n"
    end

    soa = resolver.query(zone, 'SOA')
    if soa.nil?
      raise RuntimeError, "couldn't get SOA for #{zone}: " + resolver.errorstring + "\n"
    end

    soa.answer.each do |rr|
      if rr.type == 'SOA'
        serial_zone = rr.serial
        break
      end
    end

    if serial_zone != serial_file
      opt_f = true
    end
  end
else
  opt_f = true
end

if opt_f
  print "nameserver = #{nameserver}, zone=#{zone}"
  zt = Dnsruby::ZoneTransfer.new
  zt.server = nameserver if nameserver != ''

  zoneref = zt.transfer(zone)
  if zoneref.nil?
    raise RuntimeError,  "couldn't transfer zone\n"
  end
  Marshal.dump(zoneref, File.open(zonefile, File::CREAT | File::RDWR))
end

# ------------------------------------------------------------------------------
#  Print the records in the zone.
# ------------------------------------------------------------------------------

unless opt_q
  zoneref.each { |z| puts z }
end