2017-08-12 12:26:06 +02:00
|
|
|
#!/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;
|
2017-09-14 18:15:02 +02:00
|
|
|
use Capture::Tiny ':all';
|
2017-08-12 12:26:06 +02:00
|
|
|
|
|
|
|
my $yml = YAML::Tiny->read("$Bin/../config.yml");
|
|
|
|
my $tld = $yml->[0]->{"tld"};
|
|
|
|
my $conf = $yml->[0]->{"zonewriter"};
|
|
|
|
|
2017-09-10 00:21:04 +02:00
|
|
|
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 cyberman");
|
|
|
|
$sth->execute;
|
|
|
|
my $cyberman = $sth->fetchrow_hashref;
|
|
|
|
exit unless $cyberman->{"intserial"} > $cyberman->{"lastserial"};
|
|
|
|
|
2017-09-14 18:15:02 +02:00
|
|
|
my $zone;
|
2017-08-12 12:26:06 +02:00
|
|
|
|
|
|
|
# Introduction
|
2017-09-14 18:15:02 +02:00
|
|
|
$zone .= <<"END";
|
2017-08-12 12:26:06 +02:00
|
|
|
; File produced by cyberman. Do not edit!
|
2017-09-14 18:15:02 +02:00
|
|
|
\$TTL 86400
|
|
|
|
\$ORIGIN $tld.
|
|
|
|
|
2017-08-12 12:26:06 +02:00
|
|
|
END
|
|
|
|
|
|
|
|
# Write SOA
|
|
|
|
# Uses mostly hard-coded values for now
|
2017-09-14 18:15:02 +02:00
|
|
|
$zone .= "@ 1D IN SOA $conf->{ns} $conf->{responsible} (\n" . time . "\n";
|
|
|
|
$zone .= <<'END';
|
2017-09-09 19:22:24 +02:00
|
|
|
1800 ; refresh
|
2017-08-12 12:26:06 +02:00
|
|
|
15 ; retry
|
|
|
|
604800 ; expire
|
2017-09-09 19:22:24 +02:00
|
|
|
1h ; nxdomain ttl
|
2017-08-12 12:26:06 +02:00
|
|
|
)
|
|
|
|
|
2017-09-14 18:15:02 +02:00
|
|
|
END
|
2017-08-12 12:26:06 +02:00
|
|
|
|
|
|
|
# Time to get the records
|
2017-09-10 00:21:04 +02:00
|
|
|
$sth = $dbh->prepare("SELECT * FROM record");
|
2017-08-12 12:26:06 +02:00
|
|
|
$sth->execute;
|
|
|
|
|
|
|
|
while (my $r = $sth->fetchrow_hashref) {
|
2017-08-20 00:45:25 +02:00
|
|
|
# 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;
|
2017-08-12 12:26:06 +02:00
|
|
|
|
2017-08-20 00:45:25 +02:00
|
|
|
# domain name
|
|
|
|
if ($r->{"name"} eq '@') {
|
2017-09-14 18:15:02 +02:00
|
|
|
$zone .= $d->{"name"} . " ";
|
2017-08-20 00:45:25 +02:00
|
|
|
} else {
|
2017-09-14 18:15:02 +02:00
|
|
|
$zone .= $r->{"name"} . "." . $d->{"name"} . " ";
|
2017-08-20 00:45:25 +02:00
|
|
|
}
|
2017-08-12 12:26:06 +02:00
|
|
|
|
2017-08-20 00:45:25 +02:00
|
|
|
# record type
|
2017-09-14 18:15:02 +02:00
|
|
|
$zone .= "IN $r->{type} ";
|
2017-08-12 12:26:06 +02:00
|
|
|
|
2017-08-20 00:45:25 +02:00
|
|
|
# value
|
2017-09-14 18:15:02 +02:00
|
|
|
$zone .= "$r->{value}\n";
|
2017-08-12 12:26:06 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 18:15:02 +02:00
|
|
|
if ($conf->{"validate"}) {
|
|
|
|
my $checkzone_exit;
|
|
|
|
capture {
|
|
|
|
open my $checkzone, "| nsd-checkzone $tld -" or die $!;
|
|
|
|
print $checkzone $zone;
|
|
|
|
close $checkzone;
|
|
|
|
$checkzone_exit = $?;
|
|
|
|
};
|
|
|
|
if ($checkzone_exit != 0) {
|
|
|
|
$sth = $dbh->prepare("UPDATE cyberman SET zonecheckstatus=1");
|
|
|
|
$sth->execute;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($conf->{"include"}->{"enabled"}) {
|
|
|
|
$zone .= "\$INCLUDE $conf->{include}->{file}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
open my $out, ">", $conf->{"file"} or die $!;
|
|
|
|
say $out $zone;
|
2017-08-12 12:26:06 +02:00
|
|
|
close $out;
|
2017-09-10 00:21:04 +02:00
|
|
|
|
2017-09-14 18:15:02 +02:00
|
|
|
$sth = $dbh->prepare("UPDATE cyberman SET lastserial=?, zonecheckstatus=0");
|
2017-09-10 00:21:04 +02:00
|
|
|
$sth->bind_param(1, $cyberman->{"intserial"});
|
|
|
|
$sth->execute;
|