]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - html/cgi-bin/ddns.cgi
Merge remote-tracking branch 'mfischer/slang' into next
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / ddns.cgi
index 64ed3dc5040e2faf2e8a63aa7dc51358a5ef3133..73a41d903fae41b47024f38ce18237adca36556c 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);
@@ -50,10 +44,8 @@ my $settingsfile = "${General::swroot}/ddns/settings";
 # Config file to store the configured ddns providers.
 my $datafile = "${General::swroot}/ddns/config";
 
-# Dynamic ddns programm call.
-my @ddnsprog = ("/usr/bin/ddns", "--config",
-               "/var/ipfire/ddns/ddns.conf",
-               "update-all", "--force" );
+# Call the ddnsctrl helper binary to perform the update.
+my @ddnsprog = ("/usr/local/bin/ddnsctrl", "update-all");
 
 my %settings=();
 my $errormessage = '';
@@ -72,6 +64,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,14 +83,10 @@ open(FILE, "$datafile") or die "Unable to open $datafile.";
 my @current = <FILE>;
 close (FILE);
 
-# Get supported ddns providers.
-my @providers = &GetProviders();
-
 #
 # Save General Settings.
 #
 if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
-
        # Open /var/ipfire/ddns/settings for writing.
        open(FILE, ">$settingsfile") or die "Unable to open $settingsfile.";
 
@@ -104,9 +101,6 @@ if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
        # Close file after writing.
        close(FILE);
 
-       # Unset given CGI parmas.
-       undef %settings;
-
        # Update ddns config file.
        &GenerateDDNSConfigFile();
 }
@@ -115,7 +109,6 @@ if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
 # Toggle enable/disable field.  Field is in second position
 #
 if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
-
        # Open /var/ipfire/ddns/config for writing.
        open(FILE, ">$datafile") or die "Unable to open $datafile.";
 
@@ -127,23 +120,19 @@ if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
 
        # Read file line by line.
        foreach my $line (@current) {
-
                # Remove newlines.
                chomp($line);
 
                if ($settings{'ID'} eq $id) {
-
                        # Splitt lines (splitting element is a single ",") and save values into temp array.
                        @temp = split(/\,/,$line);
 
                        # Check if we want to toggle ENABLED or WILDCARDS.
                        if ($settings{'ENABLED'} ne '') {
-
                                # Update ENABLED.
                                print FILE "$temp[0],$temp[1],$temp[2],$temp[3],$temp[4],$temp[5],$temp[6],$settings{'ENABLED'}\n";
                        }
                } else {
-
                        # Print unmodified line.
                        print FILE "$line\n";
                }
@@ -151,13 +140,11 @@ if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
                # Increase $id.
                $id++;
        }
+       undef $settings{'ID'};
 
        # Close file after writing.
        close(FILE);
 
-       # Unset given CGI params.
-       undef %settings;
-
        # Write out logging notice.
        &General::log($Lang::tr{'ddns hostname modified'});
 
