[vhffs-dev] [571] Add SRV record support to DNS service.

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 571
Author:   beuss
Date:     2007-04-17 20:36:36 +0000 (Tue, 17 Apr 2007)

Log Message:
-----------
Add SRV record support to DNS service.

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/prefs.tmpl


Modified: trunk/vhffs-api/src/Vhffs/Panel/DNS.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Panel/DNS.pm	2007-04-17 20:32:37 UTC (rev 570)
+++ trunk/vhffs-api/src/Vhffs/Panel/DNS.pm	2007-04-17 20:36:36 UTC (rev 571)
@@ -329,6 +329,48 @@
     die(gettext('Database error')."\n") if($rval == -4);
     die(gettext('Unknown error')."\n");
 }
- 
 
+sub add_srv {
+    my ($dns, $proto, $svc, $host, $port, $priority, $weight) = @_;
+    die() unless(defined $dns && defined $proto && defined $svc && defined $host && defined $port && defined $priority && defined $weight);
+    my $rval = $dns->add_srv($proto, $svc, $host, $port, $priority, $weight);
+    return 1 if($rval > 0);
+    die(gettext('Invalid protocol syntax')."\n") if($rval == -1);
+    die(gettext('Invalid service syntax')."\n") if($rval == -2);
+    die(gettext('Invalid destination domain name')."\n") if($rval == -3);
+    die(gettext('Invalid port')."\n") if($rval == -4);
+    die(gettext('Invalid priority')."\n") if($rval == -5);
+    die(gettext('Invalid weight')."\n") if($rval == -6);
+    die(gettext('This host is already registered for this service')."\n") if($rval == -7);
+    die(gettext('Database error')."\n") if($rval == -8);
+    die(gettext('Unknown error')."\n");
+}
+
+sub update_srv {
+    my ($dns, $id, $host, $port, $priority, $weight) = @_;
+    die() unless(defined $dns && defined $host && defined $port && defined $priority && defined $weight);
+    my $rval = $dns->update_srv($id, $host, $port, $priority, $weight);
+    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('Invalid destination domain name')."\n") if($rval == -3);
+    die(gettext('Invalid port')."\n") if($rval == -4);
+    die(gettext('Invalid priority')."\n") if($rval == -5);
+    die(gettext('Invalid weight')."\n") if($rval == -6);
+    die(gettext('Database error')."\n") if($rval == -7);
+    die(gettext('Unknown error')."\n");
+}
+
+sub delete_srv {
+    my ($dns, $id) = @_;
+    die() unless(defined $dns && defined $id);
+    my $rval = $dns->delete_srv($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-17 20:32:37 UTC (rev 570)
+++ trunk/vhffs-api/src/Vhffs/Services/DNS.pm	2007-04-17 20:36:36 UTC (rev 571)
@@ -245,12 +245,27 @@
     my $mx = $sth->fetchall_hashref('id');
     push @params, $mx;
 
+    # Fetches SRV 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, 'SRV');
+    my $srv = {};
+    while(my $rr = $sth->fetchrow_hashref('NAME_lc')) {
+        my @fields = split(/ /, $rr->{data});
+        $rr->{weight} = shift(@fields);
+        $rr->{port} = shift(@fields);
+        $rr->{host} = join(' ', @fields);
+        delete $rr->{data};
+        $srv->{$rr->{id}} = $rr;
+    }
+    push @params, $srv;
+
     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) = @_;
+    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) = @_;
 
     my $self = $class->SUPER::_new($main, $oid, $owner_uid, $date_creation, $description, $state, Vhffs::Constants::TYPE_DNS);
 
@@ -269,6 +284,7 @@
     $self->{NS} = $nsr;
     $self->{CNAME} = $cname;
     $self->{MX} = $mx;
+    $self->{SRV} = $srv;
 
     return $self;
 }
@@ -471,6 +487,7 @@
 =pod
 
 =head2 update_mx
+
     $dns->update_mx($rr_id, $host);
 
 Replace address for MX record C<$rr_id>.
@@ -537,8 +554,133 @@
     return $id;
 }
 
+=pod
 
+=head2 add_srv
 
