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);
92 # Call function to handle unbound restart, etc.
93 &_handle_unbound_and_more
()
99 if (($cgiparams{'SERVERS'} eq $Lang::tr
{'save'}) || ($cgiparams{'SERVERS'} eq $Lang::tr
{'update'})) {
100 # Hash to store the generic DNS settings.
103 # Read-in generic settings.
104 &General
::readhash
("$settings_file", \
%settings);
106 # Check if an IP-address has been given.
107 if ($cgiparams{"NAMESERVER"} eq "") {
108 $errormessage = "$Lang::tr{'dns no address given'}";
111 # Check if the given DNS server is valid.
112 elsif(!&General
::validip
($cgiparams{"NAMESERVER"})) {
113 $errormessage = "$Lang::tr{'invalid ip'}: $cgiparams{'NAMESERVER'}";
116 # Check if a TLS is enabled and no TLS_HOSTNAME has benn specified.
117 elsif($settings{'PROTO'} eq "TLS") {
118 unless($cgiparams{"TLS_HOSTNAME"}) {
119 $errormessage = "$Lang::tr{'dns no tls hostname given'}";
121 # Check if the provided domain is valid.
122 unless(&General
::validfqdn
($cgiparams{"TLS_HOSTNAME"})) {
123 $errormessage = "$Lang::tr{'invalid ip or hostname'}: $cgiparams{'TLS_HOSTNAME'}";
128 # Go further if there was no error.
129 if ( ! $errormessage) {
130 # Check if a remark has been entered.
131 $cgiparams{'REMARK'} = &Header
::cleanhtml
($cgiparams{'REMARK'});
133 my %dns_servers = ();
137 # Read-in configfile.
138 &General
::readhasharray
($servers_file, \
%dns_servers);
140 # Check if we should edit an existing entry and got an ID.
141 if (($cgiparams{'SERVERS'} eq $Lang::tr
{'update'}) && ($cgiparams{'ID'})) {
142 # Assin the provided id.
143 $id = $cgiparams{'ID'};
145 # Undef the given ID.
146 undef($cgiparams{'ID'});
148 # Grab the configured status of the corresponding entry.
149 $status = $dns_servers{$id}[2];
151 # Each newly added entry automatically should be enabled.
154 # Generate the ID for the new entry.
156 # Sort the keys by their ID and store them in an array.
157 my @keys = sort { $a <=> $b } keys %dns_servers;
159 # Reverse the key array.
160 my @reversed = reverse(@keys);
162 # Obtain the last used id.
163 my $last_id = @reversed[0];
165 # Increase the last id by one and use it as id for the new entry.
168 # The first allowed id is 3 to keep space for
169 # possible ISP assigned DNS servers.
175 # Add/Modify the entry to/in the dns_servers hash.
176 $dns_servers{$id} = ["$cgiparams{'NAMESERVER'}", "$cgiparams{'TLS_HOSTNAME'}", "$status", "$cgiparams{'REMARK'}"];
178 # Write the changed hash to the config file.
179 &General
::writehasharray
($servers_file, \
%dns_servers);
181 # Call function to handle unbound restart, etc.
182 &_handle_unbound_and_more
();
184 # Switch back to previous mode.
185 $cgiparams{'SERVERS'} = $cgiparams{'MODE'};
188 # Toggle enable / disable.
190 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr
{'toggle enable disable'}) {
191 my %dns_servers = ();
193 # Only go further, if an ID has been passed.
194 if ($cgiparams{'ID'}) {
195 # Assign the given ID.
196 my $id = $cgiparams{'ID'};
198 # Undef the given ID.
199 undef($cgiparams{'ID'});
201 # Read-in configfile.
202 &General
::readhasharray
($servers_file, \
%dns_servers);
204 # Grab the configured status of the corresponding entry.
205 my $status = $dns_servers{$id}[2];
208 if ($status eq "disabled") {
211 $status = "disabled";
214 # Modify the status of the existing entry.
215 $dns_servers{$id} = ["$dns_servers{$id}[0]", "$dns_servers{$id}[1]", "$status", "$dns_servers{$id}[3]"];
217 # Write the changed hash back to the config file.
218 &General
::writehasharray
($servers_file, \
%dns_servers);
220 # Call function to handle unbound restart, etc.
221 &_handle_unbound_and_more
();
224 ## Remove entry from DNS servers list.
226 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr
{'remove'}) {
227 my %dns_servers = ();
229 # Read-in configfile.
230 &General
::readhasharray
($servers_file, \
%dns_servers);
232 # Drop entry from the hash.
233 delete($dns_servers{$cgiparams{'ID'}});
235 # Undef the given ID.
236 undef($cgiparams{'ID'});
238 # Write the changed hash to the config file.
239 &General
::writehasharray
($servers_file, \
%dns_servers);
241 # Call function to handle unbound restart, etc.
242 &_handle_unbound_and_more
();
244 ## Handle request to check the servers.
246 } elsif ($cgiparams{'SERVERS'} eq $Lang::tr
{'dns check servers'}) {
250 # Hash to store the generic DNS settings.
253 # Read-in general DNS settings.
254 &General
::readhash
("$settings_file", \
%settings);
256 # Hash which contains the configured DNS servers.
257 my %dns_servers = ();
259 # Read-in config file.
260 &General
::readhasharray
("$servers_file", \
%dns_servers);
262 &Header
::openpage
($Lang::tr
{'dns'}, 1, '');
264 &Header
::openbigbox
('100%', 'left', '', $errormessage);
267 # Error messages layout.
270 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
271 print "<class name='base'>$errormessage\n";
272 print " </class>\n";
276 # Handle if a nameserver should be added or edited.
277 if (($cgiparams{'SERVERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'SERVERS'} eq "$Lang::tr{'edit'}")) {
278 # Display the sub page.
279 &show_add_edit_nameserver
();
282 &Header
::closebigbox
();
283 &Header
::closepage
();
285 # Finished here for the moment.
289 $cgiparams{'GENERAL'} = '';
290 $cgiparams{'SERVERS'} = '';
291 $cgiparams{'NAMESERVER'} = '';
292 $cgiparams{'TLS_HOSTNAME'} = '';
293 $cgiparams{'REMARK'} ='';
295 $checked{'USE_ISP_NAMESERVERS'}{'off'} = '';
296 $checked{'USE_ISP_NAMESERVERS'}{'on'} = '';
297 $checked{'USE_ISP_NAMESERVERS'}{$settings{'USE_ISP_NAMESERVERS'}} = "checked='checked'";
299 $checked{'ENABLE_SAFE_SEARCH'}{'off'} = '';
300 $checked{'ENABLE_SAFE_SEARCH'}{'on'} = '';
301 $checked{'ENABLE_SAFE_SEARCH'}{$settings{'ENABLE_SAFE_SEARCH'}} = "checked='checked'";
303 $selected{'PROTO'}{'UDP'} = '';
304 $selected{'PROTO'}{'TLS'} = '';
305 $selected{'PROTO'}{'TCP'} = '';
306 $selected{'PROTO'}{$settings{'PROTO'}} = "selected='selected'";
308 $selected{'QNAME_MIN'}{'standard'} = '';
309 $selected{'QNAME_MIN'}{'strict'} = '';
310 $selected{'QNAME_MIN'}{$settings{'QNAME_MIN'}} = "selected='selected'";
312 # Display nameserver and configuration sections.
314 &show_general_dns_configuration
();
316 &Header
::closebigbox
();
317 &Header
::closepage
();
320 # General DNS-Servers sektion.
322 sub show_general_dns_configuration
() {
323 &Header
::openbox
('100%', 'center', "$Lang::tr{'dns configuration'}");
326 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
330 $Lang::tr{'dns use isp assigned nameservers'}
334 <input type="checkbox" name="USE_ISP_NAMESERVERS" $checked{'USE_ISP_NAMESERVERS'}{'on'}>
346 $Lang::tr{'dns use protocol for dns queries'}
350 <select name="PROTO">
351 <option value="UDP" $selected{'PROTO'}{'UDP'}>UDP</option>
352 <option value="TLS" $selected{'PROTO'}{'TLS'}>TLS</option>
353 <option value="TCP" $selected{'PROTO'}{'TCP'}>TCP</option>
366 $Lang::tr{'dns enable safe-search'}
370 <input type="checkbox" name="ENABLE_SAFE_SEARCH" $checked{'ENABLE_SAFE_SEARCH'}{'on'}>
382 $Lang::tr{'dns mode for qname minimisation'}
386 <select name="QNAME_MIN">
387 <option value="standard" $selected{'QNAME_MIN'}{'standard'}>$Lang::tr{'standard'}</option>
388 <option value="strict" $selected{'QNAME_MIN'}{'strict'}>$Lang::tr{'strict'}</option>
394 <td colspan="2" align="right">
395 <input type="submit" name="GENERAL" value="$Lang::tr{'save'}">
406 # Section to display the configured and used DNS servers.
408 sub show_nameservers
() {
409 &Header
::openbox
('100%', 'center', "$Lang::tr{'dns title'}");
411 my $dns_status_string;
414 # Test if the DNS system is working.
416 # Simple send a request to unbound and check if it can resolve the
418 my $dns_status_ret = &check_nameserver
("127.0.0.1", "$dns_test_server", "UDP");
420 if ($dns_status_ret eq "2") {
421 $dns_status_string = "$Lang::tr{'working'}";
422 $dns_status_col = "${Header::colourgreen}";
424 $dns_status_string = "$Lang::tr{'broken'}";
425 $dns_status_col = "${Header::colourred}";
432 <strong>$Lang::tr{'status'}: </strong>
433 <strong><font color='$dns_status_col'>$dns_status_string</font></strong>
440 <table class="tbl" width='100%'>
443 <strong>$Lang::tr{'nameserver'}</strong>
447 <strong>$Lang::tr{'country'}</strong>
451 <strong>$Lang::tr{'rdns'}</strong>
455 <strong>$Lang::tr{'remark'}</strong>
458 # Check if the status should be displayed.
459 if ($check_servers) {
462 <strong>$Lang::tr{'status'}</strong>
470 <td align="center" colspan="3">
471 <strong>$Lang::tr{'action'}</strong>
477 # Check the usage of ISP assigned nameservers is enabled.
480 # Loop through the array which stores the files.
481 foreach my $file (@ISP_nameserver_files) {
482 # Grab the address of the nameserver.
483 my $address = &General
::grab_address_from_file
($file);
485 # Check if we got an address.
487 # Add the address to the hash of nameservers.
488 $dns_servers{$id} = [ "$address", "none",
489 ($settings{'USE_ISP_NAMESERVERS'} eq "on") ?
"enabled" : "disabled",
490 "$Lang::tr{'dns isp assigned nameserver'}" ];
492 # Increase id by one.
497 # Check some DNS servers have been configured. In this case
498 # the hash contains at least one key.
500 if (keys %dns_servers) {
501 # Sort the keys by their ID and store them in an array.
502 my @keys = sort { $a <=> $b } keys %dns_servers;
504 # Loop through all entries of the array/hash.
505 foreach my $id (@keys) {
506 # Inrease server_amount.
509 # Assign data array positions to some nice variable names.
510 my $nameserver = $dns_servers{$id}[0];
511 my $tls_hostname = $dns_servers{$id}[1];
512 my $enabled = $dns_servers{$id}[2];
513 my $remark = $dns_servers{$id}[3];
522 if ($server_amount % 2) {
523 $col="bgcolor='$color{'color22'}'"; }
525 $col="bgcolor='$color{'color20'}'";
528 if ($enabled eq 'enabled') {
529 $gif='on.gif'; $toggle='off'; $gdesc=$Lang::tr
{'click to disable'};
531 $gif='off.gif'; $toggle='on'; $gdesc=$Lang::tr
{'click to enable'};
539 # Only grab the status if the nameserver is enabled.
540 if (($check_servers) && ($enabled eq "enabled")) {
541 $status = &check_nameserver
("$nameserver", "ping.ipfire.org", "$settings{'PROTO'}", "$tls_hostname");
545 $status_short = "$Lang::tr{'disabled'}";
547 # DNSSEC Not supported
548 } elsif ($status eq 0) {
549 $status_short = "$Lang::tr{'broken'}";
550 $status_message = $Lang::tr
{'dnssec not supported'};
551 $status_colour = ${Header
::colourred
};
554 } elsif ($status eq 1) {
555 $status_short = "$Lang::tr{'not validating'}";
556 $status_message = $Lang::tr
{'dnssec aware'};
557 $status_colour = ${Header
::colourblack
};
560 } elsif ($status eq 2) {
561 $status_short = "$Lang::tr{'ok'}";
562 $status_message = $Lang::tr
{'dnssec validating'};
563 $status_colour = ${Header
::colourgreen
};
567 $status_short = "$Lang::tr{'error'}";
568 $status_message = $status;
569 $status_colour = ${Header
::colourred
};
572 # collect more information about name server (rDNS, GeoIP country code)
573 my $ccode = &GeoIP
::lookup
($nameserver);
574 my $flag_icon = &GeoIP
::get_flag_icon
($ccode);
578 # Only do the reverse lookup if the system is online.
579 if (&red_is_active
()) {
580 my $iaddr = inet_aton
($nameserver);
581 $rdns = gethostbyaddr($iaddr, AF_INET
);
584 if (!$rdns) { $rdns = $Lang::tr
{'lookup failed'}; }
586 # Mark ISP name servers as disabled
587 if ($id <= 2 && $enabled eq "disabled") {
588 $nameserver = "<del>$nameserver</del>";
593 <td align="center" $col>
597 <td align="center" $col>
598 <a href='country.cgi#$ccode'><img src="$flag_icon" border="0" alt="$ccode" title="$ccode" /></a>
601 <td align="center" $col>
605 <td align="center" $col>
610 # Display server status if requested.
611 if ($check_servers) {
613 <td align="center" $col>
614 <strong><font color="$status_colour"><abbr title="$status_message">$status_short</abbr></font></strong>
620 # Check if the id is greater than "2".
622 # Nameservers with an ID's of one or two are ISP assigned,
623 # and we cannot perform any actions on them, so hide the tools for
628 <td align='center' width='5%' $col>
629 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
630 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' title='$gdesc' alt='$gdesc' />
631 <input type='hidden' name='ID' value='$id' />
632 <input type='hidden' name='ENABLE' value='$toggle' />
633 <input type='hidden' name='SERVERS' value='$Lang::tr{'toggle enable disable'}' />
637 <td align='center' width='5%' $col>
638 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
639 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' title='$Lang::tr{'edit'}' alt='$Lang::tr{'edit'}' />
640 <input type='hidden' name='ID' value='$id' />
641 <input type='hidden' name='SERVERS' value='$Lang::tr{'edit'}' />
645 <td align='center' width='5%' $col>
646 <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>
647 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}' />
648 <input type='hidden' name='ID' value='$id' />
649 <input type='hidden' name='SERVERS' value='$Lang::tr{'remove'}' />
655 print "<td colspan='3' $col> </td>\n";
665 print"<table width='100%'>\n";
667 # Check if the usage of the ISP nameservers is enabled and there are more than 2 servers.
668 if (($settings{'USE_ISP_NAMESERVERS'} eq "on") && ($server_amount gt "2")) {
671 <td class='boldbase'> <b>$Lang::tr{'legend'}:</b></td>
672 <td> <img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
673 <td class='base'>$Lang::tr{'click to disable'}</td>
674 <td> <img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
675 <td class='base'>$Lang::tr{'click to enable'}</td>
676 <td> <img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
677 <td class='base'>$Lang::tr{'edit'}</td>
678 <td> <img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
679 <td class='base'>$Lang::tr{'remove'}</td>
686 <form method="post" action="$ENV{'SCRIPT_NAME'}">
687 <td colspan="9" align="right">
688 <input type="submit" name="SERVERS" value="$Lang::tr{'add'}">
689 <input type="submit" name="SERVERS" value="$Lang::tr{'dns check servers'}">
701 <td colspan="6" align="center">
702 <br>$Lang::tr{'guardian no entries'}<br>
707 <form method="post" action="$ENV{'SCRIPT_NAME'}">
708 <td colspan="6" align="right"><input type="submit" name="SERVERS" value="$Lang::tr{'add'}"></td>
721 # Section to display the add or edit subpage.
723 sub show_add_edit_nameserver
() {
724 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
726 my $buttontext = $Lang::tr
{'save'};
729 if ($cgiparams{'SERVERS'} eq $Lang::tr
{'edit'}) {
730 &Header
::openbox
('100%', 'left', $Lang::tr
{'dnsforward edit an entry'});
732 # Update button text for upate the existing entry.
733 $buttontext = $Lang::tr
{'update'};
735 # Add hidden input for sending ID.
736 print"<input type='hidden' name='ID' value='$cgiparams{'ID'}'>\n";
738 # Check if an ID has been given.
739 if ($cgiparams{'ID'}) {
740 # Assign cgiparams values.
741 $cgiparams{'NAMESERVER'} = $dns_servers{$cgiparams{'ID'}}[0];
742 $cgiparams{'TLS_HOSTNAME'} = $dns_servers{$cgiparams{'ID'}}[1];
743 $cgiparams{'REMARK'} = $dns_servers{$cgiparams{'ID'}}[3];
746 &Header
::openbox
('100%', 'left', $Lang::tr
{'dnsforward add a new entry'});
749 my $tls_required_image;
751 # If the protocol is TLS, dispaly the required image.
752 if ($settings{'PROTO'} eq "TLS") {
753 $tls_required_image = "<img src='/blob.gif' alt='*'>";
756 # Add hidden input to store the mode.
757 print "<input type='hidden' name='MODE' value='$cgiparams{'SERVERS'}'>\n";
762 <td width='20%' class='base'>$Lang::tr{'ip address'}: <img src='/blob.gif' alt='*' /></td>
763 <td><input type='text' name='NAMESERVER' value='$cgiparams{"NAMESERVER"}' size='24' /></td>
768 <td width='20%' class='base'>$Lang::tr{'dns tls hostname'}: $tls_required_image</td>
769 <td><input type='text' name='TLS_HOSTNAME' value='$cgiparams{'TLS_HOSTNAME'}' size='24'></td>
774 <td width ='20%' class='base'>$Lang::tr{'remark'}:</td>
775 <td><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' maxlength='50' /></td>
784 <td class='base' width='55%'><img src='/blob.gif' alt ='*' align='top' /> $Lang::tr{'required field'}</td>
785 <td width='40%' align='right'>
786 <input type="submit" name="SERVERS" value="$buttontext">
787 <input type="submit" name="SERVERS" value="$Lang::tr{'back'}">
800 # Private function to handle the restart of unbound and more.
801 sub _handle_unbound_and_more
() {
803 system('/usr/local/bin/unboundctrl restart >/dev/null');
806 # Check if the system is online (RED is connected).
807 sub red_is_active
() {
808 # Check if the "active" file is present.
809 if ( -f
"${General::swroot}/red/active") {
813 # Return nothing - False.
818 # Function to check a given nameserver against propper work.
819 sub check_nameserver
($$$$) {
820 my ($nameserver, $record, $proto, $tls_hostname) = @_;
822 # Check if the system is online.
823 unless (&red_is_active
()) {
824 return "$Lang::tr{'system is offline'}";
828 my @command = ("kdig", "+timeout=2", "+retry=0", "+dnssec",
831 # Handle different protols.
832 if ($proto eq "TCP") {
833 # Add TCP switch to the command.
834 push(@command, "+tcp");
836 } elsif($proto eq "TLS") {
837 # Add TLS switch to the command and provide the
838 # path to our file which contains the ca certs.
839 push(@command, "+tls-ca=$ca_certs_file");
841 # Check if a TLS hostname has been provided.
843 # Add TLS hostname to the command.
844 push(@command, "+tls-hostname=$tls_hostname");
846 return "$Lang::tr{'dns no tls hostname given'}";
850 # Add record to the command array.
851 push(@command, "$record");
853 # Add nameserver to the command array.
854 push(@command, "\@$nameserver");
856 # Connect to STDOUT and STDERR.
857 push(@command, "2>&1");
859 my @output = qx(@command);
860 my $output = join("", @output);
864 if ($output =~ m/status: (\w+)/) {
865 $status = ($1 eq "NOERROR");
873 while ($output =~ m/WARNING: (.*)/g) {
874 # Add the current grabbed warning to the warning string.
878 # Return the warning string, if we grabbed at least one.
885 if ($output =~ m/Flags: (.*);/) {
886 @flags = split(/ /, $1);
889 my $aware = ($output =~ m/RRSIG/);
890 my $validating = (grep(/ad;/, @flags));
892 return $aware + $validating;