[vhffs-dev] [1336] we can now create subdomains for NS, SRV, MX types through API

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


Revision: 1336
Author:   gradator
Date:     2009-02-08 04:34:53 +0100 (Sun, 08 Feb 2009)

Log Message:
-----------
we can now create subdomains for NS, SRV, MX types through API

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-tools/src/vhffs-managedns


Modified: trunk/vhffs-api/src/Vhffs/Panel/DNS.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Panel/DNS.pm	2009-02-08 00:15:35 UTC (rev 1335)
+++ trunk/vhffs-api/src/Vhffs/Panel/DNS.pm	2009-02-08 03:34:53 UTC (rev 1336)
@@ -242,14 +242,15 @@
 }
 
 sub add_mx {
-    my ($dns, $host, $priority) = @_;
-    die() unless(defined $dns && defined $host && defined $priority);
-    my $rval = $dns->add_mx($host, $priority);
+    my ($dns, $name, $host, $priority) = @_;
+    die() unless(defined $dns && defined $name && defined $host && defined $priority);
+    my $rval = $dns->add_mx( $name, $host, $priority);
     return 1 if($rval > 0);
     die(gettext('Invalid hostname')."\n") if($rval == -1);
     die(gettext('Invalid priority')."\n") if($rval == -2);
     die(gettext('An MX record with the same name already exists for this domain')."\n") if($rval == -3);
     die(gettext('Database error')."\n") if($rval == -4);
+    die(gettext('Invalid prefix')."\n") if($rval == -5);
     die(gettext('Unknown error')."\n");
 }
 
@@ -266,13 +267,14 @@
 }
 
 sub add_ns {
-    my ($dns, $host) = @_;
-    die() unless(defined $dns && defined $host);
-    my $rval = $dns->add_ns($host);
+    my ($dns, $name, $host) = @_;
+    die() unless(defined $dns && defined $name && defined $host);
+    my $rval = $dns->add_ns($name, $host);
     return 1 if($rval > 0);
     die(gettext('Invalid hostname')."\n") if($rval == -1);
     die(gettext('An NS record with the same name already exists for this domain')."\n") if($rval == -2);
     die(gettext('Database error')."\n") if($rval == -3);
+    die(gettext('Invalid prefix')."\n") if($rval == -5);
     die(gettext('Unknown error')."\n");
 }
 
@@ -302,9 +304,9 @@
 }
 
 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);
+    my ($dns, $name, $proto, $svc, $host, $port, $priority, $weight) = @_;
+    die() unless(defined $dns && defined $name && defined $proto && defined $svc && defined $host && defined $port && defined $priority && defined $weight);
+    my $rval = $dns->add_srv($name, $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);
@@ -312,8 +314,9 @@
     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('Invalid record')."\n") if($rval == -7);
+    die(gettext('This host is already registered for this service')."\n") if($rval == -8);
+    die(gettext('Database error')."\n") if($rval == -9);
     die(gettext('Unknown error')."\n");
 }
 

Modified: trunk/vhffs-api/src/Vhffs/Services/DNS.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Services/DNS.pm	2009-02-08 00:15:35 UTC (rev 1335)
+++ trunk/vhffs-api/src/Vhffs/Services/DNS.pm	2009-02-08 03:34:53 UTC (rev 1336)
@@ -157,13 +157,13 @@
             foreach( keys %{$init->{mx}} ) {
                 $prio = $_;
                 $ip = $init->{mx}{$_};
-                $self->add_mx( $ip , $prio );
+                $self->add_mx( '@', $ip , $prio );
             }
         }    
         if( defined $init->{ns} ) {
             foreach( keys %{$init->{ns}} ) {
                 $name = $_;
-                $self->add_ns( $name );
+                $self->add_ns( '@', $name );
             }
         }
     }