+    die('Unable to add SRV record') unless($dns->add_srv($protocol, $service, $host, $port, $priority, $weight);
+
+Add a SRV record to the $dns object. C<$protocol> and $service may start with an underscore or not.
+See IETF RFC 2782 for more details.
+
+=cut
+
+sub add_srv
+{
+    my ($self, $proto, $svc, $host, $port, $priority, $weight) = @_;
+    return -1 unless($proto =~ /^(?:_\w+)|(?:[^_]\w*)$/);
+    return -2 unless($svc =~ /^(?:_\w+)|(?:[^_]\w*)$/);
+    return -3 unless(Vhffs::Functions::check_domain_name($host, 1));
+    return -4 unless($port =~ /^\d+$/ && $port <= 65535 && $port > 0);
+    return -5 unless($priority =~ /^\d+$/ && $priority <= 65535 && $priority > 0);
+    return -6 unless($weight =~ /^\d+$/ && $weight <= 65535 && $weight > 0);
+
+    $proto = "_$proto" unless($proto =~ /^_/);
+    $proto = lc($proto);
+    $svc = "_$svc" unless($svc =~ /^_/);
+    $svc = lc($svc);
+    my $name = "$svc.$proto";
+    my $data = "$weight $port $host";
+    $host .= '.' unless(/[.]$/);
+
+
+    # Looks if this host is already registered for the same service
+    # and the same protocol.
+    my $sql = 'SELECT id FROM vhffs_dns_rr WHERE type=\'SRV\' AND name=? AND data LIKE ? AND zone=?';
+    my $dbh = $self->get_main()->get_db_object();
+    return -7 if($dbh->do($sql, undef, $name, "\%$host", $self->{dns_id}) != 0);
+
+    $sql = 'INSERT INTO vhffs_dns_rr(zone, name, type, data, aux, ttl) VALUES(?, ?, \'SRV\', ?, ?, 900)';
+    $dbh->do($sql, undef, $self->{dns_id}, $name, $data, $priority) or return -8;
+
+    my $id = $dbh->last_insert_id(undef, undef, 'vhffs_dns_rr', undef);
+    my $srv = {id => $id,
+              zone => $self->{dns_id},
+              name => $name,
+              type => 'SRV',
+              host => $host,
+              port => $port,
+              weight => $weight,
+              aux => $priority,
+              ttl => 900
+            };
+    $self->{SRV}->{$id} = $srv;
+
+    $self->add_history("Added an SRV record, $name -> $data - priority : $priority");
+
+    $self->update_serial();
+    return $id;
+
+}
+
+=pod
+
+=head2 update_srv
+
+    die("Unable to update SRV record #$id\n") unless($dns->update_srv($id, $newhost, $newport, $newpriority, $newweight) > 0);
+
+Updates data about SRV resource record which id is C<$id>.
+
+=cut
+
+sub update_srv {
+    my ($self, $id, $host, $port, $priority, $weight) = @_;
+    return -1 unless($id =~ /^\d+$/);
+    my $rr = $self->{SRV}{$id};
+    return -2 unless(defined $rr);
+    return -3 unless(Vhffs::Functions::check_domain_name($host, 1));
+    return -4 unless($port =~ /^\d+$/ && $port <= 65535 && $port > 0);
+    return -5 unless($priority =~ /^\d+$/ && $priority <= 65535 && $priority > 0);
+    return -6 unless($weight =~ /^\d+$/ && $weight <= 65535 && $weight > 0);
+
+    $host .= '.' unless(/[.]$/);
+
+    my $data = "$weight $port $host";
+
+    my $sql = 'UPDATE vhffs_dns_rr SET data = ?, aux = ? WHERE id = ? AND zone = ? AND type = \'SRV\'';
+    my $dbh = $self->get_main()->get_db_object();
+    $dbh->do($sql, undef, $data, $priority, $id, $self->{dns_id}) or return -7;
+
+    $rr->{aux} = $priority;
+    $rr->{weight} = $weight;
+    $rr->{host} = $host;
+    $rr->{port} = $port;
+
+    $self->add_history('Updated an SRV record, '.$rr->{name}." -> $data - priority : $priority");
+
+    $self->update_serial();
+
+
+}
+
+=pod
+
+=head2 delete_srv
+
+    die("Unable to delete SRV record #$id\n") unless($dns->delete_srv($id));
+
+Deletes SRV record which id is $id.
+
+=cut
+
+sub delete_srv
+{
+    my ($self, $id) = @_;
+
+    return -1 unless($id =~ /^\d+$/ );
+    return -2 unless(defined $self->{SRV}{$id});
+
+    my $dbh = $self->get_main->get_db_object();
+    my $sql = 'DELETE FROM vhffs_dns_rr WHERE type = \'SRV\' AND zone = ? AND id = ?';
+    $dbh->do($sql, undef, $self->{dns_id}, $id) or return -3;
+
+    $self->add_history('Deleted SRV record '.$self->{SRV}{$id}{name});
+    delete $self->{SRV}{$id};
+
+    $self->update_serial();
+    return 1;
+}
+
 sub update_cname
 {
     my ($self, $id, $dest) = @_;
@@ -692,6 +834,11 @@
     return $self->{NS};
 }
 
+sub get_srv_type {
+    my $self = shift;
+    return $self->{SRV};
+}
+
 sub get_soa_ns
 {
     my $self = shift;

Modified: trunk/vhffs-panel/dns/prefs.pl
===================================================================
--- trunk/vhffs-panel/dns/prefs.pl	2007-04-17 20:32:37 UTC (rev 570)
+++ trunk/vhffs-panel/dns/prefs.pl	2007-04-17 20:36:36 UTC (rev 571)
@@ -170,6 +170,29 @@
             eval { Vhffs::Panel::DNS::add_cname($dns, $name, $data); };
             if($@) { $panel->add_error(sprintf(gettext('Unable to add CNAME record: %s'), $@)); }
             else { $panel->add_info(gettext('CNAME Record added')); }
+        } elsif($action eq 'manage_srv') {
+            if(defined $cgi->param('modify_srv_submit')) {
+                my $host = $cgi->param('host');
+                my $port = $cgi->param('port');
+                my $weight = $cgi->param('weight');
+                eval { Vhffs::Panel::DNS::update_srv($dns, $id, $host, $port, $aux, $weight); };
+                if($@) { $panel->add_error(sprintf(gettext('Unable to modify SRV record: %s'), $@)); }
+                else { $panel->add_info(gettext('SRV Record updated')); }
+            } else {
+                eval { Vhffs::Panel::DNS::delete_srv($dns, $id); };
+                 if($@) { $panel->add_error(sprintf(gettext('Unable to delete SRV record: %s'), $@)); }
+                 else { $panel->add_info(gettext('SRV Record deleted')); }
+            }
+        }elsif($action eq 'add_srv') {
+            my $proto = $cgi->param('protocol');
+            my $svc = $cgi->param('service');
+            my $host = $cgi->param('host');
+            my $port = $cgi->param('port');
+            my $aux = $cgi->param('aux');
+            my $weight = $cgi->param('weight');
+            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')); }
         }
     }
 }
