diff --git a/app/zone/bind_interface.pm b/app/zone/bind_interface.pm deleted file mode 100644 index 120263d..0000000 --- a/app/zone/bind_interface.pm +++ /dev/null @@ -1,165 +0,0 @@ -use Modern::Perl; -use strict; -use warnings; -use Data::Dump "dump"; -use v5.14; -use re '/x'; # very important - -use lib '../../'; -package app::zone::bind_interface; -use Moose; -#use Sudo; -# to know where the zone files are stored / to execute a sudo command -# has [ qw/zone_path sudo_pass/ ] => qw/is ro required 1/; -has [ qw/zone_path/ ] => qw/is ro required 1/; - -sub activate_zone { - my ($self, $domain, $admin_file) = @_; - open(my $file, ">>", $admin_file) - or die("error : impossible to open admin file"); -} - -=pod -zone "karchnu.fr" { - type master; - file "/srv/named/karchnu.fr"; - forwarders { 8.8.8.8; }; - allow-update { key DDNS\_UPDATER ; }; - allow-transfer { any; }; - allow-query { any; }; -}; - -zone "0.0.1.0.0.0.0.0.0.0.0.0.0.0.f.c.ip6.arpa" { - type master; - file "/srv/named/karchnu.fr.rv"; - allow-update { key DDNS\_UPDATER ; }; - allow-transfer { any; }; - allow-query { any; }; -}; - -=cut - -# TODO -sub update { - my ($self) = @_; - - #open(my $process, "service bind9 reload|"); - #say while(<$process>); - #close($process); - #my $su = Sudo->new( - # { - # sudo => '/usr/bin/sudo', - # username => 'root', - # password => $self->sudo_pass, - # program => '/usr/bin/service', - # program_args => 'bind9 reload', - # } - #); - -# my $result = $su->sudo_run(); -# if (exists($result->{error})) { -# return 0; -# } -# -# printf "STDOUT: %s\n",$result->{stdout}; -# printf "STDERR: %s\n",$result->{stderr}; -# printf "return: %s\n",$result->{rc}; -# return 1; -} - -sub parse { - my ($self, $file) = @_; - my $fh; - open($fh, "<", $self->zone_path . $file) or return; - my %zone = $self->parse_zone_file($fh) ; - close($fh); - return %zone; -} - -sub comment { - my $self = shift; - m{ ^ \s* ; \s* ( .+ ) } - and return { comment => $1 }; -} - -sub SOA { - my $self = shift; - m{ ^\s* (? \S+) - \s+ (? \S+) - \s+ SOA - \s+(? \S+) - \s+(? \S+) - \s+ \( - \s*(? \d+) - \s+(? \d+) - \s+(? \d+) - \s+(? \d+) - \s+(? \d+) - \s* - \) - } and return {%+} -} - -sub TTL { - my $self = shift; - m{ ^ \s* \$TTL \s+ (\d+) \s* $ } - and return { TTL => $1 } -} - -# rocoto IN A 192.168.0.180 -# karchnu.fr. IN MX 5 rocoto - -# exemple: -# karchnu.fr. IN MX 5 rocoto -sub entry { - my $self = shift; - m{ ^ - \s* (? \S+) - \s+ (? \S+) - (?: - \s+ (? MX) - \s+ (? \S+) - \s+ (? \S+) - | - \s+ (? A | AAAA | CNAME) - \s+ (? \S+) - | - \s+ TXT - \s+ "(? \\. | [^"]+)" - ) - } and return {%+}; -} - -sub empty_line { - my $self = shift; - /^ \s* $/x -} -# element must be used without args -# () is very important - -sub alias { - my $self = shift; - m{^ - \s* \@ - \s+ (? IN ) - \s+ (? A | AAAA | NS | MX | SOA ) - \s+ (? .* ) - } and return {%+} -} - -sub element () { - my $self = shift; - return if empty_line || comment; - SOA || TTL - || alias - || entry - || die "unparsable $_"; -} - -sub parse_zone_file { - my ($self, $fh) = @_; - map element, <$fh>; -} - - -1; diff --git a/app/zone/edit.pm b/app/zone/edit.pm index 89fd15f..dfe5682 100644 --- a/app/zone/edit.pm +++ b/app/zone/edit.pm @@ -14,8 +14,7 @@ has [ qw/zname zdir/ ] => qw/is ro required 1/; sub get { my ($self) = @_; my $file = $self->zdir.'/'.$self->zname; - my $zonefile = DNS::ZoneParse->new($file, $self->zname); - return $zonefile; + return DNS::ZoneParse->new($file, $self->zname); } =pod diff --git a/app/zone/interface.pm b/app/zone/interface.pm index 4de538b..26bb334 100644 --- a/app/zone/interface.pm +++ b/app/zone/interface.pm @@ -1,12 +1,10 @@ use lib '../../'; -use app::zone::bind_interface; use app::zone::rndc_interface; package app::zone::interface; use Moose; sub get_interface { my ($self, $type, $zp) = @_; - return 1, app::zone::bind_interface->new(zone_path => $zp) if $type eq 'bind'; return 1, app::zone::rndc_interface->new(zdir => $zp) if $type eq 'rndc'; return 0; } diff --git a/app/zone/rndc_interface.pm b/app/zone/rndc_interface.pm index e8e3826..33d8556 100644 --- a/app/zone/rndc_interface.pm +++ b/app/zone/rndc_interface.pm @@ -20,7 +20,6 @@ sub addzone { $command .= "'{ type master; file \"$zdir/$zname\"; };'" } - $command .= " 2>/dev/null 1>/dev/null"; system($command); diff --git a/www/lib/DNSManager.pm b/www/lib/DNSManager.pm index 93c6bd3..10a4c17 100644 --- a/www/lib/DNSManager.pm +++ b/www/lib/DNSManager.pm @@ -14,35 +14,39 @@ $Storable::Deparse = true; $Storable::Eval=true; # Include other libs relative to current path -use Find::Lib '../../'; +use Find::Lib '../../'; # TODO remove it when it won't be usefull anymore use app::app; -my $success; -our $cfg = new Config::Simple(dirname(__FILE__).'/../conf/config.ini'); - our $VERSION = '0.1'; +# eventually change place +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') ); + + $app->init(); + + return $app; +} + get '/' => sub { if( session('login') ) { - # my ($auth_ok, $user, $admin) = - # $usermanagement->auth( session('login'), session('password') ); - 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(); + my $app = initco(); $app->get_domains( session('login') ); - template 'index' => - { 'logged' => true, - 'login' => session('login'), - 'admin' => session('admin'), - 'domains' => $app->get_domains(session('login')) - }; + template index => { + logged => true + , login => session('login') + , admin => session('admin') + , domains => $app->get_domains(session('login')) }; } else { @@ -58,17 +62,9 @@ post '/login' => sub { # Check user login and password if ( param('login') && param('password') ) { - 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(); + my $app = initco(); my ($auth_ok, $user, $isadmin) = $app->auth(param('login'), - param('password') ); + param('password') ); if( $auth_ok ) { @@ -96,118 +92,52 @@ get '/mapage' => sub { } else { - # my ($auth_ok, $user, $admin) = - # $usermanagement->auth( session('login'), session('password') ); - 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(); + my $app = initco(); my %domains = (); my %zone_properties = (); - my @d = @{$app->get_domains( session('login') )}; + template mapage => { + login => session('login') + , domains => $app->get_domains(session('login')) + , zones_domains => \%domains + , zone_properties => \%zone_properties + , admin => session('admin') }; - # loop on domains - #foreach( @{ $app->get_domains( session('login') )} ) - #{ - # my @zones = (); - # # TODO - # foreach my $zone ( $app->get_domain($_)->output() ) - # { - # # avoid some var - # # keep only hash type - # if( ref($zone) eq 'HASH' ) - # { - # if( $zone->{'addr'} ) - # { - # unless( $zone->{'addr'} eq '@' ) - # { - # # normal zone, push it - # push( @zones, $zone ); - # } - # else - # { - # # domain properties - # $zone_properties{$_} = $zone; - # } - # } - # } - # } - # $domains{$_} = [ @zones ]; - #} - - #my @keys = keys(%domains); - #print "key : $_ value : $domains{$_}\n" foreach(@keys); -# foreach my $k ( keys %domains) { -# foreach my $v ( keys @{ $domains{$k} } ) { -# #print "dump : ".dump( $v )."\n"; -# if( UNIVERSAL::isa($domains{$k}[$v], "HASH" ) ) -# { -# print "hash...\n"; -# print "start ------\n"; -# print "$_ => $domains{$k}[$v]{$_}\n" foreach( keys $domains{$k}[$v] ); -# print "end ------\n\n"; -# } -# print "value : " . dump( $domains{$k}[$v] ) . "\n"; -# } -# } - #print 'manual dump : ' . dump( $domains{'karchnu.fr'} )."\n"; - #print 'prop dump : ' . dump( %zone_properties ) . "\n"; - template 'mapage' => - { 'login' => session('login'), - 'domains' => $app->get_domains(session('login')), - 'zones_domains' => \%domains, - 'zone_properties' => \%zone_properties, - 'admin' => session('admin') }; - } + } }; get '/details' => sub { - # check if user is logged & if domain parameter is set - unless( session('login') && param('domain')) - { - redirect '/'; - } - else - { + # check if user is logged & if domain parameter is set + unless( session('login') && param('domain')) + { + redirect '/'; + } + else + { + my $app = initco(); + my ($auth_ok, $user, $isadmin) = $app->auth( param('login') ); + my @zones = (); + my $zone_properties; + #say 'dump : ' . dump $user->get_zone( param('domain') ); - # my ($auth_ok, $user, $admin) = - # $usermanagement->auth( session('login'), session('password') ); - 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(); - my ($auth_ok, $user, $isadmin) = $app->auth( param('login') ); - my @zones = (); - my $zone_properties; - #say 'dump : ' . dump $user->get_zone( param('domain') ); + for( $user->get_zone( param('domain') ) ) + { - for( $user->get_zone( param('domain') ) ) { + if( ref($_) eq 'HASH' and exists $_->{addr} ) { + push( @zones, $_ ) when $_->{addr} ne '@'; + $zone_properties = $_ when $_->{addr} eq '@'; + } - if( ref($_) eq 'HASH' and exists $_->{addr} ) { - push( @zones, $_ ) when $_->{addr} ne '@'; - $zone_properties = $_ when $_->{addr} eq '@'; - } + } - } - template 'details' => - { 'login' => session('login'), - 'domain' => param('domain'), - 'zones' => \@zones, - 'zone_properties' => $zone_properties }; - } + template details => { + login => session('login') + , domain => param('domain') + , zones => \@zones + , zone_properties => $zone_properties }; + } }; @@ -219,9 +149,9 @@ any ['get', 'post'] => '/administration' => sub { } else { - template 'administration' => - { 'login' => session('login'), - 'admin' => session('admin') }; + template administration => { + login => session('login') + , admin => session('admin') }; } }; @@ -231,23 +161,15 @@ any ['post', 'get'] => '/logout' => sub { }; get '/domainadd' => sub { - # check if user is logged & if domain parameter is set - unless( session('login') ) - { - redirect '/'; - } - else - { + # check if user is logged & if domain parameter is set + unless( session('login') ) + { + redirect '/'; + } + else + { - 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(); + my $app = initco(); if( param('domain') ) { @@ -257,22 +179,13 @@ get '/domainadd' => sub { redirect '/mapage'; } - } + } }; get qr{/domaindel/(.*)} => sub { my ($domainToDelete) = splat; - 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(); + my $app = initco(); $app->delete_domain(session('login'), $domainToDelete); redirect '/mapage'; } -