mirror of
https://github.com/cmur2/dyndnsd.git
synced 2025-06-26 02:30:22 +02:00
Fiat lux
This commit is contained in:
127
lib/dyndnsd.rb
Normal file
127
lib/dyndnsd.rb
Normal file
@ -0,0 +1,127 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require 'logger'
|
||||
require 'ipaddr'
|
||||
require 'json'
|
||||
require 'yaml'
|
||||
require 'rack'
|
||||
|
||||
require 'dyndnsd/generator/bind'
|
||||
require 'dyndnsd/updater/command_with_bind_zone'
|
||||
require 'dyndnsd/responder/rest_style'
|
||||
require 'dyndnsd/database'
|
||||
require 'dyndnsd/version'
|
||||
|
||||
module Dyndnsd
|
||||
def self.logger
|
||||
@logger
|
||||
end
|
||||
|
||||
def self.logger=(logger)
|
||||
@logger = logger
|
||||
end
|
||||
|
||||
class LogFormatter
|
||||
def call(lvl, time, progname, msg)
|
||||
"%s: %s\n" % [lvl, msg.to_s]
|
||||
end
|
||||
end
|
||||
|
||||
class Daemon
|
||||
def initialize(config, db, updater, responder)
|
||||
@users = config['users']
|
||||
@db = db
|
||||
@updater = updater
|
||||
@responder = responder
|
||||
|
||||
@db.load
|
||||
@db['serial'] ||= 1
|
||||
@db['hosts'] ||= {}
|
||||
(@db.save; update) if @db.changed?
|
||||
end
|
||||
|
||||
def update
|
||||
@updater.update(@db)
|
||||
end
|
||||
|
||||
def call(env)
|
||||
return @responder.response_for(:method_forbidden) if env["REQUEST_METHOD"] != "GET"
|
||||
return @responder.response_for(:not_found) if env["PATH_INFO"] != "/nic/update"
|
||||
|
||||
params = Rack::Utils.parse_query(env["QUERY_STRING"])
|
||||
|
||||
return @responder.response_for(:hostname_missing) if not params["hostname"]
|
||||
|
||||
hostname = params["hostname"]
|
||||
|
||||
# Check if hostname(s) match rules
|
||||
#return @responder.response_for(:hostname_malformed) if XY
|
||||
|
||||
user = env["REMOTE_USER"]
|
||||
|
||||
return @responder.response_for(:host_forbidden) if not @users[user]['hosts'].include? hostname
|
||||
|
||||
# no myip?
|
||||
if not params["myip"]
|
||||
params["myip"] = env["REMOTE_ADDR"]
|
||||
end
|
||||
|
||||
# malformed myip?
|
||||
begin
|
||||
IPAddr.new(params["myip"], Socket::AF_INET)
|
||||
rescue ArgumentError
|
||||
params["myip"] = env["REMOTE_ADDR"]
|
||||
end
|
||||
|
||||
myip = params["myip"]
|
||||
|
||||
@db['hosts'][hostname] = myip
|
||||
|
||||
if @db.changed?
|
||||
@db['serial'] += 1
|
||||
@db.save
|
||||
update
|
||||
return @responder.response_for(:good)
|
||||
end
|
||||
|
||||
@responder.response_for(:nochg)
|
||||
end
|
||||
|
||||
def self.run!
|
||||
Dyndnsd.logger = Logger.new(STDOUT)
|
||||
Dyndnsd.logger.formatter = LogFormatter.new
|
||||
|
||||
if ARGV.length != 1
|
||||
puts "Usage: dyndnsd config_file"
|
||||
exit 1
|
||||
end
|
||||
|
||||
config_file = ARGV[0]
|
||||
|
||||
if not File.file?(config_file)
|
||||
Dyndnsd.logger.fatal "Config file not found!"
|
||||
exit 1
|
||||
end
|
||||
|
||||
Dyndnsd.logger.info "DynDNSd version #{Dyndnsd::VERSION}"
|
||||
Dyndnsd.logger.info "Using config file #{config_file}"
|
||||
|
||||
config = YAML::load(File.open(config_file, 'r') { |f| f.read })
|
||||
|
||||
db = Database.new(config['db_file'])
|
||||
updater = Updater::CommandWithBindZone.new(config['updater_params']) if config['updater'] == 'command_with_bind_zone'
|
||||
responder = Responder::RestStyle.new
|
||||
|
||||
app = Daemon.new(config, db, updater, responder)
|
||||
app = Rack::Auth::Basic.new(app, "DynDNS") do |user,pass|
|
||||
(config['users'].has_key? user) and (config['users'][user]['password'] == pass)
|
||||
end
|
||||
|
||||
Signal.trap('INT') do
|
||||
Rack::Handler::WEBrick.shutdown
|
||||
end
|
||||
|
||||
Rack::Handler::WEBrick.run app, :Host => config['host'], :Port => config['port']
|
||||
end
|
||||
end
|
||||
end
|
32
lib/dyndnsd/database.rb
Normal file
32
lib/dyndnsd/database.rb
Normal file
@ -0,0 +1,32 @@
|
||||
|
||||
require 'forwardable'
|
||||
|
||||
module Dyndnsd
|
||||
class Database
|
||||
extend Forwardable
|
||||
|
||||
def_delegators :@db, :[], :[]=, :each, :has_key?
|
||||
|
||||
def initialize(db_file)
|
||||
@db_file = db_file
|
||||
end
|
||||
|
||||
def load
|
||||
if File.file?(@db_file)
|
||||
@db = JSON.load(File.open(@db_file, 'r') { |f| f.read })
|
||||
else
|
||||
@db = {}
|
||||
end
|
||||
@db_hash = @db.hash
|
||||
end
|
||||
|
||||
def save
|
||||
File.open(@db_file, 'w') { |f| JSON.dump(@db, f) }
|
||||
@db_hash = @db.hash
|
||||
end
|
||||
|
||||
def changed?
|
||||
@db_hash != @db.hash
|
||||
end
|
||||
end
|
||||
end
|
29
lib/dyndnsd/generator/bind.rb
Normal file
29
lib/dyndnsd/generator/bind.rb
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
module Dyndnsd
|
||||
module Generator
|
||||
class Bind
|
||||
def initialize(config)
|
||||
@ttl = config['ttl']
|
||||
@origin = config['origin']
|
||||
@dns = config['dns']
|
||||
@email_addr = config['email_addr']
|
||||
end
|
||||
|
||||
def generate(zone)
|
||||
out = []
|
||||
out << "$TTL #{@ttl}"
|
||||
out << "$ORIGIN #{@origin}"
|
||||
out << ""
|
||||
out << "@ IN SOA #{@dns} #{@email_addr} ( #{zone['serial']} 3h 5m 1w 1h )"
|
||||
out << "@ IN NS #{@dns}"
|
||||
out << ""
|
||||
zone['hosts'].each do |hostname,ip|
|
||||
name = hostname.chomp('.' + @origin[0..-2])
|
||||
out << "#{name} IN A #{ip}"
|
||||
end
|
||||
out << ""
|
||||
out.join("\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
19
lib/dyndnsd/responder/rest_style.rb
Normal file
19
lib/dyndnsd/responder/rest_style.rb
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
module Dyndnsd
|
||||
module Responder
|
||||
class RestStyle
|
||||
def response_for(state)
|
||||
# general http errors
|
||||
return [405, {"Content-Type" => "text/plain"}, ["Method Not Allowed"]] if state == :method_forbidden
|
||||
return [404, {"Content-Type" => "text/plain"}, ["Not Found"]] if state == :not_found
|
||||
# specific errors
|
||||
return [422, {"Content-Type" => "text/plain"}, ["Hostname missing"]] if state == :hostname_missing
|
||||
return [403, {"Content-Type" => "text/plain"}, ["Forbidden"]] if state == :host_forbidden
|
||||
return [422, {"Content-Type" => "text/plain"}, ["Hostname malformed"]] if state == :hostname_malformed
|
||||
# OKs
|
||||
return [200, {"Content-Type" => "text/plain"}, ["Good"]] if state == :good
|
||||
return [200, {"Content-Type" => "text/plain"}, ["No change"]] if state == :nochg
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
21
lib/dyndnsd/updater/command_with_bind_zone.rb
Normal file
21
lib/dyndnsd/updater/command_with_bind_zone.rb
Normal file
@ -0,0 +1,21 @@
|
||||
|
||||
module Dyndnsd
|
||||
module Updater
|
||||
class CommandWithBindZone
|
||||
def initialize(config)
|
||||
@zone_file = config['zone_file']
|
||||
@command = config['command']
|
||||
@generator = Generator::Bind.new(config)
|
||||
end
|
||||
|
||||
def update(zone)
|
||||
# write zone file in bind syntax
|
||||
File.open(@zone_file, 'w') { |f| f.write(@generator.generate(zone)) }
|
||||
# call user-defined command
|
||||
pid = fork do
|
||||
exec @command
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
4
lib/dyndnsd/version.rb
Normal file
4
lib/dyndnsd/version.rb
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
module Dyndnsd
|
||||
VERSION = "0.0.1"
|
||||
end
|
Reference in New Issue
Block a user