]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/dnsforward.cgi
Merge remote-tracking branch 'ms/dns-forwarding' into next
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / dnsforward.cgi
CommitLineData
e4ba53ed
SS
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2013 IPFire Development Team #
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;
23
24# enable only the following on debugging purpose
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31
32#workaround to suppress a warning when a variable is used only once
33my @dummy = ( ${Header::colouryellow} );
34undef (@dummy);
35
36my %cgiparams=();
37my %checked=();
38my %selected=();
39my $errormessage = '';
40my $filename = "${General::swroot}/dnsforward/config";
41my $changed = 'no';
42
43my %color = ();
44my %mainsettings = ();
45&General::readhash("${General::swroot}/main/settings", \%mainsettings);
46&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
47
48&Header::showhttpheaders();
49
50$cgiparams{'ENABLED'} = 'off';
51$cgiparams{'ACTION'} = '';
52$cgiparams{'ZONE'} = '';
1a26564e 53$cgiparams{'FORWARD_SERVERS'} = '';
e4ba53ed 54$cgiparams{'REMARK'} ='';
025d8e63 55$cgiparams{'DISABLE_DNSSEC'} = 'off';
e4ba53ed
SS
56&Header::getcgihash(\%cgiparams);
57open(FILE, $filename) or die 'Unable to open config file.';
58my @current = <FILE>;
59close(FILE);
60
61###
62# Add / Edit entries.
63#
64if ($cgiparams{'ACTION'} eq $Lang::tr{'add'})
65{
66 # Check if the entered domainname is valid.
67 unless (&General::validdomainname($cgiparams{'ZONE'})) {
68 $errormessage = $Lang::tr{'invalid domain name'};
69 }
70
1a26564e
MT
71 my @forward_servers = split(/\,/, $cgiparams{'FORWARD_SERVERS'});
72 foreach my $forward_server (@forward_servers) {
73 # Check if the settings for the forward server are valid.
cb8a25e5
MT
74 unless(&General::validip($forward_server) || &General::validfqdn($forward_server)) {
75 $errormessage = "$Lang::tr{'invalid ip or hostname'}: $forward_server";
1a26564e
MT
76 last;
77 }
e4ba53ed
SS
78 }
79
025d8e63
MT
80 if ($cgiparams{'DISABLE_DNSSEC'} !~ /^(on|off)?$/) {
81 $errormessage = $Lang::tr{'invalid input'};
82 }
83
e4ba53ed
SS
84 # Go further if there was no error.
85 if ( ! $errormessage)
86 {
1a26564e
MT
87 # Save servers separated by |
88 $cgiparams{'FORWARD_SERVERS'} = join("|", @forward_servers);
89
e4ba53ed
SS
90 # Check if a remark has been entered.
91 $cgiparams{'REMARK'} = &Header::cleanhtml($cgiparams{'REMARK'});
92
025d8e63
MT
93 # Set to off if not enabled
94 if (!$cgiparams{'DISABLE_DNSSEC'}) {
95 $cgiparams{'DISABLE_DNSSEC'} = "off";
96 }
97
e4ba53ed
SS
98 # Check if we want to edit an existing or add a new entry.
99 if($cgiparams{'EDITING'} eq 'no') {
100 open(FILE,">>$filename") or die 'Unable to open config file.';
101 flock FILE, 2;
025d8e63 102 print FILE "$cgiparams{'ENABLED'},$cgiparams{'ZONE'},$cgiparams{'FORWARD_SERVERS'},$cgiparams{'REMARK'},$cgiparams{'DISABLE_DNSSEC'}\n";
e4ba53ed
SS
103 } else {
104 open(FILE, ">$filename") or die 'Unable to open config file.';
105 flock FILE, 2;
106 my $id = 0;
107 foreach my $line (@current)
108 {
109 $id++;
110 if ($cgiparams{'EDITING'} eq $id) {
025d8e63 111 print FILE "$cgiparams{'ENABLED'},$cgiparams{'ZONE'},$cgiparams{'FORWARD_SERVERS'},$cgiparams{'REMARK'},$cgiparams{'DISABLE_DNSSEC'}\n";
e4ba53ed
SS
112 } else { print FILE "$line"; }
113 }
114 }
115 close(FILE);
116 undef %cgiparams;
117 $changed = 'yes';
118 } else {
119 # stay on edit mode if an error occur
120 if ($cgiparams{'EDITING'} ne 'no')
121 {
122 $cgiparams{'ACTION'} = $Lang::tr{'edit'};
123 $cgiparams{'ID'} = $cgiparams{'EDITING'};
124 }
125 }
df7340d2
MT
126 # Restart unbound
127 system('/usr/local/bin/unboundctrl restart >/dev/null');
e4ba53ed
SS
128}
129
130###
131# Remove existing entries.
132#
133if ($cgiparams{'ACTION'} eq $Lang::tr{'remove'})
134{
135 my $id = 0;
136 open(FILE, ">$filename") or die 'Unable to open config file.';
137 flock FILE, 2;
138 foreach my $line (@current)
139 {
140 $id++;
141 unless ($cgiparams{'ID'} eq $id) { print FILE "$line"; }
142 }
143 close(FILE);
df7340d2
MT
144 # Restart unbound.
145 system('/usr/local/bin/unboundctrl restart >/dev/null');
e4ba53ed
SS
146}
147
148###
149# Toggle Enable/Disable for entries.
150#
151if ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'})
152{
153 open(FILE, ">$filename") or die 'Unable to open config file.';
154 flock FILE, 2;
155 my $id = 0;
156 foreach my $line (@current)
157 {
158 $id++;
159 unless ($cgiparams{'ID'} eq $id) { print FILE "$line"; }
160 else
161 {
162 chomp($line);
163 my @temp = split(/\,/,$line);
025d8e63
MT
164
165 $temp[0] = $cgiparams{'ENABLE'};
166
167 print FILE join(",", @temp) . "\n";
e4ba53ed
SS
168 }
169 }
170 close(FILE);
df7340d2
MT
171 # Restart unbound.
172 system('/usr/local/bin/unboundctrl restart >/dev/null');
e4ba53ed
SS
173}
174
175###
176# Read items for edit mode.
177#
178if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'})
179{
180 my $id = 0;
181 foreach my $line (@current)
182 {
183 $id++;
184 if ($cgiparams{'ID'} eq $id)
185 {
186 chomp($line);
187 my @temp = split(/\,/,$line);
188 $cgiparams{'ENABLED'} = $temp[0];
189 $cgiparams{'ZONE'} = $temp[1];
1a26564e 190 $cgiparams{'FORWARD_SERVERS'} = join(",", split(/\|/, $temp[2]));
e4ba53ed 191 $cgiparams{'REMARK'} = $temp[3];
025d8e63 192 $cgiparams{'DISABLE_DNSSEC'} = $temp[4];
e4ba53ed
SS
193 }
194 }
195}
196
197$checked{'ENABLED'}{'off'} = '';
198$checked{'ENABLED'}{'on'} = '';
199$checked{'ENABLED'}{$cgiparams{'ENABLED'}} = "checked='checked'";
200
025d8e63
MT
201$checked{'DISABLE_DNSSEC'}{'off'} = '';
202$checked{'DISABLE_DNSSEC'}{'on'} = '';
203$checked{'DISABLE_DNSSEC'}{$cgiparams{'DISABLE_DNSSEC'}} = "checked='checked'";
204
e4ba53ed
SS
205&Header::openpage($Lang::tr{'dnsforward configuration'}, 1, '');
206
207&Header::openbigbox('100%', 'left', '', $errormessage);
208
209###
210# Error messages layout.
211#
212if ($errormessage) {
213 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
214 print "<class name='base'>$errormessage\n";
215 print "&nbsp;</class>\n";
216 &Header::closebox();
217}
218
219print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
220
221my $buttontext = $Lang::tr{'add'};
222if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) {
223 &Header::openbox('100%', 'left', $Lang::tr{'dnsforward edit an entry'});
224 $buttontext = $Lang::tr{'update'};
225} else {
226 &Header::openbox('100%', 'left', $Lang::tr{'dnsforward add a new entry'});
227}
228
229###
230# Content of the main page.
231#
232print <<END
233<table width='100%'>
234 <tr>
e3edceeb 235 <td width='20%' class='base'>$Lang::tr{'dnsforward zone'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
e4ba53ed
SS
236 <td><input type='text' name='ZONE' value='$cgiparams{'ZONE'}' size='24' /></td>
237 <td width='30%' class='base'>$Lang::tr{'enabled'}<input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
238 </tr>
239
240 <tr>
0a12cd70 241 <td width='20%' class='base'>$Lang::tr{'dnsforward forward_servers'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
1a26564e 242 <td><input type='text' name='FORWARD_SERVERS' value='$cgiparams{'FORWARD_SERVERS'}' size='24' /></td>
e4ba53ed
SS
243 </tr>
244</table>
245
246<table width='100%'>
247 <tr>
e3edceeb 248 <td width ='20%' class='base'>$Lang::tr{'remark'}:</td>
e4ba53ed
SS
249 <td><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' maxlength='50' /></td>
250 </tr>
025d8e63
MT
251 <tr>
252 <td width ='20%' class='base'>$Lang::tr{'dns forward disable dnssec'}:</td>
253 <td><input type='checkbox' name='DISABLE_DNSSEC' $checked{'DISABLE_DNSSEC'}' /></td>
254 </tr>
e4ba53ed 255</table>
ff92ff17 256<br>
e4ba53ed
SS
257<hr>
258
259<table width='100%'>
260 <tr>
e3edceeb 261 <td class='base' width='55%'><img src='/blob.gif' alt ='*' align='top' />&nbsp;$Lang::tr{'required field'}</td>
ff92ff17 262 <td width='40%' align='right'>
e4ba53ed
SS
263 <input type='hidden' name='ACTION' value='$Lang::tr{'add'}' />
264 <input type='submit' name='SUBMIT' value='$buttontext' />
265 </td>
266 </tr>
267</table>
268END
269;
270if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) {
271 print "<input type='hidden' name='EDITING' value='$cgiparams{'ID'}' />\n";
272} else {
273 print "<input type='hidden' name='EDITING' value='no' />\n";
274}
275
276&Header::closebox();
277print "</form>\n";
278
279###
280# Existing rules.
281#
282&Header::openbox('100%', 'left', $Lang::tr{'dnsforward entries'});
283print <<END
0317493a 284<table width='100%' class='tbl'>
e4ba53ed 285 <tr>
0317493a 286 <th width='35%' class='boldbase' align='center'><b>$Lang::tr{'dnsforward zone'}</b></th>
1a26564e 287 <th width='30%' class='boldbase' align='center'><b>$Lang::tr{'dnsforward forward_servers'}</b></th>
0317493a
AM
288 <th width='30%' class='boldbase' align='center'><b>$Lang::tr{'remark'}</b></th>
289 <th width='5%' class='boldbase' colspan='3' align='center'><b>$Lang::tr{'action'}</b></th>
e4ba53ed
SS
290 </tr>
291END
292;
293
294# If something has happened re-read config
295if($cgiparams{'ACTION'} ne '' or $changed ne 'no')
296{
297 open(FILE, $filename) or die 'Unable to open config file.';
298 @current = <FILE>;
299 close(FILE);
300}
301
302###
303# Re-read entries and highlight selected item for editing.
304#
305my $id = 0;
0317493a 306my $col="";
e4ba53ed
SS
307foreach my $line (@current)
308{
309 $id++;
310 chomp($line);
311 my @temp = split(/\,/,$line);
312 my $toggle = '';
313 my $gif = '';
314 my $gdesc = '';
315 my $toggle = '';
025d8e63 316 my $notice = "";
1a26564e
MT
317
318 # Format lists of servers
319 my $servers = join(", ", split(/\|/, $temp[2]));
320
025d8e63
MT
321 my $disable_dnssec = $temp[4];
322
e4ba53ed 323 if($cgiparams{'ACTION'} eq $Lang::tr{'edit'} && $cgiparams{'ID'} eq $id) {
0317493a
AM
324 print "<tr>";
325 $col="bgcolor='${Header::colouryellow}'"; }
025d8e63
MT
326 elsif ($disable_dnssec eq 'on') {
327 print "<tr>";
328 $col="bgcolor='${Header::colourred}' style='color: white'"; }
e4ba53ed 329 elsif ($id % 2) {
0317493a
AM
330 print "<tr>";
331 $col="bgcolor='$color{'color22'}'"; }
e4ba53ed 332 else {
0317493a
AM
333 print "<tr>";
334 $col="bgcolor='$color{'color20'}'"; }
e4ba53ed
SS
335
336 if ($temp[0] eq 'on') { $gif='on.gif'; $toggle='off'; $gdesc=$Lang::tr{'click to disable'};}
337 else { $gif='off.gif'; $toggle='on'; $gdesc=$Lang::tr{'click to enable'}; }
338
025d8e63
MT
339 if ($disable_dnssec eq "on") {
340 $notice = $Lang::tr{'dns forwarding dnssec disabled notice'};
341 }
342
e4ba53ed
SS
343###
344# Display edit page.
345#
346print <<END
025d8e63 347 <td align='center' $col>$temp[1] $notice</td>
1a26564e 348 <td align='center' $col>$servers</td>
0317493a
AM
349 <td align='center' $col>$temp[3]</td>
350 <td align='center' $col>
e4ba53ed
SS
351 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
352 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' title='$gdesc' alt='$gdesc' />
353 <input type='hidden' name='ID' value='$id' />
354 <input type='hidden' name='ENABLE' value='$toggle' />
355 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
356 </form>
357 </td>
0317493a 358 <td align='center' $col>
e4ba53ed
SS
359 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
360 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' title='$Lang::tr{'edit'}' alt='$Lang::tr{'edit'}' />
361 <input type='hidden' name='ID' value='$id' />
362 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
363 </form>
364 </td>
0317493a 365 <td align='center' $col>
e4ba53ed
SS
366 <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>
367 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}' />
368 <input type='hidden' name='ID' value='$id' />
369 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
370 </form>
371 </td>
372</tr>
373END
374 ;
375}
376print "</table>\n";
377
378###
379# Print the legend at the bottom if there are any configured entries.
380#
381# Check if the file size is zero - no existing entries.
382if ( ! -z "$filename") {
383print <<END
384<table>
385 <tr>
386 <td class='boldbase'>&nbsp; <b>$Lang::tr{'legend'}:</b></td>
387 <td>&nbsp; <img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
388 <td class='base'>$Lang::tr{'click to disable'}</td>
389 <td>&nbsp; &nbsp; <img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
390 <td class='base'>$Lang::tr{'click to enable'}</td>
391 <td>&nbsp; &nbsp; <img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
392 <td class='base'>$Lang::tr{'edit'}</td>
393 <td>&nbsp; &nbsp; <img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
394 <td class='base'>$Lang::tr{'remove'}</td>
395 </tr>
396</table>
397END
398;
399}
400
401&Header::closebox();
402
403&Header::closebigbox();
404
405&Header::closepage();