@@ -169,7 +156,6 @@ if ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
 # Add new accounts, or edit existing ones.
 #
 if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settings{'ACTION'} eq $Lang::tr{'update'})) {
-
        # Check if a hostname has been given.
        if ($settings{'HOSTNAME'} eq '') {
                $errormessage = $Lang::tr{'hostname not set'};
@@ -187,19 +173,23 @@ 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'};
        }
 
        # Go furter if there was no error.
-       if ( ! $errormessage) {
-
+       if (!$errormessage) {
                # 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'}) {
-
                        # Open /var/ipfire/ddns/config for writing.
                        open(FILE, ">>$datafile") or die "Unable to open $datafile.";
 
@@ -215,11 +205,8 @@ 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'}) {
-
                        # Open /var/ipfire/ddns/config for writing.
                        open(FILE, ">$datafile") or die "Unable to open $datafile.";
 
@@ -230,7 +217,6 @@ if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settings{'ACTION'} eq $Lang::
 
                        # Read file line by line.
                        foreach my $line (@current) {
-
                                if ($settings{'ID'} eq $id) {
                                        print FILE "$settings{'SERVICE'},$hostname,$domain,$settings{'PROXY'},$settings{'WILDCARDS'},$settings{'LOGIN'},$settings{'PASSWORD'},$settings{'ENABLED'}\n";
                                } else {
@@ -247,9 +233,7 @@ if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settings{'ACTION'} eq $Lang::
                        # Write out notice to logfile.
                        &General::log($Lang::tr{'ddns hostname modified'});
                }
-
-               # Unset given CGI params.
-               undef %settings;
+               undef $settings{'ID'};
 
                # Update ddns config file.
                &GenerateDDNSConfigFile();
@@ -260,7 +244,6 @@ if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settings{'ACTION'} eq $Lang::
 # Remove existing accounts.
 #
 if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
-
        # Open /var/ipfire/ddns/config for writing.
        open(FILE, ">$datafile") or die "Unable to open $datafile.";
 
@@ -271,7 +254,6 @@ if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
 
        # Read file line by line.
        foreach my $line (@current) {
-
                # Write back every line, except the one we want to drop
                # (identified by the ID)
                unless ($settings{'ID'} eq $id) {
@@ -281,13 +263,11 @@ if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
                # Increase id.
                $id++;
        }
+       undef $settings{'ID'};
 
        # Close file after writing.
        close(FILE);
 
-       # Unset given CGI params.
-       undef %settings;
-
        # Write out notice to logfile.
        &General::log($Lang::tr{'ddns hostname removed'});
 
@@ -299,33 +279,41 @@ if ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
 # Read items for editing.
 #
 if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
-
        my $id = 0;
        my @temp;
 
        # Read file line by line.
        foreach my $line (@current) {
-
                if ($settings{'ID'} eq $id) {
-
                        # Remove newlines.
                        chomp($line);
 
                        # Splitt lines (splitting element is a single ",") and save values into temp array.
                        @temp = split(/\,/,$line);
 
+                       # Handle hostname details. Only connect the values with a dott if both are available.
+                       my $hostname;
+
+                       if (($temp[1]) && ($temp[2])) {
+                               $hostname = "$temp[1].$temp[2]";
+                       } else {
+                               $hostname = "$temp[1]";
+                       }
+
                        $settings{'SERVICE'} = $temp[0];
-                       $settings{'HOSTNAME'} = "$temp[1].$temp[2]";
+                       $settings{'HOSTNAME'} = $hostname;
                        $settings{'PROXY'} = $temp[3];
                        $settings{'WILDCARDS'} = $temp[4];
                        $settings{'LOGIN'} = $temp[5];
                        $settings{'PASSWORD'} = $temp[6];
                        $settings{'ENABLED'} = $temp[7];
                }
-       # Increase $id.
-       $id++;
 
+               # Increase $id.
+               $id++;
        }
+
+       &GenerateDDNSConfigFile();
 }
 
 #
@@ -338,9 +326,10 @@ if ($settings{'ACTION'} eq $Lang::tr{'instant update'}) {
 #
 # Set default values.
 #
-if (! $settings{'ACTION'}) {
+if (!$settings{'ACTION'}) {
        $settings{'SERVICE'} = 'dyndns.org';
        $settings{'ENABLED'} = 'on';
+       $settings{'ID'} = '';
 }
 
 &Header::openpage($Lang::tr{'dynamic dns'}, 1, '');
@@ -354,7 +343,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) {
@@ -404,18 +395,15 @@ my $buttontext = $Lang::tr{'add'};
 
 # Change buttontext and headline if we edit an account.
 if ($settings{'ACTION'} eq $Lang::tr{'edit'}) {
-
        # Rename button and print headline for updating.
        $buttontext = $Lang::tr{'update'};
        &Header::openbox('100%', 'left', $Lang::tr{'edit an existing host'});
 } else {
-
        # Otherwise use default button text and show headline for adding a new account.
        &Header::openbox('100%', 'left', $Lang::tr{'add a host'});
 }
 
 print <<END
-
 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
 <input type='hidden' name='ID' value='$settings{'ID'}' />
 <table width='100%'>
@@ -431,7 +419,6 @@ END
 
                # Loop to print the providerlist.
                foreach my $provider (@providers) {
-
                        # Check if the current provider needs to be selected.
                        if ($provider eq $settings{'SERVICE'}) {
                                $selected = 'selected';
@@ -450,8 +437,8 @@ print <<END
        </tr>
 
        <tr>
-               <td class='base'></td>
-               <td></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 +449,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 +467,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,58 +488,70 @@ 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'}'";
+               }
+
+               # Handle hostname details. Only connect the values with a dott if both are available.
+               my $hostname="";
+
+               if (($temp[1]) && ($temp[2])) {
+                       $hostname="$temp[1].$temp[2]";
+               } else {
+                       $hostname="$temp[1]";
+               }
+
+               # 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>
+       <td align='center' $col>$sync$hostname</td>
 
        <td align='center' $col><form method='post' action='$ENV{'SCRIPT_NAME'}'>
                <input type='hidden' name='ID' value='$id'>
@@ -570,14 +573,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 +598,10 @@ print <<END
        </tr>
 </table>
 END
-;
+
+       &Header::closebox();
 }
 
-&Header::closebox();
 &Header::closebigbox();
 &Header::closepage();
 
@@ -641,28 +641,36 @@ 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");
+
+               # Handle hostname details. Only connect the values with a dott if both are available.
+               if (($hostname) && ($domain)) {
+                       print FILE "[$hostname.$domain]\n";
+               } else {
+                       print FILE "[$hostname]\n";
+               }
 
-               print FILE "[$hostname.$domain]\n";
                print FILE "provider = $provider\n";
 
                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",
+                                 "spdns.de", "zzzz.io"] && $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;
 
@@ -690,11 +698,6 @@ sub GenerateDDNSConfigFile {
                        print FILE "password = $password\n";
                }
 
-               # These providers need to be set to only use IPv4.
-               if ($provider ~~ ["freedns.afraid.org", "nsupdate.info", "variomedia.de", "zoneedit.com"]) {
-                       print FILE "proto = ipv4\n";
-               }
-
                print FILE "\n";
        }