]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - html/cgi-bin/dns.cgi
dns.cgi: Shorten time when checking if DNS is alive
[ipfire-2.x.git] / html / cgi-bin / dns.cgi
index 9683959b658735f93fb85ec23e7f299e146d5071..3b47f5ed4bb1fec6ddaf4c15a4bbb149ee6cea77 100755 (executable)
@@ -28,6 +28,7 @@ use IO::Socket;
 
 require '/var/ipfire/general-functions.pl';
 require "${General::swroot}/geoip-functions.pl";
+require "${General::swroot}/ids-functions.pl";
 require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
 
@@ -86,11 +87,20 @@ if ($cgiparams{'GENERAL'} eq $Lang::tr{'save'}) {
                $cgiparams{'ENABLE_SAFE_SEARCH'} = "off";
        }
 
-       # Store settings into settings file.
-       &General::writehash("$settings_file", \%cgiparams);
+       # Check if using ISP nameservers and TLS is enabled at the same time.
+       if (($cgiparams{'USE_ISP_NAMESERVERS'} eq "on") && ($cgiparams{'PROTO'} eq "TLS")) {
+               $errormessage = $Lang::tr{'dns isp nameservers and tls not allowed'}
+       }
 
-       # Call function to handle unbound restart, etc.
-       &_handle_unbound_and_more()
+       # Check if there was an error.
+       if ( ! $errormessage) {
+
+               # Store settings into settings file.
+               &General::writehash("$settings_file", \%cgiparams);
+
+               # Call function to handle unbound restart, etc.
+               &_handle_unbound_and_more()
+       }
 }
 
 ###
@@ -167,7 +177,7 @@ if (($cgiparams{'SERVERS'} eq $Lang::tr{'save'}) || ($cgiparams{'SERVERS'} eq $L
 
                        # The first allowed id is 3 to keep space for
                        # possible ISP assigned DNS servers.
-                       if ($id le "2") {
+                       if ($id <= "2") {
                                $id = "3";
                        }
                }
@@ -216,6 +226,9 @@ if (($cgiparams{'SERVERS'} eq $Lang::tr{'save'}) || ($cgiparams{'SERVERS'} eq $L
 
                # Write the changed hash back to the config file.
                &General::writehasharray($servers_file, \%dns_servers);
+
+               # Call function to handle unbound restart, etc.
+               &_handle_unbound_and_more();
        }
 
 ## Remove entry from DNS servers list.
@@ -235,6 +248,9 @@ 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);
 
+       # Call function to handle unbound restart, etc.
+       &_handle_unbound_and_more();
+
 ## Handle request to check the servers.
 #
 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr{'dns check servers'}) {
@@ -402,23 +418,37 @@ END
 sub show_nameservers () {
        &Header::openbox('100%', 'center', "$Lang::tr{'dns title'}");
 
+       # Determine if we are running in recursor mode
+       my $recursor = 0;
+       my $unbound_forward = qx(unbound-control forward);
+       if ($unbound_forward =~ m/^off/) {
+               $recursor = 1;
+       }
+
        my $dns_status_string;
        my $dns_status_col;
+       my $dns_working;
+
 
        # 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");
+       my $dns_status_ret = &check_nameserver("127.0.0.1", "$dns_test_server", "UDP", undef, "+timeout=1", "+retry=0");
 
        if ($dns_status_ret eq "2") {
                $dns_status_string = "$Lang::tr{'working'}";
                $dns_status_col = "${Header::colourgreen}";
+               $dns_working = 1;
        } else {
-               $dns_status_string = "$Lang::tr{'broken'} status: $dns_status_ret";
+               $dns_status_string = "$Lang::tr{'broken'}";
                $dns_status_col = "${Header::colourred}";
        }
 
+       if ($recursor) {
+               $dns_status_string .= " (" . $Lang::tr{'dns recursor mode'} . ")";
+       }
+
 print <<END;
                <table width='100%'>
                        <tr>
@@ -474,7 +504,7 @@ END
                # 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);
+                       my $address = &General::grab_address_from_file($file);
 
                        # Check if we got an address.
                        if ($address) {
@@ -535,7 +565,7 @@ END
                                        $status = &check_nameserver("$nameserver", "ping.ipfire.org", "$settings{'PROTO'}", "$tls_hostname");
                                }
 
-                               if (!$status) {
+                               if (!defined $status) {
                                        $status_short = "$Lang::tr{'disabled'}";
 
                                # DNSSEC Not supported
@@ -570,7 +600,7 @@ END
                                my $rdns;
 
                                # Only do the reverse lookup if the system is online.
-                               if (&red_is_active()) {
+                               if ($dns_working) {
                                        my $iaddr = inet_aton($nameserver);
                                        $rdns = gethostbyaddr($iaddr, AF_INET);
                                }
@@ -616,7 +646,7 @@ END
                                # 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") {
+                               if ($id > 2) {
 
 print <<END;
                                        <td align='center' width='5%' $col>
@@ -659,7 +689,7 @@ END
                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")) {
+               if (($settings{'USE_ISP_NAMESERVERS'} eq "on") && ($server_amount > 2)) {
 print <<END;
                        <tr>
                                <td class='boldbase'>&nbsp; <b>$Lang::tr{'legend'}:</b></td>
@@ -687,25 +717,16 @@ print <<END;
                </table>
 END
 ;
-
                } else {
-print <<END;
+                       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();
@@ -794,7 +815,17 @@ END
 # Private function to handle the restart of unbound and more.
 sub _handle_unbound_and_more () {
        # Restart unbound
-       system('/usr/local/bin/unboundctrl restart >/dev/null');
+       system('/usr/local/bin/unboundctrl reload >/dev/null');
+
+       # Check if the IDS is running.
+       if(&IDS::ids_is_running()) {
+               # Re-generate the file which contains the DNS Server
+               # details.
+               &IDS::generate_dns_servers_file();
+
+               # Call suricatactrl to perform a reload.
+               &IDS::call_suricatactrl("restart");
+       }
 }
 
 # Check if the system is online (RED is connected).
@@ -809,40 +840,9 @@ sub red_is_active () {
        }
 }
 
-# Tiny function to grab an IP-address of a given file.
-sub grab_address_from_file($) {
-       my ($file) = @_;
-
-       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) = @_;
+sub check_nameserver($$$$$) {
+       my ($nameserver, $record, $proto, $tls_hostname, @args) = @_;
 
        # Check if the system is online.
        unless (&red_is_active()) {
@@ -850,8 +850,8 @@ sub check_nameserver($$$$) {
        }
 
        # Default values.
-       my @command = ("kdig", "+timeout=2", "+retry=0", "+dnssec",
-               "+bufsize=1232");
+       my @command = ("kdig", "+dnssec",
+               "+bufsize=1232", @args);
 
        # Handle different protols.
        if ($proto eq "TCP") {