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