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