dnsmasq: Add feature to forward domains to certain DNS servers.
[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_SERVER'} = '';
54 $cgiparams{'REMARK'} ='';
55 &Header::getcgihash(\%cgiparams);
56 open(FILE, $filename) or die 'Unable to open config file.';
57 my @current = <FILE>;
58 close(FILE);
59
60 ###
61 # Add / Edit entries.
62 #
63 if ($cgiparams{'ACTION'} eq $Lang::tr{'add'})
64 {
65         # Check if the entered domainname is valid.
66         unless (&General::validdomainname($cgiparams{'ZONE'})) {
67                 $errormessage = $Lang::tr{'invalid domain name'};
68         }
69
70         # Check if the settings for the forward server are valid.
71         unless(&General::validip($cgiparams{'FORWARD_SERVER'})) {
72                 $errormessage = $Lang::tr{'invalid ip'};
73         }
74
75         # Go further if there was no error.
76         if ( ! $errormessage)
77         {
78             # Check if a remark has been entered.
79             $cgiparams{'REMARK'} = &Header::cleanhtml($cgiparams{'REMARK'});
80
81                 # Check if we want to edit an existing or add a new entry.
82                 if($cgiparams{'EDITING'} eq 'no') {
83                         open(FILE,">>$filename") or die 'Unable to open config file.';
84                         flock FILE, 2;
85                         print FILE "$cgiparams{'ENABLED'},$cgiparams{'ZONE'},$cgiparams{'FORWARD_SERVER'},$cgiparams{'REMARK'}\n";
86                 } else {
87                         open(FILE, ">$filename") or die 'Unable to open config file.';
88                         flock FILE, 2;
89                         my $id = 0;
90                         foreach my $line (@current)
91                         {
92                                 $id++;
93                                 if ($cgiparams{'EDITING'} eq $id) {
94                                         print FILE "$cgiparams{'ENABLED'},$cgiparams{'ZONE'},$cgiparams{'FORWARD_SERVER'},$cgiparams{'REMARK'}\n";
95                                 } else { print FILE "$line"; }
96                         }
97                 }
98                 close(FILE);
99                 undef %cgiparams;
100                 $changed = 'yes';
101         } else {
102                 # stay on edit mode if an error occur
103                 if ($cgiparams{'EDITING'} ne 'no')
104                 {
105                         $cgiparams{'ACTION'} = $Lang::tr{'edit'};
106                         $cgiparams{'ID'} = $cgiparams{'EDITING'};
107                 }
108         }
109         # Restart dnsmasq.
110         system('/usr/local/bin/dnsmasqctrl restart >/dev/null');
111 }
112
113 ###
114 # Remove existing entries.
115 #
116 if ($cgiparams{'ACTION'} eq $Lang::tr{'remove'})
117 {
118         my $id = 0;
119         open(FILE, ">$filename") or die 'Unable to open config file.';
120         flock FILE, 2;
121         foreach my $line (@current)
122         {
123                 $id++;
124                 unless ($cgiparams{'ID'} eq $id) { print FILE "$line"; }
125         }
126         close(FILE);
127         # Restart dnsmasq.
128         system('/usr/local/bin/dnsmasqctrl restart >/dev/null');
129 }
130
131 ###
132 # Toggle Enable/Disable for entries.
133 #
134 if ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'})
135 {
136         open(FILE, ">$filename") or die 'Unable to open config file.';
137         flock FILE, 2;
138         my $id = 0;
139         foreach my $line (@current)
140         {
141                 $id++;
142                 unless ($cgiparams{'ID'} eq $id) { print FILE "$line"; }
143                 else
144                 {
145                         chomp($line);
146                         my @temp = split(/\,/,$line);
147                         print FILE "$cgiparams{'ENABLE'},$temp[1],$temp[2],$temp[3]\n";
148                 }
149         }
150         close(FILE);
151         # Restart dnsmasq.
152         system('/usr/local/bin/dnsmasqctrl restart >/dev/null');
153 }
154
155 ###
156 # Read items for edit mode.
157 #
158 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'})
159 {
160         my $id = 0;
161         foreach my $line (@current)
162         {
163                 $id++;
164                 if ($cgiparams{'ID'} eq $id)
165                 {
166                         chomp($line);
167                         my @temp = split(/\,/,$line);
168                         $cgiparams{'ENABLED'} = $temp[0];
169                         $cgiparams{'ZONE'} = $temp[1];
170                         $cgiparams{'FORWARD_SERVER'} = $temp[2];
171                         $cgiparams{'REMARK'} = $temp[3];
172                 }
173         }
174 }
175
176 $checked{'ENABLED'}{'off'} = '';
177 $checked{'ENABLED'}{'on'} = '';
178 $checked{'ENABLED'}{$cgiparams{'ENABLED'}} = "checked='checked'";
179
180 &Header::openpage($Lang::tr{'dnsforward configuration'}, 1, '');
181
182 &Header::openbigbox('100%', 'left', '', $errormessage);
183
184 ###
185 # Error messages layout.
186 #
187 if ($errormessage) {
188         &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
189         print "<class name='base'>$errormessage\n";
190         print "&nbsp;</class>\n";
191         &Header::closebox();
192 }
193
194 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
195
196 my $buttontext = $Lang::tr{'add'};
197 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) {
198         &Header::openbox('100%', 'left', $Lang::tr{'dnsforward edit an entry'});
199         $buttontext = $Lang::tr{'update'};
200 } else {
201         &Header::openbox('100%', 'left', $Lang::tr{'dnsforward add a new entry'});
202 }
203
204 ###
205 # Content of the main page.
206 #
207 print <<END
208 <table width='100%'>
209         <tr>
210                 <td width='20%' class='base'><font>$Lang::tr{'dnsforward zone'}:</font></td>
211                 <td><input type='text' name='ZONE' value='$cgiparams{'ZONE'}' size='24' /></td>
212                 <td width='30%' class='base'>$Lang::tr{'enabled'}<input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
213         </tr>
214
215         <tr>
216                 <td width='20%' class='base'><font>$Lang::tr{'dnsforward forward_server'}:</font></td>
217                 <td><input type='text' name='FORWARD_SERVER' value='$cgiparams{'FORWARD_SERVER'}' size='24' /></td>
218         </tr>
219 </table>
220
221 <table width='100%'>
222         <tr>
223                 <td width ='20%' class='base'><font class='boldbase'>$Lang::tr{'remark'}:</font>&nbsp;<img src='/blob.gif' alt='*' /></td>
224                 <td><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' maxlength='50' /></td>
225         </tr>
226 </table>
227
228 <hr>
229
230 <table width='100%'>
231         <tr>
232                 <td class='base' width='55%'><img src='/blob.gif' alt ='*' align='top' />&nbsp;<font class='base'>$Lang::tr{'this field may be blank'}</font></td>
233                 <td width='40%' align='center'>
234                         <input type='hidden' name='ACTION' value='$Lang::tr{'add'}' />
235                         <input type='submit' name='SUBMIT' value='$buttontext' />
236                 </td>
237         </tr>
238 </table>
239 END
240 ;
241 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) {
242         print "<input type='hidden' name='EDITING' value='$cgiparams{'ID'}' />\n";
243 } else {
244         print "<input type='hidden' name='EDITING' value='no' />\n";
245 }
246
247 &Header::closebox();
248 print "</form>\n";
249
250 ###
251 # Existing rules.
252 #
253 &Header::openbox('100%', 'left', $Lang::tr{'dnsforward entries'});
254 print <<END
255 <table width='100%'>
256         <tr>
257                 <td width='35%' class='boldbase' align='center'><b>$Lang::tr{'dnsforward zone'}</b></td>
258                 <td width='30%' class='boldbase' align='center'><b>$Lang::tr{'dnsforward forward_server'}</b></td>
259                 <td width='30%' class='boldbase' align='center'><b>$Lang::tr{'remark'}</b></td>
260                 <td width='5%' class='boldbase' colspan='3' align='center'><b>$Lang::tr{'action'}</b></td>
261         </tr>
262 END
263 ;
264
265 # If something has happened re-read config
266 if($cgiparams{'ACTION'} ne '' or $changed ne 'no')
267 {
268         open(FILE, $filename) or die 'Unable to open config file.';
269         @current = <FILE>;
270         close(FILE);
271 }
272
273 ###
274 # Re-read entries and highlight selected item for editing.
275 #
276 my $id = 0;
277 foreach my $line (@current)
278 {
279         $id++;
280         chomp($line);
281         my @temp = split(/\,/,$line);
282         my $toggle = '';
283         my $gif = '';
284         my $gdesc = '';
285         my $toggle = '';
286         
287         if($cgiparams{'ACTION'} eq $Lang::tr{'edit'} && $cgiparams{'ID'} eq $id) {
288                 print "<tr bgcolor='${Header::colouryellow}'>\n"; }
289         elsif ($id % 2) {
290                 print "<tr bgcolor='$color{'color22'}'>\n"; }
291         else {
292                 print "<tr bgcolor='$color{'color20'}'>\n"; }
293
294         if ($temp[0] eq 'on') { $gif='on.gif'; $toggle='off'; $gdesc=$Lang::tr{'click to disable'};}
295         else { $gif='off.gif'; $toggle='on'; $gdesc=$Lang::tr{'click to enable'}; }
296
297 ###
298 # Display edit page.
299 #
300 print <<END
301         <td align='center'>$temp[1]</td>
302         <td align='center'>$temp[2]</td>
303         <td align='center'>$temp[3]</td>
304         <td align='center'>
305                 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
306                         <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' title='$gdesc' alt='$gdesc' />
307                         <input type='hidden' name='ID' value='$id' />
308                         <input type='hidden' name='ENABLE' value='$toggle' />
309                         <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
310                 </form>
311         </td>
312         <td align='center'>
313                 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
314                         <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' title='$Lang::tr{'edit'}' alt='$Lang::tr{'edit'}' />
315                         <input type='hidden' name='ID' value='$id' />
316                         <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
317                 </form>
318         </td>
319         <td align='center'>
320                 <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>
321                         <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}' />
322                         <input type='hidden' name='ID' value='$id' />
323                         <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
324                 </form>
325         </td>
326 </tr>
327 END
328         ;
329 }
330 print "</table>\n";
331
332 ###
333 # Print the legend at the bottom if there are any configured entries.
334 #
335 # Check if the file size is zero - no existing entries.
336 if ( ! -z "$filename") {
337 print <<END
338 <table>
339         <tr>
340                 <td class='boldbase'>&nbsp; <b>$Lang::tr{'legend'}:</b></td>
341                 <td>&nbsp; <img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
342                 <td class='base'>$Lang::tr{'click to disable'}</td>
343                 <td>&nbsp; &nbsp; <img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
344                 <td class='base'>$Lang::tr{'click to enable'}</td>
345                 <td>&nbsp; &nbsp; <img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
346                 <td class='base'>$Lang::tr{'edit'}</td>
347                 <td>&nbsp; &nbsp; <img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
348                 <td class='base'>$Lang::tr{'remove'}</td>
349         </tr>
350 </table>
351 END
352 ;
353 }
354
355 &Header::closebox();
356
357 &Header::closebigbox();
358
359 &Header::closepage();