...
 
Commits (2)
......@@ -11,9 +11,9 @@ use conf;
use checks qw(check_params check_key check_method subdomain_exists valid_subdomain);
use util qw(rand_pass ip_record_type);
use database;
#use routes::createsub;
use routes::createsub;
#use routes::modifysub;
#use routes::deletesub;
use routes::deletesub;
#use routes::locksub;
#use routes::chgkey;
use routes::listsub;
......@@ -23,14 +23,13 @@ use routes::help;
use DBI;
use Plack::Request;
use Net::DNS;
database::prepare_database();
my %routes = (
"/create" => \&create_subdomain,
"/create" => \&createsub::create_subdomain,
"/modify" => \&modify_subdomain,
"/delete" => \&delete_subdomain,
"/delete" => \&deletesub::delete_subdomain,
"/lock" => \&lock_subdomain,
"/chgkey" => \&chgkey_subdomain,
"/list" => \&listsub::list_subdomains,
......@@ -39,74 +38,6 @@ my %routes = (
"/clear" => \&clear_ddns,
);
sub create_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 == 0) {
my $res = $req->new_response(404, [], "The specified subdomain already exists.\n");
return $res->finalize;
}
my $subdomain = $req->body_parameters->get("subdomain");
my $subdomain_key = rand_pass(64);
my $time = time;
my $s_res = $database::DDNS_DB_INS->execute($subdomain, $subdomain_key, "(unset)", "(unset)", 0, $time, 0, 0);
if (defined $s_res) {
my $res = $req->new_response(200, [],
"Successfully created subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\nKey: $subdomain_key\n");
return $res->finalize;
} else {
my $res = $req->new_response(400, [],
"Failed to create subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n");
return $res->finalize;
}
}
sub delete_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 $dns_update = new Net::DNS::Update($conf::NAMESERVER_DNS_ZONE);
$dns_update->push(update => rr_del("$subdomain.$conf::NAMESERVER_DNS_ZONE. A"));
$dns_update->push(update => rr_del("$subdomain.$conf::NAMESERVER_DNS_ZONE. AAAA"));
$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, [],
"Failed to delete subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\" from nameserver.\n");
return $res->finalize;
}
my $s_res = $database::DDNS_DB_DEL->execute($subdomain);
if (defined $s_res) {
my $res = $req->new_response(200, [],
"Successfully deleted subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n");
return $res->finalize;
} else {
my $res = $req->new_response(400, [],
"Failed to delete subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\" from database.\n");
return $res->finalize;
}
}
sub lock_subdomain {
......@@ -289,24 +220,6 @@ sub clear_ddns {
}
}
sub execute_ddns {
my $dns_update = shift;
my $dns_resolv = new Net::DNS::Resolver;
$dns_resolv->nameserver($conf::NAMESERVER); # convert to list later
my $reply = $dns_resolv->send($dns_update);
if ($reply) {
if ($reply->header->rcode eq 'NOERROR') {
return (1, "");
} else {
return (0, $reply->header->r_code);
}
} else {
return (0, $dns_resolv->errorstring);
}
}
my $app_or_middleware = sub {
my $env = shift; # PSGI env
......@@ -314,9 +227,6 @@ my $app_or_middleware = sub {
my $path_info = $req->path_info;
my $query = $req->body_parameters;
#my $c_method = check_method($req); if (defined $c_method) { return $c_method; }
#my $c_params = check_params($req); if (defined $c_params) { return $c_params; }
if (defined $routes{$path_info}) {
return $routes{$path_info}($req);
......
package createsub;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(create_subdomain);
use lib "..";
use checks qw(check_params check_key check_method subdomain_exists valid_subdomain);
use util qw(rand_pass);
use database;
sub create_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 == 0) {
my $res = $req->new_response(404, [], "The specified subdomain already exists.\n");
return $res->finalize;
}
my $subdomain = $req->body_parameters->get("subdomain");
my $subdomain_key = rand_pass(64);
my $time = time;
my $s_res = $database::DDNS_DB_INS->execute($subdomain, $subdomain_key, "(unset)", "(unset)", 0, $time, 0, 0);
if (defined $s_res) {
my $res = $req->new_response(200, [],
"Successfully created subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\nKey: $subdomain_key\n");
return $res->finalize;
} else {
my $res = $req->new_response(400, [],
"Failed to create subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n");
return $res->finalize;
}
}
1;
\ No newline at end of file
package deletesub;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(delete_subdomain);
use lib "..";
use checks qw(check_params check_key check_method subdomain_exists valid_subdomain);
use util qw(rand_pass execute_ddns);
use database;
use Net::DNS;
sub delete_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 $dns_update = new Net::DNS::Update($conf::NAMESERVER_DNS_ZONE);
$dns_update->push(update => rr_del("$subdomain.$conf::NAMESERVER_DNS_ZONE. A"));
$dns_update->push(update => rr_del("$subdomain.$conf::NAMESERVER_DNS_ZONE. AAAA"));
$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, [],
"Failed to delete subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\" from nameserver.\n");
return $res->finalize;
}
my $s_res = $database::DDNS_DB_DEL->execute($subdomain);
if (defined $s_res) {
my $res = $req->new_response(200, [],
"Successfully deleted subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\"\n");
return $res->finalize;
} else {
my $res = $req->new_response(400, [],
"Failed to delete subdomain \"$subdomain.$conf::NAMESERVER_DNS_ZONE\" from database.\n");
return $res->finalize;
}
}
1;
\ No newline at end of file
......@@ -3,8 +3,9 @@ use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(rand_pass ip_record_type);
our @EXPORT_OK = qw(rand_pass ip_record_type execute_ddns);
use Net::DNS;
use Regexp::Common qw /net/;
my @key_char_set = ("A".."Z", "a".."z", "0".."9");
......@@ -32,4 +33,22 @@ sub ip_record_type {
return $type;
}
sub execute_ddns {
my $dns_update = shift;
my $dns_resolv = new Net::DNS::Resolver;
$dns_resolv->nameserver($conf::NAMESERVER); # convert to list later
my $reply = $dns_resolv->send($dns_update);
if ($reply) {
if ($reply->header->rcode eq 'NOERROR') {
return (1, "");
} else {
return (0, $reply->header->r_code);
}
} else {
return (0, $dns_resolv->errorstring);
}
}
1;
\ No newline at end of file