]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - html/cgi-bin/ddns.cgi
ddns.cgi: Allow enabling/disabling entries.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / ddns.cgi
index 3814ad7d2647e71258543bab4c40b1fceea41194..80126205ee3de4cb61207510c013f75ffe838a79 100644 (file)
@@ -29,12 +29,6 @@ require '/var/ipfire/general-functions.pl';
 require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
 
-# Hook to regenerate the configuration files, if cgi got called from command line.
-if ($ENV{"REMOTE_ADDR"} eq "") {
-       &GenerateDDNSConfigFile();
-       exit(0);
-}
-
 #workaround to suppress a warning when a variable is used only once
 my @dummy = ( ${Header::table2colour}, ${Header::colouryellow} );
 undef (@dummy);
@@ -53,7 +47,7 @@ my $datafile = "${General::swroot}/ddns/config";
 # Dynamic ddns programm call.
 my @ddnsprog = ("/usr/bin/ddns", "--config",
                "/var/ipfire/ddns/ddns.conf",
-               "update-all", "--force" );
+               "update-all");
 
 my %settings=();
 my $errormessage = '';
@@ -72,6 +66,15 @@ $settings{'SERVICE'} = '';
 
 $settings{'ACTION'} = '';
 
+# Get supported ddns providers.
+my @providers = &GetProviders();
+
+# Hook to regenerate the configuration files, if cgi got called from command line.
+if ($ENV{"REMOTE_ADDR"} eq "") {
+       &GenerateDDNSConfigFile();
+       exit(0);
+}
+
 &Header::showhttpheaders();
 
 #Get GUI values
@@ -82,9 +85,6 @@ open(FILE, "$datafile") or die "Unable to open $datafile.";
 my @current = <FILE>;
 close (FILE);
 
-# Get supported ddns providers.
-my @providers = &GetProviders();
-
 #
 # Save General Settings.
 #
@@ -187,7 +187,7 @@ if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settings{'ACTION'} eq $Lang::
 
        # Check if a password has been typed in.
        # freedns.afraid.org does not require this field.
