2020-03-06 21:33:29 +01:00
|
|
|
# frozen_string_literal: true
|
2013-04-27 14:07:14 +02:00
|
|
|
|
2020-08-14 17:59:53 +02:00
|
|
|
require 'date'
|
2013-04-30 23:19:08 +02:00
|
|
|
require 'etc'
|
2013-04-27 14:07:14 +02:00
|
|
|
require 'logger'
|
|
|
|
require 'ipaddr'
|
|
|
|
require 'json'
|
|
|
|
require 'yaml'
|
|
|
|
require 'rack'
|
2022-09-07 19:46:35 +02:00
|
|
|
require 'rackup'
|
2013-04-28 16:29:36 +02:00
|
|
|
require 'metriks'
|
2021-05-28 11:15:14 +02:00
|
|
|
require 'opentelemetry/instrumentation/rack'
|
|
|
|
require 'opentelemetry/sdk'
|
2013-06-05 20:18:17 +02:00
|
|
|
require 'metriks/reporter/graphite'
|
2013-04-27 14:07:14 +02:00
|
|
|
|
|
|
|
require 'dyndnsd/generator/bind'
|
|
|
|
require 'dyndnsd/updater/command_with_bind_zone'
|
2020-07-18 14:49:23 +02:00
|
|
|
require 'dyndnsd/updater/zone_transfer_server'
|
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'
|
2018-02-03 21:40:44 +01:00
|
|
|
require 'dyndnsd/helper'
|
2018-04-23 07:11:18 +02:00
|
|
|
require 'dyndnsd/textfile_reporter'
|
2013-04-27 14:07:14 +02:00
|
|
|
require 'dyndnsd/version'
|
|
|
|
|
|
|
|
module Dyndnsd
|
2020-02-28 15:13:28 +01:00
|
|
|
# @return [Logger]
|
2013-04-27 14:07:14 +02:00
|
|
|
def self.logger
|
|
|
|
@logger
|
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param logger [Logger]
|
|
|
|
# @return [Logger]
|
2013-04-27 14:07:14 +02:00
|
|
|
def self.logger=(logger)
|
|
|
|
@logger = logger
|
|
|
|
end
|
|
|
|
|
|
|
|
class LogFormatter
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param lvl [Object]
|
|
|
|
# @param _time [DateTime]
|
|
|
|
# @param _progname [String]
|
|
|
|
# @param msg [Object]
|
|
|
|
# @return [String]
|
2018-02-23 12:54:43 +01:00
|
|
|
def call(lvl, _time, _progname, msg)
|
|
|
|
format("[%s] %-5s %s\n", Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s)
|
2013-04-27 14:07:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Daemon
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param config [Hash{String => Object}]
|
|
|
|
# @param db [Dyndnsd::Database]
|
|
|
|
# @param updater [#update]
|
2018-02-01 20:26:48 +01:00
|
|
|
def initialize(config, db, updater)
|
2013-04-27 14:07:14 +02:00
|
|
|
@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
|
|
|
|
|
|
|
|
@db.load
|
|
|
|
@db['serial'] ||= 1
|
|
|
|
@db['hosts'] ||= {}
|
2020-07-20 11:56:23 +02:00
|
|
|
@updater.update(@db)
|
2018-02-23 12:54:43 +01:00
|
|
|
if @db.changed?
|
|
|
|
@db.save
|
|
|
|
end
|
2013-04-27 14:59:25 +02:00
|
|
|
end
|
2016-11-30 21:59:12 +01:00
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param username [String]
|
|
|
|
# @param password [String]
|
|
|
|
# @return [Boolean]
|
2018-02-23 12:54:43 +01:00
|
|
|
def authorized?(username, password)
|
2018-01-26 16:25:15 +01:00
|
|
|
Helper.span('check_authorized') do |span|
|
2021-05-28 11:15:14 +02:00
|
|
|
span.set_attribute('enduser.id', username)
|
2018-01-26 16:25:15 +01:00
|
|
|
|
|
|
|
allow = Helper.user_allowed?(username, password, @users)
|
|
|
|
if !allow
|
|
|
|
Dyndnsd.logger.warn "Login failed for #{username}"
|
|
|
|
Metriks.meter('requests.auth_failed').mark
|
|
|
|
end
|
|
|
|
allow
|
2018-02-05 15:29:12 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param env [Hash{String => String}]
|
2020-08-14 17:59:53 +02:00
|
|
|
# @return [Array{Integer,Hash{String => String},Array<String>}]
|
2013-04-27 14:07:14 +02:00
|
|
|
def call(env)
|
2018-02-23 12:54:43 +01:00
|
|
|
return [422, {'X-DynDNS-Response' => 'method_forbidden'}, []] if env['REQUEST_METHOD'] != 'GET'
|
|
|
|
return [422, {'X-DynDNS-Response' => 'not_found'}, []] if env['PATH_INFO'] != '/nic/update'
|
2016-11-30 21:59:12 +01:00
|
|
|
|
2018-02-01 21:50:56 +01:00
|
|
|
handle_dyndns_request(env)
|
2013-04-27 14:07:14 +02:00
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @return [void]
|
2013-04-27 14:07:14 +02:00
|
|
|
def self.run!
|
|
|
|
if ARGV.length != 1
|
2018-02-23 12:54:43 +01:00
|
|
|
puts 'Usage: dyndnsd config_file'
|
2013-04-27 14:07:14 +02:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
config_file = ARGV[0]
|
|
|
|
|
2018-02-23 12:54:43 +01:00
|
|
|
if !File.file?(config_file)
|
|
|
|
puts 'Config file not found!'
|
2013-04-27 14:07:14 +02:00
|
|
|
exit 1
|
|
|
|
end
|
2016-11-30 21:59:12 +01:00
|
|
|
|
2013-04-27 22:02:54 +02:00
|
|
|
puts "DynDNSd version #{Dyndnsd::VERSION}"
|
|
|
|
puts "Using config file #{config_file}"
|
2013-04-27 14:07:14 +02:00
|
|
|
|
2023-06-24 13:21:22 +02:00
|
|
|
config = YAML.safe_load_file(config_file)
|
2016-11-30 21:59:12 +01:00
|
|
|
|
2018-02-02 16:45:53 +01:00
|
|
|
setup_logger(config)
|
2013-04-27 22:02:54 +02:00
|
|
|
|
2018-02-23 12:54:43 +01:00
|
|
|
Dyndnsd.logger.info 'Starting...'
|
2016-11-30 21:59:12 +01:00
|
|
|
|
2021-06-03 20:49:20 +02:00
|
|
|
# drop privileges as soon as possible
|
2018-02-02 16:45:53 +01:00
|
|
|
# NOTE: first change group than user
|
2020-02-28 16:44:27 +01:00
|
|
|
if config['group']
|
|
|
|
group = Etc.getgrnam(config['group'])
|
|
|
|
Process::Sys.setgid(group.gid) if group
|
|
|
|
end
|
|
|
|
if config['user']
|
|
|
|
user = Etc.getpwnam(config['user'])
|
|
|
|
Process::Sys.setuid(user.uid) if user
|
|
|
|
end
|
2013-04-27 14:07:14 +02:00
|
|
|
|
2018-02-23 12:54:43 +01:00
|
|
|
setup_traps
|
2013-04-28 16:29:36 +02:00
|
|
|
|
2018-02-02 16:45:53 +01:00
|
|
|
setup_monitoring(config)
|
2016-11-30 21:59:12 +01:00
|
|
|
|
2018-01-26 16:25:15 +01:00
|
|
|
setup_tracing(config)
|
|
|
|
|
2018-02-02 16:45:53 +01:00
|
|
|
setup_rack(config)
|
2013-04-27 14:07:14 +02:00
|
|
|
end
|
2018-02-01 21:50:56 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param params [Hash{String => String}]
|
2020-08-14 17:59:53 +02:00
|
|
|
# @return [Array<String>]
|
2018-02-23 12:54:43 +01:00
|
|
|
def extract_v4_and_v6_address(params)
|
|
|
|
return [] if !(params['myip'])
|
2018-02-02 16:14:28 +01:00
|
|
|
begin
|
2018-02-23 12:54:43 +01:00
|
|
|
IPAddr.new(params['myip'], Socket::AF_INET)
|
|
|
|
IPAddr.new(params['myip6'], Socket::AF_INET6)
|
|
|
|
[params['myip'], params['myip6']]
|
2018-02-02 16:14:28 +01:00
|
|
|
rescue ArgumentError
|
|
|
|
[]
|
2018-02-01 21:50:56 +01:00
|
|
|
end
|
2018-02-02 16:14:28 +01:00
|
|
|
end
|
2018-02-01 21:50:56 +01:00
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param env [Hash{String => String}]
|
|
|
|
# @param params [Hash{String => String}]
|
2020-08-14 17:59:53 +02:00
|
|
|
# @return [Array<String>]
|
2018-02-02 16:14:28 +01:00
|
|
|
def extract_myips(env, params)
|
|
|
|
# require presence of myip parameter as valid IPAddr (v4) and valid myip6
|
2018-02-23 12:54:43 +01:00
|
|
|
return extract_v4_and_v6_address(params) if params.key?('myip6')
|
2018-02-02 16:14:28 +01:00
|
|
|
|
|
|
|
# check whether myip parameter has valid IPAddr
|
2018-02-23 12:54:43 +01:00
|
|
|
return [params['myip']] if params.key?('myip') && Helper.ip_valid?(params['myip'])
|
2018-02-02 16:14:28 +01:00
|
|
|
|
|
|
|
# check whether X-Real-IP header has valid IPAddr
|
2018-02-23 12:54:43 +01:00
|
|
|
return [env['HTTP_X_REAL_IP']] if env.key?('HTTP_X_REAL_IP') && Helper.ip_valid?(env['HTTP_X_REAL_IP'])
|
2018-02-02 16:14:28 +01:00
|
|
|
|
|
|
|
# fallback value, always present
|
2018-02-23 12:54:43 +01:00
|
|
|
[env['REMOTE_ADDR']]
|
2018-02-01 21:50:56 +01:00
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param hostnames [String]
|
2020-08-14 17:59:53 +02:00
|
|
|
# @param myips [Array<String>]
|
|
|
|
# @return [Array<Symbol>]
|
2018-02-02 16:14:28 +01:00
|
|
|
def process_changes(hostnames, myips)
|
2018-02-01 21:50:56 +01:00
|
|
|
changes = []
|
2018-01-26 16:25:15 +01:00
|
|
|
Helper.span('process_changes') do |span|
|
2021-05-28 11:15:14 +02:00
|
|
|
span.set_attribute('dyndnsd.hostnames', hostnames.join(','))
|
2018-01-26 16:25:15 +01:00
|
|
|
|
|
|
|
hostnames.each do |hostname|
|
|
|
|
# myips order is always deterministic
|
2018-02-23 16:37:10 +01:00
|
|
|
if myips.empty? && @db['hosts'].include?(hostname)
|
|
|
|
@db['hosts'].delete(hostname)
|
|
|
|
changes << :good
|
|
|
|
Metriks.meter('requests.good').mark
|
|
|
|
elsif Helper.changed?(hostname, myips, @db['hosts'])
|
2018-01-26 16:25:15 +01:00
|
|
|
@db['hosts'][hostname] = myips
|
|
|
|
changes << :good
|
|
|
|
Metriks.meter('requests.good').mark
|
|
|
|
else
|
|
|
|
changes << :nochg
|
|
|
|
Metriks.meter('requests.nochg').mark
|
|
|
|
end
|
2018-02-01 21:50:56 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
changes
|
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @return [void]
|
2018-02-23 12:54:43 +01:00
|
|
|
def update_db
|
2018-02-01 21:50:56 +01:00
|
|
|
@db['serial'] += 1
|
|
|
|
Dyndnsd.logger.info "Committing update ##{@db['serial']}"
|
|
|
|
@updater.update(@db)
|
2020-07-20 11:56:23 +02:00
|
|
|
@db.save
|
2018-02-01 21:50:56 +01:00
|
|
|
Metriks.meter('updates.committed').mark
|
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param env [Hash{String => String}]
|
2020-08-14 17:59:53 +02:00
|
|
|
# @return [Array{Integer,Hash{String => String},Array<String>}]
|
2018-02-01 21:50:56 +01:00
|
|
|
def handle_dyndns_request(env)
|
2018-02-23 12:54:43 +01:00
|
|
|
params = Rack::Utils.parse_query(env['QUERY_STRING'])
|
2018-02-01 21:50:56 +01:00
|
|
|
|
2018-02-02 16:14:28 +01:00
|
|
|
# require hostname parameter
|
2018-02-23 12:54:43 +01:00
|
|
|
return [422, {'X-DynDNS-Response' => 'hostname_missing'}, []] if !(params['hostname'])
|
2018-02-01 21:50:56 +01:00
|
|
|
|
2018-02-23 12:54:43 +01:00
|
|
|
hostnames = params['hostname'].split(',')
|
2018-02-01 21:50:56 +01:00
|
|
|
|
2018-02-02 16:14:28 +01:00
|
|
|
# check for invalid hostnames
|
2018-01-26 16:25:15 +01:00
|
|
|
invalid_hostnames = hostnames.select { |h| !Helper.fqdn_valid?(h, @domain) }
|
2018-02-02 16:14:28 +01:00
|
|
|
return [422, {'X-DynDNS-Response' => 'hostname_malformed'}, []] if invalid_hostnames.any?
|
2018-02-01 21:50:56 +01:00
|
|
|
|
2022-09-20 19:30:02 +02:00
|
|
|
# we can trust this information since user was authorized by middleware
|
2018-02-23 12:54:43 +01:00
|
|
|
user = env['REMOTE_USER']
|
2018-02-01 21:50:56 +01:00
|
|
|
|
2018-02-02 16:14:28 +01:00
|
|
|
# check for hostnames that the user does not own
|
2022-09-20 19:30:02 +02:00
|
|
|
forbidden_hostnames = hostnames - @users[user].fetch('hosts', [])
|
2018-02-02 16:14:28 +01:00
|
|
|
return [422, {'X-DynDNS-Response' => 'host_forbidden'}, []] if forbidden_hostnames.any?
|
2018-02-01 21:50:56 +01:00
|
|
|
|
2018-02-23 16:37:10 +01:00
|
|
|
if params['offline'] == 'YES'
|
|
|
|
myips = []
|
|
|
|
else
|
|
|
|
myips = extract_myips(env, params)
|
|
|
|
# require at least one IP to update
|
|
|
|
return [422, {'X-DynDNS-Response' => 'host_forbidden'}, []] if myips.empty?
|
|
|
|
end
|
2018-02-01 21:50:56 +01:00
|
|
|
|
|
|
|
Metriks.meter('requests.valid').mark
|
2018-02-02 16:14:28 +01:00
|
|
|
Dyndnsd.logger.info "Request to update #{hostnames} to #{myips} for user #{user}"
|
2018-02-01 21:50:56 +01:00
|
|
|
|
2018-02-02 16:14:28 +01:00
|
|
|
changes = process_changes(hostnames, myips)
|
2018-02-01 21:50:56 +01:00
|
|
|
|
|
|
|
update_db if @db.changed?
|
|
|
|
|
2018-02-02 16:14:28 +01:00
|
|
|
[200, {'X-DynDNS-Response' => 'success'}, [changes, myips]]
|
2018-02-01 21:50:56 +01:00
|
|
|
end
|
2018-02-02 16:45:53 +01:00
|
|
|
|
|
|
|
# SETUP
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param config [Hash{String => Object}]
|
|
|
|
# @return [void]
|
2018-02-23 12:54:43 +01:00
|
|
|
private_class_method def self.setup_logger(config)
|
2018-02-02 16:45:53 +01:00
|
|
|
if config['logfile']
|
|
|
|
Dyndnsd.logger = Logger.new(config['logfile'])
|
|
|
|
else
|
2020-08-07 08:36:49 +02:00
|
|
|
Dyndnsd.logger = Logger.new($stdout)
|
2018-02-02 16:45:53 +01:00
|
|
|
end
|
|
|
|
|
2018-02-23 12:54:43 +01:00
|
|
|
Dyndnsd.logger.progname = 'dyndnsd'
|
2018-02-02 16:45:53 +01:00
|
|
|
Dyndnsd.logger.formatter = LogFormatter.new
|
2020-07-20 12:04:22 +02:00
|
|
|
Dyndnsd.logger.level = config['debug'] ? Logger::DEBUG : Logger::INFO
|
2021-05-28 11:15:14 +02:00
|
|
|
|
|
|
|
OpenTelemetry.logger = Dyndnsd.logger
|
2018-02-02 16:45:53 +01:00
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @return [void]
|
2018-02-23 12:54:43 +01:00
|
|
|
private_class_method def self.setup_traps
|
2018-02-02 16:45:53 +01:00
|
|
|
Signal.trap('INT') do
|
2022-09-07 19:46:35 +02:00
|
|
|
Rackup::Handler::WEBrick.shutdown
|
2018-02-02 16:45:53 +01:00
|
|
|
end
|
|
|
|
Signal.trap('TERM') do
|
2022-09-07 19:46:35 +02:00
|
|
|
Rackup::Handler::WEBrick.shutdown
|
2018-02-02 16:45:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param config [Hash{String => Object}]
|
|
|
|
# @return [void]
|
2018-02-23 12:54:43 +01:00
|
|
|
private_class_method def self.setup_monitoring(config)
|
2018-02-02 16:45:53 +01:00
|
|
|
# configure metriks
|
|
|
|
if config['graphite']
|
|
|
|
host = config['graphite']['host'] || 'localhost'
|
|
|
|
port = config['graphite']['port'] || 2003
|
|
|
|
options = {}
|
|
|
|
options[:prefix] = config['graphite']['prefix'] if config['graphite']['prefix']
|
|
|
|
reporter = Metriks::Reporter::Graphite.new(host, port, options)
|
|
|
|
reporter.start
|
2018-04-23 07:11:18 +02:00
|
|
|
elsif config['textfile']
|
|
|
|
file = config['textfile']['file'] || '/tmp/dyndnsd-metrics.prom'
|
|
|
|
options = {}
|
|
|
|
options[:prefix] = config['textfile']['prefix'] if config['textfile']['prefix']
|
|
|
|
reporter = Dyndnsd::TextfileReporter.new(file, options)
|
|
|
|
reporter.start
|
2018-02-02 16:45:53 +01:00
|
|
|
else
|
|
|
|
reporter = Metriks::Reporter::ProcTitle.new
|
|
|
|
reporter.add 'good', 'sec' do
|
|
|
|
Metriks.meter('requests.good').mean_rate
|
|
|
|
end
|
|
|
|
reporter.add 'nochg', 'sec' do
|
|
|
|
Metriks.meter('requests.nochg').mean_rate
|
|
|
|
end
|
|
|
|
reporter.start
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param config [Hash{String => Object}]
|
|
|
|
# @return [void]
|
2018-01-26 16:25:15 +01:00
|
|
|
private_class_method def self.setup_tracing(config)
|
2021-05-28 11:15:14 +02:00
|
|
|
# by default do not try to emit any traces until the user opts in
|
|
|
|
ENV['OTEL_TRACES_EXPORTER'] ||= 'none'
|
|
|
|
|
|
|
|
# configure OpenTelemetry
|
|
|
|
OpenTelemetry::SDK.configure do |c|
|
|
|
|
if config.dig('tracing', 'jaeger')
|
|
|
|
require 'opentelemetry/exporter/jaeger'
|
|
|
|
|
|
|
|
c.add_span_processor(
|
|
|
|
OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
|
|
|
|
OpenTelemetry::Exporter::Jaeger::AgentExporter.new
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
if config.dig('tracing', 'service_name')
|
|
|
|
c.service_name = config['tracing']['service_name']
|
|
|
|
end
|
|
|
|
|
|
|
|
c.service_version = Dyndnsd::VERSION
|
|
|
|
c.use('OpenTelemetry::Instrumentation::Rack')
|
|
|
|
end
|
|
|
|
|
|
|
|
if !config.dig('tracing', 'trust_incoming_span')
|
|
|
|
OpenTelemetry.propagation = OpenTelemetry::Context::Propagation::NoopTextMapPropagator.new
|
2018-01-26 16:25:15 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-28 15:13:28 +01:00
|
|
|
# @param config [Hash{String => Object}]
|
|
|
|
# @return [void]
|
2018-02-23 12:54:43 +01:00
|
|
|
private_class_method def self.setup_rack(config)
|
2018-02-02 16:45:53 +01:00
|
|
|
# configure daemon
|
|
|
|
db = Database.new(config['db'])
|
2020-07-20 11:56:23 +02:00
|
|
|
case config.dig('updater', 'name')
|
|
|
|
when 'command_with_bind_zone'
|
|
|
|
updater = Updater::CommandWithBindZone.new(config['domain'], config.dig('updater', 'params'))
|
2020-07-18 14:49:23 +02:00
|
|
|
when 'zone_transfer_server'
|
|
|
|
updater = Updater::ZoneTransferServer.new(config['domain'], config.dig('updater', 'params'))
|
2020-07-20 11:56:23 +02:00
|
|
|
end
|
2018-02-05 15:29:12 +01:00
|
|
|
daemon = Daemon.new(config, db, updater)
|
2018-02-02 16:45:53 +01:00
|
|
|
|
|
|
|
# configure rack
|
2018-02-23 12:54:43 +01:00
|
|
|
app = Rack::Auth::Basic.new(daemon, 'DynDNS', &daemon.method(:authorized?))
|
2018-02-02 16:45:53 +01:00
|
|
|
|
|
|
|
if config['responder'] == 'RestStyle'
|
|
|
|
app = Responder::RestStyle.new(app)
|
|
|
|
else
|
|
|
|
app = Responder::DynDNSStyle.new(app)
|
|
|
|
end
|
|
|
|
|
2021-05-28 11:15:14 +02:00
|
|
|
app = OpenTelemetry::Instrumentation::Rack::Middlewares::TracerMiddleware.new(app)
|
2018-01-26 16:25:15 +01:00
|
|
|
|
2022-09-07 19:46:35 +02:00
|
|
|
Rackup::Handler::WEBrick.run app, Host: config['host'], Port: config['port']
|
2018-02-02 16:45:53 +01:00
|
|
|
end
|
2013-04-27 14:07:14 +02:00
|
|
|
end
|
|
|
|
end
|