2015-10-13 19:06:37 +02:00
|
|
|
package zone;
|
|
|
|
use v5.14;
|
|
|
|
use Moo;
|
|
|
|
|
|
|
|
use Modern::Perl;
|
|
|
|
|
|
|
|
# TODO all this file is to redesign
|
|
|
|
|
|
|
|
use getiface ':all';
|
|
|
|
use copycat ':all';
|
|
|
|
use fileutil ':all';
|
|
|
|
use configuration ':all';
|
2015-12-21 09:39:50 +01:00
|
|
|
#use Data::Dump qw( dump );
|
2015-10-13 19:06:37 +02:00
|
|
|
|
|
|
|
use zonefile;
|
|
|
|
|
|
|
|
# primary dns interface
|
|
|
|
has dnsi => ( is => 'rw', builder => '_void_arr');
|
|
|
|
|
|
|
|
# dns interface for secondary name servers
|
|
|
|
has dnsisec => ( is => 'rw', builder => '_void');
|
|
|
|
|
|
|
|
has [ qw/tld tmpdir domain primarydnsserver secondarydnsserver slavedzones/ ]
|
|
|
|
=> qw/is ro required 1/;
|
|
|
|
|
|
|
|
sub _void { my $x = ''; \$x; }
|
|
|
|
sub _void_arr { [] }
|
|
|
|
|
|
|
|
sub get_ztmp_file_ {my $s = shift; "$$s{tmpdir}/$$s{domain}" }
|
|
|
|
sub get_ztpl_dir_ {my $s = shift; "$$s{dnsi}{mycfg}{zonedir}" }
|
|
|
|
sub get_ztpl_file_ {
|
|
|
|
my $s = shift;
|
|
|
|
|
|
|
|
# for each TLD
|
|
|
|
for(@{$$s{tld}}) {
|
|
|
|
# if our domain is part of this TLD, get the right template
|
|
|
|
if($$s{domain} =~ $_) {
|
|
|
|
return $s->get_ztpl_dir_() . '/' . $_ . '.tpl';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
die "There is no template for $$s{domain}.";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_dnsserver_interface {
|
|
|
|
my ($self, $dnsserver) = @_;
|
|
|
|
my $cfg = {
|
|
|
|
mycfg => $dnsserver
|
|
|
|
, primarydnsserver => $$self{primarydnsserver}
|
|
|
|
, secondarydnsserver => $$self{secondarydnsserver}
|
|
|
|
, tmpdir => $$self{tmpdir}
|
|
|
|
};
|
|
|
|
|
|
|
|
getiface $$dnsserver{app}, $cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_dns_server_interfaces {
|
|
|
|
my $self = shift;
|
|
|
|
my $primary = $$self{primarydnsserver};
|
|
|
|
my $s = $$self{secondarydnsserver};
|
|
|
|
|
|
|
|
my $prim = $self->get_dnsserver_interface($primary);
|
|
|
|
|
|
|
|
my @sec;
|
|
|
|
for(@{$s}) {
|
|
|
|
push @sec, $self->get_dnsserver_interface($_);
|
|
|
|
}
|
|
|
|
|
|
|
|
($prim, [ @sec ])
|
|
|
|
}
|
|
|
|
|
|
|
|
sub BUILD {
|
|
|
|
my $self = shift;
|
|
|
|
($$self{dnsi}, $$self{dnsisec}) = $self->get_dns_server_interfaces()
|
|
|
|
}
|
|
|
|
|
|
|
|
# change the origin in a zone file template
|
|
|
|
sub mod_orig_template {
|
|
|
|
my ($file, $domain) = @_;
|
|
|
|
say "s/CHANGEMEORIGIN/$domain/ on $file";
|
|
|
|
qx[sed -i "s/CHANGEMEORIGIN/$domain/" $file];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_remote_zf_ {
|
|
|
|
my $self = shift;
|
|
|
|
"$$self{dnsi}{mycfg}{zonedir}/$$self{domain}"
|
|
|
|
}
|
|
|
|
|
|
|
|
sub reload_secondary_dns_servers {
|
|
|
|
my $self = shift;
|
|
|
|
$_->reload_sec($$self{slavedzones}) for(@{$$self{dnsisec}})
|
|
|
|
}
|
|
|
|
|
2015-12-21 09:39:50 +01:00
|
|
|
sub get_zonefile {
|
2015-10-13 19:06:37 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $file = $self->get_remote_zf_();
|
|
|
|
my $dest = $self->get_ztmp_file_();
|
|
|
|
|
2016-01-19 05:47:10 +01:00
|
|
|
my $path = $dest;
|
|
|
|
|
|
|
|
# dest is the filename
|
|
|
|
if($dest =~ "://") {
|
|
|
|
my $fileuri = URI->new($dest);
|
|
|
|
$path = $fileuri->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( -f $path ) {
|
|
|
|
say "FILE $path already exists : do not copy from ns server";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
copycat ($file, $dest);
|
|
|
|
}
|
|
|
|
|
2015-10-13 19:06:37 +02:00
|
|
|
zonefile->new(domain => $$self{domain}, zonefile => $dest);
|
|
|
|
}
|
|
|
|
|
|
|
|
=pod
|
|
|
|
copie du template pour créer une nouvelle zone
|
|
|
|
update du serial
|
|
|
|
ajout de la zone via dnsapp (rndc, knot…)
|
|
|
|
retourne la zone + le nom de la zone
|
|
|
|
=cut
|
|
|
|
|
|
|
|
sub addzone {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $tpl = $self->get_ztpl_file_();
|
|
|
|
my $tmpfile = $self->get_ztmp_file_();
|
|
|
|
|
|
|
|
copycat ($tpl, $tmpfile); # get the template
|
|
|
|
|
|
|
|
# get the file path
|
|
|
|
my $f = URI->new($tmpfile);
|
|
|
|
|
|
|
|
# sed CHANGEMEORIGIN by the real origin
|
|
|
|
mod_orig_template ($f->path, $$self{domain});
|
|
|
|
|
|
|
|
my $zonefile = zonefile->new(zonefile => $f->path
|
|
|
|
, domain => $$self{domain});
|
|
|
|
$zonefile->new_serial(); # update the serial number
|
|
|
|
|
|
|
|
# write the new zone tmpfile to disk
|
2015-12-21 09:39:50 +01:00
|
|
|
write_file $f->path, $zonefile->dump();
|
2015-10-13 19:06:37 +02:00
|
|
|
|
|
|
|
my $file = $self->get_remote_zf_();
|
|
|
|
copycat ($tmpfile, $file); # put the final zone on the server
|
|
|
|
unlink($f->path); # del the temporary file
|
|
|
|
|
|
|
|
# add new zone on the primary ns
|
|
|
|
$self->dnsi->primary_addzone($$self{domain});
|
|
|
|
|
|
|
|
# add new zone on secondary ns
|
|
|
|
$self->reload_secondary_dns_servers()
|
|
|
|
}
|
|
|
|
|
|
|
|
=pod
|
|
|
|
màj du serial
|
|
|
|
push reload de la conf
|
|
|
|
=cut
|
|
|
|
|
|
|
|
sub update {
|
|
|
|
my ($self, $zonefile) = @_;
|
|
|
|
|
|
|
|
# update the serial number
|
|
|
|
$zonefile->new_serial();
|
|
|
|
|
|
|
|
my $tmpfile = $self->get_ztmp_file_();
|
|
|
|
|
|
|
|
# write the new zone tmpfile to disk
|
2015-12-21 09:39:50 +01:00
|
|
|
write_file $tmpfile, $zonefile->dump();
|
2015-10-13 19:06:37 +02:00
|
|
|
|
|
|
|
my $file = $self->get_remote_zf_();
|
|
|
|
copycat ($tmpfile, $file); # put the final zone on the server
|
|
|
|
unlink($tmpfile); # del the temporary file
|
|
|
|
|
|
|
|
$self->dnsi->reload($$self{domain});
|
|
|
|
}
|
|
|
|
|
|
|
|
=pod
|
|
|
|
udpate via the raw content of the zonefile
|
|
|
|
=cut
|
|
|
|
|
|
|
|
sub update_raw {
|
|
|
|
my ($self, $zonetext) = @_;
|
|
|
|
|
|
|
|
my $zonefile;
|
|
|
|
my $file = $self->get_ztmp_file_();
|
|
|
|
|
2015-12-21 09:39:50 +01:00
|
|
|
# write the updated zone file to disk
|
2015-10-13 19:06:37 +02:00
|
|
|
write_file $file, $zonetext;
|
|
|
|
|
2015-12-21 09:39:50 +01:00
|
|
|
eval { $zonefile = zonefile->new(zonefile => $file); };
|
2015-10-13 19:06:37 +02:00
|
|
|
|
|
|
|
if( $@ ) {
|
|
|
|
unlink($file);
|
|
|
|
die 'zone update_raw, zonefile->new error. ' . $@;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink($file);
|
|
|
|
|
2015-12-21 09:39:50 +01:00
|
|
|
$zonefile
|
2015-10-13 19:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub del {
|
|
|
|
my ($self) = @_;
|
|
|
|
$self->dnsi->delzone($$self{domain});
|
|
|
|
$self->dnsi->reconfig();
|
|
|
|
$self->reload_secondary_dns_servers()
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|