From 755a4481748bed352db2bd0d7d86219e224e320c Mon Sep 17 00:00:00 2001 From: cn Date: Sat, 27 Apr 2013 14:07:14 +0200 Subject: [PATCH] Fiat lux --- .gitignore | 3 + .travis.yml | 8 + Gemfile | 3 + LICENSE | 187 ++++++++++++++++++ README.md | 9 + Rakefile | 6 + bin/dyndnsd | 4 + dyndnsd.gemspec | 29 +++ lib/dyndnsd.rb | 127 ++++++++++++ lib/dyndnsd/database.rb | 32 +++ lib/dyndnsd/generator/bind.rb | 29 +++ lib/dyndnsd/responder/rest_style.rb | 19 ++ lib/dyndnsd/updater/command_with_bind_zone.rb | 21 ++ lib/dyndnsd/version.rb | 4 + spec/daemon_spec.rb | 87 ++++++++ spec/spec_helper.rb | 8 + spec/support/dummy_database.rb | 29 +++ spec/support/dummy_updater.rb | 10 + 18 files changed, 615 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 Gemfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Rakefile create mode 100755 bin/dyndnsd create mode 100644 dyndnsd.gemspec create mode 100644 lib/dyndnsd.rb create mode 100644 lib/dyndnsd/database.rb create mode 100644 lib/dyndnsd/generator/bind.rb create mode 100644 lib/dyndnsd/responder/rest_style.rb create mode 100644 lib/dyndnsd/updater/command_with_bind_zone.rb create mode 100644 lib/dyndnsd/version.rb create mode 100644 spec/daemon_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/support/dummy_database.rb create mode 100644 spec/support/dummy_updater.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8c23188 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +*.lock +pkg/* diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..421344b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: ruby + +rvm: + - 2.0.0 + - 1.9.3 + +gemfile: + - Gemfile diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fa75df1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gemspec diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3c4dfe0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,187 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. diff --git a/README.md b/README.md new file mode 100644 index 0000000..06f1c6e --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# dyndnsd.rb + +[![Build Status](https://travis-ci.org/cmur2/dyndnsd.png)](https://travis-ci.org/cmur2/dyndnsd) + +A small, lightweight and extensible DynDNS server written with Ruby and Rack. + +## License + +dyndnsd.rb is licensed under the Apache License, Version 2.0. See LICENSE for more information. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..93cb943 --- /dev/null +++ b/Rakefile @@ -0,0 +1,6 @@ +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) + +task :default => :spec diff --git a/bin/dyndnsd b/bin/dyndnsd new file mode 100755 index 0000000..fa15a52 --- /dev/null +++ b/bin/dyndnsd @@ -0,0 +1,4 @@ + +require 'dyndnsd' + +Dyndnsd::Daemon.run! diff --git a/dyndnsd.gemspec b/dyndnsd.gemspec new file mode 100644 index 0000000..e0ffcd4 --- /dev/null +++ b/dyndnsd.gemspec @@ -0,0 +1,29 @@ + +$:.push File.expand_path("../lib", __FILE__) + +require 'dyndnsd/version' + +Gem::Specification.new do |s| + s.name = 'dyndnsd' + s.version = Dyndnsd::VERSION + s.summary = 'dyndnsd.rb' + s.description = 'A small, lightweight and extensible DynDNS server written with Ruby and Rack.' + s.author = 'Christian Nicolai' + s.email = 'chrnicolai@gmail.com' + s.license = 'Apache License Version 2.0' + s.homepage = 'https://github.com/cmur2/dyndnsd' + + s.files = `git ls-files`.split($/) + s.test_files = s.files.grep(%r{^(test|spec|features)/}) + + s.require_paths = ['lib'] + + s.executables = ['dyndnsd'] + + s.add_runtime_dependency 'rack' + + s.add_development_dependency 'bundler', '~> 1.3' + s.add_development_dependency 'rake' + s.add_development_dependency 'rspec' + s.add_development_dependency 'rack-test' +end diff --git a/lib/dyndnsd.rb b/lib/dyndnsd.rb new file mode 100644 index 0000000..e8720c4 --- /dev/null +++ b/lib/dyndnsd.rb @@ -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 diff --git a/lib/dyndnsd/database.rb b/lib/dyndnsd/database.rb new file mode 100644 index 0000000..e5fa710 --- /dev/null +++ b/lib/dyndnsd/database.rb @@ -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 diff --git a/lib/dyndnsd/generator/bind.rb b/lib/dyndnsd/generator/bind.rb new file mode 100644 index 0000000..a8c1f3f --- /dev/null +++ b/lib/dyndnsd/generator/bind.rb @@ -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 diff --git a/lib/dyndnsd/responder/rest_style.rb b/lib/dyndnsd/responder/rest_style.rb new file mode 100644 index 0000000..04b4928 --- /dev/null +++ b/lib/dyndnsd/responder/rest_style.rb @@ -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 diff --git a/lib/dyndnsd/updater/command_with_bind_zone.rb b/lib/dyndnsd/updater/command_with_bind_zone.rb new file mode 100644 index 0000000..57a33a9 --- /dev/null +++ b/lib/dyndnsd/updater/command_with_bind_zone.rb @@ -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 diff --git a/lib/dyndnsd/version.rb b/lib/dyndnsd/version.rb new file mode 100644 index 0000000..4d6c283 --- /dev/null +++ b/lib/dyndnsd/version.rb @@ -0,0 +1,4 @@ + +module Dyndnsd + VERSION = "0.0.1" +end diff --git a/spec/daemon_spec.rb b/spec/daemon_spec.rb new file mode 100644 index 0000000..5a6920a --- /dev/null +++ b/spec/daemon_spec.rb @@ -0,0 +1,87 @@ +require 'spec_helper' + +describe Dyndnsd::Daemon do + include Rack::Test::Methods + + def app + config = { + 'users' => { + 'test' => { + 'password' => 'secret', + 'hosts' => ['foo.example.org'] + } + } + } + db = Dyndnsd::DummyDatabase.new({}) + updater = Dyndnsd::Updater::Dummy.new + responder = Dyndnsd::Responder::RestStyle.new + app = Dyndnsd::Daemon.new(config, db, updater, responder) + + Rack::Auth::Basic.new(app, "DynDNS") do |user,pass| + (config['users'].has_key? user) and (config['users'][user]['password'] == pass) + end + end + + it 'requires authentication' do + get '/' + last_response.status.should == 401 + end + + it 'only supports GET requests' do + authorize 'test', 'secret' + post '/nic/update' + last_response.status.should == 405 + end + + it 'provides only the /nic/update' do + authorize 'test', 'secret' + get '/other/url' + last_response.status.should == 404 + end + + it 'requires the hostname query parameter' do + authorize 'test', 'secret' + get '/nic/update' + last_response.status.should == 422 + end + + it 'forbids changing hosts a user does not own' do + authorize 'test', 'secret' + get '/nic/update?hostname=notmyhost.example.org' + last_response.status.should == 403 + end + + it 'updates a host on change' do + authorize 'test', 'secret' + + get '/nic/update?hostname=foo.example.org&myip=1.2.3.4' + last_response.should be_ok + + get '/nic/update?hostname=foo.example.org&myip=1.2.3.400' + last_response.should be_ok + last_response.body.should == 'Good' + end + + it 'returns no change' do + authorize 'test', 'secret' + + get '/nic/update?hostname=foo.example.org&myip=1.2.3.4' + last_response.should be_ok + + get '/nic/update?hostname=foo.example.org&myip=1.2.3.4' + last_response.should be_ok + last_response.body.should == 'No change' + end + + it 'forbids invalid hostnames' do + pending + end + + it 'outputs status per hostname' do + pending + end + + it 'supports multiple hostnames in request' do + pending + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..e766e4f --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,8 @@ + +require 'rubygems' +require 'bundler/setup' +require 'rack/test' + +require 'dyndnsd' +require 'support/dummy_database' +require 'support/dummy_updater' diff --git a/spec/support/dummy_database.rb b/spec/support/dummy_database.rb new file mode 100644 index 0000000..82c530f --- /dev/null +++ b/spec/support/dummy_database.rb @@ -0,0 +1,29 @@ + +require 'forwardable' + +module Dyndnsd + class DummyDatabase + extend Forwardable + + def_delegators :@db, :[], :[]=, :each, :has_key? + + def initialize(db_init) + @db_init = db_init + end + + def load + @db = @db_init + @db_hash = @db.hash + end + + def save + @db_hash = @db.hash + end + + def changed? + @db_hash != @db.hash + end + end +end + + diff --git a/spec/support/dummy_updater.rb b/spec/support/dummy_updater.rb new file mode 100644 index 0000000..ca33cc4 --- /dev/null +++ b/spec/support/dummy_updater.rb @@ -0,0 +1,10 @@ + +module Dyndnsd + module Updater + class Dummy + def update(zone) + # nothing + end + end + end +end