]>
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'} = ''; | |
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 | } | |
df7340d2 MT |
109 | # Restart unbound |
110 | system('/usr/local/bin/unboundctrl restart >/dev/null'); | |
e4ba53ed SS |
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); | |
df7340d2 MT |
127 | # Restart unbound. |
128 | system('/usr/local/bin/unboundctrl restart >/dev/null'); | |
e4ba53ed SS |
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); | |
df7340d2 MT |
151 | # Restart unbound. |
152 | system('/usr/local/bin/unboundctrl restart >/dev/null'); | |
e4ba53ed SS |
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 " </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> | |
e3edceeb | 210 | <td width='20%' class='base'>$Lang::tr{'dnsforward zone'}: <img src='/blob.gif' alt='*' /></td> |
e4ba53ed SS |
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> | |
e3edceeb | 216 | <td width='20%' class='base'>$Lang::tr{'dnsforward forward_server'}: <img src='/blob.gif' alt='*' /></td> |
e4ba53ed SS |
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> | |
e3edceeb | 223 | <td width ='20%' class='base'>$Lang::tr{'remark'}:</td> |
e4ba53ed SS |
224 | <td><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' maxlength='50' /></td> |
225 | </tr> | |
226 | </table> | |
ff92ff17 | 227 | <br> |
e4ba53ed SS |
228 | <hr> |
229 | ||
230 | <table width='100%'> | |
231 | <tr> | |
e3edceeb | 232 | <td class='base' width='55%'><img src='/blob.gif' alt ='*' align='top' /> $Lang::tr{'required field'}</td> |
ff92ff17 | 233 | <td width='40%' align='right'> |
e4ba53ed SS |
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 | |
0317493a | 255 | <table width='100%' class='tbl'> |
e4ba53ed | 256 | <tr> |
0317493a AM |
257 | <th width='35%' class='boldbase' align='center'><b>$Lang::tr{'dnsforward zone'}</b></th> |
258 | <th width='30%' class='boldbase' align='center'><b>$Lang::tr{'dnsforward forward_server'}</b></th> | |
259 | <th width='30%' class='boldbase' align='center'><b>$Lang::tr{'remark'}</b></th> | |
260 | <th width='5%' class='boldbase' colspan='3' align='center'><b>$Lang::tr{'action'}</b></th> | |
e4ba53ed SS |
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; | |
0317493a | 277 | my $col=""; |
e4ba53ed SS |
278 | foreach my $line (@current) |
279 | { | |
280 | $id++; | |
281 | chomp($line); | |
282 | my @temp = split(/\,/,$line); | |
283 | my $toggle = ''; | |
284 | my $gif = ''; | |
285 | my $gdesc = ''; | |
286 | my $toggle = ''; | |
287 | ||
288 | if($cgiparams{'ACTION'} eq $Lang::tr{'edit'} && $cgiparams{'ID'} eq $id) { | |
0317493a AM |
289 | print "<tr>"; |
290 | $col="bgcolor='${Header::colouryellow}'"; } | |
e4ba53ed | 291 | elsif ($id % 2) { |
0317493a AM |
292 | print "<tr>"; |
293 | $col="bgcolor='$color{'color22'}'"; } | |
e4ba53ed | 294 | else { |
0317493a AM |
295 | print "<tr>"; |
296 | $col="bgcolor='$color{'color20'}'"; } | |
e4ba53ed SS |
297 | |
298 | if ($temp[0] eq 'on') { $gif='on.gif'; $toggle='off'; $gdesc=$Lang::tr{'click to disable'};} | |
299 | else { $gif='off.gif'; $toggle='on'; $gdesc=$Lang::tr{'click to enable'}; } | |
300 | ||
301 | ### | |
302 | # Display edit page. | |
303 | # | |
304 | print <<END | |
0317493a AM |
305 | <td align='center' $col>$temp[1]</td> |
306 | <td align='center' $col>$temp[2]</td> | |
307 | <td align='center' $col>$temp[3]</td> | |
308 | <td align='center' $col> | |
e4ba53ed SS |
309 | <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'> |
310 | <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' title='$gdesc' alt='$gdesc' /> | |
311 | <input type='hidden' name='ID' value='$id' /> | |
312 | <input type='hidden' name='ENABLE' value='$toggle' /> | |
313 | <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' /> | |
314 | </form> | |
315 | </td> | |
0317493a | 316 | <td align='center' $col> |
e4ba53ed SS |
317 | <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'> |
318 | <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' title='$Lang::tr{'edit'}' alt='$Lang::tr{'edit'}' /> | |
319 | <input type='hidden' name='ID' value='$id' /> | |
320 | <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' /> | |
321 | </form> | |
322 | </td> | |
0317493a | 323 | <td align='center' $col> |
e4ba53ed SS |
324 | <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'> |
325 | <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}' /> | |
326 | <input type='hidden' name='ID' value='$id' /> | |
327 | <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' /> | |
328 | </form> | |
329 | </td> | |
330 | </tr> | |
331 | END | |
332 | ; | |
333 | } | |
334 | print "</table>\n"; | |
335 | ||
336 | ### | |
337 | # Print the legend at the bottom if there are any configured entries. | |
338 | # | |
339 | # Check if the file size is zero - no existing entries. | |
340 | if ( ! -z "$filename") { | |
341 | print <<END | |
342 | <table> | |
343 | <tr> | |
344 | <td class='boldbase'> <b>$Lang::tr{'legend'}:</b></td> | |
345 | <td> <img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td> | |
346 | <td class='base'>$Lang::tr{'click to disable'}</td> | |
347 | <td> <img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td> | |
348 | <td class='base'>$Lang::tr{'click to enable'}</td> | |
349 | <td> <img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td> | |
350 | <td class='base'>$Lang::tr{'edit'}</td> | |
351 | <td> <img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td> | |
352 | <td class='base'>$Lang::tr{'remove'}</td> | |
353 | </tr> | |
354 | </table> | |
355 | END | |
356 | ; | |
357 | } | |
358 | ||
359 | &Header::closebox(); | |
360 | ||
361 | &Header::closebigbox(); | |
362 | ||
363 | &Header::closepage(); |