Files
martnet-ddns/bin/sync-master-vhosts
2016-02-22 12:16:03 -05:00

108 lines
2.5 KiB
Perl
Executable File

#!/usr/bin/perl
use strict;
use warnings;
use Martnet::DDNS;
use File::Temp qw/tempfile/;
my $ddns = Martnet::DDNS->new();
my @vh = $ddns->get('_vhosts');
# For each virtual host, generate a vhost zone file if there isn't one
my $changecount = 0;
foreach my $i (@vh) {
my $zf = "/etc/bind/vhost/db.$i->{zone}";
unless ( -f $zf ) {
print "Generating new zone file for $i->{zone}\n";
open(my $fh, ">", $zf)
|| die "Can't create output file $zf: $!";
create_zonefile($fh, $i);
$changecount++;
}
}
# For each vhost in /etc/bind/vhost, make sure it has an entry in the zone file.
foreach my $i (</etc/bind/vhost/*>) {
my ($zone) = ($i =~ /\/db.(.+)$/);
next unless ($zone);
unless (contains_zone($zone, @vh)) {
print "Unlinking old zone file for $zone\n";
unlink($i);
$changecount++;
}
}
if ($changecount) {
# If we made any changes, then generate the full vhost list
my ($fh, $path) = tempfile();
foreach my $i (sort {$a->{zone} cmp $b->{zone}} @vh) {
print $fh "zone \"$i->{zone}\" { type master; file \"/etc/bind/vhost/db.$i->{zone}\"; };\n";
}
close $fh;
print "Installing new vhost list\n";
system("install -o bind -g bind $path /etc/bind/vhost.zones.9");
print "Reloading DNS files\n";
system("rndc reload");
}
exit 0;
sub contains_zone {
my ($zone, @zl) = @_;
foreach my $i (@zl) {
return 1
if ($i->{zone} eq $zone);
}
return 0;
}
sub create_zonefile {
my ($fh, $i) = @_;
my $zone = $i->{zone} . ".";
my @now = localtime();
my $serial = sprintf("%.4d%.2d%.2d00", $now[5]+1900, $now[4]+1, $now[3]);
print $fh <<EOF
;
; This is an automatically-generated file. Do not edit by hand; it will be overwritten.
; Use instead:
; # add-vhost <hostname>.
; # del-vhost <hostname>.
; # list-vhosts
;
\$TTL 43200
$zone IN SOA ns.martnet.com. root.ns.martnet.com. (
$serial ; Serial
43200 ; Refresh every 12 hours
3600 ; Retry every hour
604800 ; Expire after a week
43200 ) ; Negative Cache TTL 12 hours
; define name servers
$zone IN NS ns.martnet.com
$zone IN NS ns1.martnet.com
$zone IN NS ns2.martnet.com
$zone IN NS ns3.martnet.com
$zone IN NS ns4.martnet.com
$zone IN NS ns5.martnet.com
; define localhost
localhost IN A 127.0.0.1
; define machine names
$zone IN A 74.109.12.4
$zone IN MX 5 $zone
$zone IN MX 10 mx2.martnet.com.
$zone IN MX 15 mx3.martnet.com.
*.$zone IN A 74.109.12.4
*.$zone IN MX 5 $zone
*.$zone IN MX 10 mx2.martnet.com.
*.$zone IN MX 15 mx3.martnet.com.
EOF
;
}