...
 
Commits (3)
......@@ -12,10 +12,10 @@ use checks qw(check_params check_key check_method subdomain_exists valid_subdoma
use util qw(rand_pass ip_record_type);
use database;
use routes::createsub;
#use routes::modifysub;
use routes::modifysub;
use routes::deletesub;
#use routes::locksub;
#use routes::chgkey;
use routes::locksub;
use routes::chgkey;
use routes::listsub;
use routes::help;
use routes::updatesub;
......@@ -29,116 +29,16 @@ database::prepare_database();
my %routes = (
"/create" => \&createsub::create_subdomain,
"/modify" => \&modify_subdomain,
"/modify" => \&modifysub::modify_subdomain,
"/delete" => \&deletesub::delete_subdomain,
"/lock" => \&lock_subdomain,
"/chgkey" => \&chgkey_subdomain,
"/lock" => \&locksub::lock_subdomain,
"/chgkey" => \&chgkey::chgkey_subdomain,
"/list" => \&listsub::list_subdomains,
"/help" => \&help::get_help,
"/update" => \&updatesub::update_ddns,
"/clear" => \&clearsub::clear_ddns,
);
sub lock_subdomain {
my $req = shift;
my $c_method = check_method($req); if (defined $c_method) { return $c_method; }
my $c_params = check_params($req, ("key", "subdomain", "state")); if (defined $c_params) { return $c_params; }
my $c_key = check_key($req, $conf::admin_key); if (defined $c_key) { return $c_key; }
my $c_isvalid = valid_subdomain($req); if (defined $c_isvalid) { return $c_isvalid; }
my @subdomain_r = subdomain_exists($req); my $c_subexists = @subdomain_r;
if ($c_subexists == 1) { return $subdomain_r[0]->finalize; }
my @states = ("unlock", "lock");
my $lock_state = $req->body_parameters->get("state");
my $subdomain = $req->body_parameters->get("subdomain");
my $time = time;
if ($lock_state ne '1' && $lock_state ne '0') {
my $res = $req->new_response(400, [],
"Invalid state specified. Use '1' for lock and '0' for unlock\n");
return $res->finalize;
}
my $s_res = $database::DDNS_DB_UPD_LCK->execute($lock_state, $subdomain);
if (defined $s_res) {
my $res = $req->new_response(200, [],
"Successfully $states[$lock_state]ed subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n");
return $res->finalize;
} else {
my $res = $req->new_response(400, [],
"Failed to $states[$lock_state] subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n");
return $res->finalize;
}
}
sub chgkey_subdomain {
my $req = shift;
my $c_method = check_method($req); if (defined $c_method) { return $c_method; }
my $c_params = check_params($req, ("key", "subdomain")); if (defined $c_params) { return $c_params; }
my $c_key = check_key($req, $conf::admin_key); if (defined $c_key) { return $c_key; }
my $c_isvalid = valid_subdomain($req); if (defined $c_isvalid) { return $c_isvalid; }
my @subdomain_r = subdomain_exists($req); my $c_subexists = @subdomain_r;
if ($c_subexists == 1) { return $subdomain_r[0]->finalize; }
my $subdomain = $req->body_parameters->get("subdomain");
my $subdomain_key = rand_pass(64);
my $s_res = $database::DDNS_DB_UPD_KEY->execute($subdomain_key, $subdomain);
if (defined $s_res) {
my $res = $req->new_response(200, [],
"Successfully regenerated key for subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n" .
"Key: $subdomain_key\n");
return $res->finalize;
} else {
my $res = $req->new_response(400, [],
"Failed to regenerate key for subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n");
return $res->finalize;
}
}
sub modify_subdomain {
my $req = shift;
my $c_method = check_method($req); if (defined $c_method) { return $c_method; }
my $c_params = check_params($req, ("key", "subdomain", "ip")); if (defined $c_params) { return $c_params; }
my $c_isvalid = valid_subdomain($req); if (defined $c_isvalid) { return $c_isvalid; }
my @subdomain_r = subdomain_exists($req); my $c_subexists = @subdomain_r;
if ($c_subexists == 1) { return $subdomain_r[0]->finalize; }
my $c_key = check_key($req, $conf::admin_key); if (defined $c_key) { return $c_key; }
my $subdomain = $req->body_parameters->get('subdomain');
my $address = $req->body_parameters->get('ip');
my $ttl = 2;
my $rec_type = ip_record_type($address);
if (!defined $rec_type) {
my $res = $req->new_response(400, [], "Invalid IP address specified.\n"); return $res->finalize;
}
my $dns_update = new Net::DNS::Update($conf::NAMESERVER_DNS_ZONE);
$dns_update->push(update => rr_del("$subdomain.$conf::NAMESERVER_DNS_ZONE. $rec_type"));
$dns_update->push(update => rr_add("$subdomain.$conf::NAMESERVER_DNS_ZONE. $ttl $rec_type $address"));
$dns_update->sign_tsig($conf::NAMESERVER_DNSSEC_KEY);
my ($r_code, $r_msg) = execute_ddns($dns_update);
if (!$r_code) {
my $res = $req->new_response(400, [], "Modfication failed.\n"); return $res->finalize;
} else {
my $time = time;
if ($rec_type eq "A") {
$database::DDNS_DB_UPD_ADR->execute($address, $subdomain_r[3], $time, $subdomain_r[0]);
} elsif ($rec_type eq "AAAA") {
$database::DDNS_DB_UPD_ADR->execute($subdomain_r[2], $address, $time, $subdomain_r[0]);
}
my $res = $req->new_response(200, [], "Modfication successful.\n"); return $res->finalize;
}
}
my $app_or_middleware = sub {
my $env = shift; # PSGI env
......
package chgkey;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(chgkey_subdomain);
use lib "..";
use checks qw(check_params check_key check_method subdomain_exists valid_subdomain);
use util qw(rand_pass);
use database;
sub chgkey_subdomain {
my $req = shift;
my $c_method = check_method($req); if (defined $c_method) { return $c_method; }
my $c_params = check_params($req, ("key", "subdomain")); if (defined $c_params) { return $c_params; }
my $c_key = check_key($req, $conf::admin_key); if (defined $c_key) { return $c_key; }
my $c_isvalid = valid_subdomain($req); if (defined $c_isvalid) { return $c_isvalid; }
my @subdomain_r = subdomain_exists($req); my $c_subexists = @subdomain_r;
if ($c_subexists == 1) { return $subdomain_r[0]->finalize; }
my $subdomain = $req->body_parameters->get("subdomain");
my $subdomain_key = rand_pass(64);
my $s_res = $database::DDNS_DB_UPD_KEY->execute($subdomain_key, $subdomain);
if (defined $s_res) {
my $res = $req->new_response(200, [],
"Successfully regenerated key for subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n" .
"Key: $subdomain_key\n");
return $res->finalize;
} else {
my $res = $req->new_response(400, [],
"Failed to regenerate key for subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n");
return $res->finalize;
}
}
1;
\ No newline at end of file
package locksub;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(lock_subdomain);
use lib "..";
use checks qw(check_params check_key check_method subdomain_exists valid_subdomain);
use database;
sub lock_subdomain {
my $req = shift;
my $c_method = check_method($req); if (defined $c_method) { return $c_method; }
my $c_params = check_params($req, ("key", "subdomain", "state")); if (defined $c_params) { return $c_params; }
my $c_key = check_key($req, $conf::admin_key); if (defined $c_key) { return $c_key; }
my $c_isvalid = valid_subdomain($req); if (defined $c_isvalid) { return $c_isvalid; }
my @subdomain_r = subdomain_exists($req); my $c_subexists = @subdomain_r;
if ($c_subexists == 1) { return $subdomain_r[0]->finalize; }
my @states = ("unlock", "lock");
my $lock_state = $req->body_parameters->get("state");
my $subdomain = $req->body_parameters->get("subdomain");
my $time = time;
if ($lock_state ne '1' && $lock_state ne '0') {
my $res = $req->new_response(400, [],
"Invalid state specified. Use '1' for lock and '0' for unlock\n");
return $res->finalize;
}
my $s_res = $database::DDNS_DB_UPD_LCK->execute($lock_state, $subdomain);
if (defined $s_res) {
my $res = $req->new_response(200, [],
"Successfully $states[$lock_state]ed subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n");
return $res->finalize;
} else {
my $res = $req->new_response(400, [],
"Failed to $states[$lock_state] subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n");
return $res->finalize;
}
}
1;
\ No newline at end of file
package modifysub;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(modify_subdomain);
use lib "..";
use checks qw(check_params check_key check_method subdomain_exists valid_subdomain);
use util qw(ip_record_type execute_ddns);
use database;
use Net::DNS;
sub modify_subdomain {
my $req = shift;
my $c_method = check_method($req); if (defined $c_method) { return $c_method; }
my $c_params = check_params($req, ("key", "subdomain", "ip")); if (defined $c_params) { return $c_params; }
my $c_isvalid = valid_subdomain($req); if (defined $c_isvalid) { return $c_isvalid; }
my @subdomain_r = subdomain_exists($req); my $c_subexists = @subdomain_r;
if ($c_subexists == 1) { return $subdomain_r[0]->finalize; }
my $c_key = check_key($req, $conf::admin_key); if (defined $c_key) { return $c_key; }
my $subdomain = $req->body_parameters->get('subdomain');
my $address = $req->body_parameters->get('ip');
my $ttl = 2;
my $rec_type = ip_record_type($address);
if (!defined $rec_type) {
my $res = $req->new_response(400, [], "Invalid IP address specified.\n"); return $res->finalize;
}
my $dns_update = new Net::DNS::Update($conf::NAMESERVER_DNS_ZONE);
$dns_update->push(update => rr_del("$subdomain.$conf::NAMESERVER_DNS_ZONE. $rec_type"));
$dns_update->push(update => rr_add("$subdomain.$conf::NAMESERVER_DNS_ZONE. $ttl $rec_type $address"));
$dns_update->sign_tsig($conf::NAMESERVER_DNSSEC_KEY);
my ($r_code, $r_msg) = execute_ddns($dns_update);
if (!$r_code) {
my $res = $req->new_response(400, [], "Modfication failed.\n"); return $res->finalize;
} else {
my $time = time;
if ($rec_type eq "A") {
$database::DDNS_DB_UPD_ADR->execute($address, $subdomain_r[3], $time, $subdomain_r[0]);
} elsif ($rec_type eq "AAAA") {
$database::DDNS_DB_UPD_ADR->execute($subdomain_r[2], $address, $time, $subdomain_r[0]);
}
my $res = $req->new_response(200, [], "Modfication successful.\n"); return $res->finalize;
}
}
1;
\ No newline at end of file