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";
62 my %mainsettings = ();
63 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
64 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
66 &Header
::showhttpheaders
();
67 &Header
::getcgihash
(\
%cgiparams);
70 # Save general settings.
72 if ($cgiparams{'GENERAL'} eq $Lang::tr
{'save'}) {
73 # Prevent form name from been stored in conf file.
74 delete $cgiparams{'GENERAL'};
76 # Add value for non-checked checkbox.
77 if ($cgiparams{'USE_ISP_NAMESERVERS'} ne "on") {
78 $cgiparams{'USE_ISP_NAMESERVERS'} = "off";
81 # Add value for non-checked checkbox.
82 if ($cgiparams{'ENABLE_SAFE_SEARCH'} ne "on") {
83 $cgiparams{'ENABLE_SAFE_SEARCH'} = "off";
86 # Store settings into settings file.
87 &General
::writehash
("$settings_file", \
%cgiparams);
93 if (($cgiparams{'SERVERS'} eq $Lang::tr
{'save'}) || ($cgiparams{'SERVERS'} eq $Lang::tr
{'update'})) {
94 # Hash to store the generic DNS settings.
97 # Read-in generic settings.
98 &General
::readhash
("$settings_file", \
%settings);
100 # Check if an IP-address has been given.
101 if ($cgiparams{"NAMESERVER"} eq "") {
102 $errormessage = "$Lang::tr{'dns no address given'}";
105 # Check if the given DNS server is valid.
106 elsif(!&General
::validip
($cgiparams{"NAMESERVER"})) {
107 $errormessage = "$Lang::tr{'invalid ip'}: $cgiparams{'NAMESERVER'}";
110 # Check if a TLS is enabled and no TLS_HOSTNAME has benn specified.
111 elsif($settings{'PROTO'} eq "TLS") {
112 unless($cgiparams{"TLS_HOSTNAME"}) {
113 $errormessage = "$Lang::tr{'dns no tls hostname given'}";
115 # Check if the provided domain is valid.
116 unless(&General
::validfqdn
($cgiparams{"TLS_HOSTNAME"})) {
117 $errormessage = "$Lang::tr{'invalid ip or hostname'}: $cgiparams{'TLS_HOSTNAME'}";
122 # Go further if there was no error.
123 if ( ! $errormessage) {
124 # Check if a remark has been entered.
125 $cgiparams{'REMARK'} = &Header
::cleanhtml
($cgiparams{'REMARK'});
127 my %dns_servers = ();
131 # Read-in configfile.
132 &General
::readhasharray
($servers_file, \
%dns_servers);
134 # Check if we should edit an existing entry and got an ID.
135 if (($cgiparams{'SERVERS'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
136 # Assin the provided id.
137 $id = $cgiparams{'ID'};
139 # Undef the given ID.
140 undef($cgiparams{'ID'});
142 # Grab the configured status of the corresponding entry.
143 $status = $dns_servers{$id}[2];
145 # Each newly added entry automatically should be enabled.
148 # Generate the ID for the new entry.
150 # Sort the keys by their ID and store them in an array.
151 my @keys = sort { $a <=> $b } keys %dns_servers;
153 # Reverse the key array.
154 my @reversed = reverse(@keys);
156 # Obtain the last used id.
157 my $last_id = @reversed[0];
159 # Increase the last id by one and use it as id for the new entry.
162 # The first allowed id is 3 to keep space for
163 # possible ISP assigned DNS servers.
169 # Add/Modify the entry to/in the dns_servers hash.
170 $dns_servers{$id} = ["$cgiparams{'NAMESERVER'}", "$cgiparams{'TLS_HOSTNAME'}", "$status", "$cgiparams{'REMARK'}"];
172 # Write the changed hash to the config file.
173 &General
::writehasharray
($servers_file, \
%dns_servers);
175 # Switch back to previous mode.
176 $cgiparams{'SERVERS'} = $cgiparams{'MODE'};
179 # Toggle enable / disable.
181 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr
{'toggle enable disable'}) {
182 my %dns_servers = ();
184 # Only go further, if an ID has been passed.
185 if ($cgiparams{'ID'}) {
186 # Assign the given ID.
187 my $id = $cgiparams{'ID'};
189 # Undef the given ID.
190 undef($cgiparams{'ID'});
192 # Read-in configfile.
193 &General
::readhasharray
($servers_file, \
%dns_servers);
195 # Grab the configured status of the corresponding entry.
196 my $status = $dns_servers{$id}[2];
199 if ($status eq "disabled") {
202 $status = "disabled";
205 # Modify the status of the existing entry.
206 $dns_servers{$id} = ["$dns_servers{$id}[0]", "$dns_servers{$id}[1]", "$status", "$dns_servers{$id}[3]"];
208 # Write the changed hash back to the config file.
209 &General
::writehasharray
($servers_file, \
%dns_servers);
212 ## Remove entry from DNS servers list.
214 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr
{'remove'}) {
215 my %dns_servers = ();
217 # Read-in configfile.
218 &General
::readhasharray
($servers_file, \
%dns_servers);
220 # Drop entry from the hash.
221 delete($dns_servers{$cgiparams{'ID'}});
223 # Undef the given ID.
224 undef($cgiparams{'ID'});
226 # Write the changed hash to the config file.
227 &General
::writehasharray
($servers_file, \
%dns_servers);
229 ## Handle request to check the servers.
231 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr
{'dns check servers'}) {
235 # Hash to store the generic DNS settings.
238 # Read-in general DNS settings.
239 &General
::readhash
("$settings_file", \
%settings);
241 # Hash which contains the configured DNS servers.
242 my %dns_servers = ();
244 # Read-in config file.
245 &General
::readhasharray
("$servers_file", \
%dns_servers);
247 &Header
::openpage
($Lang::tr
{'dns'}, 1, '');
249 &Header
::openbigbox
('100%', 'left', '', $errormessage);
252 # Error messages layout.
255 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
256 print "<class name='base'>$errormessage\n";
257 print " </class>\n";
261 # Handle if a nameserver should be added or edited.
262 if (($cgiparams{'SERVERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'SERVERS'} eq "$Lang::tr{'edit'}")) {
263 # Display the sub page.
264 &show_add_edit_nameserver
();
267 &Header
::closebigbox
();
268 &Header
::closepage
();
270 # Finished here for the moment.
274 $cgiparams{'GENERAL'} = '';
275 $cgiparams{'SERVERS'} = '';
276 $cgiparams{'NAMESERVER'} = '';
277 $cgiparams{'TLS_HOSTNAME'} = '';
278 $cgiparams{'REMARK'} ='';
280 $checked{'USE_ISP_NAMESERVERS'}{'off'} = '';
281 $checked{'USE_ISP_NAMESERVERS'}{'on'} = '';
282 $checked{'USE_ISP_NAMESERVERS'}{$settings{'USE_ISP_NAMESERVERS'}} = "checked='checked'";
284 $checked{'ENABLE_SAFE_SEARCH'}{'off'} = '';
285 $checked{'ENABLE_SAFE_SEARCH'}{'on'} = '';
286 $checked{'ENABLE_SAFE_SEARCH'}{$settings{'ENABLE_SAFE_SEARCH'}} = "checked='checked'";
288 $selected{'PROTO'}{'UDP'} = '';
289 $selected{'PROTO'}{'TLS'} = '';
290 $selected{'PROTO'}{'TCP'} = '';
291 $selected{'PROTO'}{$settings{'PROTO'}} = "selected='selected'";
293 $selected{'QNAME_MIN'}{'standard'} = '';
294 $selected{'QNAME_MIN'}{'strict'} = '';
295 $selected{'QNAME_MIN'}{$settings{'QNAME_MIN'}} = "selected='selected'";
297 # Display nameserver and configuration sections.
299 &show_general_dns_configuration
();
301 &Header
::closebigbox
();
302 &Header
::closepage
();
305 # General DNS-Servers sektion.
307 sub show_general_dns_configuration
() {
308 &Header
::openbox
('100%', 'center', "$Lang::tr{'dns configuration'}");
311 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
315 $Lang::tr{'dns use isp assigned nameservers'}
319 <input type="checkbox" name="USE_ISP_NAMESERVERS" $checked{'USE_ISP_NAMESERVERS'}{'on'}>
331 $Lang::tr{'dns use protocol for dns queries'}
335 <select name="PROTO">
336 <option value="UDP" $selected{'PROTO'}{'UDP'}>UDP</option>
337 <option value="TLS" $selected{'PROTO'}{'TLS'}>TLS</option>
338 <option value="TCP" $selected{'PROTO'}{'TCP'}>TCP</option>
351 $Lang::tr{'dns enable safe-search'}
355 <input type="checkbox" name="ENABLE_SAFE_SEARCH" $checked{'ENABLE_SAFE_SEARCH'}{'on'}>
367 $Lang::tr{'dns mode for qname minimisation'}
371 <select name="QNAME_MIN">
372 <option value="standard" $selected{'QNAME_MIN'}{'standard'}>$Lang::tr{'standard'}</option>
373 <option value="strict" $selected{'QNAME_MIN'}{'strict'}>$Lang::tr{'strict'}</option>
379 <td colspan="2" align="right">
380 <input type="submit" name="GENERAL" value="$Lang::tr{'save'}">
391 # Section to display the configured and used DNS servers.
393 sub show_nameservers
() {
394 &Header
::openbox
('100%', 'center', "$Lang::tr{'dns title'}");
397 <table class="tbl" width='100%'>
400 <strong>$Lang::tr{'nameserver'}</strong>
404 <strong>$Lang::tr{'country'}</strong>
408 <strong>$Lang::tr{'rdns'}</strong>
412 <strong>$Lang::tr{'remark'}</strong>
415 # Check if the status should be displayed.
416 if ($check_servers) {
419 <strong>$Lang::tr{'status'}</strong>
427 <td align="center" colspan="3">
428 <strong>$Lang::tr{'action'}</strong>
434 # Check the usage of ISP assigned nameservers is enabled.
437 # Loop through the array which stores the files.
438 foreach my $file (@ISP_nameserver_files) {
439 # Grab the address of the nameserver.
440 my $address = &grab_address_from_file
($file);
442 # Check if we got an address.
444 # Add the address to the hash of nameservers.
445 $dns_servers{$id} = [ "$address", "none",
446 ($settings{'USE_ISP_NAMESERVERS'} eq "on") ?
"enabled" : "disabled",
447 "$Lang::tr{'dns isp assigned nameserver'}" ];
449 # Increase id by one.
454 # Check some DNS servers have been configured. In this case
455 # the hash contains at least one key.
457 if (keys %dns_servers) {
458 # Sort the keys by their ID and store them in an array.
459 my @keys = sort { $a <=> $b } keys %dns_servers;
461 # Loop through all entries of the array/hash.
462 foreach my $id (@keys) {
463 # Inrease server_amount.
466 # Assign data array positions to some nice variable names.
467 my $nameserver = $dns_servers{$id}[0];
468 my $tls_hostname = $dns_servers{$id}[1];
469 my $enabled = $dns_servers{$id}[2];
470 my $remark = $dns_servers{$id}[3];
479 if ($server_amount % 2) {
480 $col="bgcolor='$color{'color22'}'"; }
482 $col="bgcolor='$color{'color20'}'";
485 if ($enabled eq 'enabled') {
486 $gif='on.gif'; $toggle='off'; $gdesc=$Lang::tr
{'click to disable'};
488 $gif='off.gif'; $toggle='on'; $gdesc=$Lang::tr
{'click to enable'};
496 # Only grab the status if the nameserver is enabled.
497 if (($check_servers) && ($enabled eq "enabled")) {
498 $status = &check_nameserver
("$nameserver", "ping.ipfire.org", "$settings{'PROTO'}", "$tls_hostname");
502 $status_short = "$Lang::tr{'disabled'}";
504 # DNSSEC Not supported
505 } elsif ($status eq 0) {
506 $status_short = "$Lang::tr{'broken'}";
507 $status_message = $Lang::tr
{'dnssec not supported'};
508 $status_colour = ${Header
::colourred
};
511 } elsif ($status eq 1) {
512 $status_short = "$Lang::tr{'not validating'}";
513 $status_message = $Lang::tr
{'dnssec aware'};
514 $status_colour = ${Header
::colourblack
};
517 } elsif ($status eq 2) {
518 $status_short = "$Lang::tr{'ok'}";
519 $status_message = $Lang::tr
{'dnssec validating'};
520 $status_colour = ${Header
::colourgreen
};
524 $status_short = "$Lang::tr{'error'}";
525 $status_message = $status;
526 $status_colour = ${Header
::colourred
};
529 # collect more information about name server (rDNS, GeoIP country code)
530 my $ccode = &GeoIP
::lookup
($nameserver);
531 my $flag_icon = &GeoIP
::get_flag_icon
($ccode);
535 # Only do the reverse lookup if the system is online.
536 if (&red_is_active
()) {
537 my $iaddr = inet_aton
($nameserver);
538 $rdns = gethostbyaddr($iaddr, AF_INET
);
541 if (!$rdns) { $rdns = $Lang::tr
{'lookup failed'}; }
543 # Mark ISP name servers as disabled
544 if ($id <= 2 && $enabled eq "disabled") {
545 $nameserver = "<del>$nameserver</del>";
550 <td align="center" $col>
554 <td align="center" $col>
555 <a href='country.cgi#$ccode'><img src="$flag_icon" border="0" alt="$ccode" title="$ccode" /></a>
558 <td align="center" $col>
562 <td align="center" $col>
567 # Display server status if requested.
568 if ($check_servers) {
570 <td align="center" $col>
571 <strong><font color="$status_colour"><abbr title="$status_message">$status_short</abbr></font></strong>
577 # Check if the id is greater than "2".
579 # Nameservers with an ID's of one or two are ISP assigned,
580 # and we cannot perform any actions on them, so hide the tools for
585 <td align='center' width='5%' $col>
586 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
587 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' title='$gdesc' alt='$gdesc' />
588 <input type='hidden' name='ID' value='$id' />
589 <input type='hidden' name='ENABLE' value='$toggle' />
590 <input type='hidden' name='SERVERS' value='$Lang::tr{'toggle enable disable'}' />
594 <td align='center' width='5%' $col>
595 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
596 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' title='$Lang::tr{'edit'}' alt='$Lang::tr{'edit'}' />
597 <input type='hidden' name='ID' value='$id' />
598 <input type='hidden' name='SERVERS' value='$Lang::tr{'edit'}' />
602 <td align='center' width='5%' $col>
603 <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>
604 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}' />
605 <input type='hidden' name='ID' value='$id' />
606 <input type='hidden' name='SERVERS' value='$Lang::tr{'remove'}' />
612 print "<td colspan='3' $col> </td>\n";
622 print"<table width='100%'>\n";
624 # Check if the usage of the ISP nameservers is enabled and there are more than 2 servers.
625 if (($settings{'USE_ISP_NAMESERVERS'} eq "on") && ($server_amount gt "2")) {
628 <td class='boldbase'> <b>$Lang::tr{'legend'}:</b></td>
629 <td> <img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
630 <td class='base'>$Lang::tr{'click to disable'}</td>
631 <td> <img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
632 <td class='base'>$Lang::tr{'click to enable'}</td>
633 <td> <img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
634 <td class='base'>$Lang::tr{'edit'}</td>
635 <td> <img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
636 <td class='base'>$Lang::tr{'remove'}</td>
643 <form method="post" action="$ENV{'SCRIPT_NAME'}">
644 <td colspan="9" align="right">
645 <input type="submit" name="SERVERS" value="$Lang::tr{'add'}">
646 <input type="submit" name="SERVERS" value="$Lang::tr{'dns check servers'}">
658 <td colspan="6" align="center">
659 <br>$Lang::tr{'guardian no entries'}<br>
664 <form method="post" action="$ENV{'SCRIPT_NAME'}">
665 <td colspan="6" align="right"><input type="submit" name="SERVERS" value="$Lang::tr{'add'}"></td>
678 # Section to display the add or edit subpage.
680 sub show_add_edit_nameserver
() {
681 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
683 my $buttontext = $Lang::tr
{'save'};
686 if ($cgiparams{'SERVERS'} eq $Lang::tr
{'edit'}) {
687 &Header
::openbox
('100%', 'left', $Lang::tr
{'dnsforward edit an entry'});
689 # Update button text for upate the existing entry.
690 $buttontext = $Lang::tr
{'update'};
692 # Add hidden input for sending ID.
693 print"<input type='hidden' name='ID' value='$cgiparams{'ID'}'>\n";
695 # Check if an ID has been given.
696 if ($cgiparams{'ID'}) {
697 # Assign cgiparams values.
698 $cgiparams{'NAMESERVER'} = $dns_servers{$cgiparams{'ID'}}[0];
699 $cgiparams{'TLS_HOSTNAME'} = $dns_servers{$cgiparams{'ID'}}[1];
700 $cgiparams{'REMARK'} = $dns_servers{$cgiparams{'ID'}}[3];
703 &Header
::openbox
('100%', 'left', $Lang::tr
{'dnsforward add a new entry'});
706 my $tls_required_image;
708 # If the protocol is TLS, dispaly the required image.
709 if ($settings{'PROTO'} eq "TLS") {
710 $tls_required_image = "<img src='/blob.gif' alt='*'>";
713 # Add hidden input to store the mode.
714 print "<input type='hidden' name='MODE' value='$cgiparams{'SERVERS'}'>\n";
719 <td width='20%' class='base'>$Lang::tr{'ip address'}: <img src='/blob.gif' alt='*' /></td>
720 <td><input type='text' name='NAMESERVER' value='$cgiparams{"NAMESERVER"}' size='24' /></td>
725 <td width='20%' class='base'>$Lang::tr{'dns tls hostname'}: $tls_required_image</td>
726 <td><input type='text' name='TLS_HOSTNAME' value='$cgiparams{'TLS_HOSTNAME'}' size='24'></td>
731 <td width ='20%' class='base'>$Lang::tr{'remark'}:</td>
732 <td><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' maxlength='50' /></td>
741 <td class='base' width='55%'><img src='/blob.gif' alt ='*' align='top' /> $Lang::tr{'required field'}</td>
742 <td width='40%' align='right'>
743 <input type="submit" name="SERVERS" value="$buttontext">
744 <input type="submit" name="SERVERS" value="$Lang::tr{'back'}">
757 # Check if the system is online (RED is connected).
758 sub red_is_active
() {
759 # Check if the "active" file is present.
760 if ( -f
"${General::swroot}/red/active") {
764 # Return nothing - False.
769 # Tiny function to grab an IP-address of a given file.
770 sub grab_address_from_file
($) {
775 # Check if the given file exists.
777 # Open the file for reading.
778 open(FILE
, $file) or die "Could not read from $file. $!\n";
780 # Read the address from the file.
789 # Check if the obtained address is valid.
790 if (&General
::validip
($address)) {
791 # Return the address.
800 # Function to check a given nameserver against propper work.
801 sub check_nameserver
($$$$) {
802 my ($nameserver, $record, $proto, $tls_hostname) = @_;
804 # Check if the system is online.
805 unless (&red_is_active
()) {
806 return "$Lang::tr{'system is offline'}";
810 my @command = ("kdig", "+timeout=2", "+retry=0", "+dnssec",
813 # Handle different protols.
814 if ($proto eq "TCP") {
815 # Add TCP switch to the command.
816 push(@command, "+tcp");
818 } elsif($proto eq "TLS") {
819 # Add TLS switch to the command and provide the
820 # path to our file which contains the ca certs.
821 push(@command, "+tls-ca=$ca_certs_file");
823 # Check if a TLS hostname has been provided.
825 # Add TLS hostname to the command.
826 push(@command, "+tls-hostname=$tls_hostname");
828 return "$Lang::tr{'dns no tls hostname given'}";
832 # Add record to the command array.
833 push(@command, "$record");
835 # Add nameserver to the command array.
836 push(@command, "\@$nameserver");
838 # Connect to STDOUT and STDERR.
839 push(@command, "2>&1");
841 my @output = qx(@command);
842 my $output = join("", @output);
846 if ($output =~ m/status: (\w+)/) {
847 $status = ($1 eq "NOERROR");
855 while ($output =~ m/WARNING: (.*)/g) {
856 # Add the current grabbed warning to the warning string.
860 # Return the warning string, if we grabbed at least one.
867 if ($output =~ m/Flags: (.*);/) {
868 @flags = split(/ /, $1);
871 my $aware = ($output =~ m/RRSIG/);
872 my $validating = (grep(/ad;/, @flags));
874 return $aware + $validating;