[vhffs-dev] [575] Added TXT records to DNS. |
[ Thread Index |
Date Index
| More vhffs.org/vhffs-dev Archives
]
Revision: 575
Author: beuss
Date: 2007-04-18 15:15:48 +0000 (Wed, 18 Apr 2007)
Log Message:
-----------
Added TXT records to DNS.
Modified Paths:
--------------
trunk/vhffs-api/src/Vhffs/Panel/DNS.pm
trunk/vhffs-api/src/Vhffs/Services/DNS.pm
trunk/vhffs-panel/dns/prefs.pl
trunk/vhffs-panel/templates/Makefile.am
trunk/vhffs-panel/templates/dns/list_a_sub.tmpl
trunk/vhffs-panel/templates/dns/list_aaaa_sub.tmpl
trunk/vhffs-panel/templates/dns/prefs.tmpl
Added Paths:
-----------
trunk/vhffs-panel/templates/dns/list_txt_sub.tmpl
Modified: trunk/vhffs-api/src/Vhffs/Panel/DNS.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Panel/DNS.pm 2007-04-18 14:00:15 UTC (rev 574)
+++ trunk/vhffs-api/src/Vhffs/Panel/DNS.pm 2007-04-18 15:15:48 UTC (rev 575)
@@ -413,5 +413,39 @@
die(gettext('Unknown error')."\n");
}
+sub add_txt {
+ my ($dns, $name, $txt) = @_;
+ die() unless(defined $dns && defined $name && defined $txt);
+ warn "$name:$txt (2)";
+ my $rval = $dns->add_txt($name, $txt);
+ return 1 if($rval > 0);
+ die(gettext('Invalid prefix')."\n") if($rval == -1);
+ die(gettext('Text can\'t be empty')."\n") if($rval == -2);
+ die(gettext('Database error')."\n") if($rval == -3);
+ die(gettext('Unknown error')."\n");
+}
+sub update_txt {
+ my ($dns, $id, $txt) = @_;
+ die() unless(defined $dns && defined $id && defined $txt);
+ my $rval = $dns->update_txt($id, $txt);
+ return 1 if($rval > 0);
+ die(gettext('Invalid record')."\n") if($rval == -1);
+ die(gettext('Record does not exists')."\n") if($rval == -2);
+ die(gettext('Text can\'t be empty')."\n") if($rval == -3);
+ die(gettext('Database error')."\n") if($rval == -4);
+ die(gettext('Unknown error')."\n"),;
+}
+
+sub delete_txt {
+ my ($dns, $id) = @_;
+ die() unless(defined $dns && defined $id);
+ my $rval = $dns->delete_txt($id);
+ return 1 if($rval > 0);
+ die(gettext('Invalid record')."\n") if($rval == -1);
+ die(gettext('Record does not exists')."\n") if($rval == -2);
+ die(gettext('Database error')."\n") if($rval == -3);
+ die(gettext('Unknown error')."\n");
+}
+
1;
Modified: trunk/vhffs-api/src/Vhffs/Services/DNS.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Services/DNS.pm 2007-04-18 14:00:15 UTC (rev 574)
+++ trunk/vhffs-api/src/Vhffs/Services/DNS.pm 2007-04-18 15:15:48 UTC (rev 575)
@@ -218,7 +218,7 @@
my $dns_id = $params[0];
# Fetches A records
- $sql = 'SELECT id, zone, (CASE WHEN name = \'\' THEN \'default\' ELSE name END) AS name, type, data, aux, ttl FROM vhffs_dns_rr WHERE zone = ? AND type = \'A\'';
+ $sql = 'SELECT id, zone, (CASE WHEN name = \'\' THEN \'@\' ELSE name END) AS name, type, data, aux, ttl FROM vhffs_dns_rr WHERE zone = ? AND type = \'A\'';
my $sth = $dbh->prepare($sql);
$sth->execute($dns_id);
my $a = $sth->fetchall_hashref('id');
@@ -232,7 +232,7 @@
push @params, $ns;
# Fetches CNAME records
- $sql = 'SELECT id, zone, (CASE WHEN name = \'\' THEN \'default\' ELSE name END) AS name, type, data, aux, ttl FROM vhffs_dns_rr WHERE zone = ? AND type = ?';
+ $sql = 'SELECT id, zone, (CASE WHEN name = \'\' THEN \'@\' ELSE name END) AS name, type, data, aux, ttl FROM vhffs_dns_rr WHERE zone = ? AND type = ?';
$sth = $dbh->prepare($sql);
$sth->execute($dns_id, 'CNAME');
my $cname = $sth->fetchall_hashref('id');
@@ -261,18 +261,26 @@
push @params, $srv;
# Fetches AAAA records
- $sql = 'SELECT id, zone, (CASE WHEN name = \'\' THEN \'default\' ELSE name END) AS name, type, data, aux, ttl FROM vhffs_dns_rr WHERE zone = ? AND type = \'AAAA\'';
+ $sql = 'SELECT id, zone, (CASE WHEN name = \'\' THEN \'@\' ELSE name END) AS name, type, data, aux, ttl FROM vhffs_dns_rr WHERE zone = ? AND type = \'AAAA\'';
$sth = $dbh->prepare($sql);
$sth->execute($dns_id);
my $aaaa = $sth->fetchall_hashref('id');
push @params, $aaaa;
+ # Fetches TXT records
+ $sql = 'SELECT id, zone, name, type, data, aux, ttl FROM vhffs_dns_rr WHERE zone = ? AND type = ?';
+ $sth = $dbh->prepare($sql);
+ $sth->execute($dns_id, 'TXT');
+ my $txt = $sth->fetchall_hashref('id');
+ push @params, $txt;
+
+
return _new Vhffs::Services::DNS($main, @params);
}
sub _new
{
- my ($class, $main, $dns_id, $domain, $owner_gid, $ns, $mbox, $serial, $refresh, $retry, $expire, $minimum, $ttl, $oid, $owner_uid, $date_creation, $state, $description, $a, $nsr, $cname, $mx, $srv, $aaaa) = @_;
+ my ($class, $main, $dns_id, $domain, $owner_gid, $ns, $mbox, $serial, $refresh, $retry, $expire, $minimum, $ttl, $oid, $owner_uid, $date_creation, $state, $description, $a, $nsr, $cname, $mx, $srv, $aaaa, $txt) = @_;
my $self = $class->SUPER::_new($main, $oid, $owner_uid, $date_creation, $description, $state, Vhffs::Constants::TYPE_DNS);
@@ -293,6 +301,7 @@
$self->{MX} = $mx;
$self->{SRV} = $srv;
$self->{AAAA} = $aaaa;
+ $self->{TXT} = $txt;
return $self;
}
@@ -466,8 +475,8 @@
my ( $self , $name , $ip , $ttl ) = @_;
$ttl = 900 if ( ! defined $ttl );
- return -1 if( ! ($name =~ /^(?:[a-z0-9\-]+|\*)$/ ) );
- $name = '' if( $name eq 'default' );
+ return -1 if( ! ($name =~ /^(?:[a-z0-9\-]+|@)$/ ) );
+ $name = '' if( $name eq '@' );
return -2 if ( $self->name_exists( $name, 'A', 'CNAME' ) != 0 );
if( ! defined $ip ) {
@@ -486,7 +495,7 @@
$dbh->do($sql, undef, $self->{dns_id}, $name, $ip, $ttl) or return -5;
my $id = $dbh->last_insert_id(undef, undef, 'vhffs_dns_rr', undef);
- $name = 'default' if($name eq '');
+ $name = '@' if($name eq '');
my $a = {id => $id,
zone => $self->{dns_id},
name => $name,
@@ -715,8 +724,8 @@
my ( $self , $name , $ip , $ttl ) = @_;
$ttl = 900 if ( ! defined $ttl );
- return -1 if( ! ($name =~ /^(?:[a-z0-9\-]+|\*)$/ ) );
- $name = '' if( $name eq 'default' );
+ return -1 if( ! ($name =~ /^(?:[a-z0-9\-]+|\@)$/ ) );
+ $name = '' if( $name eq '@' );
return -2 if ( $self->name_exists( $name, 'CNAME', 'AAAA' ) != 0 );
if( ! defined $ip ) {
@@ -735,7 +744,7 @@
$dbh->do($sql, undef, $self->{dns_id}, $name, $ip, $ttl) or return -5;
my $id = $dbh->last_insert_id(undef, undef, 'vhffs_dns_rr', undef);
- $name = 'default' if($name eq '');
+ $name = '@' if($name eq '');
my $aaaa = {id => $id,
zone => $self->{dns_id},
name => $name,
@@ -783,6 +792,16 @@
return 1;
}
+=pod
+
+=head2 delete_aaaa
+
+ die("Unable to delete AAAA record #$id\n") unless($dns->delete_aaaa($id) > 0);
+
+Delete AAAA record whose id is C<$id>.
+
+=cut
+
sub delete_aaaa
{
my ($self, $id) = @_;
@@ -801,9 +820,108 @@
return 1;
}
+=pod
+=head2 add_txt
+ die('Unable to add TXT record') unless($dns->add_txt($prefix, $txt));
+Associate text $txt to hostname $prefix.
+
+=cut
+
+sub add_txt
+{
+ my ($self, $name, $data, $ttl) = @_;
+
+ $ttl = 900 if ( ! defined $ttl );
+ return -1 unless($name =~ /^(?:[a-z0-9\-]+|\@)$/ );
+ $name = '' if( $name eq '@' );
+ return -2 if($data =~ /^\s*$/);
+
+ my $dbh = $self->get_main()->get_db_object();
+ my $sql = 'INSERT INTO vhffs_dns_rr (zone, name, type, data, aux, ttl) VALUES(?, ?, \'TXT\', ?, 0, ?)';
+ $dbh->do($sql, undef, $self->{dns_id}, $name, $data, $ttl) or return -3;
+
+ my $id = $dbh->last_insert_id(undef, undef, 'vhffs_dns_rr', undef);
+ $name = '@' if($name eq '');
+ my $txt = {id => $id,
+ zone => $self->{dns_id},
+ name => $name,
+ type => 'TXT',
+ data => $data,
+ aux => 0,
+ ttl => $ttl
+ };
+ $self->{TXT}{$id} = $txt;
+
+ $self->update_serial();
+
+ $self->add_history("Added a TXT record for prefix $name ($txt)");
+ return $id;
+
+
+}
+
+=pod
+
+=head2 update_txt
+
+ die("Unable to set new value $newtxt for TXT record #$id\n") unless($dns->update_txt($id, $newtxt));
+
+Update data for a TXT record.
+
+=cut
+
+sub update_txt
+{
+ my ($self, $id, $data) = @_;
+
+ return -1 unless($id =~ /^\d+$/);
+ my $rr = $self->{TXT}{$id};
+ return -2 unless(defined $rr);
+ return -3 if($data =~ /^\s*$/);
+
+ my $sql = 'UPDATE vhffs_dns_rr SET data = ? WHERE id = ? AND zone = ? AND type=\'TXT\'';
+ my $dbh = $self->get_main()->get_db_object();
+
+ $dbh->do($sql, undef, $data, $id, $self->{dns_id}) or return -4;
+
+ $rr->{data} = $data;
+
+ $self->add_history('Changed the TXT data for '.$rr->{name}.": $data");
+
+ $self->update_serial();
+}
+
+=pod
+
+=head2 delete_txt
+
+ die("Unable to delete TXT record #$id\n") unless($dns->delete_txt($id));
+
+Deletes TXT record which id is $id.
+
+=cut
+
+sub delete_txt
+{
+ my ($self, $id) = @_;
+
+ return -1 unless($id =~ /^\d+$/ );
+ return -2 unless(defined $self->{TXT}{$id});
+
+ my $dbh = $self->get_main->get_db_object();
+ my $sql = 'DELETE FROM vhffs_dns_rr WHERE type = \'TXT\' AND zone = ? AND id = ?';
+ $dbh->do($sql, undef, $self->{dns_id}, $id) or return -3;
+
+ $self->add_history('Deleted TXT record '.$self->{TXT}{$id}{name});
+ delete $self->{TXT}{$id};
+
+ $self->update_serial();
+ return 1;
+}
+
sub update_cname
{
my ($self, $id, $dest) = @_;
@@ -831,7 +949,7 @@
$ttl = 900 if ( ! defined $ttl );
return -1 unless($name =~ /^[a-z0-9\-]+$/ );
return -2 unless(Vhffs::Functions::check_domain_name($dest, 1));
- $name = '' if( $name eq 'default' );
+ $name = '' if( $name eq '@' );
return -3 if ( $self->name_exists( $name, 'A', 'AAAA', 'CNAME' ) != 0 );
# Add a '.' the the submitted name. Otherwise, if the user submit tata.toto.com IN CNAME foo.com
@@ -844,7 +962,7 @@
$dbh->do($sql, undef, $self->{dns_id}, $name, $dest, $ttl) or return -4;
my $id = $dbh->last_insert_id(undef, undef, 'vhffs_dns_rr', undef);
- $name = 'default' if($name eq '');
+ $name = '@' if($name eq '');
my $cname = {id => $id,
zone => $self->{dns_id},
name => $name,
@@ -967,6 +1085,11 @@
return $self->{SRV};
}
+sub get_txt_type {
+ my $self = shift;
+ return $self->{TXT};
+}
+
sub get_soa_ns
{
my $self = shift;
Modified: trunk/vhffs-panel/dns/prefs.pl
===================================================================
--- trunk/vhffs-panel/dns/prefs.pl 2007-04-18 14:00:15 UTC (rev 574)
+++ trunk/vhffs-panel/dns/prefs.pl 2007-04-18 15:15:48 UTC (rev 575)
@@ -210,6 +210,23 @@
eval { Vhffs::Panel::DNS::add_srv($dns, $proto, $svc, $host, $port, $aux, $weight); };
if($@) { $panel->add_error(sprintf(gettext('Unable to add SRV record: %s'), $@)); }
else { $panel->add_info(gettext('SRV Record added')); }
+ } elsif($action eq 'add_txt') {
+ warn "$name:$data";
+ eval { Vhffs::Panel::DNS::add_txt($dns, $name, $data); };
+ if($@) { $panel->add_error(sprintf(gettext('Unable to add TXT record: %s'), $@)); }
+ else { $panel->add_info(gettext('TXT Record added')); }
+ } elsif($action eq 'manage_txt') {
+ if(defined $cgi->param('modify_txt_submit')) {
+ # User wants to modify an TXT record
+ eval { Vhffs::Panel::DNS::update_txt($dns, $id, $data); };
+ if($@) { $panel->add_error(sprintf(gettext('Unable to modify TXT record: %s'), $@)); }
+ else { $panel->add_info(gettext('TXT Record updated')); }
+ } else {
+ # TXT deletion
+ eval { Vhffs::Panel::DNS::delete_txt($dns, $id); };
+ if($@) { $panel->add_error(sprintf(gettext('Unable to delete TXT record: %s'), $@)); }
+ else { $panel->add_info(gettext('TXT Record deleted')); }
+ }
}
}
}
@@ -219,16 +236,17 @@
my $mx = $dns->get_mx_type;
my $ns = $dns->get_ns_type;
my $srv = $dns->get_srv_type;
+ my $txt = $dns->get_txt_type;
$panel->set_title(sprintf(gettext("DNS Administration - %s"), $domain_name));
$template = new HTML::Template( filename => $templatedir."/dns/prefs.tmpl" );
$template->param( DOMAIN_NAME => $domain_name );
- $template->param( TEXT_TYPEA => gettext("All A TYPE for you domain name") );
- $template->param( TEXT_TYPEAAAA => gettext("All AAAA TYPE for you domain name") );
- $template->param( TEXT_TYPEMX => gettext("All MX TYPE for you domain name") );
- $template->param( TEXT_TYPECNAME => gettext("All CNAME for you domain name") );
+ $template->param( TEXT_TYPEA => gettext('All A TYPE for your domain name') );
+ $template->param( TEXT_TYPEAAAA => gettext('All AAAA TYPE for your domain name') );
+ $template->param( TEXT_TYPEMX => gettext('All MX TYPE for your domain name') );
+ $template->param( TEXT_TYPECNAME => gettext('All CNAME for your domain name') );
$template->param( TEXT_LIST_A => gettext("List all A reccords") );
$template->param( TEXT_LIST_AAAA => gettext("List all AAAA reccords") );
$template->param( TEXT_ADD_A => gettext("Add an A record") );
@@ -294,9 +312,13 @@
$template->param( TEXT_SUBMIT => gettext("Submit") );
+ # Treat TXT part
+ $template->param( TEXT_LIST_TXT => gettext('List all TXT records') );
+ $template->param( TEXT_ADD_TXT => gettext('Add a TXT record') );
+ $template->param( TEXT_TYPETXT => gettext('All TXT records for your domain name') );
+ $template->param( TXT_DATA_TEXT => gettext('Text') );
+ $template->param( TXT_NAME_TEXT => gettext('Name for record') );
-# $template->param( TEXT_LIST_TYPE_CNAME => gettext("All CNAME for you domain name") );
-
$subtemplate = new HTML::Template( filename =>$templatedir. "/dns/list_sub.tmpl" );
if( scalar(keys %{$a_rr}) ) {
@@ -375,11 +397,25 @@
$thirdtemplate->param( PRIORITY_TXT => gettext( 'Priority' ) );
$thirdtemplate->param( WEIGHT_TXT => gettext( 'Weight' ) );
$thirdtemplate->param( SRV_RR => \@list_srv );
- $template->param( LIST_SRV => $thirdtemplate->output );
+ $template->param( LIST_TXT => $thirdtemplate->output );
} else {
- $template->param( LIST_SRV => gettext( "No SRV available on this domain") );
+ $template->param( LIST_TXT => gettext( "No TXT available on this domain") );
}
+ if( scalar(keys %{$txt}) ) {
+ my @list_txt = sort {$a->{data} cmp $b->{data}} values(%{$txt});
+ $thirdtemplate = new HTML::Template( filename => $templatedir.'/dns/list_txt_sub.tmpl', global_vars => 1, die_on_bad_params => 0 );
+ $thirdtemplate->param( DEL => gettext( 'Delete' ) );
+ $thirdtemplate->param( MODIFY => gettext( 'Modify' ) );
+ $thirdtemplate->param( DOMAIN => $domain_name );
+ $thirdtemplate->param( HOST_TXT => gettext( 'Host' ) );
+ $thirdtemplate->param( TXT_RR => \@list_txt );
+ $template->param( LIST_TXT => $thirdtemplate->output );
+ } else {
+ $template->param( LIST_TXT => gettext( "No TXT available on this domain") );
+ }
+
+
if( $user->is_admin == 1 )
{
$subtemplate = new HTML::Template( filename => $templatedir."/admin/misc/opart.tmpl" );
Modified: trunk/vhffs-panel/templates/Makefile.am
===================================================================
--- trunk/vhffs-panel/templates/Makefile.am 2007-04-18 14:00:15 UTC (rev 574)
+++ trunk/vhffs-panel/templates/Makefile.am 2007-04-18 15:15:48 UTC (rev 575)
@@ -98,6 +98,7 @@
dns/list_mx_sub.tmpl \
dns/list_ns_sub.tmpl \
dns/list_srv_sub.tmpl \
+ dns/list_txt_sub.tmpl \
dns/list_sub.tmpl \
dns/menu_sub.tmpl \
dns/menu_sub_wait.tmpl \
Modified: trunk/vhffs-panel/templates/dns/list_a_sub.tmpl
===================================================================
--- trunk/vhffs-panel/templates/dns/list_a_sub.tmpl 2007-04-18 14:00:15 UTC (rev 574)
+++ trunk/vhffs-panel/templates/dns/list_a_sub.tmpl 2007-04-18 15:15:48 UTC (rev 575)
@@ -8,7 +8,6 @@
<input type="text" name="data" id="data_<tmpl_var name="id">" value="<TMPL_VAR NAME="data">" />
<input type="hidden" name="domain" value="<TMPL_VAR NAME="DOMAIN">" />
<input type="hidden" name="rr_id" value="<tmpl_var name="id">" />
- <input type="hidden" name="name" value="<TMPL_VAR NAME="NAME">" />
<input type="hidden" name="action" value="manage_a"/>
<input type="submit" value="<TMPL_VAR NAME="MODIFY">" class="autowidth" name="modify_a_submit" />
<input type="submit" value="<TMPL_VAR NAME="DEL">" name="delete_a_submit" class="autowidth"/>
Modified: trunk/vhffs-panel/templates/dns/list_aaaa_sub.tmpl
===================================================================
--- trunk/vhffs-panel/templates/dns/list_aaaa_sub.tmpl 2007-04-18 14:00:15 UTC (rev 574)
+++ trunk/vhffs-panel/templates/dns/list_aaaa_sub.tmpl 2007-04-18 15:15:48 UTC (rev 575)
@@ -8,7 +8,6 @@
<input type="text" name="data" id="data_<tmpl_var name="id">" value="<TMPL_VAR NAME="data">" />
<input type="hidden" name="domain" value="<TMPL_VAR NAME="DOMAIN">" />
<input type="hidden" name="rr_id" value="<tmpl_var name="id">" />
- <input type="hidden" name="name" value="<TMPL_VAR NAME="name">" />
<input type="hidden" name="action" value="manage_aaaa"/>
<input type="submit" value="<TMPL_VAR NAME="MODIFY">" class="autowidth" name="modify_aaaa_submit" />
<input type="submit" value="<TMPL_VAR NAME="DEL">" name="delete_aaaa_submit" class="autowidth"/>
Added: trunk/vhffs-panel/templates/dns/list_txt_sub.tmpl
===================================================================
--- trunk/vhffs-panel/templates/dns/list_txt_sub.tmpl 2007-04-18 14:00:15 UTC (rev 574)
+++ trunk/vhffs-panel/templates/dns/list_txt_sub.tmpl 2007-04-18 15:15:48 UTC (rev 575)
@@ -0,0 +1,16 @@
+<p class="help"><tmpl_var name="LIST_HELP"></p>
+<tmpl_loop name="TXT_RR">
+<form method="post" action="prefs.pl">
+ <p>
+ <label for="data_<tmpl_var name="id">">
+ <TMPL_VAR NAME="NAME">->
+ </label>
+ <input type="text" name="data" id="data_<tmpl_var name="id">" value="<TMPL_VAR NAME="data">" />
+ <input type="hidden" name="domain" value="<TMPL_VAR NAME="DOMAIN">" />
+ <input type="hidden" name="rr_id" value="<tmpl_var name="id">" />
+ <input type="hidden" name="action" value="manage_txt"/>
+ <input type="submit" value="<TMPL_VAR NAME="MODIFY">" class="autowidth" name="modify_txt_submit" />
+ <input type="submit" value="<TMPL_VAR NAME="DEL">" name="delete_txt_submit" class="autowidth"/>
+ </p>
+</form>
+</tmpl_loop>
Modified: trunk/vhffs-panel/templates/dns/prefs.tmpl
===================================================================
--- trunk/vhffs-panel/templates/dns/prefs.tmpl 2007-04-18 14:00:15 UTC (rev 574)
+++ trunk/vhffs-panel/templates/dns/prefs.tmpl 2007-04-18 15:15:48 UTC (rev 575)
@@ -156,6 +156,33 @@
</p>
</form>
+<h2><TMPL_VAR NAME="TEXT_TYPETXT"></h2>
+<h3><TMPL_VAR NAME="TEXT_LIST_TXT"></h3>
+<TMPL_VAR NAME="LIST_TXT">
+
+<h3><TMPL_VAR NAME="TEXT_ADD_TXT"></h3>
+<form method="post" action="prefs.pl">
+ <p class="help"><TMPL_VAR NAME="TEXT_ORIGIN"></p>
+ <p>
+ <label for="add_txt_prefix">
+ <TMPL_VAR NAME="TXT_NAME_TEXT">
+ </label>
+ <input type="text" name="name" id="add_txt_prefix" /><strong>.<TMPL_VAR NAME="DOMAIN_NAME"></strong>
+ </p>
+ <p>
+ <label for="add_txt_data">
+ <TMPL_VAR NAME="TXT_DATA_TEXT">
+ </label>
+ <textarea name="data" id="add_txt_data"></textarea>
+ </p>
+ <p class="button">
+ <input type="hidden" name="domain" value="<TMPL_VAR NAME="DOMAIN_NAME">" />
+ <input type="hidden" name="action" value="add_txt"/>
+ <input type="submit" value="<TMPL_VAR NAME="TEXT_SUBMIT">" />
+ </p>
+</form>
+
+
<h2><tmpl_var name="TEXT_ACL_ADMIN"></h2>
<form method="post" action="../acl/view.pl">