2013-04-27 14:07:14 +02:00
|
|
|
#!/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'
|
2013-04-27 14:30:41 +02:00
|
|
|
require 'dyndnsd/responder/dyndns_style'
|
2013-04-27 14:07:14 +02:00
|
|
|
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']
|
2013-04-27 14:59:25 +02:00
|
|
|
@domain = config['domain']
|
2013-04-27 14:07:14 +02:00
|
|
|
@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
|
|
|
|
|
2013-04-27 14:59:25 +02:00
|
|
|
def is_fqdn_valid?(hostname)
|
|
|
|
return false if hostname.length < @domain.length + 2
|
|
|
|
return false if not hostname.end_with?(@domain)
|
|
|
|
name = hostname.chomp(@domain)
|
|
|
|
return false if not name.match(/^[a-zA-Z0-9_-]+\.$/)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-04-27 14:07:14 +02:00
|
|
|
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"]
|
|
|
|
|
2013-04-27 14:59:25 +02:00
|
|
|
# Check if hostname match rules
|
|
|
|
return @responder.response_for(:hostname_malformed) if not is_fqdn_valid?(hostname)
|
2013-04-27 14:07:14 +02:00
|
|
|
|
|
|
|
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
|
2013-04-27 14:30:15 +02:00
|
|
|
return @responder.response_for(:good, myip)
|
2013-04-27 14:07:14 +02:00
|
|
|
end
|
|
|
|
|
2013-04-27 14:30:15 +02:00
|
|
|
@responder.response_for(:nochg, myip)
|
2013-04-27 14:07:14 +02:00
|
|
|
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 })
|
|
|
|
|
2013-04-27 14:12:04 +02:00
|
|
|
db = Database.new(config['db'])
|
2013-04-27 15:08:36 +02:00
|
|
|
updater = Updater::CommandWithBindZone.new(config['domain'], config['updater']['params']) if config['updater']['name'] == 'command_with_bind_zone'
|
2013-04-27 14:30:41 +02:00
|
|
|
responder = Responder::DynDNSStyle.new
|
2013-04-27 14:07:14 +02:00
|
|
|
|
|
|
|
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
|