@@ -177,6 +200,7 @@
 	my $a_rr = $dns->get_a_type;
 	my $mx = $dns->get_mx_type;
 	my $ns = $dns->get_ns_type;
+    my $srv = $dns->get_srv_type;
 
     $panel->set_title(sprintf(gettext("DNS Administration - %s"), $domain_name));
 	$template = new HTML::Template( filename => $templatedir."/dns/prefs.tmpl" );
@@ -236,8 +260,17 @@
     $template->param( TEXT_ADD_NS => gettext("Add a NS field to your domain") );
     $template->param( NS_HOSTNAME_TEXT => gettext("IP/host of your NS") );
 
+    #Treat SRV part
+    $template->param( TEXT_TYPESRV => gettext('SRV TYPE') );
+    $template->param( TEXT_LIST_SRV => gettext('All SRV records for your domain name') );
+    $template->param( TEXT_ADD_SRV => gettext('Add a SRV record to your domain') );
+    $template->param( SRV_PROTOCOL_TEXT => gettext('Protocol') );
+    $template->param( SRV_SERVICE_TEXT => gettext('Service') );
+    $template->param( SRV_HOSTNAME_TEXT => gettext('Host') );
+    $template->param( SRV_PORT_TEXT => gettext('Port') );
+    $template->param( SRV_PRIORITY_TEXT => gettext('Priority') );
+    $template->param( SRV_WEIGHT_TEXT => gettext('Weight') );
 
