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";
60 my %mainsettings = ();
61 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
62 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
64 &Header
::showhttpheaders
();
65 &Header
::getcgihash
(\
%cgiparams);
68 # Save general settings.
70 if ($cgiparams{'GENERAL'} eq $Lang::tr
{'save'}) {
71 # Prevent form name from been stored in conf file.
72 delete $cgiparams{'GENERAL'};
74 # Add value for non-checked checkbox.
75 if ($cgiparams{'USE_ISP_NAMESERVERS'} ne "on") {
76 $cgiparams{'USE_ISP_NAMESERVERS'} = "off";
79 # Add value for non-checked checkbox.
80 if ($cgiparams{'ENABLE_SAFE_SEARCH'} ne "on") {
81 $cgiparams{'ENABLE_SAFE_SEARCH'} = "off";
84 # Store settings into settings file.
85 &General
::writehash
("$settings_file", \
%cgiparams);
91 if (($cgiparams{'SERVERS'} eq $Lang::tr
{'save'}) || ($cgiparams{'SERVERS'} eq $Lang::tr
{'update'})) {
92 # Hash to store the generic DNS settings.
95 # Read-in generic settings.
96 &General
::readhash
("$settings_file", \
%settings);
98 # Check if an IP-address has been given.
99 if ($cgiparams{"NAMESERVER"} eq "") {
100 $errormessage = "$Lang::tr{'dns no address given'}";
103 # Check if the given DNS server is valid.
104 elsif(!&General
::validip
($cgiparams{"NAMESERVER"})) {
105 $errormessage = "$Lang::tr{'invalid ip'}: $cgiparams{'NAMESERVER'}";
108 # Check if a TLS is enabled and no TLS_HOSTNAME has benn specified.
109 elsif($settings{'PROTO'} eq "TLS") {
110 unless($cgiparams{"TLS_HOSTNAME"}) {
111 $errormessage = "$Lang::tr{'dns no tls hostname given'}";
113 # Check if the provided domain is valid.
114 unless(&General
::validfqdn
($cgiparams{"TLS_HOSTNAME"})) {
115 $errormessage = "$Lang::tr{'invalid ip or hostname'}: $cgiparams{'TLS_HOSTNAME'}";
120 # Go further if there was no error.
121 if ( ! $errormessage) {
122 # Check if a remark has been entered.
123 $cgiparams{'REMARK'} = &Header
::cleanhtml
($cgiparams{'REMARK'});
125 my %dns_servers = ();
129 # Read-in configfile.
130 &General
::readhasharray
($servers_file, \
%dns_servers);
132 # Check if we should edit an existing entry and got an ID.
133 if (($cgiparams{'SERVERS'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
134 # Assin the provided id.
135 $id = $cgiparams{'ID'};
137 # Undef the given ID.
138 undef($cgiparams{'ID'});
140 # Grab the configured status of the corresponding entry.
141 $status = $dns_servers{$id}[2];
143 # Each newly added entry automatically should be enabled.
146 # Generate the ID for the new entry.
148 # Sort the keys by their ID and store them in an array.
149 my @keys = sort { $a <=> $b } keys %dns_servers;
151 # Reverse the key array.
152 my @reversed = reverse(@keys);
154 # Obtain the last used id.
155 my $last_id = @reversed[0];
157 # Increase the last id by one and use it as id for the new entry.
160 # The first allowed id is 3 to keep space for
161 # possible ISP assigned DNS servers.
167 # Add/Modify the entry to/in the dns_servers hash.
168 $dns_servers{$id} = ["$cgiparams{'NAMESERVER'}", "$cgiparams{'TLS_HOSTNAME'}", "$status", "$cgiparams{'REMARK'}"];
170 # Write the changed hash to the config file.
171 &General
::writehasharray
($servers_file, \
%dns_servers);
173 # Switch back to previous mode.
174 $cgiparams{'SERVERS'} = $cgiparams{'MODE'};
177 # Toggle enable / disable.
179 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr
{'toggle enable disable'}) {
180 my %dns_servers = ();
182 # Only go further, if an ID has been passed.
183 if ($cgiparams{'ID'}) {
184 # Assign the given ID.
185 my $id = $cgiparams{'ID'};
187 # Undef the given ID.
188 undef($cgiparams{'ID'});
190 # Read-in configfile.
191 &General
::readhasharray
($servers_file, \
%dns_servers);
193 # Grab the configured status of the corresponding entry.
194 my $status = $dns_servers{$id}[2];
197 if ($status eq "disabled") {
200 $status = "disabled";
203 # Modify the status of the existing entry.
204 $dns_servers{$id} = ["$dns_servers{$id}[0]", "$dns_servers{$id}[1]", "$status", "$dns_servers{$id}[3]"];
206 # Write the changed hash back to the config file.
207 &General
::writehasharray
($servers_file, \
%dns_servers);
210 ## Remove entry from DNS servers list.
212 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr
{'remove'}) {
213 my %dns_servers = ();
215 # Read-in configfile.
216 &General
::readhasharray
($servers_file, \
%dns_servers);
218 # Drop entry from the hash.
219 delete($dns_servers{$cgiparams{'ID'}});
221 # Undef the given ID.
222 undef($cgiparams{'ID'});
224 # Write the changed hash to the config file.
225 &General
::writehasharray
($servers_file, \
%dns_servers);
228 # Hash to store the generic DNS settings.
231 # Read-in general DNS settings.
232 &General
::readhash
("$settings_file", \
%settings);
234 # Hash which contains the configured DNS servers.
235 my %dns_servers = ();
237 # Read-in config file.
238 &General
::readhasharray
("$servers_file", \
%dns_servers);
240 &Header
::openpage
($Lang::tr
{'dns'}, 1, '');
242 &Header
::openbigbox
('100%', 'left', '', $errormessage);
245 # Error messages layout.
248 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
249 print "<class name='base'>$errormessage\n";
250 print " </class>\n";
254 # Handle if a nameserver should be added or edited.
255 if (($cgiparams{'SERVERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'SERVERS'} eq "$Lang::tr{'edit'}")) {
256 # Display the sub page.
257 &show_add_edit_nameserver
();
260 &Header
::closebigbox
();
261 &Header
::closepage
();
263 # Finished here for the moment.
267 $cgiparams{'GENERAL'} = '';
268 $cgiparams{'SERVERS'} = '';
269 $cgiparams{'NAMESERVER'} = '';
270 $cgiparams{'TLS_HOSTNAME'} = '';
271 $cgiparams{'REMARK'} ='';
273 $checked{'USE_ISP_NAMESERVERS'}{'off'} = '';
274 $checked{'USE_ISP_NAMESERVERS'}{'on'} = '';
275 $checked{'USE_ISP_NAMESERVERS'}{$settings{'USE_ISP_NAMESERVERS'}} = "checked='checked'";
277 $checked{'ENABLE_SAFE_SEARCH'}{'off'} = '';
278 $checked{'ENABLE_SAFE_SEARCH'}{'on'} = '';
279 $checked{'ENABLE_SAFE_SEARCH'}{$settings{'ENABLE_SAFE_SEARCH'}} = "checked='checked'";
281 $selected{'PROTO'}{'UDP'} = '';
282 $selected{'PROTO'}{'TLS'} = '';
283 $selected{'PROTO'}{'TCP'} = '';
284 $selected{'PROTO'}{$settings{'PROTO'}} = "selected='selected'";
286 $selected{'QNAME_MIN'}{'standard'} = '';
287 $selected{'QNAME_MIN'}{'strict'} = '';
288 $selected{'QNAME_MIN'}{$settings{'QNAME_MIN'}} = "selected='selected'";
290 # Display nameserver and configuration sections.
292 &show_general_dns_configuration
();
294 &Header
::closebigbox
();
295 &Header
::closepage
();
298 # General DNS-Servers sektion.
300 sub show_general_dns_configuration
() {
301 &Header
::openbox
('100%', 'center', "$Lang::tr{'dns configuration'}");
304 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
308 $Lang::tr{'dns use isp assigned nameservers'}
312 <input type="checkbox" name="USE_ISP_NAMESERVERS" $checked{'USE_ISP_NAMESERVERS'}{'on'}>
324 $Lang::tr{'dns use protocol for dns queries'}
328 <select name="PROTO">
329 <option value="UDP" $selected{'PROTO'}{'UDP'}>UDP</option>
330 <option value="TLS" $selected{'PROTO'}{'TLS'}>TLS</option>
331 <option value="TCP" $selected{'PROTO'}{'TCP'}>TCP</option>
344 $Lang::tr{'dns enable safe-search'}
348 <input type="checkbox" name="ENABLE_SAFE_SEARCH" $checked{'ENABLE_SAFE_SEARCH'}{'on'}>
360 $Lang::tr{'dns mode for qname minimisation'}
364 <select name="QNAME_MIN">
365 <option value="standard" $selected{'QNAME_MIN'}{'standard'}>$Lang::tr{'standard'}</option>
366 <option value="strict" $selected{'QNAME_MIN'}{'strict'}>$Lang::tr{'strict'}</option>
372 <td colspan="2" align="right">
373 <input type="submit" name="GENERAL" value="$Lang::tr{'save'}">
384 # Section to display the configured and used DNS servers.
386 sub show_nameservers
() {
387 &Header
::openbox
('100%', 'center', "$Lang::tr{'dns title'}");
390 <table class="tbl" width='100%'>
393 <strong>$Lang::tr{'nameserver'}</strong>
397 <strong>$Lang::tr{'country'}</strong>
401 <strong>$Lang::tr{'rdns'}</strong>
405 <strong>$Lang::tr{'remark'}</strong>
409 <strong>$Lang::tr{'status'}</strong>
412 <td align="center" colspan="3">
413 <strong>$Lang::tr{'action'}</strong>
418 # Check the usage of ISP assigned nameservers is enabled.
421 # Loop through the array which stores the files.
422 foreach my $file (@ISP_nameserver_files) {
423 # Grab the address of the nameserver.
424 my $address = &grab_address_from_file
($file);
426 # Check if we got an address.
428 # Add the address to the hash of nameservers.
429 $dns_servers{$id} = [ "$address", "none",
430 ($settings{'USE_ISP_NAMESERVERS'} eq "on") ?
"enabled" : "disabled",
431 "$Lang::tr{'dns isp assigned nameserver'}" ];
433 # Increase id by one.
438 # Check some DNS servers have been configured. In this case
439 # the hash contains at least one key.
441 if (keys %dns_servers) {
442 # Sort the keys by their ID and store them in an array.
443 my @keys = sort { $a <=> $b } keys %dns_servers;
445 # Loop through all entries of the array/hash.
446 foreach my $id (@keys) {
447 # Inrease server_amount.
450 # Assign data array positions to some nice variable names.
451 my $nameserver = $dns_servers{$id}[0];
452 my $tls_hostname = $dns_servers{$id}[1];
453 my $enabled = $dns_servers{$id}[2];
454 my $remark = $dns_servers{$id}[3];
463 if ($server_amount % 2) {
464 $col="bgcolor='$color{'color22'}'"; }
466 $col="bgcolor='$color{'color20'}'";
469 if ($enabled eq 'enabled') {
470 $gif='on.gif'; $toggle='off'; $gdesc=$Lang::tr
{'click to disable'};
472 $gif='off.gif'; $toggle='on'; $gdesc=$Lang::tr
{'click to enable'};
480 # Only grab the status if the nameserver is enabled.
481 if ($enabled eq "enabled") {
482 $status = &check_nameserver
("$nameserver", "ping.ipfire.org", "$settings{'PROTO'}", "$tls_hostname");
486 $status_short = "$Lang::tr{'disabled'}";
488 # DNSSEC Not supported
489 } elsif ($status eq 0) {
490 $status_short = "$Lang::tr{'broken'}";
491 $status_message = $Lang::tr
{'dnssec not supported'};
492 $status_colour = ${Header
::colourred
};
495 } elsif ($status eq 1) {
496 $status_short = "$Lang::tr{'not validating'}";
497 $status_message = $Lang::tr
{'dnssec aware'};
498 $status_colour = ${Header
::colourblack
};
501 } elsif ($status eq 2) {
502 $status_short = "$Lang::tr{'ok'}";
503 $status_message = $Lang::tr
{'dnssec validating'};
504 $status_colour = ${Header
::colourgreen
};
508 $status_short = "$Lang::tr{'error'}";
509 $status_message = $status;
510 $status_colour = ${Header
::colourred
};
513 # collect more information about name server (rDNS, GeoIP country code)
514 my $ccode = &GeoIP
::lookup
($nameserver);
515 my $flag_icon = &GeoIP
::get_flag_icon
($ccode);
519 # Only do the reverse lookup if the system is online.
520 if (&red_is_active
()) {
521 my $iaddr = inet_aton
($nameserver);
522 $rdns = gethostbyaddr($iaddr, AF_INET
);
525 if (!$rdns) { $rdns = $Lang::tr
{'lookup failed'}; }
527 # Mark ISP name servers as disabled
528 if ($id <= 2 && $enabled eq "disabled") {
529 $nameserver = "<del>$nameserver</del>";
534 <td align="center" $col>
538 <td align="center" $col>
539 <a href='country.cgi#$ccode'><img src="$flag_icon" border="0" alt="$ccode" title="$ccode" /></a>
542 <td align="center" $col>
546 <td align="center" $col>
550 <td align="center" $col>
551 <strong><font color="$status_colour"><abbr title="$status_message">$status_short</abbr></font></strong>
555 # Check if the id is greater than "2".
557 # Nameservers with an ID's of one or two are ISP assigned,
558 # and we cannot perform any actions on them, so hide the tools for
563 <td align='center' width='5%' $col>
564 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
565 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' title='$gdesc' alt='$gdesc' />
566 <input type='hidden' name='ID' value='$id' />
567 <input type='hidden' name='ENABLE' value='$toggle' />
568 <input type='hidden' name='SERVERS' value='$Lang::tr{'toggle enable disable'}' />
572 <td align='center' width='5%' $col>
573 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
574 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' title='$Lang::tr{'edit'}' alt='$Lang::tr{'edit'}' />
575 <input type='hidden' name='ID' value='$id' />
576 <input type='hidden' name='SERVERS' value='$Lang::tr{'edit'}' />
580 <td align='center' width='5%' $col>
581 <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>
582 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}' />
583 <input type='hidden' name='ID' value='$id' />
584 <input type='hidden' name='SERVERS' value='$Lang::tr{'remove'}' />
590 print "<td colspan='3' $col> </td>\n";
600 print"<table width='100%'>\n";
602 # Check if the usage of the ISP nameservers is enabled and there are more than 2 servers.
603 if (($settings{'USE_ISP_NAMESERVERS'} eq "on") && ($server_amount gt "2")) {
606 <td class='boldbase'> <b>$Lang::tr{'legend'}:</b></td>
607 <td> <img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
608 <td class='base'>$Lang::tr{'click to disable'}</td>
609 <td> <img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
610 <td class='base'>$Lang::tr{'click to enable'}</td>
611 <td> <img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
612 <td class='base'>$Lang::tr{'edit'}</td>
613 <td> <img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
614 <td class='base'>$Lang::tr{'remove'}</td>
621 <form method="post" action="$ENV{'SCRIPT_NAME'}">
622 <td colspan="9" align="right"><input type="submit" name="SERVERS" value="$Lang::tr{'add'}"></td>
633 <td colspan="6" align="center">
634 <br>$Lang::tr{'guardian no entries'}<br>
639 <form method="post" action="$ENV{'SCRIPT_NAME'}">
640 <td colspan="6" align="right"><input type="submit" name="SERVERS" value="$Lang::tr{'add'}"></td>
653 # Section to display the add or edit subpage.
655 sub show_add_edit_nameserver
() {
656 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
658 my $buttontext = $Lang::tr
{'save'};
661 if ($cgiparams{'SERVERS'} eq $Lang::tr
{'edit'}) {
662 &Header
::openbox
('100%', 'left', $Lang::tr
{'dnsforward edit an entry'});
664 # Update button text for upate the existing entry.
665 $buttontext = $Lang::tr
{'update'};
667 # Add hidden input for sending ID.
668 print"<input type='hidden' name='ID' value='$cgiparams{'ID'}'>\n";
670 # Check if an ID has been given.
671 if ($cgiparams{'ID'}) {
672 # Assign cgiparams values.
673 $cgiparams{'NAMESERVER'} = $dns_servers{$cgiparams{'ID'}}[0];
674 $cgiparams{'TLS_HOSTNAME'} = $dns_servers{$cgiparams{'ID'}}[1];
675 $cgiparams{'REMARK'} = $dns_servers{$cgiparams{'ID'}}[3];
678 &Header
::openbox
('100%', 'left', $Lang::tr
{'dnsforward add a new entry'});
681 my $tls_required_image;
683 # If the protocol is TLS, dispaly the required image.
684 if ($settings{'PROTO'} eq "TLS") {
685 $tls_required_image = "<img src='/blob.gif' alt='*'>";
688 # Add hidden input to store the mode.
689 print "<input type='hidden' name='MODE' value='$cgiparams{'SERVERS'}'>\n";
694 <td width='20%' class='base'>$Lang::tr{'ip address'}: <img src='/blob.gif' alt='*' /></td>
695 <td><input type='text' name='NAMESERVER' value='$cgiparams{"NAMESERVER"}' size='24' /></td>
700 <td width='20%' class='base'>$Lang::tr{'dns tls hostname'}: $tls_required_image</td>
701 <td><input type='text' name='TLS_HOSTNAME' value='$cgiparams{'TLS_HOSTNAME'}' size='24'></td>
706 <td width ='20%' class='base'>$Lang::tr{'remark'}:</td>
707 <td><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' maxlength='50' /></td>
716 <td class='base' width='55%'><img src='/blob.gif' alt ='*' align='top' /> $Lang::tr{'required field'}</td>
717 <td width='40%' align='right'>
718 <input type="submit" name="SERVERS" value="$buttontext">
719 <input type="submit" name="SERVERS" value="$Lang::tr{'back'}">
732 # Check if the system is online (RED is connected).
733 sub red_is_active
() {
734 # Check if the "active" file is present.
735 if ( -f
"${General::swroot}/red/active") {
739 # Return nothing - False.
744 # Tiny function to grab an IP-address of a given file.
745 sub grab_address_from_file
($) {
750 # Check if the given file exists.
752 # Open the file for reading.
753 open(FILE
, $file) or die "Could not read from $file. $!\n";
755 # Read the address from the file.
764 # Check if the obtained address is valid.
765 if (&General
::validip
($address)) {
766 # Return the address.
775 # Function to check a given nameserver against propper work.
776 sub check_nameserver
($$$$) {
777 my ($nameserver, $record, $proto, $tls_hostname) = @_;
779 # Check if the system is online.
780 unless (&red_is_active
()) {
781 return "$Lang::tr{'system is offline'}";
785 my @command = ("kdig", "+timeout=2", "+retry=0", "+dnssec",
788 # Handle different protols.
789 if ($proto eq "TCP") {
790 # Add TCP switch to the command.
791 push(@command, "+tcp");
793 } elsif($proto eq "TLS") {
794 # Add TLS switch to the command and provide the
795 # path to our file which contains the ca certs.
796 push(@command, "+tls-ca=$ca_certs_file");
798 # Check if a TLS hostname has been provided.
800 # Add TLS hostname to the command.
801 push(@command, "+tls-hostname=$tls_hostname");
803 return "$Lang::tr{'dns no tls hostname given'}";
807 # Add record to the command array.
808 push(@command, "$record");
810 # Add nameserver to the command array.
811 push(@command, "\@$nameserver");
813 # Connect to STDOUT and STDERR.
814 push(@command, "2>&1");
816 my @output = qx(@command);
817 my $output = join("", @output);
821 if ($output =~ m/status: (\w+)/) {
822 $status = ($1 eq "NOERROR");
830 while ($output =~ m/WARNING: (.*)/g) {
831 # Add the current grabbed warning to the warning string.
835 # Return the warning string, if we grabbed at least one.
842 if ($output =~ m/Flags: (.*);/) {
843 @flags = split(/ /, $1);
846 my $aware = ($output =~ m/RRSIG/);
847 my $validating = (grep(/ad;/, @flags));
849 return $aware + $validating;