]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/dns.cgi
dns.cgi: Shorten time when checking if DNS is alive
[ipfire-2.x.git] / html / cgi-bin / dns.cgi
CommitLineData
e0639d80
JPT
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
24d7c5ef 5# Copyright (C) 2020 IPFire Development Team #
e0639d80
JPT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22use strict;
24d7c5ef 23use IO::Socket;
e0639d80
JPT
24
25# enable only the following on debugging purpose
26#use warnings;
27#use CGI::Carp 'fatalsToBrowser';
28
29require '/var/ipfire/general-functions.pl';
24d7c5ef 30require "${General::swroot}/geoip-functions.pl";
8fbb12f1 31require "${General::swroot}/ids-functions.pl";
e0639d80
JPT
32require "${General::swroot}/lang.pl";
33require "${General::swroot}/header.pl";
34
24d7c5ef
SS
35#workaround to suppress a warning when a variable is used only once
36my @dummy = ( ${Header::colouryellow} );
37undef (@dummy);
38
39my %cgiparams=();
40my %checked=();
41my %selected=();
e0639d80
JPT
42my $errormessage = '';
43
24d7c5ef
SS
44# Config file which stores the DNS settings.
45my $settings_file = "${General::swroot}/dns/settings";
46
47# File which stores the configured DNS-Servers.
48my $servers_file = "${General::swroot}/dns/servers";
49
50# Create files if the does not exist.
51unless (-f $settings_file) { system("touch $settings_file") };
52unless (-f $servers_file) { system("touch $servers_file") };
53
54# File which stores the ISP assigned DNS servers.
1434fa0d 55my @ISP_nameserver_files = ( "/var/run/dns1", "/var/run/dns2" );
24d7c5ef
SS
56
57# File which contains the ca-certificates.
58my $ca_certs_file = "/etc/ssl/certs/ca-bundle.crt";
59
770ea81e
SS
60# Server which is used, to determine if the whole DNS system works properly.
61my $dns_test_server = "ping.ipfire.org";
62
43140993
SS
63my $check_servers;
64
24d7c5ef
SS
65my %color = ();
66my %mainsettings = ();
67&General::readhash("${General::swroot}/main/settings", \%mainsettings);
68&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
69
e0639d80 70&Header::showhttpheaders();
24d7c5ef 71&Header::getcgihash(\%cgiparams);
e0639d80 72
24d7c5ef
SS
73##
74# Save general settings.
75#
76if ($cgiparams{'GENERAL'} eq $Lang::tr{'save'}) {
77 # Prevent form name from been stored in conf file.
78 delete $cgiparams{'GENERAL'};
e0639d80 79
24d7c5ef
SS
80 # Add value for non-checked checkbox.
81 if ($cgiparams{'USE_ISP_NAMESERVERS'} ne "on") {
82 $cgiparams{'USE_ISP_NAMESERVERS'} = "off";
83 }
e0639d80 84
24d7c5ef
SS
85 # Add value for non-checked checkbox.
86 if ($cgiparams{'ENABLE_SAFE_SEARCH'} ne "on") {
87 $cgiparams{'ENABLE_SAFE_SEARCH'} = "off";
88 }
e0639d80 89
39a6219f
SS
90 # Check if using ISP nameservers and TLS is enabled at the same time.
91 if (($cgiparams{'USE_ISP_NAMESERVERS'} eq "on") && ($cgiparams{'PROTO'} eq "TLS")) {
92 $errormessage = $Lang::tr{'dns isp nameservers and tls not allowed'}
93 }
94
95 # Check if there was an error.
96 if ( ! $errormessage) {
719db1cd 97
39a6219f
SS
98 # Store settings into settings file.
99 &General::writehash("$settings_file", \%cgiparams);
100
101 # Call function to handle unbound restart, etc.
102 &_handle_unbound_and_more()
103 }
24d7c5ef
SS
104}
105
106###
107# Add / Edit entries.
108#
109if (($cgiparams{'SERVERS'} eq $Lang::tr{'save'}) || ($cgiparams{'SERVERS'} eq $Lang::tr{'update'})) {
110 # Hash to store the generic DNS settings.
111 my %settings = ();
112
113 # Read-in generic settings.
114 &General::readhash("$settings_file", \%settings);
e0639d80 115
038f962e
SS
116 # Check if an IP-address has been given.
117 if ($cgiparams{"NAMESERVER"} eq "") {
118 $errormessage = "$Lang::tr{'dns no address given'}";
119 }
120
24d7c5ef 121 # Check if the given DNS server is valid.
038f962e 122 elsif(!&General::validip($cgiparams{"NAMESERVER"})) {
24d7c5ef
SS
123 $errormessage = "$Lang::tr{'invalid ip'}: $cgiparams{'NAMESERVER'}";
124 }
125
126 # Check if a TLS is enabled and no TLS_HOSTNAME has benn specified.
127 elsif($settings{'PROTO'} eq "TLS") {
128 unless($cgiparams{"TLS_HOSTNAME"}) {
129 $errormessage = "$Lang::tr{'dns no tls hostname given'}";
130 } else {
131 # Check if the provided domain is valid.
132 unless(&General::validfqdn($cgiparams{"TLS_HOSTNAME"})) {
133 $errormessage = "$Lang::tr{'invalid ip or hostname'}: $cgiparams{'TLS_HOSTNAME'}";
134 }
e0639d80 135 }
24d7c5ef
SS
136 }
137
24d7c5ef
SS
138 # Go further if there was no error.
139 if ( ! $errormessage) {
140 # Check if a remark has been entered.
141 $cgiparams{'REMARK'} = &Header::cleanhtml($cgiparams{'REMARK'});
142
143 my %dns_servers = ();
144 my $id;
145 my $status;
146
147 # Read-in configfile.
148 &General::readhasharray($servers_file, \%dns_servers);
149
150 # Check if we should edit an existing entry and got an ID.
151 if (($cgiparams{'SERVERS'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) {
152 # Assin the provided id.
153 $id = $cgiparams{'ID'};
154
155 # Undef the given ID.
156 undef($cgiparams{'ID'});
157
158 # Grab the configured status of the corresponding entry.
159 $status = $dns_servers{$id}[2];
160 } else {
161 # Each newly added entry automatically should be enabled.
162 $status = "enabled";
163
164 # Generate the ID for the new entry.
165 #
166 # Sort the keys by their ID and store them in an array.
167 my @keys = sort { $a <=> $b } keys %dns_servers;
168
169 # Reverse the key array.
170 my @reversed = reverse(@keys);
171
172 # Obtain the last used id.
173 my $last_id = @reversed[0];
174
175 # Increase the last id by one and use it as id for the new entry.
176 $id = ++$last_id;
177
178 # The first allowed id is 3 to keep space for
179 # possible ISP assigned DNS servers.
611587cf 180 if ($id <= "2") {
24d7c5ef
SS
181 $id = "3";
182 }
e0639d80 183 }
24d7c5ef
SS
184
185 # Add/Modify the entry to/in the dns_servers hash.
186 $dns_servers{$id} = ["$cgiparams{'NAMESERVER'}", "$cgiparams{'TLS_HOSTNAME'}", "$status", "$cgiparams{'REMARK'}"];
187
188 # Write the changed hash to the config file.
189 &General::writehasharray($servers_file, \%dns_servers);
719db1cd
SS
190
191 # Call function to handle unbound restart, etc.
192 &_handle_unbound_and_more();
24d7c5ef
SS
193 } else {
194 # Switch back to previous mode.
195 $cgiparams{'SERVERS'} = $cgiparams{'MODE'};
196 }
197###
198# Toggle enable / disable.
199#
200} elsif ($cgiparams{'SERVERS'} eq $Lang::tr{'toggle enable disable'}) {
201 my %dns_servers = ();
202
203 # Only go further, if an ID has been passed.
204 if ($cgiparams{'ID'}) {
205 # Assign the given ID.
206 my $id = $cgiparams{'ID'};
207
208 # Undef the given ID.
209 undef($cgiparams{'ID'});
210
211 # Read-in configfile.
212 &General::readhasharray($servers_file, \%dns_servers);
213
214 # Grab the configured status of the corresponding entry.
215 my $status = $dns_servers{$id}[2];
216
217 # Switch the status.
218 if ($status eq "disabled") {
219 $status = "enabled";
220 } else {
221 $status = "disabled";
e0639d80 222 }
24d7c5ef
SS
223
224 # Modify the status of the existing entry.
225 $dns_servers{$id} = ["$dns_servers{$id}[0]", "$dns_servers{$id}[1]", "$status", "$dns_servers{$id}[3]"];
226
227 # Write the changed hash back to the config file.
228 &General::writehasharray($servers_file, \%dns_servers);
8f4bde65
SS
229
230 # Call function to handle unbound restart, etc.
231 &_handle_unbound_and_more();
e0639d80 232 }
24d7c5ef
SS
233
234## Remove entry from DNS servers list.
235#
236} elsif ($cgiparams{'SERVERS'} eq $Lang::tr{'remove'}) {
237 my %dns_servers = ();
238
239 # Read-in configfile.
240 &General::readhasharray($servers_file, \%dns_servers);
241
242 # Drop entry from the hash.
243 delete($dns_servers{$cgiparams{'ID'}});
244
245 # Undef the given ID.
246 undef($cgiparams{'ID'});
247
248 # Write the changed hash to the config file.
249 &General::writehasharray($servers_file, \%dns_servers);
43140993 250
8f4bde65
SS
251 # Call function to handle unbound restart, etc.
252 &_handle_unbound_and_more();
253
43140993
SS
254## Handle request to check the servers.
255#
256} elsif ($cgiparams{'SERVERS'} eq $Lang::tr{'dns check servers'}) {
257 $check_servers = 1;
e0639d80
JPT
258}
259
24d7c5ef
SS
260# Hash to store the generic DNS settings.
261my %settings = ();
262
263# Read-in general DNS settings.
264&General::readhash("$settings_file", \%settings);
265
266# Hash which contains the configured DNS servers.
267my %dns_servers = ();
268
269# Read-in config file.
270&General::readhasharray("$servers_file", \%dns_servers);
271
272&Header::openpage($Lang::tr{'dns'}, 1, '');
273
274&Header::openbigbox('100%', 'left', '', $errormessage);
275
276###
277# Error messages layout.
278#
279if ($errormessage) {
280 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
281 print "<class name='base'>$errormessage\n";
282 print "&nbsp;</class>\n";
283 &Header::closebox();
e0639d80
JPT
284}
285
24d7c5ef
SS
286# Handle if a nameserver should be added or edited.
287if (($cgiparams{'SERVERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'SERVERS'} eq "$Lang::tr{'edit'}")) {
288 # Display the sub page.
289 &show_add_edit_nameserver();
290
291 # Close webpage.
292 &Header::closebigbox();
293 &Header::closepage();
294
295 # Finished here for the moment.
296 exit(0);
2e1432c4
JPT
297}
298
24d7c5ef
SS
299$cgiparams{'GENERAL'} = '';
300$cgiparams{'SERVERS'} = '';
301$cgiparams{'NAMESERVER'} = '';
302$cgiparams{'TLS_HOSTNAME'} = '';
303$cgiparams{'REMARK'} ='';
304
305$checked{'USE_ISP_NAMESERVERS'}{'off'} = '';
306$checked{'USE_ISP_NAMESERVERS'}{'on'} = '';
307$checked{'USE_ISP_NAMESERVERS'}{$settings{'USE_ISP_NAMESERVERS'}} = "checked='checked'";
308
309$checked{'ENABLE_SAFE_SEARCH'}{'off'} = '';
310$checked{'ENABLE_SAFE_SEARCH'}{'on'} = '';
311$checked{'ENABLE_SAFE_SEARCH'}{$settings{'ENABLE_SAFE_SEARCH'}} = "checked='checked'";
312
313$selected{'PROTO'}{'UDP'} = '';
314$selected{'PROTO'}{'TLS'} = '';
315$selected{'PROTO'}{'TCP'} = '';
316$selected{'PROTO'}{$settings{'PROTO'}} = "selected='selected'";
317
318$selected{'QNAME_MIN'}{'standard'} = '';
319$selected{'QNAME_MIN'}{'strict'} = '';
320$selected{'QNAME_MIN'}{$settings{'QNAME_MIN'}} = "selected='selected'";
321
322# Display nameserver and configuration sections.
323&show_nameservers();
324&show_general_dns_configuration();
325
326&Header::closebigbox();
327&Header::closepage();
328
329###
330# General DNS-Servers sektion.
331#
332sub show_general_dns_configuration () {
333 &Header::openbox('100%', 'center', "$Lang::tr{'dns configuration'}");
334
335 print <<END;
336 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
337 <table width="100%">
338 <tr>
339 <td width="33%">
340 $Lang::tr{'dns use isp assigned nameservers'}
341 </td>
342
343 <td>
344 <input type="checkbox" name="USE_ISP_NAMESERVERS" $checked{'USE_ISP_NAMESERVERS'}{'on'}>
345 </td>
346 </tr>
347
348 <tr>
349 <td colspan="2">
350 <br>
351 </td>
352 </tr>
353
354 <tr>
355 <td width="33%">
356 $Lang::tr{'dns use protocol for dns queries'}
357 </td>
358
359 <td>
360 <select name="PROTO">
361 <option value="UDP" $selected{'PROTO'}{'UDP'}>UDP</option>
362 <option value="TLS" $selected{'PROTO'}{'TLS'}>TLS</option>
363 <option value="TCP" $selected{'PROTO'}{'TCP'}>TCP</option>
364 </select>
365 </td>
366 </tr>
367
368 <tr>
369 <td colspan="2">
370 <br>
371 </td>
372 </tr>
373
374 <tr>
375 <td width="33%">
376 $Lang::tr{'dns enable safe-search'}
377 </td>
378
379 <td>
380 <input type="checkbox" name="ENABLE_SAFE_SEARCH" $checked{'ENABLE_SAFE_SEARCH'}{'on'}>
381 </td>
382 </tr>
383
384 <tr>
385 <td colspan="2">
386 <br>
387 </td>
388 </tr>
389
390 <tr>
391 <td width="33%">
392 $Lang::tr{'dns mode for qname minimisation'}
393 </td>
394
395 <td>
396 <select name="QNAME_MIN">
397 <option value="standard" $selected{'QNAME_MIN'}{'standard'}>$Lang::tr{'standard'}</option>
398 <option value="strict" $selected{'QNAME_MIN'}{'strict'}>$Lang::tr{'strict'}</option>
399 </select>
400 </td>
401 </tr>
402
403 <tr>
404 <td colspan="2" align="right">
405 <input type="submit" name="GENERAL" value="$Lang::tr{'save'}">
406 </td>
407 </tr>
408 </table>
409 </form>
410END
411
e0639d80
JPT
412 &Header::closebox();
413}
414
24d7c5ef
SS
415###
416# Section to display the configured and used DNS servers.
417#
418sub show_nameservers () {
a969acc7 419 &Header::openbox('100%', 'center', "$Lang::tr{'dns title'}");
e0639d80 420
351ad526
MT
421 # Determine if we are running in recursor mode
422 my $recursor = 0;
423 my $unbound_forward = qx(unbound-control forward);
424 if ($unbound_forward =~ m/^off/) {
425 $recursor = 1;
426 }
427
770ea81e
SS
428 my $dns_status_string;
429 my $dns_status_col;
f03f3429 430 my $dns_working;
770ea81e 431
351ad526 432
770ea81e
SS
433 # Test if the DNS system is working.
434 #
435 # Simple send a request to unbound and check if it can resolve the
436 # DNS test server.
38f51465 437 my $dns_status_ret = &check_nameserver("127.0.0.1", "$dns_test_server", "UDP", undef, "+timeout=1", "+retry=0");
770ea81e
SS
438
439 if ($dns_status_ret eq "2") {
440 $dns_status_string = "$Lang::tr{'working'}";
441 $dns_status_col = "${Header::colourgreen}";
f03f3429 442 $dns_working = 1;
770ea81e 443 } else {
46cc88ed 444 $dns_status_string = "$Lang::tr{'broken'}";
770ea81e
SS
445 $dns_status_col = "${Header::colourred}";
446 }
447
351ad526
MT
448 if ($recursor) {
449 $dns_status_string .= " (" . $Lang::tr{'dns recursor mode'} . ")";
450 }
451
24d7c5ef 452print <<END;
770ea81e
SS
453 <table width='100%'>
454 <tr>
455 <td>
456 <strong>$Lang::tr{'status'}:&nbsp;</strong>
457 <strong><font color='$dns_status_col'>$dns_status_string</font></strong>
458 </td>
459 </tr>
460 </table>
461
462 <br>
463
24d7c5ef
SS
464 <table class="tbl" width='100%'>
465 <tr>
466 <td align="center">
467 <strong>$Lang::tr{'nameserver'}</strong>
468 </td>
469
470 <td align="center">
471 <strong>$Lang::tr{'country'}</strong>
472 </td>
473
474 <td align="center">
475 <strong>$Lang::tr{'rdns'}</strong>
476 </td>
477
478 <td align="center">
479 <strong>$Lang::tr{'remark'}</strong>
480 </td>
43140993
SS
481END
482 # Check if the status should be displayed.
483 if ($check_servers) {
484print <<END
24d7c5ef
SS
485 <td align="center">
486 <strong>$Lang::tr{'status'}</strong>
487 </td>
43140993
SS
488END
489;
490 }
491
492print <<END
e0639d80 493
24d7c5ef
SS
494 <td align="center" colspan="3">
495 <strong>$Lang::tr{'action'}</strong>
496 </td>
497 </tr>
830f6177 498END
43140993 499;
24d7c5ef
SS
500
501 # Check the usage of ISP assigned nameservers is enabled.
77c7a94c
MT
502 my $id = 1;
503
504 # Loop through the array which stores the files.
505 foreach my $file (@ISP_nameserver_files) {
506 # Grab the address of the nameserver.
97022524 507 my $address = &General::grab_address_from_file($file);
77c7a94c
MT
508
509 # Check if we got an address.
510 if ($address) {
511 # Add the address to the hash of nameservers.
512 $dns_servers{$id} = [ "$address", "none",
513 ($settings{'USE_ISP_NAMESERVERS'} eq "on") ? "enabled" : "disabled",
514 "$Lang::tr{'dns isp assigned nameserver'}" ];
515
516 # Increase id by one.
517 $id++;
24d7c5ef
SS
518 }
519 }
520
521 # Check some DNS servers have been configured. In this case
522 # the hash contains at least one key.
523 my $server_amount;
524 if (keys %dns_servers) {
525 # Sort the keys by their ID and store them in an array.
526 my @keys = sort { $a <=> $b } keys %dns_servers;
527
528 # Loop through all entries of the array/hash.
529 foreach my $id (@keys) {
530 # Inrease server_amount.
531 $server_amount++;
532
533 # Assign data array positions to some nice variable names.
534 my $nameserver = $dns_servers{$id}[0];
535 my $tls_hostname = $dns_servers{$id}[1];
536 my $enabled = $dns_servers{$id}[2];
537 my $remark = $dns_servers{$id}[3];
538
539 my $col = '';
540 my $toggle = '';
541 my $gif = '';
542 my $gdesc = '';
543 my $notice = "";
544
545 # Colorize columns.
546 if ($server_amount % 2) {
547 $col="bgcolor='$color{'color22'}'"; }
548 else {
549 $col="bgcolor='$color{'color20'}'";
550 }
551
552 if ($enabled eq 'enabled') {
553 $gif='on.gif'; $toggle='off'; $gdesc=$Lang::tr{'click to disable'};
554 } else {
555 $gif='off.gif'; $toggle='on'; $gdesc=$Lang::tr{'click to enable'};
556 }
557
558 my $status;
559 my $status_short;
560 my $status_message;
561 my $status_colour;
562
563 # Only grab the status if the nameserver is enabled.
43140993 564 if (($check_servers) && ($enabled eq "enabled")) {
24d7c5ef
SS
565 $status = &check_nameserver("$nameserver", "ping.ipfire.org", "$settings{'PROTO'}", "$tls_hostname");
566 }
567
a25dcda2 568 if (!defined $status) {
24d7c5ef
SS
569 $status_short = "$Lang::tr{'disabled'}";
570
571 # DNSSEC Not supported
572 } elsif ($status eq 0) {
573 $status_short = "$Lang::tr{'broken'}";
574 $status_message = $Lang::tr{'dnssec not supported'};
575 $status_colour = ${Header::colourred};
576
577 # DNSSEC Aware
578 } elsif ($status eq 1) {
579 $status_short = "$Lang::tr{'not validating'}";
580 $status_message = $Lang::tr{'dnssec aware'};
581 $status_colour = ${Header::colourblack};
582
583 # DNSSEC Validating
584 } elsif ($status eq 2) {
585 $status_short = "$Lang::tr{'ok'}";
586 $status_message = $Lang::tr{'dnssec validating'};
587 $status_colour = ${Header::colourgreen};
588
589 # Error
590 } else {
591 $status_short = "$Lang::tr{'error'}";
592 $status_message = $status;
593 $status_colour = ${Header::colourred};
594 }
595
596 # collect more information about name server (rDNS, GeoIP country code)
597 my $ccode = &GeoIP::lookup($nameserver);
598 my $flag_icon = &GeoIP::get_flag_icon($ccode);
599
25dda4a0
SS
600 my $rdns;
601
602 # Only do the reverse lookup if the system is online.
f03f3429 603 if ($dns_working) {
25dda4a0
SS
604 my $iaddr = inet_aton($nameserver);
605 $rdns = gethostbyaddr($iaddr, AF_INET);
606 }
24d7c5ef
SS
607
608 if (!$rdns) { $rdns = $Lang::tr{'lookup failed'}; }
609
77c7a94c
MT
610 # Mark ISP name servers as disabled
611 if ($id <= 2 && $enabled eq "disabled") {
612 $nameserver = "<del>$nameserver</del>";
613 }
614
24d7c5ef
SS
615print <<END;
616 <tr>
617 <td align="center" $col>
618 $nameserver
619 </td>
620
621 <td align="center" $col>
622 <a href='country.cgi#$ccode'><img src="$flag_icon" border="0" alt="$ccode" title="$ccode" /></a>
623 </td>
624
625 <td align="center" $col>
626 $rdns
627 </td>
628
629 <td align="center" $col>
630 $remark
631 </td>
830f6177 632END
24d7c5ef 633;
43140993
SS
634 # Display server status if requested.
635 if ($check_servers) {
636print <<END
637 <td align="center" $col>
638 <strong><font color="$status_colour"><abbr title="$status_message">$status_short</abbr></font></strong>
639 </td>
640END
641;
642 }
643
24d7c5ef
SS
644 # Check if the id is greater than "2".
645 #
646 # Nameservers with an ID's of one or two are ISP assigned,
647 # and we cannot perform any actions on them, so hide the tools for
648 # them.
19602b68 649 if ($id > 2) {
24d7c5ef
SS
650
651print <<END;
652 <td align='center' width='5%' $col>
653 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
654 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' title='$gdesc' alt='$gdesc' />
655 <input type='hidden' name='ID' value='$id' />
656 <input type='hidden' name='ENABLE' value='$toggle' />
657 <input type='hidden' name='SERVERS' value='$Lang::tr{'toggle enable disable'}' />
658 </form>
659 </td>
660
661 <td align='center' width='5%' $col>
662 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
663 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' title='$Lang::tr{'edit'}' alt='$Lang::tr{'edit'}' />
664 <input type='hidden' name='ID' value='$id' />
665 <input type='hidden' name='SERVERS' value='$Lang::tr{'edit'}' />
666 </form>
667 </td>
668
669 <td align='center' width='5%' $col>
670 <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>
671 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}' />
672 <input type='hidden' name='ID' value='$id' />
673 <input type='hidden' name='SERVERS' value='$Lang::tr{'remove'}' />
674 </form>
675 </td>
830f6177 676END
24d7c5ef
SS
677;
678 } else {
679 print "<td colspan='3' $col>&nbsp;</td>\n";
680 }
681
682
683 print"</tr>\n";
684
685 }
686
687 print"</table>\n";
688
689 print"<table width='100%'>\n";
690
691 # Check if the usage of the ISP nameservers is enabled and there are more than 2 servers.
19602b68 692 if (($settings{'USE_ISP_NAMESERVERS'} eq "on") && ($server_amount > 2)) {
24d7c5ef
SS
693print <<END;
694 <tr>
695 <td class='boldbase'>&nbsp; <b>$Lang::tr{'legend'}:</b></td>
696 <td>&nbsp; <img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
697 <td class='base'>$Lang::tr{'click to disable'}</td>
698 <td>&nbsp; &nbsp; <img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
699 <td class='base'>$Lang::tr{'click to enable'}</td>
700 <td>&nbsp; &nbsp; <img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
701 <td class='base'>$Lang::tr{'edit'}</td>
702 <td>&nbsp; &nbsp; <img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
703 <td class='base'>$Lang::tr{'remove'}</td>
704 </tr>
830f6177 705END
24d7c5ef
SS
706;
707 }
708print <<END;
709 <tr>
710 <form method="post" action="$ENV{'SCRIPT_NAME'}">
43140993
SS
711 <td colspan="9" align="right">
712 <input type="submit" name="SERVERS" value="$Lang::tr{'add'}">
713 <input type="submit" name="SERVERS" value="$Lang::tr{'dns check servers'}">
714 </td>
24d7c5ef
SS
715 </form>
716 </tr>
717 </table>
830f6177 718END
24d7c5ef 719;
24d7c5ef 720 } else {
351ad526 721 print <<END;
24d7c5ef 722 <table width="100%">
24d7c5ef
SS
723 <tr>
724 <form method="post" action="$ENV{'SCRIPT_NAME'}">
725 <td colspan="6" align="right"><input type="submit" name="SERVERS" value="$Lang::tr{'add'}"></td>
726 </form>
727 </tr>
728 </table>
830f6177 729END
24d7c5ef
SS
730 }
731
732 &Header::closebox();
830f6177 733}
24d7c5ef
SS
734
735###
736# Section to display the add or edit subpage.
737#
738sub show_add_edit_nameserver() {
739 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
740
741 my $buttontext = $Lang::tr{'save'};
742 my $dnssec_checked;
743 my $dot_checked;
744 if ($cgiparams{'SERVERS'} eq $Lang::tr{'edit'}) {
745 &Header::openbox('100%', 'left', $Lang::tr{'dnsforward edit an entry'});
746
747 # Update button text for upate the existing entry.
748 $buttontext = $Lang::tr{'update'};
749
750 # Add hidden input for sending ID.
751 print"<input type='hidden' name='ID' value='$cgiparams{'ID'}'>\n";
752
753 # Check if an ID has been given.
754 if ($cgiparams{'ID'}) {
755 # Assign cgiparams values.
756 $cgiparams{'NAMESERVER'} = $dns_servers{$cgiparams{'ID'}}[0];
757 $cgiparams{'TLS_HOSTNAME'} = $dns_servers{$cgiparams{'ID'}}[1];
758 $cgiparams{'REMARK'} = $dns_servers{$cgiparams{'ID'}}[3];
759 }
760 } else {
761 &Header::openbox('100%', 'left', $Lang::tr{'dnsforward add a new entry'});
762 }
763
f10fb4bf
SS
764 my $tls_required_image;
765
766 # If the protocol is TLS, dispaly the required image.
767 if ($settings{'PROTO'} eq "TLS") {
768 $tls_required_image = "<img src='/blob.gif' alt='*'>";
769 }
770
24d7c5ef
SS
771 # Add hidden input to store the mode.
772 print "<input type='hidden' name='MODE' value='$cgiparams{'SERVERS'}'>\n";
773
774print <<END
775 <table width='100%'>
776 <tr>
777 <td width='20%' class='base'>$Lang::tr{'ip address'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
778 <td><input type='text' name='NAMESERVER' value='$cgiparams{"NAMESERVER"}' size='24' /></td>
779 </tr>
f10fb4bf
SS
780
781
24d7c5ef 782 <tr>
f10fb4bf 783 <td width='20%' class='base'>$Lang::tr{'dns tls hostname'}:&nbsp;$tls_required_image</td>
24d7c5ef
SS
784 <td><input type='text' name='TLS_HOSTNAME' value='$cgiparams{'TLS_HOSTNAME'}' size='24'></td>
785 </tr>
24d7c5ef 786
e0639d80 787
24d7c5ef
SS
788 <tr>
789 <td width ='20%' class='base'>$Lang::tr{'remark'}:</td>
790 <td><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' maxlength='50' /></td>
791 </tr>
792 </table>
793
794 <br>
795 <hr>
796
797 <table width='100%'>
798 <tr>
799 <td class='base' width='55%'><img src='/blob.gif' alt ='*' align='top' />&nbsp;$Lang::tr{'required field'}</td>
800 <td width='40%' align='right'>
801 <input type="submit" name="SERVERS" value="$buttontext">
802 <input type="submit" name="SERVERS" value="$Lang::tr{'back'}">
803 </td>
804 </tr>
805 </table>
e0639d80
JPT
806END
807;
808
24d7c5ef
SS
809 &Header::closebox();
810 print "</form>\n";
e0639d80 811
24d7c5ef
SS
812 &Header::closebox();
813}
e0639d80 814
719db1cd
SS
815# Private function to handle the restart of unbound and more.
816sub _handle_unbound_and_more () {
817 # Restart unbound
4caa0efc 818 system('/usr/local/bin/unboundctrl reload >/dev/null');
8fbb12f1
SS
819
820 # Check if the IDS is running.
821 if(&IDS::ids_is_running()) {
822 # Re-generate the file which contains the DNS Server
823 # details.
824 &IDS::generate_dns_servers_file();
825
826 # Call suricatactrl to perform a reload.
827 &IDS::call_suricatactrl("restart");
828 }
719db1cd
SS
829}
830
f36855fe
SS
831# Check if the system is online (RED is connected).
832sub red_is_active () {
833 # Check if the "active" file is present.
834 if ( -f "${General::swroot}/red/active") {
835 # Return "1" - True.
836 return 1;
837 } else {
838 # Return nothing - False.
839 return;
840 }
841}
842
24d7c5ef 843# Function to check a given nameserver against propper work.
38f51465
MT
844sub check_nameserver($$$$$) {
845 my ($nameserver, $record, $proto, $tls_hostname, @args) = @_;
24d7c5ef 846
70187da6
SS
847 # Check if the system is online.
848 unless (&red_is_active()) {
849 return "$Lang::tr{'system is offline'}";
850 }
851
24d7c5ef 852 # Default values.
3a5866ac 853 my @command = ("kdig", "+dnssec",
38f51465 854 "+bufsize=1232", @args);
24d7c5ef
SS
855
856 # Handle different protols.
857 if ($proto eq "TCP") {
858 # Add TCP switch to the command.
859 push(@command, "+tcp");
860
861 } elsif($proto eq "TLS") {
862 # Add TLS switch to the command and provide the
863 # path to our file which contains the ca certs.
864 push(@command, "+tls-ca=$ca_certs_file");
865
866 # Check if a TLS hostname has been provided.
867 if ($tls_hostname) {
868 # Add TLS hostname to the command.
869 push(@command, "+tls-hostname=$tls_hostname");
870 } else {
871 return "$Lang::tr{'dns no tls hostname given'}";
872 }
873 }
874
875 # Add record to the command array.
876 push(@command, "$record");
877
878 # Add nameserver to the command array.
879 push(@command, "\@$nameserver");
880
881 # Connect to STDOUT and STDERR.
882 push(@command, "2>&1");
883
884 my @output = qx(@command);
885 my $output = join("", @output);
886
887 my $status = 0;
24d7c5ef
SS
888
889 if ($output =~ m/status: (\w+)/) {
890 $status = ($1 eq "NOERROR");
891
892 if (!$status) {
893 return -1;
894 }
984f14bd
MT
895 } else {
896 my $warning;
897
898 while ($output =~ m/WARNING: (.*)/g) {
899 # Add the current grabbed warning to the warning string.
900 $warning .= "$1\; ";
901 }
902
903 # Return the warning string, if we grabbed at least one.
904 if ($warning) {
905 return $warning;
906 }
24d7c5ef
SS
907 }
908
909 my @flags = ();
910 if ($output =~ m/Flags: (.*);/) {
911 @flags = split(/ /, $1);
912 }
913
914 my $aware = ($output =~ m/RRSIG/);
71471d9b 915 my $validating = (grep(/ad;/, @flags));
24d7c5ef
SS
916
917 return $aware + $validating;
918}