2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2020 IPFire Development Team #
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. #
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. #
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/>. #
20 ###############################################################################
25 # enable only the following on debugging purpose
27 #use CGI::Carp 'fatalsToBrowser';
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/geoip-functions.pl";
31 require "${General::swroot}/lang.pl";
32 require "${General::swroot}/header.pl";
34 #workaround to suppress a warning when a variable is used only once
35 my @dummy = ( ${Header
::colouryellow
} );
41 my $errormessage = '';
43 # Config file which stores the DNS settings.
44 my $settings_file = "${General::swroot}/dns/settings";
46 # File which stores the configured DNS-Servers.
47 my $servers_file = "${General::swroot}/dns/servers";
49 # Create files if the does not exist.
50 unless (-f
$settings_file) { system("touch $settings_file") };
51 unless (-f
$servers_file) { system("touch $servers_file") };
53 # File which stores the ISP assigned DNS servers.
54 my @ISP_nameserver_files = ( "/var/run/dns1", "/var/run/dns2" );
56 # File which contains the ca-certificates.
57 my $ca_certs_file = "/etc/ssl/certs/ca-bundle.crt";
59 # Server which is used, to determine if the whole DNS system works properly.
60 my $dns_test_server = "ping.ipfire.org";
65 my %mainsettings = ();
66 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
67 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
69 &Header
::showhttpheaders
();
70 &Header
::getcgihash
(\
%cgiparams);
73 # Save general settings.
75 if ($cgiparams{'GENERAL'} eq $Lang::tr
{'save'}) {
76 # Prevent form name from been stored in conf file.
77 delete $cgiparams{'GENERAL'};
79 # Add value for non-checked checkbox.
80 if ($cgiparams{'USE_ISP_NAMESERVERS'} ne "on") {
81 $cgiparams{'USE_ISP_NAMESERVERS'} = "off";
84 # Add value for non-checked checkbox.
85 if ($cgiparams{'ENABLE_SAFE_SEARCH'} ne "on") {
86 $cgiparams{'ENABLE_SAFE_SEARCH'} = "off";
89 # Store settings into settings file.
90 &General
::writehash
("$settings_file", \
%cgiparams);
96 if (($cgiparams{'SERVERS'} eq $Lang::tr
{'save'}) || ($cgiparams{'SERVERS'} eq $Lang::tr
{'update'})) {
97 # Hash to store the generic DNS settings.
100 # Read-in generic settings.
101 &General
::readhash
("$settings_file", \
%settings);
103 # Check if an IP-address has been given.
104 if ($cgiparams{"NAMESERVER"} eq "") {
105 $errormessage = "$Lang::tr{'dns no address given'}";
108 # Check if the given DNS server is valid.
109 elsif(!&General
::validip
($cgiparams{"NAMESERVER"})) {
110 $errormessage = "$Lang::tr{'invalid ip'}: $cgiparams{'NAMESERVER'}";
113 # Check if a TLS is enabled and no TLS_HOSTNAME has benn specified.
114 elsif($settings{'PROTO'} eq "TLS") {
115 unless($cgiparams{"TLS_HOSTNAME"}) {
116 $errormessage = "$Lang::tr{'dns no tls hostname given'}";
118 # Check if the provided domain is valid.
119 unless(&General
::validfqdn
($cgiparams{"TLS_HOSTNAME"})) {
120 $errormessage = "$Lang::tr{'invalid ip or hostname'}: $cgiparams{'TLS_HOSTNAME'}";
125 # Go further if there was no error.
126 if ( ! $errormessage) {
127 # Check if a remark has been entered.
128 $cgiparams{'REMARK'} = &Header
::cleanhtml
($cgiparams{'REMARK'});
130 my %dns_servers = ();
134 # Read-in configfile.
135 &General
::readhasharray
($servers_file, \
%dns_servers);
137 # Check if we should edit an existing entry and got an ID.
138 if (($cgiparams{'SERVERS'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
139 # Assin the provided id.
140 $id = $cgiparams{'ID'};
142 # Undef the given ID.
143 undef($cgiparams{'ID'});
145 # Grab the configured status of the corresponding entry.
146 $status = $dns_servers{$id}[2];
148 # Each newly added entry automatically should be enabled.
151 # Generate the ID for the new entry.
153 # Sort the keys by their ID and store them in an array.
154 my @keys = sort { $a <=> $b } keys %dns_servers;
156 # Reverse the key array.
157 my @reversed = reverse(@keys);
159 # Obtain the last used id.
160 my $last_id = @reversed[0];
162 # Increase the last id by one and use it as id for the new entry.
165 # The first allowed id is 3 to keep space for
166 # possible ISP assigned DNS servers.
172 # Add/Modify the entry to/in the dns_servers hash.
173 $dns_servers{$id} = ["$cgiparams{'NAMESERVER'}", "$cgiparams{'TLS_HOSTNAME'}", "$status", "$cgiparams{'REMARK'}"];
175 # Write the changed hash to the config file.
176 &General
::writehasharray
($servers_file, \
%dns_servers);
178 # Switch back to previous mode.
179 $cgiparams{'SERVERS'} = $cgiparams{'MODE'};
182 # Toggle enable / disable.
184 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr
{'toggle enable disable'}) {
185 my %dns_servers = ();
187 # Only go further, if an ID has been passed.
188 if ($cgiparams{'ID'}) {
189 # Assign the given ID.
190 my $id = $cgiparams{'ID'};
192 # Undef the given ID.
193 undef($cgiparams{'ID'});
195 # Read-in configfile.
196 &General
::readhasharray
($servers_file, \
%dns_servers);
198 # Grab the configured status of the corresponding entry.
199 my $status = $dns_servers{$id}[2];
202 if ($status eq "disabled") {
205 $status = "disabled";
208 # Modify the status of the existing entry.
209 $dns_servers{$id} = ["$dns_servers{$id}[0]", "$dns_servers{$id}[1]", "$status", "$dns_servers{$id}[3]"];
211 # Write the changed hash back to the config file.
212 &General
::writehasharray
($servers_file, \
%dns_servers);
215 ## Remove entry from DNS servers list.
217 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr
{'remove'}) {
218 my %dns_servers = ();
220 # Read-in configfile.
221 &General
::readhasharray
($servers_file, \
%dns_servers);
223 # Drop entry from the hash.
224 delete($dns_servers{$cgiparams{'ID'}});
226 # Undef the given ID.
227 undef($cgiparams{'ID'});
229 # Write the changed hash to the config file.
230 &General
::writehasharray
($servers_file, \
%dns_servers);
232 ## Handle request to check the servers.
234 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr
{'dns check servers'}) {
238 # Hash to store the generic DNS settings.
241 # Read-in general DNS settings.
242 &General
::readhash
("$settings_file", \
%settings);
244 # Hash which contains the configured DNS servers.
245 my %dns_servers = ();
247 # Read-in config file.
248 &General
::readhasharray
("$servers_file", \
%dns_servers);
250 &Header
::openpage
($Lang::tr
{'dns'}, 1, '');
252 &Header
::openbigbox
('100%', 'left', '', $errormessage);
255 # Error messages layout.
258 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
259 print "<class name='base'>$errormessage\n";
260 print " </class>\n";
264 # Handle if a nameserver should be added or edited.
265 if (($cgiparams{'SERVERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'SERVERS'} eq "$Lang::tr{'edit'}")) {
266 # Display the sub page.
267 &show_add_edit_nameserver
();
270 &Header
::closebigbox
();
271 &Header
::closepage
();
273 # Finished here for the moment.
277 $cgiparams{'GENERAL'} = '';
278 $cgiparams{'SERVERS'} = '';
279 $cgiparams{'NAMESERVER'} = '';
280 $cgiparams{'TLS_HOSTNAME'} = '';
281 $cgiparams{'REMARK'} ='';
283 $checked{'USE_ISP_NAMESERVERS'}{'off'} = '';
284 $checked{'USE_ISP_NAMESERVERS'}{'on'} = '';
285 $checked{'USE_ISP_NAMESERVERS'}{$settings{'USE_ISP_NAMESERVERS'}} = "checked='checked'";
287 $checked{'ENABLE_SAFE_SEARCH'}{'off'} = '';
288 $checked{'ENABLE_SAFE_SEARCH'}{'on'} = '';
289 $checked{'ENABLE_SAFE_SEARCH'}{$settings{'ENABLE_SAFE_SEARCH'}} = "checked='checked'";
291 $selected{'PROTO'}{'UDP'} = '';
292 $selected{'PROTO'}{'TLS'} = '';
293 $selected{'PROTO'}{'TCP'} = '';
294 $selected{'PROTO'}{$settings{'PROTO'}} = "selected='selected'";
296 $selected{'QNAME_MIN'}{'standard'} = '';
297 $selected{'QNAME_MIN'}{'strict'} = '';
298 $selected{'QNAME_MIN'}{$settings{'QNAME_MIN'}} = "selected='selected'";
300 # Display nameserver and configuration sections.
302 &show_general_dns_configuration
();
304 &Header
::closebigbox
();
305 &Header
::closepage
();
308 # General DNS-Servers sektion.
310 sub show_general_dns_configuration
() {
311 &Header
::openbox
('100%', 'center', "$Lang::tr{'dns configuration'}");
314 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
318 $Lang::tr{'dns use isp assigned nameservers'}
322 <input type="checkbox" name="USE_ISP_NAMESERVERS" $checked{'USE_ISP_NAMESERVERS'}{'on'}>
334 $Lang::tr{'dns use protocol for dns queries'}
338 <select name="PROTO">
339 <option value="UDP" $selected{'PROTO'}{'UDP'}>UDP</option>
340 <option value="TLS" $selected{'PROTO'}{'TLS'}>TLS</option>
341 <option value="TCP" $selected{'PROTO'}{'TCP'}>TCP</option>
354 $Lang::tr{'dns enable safe-search'}
358 <input type="checkbox" name="ENABLE_SAFE_SEARCH" $checked{'ENABLE_SAFE_SEARCH'}{'on'}>
370 $Lang::tr{'dns mode for qname minimisation'}
374 <select name="QNAME_MIN">
375 <option value="standard" $selected{'QNAME_MIN'}{'standard'}>$Lang::tr{'standard'}</option>
376 <option value="strict" $selected{'QNAME_MIN'}{'strict'}>$Lang::tr{'strict'}</option>
382 <td colspan="2" align="right">
383 <input type="submit" name="GENERAL" value="$Lang::tr{'save'}">
394 # Section to display the configured and used DNS servers.
396 sub show_nameservers
() {
397 &Header
::openbox
('100%', 'center', "$Lang::tr{'dns title'}");
399 my $dns_status_string;
402 # Test if the DNS system is working.
404 # Simple send a request to unbound and check if it can resolve the
406 my $dns_status_ret = &check_nameserver
("127.0.0.1", "$dns_test_server", "UDP");
408 if ($dns_status_ret eq "2") {
409 $dns_status_string = "$Lang::tr{'working'}";
410 $dns_status_col = "${Header::colourgreen}";
412 $dns_status_string = "$Lang::tr{'broken'} status: $dns_status_ret";
413 $dns_status_col = "${Header::colourred}";
420 <strong>$Lang::tr{'status'}: </strong>
421 <strong><font color='$dns_status_col'>$dns_status_string</font></strong>
428 <table class="tbl" width='100%'>
431 <strong>$Lang::tr{'nameserver'}</strong>
435 <strong>$Lang::tr{'country'}</strong>
439 <strong>$Lang::tr{'rdns'}</strong>
443 <strong>$Lang::tr{'remark'}</strong>
446 # Check if the status should be displayed.
447 if ($check_servers) {
450 <strong>$Lang::tr{'status'}</strong>
458 <td align="center" colspan="3">
459 <strong>$Lang::tr{'action'}</strong>
465 # Check the usage of ISP assigned nameservers is enabled.
468 # Loop through the array which stores the files.
469 foreach my $file (@ISP_nameserver_files) {
470 # Grab the address of the nameserver.
471 my $address = &grab_address_from_file
($file);
473 # Check if we got an address.
475 # Add the address to the hash of nameservers.
476 $dns_servers{$id} = [ "$address", "none",
477 ($settings{'USE_ISP_NAMESERVERS'} eq "on") ?
"enabled" : "disabled",
478 "$Lang::tr{'dns isp assigned nameserver'}" ];
480 # Increase id by one.
485 # Check some DNS servers have been configured. In this case
486 # the hash contains at least one key.
488 if (keys %dns_servers) {
489 # Sort the keys by their ID and store them in an array.
490 my @keys = sort { $a <=> $b } keys %dns_servers;
492 # Loop through all entries of the array/hash.
493 foreach my $id (@keys) {
494 # Inrease server_amount.
497 # Assign data array positions to some nice variable names.
498 my $nameserver = $dns_servers{$id}[0];
499 my $tls_hostname = $dns_servers{$id}[1];
500 my $enabled = $dns_servers{$id}[2];
501 my $remark = $dns_servers{$id}[3];
510 if ($server_amount % 2) {
511 $col="bgcolor='$color{'color22'}'"; }
513 $col="bgcolor='$color{'color20'}'";
516 if ($enabled eq 'enabled') {
517 $gif='on.gif'; $toggle='off'; $gdesc=$Lang::tr
{'click to disable'};
519 $gif='off.gif'; $toggle='on'; $gdesc=$Lang::tr
{'click to enable'};
527 # Only grab the status if the nameserver is enabled.
528 if (($check_servers) && ($enabled eq "enabled")) {
529 $status = &check_nameserver
("$nameserver", "ping.ipfire.org", "$settings{'PROTO'}", "$tls_hostname");
533 $status_short = "$Lang::tr{'disabled'}";
535 # DNSSEC Not supported
536 } elsif ($status eq 0) {
537 $status_short = "$Lang::tr{'broken'}";
538 $status_message = $Lang::tr
{'dnssec not supported'};
539 $status_colour = ${Header
::colourred
};
542 } elsif ($status eq 1) {
543 $status_short = "$Lang::tr{'not validating'}";
544 $status_message = $Lang::tr
{'dnssec aware'};
545 $status_colour = ${Header
::colourblack
};
548 } elsif ($status eq 2) {
549 $status_short = "$Lang::tr{'ok'}";
550 $status_message = $Lang::tr
{'dnssec validating'};
551 $status_colour = ${Header
::colourgreen
};
555 $status_short = "$Lang::tr{'error'}";
556 $status_message = $status;
557 $status_colour = ${Header
::colourred
};
560 # collect more information about name server (rDNS, GeoIP country code)
561 my $ccode = &GeoIP
::lookup
($nameserver);
562 my $flag_icon = &GeoIP
::get_flag_icon
($ccode);
566 # Only do the reverse lookup if the system is online.
567 if (&red_is_active
()) {
568 my $iaddr = inet_aton
($nameserver);
569 $rdns = gethostbyaddr($iaddr, AF_INET
);
572 if (!$rdns) { $rdns = $Lang::tr
{'lookup failed'}; }
574 # Mark ISP name servers as disabled
575 if ($id <= 2 && $enabled eq "disabled") {
576 $nameserver = "<del>$nameserver</del>";
581 <td align="center" $col>
585 <td align="center" $col>
586 <a href='country.cgi#$ccode'><img src="$flag_icon" border="0" alt="$ccode" title="$ccode" /></a>
589 <td align="center" $col>
593 <td align="center" $col>
598 # Display server status if requested.
599 if ($check_servers) {
601 <td align="center" $col>
602 <strong><font color="$status_colour"><abbr title="$status_message">$status_short</abbr></font></strong>
608 # Check if the id is greater than "2".
610 # Nameservers with an ID's of one or two are ISP assigned,
611 # and we cannot perform any actions on them, so hide the tools for
616 <td align='center' width='5%' $col>
617 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
618 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' title='$gdesc' alt='$gdesc' />
619 <input type='hidden' name='ID' value='$id' />
620 <input type='hidden' name='ENABLE' value='$toggle' />
621 <input type='hidden' name='SERVERS' value='$Lang::tr{'toggle enable disable'}' />
625 <td align='center' width='5%' $col>
626 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
627 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' title='$Lang::tr{'edit'}' alt='$Lang::tr{'edit'}' />
628 <input type='hidden' name='ID' value='$id' />
629 <input type='hidden' name='SERVERS' value='$Lang::tr{'edit'}' />
633 <td align='center' width='5%' $col>
634 <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>
635 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}' />
636 <input type='hidden' name='ID' value='$id' />
637 <input type='hidden' name='SERVERS' value='$Lang::tr{'remove'}' />
643 print "<td colspan='3' $col> </td>\n";
653 print"<table width='100%'>\n";
655 # Check if the usage of the ISP nameservers is enabled and there are more than 2 servers.
656 if (($settings{'USE_ISP_NAMESERVERS'} eq "on") && ($server_amount gt "2")) {
659 <td class='boldbase'> <b>$Lang::tr{'legend'}:</b></td>
660 <td> <img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
661 <td class='base'>$Lang::tr{'click to disable'}</td>
662 <td> <img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
663 <td class='base'>$Lang::tr{'click to enable'}</td>
664 <td> <img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
665 <td class='base'>$Lang::tr{'edit'}</td>
666 <td> <img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
667 <td class='base'>$Lang::tr{'remove'}</td>
674 <form method="post" action="$ENV{'SCRIPT_NAME'}">
675 <td colspan="9" align="right">
676 <input type="submit" name="SERVERS" value="$Lang::tr{'add'}">
677 <input type="submit" name="SERVERS" value="$Lang::tr{'dns check servers'}">
689 <td colspan="6" align="center">
690 <br>$Lang::tr{'guardian no entries'}<br>
695 <form method="post" action="$ENV{'SCRIPT_NAME'}">
696 <td colspan="6" align="right"><input type="submit" name="SERVERS" value="$Lang::tr{'add'}"></td>
709 # Section to display the add or edit subpage.
711 sub show_add_edit_nameserver
() {
712 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
714 my $buttontext = $Lang::tr
{'save'};
717 if ($cgiparams{'SERVERS'} eq $Lang::tr
{'edit'}) {
718 &Header
::openbox
('100%', 'left', $Lang::tr
{'dnsforward edit an entry'});
720 # Update button text for upate the existing entry.
721 $buttontext = $Lang::tr
{'update'};
723 # Add hidden input for sending ID.
724 print"<input type='hidden' name='ID' value='$cgiparams{'ID'}'>\n";
726 # Check if an ID has been given.
727 if ($cgiparams{'ID'}) {
728 # Assign cgiparams values.
729 $cgiparams{'NAMESERVER'} = $dns_servers{$cgiparams{'ID'}}[0];
730 $cgiparams{'TLS_HOSTNAME'} = $dns_servers{$cgiparams{'ID'}}[1];
731 $cgiparams{'REMARK'} = $dns_servers{$cgiparams{'ID'}}[3];
734 &Header
::openbox
('100%', 'left', $Lang::tr
{'dnsforward add a new entry'});
737 my $tls_required_image;
739 # If the protocol is TLS, dispaly the required image.
740 if ($settings{'PROTO'} eq "TLS") {
741 $tls_required_image = "<img src='/blob.gif' alt='*'>";
744 # Add hidden input to store the mode.
745 print "<input type='hidden' name='MODE' value='$cgiparams{'SERVERS'}'>\n";
750 <td width='20%' class='base'>$Lang::tr{'ip address'}: <img src='/blob.gif' alt='*' /></td>
751 <td><input type='text' name='NAMESERVER' value='$cgiparams{"NAMESERVER"}' size='24' /></td>
756 <td width='20%' class='base'>$Lang::tr{'dns tls hostname'}: $tls_required_image</td>
757 <td><input type='text' name='TLS_HOSTNAME' value='$cgiparams{'TLS_HOSTNAME'}' size='24'></td>
762 <td width ='20%' class='base'>$Lang::tr{'remark'}:</td>
763 <td><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' maxlength='50' /></td>
772 <td class='base' width='55%'><img src='/blob.gif' alt ='*' align='top' /> $Lang::tr{'required field'}</td>
773 <td width='40%' align='right'>
774 <input type="submit" name="SERVERS" value="$buttontext">
775 <input type="submit" name="SERVERS" value="$Lang::tr{'back'}">
788 # Check if the system is online (RED is connected).
789 sub red_is_active
() {
790 # Check if the "active" file is present.
791 if ( -f
"${General::swroot}/red/active") {
795 # Return nothing - False.
800 # Tiny function to grab an IP-address of a given file.
801 sub grab_address_from_file
($) {
806 # Check if the given file exists.
808 # Open the file for reading.
809 open(FILE
, $file) or die "Could not read from $file. $!\n";
811 # Read the address from the file.
820 # Check if the obtained address is valid.
821 if (&General
::validip
($address)) {
822 # Return the address.
831 # Function to check a given nameserver against propper work.
832 sub check_nameserver
($$$$) {
833 my ($nameserver, $record, $proto, $tls_hostname) = @_;
835 # Check if the system is online.
836 unless (&red_is_active
()) {
837 return "$Lang::tr{'system is offline'}";
841 my @command = ("kdig", "+timeout=2", "+retry=0", "+dnssec",
844 # Handle different protols.
845 if ($proto eq "TCP") {
846 # Add TCP switch to the command.
847 push(@command, "+tcp");
849 } elsif($proto eq "TLS") {
850 # Add TLS switch to the command and provide the
851 # path to our file which contains the ca certs.
852 push(@command, "+tls-ca=$ca_certs_file");
854 # Check if a TLS hostname has been provided.
856 # Add TLS hostname to the command.
857 push(@command, "+tls-hostname=$tls_hostname");
859 return "$Lang::tr{'dns no tls hostname given'}";
863 # Add record to the command array.
864 push(@command, "$record");
866 # Add nameserver to the command array.
867 push(@command, "\@$nameserver");
869 # Connect to STDOUT and STDERR.
870 push(@command, "2>&1");
872 my @output = qx(@command);
873 my $output = join("", @output);
877 if ($output =~ m/status: (\w+)/) {
878 $status = ($1 eq "NOERROR");
886 while ($output =~ m/WARNING: (.*)/g) {
887 # Add the current grabbed warning to the warning string.
891 # Return the warning string, if we grabbed at least one.
898 if ($output =~ m/Flags: (.*);/) {
899 @flags = split(/ /, $1);
902 my $aware = ($output =~ m/RRSIG/);
903 my $validating = (grep(/ad;/, @flags));
905 return $aware + $validating;