@@ -426,24 +426,26 @@
 
 sub add_ns
 {
-    my ($self, $host, $ttl) = @_;
+    my ($self, $name, $host, $ttl) = @_;
     
-    $ttl = 900 if ( ! defined $ttl );
-    
+    $ttl = 900 unless defined $ttl;
+    return -5 unless check_rr_name($name);
+    $name = '' if $name eq '@';
+
     return -1 unless( Vhffs::Functions::check_domain_name($host) || ( $host =~ /[a-z0-9\-]{1,63}/ ) );
 
-    my $sql = 'SELECT * FROM vhffs_dns_rr WHERE type=\'NS\' AND data=? AND zone=?';
+    my $sql = 'SELECT * FROM vhffs_dns_rr WHERE zone=? AND type=\'NS\' AND name=? AND data=?';
     my $dbh = $self->get_main->get_db_object();
-    return -2 if($dbh->do($sql, undef, $host, $self->{dns_id}) != 0);
+    return -2 if($dbh->do($sql, undef, $self->{dns_id}, $name, $host) != 0);
 
-    $sql = 'INSERT INTO vhffs_dns_rr(zone, name, type, data, aux, ttl) VALUES(?, \'\', \'NS\', ?, 0, ?)';
-    $dbh->do($sql, undef, $self->{dns_id}, $host, $ttl) or return -3;
+    $sql = 'INSERT INTO vhffs_dns_rr(zone, name, type, data, aux, ttl) VALUES(?, ?, \'NS\', ?, 0, ?)';
+    $dbh->do($sql, undef, $self->{dns_id}, $name, $host, $ttl) or return -3;
 
     my $id = $dbh->last_insert_id(undef, undef, 'vhffs_dns_rr', undef);
 
     my $ns = {id => $id,
               zone => $self->{dns_id},
-              name => '',
+              name => $name,
               type => 'NS',
               data => $host,
               aux => 0,
@@ -453,7 +455,7 @@
 
     $self->update_serial;
 
-    $self->add_history("Added a NS record ($host)");
+    $self->add_history('Added a NS record with name '.($name or '@').' pointing to '.$host);
     return $id;
 }
 
@@ -467,9 +469,9 @@
 {
     my ( $self , $name , $ip , $ttl ) = @_;
     
-    $ttl = 900 if ( ! defined $ttl );
-    return -1 if( ! check_rr_name($name) );
-    $name = '' if( $name eq '@' );
+    $ttl = 900 unless defined $ttl;
+    return -1 unless check_rr_name($name);
+    $name = '' if $name eq '@';
     return -2 if ( $self->name_exists( $name, 'A', 'CNAME' ) != 0 );
 
     if( ! defined $ip ) {
@@ -541,33 +543,35 @@
 
 sub add_mx
 {
-    my ($self, $host, $aux, $ttl) = @_;
+    my ($self, $name, $host, $priority, $ttl) = @_;
 
-    $ttl = 900 if ( ! defined $ttl );
-    $aux = 10 if ( !defined $aux );
-    
+    $ttl = 900 unless defined $ttl;
+    $priority = 10 unless defined $priority;
+    return -5 unless check_rr_name($name);
+    $name = '' if $name eq '@';
+
     return -1 unless( Vhffs::Functions::check_domain_name($host, 1) || check_rr_name($host) );
-    return -2 unless( $aux =~ /^\d+$/ );
+    return -2 unless( $priority =~ /^\d+$/ );
 
-    my $sql = 'SELECT id FROM vhffs_dns_rr WHERE type=\'MX\' AND data=? AND zone=?';
+    my $sql = 'SELECT id FROM vhffs_dns_rr WHERE zone=? AND type=\'MX\' AND name=? AND data=?';
     my $dbh = $self->get_main()->get_db_object();
-    return -3 if($dbh->do($sql, undef, $host, $self->{dns_id}) != 0); 
+    return -3 if($dbh->do($sql, undef, $self->{dns_id}, $name, $host) != 0); 
 
-    $sql = 'INSERT INTO vhffs_dns_rr(zone, name, type, data, aux, ttl) VALUES(?, \'\', \'MX\', ?, ?, ?)';
-    $dbh->do($sql, undef, $self->{dns_id}, $host, $aux, $ttl) or return -4;
+    $sql = 'INSERT INTO vhffs_dns_rr(zone, name, type, data, aux, ttl) VALUES(?, ?, \'MX\', ?, ?, ?)';
+    $dbh->do($sql, undef, $self->{dns_id}, $name, $host, $priority, $ttl) or return -4;
     
     my $id = $dbh->last_insert_id(undef, undef, 'vhffs_dns_rr', undef);
     my $mx = {id => $id,
               zone => $self->{dns_id},
-              name => '',
+              name => $name,
               type => 'MX',
               data => $host,
-              aux => $aux,
+              aux => $priority,
               ttl => $ttl
             };
     $self->{MX}->{$id} = $mx;
 
-    $self->add_history("Added an MX record, host : $host - priority : $aux");
+    $self->add_history('Added an MX record, name: '.($name or '@').' - exchanger: '.$host.' - priority: '.$priority);
     
     $self->update_serial();
     return $id;
@@ -586,19 +590,22 @@
 
 sub add_srv
 {
-    my ($self, $proto, $svc, $host, $port, $priority, $weight) = @_;
+    my ($self, $name, $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) || ( check_rr_name($host) ) );
     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);
+    return -7 unless check_rr_name($name);
 
     $proto = "_$proto" unless($proto =~ /^_/);
     $proto = lc($proto);
     $svc = "_$svc" unless($svc =~ /^_/);
     $svc = lc($svc);
-    my $name = "$svc.$proto";
+    $name = '' if $name eq '@';
+    $name = $svc.'.'.$proto.'.'.$name;
+    $name =~ s/\.$//;
     my $data = "$weight $port $host";
 
 
@@ -606,10 +613,10 @@
     # 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);
+    return -8 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;
+    $dbh->do($sql, undef, $self->{dns_id}, $name, $data, $priority) or return -9;
 
     my $id = $dbh->last_insert_id(undef, undef, 'vhffs_dns_rr', undef);
     my $srv = {id => $id,
@@ -683,9 +690,9 @@
 {
     my ( $self , $name , $ip , $ttl ) = @_;
     
-    $ttl = 900 if ( ! defined $ttl );
-    return -1 if( ! check_rr_name($name) );
-    $name = '' if( $name eq '@' );
+    $ttl = 900 unless defined $ttl;
+    return -1 unless check_rr_name($name);
+    $name = '' if $name eq '@';
     return -2 if ( $self->name_exists( $name, 'CNAME', 'AAAA' ) != 0 );
 
     if( ! defined $ip ) {

Modified: trunk/vhffs-panel/dns/prefs.pl
===================================================================
--- trunk/vhffs-panel/dns/prefs.pl	2009-02-08 00:15:35 UTC (rev 1335)
+++ trunk/vhffs-panel/dns/prefs.pl	2009-02-08 03:34:53 UTC (rev 1336)
@@ -147,7 +147,7 @@
                 else { $panel->add_info(gettext('MX Record deleted')); }
             }
         } elsif($action eq 'add_mx') {
-            eval { Vhffs::Panel::DNS::add_mx($dns, $data, $aux); };
+            eval { Vhffs::Panel::DNS::add_mx($dns, '@', $data, $aux); };
             if($@) { $panel->add_error(sprintf(gettext('Unable to add MX record: %s'), $@)); }
             else { $panel->add_info(gettext('MX Record added')); }
         } elsif($action eq 'manage_ns') {
@@ -156,7 +156,7 @@
             if($@) { $panel->add_error(sprintf(gettext('Unable to delete NS record: %s'), $@)); }
             else { $panel->add_info(gettext('NS Record deleted')); }
         } elsif($action eq 'add_ns') {
-            eval { Vhffs::Panel::DNS::add_ns($dns, $data); };
+            eval { Vhffs::Panel::DNS::add_ns($dns, '@', $data); };
             if($@) { $panel->add_error(sprintf(gettext('Unable to add NS record: %s'), $@)); }
             else { $panel->add_info(gettext('NS Record added')); }
         } elsif($action eq 'manage_cname') {
@@ -193,7 +193,7 @@
             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); };
+            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') {

Modified: trunk/vhffs-tools/src/vhffs-managedns
===================================================================
--- trunk/vhffs-tools/src/vhffs-managedns	2009-02-08 00:15:35 UTC (rev 1335)
+++ trunk/vhffs-tools/src/vhffs-managedns	2009-02-08 03:34:53 UTC (rev 1336)
@@ -513,7 +513,7 @@
 sub create_ns_rr {
 	my $domain = shift;
 	my $name = $w_new_rr->getobj('new_rr_name')->get();
-	my $rval = $domain->add_ns( $name );
+	my $rval = $domain->add_ns( '@', $name );
 
 	$ui->error('Invalid name (must be an hostname)') if($rval == -1);
 	$ui->error('DB error while adding NS record') if($rval == -2);
@@ -527,7 +527,7 @@
 	my $domain = shift;
 	my $name = $w_new_rr->getobj('new_rr_name')->get();
 	my $priority = $w_new_rr->getobj('new_rr_priority')->get();
-	my $rval = $domain->add_mx( $name, $priority );
+	my $rval = $domain->add_mx( '@', $name, $priority );
 
 	$ui->error('Invalid name (must be an hostname') if($rval == -1);
 	$ui->error('Invalid priority (must be an integer)') if($rval == -2);


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