A small, lightweight and extensible DynDNS server written with Ruby and Rack.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dyndnsd.rb 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #!/usr/bin/env ruby
  2. require 'etc'
  3. require 'logger'
  4. require 'ipaddr'
  5. require 'json'
  6. require 'yaml'
  7. require 'rack'
  8. require 'metriks'
  9. require 'metriks/reporter/graphite'
  10. require 'opentracing'
  11. require 'rack/tracer'
  12. require 'spanmanager'
  13. require 'dyndnsd/generator/bind'
  14. require 'dyndnsd/updater/command_with_bind_zone'
  15. require 'dyndnsd/responder/dyndns_style'
  16. require 'dyndnsd/responder/rest_style'
  17. require 'dyndnsd/database'
  18. require 'dyndnsd/helper'
  19. require 'dyndnsd/textfile_reporter'
  20. require 'dyndnsd/version'
  21. module Dyndnsd
  22. def self.logger
  23. @logger
  24. end
  25. def self.logger=(logger)
  26. @logger = logger
  27. end
  28. class LogFormatter
  29. def call(lvl, _time, _progname, msg)
  30. format("[%s] %-5s %s\n", Time.now.strftime('%Y-%m-%d %H:%M:%S'), lvl, msg.to_s)
  31. end
  32. end
  33. class Daemon
  34. def initialize(config, db, updater)
  35. @users = config['users']
  36. @domain = config['domain']
  37. @db = db
  38. @updater = updater
  39. @db.load
  40. @db['serial'] ||= 1
  41. @db['hosts'] ||= {}
  42. if @db.changed?
  43. @db.save
  44. @updater.update(@db)
  45. end
  46. end
  47. def authorized?(username, password)
  48. Helper.span('check_authorized') do |span|
  49. span.set_tag('dyndnsd.user', username)
  50. allow = Helper.user_allowed?(username, password, @users)
  51. if !allow
  52. Dyndnsd.logger.warn "Login failed for #{username}"
  53. Metriks.meter('requests.auth_failed').mark
  54. end
  55. allow
  56. end
  57. end
  58. def call(env)
  59. return [422, {'X-DynDNS-Response' => 'method_forbidden'}, []] if env['REQUEST_METHOD'] != 'GET'
  60. return [422, {'X-DynDNS-Response' => 'not_found'}, []] if env['PATH_INFO'] != '/nic/update'
  61. handle_dyndns_request(env)
  62. end
  63. def self.run!
  64. if ARGV.length != 1
  65. puts 'Usage: dyndnsd config_file'
  66. exit 1
  67. end
  68. config_file = ARGV[0]
  69. if !File.file?(config_file)
  70. puts 'Config file not found!'
  71. exit 1
  72. end
  73. puts "DynDNSd version #{Dyndnsd::VERSION}"
  74. puts "Using config file #{config_file}"
  75. config = YAML.safe_load(File.open(config_file, 'r', &:read))
  76. setup_logger(config)
  77. Dyndnsd.logger.info 'Starting...'
  78. # drop priviliges as soon as possible
  79. # NOTE: first change group than user
  80. Process::Sys.setgid(Etc.getgrnam(config['group']).gid) if config['group']
  81. Process::Sys.setuid(Etc.getpwnam(config['user']).uid) if config['user']
  82. setup_traps
  83. setup_monitoring(config)
  84. setup_tracing(config)
  85. setup_rack(config)
  86. end
  87. private
  88. def extract_v4_and_v6_address(params)
  89. return [] if !(params['myip'])
  90. begin
  91. IPAddr.new(params['myip'], Socket::AF_INET)
  92. IPAddr.new(params['myip6'], Socket::AF_INET6)
  93. [params['myip'], params['myip6']]
  94. rescue ArgumentError
  95. []
  96. end
  97. end
  98. def extract_myips(env, params)
  99. # require presence of myip parameter as valid IPAddr (v4) and valid myip6
  100. return extract_v4_and_v6_address(params) if params.key?('myip6')
  101. # check whether myip parameter has valid IPAddr
  102. return [params['myip']] if params.key?('myip') && Helper.ip_valid?(params['myip'])
  103. # check whether X-Real-IP header has valid IPAddr
  104. return [env['HTTP_X_REAL_IP']] if env.key?('HTTP_X_REAL_IP') && Helper.ip_valid?(env['HTTP_X_REAL_IP'])
  105. # fallback value, always present
  106. [env['REMOTE_ADDR']]
  107. end
  108. def process_changes(hostnames, myips)
  109. changes = []
  110. Helper.span('process_changes') do |span|
  111. span.set_tag('dyndnsd.hostnames', hostnames.join(','))
  112. hostnames.each do |hostname|
  113. # myips order is always deterministic
  114. if myips.empty? && @db['hosts'].include?(hostname)
  115. @db['hosts'].delete(hostname)
  116. changes << :good
  117. Metriks.meter('requests.good').mark
  118. elsif Helper.changed?(hostname, myips, @db['hosts'])
  119. @db['hosts'][hostname] = myips
  120. changes << :good
  121. Metriks.meter('requests.good').mark
  122. else
  123. changes << :nochg
  124. Metriks.meter('requests.nochg').mark
  125. end
  126. end
  127. end
  128. changes
  129. end
  130. def update_db
  131. @db['serial'] += 1
  132. Dyndnsd.logger.info "Committing update ##{@db['serial']}"
  133. @db.save
  134. @updater.update(@db)
  135. Metriks.meter('updates.committed').mark
  136. end
  137. def handle_dyndns_request(env)
  138. params = Rack::Utils.parse_query(env['QUERY_STRING'])
  139. # require hostname parameter
  140. return [422, {'X-DynDNS-Response' => 'hostname_missing'}, []] if !(params['hostname'])
  141. hostnames = params['hostname'].split(',')
  142. # check for invalid hostnames
  143. invalid_hostnames = hostnames.select { |h| !Helper.fqdn_valid?(h, @domain) }
  144. return [422, {'X-DynDNS-Response' => 'hostname_malformed'}, []] if invalid_hostnames.any?
  145. user = env['REMOTE_USER']
  146. # check for hostnames that the user does not own
  147. forbidden_hostnames = hostnames - @users[user]['hosts']
  148. return [422, {'X-DynDNS-Response' => 'host_forbidden'}, []] if forbidden_hostnames.any?
  149. if params['offline'] == 'YES'
  150. myips = []
  151. else
  152. myips = extract_myips(env, params)
  153. # require at least one IP to update
  154. return [422, {'X-DynDNS-Response' => 'host_forbidden'}, []] if myips.empty?
  155. end
  156. Metriks.meter('requests.valid').mark
  157. Dyndnsd.logger.info "Request to update #{hostnames} to #{myips} for user #{user}"
  158. changes = process_changes(hostnames, myips)
  159. update_db if @db.changed?
  160. [200, {'X-DynDNS-Response' => 'success'}, [changes, myips]]
  161. end
  162. # SETUP
  163. private_class_method def self.setup_logger(config)
  164. if config['logfile']
  165. Dyndnsd.logger = Logger.new(config['logfile'])
  166. else
  167. Dyndnsd.logger = Logger.new(STDOUT)
  168. end
  169. Dyndnsd.logger.progname = 'dyndnsd'
  170. Dyndnsd.logger.formatter = LogFormatter.new
  171. end
  172. private_class_method def self.setup_traps
  173. Signal.trap('INT') do
  174. Dyndnsd.logger.info 'Quitting...'
  175. Rack::Handler::WEBrick.shutdown
  176. end
  177. Signal.trap('TERM') do
  178. Dyndnsd.logger.info 'Quitting...'
  179. Rack::Handler::WEBrick.shutdown
  180. end
  181. end
  182. private_class_method def self.setup_monitoring(config)
  183. # configure metriks
  184. if config['graphite']
  185. host = config['graphite']['host'] || 'localhost'
  186. port = config['graphite']['port'] || 2003
  187. options = {}
  188. options[:prefix] = config['graphite']['prefix'] if config['graphite']['prefix']
  189. reporter = Metriks::Reporter::Graphite.new(host, port, options)
  190. reporter.start
  191. elsif config['textfile']
  192. file = config['textfile']['file'] || '/tmp/dyndnsd-metrics.prom'
  193. options = {}
  194. options[:prefix] = config['textfile']['prefix'] if config['textfile']['prefix']
  195. reporter = Dyndnsd::TextfileReporter.new(file, options)
  196. reporter.start
  197. else
  198. reporter = Metriks::Reporter::ProcTitle.new
  199. reporter.add 'good', 'sec' do
  200. Metriks.meter('requests.good').mean_rate
  201. end
  202. reporter.add 'nochg', 'sec' do
  203. Metriks.meter('requests.nochg').mean_rate
  204. end
  205. reporter.start
  206. end
  207. end
  208. private_class_method def self.setup_tracing(config)
  209. # configure OpenTracing
  210. if config.dig('tracing', 'jaeger')
  211. require 'jaeger/client'
  212. host = config['tracing']['jaeger']['host'] || '127.0.0.1'
  213. port = config['tracing']['jaeger']['port'] || 6831
  214. service_name = config['tracing']['jaeger']['service_name'] || 'dyndnsd'
  215. OpenTracing.global_tracer = Jaeger::Client.build(
  216. host: host, port: port, service_name: service_name, flush_interval: 1
  217. )
  218. end
  219. # always use SpanManager
  220. OpenTracing.global_tracer = SpanManager::Tracer.new(OpenTracing.global_tracer)
  221. end
  222. private_class_method def self.setup_rack(config)
  223. # configure daemon
  224. db = Database.new(config['db'])
  225. updater = Updater::CommandWithBindZone.new(config['domain'], config['updater']['params']) if config['updater']['name'] == 'command_with_bind_zone'
  226. daemon = Daemon.new(config, db, updater)
  227. # configure rack
  228. app = Rack::Auth::Basic.new(daemon, 'DynDNS', &daemon.method(:authorized?))
  229. if config['responder'] == 'RestStyle'
  230. app = Responder::RestStyle.new(app)
  231. else
  232. app = Responder::DynDNSStyle.new(app)
  233. end
  234. trust_incoming_span = config.dig('tracing', 'trust_incoming_span') || false
  235. app = Rack::Tracer.new(app, trust_incoming_span: trust_incoming_span)
  236. Rack::Handler::WEBrick.run app, Host: config['host'], Port: config['port']
  237. end
  238. end
  239. end