]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/dmzholes.cgi
git-svn-id: http://svn.ipfire.org/svn/ipfire/IPFire/source@16 ea5c0bd1-69bd-2848...
[ipfire-2.x.git] / html / cgi-bin / dmzholes.cgi
1 #!/usr/bin/perl
2 #
3 # SmoothWall CGIs
4 #
5 # This code is distributed under the terms of the GPL
6 #
7 # (c) The SmoothWall Team
8 #
9 # $Id: dmzholes.cgi,v 1.9.2.16 2005/10/18 17:05:27 franck78 Exp $
10 #
11
12 use strict;
13
14 # enable only the following on debugging purpose
15 #use warnings;
16 #use CGI::Carp 'fatalsToBrowser';
17
18 require 'CONFIG_ROOT/general-functions.pl';
19 require "${General::swroot}/lang.pl";
20 require "${General::swroot}/header.pl";
21
22 #workaround to suppress a warning when a variable is used only once
23 my @dummy = ( ${Header::table2colour}, ${Header::colouryellow} );
24 undef (@dummy);
25
26 my %cgiparams=();
27 my %checked=();
28 my %selected=();
29 my %netsettings=();
30 my $errormessage = '';
31 my $filename = "${General::swroot}/dmzholes/config";
32
33 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
34
35 &Header::showhttpheaders();
36
37 $cgiparams{'ENABLED'} = 'off';
38 $cgiparams{'REMARK'} = '';
39 $cgiparams{'ACTION'} = '';
40 $cgiparams{'SRC_IP'} = '';
41 $cgiparams{'DEST_IP'} ='';
42 $cgiparams{'DEST_PORT'} = '';
43 &Header::getcgihash(\%cgiparams);
44
45 open(FILE, $filename) or die 'Unable to open config file.';
46 my @current = <FILE>;
47 close(FILE);
48
49 if ($cgiparams{'ACTION'} eq $Lang::tr{'add'})
50 {
51 unless($cgiparams{'PROTOCOL'} =~ /^(tcp|udp)$/) { $errormessage = $Lang::tr{'invalid input'}; }
52 unless(&General::validipormask($cgiparams{'SRC_IP'})) { $errormessage = $Lang::tr{'source ip bad'}; }
53 unless($errormessage){$errormessage = &General::validportrange($cgiparams{'DEST_PORT'},'dst');}
54 unless(&General::validipormask($cgiparams{'DEST_IP'})) { $errormessage = $Lang::tr{'destination ip bad'}; }
55 unless ($errormessage) {
56 $errormessage = &validNet($cgiparams{'SRC_NET'},$cgiparams{'DEST_NET'}); }
57 # Darren Critchley - Remove commas from remarks
58 $cgiparams{'REMARK'} = &Header::cleanhtml($cgiparams{'REMARK'});
59
60 unless ($errormessage)
61 {
62 if($cgiparams{'EDITING'} eq 'no') {
63 open(FILE,">>$filename") or die 'Unable to open config file.';
64 flock FILE, 2;
65 print FILE "$cgiparams{'PROTOCOL'},"; # [0]
66 print FILE "$cgiparams{'SRC_IP'},"; # [1]
67 print FILE "$cgiparams{'DEST_IP'},"; # [2]
68 print FILE "$cgiparams{'DEST_PORT'},"; # [3]
69 print FILE "$cgiparams{'ENABLED'},"; # [4]
70 print FILE "$cgiparams{'SRC_NET'},"; # [5]
71 print FILE "$cgiparams{'DEST_NET'},"; # [6]
72 print FILE "$cgiparams{'REMARK'}\n"; # [7]
73 } else {
74 open(FILE,">$filename") or die 'Unable to open config file.';
75 flock FILE, 2;
76 my $id = 0;
77 foreach my $line (@current)
78 {
79 $id++;
80 if ($cgiparams{'EDITING'} eq $id) {
81 print FILE "$cgiparams{'PROTOCOL'},"; # [0]
82 print FILE "$cgiparams{'SRC_IP'},"; # [1]
83 print FILE "$cgiparams{'DEST_IP'},"; # [2]
84 print FILE "$cgiparams{'DEST_PORT'},"; # [3]
85 print FILE "$cgiparams{'ENABLED'},"; # [4]
86 print FILE "$cgiparams{'SRC_NET'},"; # [5]
87 print FILE "$cgiparams{'DEST_NET'},"; # [6]
88 print FILE "$cgiparams{'REMARK'}\n"; # [7]
89 } else { print FILE "$line"; }
90 }
91 }
92 close(FILE);
93 undef %cgiparams;
94 &General::log($Lang::tr{'dmz pinhole rule added'});
95 system('/usr/local/bin/setdmzholes');
96 }
97 }
98 if ($cgiparams{'ACTION'} eq $Lang::tr{'remove'})
99 {
100 my $id = 0;
101 open(FILE, ">$filename") or die 'Unable to open config file.';
102 flock FILE, 2;
103 foreach my $line (@current)
104 {
105 $id++;
106 unless ($cgiparams{'ID'} eq $id) { print FILE "$line"; }
107 }
108 close(FILE);
109 system('/usr/local/bin/setdmzholes');
110 &General::log($Lang::tr{'dmz pinhole rule removed'});
111 }
112 if ($cgiparams{'ACTION'} eq $Lang::tr{'toggle enable disable'})
113 {
114 my $id = 0;
115 open(FILE, ">$filename") or die 'Unable to open config file.';
116 flock FILE, 2;
117 foreach my $line (@current)
118 {
119 $id++;
120 unless ($cgiparams{'ID'} eq $id) { print FILE "$line"; }
121 else
122 {
123 chomp($line);
124 my @temp = split(/\,/,$line);
125 print FILE "$temp[0],$temp[1],$temp[2],$temp[3],$cgiparams{'ENABLE'},$temp[5],$temp[6],$temp[7]\n";
126 }
127 }
128 close(FILE);
129 system('/usr/local/bin/setdmzholes');
130 }
131 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'})
132 {
133 my $id = 0;
134 foreach my $line (@current)
135 {
136 $id++;
137 if ($cgiparams{'ID'} eq $id)
138 {
139 chomp($line);
140 my @temp = split(/\,/,$line);
141 $cgiparams{'PROTOCOL'} = $temp[0];
142 $cgiparams{'SRC_IP'} = $temp[1];
143 $cgiparams{'DEST_IP'} = $temp[2];
144 $cgiparams{'DEST_PORT'} = $temp[3];
145 $cgiparams{'ENABLED'} = $temp[4];
146 $cgiparams{'SRC_NET'} = $temp[5];
147 $cgiparams{'DEST_NET'} = $temp[6];
148 $cgiparams{'REMARK'} = $temp[7];
149 }
150 }
151 }
152
153 if ($cgiparams{'ACTION'} eq '')
154 {
155 $cgiparams{'PROTOCOL'} = 'tcp';
156 $cgiparams{'ENABLED'} = 'on';
157 $cgiparams{'SRC_NET'} = 'orange';
158 $cgiparams{'DEST_NET'} = 'blue';
159 }
160
161 $selected{'PROTOCOL'}{'udp'} = '';
162 $selected{'PROTOCOL'}{'tcp'} = '';
163 $selected{'PROTOCOL'}{$cgiparams{'PROTOCOL'}} = "selected='selected'";
164
165 $selected{'SRC_NET'}{'orange'} = '';
166 $selected{'SRC_NET'}{'blue'} = '';
167 $selected{'SRC_NET'}{$cgiparams{'SRC_NET'}} = "selected='selected'";
168
169 $selected{'DEST_NET'}{'blue'} = '';
170 $selected{'DEST_NET'}{'green'} = '';
171 $selected{'DEST_NET'}{$cgiparams{'DEST_NET'}} = "selected='selected'";
172
173 $checked{'ENABLED'}{'off'} = '';
174 $checked{'ENABLED'}{'on'} = '';
175 $checked{'ENABLED'}{$cgiparams{'ENABLED'}} = "checked='checked'";
176
177 &Header::openpage($Lang::tr{'dmz pinhole configuration'}, 1, '');
178
179 &Header::openbigbox('100%', 'left', '', $errormessage);
180
181 if ($errormessage) {
182 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
183 print "<class name='base'>$errormessage\n";
184 print "&nbsp;</class>\n";
185 &Header::closebox();
186 }
187
188 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
189
190 my $buttonText = $Lang::tr{'add'};
191 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) {
192 &Header::openbox('100%', 'left', $Lang::tr{'edit a rule'});
193 $buttonText = $Lang::tr{'update'};
194 } else {
195 &Header::openbox('100%', 'left', $Lang::tr{'add a new rule'});
196 }
197 print <<END
198 <table width='100%'>
199 <tr>
200 <td>
201 <select name='PROTOCOL'>
202 <option value='udp' $selected{'PROTOCOL'}{'udp'}>UDP</option>
203 <option value='tcp' $selected{'PROTOCOL'}{'tcp'}>TCP</option>
204 </select>
205 </td>
206 <td>
207 $Lang::tr{'source net'}:</td>
208 <td>
209 <select name='SRC_NET'>
210 END
211 ;
212 if (&haveOrangeNet()) {
213 print "<option value='orange' $selected{'SRC_NET'}{'orange'}>$Lang::tr{'orange'}</option>";
214 }
215 if (&haveBlueNet()) {
216 print "<option value='blue' $selected{'SRC_NET'}{'blue'}>$Lang::tr{'blue'}</option>";
217 }
218 print <<END
219 </select>
220 </td>
221 <td class='base'>$Lang::tr{'source ip or net'}:</td>
222 <td><input type='text' name='SRC_IP' value='$cgiparams{'SRC_IP'}' size='15' /></td>
223 </tr>
224 <tr>
225 <td>
226 &nbsp;</td>
227 <td>
228 $Lang::tr{'destination net'}:</td>
229 <td>
230 <select name='DEST_NET'>
231 END
232 ;
233 if (&haveOrangeNet() && &haveBlueNet()) {
234 print "<option value='blue' $selected{'DEST_NET'}{'blue'}>$Lang::tr{'blue'}</option>";
235 }
236
237 print <<END
238 <option value='green' $selected{'DEST_NET'}{'green'}>$Lang::tr{'green'}</option>
239 </select>
240 </td>
241 <td class='base'>
242 $Lang::tr{'destination ip or net'}:</td>
243 <td>
244 <input type='text' name='DEST_IP' value='$cgiparams{'DEST_IP'}' size='15' />
245 </td>
246 <td class='base'>
247 $Lang::tr{'destination port'}:&nbsp;
248 <input type='text' name='DEST_PORT' value='$cgiparams{'DEST_PORT'}' size='5' />
249 </td>
250 </tr>
251 </table>
252 <table width='100%'>
253 <tr>
254 <td colspan='3' width='50%' class='base'>
255 <font class='boldbase'>$Lang::tr{'remark title'}&nbsp;<img src='/blob.gif' alt='*' /></font>
256 <input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='55' maxlength='50' />
257 </td>
258 </tr>
259 <tr>
260 <td class='base' width='50%'>
261 <img src='/blob.gif' alt ='*' align='top' />&nbsp;
262 <font class='base'>$Lang::tr{'this field may be blank'}</font>
263 </td>
264 <td class='base' width='25%' align='center'>$Lang::tr{'enabled'}<input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
265 <td width='25%' align='center'>
266 <input type='hidden' name='ACTION' value='$Lang::tr{'add'}' />
267 <input type='submit' name='SUBMIT' value='$buttonText' />
268 </td>
269 </tr>
270 </table>
271 END
272 ;
273 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'}) {
274 print "<input type='hidden' name='EDITING' value='$cgiparams{'ID'}' />\n";
275 } else {
276 print "<input type='hidden' name='EDITING' value='no' />\n";
277 }
278 &Header::closebox();
279 print "</form>\n";
280
281 &Header::openbox('100%', 'left', $Lang::tr{'current rules'});
282 print <<END
283 <table width='100%'>
284 <tr>
285 <td width='7%' class='boldbase' align='center'><b>$Lang::tr{'proto'}</b></td>
286 <td width='3%' class='boldbase' align='center'><b>$Lang::tr{'net'}</b></td>
287 <td width='25%' class='boldbase' align='center'><b>$Lang::tr{'source'}</b></td>
288 <td width='2%' class='boldbase' align='center'>&nbsp;</td>
289 <td width='3%' class='boldbase' align='center'><b>$Lang::tr{'net'}</b></td>
290 <td width='25%' class='boldbase' align='center'><b>$Lang::tr{'destination'}</b></td>
291 <td width='30%' class='boldbase' align='center'><b>$Lang::tr{'remark'}</b></td>
292 <td width='1%' class='boldbase' align='center'>&nbsp;</td>
293 <td width='4%' class='boldbase' colspan='3' align='center'><b>$Lang::tr{'action'}</b></td>
294 END
295 ;
296
297 # Achim Weber: if i add a new rule, this rule is not displayed?!?
298 # we re-read always config.
299 # If something has happeened re-read config
300 #if($cgiparams{'ACTION'} ne '')
301 #{
302 open(FILE, $filename) or die 'Unable to open config file.';
303 @current = <FILE>;
304 close(FILE);
305 #}
306 my $id = 0;
307 foreach my $line (@current)
308 {
309 my $protocol='';
310 my $gif='';
311 my $toggle='';
312 my $gdesc='';
313 $id++;
314 chomp($line);
315 my @temp = split(/\,/,$line);
316 if ($temp[0] eq 'udp') { $protocol = 'UDP'; } else { $protocol = 'TCP' }
317
318 my $srcnetcolor = ($temp[5] eq 'blue')? ${Header::colourblue} : ${Header::colourorange};
319 my $destnetcolor = ($temp[6] eq 'blue')? ${Header::colourblue} : ${Header::colourgreen};
320
321 if ($cgiparams{'ACTION'} eq $Lang::tr{'edit'} && $cgiparams{'ID'} eq $id) {
322 print "<tr bgcolor='${Header::colouryellow}'>\n"; }
323 elsif ($id % 2) {
324 print "<tr bgcolor='${Header::table1colour}'>\n"; }
325 else {
326 print "<tr bgcolor='${Header::table2colour}'>\n"; }
327 if ($temp[4] eq 'on') { $gif='on.gif'; $toggle='off'; $gdesc=$Lang::tr{'click to disable'};}
328 else { $gif = 'off.gif'; $toggle='on'; $gdesc=$Lang::tr{'click to enable'}; }
329
330 # Darren Critchley - Get Port Service Name if we can - code borrowed from firewalllog.dat
331 my $dstprt =$temp[3];
332 $_=$temp[3];
333 if (/^\d+$/) {
334 my $servi = uc(getservbyport($temp[3], lc($temp[0])));
335 if ($servi ne '' && $temp[3] < 1024) {
336 $dstprt = "$dstprt($servi)"; }
337 }
338 # Darren Critchley - If the line is too long, wrap the port numbers
339 my $dstaddr = "$temp[2] : $dstprt";
340 if (length($dstaddr) > 26) {
341 $dstaddr = "$temp[2] :<br /> $dstprt";
342 }
343 print <<END
344 <td align='center'>$protocol</td>
345 <td bgcolor='$srcnetcolor'></td>
346 <td align='center'>$temp[1]</td>
347 <td align='center'><img src='/images/forward.gif' /></td>
348 <td bgcolor='$destnetcolor'></td>
349 <td align='center'>$dstaddr</td>
350 <td align='center'>$temp[7]</td>
351
352 <td align='center'>
353 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
354 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' />
355 <input type='hidden' name='ID' value='$id' />
356 <input type='hidden' name='ENABLE' value='$toggle' />
357 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
358 </form>
359 </td>
360
361 <td align='center'>
362 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
363 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' />
364 <input type='hidden' name='ID' value='$id' />
365 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
366 </form>
367 </td>
368
369 <td align='center'>
370 <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>
371 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' />
372 <input type='hidden' name='ID' value='$id' />
373 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
374 </form>
375 </td>
376
377 </tr>
378 END
379 ;
380 }
381 print "</table>\n";
382
383 # If the fixed lease file contains entries, print Key to action icons
384 if ( ! -z "$filename") {
385 print <<END
386 <table>
387 <tr>
388 <td class='boldbase'>&nbsp; <b>$Lang::tr{'legend'}:</b></td>
389 <td>&nbsp; <img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
390 <td class='base'>$Lang::tr{'click to disable'}</td>
391 <td>&nbsp; &nbsp; <img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
392 <td class='base'>$Lang::tr{'click to enable'}</td>
393 <td>&nbsp; &nbsp; <img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
394 <td class='base'>$Lang::tr{'edit'}</td>
395 <td>&nbsp; &nbsp; <img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
396 <td class='base'>$Lang::tr{'remove'}</td>
397 </tr>
398 </table>
399 END
400 ;
401 }
402
403 &Header::closebox();
404
405 &Header::closebigbox();
406
407 &Header::closepage();
408
409 sub validNet
410 {
411 my $srcNet = $_[0];
412 my $destNet = $_[1];
413
414 if ($srcNet eq $destNet) {
415 return $Lang::tr{'dmzpinholes for same net not necessary'}; }
416 unless ($srcNet =~ /^(blue|orange)$/) {
417 return $Lang::tr{'select source net'}; }
418 unless ($destNet =~ /^(blue|green)$/) {
419 return $Lang::tr{'select dest net'}; }
420
421 return '';
422 }
423
424 sub haveOrangeNet
425 {
426 if ($netsettings{'CONFIG_TYPE'} == 1) {return 1;}
427 if ($netsettings{'CONFIG_TYPE'} == 3) {return 1;}
428 if ($netsettings{'CONFIG_TYPE'} == 5) {return 1;}
429 if ($netsettings{'CONFIG_TYPE'} == 7) {return 1;}
430 return 0;
431 }
432
433 sub haveBlueNet
434 {
435 if ($netsettings{'CONFIG_TYPE'} == 4) {return 1;}
436 if ($netsettings{'CONFIG_TYPE'} == 5) {return 1;}
437 if ($netsettings{'CONFIG_TYPE'} == 6) {return 1;}
438 if ($netsettings{'CONFIG_TYPE'} == 7) {return 1;}
439 return 0;
440 }