2014-01-20 01:08:03 +01:00
|
|
|
use Modern::Perl;
|
|
|
|
use Data::Dump "dump";
|
|
|
|
use DNS::ZoneParse;
|
|
|
|
use File::Copy;
|
2014-03-04 00:45:33 +01:00
|
|
|
use Net::OpenSSH;
|
2014-01-26 20:46:22 +01:00
|
|
|
use Net::SSH q<sshopen2>;
|
2014-01-20 01:08:03 +01:00
|
|
|
use v5.14;
|
|
|
|
|
|
|
|
use lib '../../';
|
2014-05-08 00:45:12 +02:00
|
|
|
use app::zone::interface;
|
2014-01-20 01:08:03 +01:00
|
|
|
package app::zone::edit;
|
|
|
|
use Moose;
|
|
|
|
|
2014-05-08 00:45:12 +02:00
|
|
|
has [ qw/dnsapp dnsappsec zname zdir host user port/ ] => qw/is ro required 1/;
|
2014-01-20 01:08:03 +01:00
|
|
|
|
|
|
|
sub get {
|
|
|
|
my ($self) = @_;
|
2014-01-25 18:49:58 +01:00
|
|
|
my $dest = '/tmp/' . $self->zname;
|
2014-01-20 01:08:03 +01:00
|
|
|
my $file = $self->zdir.'/'.$self->zname;
|
2014-01-25 18:49:58 +01:00
|
|
|
|
|
|
|
$self->_scp_get($file, $dest);
|
|
|
|
DNS::ZoneParse->new($dest, $self->zname);
|
2014-01-20 01:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
=pod
|
|
|
|
copie du template pour créer une nouvelle zone
|
|
|
|
update du serial
|
2014-05-08 00:45:12 +02:00
|
|
|
ajout de la zone via dnsapp (rndc, knot…)
|
2014-01-20 01:08:03 +01:00
|
|
|
retourne la zone + le nom de la zone
|
|
|
|
=cut
|
|
|
|
|
|
|
|
sub addzone {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $tpl = $self->zdir."/tpl.zone";
|
2014-01-25 18:49:58 +01:00
|
|
|
my $tmpfile = '/tmp/'.$self->zname;
|
2014-01-20 01:08:03 +01:00
|
|
|
|
2014-01-25 18:49:58 +01:00
|
|
|
$self->_scp_get($tpl, $tmpfile); # get the template
|
|
|
|
$self->_sed($tmpfile); # sed CHANGEMEORIGIN by the real origin
|
2014-01-20 01:08:03 +01:00
|
|
|
|
2014-01-25 18:49:58 +01:00
|
|
|
my $zonefile = DNS::ZoneParse->new($tmpfile, $self->zname);
|
2014-01-20 01:08:03 +01:00
|
|
|
$zonefile->new_serial(); # update the serial number
|
|
|
|
|
2014-01-25 18:49:58 +01:00
|
|
|
# write the new zone tmpfile to disk
|
2014-01-20 01:08:03 +01:00
|
|
|
my $newzone;
|
2014-01-25 18:49:58 +01:00
|
|
|
open($newzone, '>', $tmpfile) or die "error";
|
2014-01-20 01:08:03 +01:00
|
|
|
print $newzone $zonefile->output();
|
|
|
|
close $newzone;
|
|
|
|
|
2014-01-25 18:49:58 +01:00
|
|
|
my $file = $self->zdir.'/'.$self->zname;
|
|
|
|
$self->_scp_put($tmpfile, $file); # put the final zone on the server
|
|
|
|
unlink($tmpfile); # del the temporary file
|
|
|
|
|
2014-05-08 00:45:12 +02:00
|
|
|
# add new zone on the primary ns
|
|
|
|
my $prim = app::zone::interface->new()
|
|
|
|
->get_interface($self->dnsapp);
|
|
|
|
$prim->addzone($self->zdir, $self->zname);
|
|
|
|
|
|
|
|
# add new zone on the secondary ns
|
|
|
|
my $sec = app::zone::interface->new()
|
|
|
|
->get_interface($self->dnsappsec);
|
|
|
|
$sec->addzone_sec($self->zdir, $self->zname);
|
2014-01-20 01:08:03 +01:00
|
|
|
|
|
|
|
return $zonefile;
|
|
|
|
}
|
|
|
|
|
|
|
|
=pod
|
|
|
|
màj du serial
|
|
|
|
push reload de la conf
|
|
|
|
=cut
|
|
|
|
|
|
|
|
sub update {
|
|
|
|
my ($self, $zonefile) = @_;
|
|
|
|
|
|
|
|
# update the serial number
|
|
|
|
$zonefile->new_serial();
|
|
|
|
|
2014-01-25 18:49:58 +01:00
|
|
|
my $tmpfile = '/tmp/' . $self->zname;
|
2014-01-20 01:08:03 +01:00
|
|
|
|
2014-01-25 18:49:58 +01:00
|
|
|
# write the new zone tmpfile to disk
|
2014-01-20 01:08:03 +01:00
|
|
|
my $newzone;
|
2014-01-25 18:49:58 +01:00
|
|
|
open($newzone, '>', $tmpfile) or die "error";
|
2014-01-20 01:08:03 +01:00
|
|
|
print $newzone $zonefile->output();
|
|
|
|
close $newzone;
|
|
|
|
|
2014-01-25 18:49:58 +01:00
|
|
|
my $file = $self->zdir.'/'.$self->zname;
|
|
|
|
$self->_scp_put($tmpfile, $file); # put the final zone on the server
|
|
|
|
unlink($tmpfile); # del the temporary file
|
|
|
|
|
2014-05-08 00:45:12 +02:00
|
|
|
my $prim = app::zone::interface->new()
|
|
|
|
->get_interface($self->dnsapp);
|
|
|
|
$prim->reload($self->zname);
|
2014-01-25 18:49:58 +01:00
|
|
|
1;
|
2014-01-20 01:08:03 +01:00
|
|
|
}
|
|
|
|
|
2014-01-21 01:33:17 +01:00
|
|
|
=pod
|
|
|
|
udpate via the raw content of the zonefile
|
|
|
|
=cut
|
|
|
|
|
|
|
|
sub update_raw {
|
|
|
|
my ($self, $zonetext) = @_;
|
|
|
|
|
2014-01-25 14:08:01 +01:00
|
|
|
my $zonefile;
|
2014-01-21 01:33:17 +01:00
|
|
|
my $file = '/tmp/'.$self->zname;
|
|
|
|
|
|
|
|
# write the updated zone file to disk
|
|
|
|
my $newzone;
|
|
|
|
open($newzone, '>', $file) or die "error";
|
|
|
|
print $newzone $zonetext;
|
|
|
|
close $newzone;
|
|
|
|
|
2014-01-25 14:08:01 +01:00
|
|
|
eval { $zonefile = DNS::ZoneParse->new($file, $self->zname); };
|
|
|
|
|
|
|
|
if( $@ ) {
|
|
|
|
unlink($file);
|
|
|
|
0;
|
|
|
|
}
|
|
|
|
|
2014-01-21 01:33:17 +01:00
|
|
|
unlink($file);
|
|
|
|
|
|
|
|
$self->update($zonefile);
|
|
|
|
}
|
|
|
|
|
2014-01-20 01:08:03 +01:00
|
|
|
# sera utile plus tard, pour l'interface
|
|
|
|
sub new_tmp {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $tpl = $self->zdir."/tpl.zone";
|
|
|
|
my $file = '/tmp/'.$self->zname;
|
|
|
|
|
2014-01-25 18:49:58 +01:00
|
|
|
$self->_scp($tpl, $file);
|
|
|
|
$self->_sed($file);
|
|
|
|
|
2014-01-20 01:08:03 +01:00
|
|
|
my $zonefile = DNS::ZoneParse->new($file, $self->zname);
|
|
|
|
$zonefile->new_serial(); # update the serial number
|
|
|
|
|
|
|
|
unlink($file);
|
|
|
|
|
|
|
|
return $zonefile;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _cp {
|
|
|
|
my ($self, $src, $dest) = @_;
|
|
|
|
|
|
|
|
File::Copy::copy($src, $dest) or die "Copy failed: $! ($src -> $dest)";
|
2014-01-25 18:49:58 +01:00
|
|
|
}
|
2014-01-20 01:08:03 +01:00
|
|
|
|
2014-01-25 18:49:58 +01:00
|
|
|
sub _scp_put {
|
|
|
|
my ($self, $src, $dest) = @_;
|
|
|
|
|
2014-03-04 00:45:33 +01:00
|
|
|
my $co = $self->user . '@' . $self->host . ':' . $self->port;
|
|
|
|
my $ssh = Net::OpenSSH->new($co);
|
|
|
|
$ssh->scp_put($src, $dest) or die "scp failed: " . $ssh->error;
|
2014-01-25 18:49:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _scp_get {
|
|
|
|
my ($self, $src, $dest) = @_;
|
|
|
|
|
2014-03-04 00:45:33 +01:00
|
|
|
my $co = $self->user . '@' . $self->host . ':' . $self->port;
|
|
|
|
my $ssh = Net::OpenSSH->new($co);
|
|
|
|
$ssh->scp_get($src, $dest) or die "scp failed: " . $ssh->error;
|
2014-01-25 18:49:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _sed {
|
|
|
|
my ($self, $file) = @_;
|
2014-01-20 01:08:03 +01:00
|
|
|
my $orig = $self->zname;
|
2014-01-25 18:49:58 +01:00
|
|
|
my $cmd = qq[sed -i "s/CHANGEMEORIGIN/$orig/" $file 2>/dev/null 1>/dev/null];
|
|
|
|
|
2014-01-20 01:08:03 +01:00
|
|
|
system($cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub del {
|
|
|
|
my ($self) = @_;
|
2014-05-08 00:45:12 +02:00
|
|
|
my $prim = app::zone::interface->new()
|
|
|
|
->get_interface($self->dnsapp);
|
|
|
|
$prim->delzone($self->zdir, $self->zname);
|
|
|
|
$prim->reconfig();
|
|
|
|
|
|
|
|
my $sec = app::zone::interface->new()
|
|
|
|
->get_interface($self->dnsappsec);
|
|
|
|
$sec->delzone($self->zdir, $self->zname);
|
|
|
|
$sec->reload($self->zdir, $self->zname);
|
2014-01-25 18:49:58 +01:00
|
|
|
|
2014-01-26 20:46:22 +01:00
|
|
|
my $file = $self->zdir.'/'.$self->zname;
|
|
|
|
my $host = $self->host;
|
|
|
|
my $user = $self->user;
|
|
|
|
my $cmd = "rm $file";
|
|
|
|
|
2014-01-26 20:53:21 +01:00
|
|
|
Net::SSH::sshopen2("$user\@$host", *READER, *WRITER, "$cmd") || die "ssh: $!";
|
2014-01-26 20:46:22 +01:00
|
|
|
|
|
|
|
close(READER);
|
|
|
|
close(WRITER);
|
2014-01-25 18:49:58 +01:00
|
|
|
|
|
|
|
1;
|
2014-01-20 01:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|