...
 
Commits (3)
......@@ -155,7 +155,7 @@ class Zone
raise ArgumentError, "invalid port: #{port}" if port < 0 || port > 65535
raise ArgumentError, "invalid protocol: #{protocol}" if protocol.to_s !~ /^[a-z]+$/
raise ArgumentError, "no data given" unless data
raise ArgumentError, 'no data given' unless data
raise ArgumentError, "invalid usage: #{usage}" unless Integer === usage
raise ArgumentError, "invalid selector: #{selector}" unless Integer === selector
raise ArgumentError, "invalid matching_type: #{matching}" unless Integer === matching
......@@ -172,7 +172,7 @@ class Zone
raise ArgumentError, "invalid key type, only RSA supported: #{type}" if type != "rsa"
push :txt, "#{selector}._domainkey", ttl, text: "v=DKIM1; k=#{type}; p=#{pubkey}"
push :txt, "#{selector}._domainkey", ttl, text: "\"v=DKIM1; k=#{type}; p=#{pubkey}\""
end
# name in not-reversed order
......
require 'fileutils'
class ZoneGenerator
attr_accessor :config, :soa, :generated, :zones_dir, :template_dir
attr_reader :basedir
......@@ -10,14 +9,14 @@ class ZoneGenerator
@basedir = basedir
@soa = {
origin: "@",
ttl: "86400",
primary: "example.com.",
email: "hostmaster@example.com",
refresh: "8H",
retry: "2H",
expire: "1W",
minimumTTL: "11h"
origin: '@',
ttl: '86400',
primary: 'example.com.',
email: 'hostmaster@example.com',
refresh: '8H',
retry: '2H',
expire: '1W',
minimumTTL: '11h'
}.merge(@config['soa'])
@zones_dir = File.expand_path(@config['ruby_zones'], basedir)
......@@ -28,8 +27,8 @@ class ZoneGenerator
@tmp_zones = "#{@generated}/zones"
# Rewrite email address
if (email = @soa[:email]).include?("@")
@soa[:email] = email.sub("@",".") << "."
if (email = @soa[:email]).include?('@')
@soa[:email] = email.sub('@','.') << '.'
end
end
......@@ -44,28 +43,28 @@ class ZoneGenerator
# Generates all zones
def generate
ruby_zones = Dir.glob("#{@zones_dir}/**/*.rb").map {|z| File.basename(z, '.rb')+'.zone'}
text_zones = Dir.glob("#{@config['local_zones_dir']}/*.zone").map {|z| File.basename(z) }
old_zones = (text_zones-ruby_zones).map{|f| File.join(@config['local_zones_dir'], f)}
ruby_zones = Dir.glob("#{@zones_dir}/**/*.rb").map { |z| File.basename(z, '.rb') + '.zone' }
text_zones = Dir.glob("#{@config['local_zones_dir']}/*.zone").map { |z| File.basename(z) }
old_zones = (text_zones - ruby_zones).map { |f| File.join(@config['local_zones_dir'], f) }
old_zones.each do |old|
FileUtils.rm old
end
File.open(@tmp_named,"w") do |f|
File.open(@tmp_named, 'w') do |f|
Dir.glob("#{@zones_dir}/**/*.rb").sort.each do |file|
domain = File.basename(file).sub(/\.rb$/,"")
domain = File.basename(file).sub(/\.rb$/, '')
puts "Parsing zone for #{domain}"
generate_zone(file, domain)
f.puts "zone \"#{domain}\" IN { type master; file \"#{@config['zones_dir']}/#{domain}.zone\"; };"
end
f.puts "\n# vim: set ft=named:"
f.puts '\n# vim: set ft=named:'
end
end
# Generates a single zone file
def generate_zone(file, domain)
zone = Zone.new(domain + ".", @template_dir, @soa)
zone = Zone.new(domain + '.', @template_dir, @soa)
zone.send :eval_file, file
new_zonefile = zone.zonefile
......@@ -73,7 +72,7 @@ class ZoneGenerator
old_file = "#{@config['local_zones_dir']}/#{domain}.zone"
# is there already a deployed version?
if File.exists?(old_file)
if File.exist?(old_file)
# parse the deployed version
old_output = File.read(old_file)
old_zonefile = Zonefile.new(old_output)
......@@ -99,6 +98,6 @@ class ZoneGenerator
# Write new zonefile
output_file_path = "#{@tmp_zones}/#{domain}.zone"
FileUtils.mkdir_p File.dirname(output_file_path)
File.open(output_file_path, "w"){|f| f.write new_output }
File.open(output_file_path, 'w'){|f| f.write new_output }
end
end
......@@ -220,7 +220,6 @@ class Zonefile
hexadeimal = /([\sA-F0-9]*)/i
quoted = /(\"[^\"]*\")/i
data = {}
if line =~ /^\$ORIGIN\s*(#{valid_name})/ix then
@origin = $1
elsif line =~ /^(#{valid_name})? \s*
......@@ -230,20 +229,20 @@ class Zonefile
/ix then
(name, ttl, dclass, type, host) = [$1, $2, $3, $4, $5]
add_record($4, :name => $1, :ttl => $2, :class => $3, :host => $5)
elsif line=~/^(#{valid_name})? \s*
elsif line =~ /^(#{valid_name})? \s*
#{ttl_cls}
AAAA \s
(#{valid_ip6})
/x then
add_record('aaaa', :name => $1, :ttl => $2, :class => $3, :host => $4)
elsif line=~/^(#{valid_name})? \s*
elsif line =~ /^(#{valid_name})? \s*
#{ttl_cls}
MX \s
(\d+) \s
(#{valid_name})
/ix then
add_record('mx', :name => $1, :ttl => $2, :class => $3, :pri => $4.to_i, :host => $5)
elsif line=~/^(#{valid_name})? \s*
elsif line =~ /^(#{valid_name})? \s*
#{ttl_cls}
SRV \s
(\d+) \s
......@@ -253,7 +252,7 @@ class Zonefile
/ix
add_record('srv', :name => $1, :ttl => $2, :class => $3, :pri => $4, :weight => $5,
:port => $6, :host => $7)
elsif line=~/^(#{valid_name})? \s*
elsif line =~ /^(#{valid_name})? \s*
#{ttl_cls}
DS \s
(\d+) \s
......@@ -263,14 +262,14 @@ class Zonefile
/ix
add_record( 'ds', :name => $1, :ttl => $2, :class => $3, :key_tag => $4.to_i, :algorithm => $5,
:digest_type => $6.to_i, :digest => $7.gsub( /\s/,'') )
elsif line=~/^(#{valid_name})? \s*
elsif line =~ /^(#{valid_name})? \s*
#{ttl_cls}
NSEC \s
(#{valid_name}) \s
([\s\w]*)
/ix
add_record( 'nsec', :name => $1, :ttl => $2, :class => $3, :next => $4, :types => $5.strip )
elsif line=~/^(#{valid_name})? \s*
elsif line =~ /^(#{valid_name})? \s*
#{ttl_cls}
NSEC3 \s
(\d+) \s
......@@ -282,7 +281,7 @@ class Zonefile
/ix
add_record( 'nsec3', :name => $1, :ttl => $2, :class => $3, :algorithm => $4, :flags => $5,
:iterations => $6, :salt => $7, :next => $8.strip, :types => $9.strip )
elsif line=~/^(#{valid_name})? \s*
elsif line =~ /^(#{valid_name})? \s*
#{ttl_cls}
NSEC3PARAM \s
(\d+) \s
......@@ -292,7 +291,7 @@ class Zonefile
/ix
add_record( 'nsec3param', :name => $1, :ttl => $2, :class => $3, :algorithm => $4, :flags => $5,
:iterations => $6, :salt => $7 )
elsif line=~/^(#{valid_name})? \s*
elsif line =~ /^(#{valid_name})? \s*
#{ttl_cls}
DNSKEY \s
(\d+) \s
......@@ -302,7 +301,7 @@ class Zonefile
/ix
add_record( 'dnskey', :name => $1, :ttl => $2, :class => $3, :flag => $4.to_i, :protocol => $5.to_i,
:algorithm => $6, :public_key => $7.gsub( /\s/,'') )
elsif line=~/^(#{valid_name})? \s*
elsif line =~ /^(#{valid_name})? \s*
#{ttl_cls}
RRSIG \s
(\w+) \s
......@@ -318,7 +317,7 @@ class Zonefile
add_record( 'rrsig', :name => $1, :ttl => $2, :class => $3, :type_covered => $4, :algorithm => $5,
:labels => $6.to_i, :original_ttl => $7.to_i, :expiration => $8.to_i, :inception => $9.to_i,
:key_tag => $10.to_i, :signer => $11, :signature => $12.gsub( /\s/,'') )
elsif line=~/^(#{valid_name}) \s*
elsif line =~ /^(#{valid_name}) \s*
#{ttl_cls}
TLSA \s
(\d+) \s
......@@ -328,7 +327,7 @@ class Zonefile
/ix
add_record( 'tlsa', :name => $1, :ttl => $2, :class => $3, :certificate_usage => $4.to_i,
:selector => $5.to_i, :matching_type => $6.to_i, :data => $7 )
elsif line=~/^(#{valid_name})? \s*
elsif line =~ /^(#{valid_name})? \s*
#{ttl_cls}
NAPTR \s
(\d+) \s
......@@ -340,7 +339,7 @@ class Zonefile
/ix
add_record( 'naptr', :name => $1, :ttl => $2, :class => $3, :order => $4.to_i, :preference => $5.to_i,
:flags => $6, :service => $7, :regexp => $8, :replacement => $9 )
elsif line=~/^(#{valid_name}) \s+
elsif line =~ /^(#{valid_name}) \s+
#{ttl_cls}
SOA \s+
(#{valid_name}) \s+
......@@ -364,7 +363,7 @@ class Zonefile
@soa[:expire] = $9
@soa[:minimumTTL] = $10
elsif line=~ /^(#{valid_name})? \s*
elsif line =~ /^(#{valid_name})? \s*
#{ttl_cls}
PTR \s+
(#{valid_name})
......@@ -374,7 +373,7 @@ class Zonefile
add_record('txt', :name => $1, :ttl => $2, :class => $3, :text => $4.strip)
elsif line =~ /^(#{valid_name})? \s* #{ttl_cls} SPF \s+ (.*)$/ix
add_record('spf', :name => $1, :ttl => $2, :class => $3, :text => $4.strip)
elsif line=~/^(#{valid_name})? \s*
elsif line =~/^(#{valid_name})? \s*
#{ttl_cls}
SSHFP \s+
(\d+) \s+
......@@ -382,7 +381,7 @@ class Zonefile
#{hexadeimal}
/ix
add_record('sshfp', :name => $1, :ttl => $2, :class => $3, key_type: $4.to_i, fingerprint_type: $5.to_i, fingerprint: $6.strip)
elsif line=~/^(#{valid_name})? \s*
elsif line =~/^(#{valid_name})? \s*
#{ttl_cls}
CAA \s+
(\d+) \s+
......@@ -450,6 +449,7 @@ ENDH
out += "\n; Zone TXT Records\n" unless self.txt.empty?
self.txt.each do |tx|
#out += "#{tx[:name]} #{tx[:ttl]} #{tx[:class]} TXT (#{tx[:text].chars.each_slice(255).map { |txt_part| "\"#{txt_part.join(nil)}\"" }.join(' ')})\n"
out += "#{tx[:name]} #{tx[:ttl]} #{tx[:class]} TXT #{tx[:text]}\n"
end
......