first fully functional version for vhosts
This commit is contained in:
19
DDNS.pm
19
DDNS.pm
@ -98,6 +98,25 @@ sub delvhost {
|
||||
$this->cleanup();
|
||||
}
|
||||
|
||||
sub getvhosts {
|
||||
my ($this) = @_;
|
||||
|
||||
my $fh;
|
||||
open($fh, "dig -t AXFR \@127.0.0.1 private.invalid. |")
|
||||
|| die "Can't open dig: $!";
|
||||
|
||||
my @vh;
|
||||
|
||||
while (<$fh>) {
|
||||
if (/^(\S+)._vhosts.private.invalid.\s+\d+\s+IN\s+TXT\s+\"(.+)\"$/) {
|
||||
push (@vh, { zone => $1,
|
||||
master => $2 });
|
||||
}
|
||||
}
|
||||
|
||||
return @vh;
|
||||
}
|
||||
|
||||
sub cleanup {
|
||||
my ($this) = @_;
|
||||
# Merge the .jnl file in with the domain file
|
||||
|
||||
@ -10,6 +10,7 @@ WriteMakefile(
|
||||
'EXE_FILES' => [ 'bin/add-vhost',
|
||||
'bin/del-vhost',
|
||||
'bin/list-vhosts',
|
||||
'bin/sync-master-vhosts',
|
||||
],
|
||||
'AUTHOR' => 'Jorj Bauer <jorj@jorj.org>',
|
||||
);
|
||||
|
||||
@ -6,7 +6,8 @@ use Martnet::DDNS;
|
||||
use Regexp::Common qw/net/;
|
||||
|
||||
my $host = shift || die "No vhost provided";
|
||||
my $master = shift || die "No master DNS IP provided";
|
||||
#my $master = shift || die "No master DNS IP provided";
|
||||
my $master = '74.109.12.14';
|
||||
|
||||
die "Hostname must end in a dot"
|
||||
unless ($host =~ /^[a-zA-Z0-9\.\-\_]+\.$/);
|
||||
|
||||
@ -2,13 +2,11 @@
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Martnet::DDNS;
|
||||
|
||||
my $fh;
|
||||
open($fh, "dig -t AXFR \@127.0.0.1 private.invalid. |")
|
||||
|| die "Can't open dig: $!";
|
||||
my $ddns = Martnet::DDNS->new();
|
||||
|
||||
while (<$fh>) {
|
||||
if (/^(\S+)._vhosts.private.invalid.\s+\d+\s+IN\s+TXT\s+\"(.+)\"$/) {
|
||||
print $1, ". master: ", $2, "\n";
|
||||
}
|
||||
my @vh = $ddns->getvhosts();
|
||||
foreach my $i (sort {$a->{zone} cmp $b->{zone}} @vh) {
|
||||
print $i->{zone}, ". master: ", $i->{master},"\n";
|
||||
}
|
||||
|
||||
107
bin/sync-master-vhosts
Executable file
107
bin/sync-master-vhosts
Executable file
@ -0,0 +1,107 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Martnet::DDNS;
|
||||
use File::Temp qw/tempfile/;
|
||||
|
||||
my $ddns = Martnet::DDNS->new();
|
||||
|
||||
my @vh = $ddns->getvhosts();
|
||||
|
||||
# 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
|
||||
;
|
||||
}
|
||||
2
sync-master-vhosts.cron
Normal file
2
sync-master-vhosts.cron
Normal file
@ -0,0 +1,2 @@
|
||||
MAILTO=jorj@jorj.org
|
||||
* * * * * bind /usr/local/bin/sync-master-vhosts
|
||||
Reference in New Issue
Block a user