]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
dns.cgi: Rework to allow central DNS configuration.
authorStefan Schantl <stefan.schantl@ipfire.org>
Tue, 7 Jan 2020 09:30:37 +0000 (10:30 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Tue, 7 Jan 2020 09:30:37 +0000 (10:30 +0100)
Fixes #12237.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
html/cgi-bin/dns.cgi [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index eee7a90..f4838b0
@@ -2,7 +2,7 @@
 ###############################################################################
 #                                                                             #
 # IPFire.org - A linux based firewall                                         #
-# Copyright (C) 2007-2019  IPFire Team  <info@ipfire.org>                     #
+# Copyright (C) 2020  IPFire Development Team                                 #
 #                                                                             #
 # This program is free software: you can redistribute it and/or modify        #
 # it under the terms of the GNU General Public License as published by        #
 ###############################################################################
 
 use strict;
+use IO::Socket;
 
 # enable only the following on debugging purpose
 #use warnings;
 #use CGI::Carp 'fatalsToBrowser';
 
 require '/var/ipfire/general-functions.pl';
+require "${General::swroot}/geoip-functions.pl";
 require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
 
-my %dnssettings=();
+#workaround to suppress a warning when a variable is used only once
+my @dummy = ( ${Header::colouryellow} );
+undef (@dummy);
+
+my %cgiparams=();
+my %checked=();
+my %selected=();
 my $errormessage = '';
 
+# Config file which stores the DNS settings.
+my $settings_file = "${General::swroot}/dns/settings";
+
+# File which stores the configured DNS-Servers.
+my $servers_file = "${General::swroot}/dns/servers";
+
+# Create files if the does not exist.
+unless (-f $settings_file) { system("touch $settings_file") };
+unless (-f $servers_file) { system("touch $servers_file") };
+
+# File which stores the ISP assigned DNS servers.
+my @ISP_nameserver_files = ( "${General::swroot}/dns/dns1", "${General::swroot}/dns/dns2" );
+
+# File which contains the ca-certificates.
+my $ca_certs_file = "/etc/ssl/certs/ca-bundle.crt";
+
+my %color = ();
+my %mainsettings = ();
+&General::readhash("${General::swroot}/main/settings", \%mainsettings);
+&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
+
 &Header::showhttpheaders();
+&Header::getcgihash(\%cgiparams);
 
-&General::readhash("${General::swroot}/dns/settings", \%dnssettings);
+##
+# Save general settings.
+#
+if ($cgiparams{'GENERAL'} eq $Lang::tr{'save'}) {
+       # Prevent form name from been stored in conf file.
+       delete $cgiparams{'GENERAL'};
 
-&Header::getcgihash(\%dnssettings);
+       # Add value for non-checked checkbox.
+       if ($cgiparams{'USE_ISP_NAMESERVERS'} ne "on") {
+               $cgiparams{'USE_ISP_NAMESERVERS'} = "off";
+       }
 
-&Header::openpage($Lang::tr{'dns title'}, 1, );
+       # Add value for non-checked checkbox.
+       if ($cgiparams{'ENABLE_SAFE_SEARCH'} ne "on") {
+               $cgiparams{'ENABLE_SAFE_SEARCH'} = "off";
+       }
 
-&Header::openbigbox('100%', 'left', '', $errormessage);
+       # Store settings into settings file.
+       &General::writehash("$settings_file", \%cgiparams);
+}
+
+###
+# Add / Edit entries.
+#
+if (($cgiparams{'SERVERS'} eq $Lang::tr{'save'}) || ($cgiparams{'SERVERS'} eq $Lang::tr{'update'})) {
+       # Hash to store the generic DNS settings.
+       my %settings = ();
+
+       # Read-in generic settings.
+       &General::readhash("$settings_file", \%settings);
 
-if ($dnssettings{'ACTION'} eq $Lang::tr{'save'}) {
-       if ((&General::validip($dnssettings{"DNS0"}) == 1)&&(&General::validip($dnssettings{"DNS1"}) == 1)) {   
-               if ($errormessage eq "") {
-                       &General::writehash("${General::swroot}/dns/settings", \%dnssettings);  
-                       &Header::openbox('100%', 'left', $Lang::tr{'dns saved'});                                                               
-                       print "<font class='base'>$Lang::tr{'dns saved txt'}</font>\n";
-                       &Header::closebox();            
+       # Check if the given DNS server is valid.
+       if(!&General::validip($cgiparams{"NAMESERVER"})) {
+               $errormessage = "$Lang::tr{'invalid ip'}: $cgiparams{'NAMESERVER'}";
+       }
+
+       # Check if a TLS is enabled and no TLS_HOSTNAME has benn specified.
+       elsif($settings{'PROTO'} eq "TLS") {
+               unless($cgiparams{"TLS_HOSTNAME"}) {
+                       $errormessage = "$Lang::tr{'dns no tls hostname given'}";
+               } else {
+                       # Check if the provided domain is valid.
+                       unless(&General::validfqdn($cgiparams{"TLS_HOSTNAME"})) {
+                               $errormessage = "$Lang::tr{'invalid ip or hostname'}: $cgiparams{'TLS_HOSTNAME'}";
+                       }
                }
-       } else {
-               if ((&General::validip($dnssettings{"DNS0"}) == 0)&&(&General::validip($dnssettings{"DNS1"}) == 1)){
-                       $errormessage = $Lang::tr{'dns error 0'};
-               }               
-               if ((&General::validip($dnssettings{"DNS1"}) == 0)&&(&General::validip($dnssettings{"DNS0"}) == 1)){
-                       $errormessage = $Lang::tr{'dns error 1'};
+       }
+
+       # Check the nameserver.
+       my $status = &check_nameserver("$cgiparams{'NAMESERVER'}", "ping.ipfire.org", "$settings{'PROTO'}", "$cgiparams{'TLS_HOSTNAME'}");
+
+       # Assign errormessage, if the nameserver does not support dnssec or any other kind of error happened.
+       if ($status eq "0") {
+               $errormessage = "$Lang::tr{'dns could not add server'} $Lang::tr{'dnssec not supported'}";
+       } elsif (($status ne "1") && ($status ne "2")) {
+               $errormessage = "$Lang::tr{'dns could not add server'} $status";
+       }
+
+       # Go further if there was no error.
+       if ( ! $errormessage) {
+               # Check if a remark has been entered.
+               $cgiparams{'REMARK'} = &Header::cleanhtml($cgiparams{'REMARK'});
+
+               my %dns_servers = ();
+               my $id;
+               my $status;
+
+               # Read-in configfile.
+               &General::readhasharray($servers_file, \%dns_servers);
+
+               # Check if we should edit an existing entry and got an ID.
+               if (($cgiparams{'SERVERS'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) {
+                       # Assin the provided id.
+                       $id = $cgiparams{'ID'};
+
+                       # Undef the given ID.
+                       undef($cgiparams{'ID'});
+
+                       # Grab the configured status of the corresponding entry.
+                       $status = $dns_servers{$id}[2];
+               } else {
+                       # Each newly added entry automatically should be enabled.
+                       $status = "enabled";
+
+                       # Generate the ID for the new entry.
+                       #
+                       # Sort the keys by their ID and store them in an array.
+                       my @keys = sort { $a <=> $b } keys %dns_servers;
+
+                       # Reverse the key array.
+                       my @reversed = reverse(@keys);
+
+                       # Obtain the last used id.
+                       my $last_id = @reversed[0];
+
+                       # Increase the last id by one and use it as id for the new entry.
+                       $id = ++$last_id;
+
+                       # The first allowed id is 3 to keep space for
+                       # possible ISP assigned DNS servers.
+                       if ($id le "2") {
+                               $id = "3";
+                       }
                }
-               if ((&General::validip($dnssettings{"DNS1"}) == 0)&&(&General::validip($dnssettings{"DNS0"}) == 0)){
-                       $errormessage = $Lang::tr{'dns error 01'};
+
+               # Add/Modify the entry to/in the dns_servers hash.
+               $dns_servers{$id} = ["$cgiparams{'NAMESERVER'}", "$cgiparams{'TLS_HOSTNAME'}", "$status", "$cgiparams{'REMARK'}"];
+
+               # Write the changed hash to the config file.
+               &General::writehasharray($servers_file, \%dns_servers);
+       } else {
+               # Switch back to previous mode.
+               $cgiparams{'SERVERS'} = $cgiparams{'MODE'};
+       }
+###
+# Toggle enable / disable.
+#
+} elsif ($cgiparams{'SERVERS'} eq $Lang::tr{'toggle enable disable'}) {
+       my %dns_servers = ();
+
+       # Only go further, if an ID has been passed.
+       if ($cgiparams{'ID'}) {
+               # Assign the given ID.
+               my $id = $cgiparams{'ID'};
+
+               # Undef the given ID.
+               undef($cgiparams{'ID'});
+
+               # Read-in configfile.
+               &General::readhasharray($servers_file, \%dns_servers);
+
+               # Grab the configured status of the corresponding entry.
+               my $status = $dns_servers{$id}[2];
+
+               # Switch the status.
+               if ($status eq "disabled") {
+                       $status = "enabled";
+               } else {
+                       $status = "disabled";
                }
+
+               # Modify the status of the existing entry.
+               $dns_servers{$id} = ["$dns_servers{$id}[0]", "$dns_servers{$id}[1]", "$status", "$dns_servers{$id}[3]"];
+
+               # Write the changed hash back to the config file.
+               &General::writehasharray($servers_file, \%dns_servers);
        }
+
+## Remove entry from DNS servers list.
+#
+} elsif ($cgiparams{'SERVERS'} eq $Lang::tr{'remove'}) {
+       my %dns_servers = ();
+
+       # Read-in configfile.
+       &General::readhasharray($servers_file, \%dns_servers);
+
+       # Drop entry from the hash.
+       delete($dns_servers{$cgiparams{'ID'}});
+
+       # Undef the given ID.
+       undef($cgiparams{'ID'});
+
+       # Write the changed hash to the config file.
+       &General::writehasharray($servers_file, \%dns_servers);
 }
 
-if ($dnssettings{'ACTION'} eq $Lang::tr{'reconnect'}) {
-       system("/usr/local/bin/redctrl restart >/dev/null 2>&1 &");
-       &Header::openbox('100%', 'left', $Lang::tr{'dns address recon'} );
-       print "<font class='base'>$Lang::tr{'dns address done'}</font>\n";
-       &Header::closebox();    
+# Hash to store the generic DNS settings.
+my %settings = ();
+
+# Read-in general DNS settings.
+&General::readhash("$settings_file", \%settings);
+
+# Hash which contains the configured DNS servers.
+my %dns_servers = ();
+
+# Read-in config file.
+&General::readhasharray("$servers_file", \%dns_servers);
+
+&Header::openpage($Lang::tr{'dns'}, 1, '');
+
+&Header::openbigbox('100%', 'left', '', $errormessage);
+
+###
+# Error messages layout.
+#
+if ($errormessage) {
+        &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
+        print "<class name='base'>$errormessage\n";
+        print "&nbsp;</class>\n";
+        &Header::closebox();
 }
 
-if ($dnssettings{'ACTION'} eq $Lang::tr{'delete'}) {
-       system("cat /dev/null > ${General::swroot}/dns/settings &");
-       &Header::openbox('100%', 'left', $Lang::tr{'dns address deleted'} );
-       print "<font class='base'>$Lang::tr{'dns address deleted txt'}</font>\n";
-       &Header::closebox();    
+# Handle if a nameserver should be added or edited.
+if (($cgiparams{'SERVERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'SERVERS'} eq "$Lang::tr{'edit'}")) {
+       # Display the sub page.
+       &show_add_edit_nameserver();
+
+       # Close webpage.
+       &Header::closebigbox();
+       &Header::closepage();
+
+       # Finished here for the moment.
+       exit(0);
 }
 
-# DPC move error message to top so it is seen!
-if ($errormessage) {
-       &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
-       print "<font class='base'>$errormessage&nbsp;</font>\n";
+$cgiparams{'GENERAL'} = '';
+$cgiparams{'SERVERS'} = '';
+$cgiparams{'NAMESERVER'} = '';
+$cgiparams{'TLS_HOSTNAME'} = '';
+$cgiparams{'REMARK'} ='';
+
+$checked{'USE_ISP_NAMESERVERS'}{'off'} = '';
+$checked{'USE_ISP_NAMESERVERS'}{'on'} = '';
+$checked{'USE_ISP_NAMESERVERS'}{$settings{'USE_ISP_NAMESERVERS'}} = "checked='checked'";
+
+$checked{'ENABLE_SAFE_SEARCH'}{'off'} = '';
+$checked{'ENABLE_SAFE_SEARCH'}{'on'} = '';
+$checked{'ENABLE_SAFE_SEARCH'}{$settings{'ENABLE_SAFE_SEARCH'}} = "checked='checked'";
+
+$selected{'PROTO'}{'UDP'} = '';
+$selected{'PROTO'}{'TLS'} = '';
+$selected{'PROTO'}{'TCP'} = '';
+$selected{'PROTO'}{$settings{'PROTO'}} = "selected='selected'";
+
+$selected{'QNAME_MIN'}{'standard'} = '';
+$selected{'QNAME_MIN'}{'strict'} = '';
+$selected{'QNAME_MIN'}{$settings{'QNAME_MIN'}} = "selected='selected'";
+
+# Display nameserver and configuration sections.
+&show_nameservers();
+&show_general_dns_configuration();
+
+&Header::closebigbox();
+&Header::closepage();
+
+###
+# General DNS-Servers sektion.
+#
+sub show_general_dns_configuration () {
+       &Header::openbox('100%', 'center', "$Lang::tr{'dns configuration'}");
+
+       print <<END;
+       <form method='post' action='$ENV{'SCRIPT_NAME'}'>
+               <table width="100%">
+                       <tr>
+                               <td width="33%">
+                                       $Lang::tr{'dns use isp assigned nameservers'}
+                               </td>
+
+                               <td>
+                                       <input type="checkbox" name="USE_ISP_NAMESERVERS" $checked{'USE_ISP_NAMESERVERS'}{'on'}>
+                               </td>
+                       </tr>
+
+                       <tr>
+                               <td colspan="2">
+                                       <br>
+                               </td>
+                       </tr>
+
+                       <tr>
+                               <td width="33%">
+                                       $Lang::tr{'dns use protocol for dns queries'}
+                               </td>
+
+                               <td>
+                                       <select name="PROTO">
+                                               <option value="UDP" $selected{'PROTO'}{'UDP'}>UDP</option>
+                                               <option value="TLS" $selected{'PROTO'}{'TLS'}>TLS</option>
+                                               <option value="TCP" $selected{'PROTO'}{'TCP'}>TCP</option>
+                                       </select>
+                               </td>
+                       </tr>
+
+                       <tr>
+                               <td colspan="2">
+                                       <br>
+                               </td>
+                       </tr>
+
+                       <tr>
+                               <td width="33%">
+                                       $Lang::tr{'dns enable safe-search'}
+                               </td>
+
+                               <td>
+                                       <input type="checkbox" name="ENABLE_SAFE_SEARCH" $checked{'ENABLE_SAFE_SEARCH'}{'on'}>
+                               </td>
+                       </tr>
+
+                       <tr>
+                               <td colspan="2">
+                                       <br>
+                               </td>
+                       </tr>
+
+                       <tr>
+                               <td width="33%">
+                                       $Lang::tr{'dns mode for qname minimisation'}
+                               </td>
+
+                               <td>
+                                       <select name="QNAME_MIN">
+                                               <option value="standard" $selected{'QNAME_MIN'}{'standard'}>$Lang::tr{'standard'}</option>
+                                               <option value="strict" $selected{'QNAME_MIN'}{'strict'}>$Lang::tr{'strict'}</option>
+                                       </select>
+                               </td>
+                       </tr>
+
+                       <tr>
+                               <td colspan="2" align="right">
+                                       <input type="submit" name="GENERAL" value="$Lang::tr{'save'}">
+                               </td>
+                       </tr>
+               </table>
+       </form>
+END
+
        &Header::closebox();
 }
 
-print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
+###
+# Section to display the configured and used DNS servers.
+#
+sub show_nameservers () {
+       &Header::openbox('100%', 'center', "DNS-Servers");
 
-&Header::openbox('100%', 'left', $Lang::tr{'dns header'});
-print <<END
+print <<END;
+               <table class="tbl" width='100%'>
+                       <tr>
+                               <td align="center">
+                                       <strong>$Lang::tr{'nameserver'}</strong>
+                               </td>
+
+                               <td align="center">
+                                       <strong>$Lang::tr{'country'}</strong>
+                               </td>
+
+                               <td align="center">
+                                       <strong>$Lang::tr{'rdns'}</strong>
+                               </td>
+
+                               <td align="center">
+                                       <strong>$Lang::tr{'remark'}</strong>
+                               </td>
+
+                               <td align="center">
+                                       <strong>$Lang::tr{'status'}</strong>
+                               </td>
 
-<table border="0"  width='100%'>
-  <tr>
-    <td colspan="2"><font class='base'>$Lang::tr{'dns desc'}<br /><img src="/images/dns_link.png" border="0" align="absmiddle"/><a href="https://wiki.ipfire.org/dns/public-servers" target="_blank">$Lang::tr{'dns list'}</a></font></td>
-  </tr>
-  <tr>
-    <td colspan="2">&nbsp;</td>
-  </tr>
-  <tr>
-    <td width="25%"><font class='base'>$Lang::tr{'dns new 0'}</font></td>
+                               <td align="center" colspan="3">
+                                       <strong>$Lang::tr{'action'}</strong>
+                               </td>
+                       </tr>
 END
-;     
-if ($dnssettings{'ACTION'} eq $Lang::tr{'delete'}) {
-print <<END    
-    <td width="75%"><input type="text" name="DNS0" maxlength="15" value=""/></td>
+
+               # Check the usage of ISP assigned nameservers is enabled.
+               if ($settings{'USE_ISP_NAMESERVERS'} eq "on") {
+                       my $id="1";
+
+                       # Loop through the array which stores the files.
+                       foreach my $file (@ISP_nameserver_files) {
+                               # Grab the address of the nameserver.
+                               my $address = &grab_address_from_file($file);
+
+                               # Check if we got an address.
+                               if ($address) {
+                                       # Add the address to the hash of nameservers.
+                                       $dns_servers{$id} = [ "$address", "none", "enabled", "$Lang::tr{'dns isp assigned nameserver'}" ];
+
+                                       # Increase id by one.
+                                       $id++;
+                               }
+                       }
+               }
+
+               # Check some DNS servers have been configured. In this case
+               # the hash contains at least one key.
+               my $server_amount;
+               if (keys %dns_servers) {
+                       # Sort the keys by their ID and store them in an array.
+                       my @keys = sort { $a <=> $b } keys %dns_servers;
+
+                       # Loop through all entries of the array/hash.
+                       foreach my $id (@keys) {
+                               # Inrease server_amount.
+                               $server_amount++;
+
+                               # Assign data array positions to some nice variable names.
+                               my $nameserver = $dns_servers{$id}[0];
+                               my $tls_hostname = $dns_servers{$id}[1];
+                               my $enabled = $dns_servers{$id}[2];
+                               my $remark = $dns_servers{$id}[3];
+
+                               my $col = '';
+                               my $toggle = '';
+                               my $gif = '';
+                               my $gdesc = '';
+                               my $notice = "";
+
+                               # Colorize columns.
+                               if ($server_amount % 2) {
+                                       $col="bgcolor='$color{'color22'}'"; }
+                               else {
+                                       $col="bgcolor='$color{'color20'}'";
+                               }
+
+                               if ($enabled eq 'enabled') {
+                                       $gif='on.gif'; $toggle='off'; $gdesc=$Lang::tr{'click to disable'};
+                               } else {
+                                       $gif='off.gif'; $toggle='on'; $gdesc=$Lang::tr{'click to enable'};
+                               }
+
+                               my $status;
+                               my $status_short;
+                               my $status_message;
+                               my $status_colour;
+
+                               # Only grab the status if the nameserver is enabled.
+                               if ($enabled eq "enabled") {
+                                       $status = &check_nameserver("$nameserver", "ping.ipfire.org", "$settings{'PROTO'}", "$tls_hostname");
+                               }
+
+                               if (!$status) {
+                                       $status_short = "$Lang::tr{'disabled'}";
+
+                               # DNSSEC Not supported
+                               } elsif ($status eq 0) {
+                                       $status_short = "$Lang::tr{'broken'}";
+                                       $status_message = $Lang::tr{'dnssec not supported'};
+                                       $status_colour = ${Header::colourred};
+
+                               # DNSSEC Aware
+                               } elsif ($status eq 1) {
+                                       $status_short = "$Lang::tr{'not validating'}";
+                                       $status_message = $Lang::tr{'dnssec aware'};
+                                       $status_colour = ${Header::colourblack};
+
+                               # DNSSEC Validating
+                               } elsif ($status eq 2) {
+                                       $status_short = "$Lang::tr{'ok'}";
+                                       $status_message = $Lang::tr{'dnssec validating'};
+                                       $status_colour = ${Header::colourgreen};
+
+                               # Error
+                               } else {
+                                       $status_short = "$Lang::tr{'error'}";
+                                       $status_message = $status;
+                                       $status_colour = ${Header::colourred};
+                               }
+
+                               # collect more information about name server (rDNS, GeoIP country code)
+                               my $ccode = &GeoIP::lookup($nameserver);
+                               my $flag_icon = &GeoIP::get_flag_icon($ccode);
+
+                               my $iaddr = inet_aton($nameserver);
+                               my $rdns = gethostbyaddr($iaddr, AF_INET);
+
+                               if (!$rdns) { $rdns = $Lang::tr{'lookup failed'}; }
+
+print <<END;
+                       <tr>
+                               <td align="center" $col>
+                                       $nameserver
+                               </td>
+
+                               <td align="center" $col>
+                                       <a href='country.cgi#$ccode'><img src="$flag_icon" border="0" alt="$ccode" title="$ccode" /></a>
+                               </td>
+
+                               <td align="center" $col>
+                                       $rdns
+                               </td>
+
+                               <td align="center" $col>
+                                       $remark
+                               </td>
+
+                               <td align="center" $col>
+                                       <strong><font color="$status_colour"><abbr title="$status_message">$status_short</abbr></font></strong>
+                               </td>
 END
-; 
-} else {   
-print <<END
-    <td width="75%"><input type="text" name="DNS0" maxlength="15" value="$dnssettings{"DNS0"}"/></td>
+;
+                               # Check if the id is greater than "2".
+                               #
+                               # Nameservers with an ID's of one or two are ISP assigned,
+                               # and we cannot perform any actions on them, so hide the tools for
+                               # them.
+                               if ($id gt "2") {
+
+print <<END;
+                                       <td align='center' width='5%' $col>
+                                               <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
+                                                       <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' title='$gdesc' alt='$gdesc' />
+                                                       <input type='hidden' name='ID' value='$id' />
+                                                       <input type='hidden' name='ENABLE' value='$toggle' />
+                                                       <input type='hidden' name='SERVERS' value='$Lang::tr{'toggle enable disable'}' />
+                                               </form>
+                                       </td>
+
+                                       <td align='center' width='5%' $col>
+                                               <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
+                                                       <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' title='$Lang::tr{'edit'}' alt='$Lang::tr{'edit'}' />
+                                                       <input type='hidden' name='ID' value='$id' />
+                                                       <input type='hidden' name='SERVERS' value='$Lang::tr{'edit'}' />
+                                               </form>
+                                       </td>
+
+                                       <td align='center' width='5%' $col>
+                                               <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>
+                                                       <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}' />
+                                                       <input type='hidden' name='ID' value='$id' />
+                                                       <input type='hidden' name='SERVERS' value='$Lang::tr{'remove'}' />
+                                               </form>
+                                       </td>
 END
-;  
-}
-print <<END
-  </tr>
-  <tr>
-    <td><font class='base'>$Lang::tr{'dns new 1'}</font></td>
+;
+                       } else {
+                                       print "<td colspan='3' $col>&nbsp;</td>\n";
+                       }
+
+
+                       print"</tr>\n";
+
+               }
+
+               print"</table>\n";
+
+               print"<table width='100%'>\n";
+
+               # Check if the usage of the ISP nameservers is enabled and there are more than 2 servers.
+               if (($settings{'USE_ISP_NAMESERVERS'} eq "on") && ($server_amount gt "2")) {
+print <<END;
+                       <tr>
+                               <td class='boldbase'>&nbsp; <b>$Lang::tr{'legend'}:</b></td>
+                               <td>&nbsp; <img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
+                               <td class='base'>$Lang::tr{'click to disable'}</td>
+                               <td>&nbsp; &nbsp; <img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
+                               <td class='base'>$Lang::tr{'click to enable'}</td>
+                               <td>&nbsp; &nbsp; <img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
+                               <td class='base'>$Lang::tr{'edit'}</td>
+                               <td>&nbsp; &nbsp; <img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
+                               <td class='base'>$Lang::tr{'remove'}</td>
+                       </tr>
 END
-;     
-if ($dnssettings{'ACTION'} eq $Lang::tr{'delete'}) {
-print <<END
-    <td><input type="text" name="DNS1" maxlength="15" value=""/></td>
+;
+               }
+print <<END;
+                       <tr>
+                               <form method="post" action="$ENV{'SCRIPT_NAME'}">
+                                       <td colspan="9" align="right"><input type="submit" name="SERVERS" value="$Lang::tr{'add'}"></td>
+                               </form>
+                       </tr>
+               </table>
 END
-; 
-} else {   
-print <<END
-    <td><input type="text" name="DNS1" maxlength="15" value="$dnssettings{"DNS1"}"/></td>
+;
+
+               } else {
+print <<END;
+               <table width="100%">
+                       <tr>
+                               <td colspan="6" align="center">
+                                       <br>$Lang::tr{'guardian no entries'}<br>
+                               </td>
+                       </tr>
+
+                       <tr>
+                               <form method="post" action="$ENV{'SCRIPT_NAME'}">
+                                       <td colspan="6" align="right"><input type="submit" name="SERVERS" value="$Lang::tr{'add'}"></td>
+                               </form>
+                       </tr>
+               </table>
+
 END
-; 
+;
+               }
+
+       &Header::closebox();
 }
-print <<END 
-  </tr>
-  <tr>
-    <td colspan="2"><hr /></td>
-  </tr>
-  <tr>
-    <td colspan="2"><div align="center">
-END
-;     
-if ($dnssettings{'ACTION'} eq $Lang::tr{'save'} && $errormessage eq "") {
-print <<END      
-        <input type='submit' name='ACTION' value='$Lang::tr{'save'}' />
-        &nbsp;&nbsp;&nbsp;&nbsp;
-        <input type='submit' name='ACTION' value='$Lang::tr{'delete'}' />
-        &nbsp;&nbsp;&nbsp;&nbsp;
-        <input type='submit' name='ACTION' value='$Lang::tr{'reconnect'}' />
-END
-; 
-} elsif ($dnssettings{'ACTION'} eq $Lang::tr{'delete'}) {
-print <<END      
-        <input type='submit' name='ACTION' value='$Lang::tr{'save'}' />
-        &nbsp;&nbsp;&nbsp;&nbsp;
-        <input type='submit' name='ACTION' value='$Lang::tr{'reconnect'}' />
+
+###
+# Section to display the add or edit subpage.
+#
+sub show_add_edit_nameserver() {
+       print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
+
+       my $buttontext = $Lang::tr{'save'};
+       my $dnssec_checked;
+       my $dot_checked;
+       if ($cgiparams{'SERVERS'} eq $Lang::tr{'edit'}) {
+               &Header::openbox('100%', 'left', $Lang::tr{'dnsforward edit an entry'});
+
+               # Update button text for upate the existing entry.
+               $buttontext = $Lang::tr{'update'};
+
+               # Add hidden input for sending ID.
+               print"<input type='hidden' name='ID' value='$cgiparams{'ID'}'>\n";
+
+               # Check if an ID has been given.
+               if ($cgiparams{'ID'}) {
+                       # Assign cgiparams values.
+                       $cgiparams{'NAMESERVER'} = $dns_servers{$cgiparams{'ID'}}[0];
+                       $cgiparams{'TLS_HOSTNAME'} = $dns_servers{$cgiparams{'ID'}}[1];
+                       $cgiparams{'REMARK'} = $dns_servers{$cgiparams{'ID'}}[3];
+               }
+       } else {
+               &Header::openbox('100%', 'left', $Lang::tr{'dnsforward add a new entry'});
+       }
+
+       # Add hidden input to store the mode.
+       print "<input type='hidden' name='MODE' value='$cgiparams{'SERVERS'}'>\n";
+
+print <<END
+       <table width='100%'>
+               <tr>
+                       <td width='20%' class='base'>$Lang::tr{'ip address'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
+                       <td><input type='text' name='NAMESERVER' value='$cgiparams{"NAMESERVER"}' size='24' /></td>
+               </tr>
 END
 ;
-} elsif ($dnssettings{'ACTION'} eq $Lang::tr{'save'}) {
-print <<END      
-        <input type='submit' name='ACTION' value='$Lang::tr{'save'}' />
-END
-; 
-} else {   
+       # If the protocol is TLS, display the TLS hostname input.
+       if ($settings{'PROTO'} eq "TLS") {
 print <<END
-        <input type='submit' name='ACTION' value='$Lang::tr{'save'}' />
-        &nbsp;&nbsp;&nbsp;&nbsp;
-        <input type='submit' name='ACTION' value='$Lang::tr{'delete'}' />
+               <tr>
+                       <td width='20%' class='base'>$Lang::tr{'dns tls hostname'}:&nbsp;<img src='/blob.gif' alt='*'></td>
+                       <td><input type='text' name='TLS_HOSTNAME' value='$cgiparams{'TLS_HOSTNAME'}' size='24'></td>
+               </tr>
 END
-;  
-} 
-print <<END     
-        </div>
-      </td>
-  </tr>
-</table>
+;
+       }
+
+print <<END
 
+               <tr>
+                       <td width ='20%' class='base'>$Lang::tr{'remark'}:</td>
+                       <td><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' maxlength='50' /></td>
+               </tr>
+       </table>
+
+       <br>
+       <hr>
+
+       <table width='100%'>
+               <tr>
+                       <td class='base' width='55%'><img src='/blob.gif' alt ='*' align='top' />&nbsp;$Lang::tr{'required field'}</td>
+                       <td width='40%' align='right'>
+                               <input type="submit" name="SERVERS" value="$buttontext">
+                               <input type="submit" name="SERVERS" value="$Lang::tr{'back'}">
+                       </td>
+               </tr>
+       </table>
 END
 ;
 
-&Header::closebox();
+       &Header::closebox();
+       print "</form>\n";
 
-print "</form>\n";
+       &Header::closebox();
+}
 
-&Header::closebigbox();
+# Tiny function to grab an IP-address of a given file.
+sub grab_address_from_file($) {
+       my ($file) = @_;
 
-&Header::closepage();
+       my $address;
+
+       # Check if the given file exists.
+       if(-f $file) {
+               # Open the file for reading.
+               open(FILE, $file) or die "Could not read from $file. $!\n";
+
+               # Read the address from the file.
+               $address = <FILE>;
 
+               # Close filehandle.
+               close(FILE);
+
+               # Remove newlines.
+               chomp($address);
+
+               # Check if the obtained address is valid.
+               if (&General::validip($address)) {
+                       # Return the address.
+                       return $address;
+               }
+       }
+
+       # Return nothing.
+       return;
+}
+
+# Function to check a given nameserver against propper work.
+sub check_nameserver($$$$) {
+       my ($nameserver, $record, $proto, $tls_hostname) = @_;
+
+       # Timout for the query in seconds.
+       my $timeout;
+       my $retry = "+retry=0";
+
+       # Default values.
+       my @command = ("kdig", "$timeout", "$retry", "+dnssec");
+
+       # Handle different protols.
+       if ($proto eq "TCP") {
+               # Add TCP switch to the command.
+               push(@command, "+tcp");
+
+       } elsif($proto eq "TLS") {
+               # Add TLS switch to the command and provide the
+               # path to our file which contains the ca certs.
+               push(@command, "+tls-ca=$ca_certs_file");
+
+               # Check if a TLS hostname has been provided.
+               if ($tls_hostname) {
+                       # Add TLS hostname to the command.
+                       push(@command, "+tls-hostname=$tls_hostname");
+               } else {
+                       return "$Lang::tr{'dns no tls hostname given'}";
+               }
+       }
+
+       # Add record to the command array.
+       push(@command, "$record");
+
+       # Add nameserver to the command array.
+       push(@command, "\@$nameserver");
+
+       # Connect to STDOUT and STDERR.
+       push(@command, "2>&1");
+
+        my @output = qx(@command);
+        my $output = join("", @output);
+
+       my $status = 0;
+       if ($output =~ m/WARNING: (.*)/) {
+               return $1;
+
+       }
+
+       if ($output =~ m/status: (\w+)/) {
+               $status = ($1 eq "NOERROR");
+
+               if (!$status) {
+                       return -1;
+               }
+       }
+
+       my @flags = ();
+       if ($output =~ m/Flags: (.*);/) {
+               @flags = split(/ /, $1);
+       }
+
+       my $aware = ($output =~ m/RRSIG/);
+       my $validating = ("ad;" ~~ @flags);
+
+       return $aware + $validating;
+}