added slave sync; bugfix for deletion; added a list-all

This commit is contained in:
2016-02-22 12:42:57 -05:00
parent dd45d2fdda
commit ecf4d0c7da
5 changed files with 95 additions and 8 deletions

20
DDNS.pm
View File

@ -43,10 +43,10 @@ sub _validateOrDie {
} }
sub _lookupOrDie { sub _lookupOrDie {
my ($dom) = @_; my ($dom, $type) = @_;
_validateOrDie($dom); _validateOrDie($dom);
my $fqdn = _fqdn($dom, '_vhosts'); my $fqdn = _fqdn($dom, $type);
my $res = Net::DNS::Resolver->new; my $res = Net::DNS::Resolver->new;
my $query = $res->query($fqdn, "TXT"); my $query = $res->query($fqdn, "TXT");
@ -91,7 +91,9 @@ sub __docmd {
sub _gethosts { sub _gethosts {
my ($this, $type) = @_; my ($this, $type) = @_;
unless (!defined($type)) {
_validateTypeOrDie($type); _validateTypeOrDie($type);
}
my $fh; my $fh;
open($fh, "dig -t AXFR \@127.0.0.1 private.invalid. |") open($fh, "dig -t AXFR \@127.0.0.1 private.invalid. |")
@ -100,10 +102,20 @@ sub _gethosts {
my @vh; my @vh;
while (<$fh>) { while (<$fh>) {
if (/^(\S+).$type.private.invalid.\s+\d+\s+IN\s+TXT\s+\"(.+)\"$/) { if ($type) {
if (/^(\S+)\.$type\.private\.invalid\.\s+\d+\s+IN\s+TXT\s+\"(.+)\"$/) {
push (@vh, { zone => $1, push (@vh, { zone => $1,
type => $type,
master => $2 }); master => $2 });
} }
} else {
# Querying everything
if (/^(\S+)\.(\S+)\.private\.invalid\.\s+\d+\s+IN\s+TXT\s+\"(.+)\"$/) {
push (@vh, { zone => $1,
type => $2,
master => $3 });
}
}
} }
return @vh; return @vh;
@ -121,7 +133,7 @@ sub add {
sub del { sub del {
my ($this, $dom, $type) = @_; my ($this, $dom, $type) = @_;
_lookupOrDie($dom); _lookupOrDie($dom, $type);
my $fqdn = _fqdn($dom, $type); my $fqdn = _fqdn($dom, $type);
$this->__docmd("update delete $fqdn TXT"); $this->__docmd("update delete $fqdn TXT");

View File

@ -17,6 +17,7 @@ WriteMakefile(
'bin/add-custom', 'bin/add-custom',
'bin/del-custom', 'bin/del-custom',
'bin/list-custom', 'bin/list-custom',
'bin/sync-slave',
], ],
'AUTHOR' => 'Jorj Bauer <jorj@jorj.org>', 'AUTHOR' => 'Jorj Bauer <jorj@jorj.org>',
); );

View File

@ -6,7 +6,8 @@ use Martnet::DDNS;
use Regexp::Common qw/net/; use Regexp::Common qw/net/;
my $host = shift || die "No zonename provided"; my $host = shift || die "No zonename provided";
my $master = shift || die "No master DNS IP provided"; my $master = shift;
$master ||= '74.109.12.14';
die "Zonename must end in a dot" die "Zonename must end in a dot"
unless ($host =~ /^[a-zA-Z0-9\.\-\_]+\.$/); unless ($host =~ /^[a-zA-Z0-9\.\-\_]+\.$/);

12
bin/list-all Executable file
View File

@ -0,0 +1,12 @@
#!/usr/bin/perl
use strict;
use warnings;
use Martnet::DDNS;
my $ddns = Martnet::DDNS->new();
my @vh = $ddns->get();
foreach my $i (sort {$a->{zone} cmp $b->{zone}} @vh) {
print $i->{zone}, ". $i->{type} master: ", $i->{master},"\n";
}

61
bin/sync-slave Executable file
View File

@ -0,0 +1,61 @@
#!/usr/bin/perl
use strict;
use warnings;
use Martnet::DDNS;
use File::Temp qw/tempfile/;
my $ddns = Martnet::DDNS->new();
my @vh = $ddns->get();
my @all = parse_slavefile("/etc/bind/martnet.slave.zones.9");
# For each virtual host, see if we've got it already
my $changecount = 0;
foreach my $i (@vh) {
unless (contains_zone($i, @all)) {
# If we find any differences, we rewrite the file
do_rewrite(@vh);
last;
}
}
exit 0;
sub parse_slavefile {
my ($f) = @_;
my @ret;
open(my $fh, $f) || die "Can't open $f: $!";
while (<$fh>) {
if (/^zone\s+\"([^\"]+)\"\s+\{/) {
push ( @ret, { zone => $1 } );
}
}
return @ret;
}
sub do_rewrite {
my (@vh) = @_;
my ($fh, $path) = tempfile();
foreach my $i (sort {$a->{zone} cmp $b->{zone}} @vh) {
print $fh "zone \"$i->{zone}\" { type slave; file \"/var/cache/bind/db.$i->{zone}\"; masters { $i->{master}; } };\n";
}
close $fh;
print "Installing new slave host list\n";
system("install -o bind -g bind $path /etc/bind/martnet.slave.zones.9");
print "Reloading DNS files\n";
system("rndc reload");
}
sub contains_zone {
my ($zone, @zl) = @_;
foreach my $i (@zl) {
return 1
if ($i->{zone} eq $zone);
}
return 0;
}