]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/dnsforward.cgi
Merge remote-tracking branch 'ms/dns-forwarding' into next
[ipfire-2.x.git] / html / cgi-bin / dnsforward.cgi
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
22 use strict;
23
24 # enable only the following on debugging purpose
25 #use warnings;
26 #use CGI::Carp 'fatalsToBrowser';
27
28 require '/var/ipfire/general-functions.pl';
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31
32 #workaround to suppress a warning when a variable is used only once
33 my @dummy = ( ${Header::colouryellow} );
34 undef (@dummy);
35
36 my %cgiparams=();
37 my %checked=();
38 my %selected=();
39 my $errormessage = '';
40 my $filename = "${General::swroot}/dnsforward/config";
41 my $changed = 'no';
42
43 my %color = ();
44 my %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'} = '';
53 $cgiparams{'FORWARD_SERVERS'} = '';
54 $cgiparams{'REMARK'} ='';
55 $cgiparams{'DISABLE_DNSSEC'} = 'off';
56 &Header::getcgihash(\%cgiparams);
57 open(FILE, $filename) or die 'Unable to open config file.';
58 my @current = <FILE>;
59 close(FILE);
60
61 ###
62 # Add / Edit entries.
63 #
64 if ($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
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.
74 unless(&General::validip($forward_server) || &General::validfqdn($forward_server)) {
75 $errormessage = "$Lang::tr{'invalid ip or hostname'}: $forward_server";
76 last;
77 }
78 }
79
80 if ($cgiparams{'DISABLE_DNSSEC'} !~ /^(on|off)?$/) {
81 $errormessage = $Lang::tr{'invalid input'};
82 }
83
84 # Go further if there was no error.
85 if ( ! $errormessage)
86 {
87 # Save servers separated by |
88 $cgiparams{'FORWARD_SERVERS'} = join("|", @forward_servers);
89
90 # Check if a remark has been entered.
91 $cgiparams{'REMARK'} = &Header::cleanhtml($cgiparams{'REMARK'});
92
93 # Set to off if not enabled
94 if (!$cgiparams{'DISABLE_DNSSEC'}) {
95 $cgiparams{'DISABLE_DNSSEC'} = "off";
96 }
97
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;
102 print FILE "$cgiparams{'ENABLED'},$cgiparams{'ZONE'},$cgiparams{'FORWARD_SERVERS'},$cgiparams{'REMARK'},$cgiparams{'DISABLE_DNSSEC'}\n";
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) {
111 print FILE "$cgiparams{'ENABLED'},$cgiparams{'ZONE'},$cgiparams{'FORWARD_SERVERS'},$cgiparams{'REMARK'},$cgiparams{'DISABLE_DNSSEC'}\n";
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 }
126 # Restart unbound
127 system('/usr/local/bin/unboundctrl restart >/dev/null');
128 }
129
130 ###
131 # Remove existing entries.
132 #
133 if ($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);
144 # Restart unbound.
145 system('/usr/local/bin/unboundctrl restart >/dev/null');
146 }
147
148 ###
149 # Toggle Enable/Disable for entries.
150 #
151 if ($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);
164
165 $temp[0] = $cgiparams{'ENABLE'};
166
167 print FILE join(",", @temp) . "\n";
168 }
169 }
170 close(FILE);
171 # Restart unbound.
172 system('/usr/local/bin/unboundctrl restart >/dev/null');
173 }
174
175 ###
176 # Read items for edit mode.
177 #
178 if ($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];
190 $cgiparams{'FORWARD_SERVERS'} = join(",", split(/\|/, $temp[2]));
191 $cgiparams{'REMARK'} = $temp[3];
192 $cgiparams{'DISABLE_DNSSEC'} = $temp[4];
193 }
194 }
195 }
196
197 $checked{'ENABLED'}{'off'} = '';
198 $checked{'ENABLED'}{'on'} = '';
199 $checked{'ENABLED'}{$cgiparams{'ENABLED'}} = "checked='checked'";
200
201 $checked{'DISABLE_DNSSEC'}{'off'} = '';
202 $checked{'DISABLE_DNSSEC'}{'on'} = '';
203 $checked{'DISABLE_DNSSEC'}{$cgiparams{'DISABLE_DNSSEC'}} = "checked='checked'";
204
205 &Header::openpage($Lang::tr{'dnsforward configuration'}, 1, '');
206
207 &Header::openbigbox('100%', 'left', '', $errormessage);
208
209 ###
210 # Error messages layout.
211 #
212 if ($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
219 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
220
221 my $buttontext = $Lang::tr{'add'};
222 if ($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 #
232 print <<END
233 <table width='100%'>
234 <tr>
235 <td width='20%' class='base'>$Lang::tr{'dnsforward zone'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
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>
241 <td width='20%' class='base'>$Lang::tr{'dnsforward forward_servers'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
242 <td><input type='text' name='FORWARD_SERVERS' value='$cgiparams{'FORWARD_SERVERS'}' size='24' /></td>
243 </tr>
244 </table>
245
246 <table width='100%'>
247 <tr>
248 <td width ='20%' class='base'>$Lang::tr{'remark'}:</td>
249 <td><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' maxlength='50' /></td>
250 </tr>
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>
255 </table>
256 <br>
257 <hr>
258
259 <table width='100%'>
260 <tr>
261 <td class='base' width='55%'><img src='/blob.gif' alt ='*' align='top' />&nbsp;$Lang::tr{'required field'}</td>
262 <td width='40%' align='right'>
263 <input type='hidden' name='ACTION' value='$Lang::tr{'add'}' />
264 <input type='submit' name='SUBMIT' value='$buttontext' />
265 </td>
266 </tr>
267 </table>
268 END
269 ;
270 if ($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();
277 print "</form>\n";
278
279 ###
280 # Existing rules.
281 #
282 &Header::openbox('100%', 'left', $Lang::tr{'dnsforward entries'});
283 print <<END
284 <table width='100%' class='tbl'>
285 <tr>
286 <th width='35%' class='boldbase' align='center'><b>$Lang::tr{'dnsforward zone'}</b></th>
287 <th width='30%' class='boldbase' align='center'><b>$Lang::tr{'dnsforward forward_servers'}</b></th>
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>
290 </tr>
291 END
292 ;
293
294 # If something has happened re-read config
295 if($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 #
305 my $id = 0;
306 my $col="";
307 foreach 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 = '';
316 my $notice = "";
317
318 # Format lists of servers
319 my $servers = join(", ", split(/\|/, $temp[2]));
320
321 my $disable_dnssec = $temp[4];
322
323 if($cgiparams{'ACTION'} eq $Lang::tr{'edit'} && $cgiparams{'ID'} eq $id) {
324 print "<tr>";
325 $col="bgcolor='${Header::colouryellow}'"; }
326 elsif ($disable_dnssec eq 'on') {
327 print "<tr>";
328 $col="bgcolor='${Header::colourred}' style='color: white'"; }
329 elsif ($id % 2) {
330 print "<tr>";
331 $col="bgcolor='$color{'color22'}'"; }
332 else {
333 print "<tr>";
334 $col="bgcolor='$color{'color20'}'"; }
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
339 if ($disable_dnssec eq "on") {
340 $notice = $Lang::tr{'dns forwarding dnssec disabled notice'};
341 }
342
343 ###
344 # Display edit page.
345 #
346 print <<END
347 <td align='center' $col>$temp[1] $notice</td>
348 <td align='center' $col>$servers</td>
349 <td align='center' $col>$temp[3]</td>
350 <td align='center' $col>
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>
358 <td align='center' $col>
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>
365 <td align='center' $col>
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>
373 END
374 ;
375 }
376 print "</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.
382 if ( ! -z "$filename") {
383 print <<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>
397 END
398 ;
399 }
400
401 &Header::closebox();
402
403 &Header::closebigbox();
404
405 &Header::closepage();