-       if (($settings{'PASSWORD'} eq '') && ($settings{'SERVICE'} ne 'freedns.afraid.org')) {
+       if (($settings{'PASSWORD'} eq '') && ($settings{'SERVICE'} ne 'freedns.afraid.org') && ($settings{'SERVICE'} ne 'regfish.com')) {
                $errormessage = $Lang::tr{'password not set'};
        }
 
@@ -197,6 +197,12 @@ if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settings{'ACTION'} eq $Lang::
                # Splitt hostname field into 2 parts for storrage.
                my($hostname, $domain) = split(/\./, $settings{'HOSTNAME'}, 2);
 
+               # Handle enabled checkbox. When the checkbox is selected a "on" will be returned,
+               # if the checkbox is not checked nothing is returned in this case we set the value to "off".
+               if ($settings{'ENABLED'} ne 'on') {
+                       $settings{'ENABLED'} = 'off';
+               }
+
                # Handle adding new accounts.
                if ($settings{'ACTION'} eq $Lang::tr{'add'}) {
 
@@ -215,8 +221,6 @@ if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settings{'ACTION'} eq $Lang::
                        # Write out notice to logfile.
                        &General::log($Lang::tr{'ddns hostname added'});
 
-                       # Update ddns config file.
-
                # Handle account edditing.
                } elsif ($settings{'ACTION'} eq $Lang::tr{'update'}) {
 
@@ -354,7 +358,9 @@ $checked{'BEHINDROUTER'}{'RED_IP'} = '';
 $checked{'BEHINDROUTER'}{'FETCH_IP'} = '';
 $checked{'BEHINDROUTER'}{$settings{'BEHINDROUTER'}} = "checked='checked'";
 
-$checked{'ENABLED'}{'on'} = ($settings{'ENABLED'} eq '' ) ? '' : "checked='checked'";
+$checked{'ENABLED'}{'on'} = '';
+$checked{'ENABLED'}{'off'} = '';
+$checked{'ENABLED'}{$settings{'ENABLED'}} = "checked='checked'";
 
 # Show box for errormessages..
 if ($errormessage) {
@@ -450,9 +456,9 @@ print <<END
        </tr>
 
        <tr>
-               <td class='base'></td>
-               <td></td>
-               <td class='base'>$Lang::tr{'username'}:</td>
+               <td class='base'>$Lang::tr{'enabled'}</td>
+               <td><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
+               <td class='base'>$Lang::tr{'username'}</td>
                <td><input type='text' name='LOGIN' value='$settings{'LOGIN'}' /></td>
        </tr>
 
@@ -462,13 +468,6 @@ print <<END
                <td class='base'>$Lang::tr{'password'}</td>
                <td><input type='password' name='PASSWORD' value='$settings{'PASSWORD'}' /></td>
        </tr>
-
-       <tr>
-               <td class='base'>$Lang::tr{'enabled'}</td>
-               <td><input type='checkbox' name='ENABLED' value='on' $checked{'ENABLED'}{'on'} /></td>
-               <td class='base'></td>
-               <td></td>
-       </tr>
 </table>
 <br>
 <hr>
@@ -487,9 +486,20 @@ END
 
 ##
 # Third section, display all created ddns hosts.
+# Re-open file to get changes.
+open(FILE, $datafile) or die "Unable to open $datafile.";
+@current = <FILE>;
+close(FILE);
 
-&Header::openbox('100%', 'left', $Lang::tr{'current hosts'});
-print <<END
+# Get IP address of the red interface.
+my $ip = &General::GetDyndnsRedIP();
+my $id = 0;
+my $toggle_enabled;
+
+if (@current) {
+       &Header::openbox('100%', 'left', $Lang::tr{'current hosts'});
+
+       print <<END;
 <table width='100%' class='tbl'>
        <tr>
                <th width='30%' align='center' class='boldbase'><b>$Lang::tr{'service'}</b></th>
@@ -497,55 +507,58 @@ print <<END
                <th width='20%' colspan='3' class='boldbase' align='center'><b>$Lang::tr{'action'}</b></th>
        </tr>
 END
-;
 
-# Re-open file to get changes.
-open(FILE, $datafile) or die "Unable to open $datafile.";
-@current = <FILE>;
-close(FILE);
+       foreach my $line (@current) {
+               # Remove newlines.
+               chomp(@current);
+               my @temp = split(/\,/,$line);
 
-# Get IP address of the red interface.
-my $ip = &General::GetDyndnsRedIP;
-my $id = 0;
-my $toggle_enabled;
+               # Handle hostname details. Only connect the values with a dott if both are available.
+               my $hostname="";
 
-foreach my $line (@current) {
+               if (($temp[1]) && ($temp[2])) {
+                       $hostname="$temp[1].$temp[2]";
+               } else {
+                       $hostname="$temp[1]";
+               }
 
-       # Remove newlines.
-       chomp(@current);
-       my @temp = split(/\,/,$line);
+               # Generate value for enable/disable checkbox.
+               my $sync = '';
+               my $gif = '';
+               my $gdesc = '';
 
-       # Generate value for enable/disable checkbox.
-       my $sync = "<font color='blue'>";
-       my $gif = '';
-       my $gdesc = '';
+               if ($temp[7] eq "on") {
+                       $gif = 'on.gif';
+                       $gdesc = $Lang::tr{'click to disable'};
 
-       if ($temp[7] eq "on") {
-               $gif = 'on.gif';
-               $gdesc = $Lang::tr{'click to disable'};
-               $sync = (&General::DyndnsServiceSync ($ip,$temp[1], $temp[2]) ? "<font color='green'>": "<font color='red'>") ;
-               $toggle_enabled = 'off';
-       } else {
-               $gif = 'off.gif';
-               $gdesc = $Lang::tr{'click to enable'};
-               $toggle_enabled = 'on';
-       }
+                       # Check if the given hostname is a FQDN before doing a nslookup.
+                       if (&General::validfqdn($hostname)) {
+                               $sync = (&General::DyndnsServiceSync ($ip,$temp[1], $temp[2]) ? "<font color='green'>": "<font color='red'>") ;
+                       }
 
-       # Background color.
-       my $col="";
+                       $toggle_enabled = 'off';
+               } else {
+                       $sync = "<font color='blue'>";
+                       $gif = 'off.gif';
+                       $gdesc = $Lang::tr{'click to enable'};
+                       $toggle_enabled = 'on';
+               }
 
-       if ($settings{'ID'} eq $id) {
-               $col="bgcolor='${Header::colouryellow}'";
-       } elsif (!&General::is_part_of("$temp[0]", @providers)) {
-               $col="bgcolor='#FF4D4D'";
-       } elsif ($id % 2) {
-               $col="bgcolor='$color{'color20'}'";
-       } else {
-               $col="bgcolor='$color{'color22'}'";
-       }
+               # Background color.
+               my $col="";
 
-# The following HTML Code still is part of the loop.
-print <<END
+               if ($settings{'ID'} eq $id) {
+                       $col="bgcolor='${Header::colouryellow}'";
+               } elsif (!($temp[0] ~~ @providers)) {
+                       $col="bgcolor='#FF4D4D'";
+               } elsif ($id % 2) {
+                       $col="bgcolor='$color{'color20'}'";
+               } else {
+                       $col="bgcolor='$color{'color22'}'";
+               }
+
+               # The following HTML Code still is part of the loop.
+               print <<END;
 <tr>
        <td align='center' $col><a href='http://$temp[0]'>$temp[0]</a></td>
        <td align='center' $col>$sync$temp[1].$sync$temp[2]</td>
@@ -570,14 +583,11 @@ print <<END
        </form></td>
 </tr>
 END
-;
-    $id++;
-}
-print "</table>";
+               $id++;
+       }
 
-# If table contains entries, print 'Key to action icons'
-if ($id) {
-print <<END
+       print <<END;
+</table>
 <table width='100%'>
        <tr>
                <td class='boldbase'>&nbsp;<b>$Lang::tr{'legend'}:&nbsp;</b></td>
@@ -598,10 +608,10 @@ print <<END
        </tr>
 </table>
 END
-;
+
+       &Header::closebox();
 }
 
-&Header::closebox();
 &Header::closebigbox();
 &Header::closepage();
 
@@ -641,16 +651,17 @@ sub GenerateDDNSConfigFile {
 
        while (<SETTINGS>) {
                my $line = $_;
+               chomp($line);
 
                # Generate array based on the line content (seperator is a single or multiple space's)
                my @settings = split(/,/, $line);
                my ($provider, $hostname, $domain, $proxy, $wildcards, $username, $password, $enabled) = @settings;
 
                # Skip entries if they are not (longer) supported.
-               next if (!&General::is_part_of("$provider", @providers));
+               next unless ($provider ~~ @providers);
 
                # Skip disabled entries.
-               next if ($enabled eq "off");
+               next unless ($enabled eq "on");
 
                print FILE "[$hostname.$domain]\n";
                print FILE "provider = $provider\n";
@@ -658,11 +669,11 @@ sub GenerateDDNSConfigFile {
                my $use_token = 0;
 
                # Handle token based auth for various providers.
-               if ($provider ~~ ["dns.lightningwirelabs.com", "regfish.com"] && $username eq "token") {
+               if ($provider ~~ ["dns.lightningwirelabs.com", "entrydns.net", "regfish.com"] && $username eq "token") {
                        $use_token = 1;
 
-               # Handle token auth for freedns.afraid.org.
-               } elsif ($provider eq "freedns.afraid.org" && $password eq "") {
+               # Handle token auth for freedns.afraid.org and regfish.com.
+               } elsif ($provider ~~ ["freedns.afraid.org", "regfish.com"] && $password eq "") {
                        $use_token = 1;
                        $password = $username;
 
@@ -691,7 +702,7 @@ sub GenerateDDNSConfigFile {
                }
 
                # These providers need to be set to only use IPv4.
-               if ($provider ~~ ["freedns.afraid.org", "nsupdate.info", "variomedia.de", "zoneedit.com"]) {
+               if ($provider ~~ ["freedns.afraid.org", "nsupdate.info", "opendns.com", "variomedia.de", "zoneedit.com"]) {
                        print FILE "proto = ipv4\n";
                }