]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - html/cgi-bin/dns.cgi
dns.cgi: Display DNS system status.
[ipfire-2.x.git] / html / cgi-bin / dns.cgi
index 54f3aa3c426a796f5c95fcf2fe654b9cddcb5dce..41212ea4ad2cbb3dd7b3fcaa26020f29d624e813 100755 (executable)
@@ -56,6 +56,11 @@ my @ISP_nameserver_files = ( "/var/run/dns1", "/var/run/dns2" );
 # File which contains the ca-certificates.
 my $ca_certs_file = "/etc/ssl/certs/ca-bundle.crt";
 
+# Server which is used, to determine if the whole DNS system works properly.
+my $dns_test_server = "ping.ipfire.org";
+
+my $check_servers;
+
 my %color = ();
 my %mainsettings = ();
 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
@@ -95,8 +100,13 @@ if (($cgiparams{'SERVERS'} eq $Lang::tr{'save'}) || ($cgiparams{'SERVERS'} eq $L
        # Read-in generic settings.
        &General::readhash("$settings_file", \%settings);
 
+       # Check if an IP-address has been given.
+       if ($cgiparams{"NAMESERVER"} eq "") {
+               $errormessage = "$Lang::tr{'dns no address given'}";
+       }
+
        # Check if the given DNS server is valid.
-       if(!&General::validip($cgiparams{"NAMESERVER"})) {
+       elsif(!&General::validip($cgiparams{"NAMESERVER"})) {
                $errormessage = "$Lang::tr{'invalid ip'}: $cgiparams{'NAMESERVER'}";
        }
 
@@ -112,16 +122,6 @@ if (($cgiparams{'SERVERS'} eq $Lang::tr{'save'}) || ($cgiparams{'SERVERS'} eq $L
                }
        }
 
-       # 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.
@@ -228,6 +228,11 @@ if (($cgiparams{'SERVERS'} eq $Lang::tr{'save'}) || ($cgiparams{'SERVERS'} eq $L
 
        # Write the changed hash to the config file.
        &General::writehasharray($servers_file, \%dns_servers);
+
+## Handle request to check the servers.
+#
+} elsif ($cgiparams{'SERVERS'} eq $Lang::tr{'dns check servers'}) {
+       $check_servers = 1;
 }
 
 # Hash to store the generic DNS settings.
@@ -389,9 +394,37 @@ END
 # Section to display the configured and used DNS servers.
 #
 sub show_nameservers () {
-       &Header::openbox('100%', 'center', "DNS-Servers");
+       &Header::openbox('100%', 'center', "$Lang::tr{'dns title'}");
+
+       my $dns_status_string;
+       my $dns_status_col;
+
+       # Test if the DNS system is working.
+       #
+       # Simple send a request to unbound and check if it can resolve the
+       # DNS test server.
+       my $dns_status_ret = &check_nameserver("127.0.0.1", "$dns_test_server", "UDP");
+
+       if ($dns_status_ret eq "2") {
+               $dns_status_string = "$Lang::tr{'working'}";
+               $dns_status_col = "${Header::colourgreen}";
+       } else {
+               $dns_status_string = "$Lang::tr{'broken'} status: $dns_status_ret";
+               $dns_status_col = "${Header::colourred}";
+       }
 
 print <<END;
+               <table width='100%'>
+                       <tr>
+                               <td>
+                                       <strong>$Lang::tr{'status'}:&nbsp;</strong>
+                                       <strong><font color='$dns_status_col'>$dns_status_string</font></strong>
+                               </td>
+                       </tr>
+               </table>
+
+               <br>
+
                <table class="tbl" width='100%'>
                        <tr>
                                <td align="center">
@@ -409,16 +442,25 @@ print <<END;
                                <td align="center">
                                        <strong>$Lang::tr{'remark'}</strong>
                                </td>
-
+END
+       # Check if the status should be displayed.
+       if ($check_servers) {
+print <<END
                                <td align="center">
                                        <strong>$Lang::tr{'status'}</strong>
                                </td>
+END
+;
+       }
+
+print <<END
 
                                <td align="center" colspan="3">
                                        <strong>$Lang::tr{'action'}</strong>
                                </td>
                        </tr>
 END
+;
 
                # Check the usage of ISP assigned nameservers is enabled.
                my $id = 1;
@@ -483,7 +525,7 @@ END
                                my $status_colour;
 
                                # Only grab the status if the nameserver is enabled.
-                               if ($enabled eq "enabled") {
+                               if (($check_servers) && ($enabled eq "enabled")) {
                                        $status = &check_nameserver("$nameserver", "ping.ipfire.org", "$settings{'PROTO'}", "$tls_hostname");
                                }
 
@@ -551,12 +593,18 @@ print <<END;
                                <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
 ;
+                               # Display server status if requested.
+                               if ($check_servers) {
+print <<END
+                                       <td align="center" $col>
+                                               <strong><font color="$status_colour"><abbr title="$status_message">$status_short</abbr></font></strong>
+                                       </td>
+END
+;
+                               }
+
                                # Check if the id is greater than "2".
                                #
                                # Nameservers with an ID's of one or two are ISP assigned,
@@ -624,7 +672,10 @@ END
 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>
+                                       <td colspan="9" align="right">
+                                               <input type="submit" name="SERVERS" value="$Lang::tr{'add'}">
+                                               <input type="submit" name="SERVERS" value="$Lang::tr{'dns check servers'}">
+                                       </td>
                                </form>
                        </tr>
                </table>
@@ -781,6 +832,11 @@ sub grab_address_from_file($) {
 sub check_nameserver($$$$) {
        my ($nameserver, $record, $proto, $tls_hostname) = @_;
 
+       # Check if the system is online.
+       unless (&red_is_active()) {
+               return "$Lang::tr{'system is offline'}";
+       }
+
        # Default values.
        my @command = ("kdig", "+timeout=2", "+retry=0", "+dnssec",
                "+bufsize=1232");