2015-10-13 19:06:37 +02:00
|
|
|
package rt::domain;
|
|
|
|
|
|
|
|
use v5.14;
|
|
|
|
use configuration ':all';
|
|
|
|
use encryption ':all';
|
|
|
|
use util ':all';
|
|
|
|
use app;
|
|
|
|
use utf8;
|
|
|
|
use Dancer ':syntax';
|
|
|
|
use Data::Dump qw( dump );
|
2015-12-22 17:50:26 +01:00
|
|
|
use Data::Validate::IP qw(is_ipv4 is_ipv6);
|
2015-12-28 01:29:24 +01:00
|
|
|
use MIME::Base64 qw(encode_base64 decode_base64);
|
2015-10-13 19:06:37 +02:00
|
|
|
|
|
|
|
use Exporter 'import';
|
|
|
|
# what we want to export eventually
|
|
|
|
our @EXPORT_OK = qw/
|
|
|
|
rt_dom_cli_mod_entry
|
2015-12-22 22:31:14 +01:00
|
|
|
rt_dom_cli_autoupdate
|
2015-10-13 19:06:37 +02:00
|
|
|
rt_dom_mod_entry
|
|
|
|
rt_dom_del_entry
|
|
|
|
rt_dom_del
|
|
|
|
rt_dom_add
|
|
|
|
rt_dom_details
|
2015-12-22 17:50:26 +01:00
|
|
|
rt_dom_add_entry
|
2015-10-13 19:06:37 +02:00
|
|
|
rt_dom_updateraw
|
|
|
|
/;
|
|
|
|
|
|
|
|
# bundle of exports (tags)
|
|
|
|
our %EXPORT_TAGS = ( all => [qw/
|
|
|
|
rt_dom_cli_mod_entry
|
2015-12-22 22:31:14 +01:00
|
|
|
rt_dom_cli_autoupdate
|
2015-10-13 19:06:37 +02:00
|
|
|
rt_dom_mod_entry
|
|
|
|
rt_dom_del_entry
|
|
|
|
rt_dom_del
|
|
|
|
rt_dom_add
|
|
|
|
rt_dom_details
|
2015-12-22 17:50:26 +01:00
|
|
|
rt_dom_add_entry
|
2015-10-13 19:06:37 +02:00
|
|
|
rt_dom_updateraw
|
|
|
|
/] );
|
|
|
|
|
2015-12-22 22:31:14 +01:00
|
|
|
sub rt_dom_cli_autoupdate {
|
|
|
|
my ($session, $param, $request) = @_;
|
|
|
|
my $res;
|
|
|
|
|
|
|
|
my @missingitems;
|
|
|
|
my @items = qw/login pass domain name type rdata/;
|
|
|
|
|
|
|
|
for(@items) {
|
|
|
|
push @missingitems, $_ unless($$param{$_});
|
|
|
|
}
|
|
|
|
|
|
|
|
if(@missingitems != 0) {
|
|
|
|
say "Il manque : " . join ', ', @missingitems;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(@items) {
|
|
|
|
say "::::::::: $_ : $$param{$_}" if $$param{$_};
|
|
|
|
}
|
|
|
|
|
|
|
|
if(! is_ipv4($$param{rdata}) && ! is_ipv6($$param{rdata})) {
|
|
|
|
say "Attention, ceci n'est pas une adresse IP : $$param{rdata}.";
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
eval {
|
|
|
|
my $pass = encrypt($$param{pass});
|
|
|
|
my $app = app->new(get_cfg());
|
|
|
|
|
2015-12-28 01:29:24 +01:00
|
|
|
my $user;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
$user = $app->auth($$param{login}, $pass);
|
|
|
|
};
|
|
|
|
|
|
|
|
# if the mdp is in base64
|
|
|
|
# useful for cli and http GET messages
|
|
|
|
if( $@ ) {
|
|
|
|
my $passb64 = decode_base64($$param{pass});
|
|
|
|
$pass = encrypt($passb64);
|
|
|
|
$user = $app->auth($$param{login}, $pass);
|
|
|
|
}
|
2015-12-22 22:31:14 +01:00
|
|
|
|
|
|
|
unless ( $user && ( $$user{admin} ||
|
|
|
|
$app->is_owning_domain($$user{login}, $$param{domain}))) {
|
|
|
|
$app->disconnect();
|
|
|
|
say q{Donnée privée, petit coquin. ;) };
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $zone = $app->get_zone( $$param{domain} );
|
|
|
|
my $zf = $zone->get_zonefile();
|
|
|
|
|
|
|
|
my $name = $$param{name};
|
|
|
|
|
2015-12-28 16:08:52 +01:00
|
|
|
$name =~ s/@/$$param{domain}./;
|
|
|
|
|
2015-12-22 22:31:14 +01:00
|
|
|
if($name =~ /$$param{domain}$/) {
|
|
|
|
$name .= '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
if($name !~ /\.$/) {
|
|
|
|
$name .= ".$$param{domain}."
|
|
|
|
}
|
|
|
|
|
|
|
|
my $rr_list = $zf->rr_search($name, $$param{type});
|
|
|
|
my $rr;
|
|
|
|
if(@$rr_list) {
|
|
|
|
$rr = pop @$rr_list;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
say "Pas d'entrée au nom $name de type $$param{type} trouvée.";
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $str_old = "$$rr{name} $$rr{ttl} $$rr{type} $$rr{rdata}";
|
|
|
|
my $str_new = "$$rr{name} $$rr{ttl} $$rr{type} $$param{rdata}";
|
|
|
|
|
|
|
|
say "old : $str_old";
|
|
|
|
say "new : $str_new";
|
2016-01-19 05:31:05 +01:00
|
|
|
if($$rr{rdata} eq $$param{rdata}) {
|
|
|
|
say "SAME";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$zf->rr_mod($str_old, $str_new);
|
|
|
|
$zone->update( $zf );
|
|
|
|
}
|
2015-12-22 22:31:14 +01:00
|
|
|
|
|
|
|
$app->disconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
say "Problème : $@";
|
|
|
|
}
|
|
|
|
|
|
|
|
$res
|
|
|
|
}
|
|
|
|
|
2015-10-13 19:06:37 +02:00
|
|
|
sub rt_dom_cli_mod_entry {
|
|
|
|
my ($session, $param, $request) = @_;
|
|
|
|
my $res;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
my $pass = encrypt($$param{pass});
|
|
|
|
my $app = app->new(get_cfg());
|
|
|
|
|
|
|
|
my $user = $app->auth($$session{login}, $pass);
|
|
|
|
|
|
|
|
unless ( $user && ( $$user{admin} ||
|
|
|
|
$app->is_owning_domain($$user{login}, $$param{domain}))) {
|
|
|
|
$app->disconnect();
|
|
|
|
$$res{deferred}{errmsg} = q{Donnée privée, petit coquin. ;) };
|
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2015-12-21 09:39:50 +01:00
|
|
|
my $zone = $app->get_zone( $$param{domain} );
|
|
|
|
my $zf = $zone->get_zonefile();
|
|
|
|
$zf->rr_mod(
|
|
|
|
"$$param{name} $$param{ttl} $$param{type} $$param{rdata}"
|
|
|
|
, "$$param{name} $$param{ttl} $$param{type} $$param{ip}"
|
|
|
|
);
|
|
|
|
$zone->update( $zf );
|
2015-10-13 19:06:37 +02:00
|
|
|
|
|
|
|
$app->disconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
$res
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rt_dom_mod_entry {
|
|
|
|
my ($session, $param, $request) = @_;
|
|
|
|
my $res;
|
|
|
|
|
|
|
|
$$res{route} = '/domain/details/'. $$param{domain};
|
|
|
|
|
|
|
|
# check if user is logged
|
|
|
|
unless( $$session{login}) {
|
|
|
|
$$res{deferred}{errmsg} = q{Vous n'êtes pas enregistré. };
|
|
|
|
$$res{sessiondestroy} = 1;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @missingitems;
|
2015-12-22 17:50:26 +01:00
|
|
|
my @items = qw/domain type
|
|
|
|
oldname oldrdata oldttl
|
|
|
|
newname newrdata newttl/;
|
2015-10-13 19:06:37 +02:00
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
if($$param{type} && $$param{type} eq 'MX') {
|
|
|
|
push @items, qw/oldpriority newpriority/;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($$param{type} && $$param{type} eq 'SRV') {
|
|
|
|
push @items, qw/
|
|
|
|
oldpriority oldweight oldport
|
|
|
|
newpriority newweight newport/;
|
2015-10-13 19:06:37 +02:00
|
|
|
}
|
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
for(@items) {
|
|
|
|
push @missingitems, $_ unless($$param{$_});
|
2015-10-13 19:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(@missingitems != 0) {
|
|
|
|
$$res{deferred}{errmsg} = "Il manque : " . join ', ', @missingitems;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
for(@items) {
|
|
|
|
say "::::::::: $_ : $$param{$_}" if $$param{$_};
|
2015-10-13 19:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
eval {
|
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
unless( $$param{domain} ) {
|
|
|
|
$$res{deferred}{errmsg} = q<Domaine non renseigné.>;
|
|
|
|
$$res{route} = ($$request{referer}) ? $$request{referer} : '/';
|
2015-10-13 19:06:37 +02:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2015-12-28 16:08:52 +01:00
|
|
|
my $oldname = $$param{oldname};
|
|
|
|
my $newname = $$param{newname};
|
|
|
|
my $oldrdata = $$param{oldrdata};
|
|
|
|
my $newrdata = $$param{newrdata};
|
2015-12-22 17:50:26 +01:00
|
|
|
|
2015-12-28 16:08:52 +01:00
|
|
|
$oldname =~ s/@/$$param{domain}./g;
|
|
|
|
$newname =~ s/@/$$param{domain}./g;
|
|
|
|
|
|
|
|
if ($$param{type} eq 'A' && ! is_ipv4($newrdata)) {
|
2015-12-22 17:50:26 +01:00
|
|
|
$$res{deferred}{errmsg} =
|
|
|
|
"Il faut une adresse IPv4 pour un enregistrement de type A."
|
2015-12-28 16:08:52 +01:00
|
|
|
. " Ceci n'est pas une adresse IPv4 : $newrdata";
|
2015-10-13 19:06:37 +02:00
|
|
|
$$res{route} = ($$request{referer}) ? $$request{referer} : '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2015-12-28 16:08:52 +01:00
|
|
|
if ($$param{type} eq 'AAAA' && ! is_ipv6($newrdata)) {
|
2015-12-22 17:50:26 +01:00
|
|
|
$$res{deferred}{errmsg} =
|
|
|
|
"Il faut une adresse IPv6 pour un enregistrement de type AAAA."
|
2015-12-28 16:08:52 +01:00
|
|
|
. " Ceci n'est pas une adresse IPv6 : $newrdata";
|
2015-12-22 17:50:26 +01:00
|
|
|
$$res{route} = ($$request{referer}) ? $$request{referer} : '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2015-12-28 16:08:52 +01:00
|
|
|
# si le type est A, AAAA, SRV, TXT, CNAME, MX, NS
|
|
|
|
# le name doit être un domaine
|
|
|
|
# si ce domaine n'est pas absolu, rajouter ".domain."
|
|
|
|
if($$param{type} =~ /^(A|AAAA|SRV|TXT|CNAME|MX|NS)$/) {
|
|
|
|
$newname .= ".$$param{domain}." if($newname !~ /\.$/);
|
|
|
|
$oldname .= ".$$param{domain}." if($oldname !~ /\.$/);
|
|
|
|
}
|
|
|
|
|
|
|
|
# si le type est CNAME, MX, NS ou PTR
|
|
|
|
# le rdata doit être un domaine
|
|
|
|
# si ce domaine n'est pas absolu, rajouter ".domain."
|
|
|
|
if($$param{type} =~ /^(CNAME|MX|NS|PTR)$/) {
|
|
|
|
$oldrdata =~ s/@/$$param{domain}./;
|
|
|
|
$newrdata =~ s/@/$$param{domain}./;
|
|
|
|
$oldrdata .= ".$$param{domain}." if($oldrdata !~ /\.$/);
|
|
|
|
$newrdata .= ".$$param{domain}." if($newrdata !~ /\.$/);
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
if ($$param{type} =~ /^(CNAME|MX|NS|PTR|SRV)$/i
|
2015-12-28 16:08:52 +01:00
|
|
|
&& ! is_domain_name ($newrdata))
|
2015-12-22 17:50:26 +01:00
|
|
|
{
|
|
|
|
$$res{deferred}{errmsg} =
|
2015-12-22 22:31:14 +01:00
|
|
|
"Une entrée $$param{type} doit avoir un nom de domaine "
|
2015-12-28 16:08:52 +01:00
|
|
|
. "(pas une URL, pas de http://) : '$newrdata' n'est pas correct.";
|
2015-12-22 17:50:26 +01:00
|
|
|
$$res{route} = ($$request{referer}) ? $$request{referer} : '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2015-12-28 16:08:52 +01:00
|
|
|
my $str_old = "$oldname $$param{oldttl} $$param{type} ";
|
|
|
|
my $str_new = "$newname $$param{newttl} $$param{type} ";
|
2015-12-22 17:50:26 +01:00
|
|
|
|
|
|
|
if($$param{type} eq "MX") {
|
2015-12-28 16:08:52 +01:00
|
|
|
$str_old .= "$$param{oldpriority} $oldrdata";
|
|
|
|
$str_new .= "$$param{newpriority} $newrdata";
|
2015-12-21 09:39:50 +01:00
|
|
|
}
|
2015-12-22 17:50:26 +01:00
|
|
|
elsif ($$param{type} eq "SRV") {
|
|
|
|
$str_old .= "$$param{oldpriority} $$param{oldweight} "
|
2015-12-28 16:08:52 +01:00
|
|
|
."$$param{oldport} $oldrdata";
|
2015-12-22 17:50:26 +01:00
|
|
|
$str_new .= "$$param{newpriority} $$param{newweight} "
|
2015-12-28 16:08:52 +01:00
|
|
|
."$$param{newport} $newrdata";
|
2015-12-22 17:50:26 +01:00
|
|
|
}
|
2015-12-21 09:39:50 +01:00
|
|
|
else {
|
2015-12-28 16:08:52 +01:00
|
|
|
$str_old .= "$oldrdata";
|
|
|
|
$str_new .= "$newrdata";
|
2015-12-21 09:39:50 +01:00
|
|
|
}
|
|
|
|
|
2015-12-28 16:08:52 +01:00
|
|
|
say "::: ___ str_old : $str_old";
|
|
|
|
say "::: ___ str_new : $str_new";
|
2015-12-22 17:50:26 +01:00
|
|
|
|
|
|
|
# Do the modification of the entry
|
|
|
|
|
|
|
|
my $app = app->new(get_cfg());
|
|
|
|
my $user = $app->auth($$session{login}, $$session{passwd});
|
|
|
|
|
|
|
|
unless ( $user && ( $$user{admin} ||
|
|
|
|
$app->is_owning_domain($$user{login}, $$param{domain}))) {
|
|
|
|
$app->disconnect();
|
|
|
|
$$res{deferred}{errmsg} = q{Donnée privée, petit coquin. ;) };
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $zone = $app->get_zone( $$param{domain} );
|
|
|
|
my $zf = $zone->get_zonefile();
|
2015-12-21 16:14:45 +01:00
|
|
|
|
2015-12-21 09:39:50 +01:00
|
|
|
$zf->rr_mod( $str_old, $str_new);
|
|
|
|
$zone->update( $zf );
|
|
|
|
|
2015-10-13 19:06:37 +02:00
|
|
|
$app->disconnect();
|
|
|
|
};
|
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
if($@) {
|
|
|
|
$$res{deferred}{errmsg} = q{Modification impossible. } . $@;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2015-10-13 19:06:37 +02:00
|
|
|
$res
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rt_dom_del_entry {
|
|
|
|
my ($session, $param, $request) = @_;
|
|
|
|
my $res;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
# Load :domain and search for corresponding data
|
|
|
|
my $app = app->new(get_cfg());
|
|
|
|
|
|
|
|
my $user = $app->auth($$session{login}, $$session{passwd});
|
|
|
|
|
|
|
|
unless ( $user && ( $$user{admin} ||
|
|
|
|
$app->is_owning_domain($$user{login}, $$param{domain}))) {
|
|
|
|
$app->disconnect();
|
|
|
|
$$res{deferred}{errmsg} = q{Donnée privée, petit coquin. ;) };
|
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
my @missingitems;
|
|
|
|
my @items = qw/domain name ttl type rdata/;
|
|
|
|
|
|
|
|
if ($$param{type} && $$param{type} eq 'SRV') {
|
|
|
|
push @items, qw/priority weight port/;
|
|
|
|
}
|
|
|
|
elsif ($$param{type} && $$param{type} eq 'MX') {
|
|
|
|
push @items, qw/priority/;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(@items) {
|
|
|
|
push @missingitems, $_ unless($$param{$_});
|
|
|
|
}
|
|
|
|
|
|
|
|
for(@items) {
|
|
|
|
say "::::::::: $_ : $$param{$_}" if $$param{$_};
|
|
|
|
}
|
|
|
|
|
|
|
|
if(@missingitems != 0) {
|
|
|
|
$$res{deferred}{errmsg} = "Il manque : " . join ', ', @missingitems;
|
2015-10-13 19:06:37 +02:00
|
|
|
$$res{route} = ($$request{referer}) ? $$request{referer} : '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2015-12-28 16:08:52 +01:00
|
|
|
my $rdata = $$param{rdata};
|
|
|
|
my $name = $$param{name};
|
|
|
|
|
|
|
|
$name =~ s/@/$$param{domain}./;
|
|
|
|
|
|
|
|
# si le type est A, AAAA, SRV, TXT, CNAME, MX, NS
|
|
|
|
# le name doit être un domaine
|
|
|
|
# si ce domaine n'est pas absolu, rajouter ".domain."
|
|
|
|
if($$param{type} =~ /^(A|AAAA|SRV|TXT|CNAME|MX|NS)$/ && $name !~ /\.$/){
|
|
|
|
$name .= ".$$param{domain}.";
|
|
|
|
}
|
|
|
|
|
|
|
|
# si le type est CNAME, MX, NS ou PTR
|
|
|
|
# le rdata doit être un domaine
|
|
|
|
# si ce domaine n'est pas absolu, rajouter ".domain."
|
2015-12-28 17:34:45 +01:00
|
|
|
if($$param{type} =~ /^(CNAME|SRV|MX|NS|PTR)$/) {
|
2015-12-28 16:08:52 +01:00
|
|
|
$rdata =~ s/@/$$param{domain}./;
|
|
|
|
$rdata .= ".$$param{domain}." if $rdata !~ /\.$/;
|
|
|
|
}
|
|
|
|
|
2015-12-21 09:39:50 +01:00
|
|
|
my $zone = $app->get_zone( $$param{domain} );
|
|
|
|
my $zf = $zone->get_zonefile();
|
2015-12-22 17:50:26 +01:00
|
|
|
|
2015-12-28 16:08:52 +01:00
|
|
|
my $str_del = "$name $$param{ttl} $$param{type} ";
|
2015-12-22 17:50:26 +01:00
|
|
|
|
|
|
|
if( $$param{type} eq 'SRV') {
|
|
|
|
$str_del .=
|
2015-12-28 16:08:52 +01:00
|
|
|
"$$param{priority} $$param{weight} $$param{port} $rdata";
|
2015-12-22 17:50:26 +01:00
|
|
|
}
|
|
|
|
elsif ($$param{type} eq 'MX') {
|
2015-12-28 16:08:52 +01:00
|
|
|
$str_del .= "$$param{priority} $rdata";
|
2015-12-22 17:50:26 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-12-28 16:08:52 +01:00
|
|
|
$str_del .= "$rdata";
|
2015-12-22 17:50:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$zf->rr_del_raw( $str_del );
|
2015-12-21 09:39:50 +01:00
|
|
|
$zone->update( $zf );
|
|
|
|
|
2015-10-13 19:06:37 +02:00
|
|
|
$app->disconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
$$res{route} = '/domain/details/'. $$param{domain};
|
|
|
|
|
|
|
|
$res
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rt_dom_del {
|
|
|
|
my ($session, $param, $request) = @_;
|
|
|
|
my $res;
|
|
|
|
|
|
|
|
unless( $$param{domain} ) {
|
|
|
|
$$res{deferred}{errmsg} = q<Domaine non renseigné.>;
|
|
|
|
$$res{route} = ($$request{referer}) ? $$request{referer} : '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! is_domain_name($$param{domain})) {
|
|
|
|
$$res{deferred}{errmsg} = q<Domaine non conforme.>;
|
|
|
|
$$res{route} = ($$request{referer}) ? $$request{referer} : '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
eval {
|
|
|
|
my $app = app->new(get_cfg());
|
|
|
|
my $user = $app->auth($$session{login}, $$session{passwd});
|
|
|
|
|
|
|
|
unless ( $user && ( $$user{admin} ||
|
|
|
|
$app->is_owning_domain($$user{login}, $$param{domain}))) {
|
|
|
|
$app->disconnect();
|
|
|
|
$$res{deferred}{errmsg} = q{Donnée privée, petit coquin. ;) };
|
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
$app->delete_domain($$param{domain});
|
|
|
|
$app->disconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
if($@) {
|
|
|
|
$$res{deferred}{errmsg} = q{Impossible de supprimer le domaine. } . $@;
|
|
|
|
$$res{route} = ($$request{referer}) ? $$request{referer} : '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $$request{referer} =~ "/domain/details" ) {
|
|
|
|
$$res{route} = '/user/home';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$$res{route} = $$request{referer};
|
|
|
|
}
|
|
|
|
|
|
|
|
$res
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rt_dom_add {
|
|
|
|
my ($session, $param) = @_;
|
|
|
|
my $res;
|
|
|
|
|
|
|
|
$$res{route} = '/user/home';
|
|
|
|
|
|
|
|
# check if user is logged
|
|
|
|
unless( $$session{login}) {
|
|
|
|
$$res{deferred}{errmsg} = q{Vous n'êtes pas enregistré. };
|
|
|
|
$$res{sessiondestroy} = 1;
|
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
# check if domain parameter is set
|
|
|
|
unless( $$param{domain} && length $$param{domain} > 0) {
|
|
|
|
$$res{deferred}{errmsg} =
|
|
|
|
q{Domaine personnel non renseigné correctement. };
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
# check if tld parameter is set
|
|
|
|
unless( $$param{tld} && length $$param{tld} > 0) {
|
|
|
|
$$res{deferred}{errmsg} = q{Choix du domaine non fait. };
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_reserved($$param{domain})) {
|
|
|
|
$$res{deferred}{errmsg} = q{Nom de domaine réservé. };
|
|
|
|
}
|
|
|
|
elsif ( ! is_domain_name($$param{domain}) ) {
|
|
|
|
$$res{deferred}{errmsg} =
|
|
|
|
q{Nom de domaine choisi comportant des caractères invalides. };
|
|
|
|
}
|
|
|
|
elsif ( ! is_valid_tld($$param{tld}) ) {
|
|
|
|
$$res{deferred}{errmsg} =
|
|
|
|
q{Mauvais choix de domaine. };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
my $domain = $$param{domain} . $$param{tld};
|
|
|
|
|
|
|
|
eval {
|
|
|
|
my $app = app->new(get_cfg());
|
|
|
|
my $user = $app->auth($$session{login}, $$session{passwd});
|
|
|
|
$app->add_domain( $$user{login}, $domain );
|
|
|
|
|
|
|
|
$$res{addsession}{domainName} = $$param{domain};
|
|
|
|
$$res{deferred}{succmsg} =
|
|
|
|
q{Le nom de domaine a bien été réservé ! };
|
|
|
|
|
|
|
|
$app->disconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
if( $@ ) {
|
|
|
|
$$res{deferred}{errmsg} = q{Une erreur est survenue. } . $@;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$res
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rt_dom_details {
|
|
|
|
my ($session, $param, $request) = @_;
|
|
|
|
my $res;
|
|
|
|
|
|
|
|
# check if user is logged & if domain parameter is set
|
|
|
|
unless($$session{login}) {
|
|
|
|
$$res{deferred}{errmsg} = q{Session inactive.};
|
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
unless($$param{domain}) {
|
|
|
|
$$res{deferred}{errmsg} = q{Domaine non renseigné.};
|
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $app;
|
|
|
|
eval {
|
|
|
|
$app = app->new(get_cfg());
|
|
|
|
|
|
|
|
my $user = $app->auth($$session{login}, $$session{passwd});
|
|
|
|
|
|
|
|
unless ( $user && ( $$user{admin} ||
|
|
|
|
$app->is_owning_domain($$user{login}, $$param{domain}))) {
|
|
|
|
$app->disconnect();
|
|
|
|
$$res{deferred}{errmsg} = q{Donnée privée, petit coquin. ;) };
|
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2015-12-21 09:39:50 +01:00
|
|
|
my $zone = $app->get_zone( $$param{domain} );
|
|
|
|
my $zf = $zone->get_zonefile();
|
2015-10-13 19:06:37 +02:00
|
|
|
|
|
|
|
$app->disconnect();
|
|
|
|
|
|
|
|
$$res{template} = 'details';
|
|
|
|
$$res{params} = {
|
|
|
|
login => $$session{login}
|
|
|
|
, admin => $$user{admin}
|
|
|
|
, domain => $$param{domain}
|
2015-12-21 09:39:50 +01:00
|
|
|
, domain_zone => $zf->dump()
|
2015-10-13 19:06:37 +02:00
|
|
|
, user_ip => $$request{address}
|
|
|
|
};
|
|
|
|
|
|
|
|
if($$param{expert}) {
|
|
|
|
$$res{params}{expert} = 1;
|
|
|
|
}
|
|
|
|
else {
|
2015-12-28 16:08:52 +01:00
|
|
|
$$res{params}{zone} = $zf->rr_array_to_array_stripped();
|
2015-10-13 19:06:37 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if($@) {
|
|
|
|
$app->disconnect() if $app;
|
2015-12-22 17:50:26 +01:00
|
|
|
$$res{deferred}{errmsg} = q{Une erreur est survenue. } . $@;
|
2015-10-13 19:06:37 +02:00
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res
|
|
|
|
}
|
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
sub rt_dom_add_entry {
|
|
|
|
my ($session, $param, $request) = @_;
|
2015-10-13 19:06:37 +02:00
|
|
|
my $res;
|
|
|
|
|
|
|
|
unless( $$session{login} && $$param{domain} ) {
|
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
$$res{route} = '/domain/details/'. $$param{domain};
|
|
|
|
|
|
|
|
my @missingitems;
|
2015-12-22 17:50:26 +01:00
|
|
|
my @items = qw/domain type name ttl rdata/;
|
2015-10-13 19:06:37 +02:00
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
if($$param{type} && $$param{type} eq 'MX') {
|
|
|
|
push @items, qw/priority/;
|
2015-10-13 19:06:37 +02:00
|
|
|
}
|
2015-12-22 17:50:26 +01:00
|
|
|
|
|
|
|
if($$param{type} && $$param{type} eq 'SRV') {
|
|
|
|
push @items, qw/priority weight port/;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(@items) {
|
|
|
|
push @missingitems, $_ unless($$param{$_});
|
2015-10-13 19:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(@missingitems != 0) {
|
|
|
|
$$res{deferred}{errmsg} = "Il manque : " . join ', ', @missingitems;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
eval {
|
2015-12-22 17:50:26 +01:00
|
|
|
# Perform tests on the different entries
|
2015-10-13 19:06:37 +02:00
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
my $name = $$param{name};
|
|
|
|
|
2015-12-28 16:08:52 +01:00
|
|
|
$name =~ s/@/$$param{domain}./;
|
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
if($name =~ /$$param{domain}$/) {
|
|
|
|
$name .= '.';
|
2015-10-13 19:06:37 +02:00
|
|
|
}
|
|
|
|
|
2015-12-22 17:50:26 +01:00
|
|
|
if($name !~ /\.$/) {
|
|
|
|
$name .= ".$$param{domain}."
|
|
|
|
}
|
2015-10-13 19:06:37 +02:00
|
|
|
|
2015-12-21 09:39:50 +01:00
|
|
|
my $str_new = "$name $$param{ttl} $$param{type} ";
|
|
|
|
my $rdata = $$param{rdata};
|
2015-10-13 19:06:37 +02:00
|
|
|
|
2015-12-28 16:08:52 +01:00
|
|
|
if($$param{type} =~ /^(CNAME|MX|NS|PTR|SRV)$/) {
|
|
|
|
$rdata =~ s/@/$$param{domain}./;
|
|
|
|
$rdata .= ".$$param{domain}." if( $rdata !~ /\.$/);
|
|
|
|
}
|
|
|
|
|
2015-12-22 22:31:14 +01:00
|
|
|
if ($$param{type} =~ /^(CNAME|MX|NS|PTR|SRV)$/i
|
2015-12-22 17:50:26 +01:00
|
|
|
&& ! is_domain_name ($rdata))
|
|
|
|
{
|
|
|
|
$$res{deferred}{errmsg} =
|
2015-12-22 22:31:14 +01:00
|
|
|
"Une entrée $$param{type} doit avoir un nom de domaine "
|
|
|
|
. "(pas une URL, pas de http://) : '$rdata' n'est pas correct.";
|
2015-12-22 17:50:26 +01:00
|
|
|
$$res{route} = ($$request{referer}) ? $$request{referer} : '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($$param{type} eq 'A' && ! is_ipv4($rdata)) {
|
|
|
|
$$res{deferred}{errmsg} =
|
|
|
|
"Il faut une adresse IPv4 pour un enregistrement de type A."
|
|
|
|
. " Ceci n'est pas une adresse IPv4 : $rdata";
|
|
|
|
$$res{route} = ($$request{referer}) ? $$request{referer} : '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($$param{type} eq 'AAAA' && ! is_ipv6($rdata)) {
|
|
|
|
$$res{deferred}{errmsg} =
|
|
|
|
"Il faut une adresse IPv6 pour un enregistrement de type AAAA."
|
|
|
|
. " Ceci n'est pas une adresse IPv6 : $rdata";
|
|
|
|
$$res{route} = ($$request{referer}) ? $$request{referer} : '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2015-12-21 09:39:50 +01:00
|
|
|
if($$param{type} eq "MX") {
|
2015-12-22 17:50:26 +01:00
|
|
|
$str_new .= "$$param{priority} $rdata";
|
|
|
|
}
|
|
|
|
elsif ($$param{type} eq "SRV") {
|
|
|
|
$str_new .=
|
|
|
|
"$$param{priority} $$param{weight} $$param{port} $rdata";
|
2015-12-21 09:39:50 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-12-22 17:50:26 +01:00
|
|
|
$str_new .= "$rdata";
|
2015-12-21 09:39:50 +01:00
|
|
|
}
|
2015-12-22 17:50:26 +01:00
|
|
|
|
|
|
|
# Add the entry
|
|
|
|
|
|
|
|
my $app = app->new(get_cfg());
|
|
|
|
my $user = $app->auth($$session{login}, $$session{passwd});
|
|
|
|
|
|
|
|
unless ( $user && ( $$user{admin} ||
|
|
|
|
$app->is_owning_domain($$user{login}, $$param{domain}))) {
|
|
|
|
$app->disconnect();
|
|
|
|
$$res{deferred}{errmsg} = q{Donnée privée, petit coquin. ;) };
|
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $zone = $app->get_zone( $$param{domain} );
|
|
|
|
my $zf = $zone->get_zonefile();
|
2015-12-21 09:39:50 +01:00
|
|
|
$zf->rr_add_raw($str_new);
|
|
|
|
$zf->new_serial();
|
|
|
|
$zone->update( $zf );
|
2015-10-13 19:06:37 +02:00
|
|
|
|
|
|
|
$app->disconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( $@ ) {
|
2015-12-22 17:50:26 +01:00
|
|
|
$$res{deferred}{errmsg} = q{Problème à l'ajout d'une entrée. }. $@;
|
2015-10-13 19:06:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$res
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rt_dom_updateraw {
|
|
|
|
my ($session, $param, $request) = @_;
|
|
|
|
my $res;
|
|
|
|
|
|
|
|
# check if user is logged & if domain parameter is set
|
|
|
|
unless($$session{login} && $$param{domain}) {
|
|
|
|
$$res{sessiondestroy} = 1;
|
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @missingitems;
|
|
|
|
|
|
|
|
for(qw/domain zoneupdated/) {
|
|
|
|
push @missingitems, $_ unless($$param{$_});
|
|
|
|
}
|
|
|
|
|
|
|
|
if(@missingitems != 0) {
|
|
|
|
$$res{deferred}{errmsg} = "Il manque : " . join ', ', @missingitems;
|
|
|
|
$$res{route} = '/user/home';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
eval {
|
|
|
|
my $app = app->new(get_cfg());
|
|
|
|
my $user = $app->auth($$session{login}, $$session{passwd});
|
|
|
|
|
|
|
|
# if the user exists and if
|
|
|
|
# he is admin or he owns the requested domain
|
|
|
|
unless ( $user && ( $$user{admin} ||
|
|
|
|
$app->is_owning_domain($$user{login}, $$param{domain}))) {
|
|
|
|
$app->disconnect();
|
|
|
|
$$res{deferred}{errmsg} = q{Donnée privée, petit coquin. ;) };
|
|
|
|
$$res{route} = '/';
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
else {
|
2015-12-21 09:39:50 +01:00
|
|
|
my $zone = $app->get_zone( $$param{domain} );
|
|
|
|
my $zf = $zone->update_raw( $$param{zoneupdated} );
|
|
|
|
$zone->update( $zf );
|
2015-10-13 19:06:37 +02:00
|
|
|
$$res{route} = '/domain/details/' . $$param{domain};
|
|
|
|
}
|
|
|
|
|
|
|
|
$app->disconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
if($@) {
|
|
|
|
$$res{deferred}{errmsg} = $@;
|
|
|
|
$$res{route} = '/user/home';
|
|
|
|
}
|
|
|
|
|
|
|
|
$res
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|