3
0

réécriture légère, suppression d'un fichier non utilisé pour le moment

This commit is contained in:
Philippe Pittoli 2014-01-20 22:58:30 +01:00
parent ac41629891
commit 3c78751240
5 changed files with 74 additions and 330 deletions

View File

@ -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* (?<addr> \S+)
\s+ (?<domain> \S+)
\s+ SOA
\s+(?<primary> \S+)
\s+(?<admin> \S+)
\s+ \(
\s*(?<serial> \d+)
\s+(?<refresh> \d+)
\s+(?<retry> \d+)
\s+(?<expire> \d+)
\s+(?<serial> \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* (?<host> \S+)
\s+ (?<domain> \S+)
(?:
\s+ (?<type> MX)
\s+ (?<check> \S+)
\s+ (?<addr> \S+)
|
\s+ (?<type> A | AAAA | CNAME)
\s+ (?<addr> \S+)
|
\s+ TXT
\s+ "(?<text> \\. | [^"]+)"
)
} 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+ (?<domain> IN )
\s+ (?<type> A | AAAA | NS | MX | SOA )
\s+ (?<alias> .* )
} 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;

View File

@ -14,8 +14,7 @@ has [ qw/zname zdir/ ] => qw/is ro required 1/;
sub get { sub get {
my ($self) = @_; my ($self) = @_;
my $file = $self->zdir.'/'.$self->zname; my $file = $self->zdir.'/'.$self->zname;
my $zonefile = DNS::ZoneParse->new($file, $self->zname); return DNS::ZoneParse->new($file, $self->zname);
return $zonefile;
} }
=pod =pod

View File

@ -1,12 +1,10 @@
use lib '../../'; use lib '../../';
use app::zone::bind_interface;
use app::zone::rndc_interface; use app::zone::rndc_interface;
package app::zone::interface; package app::zone::interface;
use Moose; use Moose;
sub get_interface { sub get_interface {
my ($self, $type, $zp) = @_; 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 1, app::zone::rndc_interface->new(zdir => $zp) if $type eq 'rndc';
return 0; return 0;
} }

View File

@ -20,7 +20,6 @@ sub addzone {
$command .= "'{ type master; file \"$zdir/$zname\"; };'" $command .= "'{ type master; file \"$zdir/$zname\"; };'"
} }
$command .= " 2>/dev/null 1>/dev/null"; $command .= " 2>/dev/null 1>/dev/null";
system($command); system($command);

View File

@ -14,19 +14,15 @@ $Storable::Deparse = true;
$Storable::Eval=true; $Storable::Eval=true;
# Include other libs relative to current path # 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; use app::app;
my $success;
our $cfg = new Config::Simple(dirname(__FILE__).'/../conf/config.ini');
our $VERSION = '0.1'; our $VERSION = '0.1';
get '/' => sub { # eventually change place
if( session('login') ) sub initco {
{
# my ($auth_ok, $user, $admin) = my $cfg = new Config::Simple(dirname(__FILE__).'/../conf/config.ini');
# $usermanagement->auth( session('login'), session('password') );
my $app = app->new( zdir => $cfg->param('zones_path'), my $app = app->new( zdir => $cfg->param('zones_path'),
dbname => $cfg->param('dbname'), dbname => $cfg->param('dbname'),
dbhost => $cfg->param('host'), dbhost => $cfg->param('host'),
@ -35,14 +31,22 @@ get '/' => sub {
dbpass => $cfg->param('passwd'), dbpass => $cfg->param('passwd'),
sgbd => $cfg->param('sgbd'), sgbd => $cfg->param('sgbd'),
dnsapp => $cfg->param('dnsapp') ); dnsapp => $cfg->param('dnsapp') );
$app->init(); $app->init();
return $app;
}
get '/' => sub {
if( session('login') )
{
my $app = initco();
$app->get_domains( session('login') ); $app->get_domains( session('login') );
template 'index' => template index => {
{ 'logged' => true, logged => true
'login' => session('login'), , login => session('login')
'admin' => session('admin'), , admin => session('admin')
'domains' => $app->get_domains(session('login')) , domains => $app->get_domains(session('login')) };
};
} }
else else
{ {
@ -58,15 +62,7 @@ post '/login' => sub {
# Check user login and password # Check user login and password
if ( param('login') && param('password') ) if ( param('login') && param('password') )
{ {
my $app = app->new( zdir => $cfg->param('zones_path'), my $app = initco();
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 ($auth_ok, $user, $isadmin) = $app->auth(param('login'),
param('password') ); param('password') );
if( $auth_ok ) if( $auth_ok )
@ -96,76 +92,19 @@ get '/mapage' => sub {
} }
else else
{ {
# my ($auth_ok, $user, $admin) = my $app = initco();
# $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 %domains = (); my %domains = ();
my %zone_properties = (); my %zone_properties = ();
my @d = @{$app->get_domains( session('login') )}; 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') };
} }
}; };
@ -177,24 +116,14 @@ get '/details' => sub {
} }
else else
{ {
my $app = initco();
# 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 ($auth_ok, $user, $isadmin) = $app->auth( param('login') );
my @zones = (); my @zones = ();
my $zone_properties; my $zone_properties;
#say 'dump : ' . dump $user->get_zone( param('domain') ); #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} ) { if( ref($_) eq 'HASH' and exists $_->{addr} ) {
push( @zones, $_ ) when $_->{addr} ne '@'; push( @zones, $_ ) when $_->{addr} ne '@';
@ -202,11 +131,12 @@ get '/details' => sub {
} }
} }
template 'details' =>
{ 'login' => session('login'), template details => {
'domain' => param('domain'), login => session('login')
'zones' => \@zones, , domain => param('domain')
'zone_properties' => $zone_properties }; , zones => \@zones
, zone_properties => $zone_properties };
} }
}; };
@ -219,9 +149,9 @@ any ['get', 'post'] => '/administration' => sub {
} }
else else
{ {
template 'administration' => template administration => {
{ 'login' => session('login'), login => session('login')
'admin' => session('admin') }; , admin => session('admin') };
} }
}; };
@ -239,15 +169,7 @@ get '/domainadd' => sub {
else else
{ {
my $app = app->new( zdir => $cfg->param('zones_path'), my $app = initco();
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();
if( param('domain') ) if( param('domain') )
{ {
@ -263,16 +185,7 @@ get '/domainadd' => sub {
get qr{/domaindel/(.*)} => sub { get qr{/domaindel/(.*)} => sub {
my ($domainToDelete) = splat; my ($domainToDelete) = splat;
my $app = app->new( zdir => $cfg->param('zones_path'), my $app = initco();
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();
$app->delete_domain(session('login'), $domainToDelete); $app->delete_domain(session('login'), $domainToDelete);
redirect '/mapage'; redirect '/mapage';
} }