root/lib/cyborghood/services/dns.rb @ 8fcaacd6
d32ee48a | Marc Dequenes | #--
|
|
# CyborgHood, a distributed system management software.
|
|||
e7315259 | Marc Dequènes (Duck) | # Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|
d32ee48a | Marc Dequenes | #
|
|
# This program is free software: you can redistribute it and/or modify
|
|||
# it under the terms of the GNU General Public License as published by
|
|||
# the Free Software Foundation, either version 3 of the License, or
|
|||
# (at your option) any later version.
|
|||
#
|
|||
# This program is distributed in the hope that it will be useful,
|
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|||
# GNU General Public License for more details.
|
|||
#
|
|||
# You should have received a copy of the GNU General Public License
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
#++
|
|||
157c68c9 | Marc Dequenes | require 'fileutils'
|
|
8fcaacd6 | Marc Dequènes (Duck) | # ensure we can find the needed programs (should be handled somewhere else)
|
|
ENV['PATH'] = (ENV['PATH'].split(":") + ["/sbin", "/usr/sbin", "/usr/local/sbin"]).uniq.join(":")
|
|||
4153cce4 | Marc Dequenes | module CyborgHood
|
|
module Services
|
|||
class DNS
|
|||
8fcaacd6 | Marc Dequènes (Duck) | def initialize
|
|
@config = Config.instance
|
|||
@zone_files_pattern = @config.dns.master_zone_pattern.gsub("#ZONE#", "*")
|
|||
@zone_files_regex = Regexp.new("^" + @config.dns.master_zone_pattern.gsub("#ZONE#", "(.*)") + "$")
|
|||
end
|
|||
def zones(zone = nil)
|
|||
if zone.nil?
|
|||
Dir.glob(@zone_files_pattern).collect do |file|
|
|||
$1 if file =~ @zone_files_regex
|
|||
end
|
|||
else
|
|||
Zone.new(zone)
|
|||
end
|
|||
end
|
|||
end
|
|||
class Zone
|
|||
157c68c9 | Marc Dequenes | attr_reader :zone
|
|
attr_accessor :content
|
|||
4153cce4 | Marc Dequenes | def initialize(zone)
|
|
@zone = zone
|
|||
@config = Config.instance
|
|||
157c68c9 | Marc Dequenes | @content = nil
|
|
fba691ae | Marc Dequenes | @filename = @config.dns.master_zone_pattern.gsub("#ZONE#", @zone)
|
|
4153cce4 | Marc Dequenes | end
|
|
def read_zone
|
|||
begin
|
|||
fba691ae | Marc Dequenes | @content = File.read(@filename)
|
|
4153cce4 | Marc Dequenes | rescue
|
|
raise CyberError.new(:unrecoverable, "services/dns", "zone '#{@zone}' cannot be read (nonexistent or lack of permission)")
|
|||
end
|
|||
end
|
|||
157c68c9 | Marc Dequenes | ||
def serial
|
|||
8fcaacd6 | Marc Dequènes (Duck) | case @config.dns.software
|
|
when 'bind'
|
|||
157c68c9 | Marc Dequenes | output = []
|
|
begin
|
|||
fba691ae | Marc Dequenes | IO.popen("named-checkzone -i none '#{@zone}' #{@filename}") do |fp|
|
|
157c68c9 | Marc Dequenes | output << fp.gets.chomp! until fp.eof?
|
|
end
|
|||
rescue
|
|||
8fcaacd6 | Marc Dequènes (Duck) | raise CyberError.new(:unrecoverable, "services/dns", "zone serial for '#{@zone}' could not be found (I/O error)")
|
|
157c68c9 | Marc Dequenes | end
|
|
8fcaacd6 | Marc Dequènes (Duck) | raise CyberError.new(:unrecoverable, "services/dns", "zone serial for '#{@zone}' could not be found (#{output.first})") unless $?.success?
|
|
157c68c9 | Marc Dequenes | ||
if $? == 0
|
|||
serial = nil
|
|||
output.each do |l|
|
|||
if l =~ /: loaded serial (\d+)$/
|
|||
serial = $1
|
|||
break
|
|||
end
|
|||
end
|
|||
if serial
|
|||
serial
|
|||
else
|
|||
raise CyberError.new(:unrecoverable, "services/dns", "no serial returned")
|
|||
end
|
|||
else
|
|||
nil
|
|||
end
|
|||
end
|
|||
end
|
|||
def check_zone_file(filename)
|
|||
8fcaacd6 | Marc Dequènes (Duck) | case @config.dns.software
|
|
when 'bind'
|
|||
157c68c9 | Marc Dequenes | output = []
|
|
begin
|
|||
IO.popen("named-checkzone '#{@zone}' #{filename}") do |fp|
|
|||
output << fp.gets.chomp! until fp.eof?
|
|||
end
|
|||
rescue
|
|||
raise CyberError.new(:unrecoverable, "services/dns", "zone '#{@zone}' could not be checked")
|
|||
end
|
|||
if $? == 0
|
|||
serial = nil
|
|||
messages = []
|
|||
output.each do |l|
|
|||
next if l == "OK"
|
|||
if l =~ /: loaded serial (\d+)$/
|
|||
serial = $1
|
|||
next
|
|||
end
|
|||
messages << l
|
|||
end
|
|||
if serial
|
|||
return {:ok => true, :serial => serial, :warnings => messages}.to_ostruct
|
|||
else
|
|||
raise CyberError.new(:unrecoverable, "services/dns", "zone validated but no serial returned")
|
|||
end
|
|||
else
|
|||
return {:ok => false, :errors => messages}.to_ostruct
|
|||
end
|
|||
end
|
|||
end
|
|||
def write_zone_from_file(new_zone_filename)
|
|||
fba691ae | Marc Dequenes | # create backup
|
|
FileUtils.cp(@filename, backup_zone_filename())
|
|||
FileUtils.cp(new_zone_filename, @filename)
|
|||
end
|
|||
def replace_zone_with_backup
|
|||
if File.exists?(backup_zone_filename())
|
|||
FileUtils.cp(backup_zone_filename(), @filename)
|
|||
else
|
|||
raise CyberError.new(:unrecoverable, "services/dns", "no zone backup file found")
|
|||
end
|
|||
end
|
|||
def reload_zone
|
|||
system "sudo rndc reload '#{@zone}' >/dev/null"
|
|||
end
|
|||
private
|
|||
157c68c9 | Marc Dequenes | ||
fba691ae | Marc Dequenes | def backup_zone_filename
|
|
@filename + ".ch-backup"
|
|||
157c68c9 | Marc Dequenes | end
|
|
4153cce4 | Marc Dequenes | end
|
|
end
|
|||
end
|