From e51f9d89dc6d9726cd4caa4f3ec1bf478f0e849c Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sat, 25 Jan 2014 18:49:58 +0100 Subject: [PATCH 1/2] exemples (ssh, scp) + tout se fait via ssh (prog externalisable) --- app/app.pm | 32 +++++++++++++---- app/zone/edit.pm | 71 +++++++++++++++++++++++++++++++------- app/zone/rndc_interface.pm | 1 - t/config.ini | 2 ++ t/initco.pm | 20 +++++------ t/scp.pl | 16 +++++++++ t/ssh.pl | 26 ++++++++++++++ www/conf/config.ini | 3 ++ www/lib/DNSManager.pm | 25 +++++++------- 9 files changed, 153 insertions(+), 43 deletions(-) create mode 100755 t/scp.pl create mode 100644 t/ssh.pl diff --git a/app/app.pm b/app/app.pm index 2790e7d..a048268 100644 --- a/app/app.pm +++ b/app/app.pm @@ -17,7 +17,7 @@ use Moose; has dbh => ( is => 'rw', builder => '_void'); has dnsi => ( is => 'rw', builder => '_void'); has um => ( is => 'rw', builder => '_void'); -has [ qw/zdir dbname dbhost dbport dbuser dbpass sgbd dnsapp/ ] => qw/is ro required 1/; +has [ qw/zdir dbname dbhost dbport dbuser dbpass sgbd dnsapp sshhost sshuser/ ] => qw/is ro required 1/; sub _void { my $x = ''; \$x; } ### users @@ -91,7 +91,10 @@ sub add_domain { return 0; } - my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir); + my $ze = app::zone::edit->new(zname => $domain + , zdir => $self->zdir + , host => $self->sshhost + , user => $self->sshuser ); $ze->addzone(); } @@ -103,7 +106,10 @@ sub delete_domain { return 0 unless $success; return 0 unless $user->delete_domain($domain); - my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir); + my $ze = app::zone::edit->new(zname => $domain + , zdir => $self->zdir + , host => $self->sshhost + , user => $self->sshuser ); $ze->del(); 1; @@ -111,19 +117,28 @@ sub delete_domain { sub update_domain_raw { my ($self, $login, $zone, $domain) = @_; - my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir); + my $ze = app::zone::edit->new(zname => $domain + , zdir => $self->zdir + , host => $self->sshhost + , user => $self->sshuser ); $ze->update_raw($zone); } sub update_domain { my ($self, $login, $zone, $domain) = @_; - my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir); + my $ze = app::zone::edit->new(zname => $domain + , zdir => $self->zdir + , host => $self->sshhost + , user => $self->sshuser ); $ze->update($zone); } sub get_domain { my ($self, $login, $domain) = @_; - my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir); + my $ze = app::zone::edit->new(zname => $domain + , zdir => $self->zdir + , host => $self->sshhost + , user => $self->sshuser ); $ze->get(); } @@ -146,7 +161,10 @@ sub get_all_users { sub new_tmp { my ($self, $login, $domain) = @_; - my $ze = app::zone::edit->new(zname => $domain, zdir => $self->zdir); + my $ze = app::zone::edit->new(zname => $domain + , zdir => $self->zdir + , host => $self->sshhost + , user => $self->sshuser ); $ze->new_tmp(); } diff --git a/app/zone/edit.pm b/app/zone/edit.pm index dd20c91..0d619d0 100644 --- a/app/zone/edit.pm +++ b/app/zone/edit.pm @@ -2,6 +2,8 @@ use Modern::Perl; use Data::Dump "dump"; use DNS::ZoneParse; use File::Copy; +use Net::SCP; +use Net::SSH2; use v5.14; use lib '../../'; @@ -9,12 +11,15 @@ use app::zone::rndc_interface; package app::zone::edit; use Moose; -has [ qw/zname zdir/ ] => qw/is ro required 1/; +has [ qw/zname zdir host user/ ] => qw/is ro required 1/; sub get { my ($self) = @_; + my $dest = '/tmp/' . $self->zname; my $file = $self->zdir.'/'.$self->zname; - return DNS::ZoneParse->new($file, $self->zname); + + $self->_scp_get($file, $dest); + DNS::ZoneParse->new($dest, $self->zname); } =pod @@ -28,19 +33,24 @@ sub addzone { my ($self) = @_; my $tpl = $self->zdir."/tpl.zone"; - my $file = $self->zdir.'/'.$self->zname; + my $tmpfile = '/tmp/'.$self->zname; - $self->_cp($tpl, $file); + $self->_scp_get($tpl, $tmpfile); # get the template + $self->_sed($tmpfile); # sed CHANGEMEORIGIN by the real origin - my $zonefile = DNS::ZoneParse->new($file, $self->zname); + my $zonefile = DNS::ZoneParse->new($tmpfile, $self->zname); $zonefile->new_serial(); # update the serial number - # write the new zone file to disk + # write the new zone tmpfile to disk my $newzone; - open($newzone, '>', $file) or die "error"; + open($newzone, '>', $tmpfile) or die "error"; print $newzone $zonefile->output(); close $newzone; + my $file = $self->zdir.'/'.$self->zname; + $self->_scp_put($tmpfile, $file); # put the final zone on the server + unlink($tmpfile); # del the temporary file + my $rndc = app::zone::rndc_interface->new(); $rndc->addzone($self->zdir, $self->zname); @@ -58,16 +68,21 @@ sub update { # update the serial number $zonefile->new_serial(); - my $file = $self->zdir.'/'.$self->zname; + my $tmpfile = '/tmp/' . $self->zname; - # write the new zone file to disk + # write the new zone tmpfile to disk my $newzone; - open($newzone, '>', $file) or die "error"; + open($newzone, '>', $tmpfile) or die "error"; print $newzone $zonefile->output(); close $newzone; + my $file = $self->zdir.'/'.$self->zname; + $self->_scp_put($tmpfile, $file); # put the final zone on the server + unlink($tmpfile); # del the temporary file + my $rndc = app::zone::rndc_interface->new(); $rndc->reload($self->zname); + 1; } =pod @@ -96,7 +111,6 @@ sub update_raw { unlink($file); $self->update($zonefile); - 1; } # sera utile plus tard, pour l'interface @@ -106,7 +120,9 @@ sub new_tmp { my $tpl = $self->zdir."/tpl.zone"; my $file = '/tmp/'.$self->zname; - $self->_cp($tpl, $file); + $self->_scp($tpl, $file); + $self->_sed($file); + my $zonefile = DNS::ZoneParse->new($file, $self->zname); $zonefile->new_serial(); # update the serial number @@ -119,9 +135,27 @@ sub _cp { my ($self, $src, $dest) = @_; File::Copy::copy($src, $dest) or die "Copy failed: $! ($src -> $dest)"; +} +sub _scp_put { + my ($self, $src, $dest) = @_; + + my $scp = Net::SCP->new( { host => $self->host, user => $self->user } ); + $scp->put($src, $dest) or die $scp->{errstr}; +} + +sub _scp_get { + my ($self, $src, $dest) = @_; + + my $scp = Net::SCP->new( { host => $self->host, user => $self->user } ); + $scp->get($src, $dest) or die $scp->{errstr}; +} + +sub _sed { + my ($self, $file) = @_; my $orig = $self->zname; - my $cmd = qq[sed -i "s/CHANGEMEORIGIN/$orig/" $dest 2>/dev/null 1>/dev/null]; + my $cmd = qq[sed -i "s/CHANGEMEORIGIN/$orig/" $file 2>/dev/null 1>/dev/null]; + system($cmd); } @@ -130,6 +164,17 @@ sub del { my $rndc = app::zone::rndc_interface->new(); $rndc->delzone($self->zdir, $self->zname); $rndc->reconfig(); + + my $ssh = Net::SSH2->new(); + + $ssh->connect($self->host); + $ssh->auth( username => $self->user); + + my $chan = $ssh->channel(); + my $file = $self->zdir.'/'.$self->zname; + $chan->exec( "rm $file" ); + $ssh->disconnect(); + 1; } 1; diff --git a/app/zone/rndc_interface.pm b/app/zone/rndc_interface.pm index 33d8556..9bc17d6 100644 --- a/app/zone/rndc_interface.pm +++ b/app/zone/rndc_interface.pm @@ -33,7 +33,6 @@ sub reconfig { sub delzone { my ($self, $zdir, $zname) = @_; system("rndc delzone $zname 2>/dev/null 1>/dev/null"); - unlink("$zdir/$zname"); } 1; diff --git a/t/config.ini b/t/config.ini index f4d9df0..d22e6a2 100644 --- a/t/config.ini +++ b/t/config.ini @@ -9,3 +9,5 @@ passwd = HardPass4bla # possible options for dnsserver : bind rndc dnsapp = rndc zones_path = "/srv/named/" +sshhost = pizza +sshuser = karchnu diff --git a/t/initco.pm b/t/initco.pm index 5145803..22586da 100644 --- a/t/initco.pm +++ b/t/initco.pm @@ -3,9 +3,7 @@ use warnings; use v5.14; use autodie; use Modern::Perl; -use DNS::ZoneParse; use Config::Simple; -use Data::Dump qw( dump ); package initco; @@ -15,14 +13,16 @@ sub initco { $cfgfile = defined $cfgfile ? $cfgfile : './config.ini'; my $cfg = new Config::Simple($cfgfile); - my $app = app->new( zdir => $cfg->param('zones_path'), - dbname => $cfg->param('dbname'), - dbhost => $cfg->param('host'), - dbport => $cfg->param('port'), - dbuser => $cfg->param('user'), - dbpass => $cfg->param('passwd'), - sgbd => $cfg->param('sgbd'), - dnsapp => $cfg->param('dnsapp') ); + my $app = app->new( zdir => $cfg->param('zones_path') + , dbname => $cfg->param('dbname') + , dbhost => $cfg->param('host') + , dbport => $cfg->param('port') + , dbuser => $cfg->param('user') + , dbpass => $cfg->param('passwd') + , sgbd => $cfg->param('sgbd') + , sshhost => $cfg->param('sshhost') + , sshuser => $cfg->param('sshuser') + , dnsapp => $cfg->param('dnsapp') ); $app->init(); diff --git a/t/scp.pl b/t/scp.pl new file mode 100755 index 0000000..c5ca5d4 --- /dev/null +++ b/t/scp.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl -w +use strict; +use warnings; +use v5.14; +use autodie; +use Modern::Perl; +use Net::SCP; + +my $hostname = "pizza"; +my $username = "karchnu"; + +my $scp = Net::SCP->new( { host => $hostname, user => $username } ); +$scp->get("/etc/resolv.conf", "kikoo") or die $scp->{errstr}; +$scp->put("kikoo", "lolwat") or die $scp->{errstr}; + +# $scp->put("filename") or die $scp->{errstr}; diff --git a/t/ssh.pl b/t/ssh.pl new file mode 100644 index 0000000..ea5e7bb --- /dev/null +++ b/t/ssh.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl -w +use strict; +use warnings; +use v5.14; +use autodie; +use Modern::Perl; +use Data::Dump qw( dump ); +use DNS::ZoneParse; + +my $hostname = "pizza"; +my $username = "karchnu"; + +use Net::SSH2; + +my $ssh = Net::SSH2->new(); + +$ssh->connect($hostname); +$ssh->auth( username => $username); + +my $chan = $ssh->channel(); +$chan->exec('ls /'); + +my $buf = ''; +say $buf while $chan->read($buf, 1500); + +$ssh->disconnect(); diff --git a/www/conf/config.ini b/www/conf/config.ini index d4fa265..08977bc 100644 --- a/www/conf/config.ini +++ b/www/conf/config.ini @@ -16,3 +16,6 @@ passwd = HardPass4bla # possible options for dnsserver : bind rndc dnsapp = rndc zones_path = "/etc/bind/named/" + +sshhost = localhost +sshuser = karchnu diff --git a/www/lib/DNSManager.pm b/www/lib/DNSManager.pm index fef11e9..90e40e0 100644 --- a/www/lib/DNSManager.pm +++ b/www/lib/DNSManager.pm @@ -23,14 +23,16 @@ our $VERSION = '0.1'; sub initco { my $cfg = new Config::Simple(dirname(__FILE__).'/../conf/config.ini'); - my $app = app->new( zdir => $cfg->param('zones_path'), - dbname => $cfg->param('dbname'), - dbhost => $cfg->param('host'), - dbport => $cfg->param('port'), - dbuser => $cfg->param('user'), - dbpass => $cfg->param('passwd'), - sgbd => $cfg->param('sgbd'), - dnsapp => $cfg->param('dnsapp') ); + my $app = app->new( zdir => $cfg->param('zones_path') + , dbname => $cfg->param('dbname') + , dbhost => $cfg->param('host') + , dbport => $cfg->param('port') + , dbuser => $cfg->param('user') + , dbpass => $cfg->param('passwd') + , sgbd => $cfg->param('sgbd') + , sshhost => $cfg->param('sshhost') + , sshuser => $cfg->param('sshuser') + , dnsapp => $cfg->param('dnsapp') ); $app->init(); @@ -224,11 +226,10 @@ prefix '/domain' => sub { } $zone->new_serial(); - my $cfg = new Config::Simple(dirname(__FILE__).'/../conf/config.ini'); - my $ed = app::zone::edit->new(zdir=>$cfg->param('zones_path') - , zname => param('domain')); + $app->update_domain( session('login') + , $zone + , param('domain')); - $ed->update($zone); redirect '/domain/details/'.param('domain'); } From 86213e94d0086254436813c729103d837ab8148e Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sat, 25 Jan 2014 17:35:42 +0100 Subject: [PATCH 2/2] tous les tests utilisent initco --- t/get_all_domains.pl | 20 ++------------------ t/get_all_users.pl | 20 ++------------------ t/get_domains.pl | 20 ++------------------ 3 files changed, 6 insertions(+), 54 deletions(-) diff --git a/t/get_all_domains.pl b/t/get_all_domains.pl index c105189..fdc4f2f 100755 --- a/t/get_all_domains.pl +++ b/t/get_all_domains.pl @@ -10,30 +10,14 @@ use Data::Dump qw( dump ); use lib '../'; use app::app; - -sub initco { - - my $cfg = new Config::Simple('./config.ini'); - my $app = app->new( zdir => $cfg->param('zones_path'), - dbname => $cfg->param('dbname'), - dbhost => $cfg->param('host'), - dbport => $cfg->param('port'), - dbuser => $cfg->param('user'), - dbpass => $cfg->param('passwd'), - sgbd => $cfg->param('sgbd'), - dnsapp => $cfg->param('dnsapp') ); - - $app->init(); - - return $app; -} +use initco; if( @ARGV != 0 ) { say "usage : ./get_all_domains.pl"; exit 1; } -my $app = initco(); +my $app = initco::initco(); my %domains = $app->get_all_domains(); diff --git a/t/get_all_users.pl b/t/get_all_users.pl index b422c33..7c8d9f4 100755 --- a/t/get_all_users.pl +++ b/t/get_all_users.pl @@ -10,30 +10,14 @@ use Data::Dump qw( dump ); use lib '../'; use app::app; - -sub initco { - - my $cfg = new Config::Simple('./config.ini'); - my $app = app->new( zdir => $cfg->param('zones_path'), - dbname => $cfg->param('dbname'), - dbhost => $cfg->param('host'), - dbport => $cfg->param('port'), - dbuser => $cfg->param('user'), - dbpass => $cfg->param('passwd'), - sgbd => $cfg->param('sgbd'), - dnsapp => $cfg->param('dnsapp') ); - - $app->init(); - - return $app; -} +use initco; if( @ARGV != 0 ) { say "usage : ./get_all_domains.pl"; exit 1; } -my $app = initco(); +my $app = initco::initco(); my %users = $app->get_all_users(); diff --git a/t/get_domains.pl b/t/get_domains.pl index 6cec119..cba903d 100755 --- a/t/get_domains.pl +++ b/t/get_domains.pl @@ -10,30 +10,14 @@ use Data::Dump qw( dump ); use lib '../'; use app::app; - -sub initco { - - my $cfg = new Config::Simple('./config.ini'); - my $app = app->new( zdir => $cfg->param('zones_path'), - dbname => $cfg->param('dbname'), - dbhost => $cfg->param('host'), - dbport => $cfg->param('port'), - dbuser => $cfg->param('user'), - dbpass => $cfg->param('passwd'), - sgbd => $cfg->param('sgbd'), - dnsapp => $cfg->param('dnsapp') ); - - $app->init(); - - return $app; -} +use initco; if( @ARGV < 2) { say "usage : ./auth.pl login mdp"; exit 1; } -my $app = initco(); +my $app = initco::initco(); my ($auth_ok, $user, $isadmin) = $app->auth($ARGV[0], $ARGV[1]); if($auth_ok) {