-
     $template->param( TEXT_SUBMIT => gettext("Submit") );
 
 
@@ -296,6 +329,22 @@
         $template->param( LIST_NS => gettext( "No NS available on this domain") );
     }
 
+    if( scalar(keys %{$srv}) ) {
+        my @list_srv = sort {$a->{data} cmp $b->{data}} values(%{$srv});
+        $thirdtemplate = new HTML::Template( filename => $templatedir.'/dns/list_srv_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( PORT_TXT => gettext( 'Port' ) );
+        $thirdtemplate->param( PRIORITY_TXT => gettext( 'Priority' ) );
+        $thirdtemplate->param( WEIGHT_TXT => gettext( 'Weight' ) );
+        $thirdtemplate->param( SRV_RR => \@list_srv );
+        $template->param( LIST_SRV => $thirdtemplate->output );
+    } else {
+        $template->param( LIST_SRV => gettext( "No SRV 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-17 20:32:37 UTC (rev 570)
+++ trunk/vhffs-panel/templates/Makefile.am	2007-04-17 20:36:36 UTC (rev 571)
@@ -96,6 +96,7 @@
 	dns/list_cname_sub.tmpl \
 	dns/list_mx_sub.tmpl \
 	dns/list_ns_sub.tmpl \
+	dns/list_srv_sub.tmpl \
 	dns/list_sub.tmpl \
 	dns/menu_sub.tmpl \
 	dns/menu_sub_wait.tmpl \

Modified: trunk/vhffs-panel/templates/dns/prefs.tmpl
===================================================================
--- trunk/vhffs-panel/templates/dns/prefs.tmpl	2007-04-17 20:32:37 UTC (rev 570)
+++ trunk/vhffs-panel/templates/dns/prefs.tmpl	2007-04-17 20:36:36 UTC (rev 571)
@@ -97,8 +97,30 @@
     <input type="submit" value="<TMPL_VAR NAME="TEXT_SUBMIT">" class="autowidth"/>
 </form>
 
+<h2><tmpl_var name="TEXT_TYPESRV"></h2>
+<h3><tmpl_var name="TEXT_LIST_SRV"></h3>
+<tmpl_var name="LIST_SRV">
+<h3><tmpl_var name="TEXT_ADD_SRV"></h3>
+<form method="post" action="prefs.pl">
+    <p><label for="add_srv_protocol"><tmpl_var name="SRV_PROTOCOL_TEXT"></label>
+        <input type="text" name="protocol" id="add_srv_protocol"/></p>
+    <p><label for="add_srv_service"><tmpl_var name="SRV_SERVICE_TEXT"></label>
+        <input type="text" name="service" id="add_srv_service"/></p>
+   <p><label for="add_srv_data"><tmpl_var name="SRV_HOSTNAME_TEXT"></label>
+        <input type="text" name="host" id="add_srv_data"/></p>
+    <p><label for="add_srv_port"><tmpl_var name="SRV_PORT_TEXT"></label>
+        <input type="text" name="port" id="add_srv_port" size="5" maxlength="5"/></p>
+    <p><label for="add_srv_priority"><tmpl_var name="SRV_PRIORITY_TEXT"></label>
+        <input type="text" name="aux" id="add_srv_priority" size="5" maxlength="5"/></p>
+    <p><label for="add_srv_weight"><tmpl_var name="SRV_WEIGHT_TEXT"></label>
+        <input type="text" name="weight" id="add_srv_weight" size="5" maxlength="5"/></p>
+    <p class="button">
+        <input type="hidden" name="action" value="add_srv" />
+        <input type="hidden" name="domain" value="<TMPL_VAR NAME="DOMAIN_NAME">" />
+        <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">


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/