diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..6fb63b3 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem 'zonefile' diff --git a/attributes/default.rb b/attributes/default.rb index e80690e..2b69441 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -26,3 +26,4 @@ default[:bind9][:data_path] = "/var/cache/bind" default[:bind9][:user] = "bind" end +default[:bind9][:serial_number] = 0 diff --git a/bin/zonefile_to_databag.rb b/bin/zonefile_to_databag.rb new file mode 100755 index 0000000..1c956e7 --- /dev/null +++ b/bin/zonefile_to_databag.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby +require 'zonefile' +require 'optparse' + +options = {} +optparse = OptionParser.new do |opts| + opts.banner = "Usage: zonefile_to_databag.rb [options]" + + opts.on("-z", "--zonefile FILE", "Parse Zone File") do |v| + options[:zonefile] = v + end +end + +begin + optparse.parse! + if options[:zonefile].nil? + puts optparse + raise OptionParser::MissingArgument + end +end + + +zf = Zonefile.from_file(options[:zonefile]) +puts '; MX-Records' +zf.mx.each do |mx_record| + puts "Mail Exchagne with priority: #{mx_record[:pri]} --> #{mx_record[:host]}" +end + +# Show SOA TTL +puts "; Record Time To Live: #{zf.soa[:ttl]}" + +# Show A-Records +puts "; A Records:" +zf.a.each do |a_record| + ttl_text = '' + if !a_record[:ttl].nil? and a_record[:ttl] != '' and a_record[:ttl] != zf.ttl + ttl_text = "\"ttl\": \"#{a_record[:ttl]}\", " + end + puts "{ \"type\": \"A\", #{ttl_text}\"name\": \"#{a_record[:name]}\", \"ip\": \"#{a_record[:host]}\"}," +end + +puts "; CNAME Records:" +zf.cname.each do |cname_record| + puts "{ \"type\": \"CNAME\" , \"name\": \"#{cname_record[:name]}\", \"ip\": \"#{cname_record[:host]}\"}," +end diff --git a/metadata.rb b/metadata.rb index 8ef079b..9abfb9d 100644 --- a/metadata.rb +++ b/metadata.rb @@ -1,9 +1,10 @@ +name "bind9" maintainer "Mike Adolphs" maintainer_email "mike@fooforge.com" license "Apache 2.0" description "Installs/Configures bind9" long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version "0.1.9" +version "0.1.10" %w{ ubuntu debian centos }.each do |os| supports os diff --git a/recipes/default.rb b/recipes/default.rb index 5c749ce..5291dd6 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -25,7 +25,7 @@ action :install end -directory "/var/log/bind/" do +directory "/var/log/named/" do owner node[:bind9][:user] group node[:bind9][:user] mode 0755 @@ -62,7 +62,8 @@ search(:zones).each do |zone| unless zone['autodomain'].nil? || zone['autodomain'] == '' - search(:node, "domain:#{zone['autodomain']}").each do |host| + log "fqdn:*.#{zone['autodomain']}" + search(:node, "fqdn:*.#{zone['autodomain']}").each do |host| next if host['ipaddress'] == '' || host['ipaddress'].nil? zone['zone_info']['records'].push( { "name" => host['hostname'], @@ -72,6 +73,19 @@ end end + # This cookbook documents yyyyMMddNN but attempts yyyyMMddhhmmss which is too long for BIND. Instead, let's wrap a 2 digit serial number in to the last 2 NN digits. + ruby_block "increment_serial_number" do + block do + current = node[:bind9][:serial_number].to_i + 1 + if current > 99 + current = 0 + end + node.set[:bind9][:serial_number] = current + end + action :nothing + end + + template "#{node[:bind9][:config_path]}/#{zone['domain']}" do source "#{node[:bind9][:config_path]}/#{zone['domain']}.erb" local true @@ -80,7 +94,7 @@ mode 0644 notifies :restart, resources(:service => "bind9") variables({ - :serial => Time.new.strftime("%Y%m%d%H%M%S") + :serial => Time.new.strftime("%Y%m%d") + node[:bind9][:serial_number].to_s.rjust(2, "0") }) action :nothing end @@ -93,12 +107,14 @@ variables({ :domain => zone['domain'], :soa => zone['zone_info']['soa'], + :soa_apex => zone['zone_info'].has_key?('soa_apex') ? zone['zone_info']['soa_apex'] : '@', :contact => zone['zone_info']['contact'], :global_ttl => zone['zone_info']['global_ttl'], :nameserver => zone['zone_info']['nameserver'], :mail_exchange => zone['zone_info']['mail_exchange'], :records => zone['zone_info']['records'] }) + notifies :run, resources(:ruby_block => "increment_serial_number"), :immediately notifies :create, resources(:template => "#{node[:bind9][:config_path]}/#{zone['domain']}"), :immediately end end diff --git a/templates/default/named.conf.options.erb b/templates/default/named.conf.options.erb index 1d536a9..71a1bd9 100644 --- a/templates/default/named.conf.options.erb +++ b/templates/default/named.conf.options.erb @@ -37,11 +37,13 @@ options { <% if node[:bind9][:enable_ipv6] %> listen-on-v6 { any; }; <% end %> + + transfer-format many-answers; }; logging { channel default_log { - file "/var/log/bind/bind.log" versions 5 size 128M; + file "/var/log/named/bind.log" versions 5 size 128M; print-time yes; print-severity yes; print-category yes; @@ -49,4 +51,5 @@ logging { category default { default_log; }; category general { default_log; }; + category lame-servers { null; }; }; diff --git a/templates/default/zonefile.erb b/templates/default/zonefile.erb index 5861fd3..94e94f7 100644 --- a/templates/default/zonefile.erb +++ b/templates/default/zonefile.erb @@ -1,5 +1,5 @@ $TTL <%= @global_ttl %> -@ IN SOA <%= @soa %> <%= @contact %> ( +<%= @soa_apex %> IN SOA <%= @soa %> <%= @contact %> ( <%%= @serial %> ; serial [yyyyMMddNN] 4H ; refresh 30M ; retry