From 98c3c97e3417c2ed0486aa41ead9e874fb2892ef Mon Sep 17 00:00:00 2001 From: Al Beano Date: Sat, 12 Aug 2017 11:26:06 +0100 Subject: [PATCH] Basic zone writing script --- bin/writezone.pl | 73 +++++++++++++++++++++++++++++++++++++++++ config.yml | 11 +++++++ cpanfile | 1 + lib/cyberman/Domains.pm | 2 -- 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100755 bin/writezone.pl diff --git a/bin/writezone.pl b/bin/writezone.pl new file mode 100755 index 0000000..55673fd --- /dev/null +++ b/bin/writezone.pl @@ -0,0 +1,73 @@ +#!/usr/bin/env perl + +# Zone writer for cyberman. +# This won't scale well, but it's a basic way to get domains online +# Tested with NSD but should work with your favourite + +use strict; +use warnings; +use feature 'say'; +use FindBin qw($Bin); +use YAML::Tiny; +use DBI; + +my $yml = YAML::Tiny->read("$Bin/../config.yml"); +my $tld = $yml->[0]->{"tld"}; +my $conf = $yml->[0]->{"zonewriter"}; + +open my $out, ">", $conf->{"file"} or die $!; + +# Introduction +say $out <<'END'; +; File produced by cyberman. Do not edit! +$TTL 86400 +$ORIGIN cyb. +END + +# Write SOA +# Uses mostly hard-coded values for now +say $out "@ 1D IN SOA $conf->{ns} $conf->{responsible} ("; +say $out time; +say $out <<'END'; +3H ; refresh +15 ; retry +604800 ; expire +3h ; nxdomain ttl +) +END + +if ($conf->{"include"}->{"enabled"}) { + say $out "\$INCLUDE $conf->{include}->{file}"; +} + +# Time to get the records +die "Unsupported database!" + unless $yml->[0]->{"plugins"}->{"Database"}->{"driver"} eq "SQLite"; +my $dbfile = "$Bin/../$yml->[0]->{plugins}->{Database}->{dbname}"; +my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", ""); + +my $sth = $dbh->prepare("SELECT * FROM record"); +$sth->execute; + +while (my $r = $sth->fetchrow_hashref) { + # Look up domain + my $dsth = $dbh->prepare("select * from domain where id=?"); + $dsth->bind_param(1, $r->{"domainid"}); + $dsth->execute; + my $d = $dsth->fetchrow_hashref; + + # domain name + if ($r->{"name"} eq '@') { + print $out $d->{"name"}, " "; + } else { + print $out $r->{"name"}, ".", $d->{"name"}, " "; + } + + # record type + print $out "IN $r->{type} "; + + # value + say $out $r->{value}; +} + +close $out; diff --git a/config.yml b/config.yml index 563a5e2..27158d0 100644 --- a/config.yml +++ b/config.yml @@ -17,3 +17,14 @@ plugins: log_queries: 1 tld: 'cyb' + +zonewriter: + # Values used for SOA + ns: 'ns12.opennic.glue' + responsible: 'hostmaster.opennic.cyb.' + file: '/var/nsd/zones/master/cyb' + include: + # Optionally, you can have the zonewriter include another file + # in the zone, which allows you to define some records manually + enabled: true + file: 'human_cyb' diff --git a/cpanfile b/cpanfile index 7e84f7c..02167dc 100644 --- a/cpanfile +++ b/cpanfile @@ -1,6 +1,7 @@ requires "Dancer2" => "0.205001"; requires "YAML" => "0"; +requires "YAML::Tiny" => "0"; requires "URL::Encode::XS" => "0"; requires "CGI::Deurl::XS" => "0"; requires "HTTP::Parser::XS" => "0"; diff --git a/lib/cyberman/Domains.pm b/lib/cyberman/Domains.pm index 8162569..e607a57 100644 --- a/lib/cyberman/Domains.pm +++ b/lib/cyberman/Domains.pm @@ -57,8 +57,6 @@ post '/domains/new' => sub { }; } - # TODO: send domains to nsd - database->quick_insert( "domain", {