ab7c2fa08d484f80bc8b44547b8cae695fa83aab
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / outgoingfw.cgi
1 #!/usr/bin/perl
2 #
3 # IPFire CGIs
4 #
5 # This code is distributed under the terms of the GPL
6 #
7 # (c) The IPFire Team
8 #
9
10 use strict;
11 # enable only the following on debugging purpose
12 use warnings;
13 use CGI::Carp 'fatalsToBrowser';
14
15 require '/var/ipfire/general-functions.pl';
16 require "${General::swroot}/lang.pl";
17 require "${General::swroot}/header.pl";
18
19 my %outfwsettings = ();
20 my %checked = ();
21 my %selected= () ;
22 my %netsettings = ();
23 my $errormessage = "";
24 my $configentry = "";
25 my @configs = ();
26 my @configline = ();
27 my $p2pentry = "";
28 my @p2ps = ();
29 my @p2pline = ();
30
31 my $configfile = "/var/ipfire/outgoing/rules";
32 my $p2pfile = "/var/ipfire/outgoing/p2protocols";
33
34 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
35
36 &Header::showhttpheaders();
37
38 ### Values that have to be initialized
39 $outfwsettings{'ACTION'} = '';
40 $outfwsettings{'VALID'} = 'yes';
41 $outfwsettings{'EDIT'} = 'no';
42 $outfwsettings{'NAME'} = '';
43 $outfwsettings{'SNET'} = '';
44 $outfwsettings{'SIP'} = '';
45 $outfwsettings{'SPORT'} = '';
46 $outfwsettings{'SMAC'} = '';
47 $outfwsettings{'DIP'} = '';
48 $outfwsettings{'DPORT'} = '';
49 $outfwsettings{'PROT'} = '';
50 $outfwsettings{'STATE'} = '';
51 $outfwsettings{'DISPLAY_DIP'} = '';
52 $outfwsettings{'DISPLAY_DPORT'} = '';
53 $outfwsettings{'DISPLAY_SMAC'} = '';
54 $outfwsettings{'DISPLAY_SIP'} = '';
55 $outfwsettings{'POLICY'} = 'MODE0';
56
57 &General::readhash("${General::swroot}/outgoing/settings", \%outfwsettings);
58 &Header::getcgihash(\%outfwsettings);
59
60 if ($outfwsettings{'POLICY'} eq 'MODE0'){ $selected{'POLICY'}{'MODE0'} = 'selected'; } else { $selected{'POLICY'}{'MODE0'} = ''; }
61 if ($outfwsettings{'POLICY'} eq 'MODE1'){ $selected{'POLICY'}{'MODE1'} = 'selected'; } else { $selected{'POLICY'}{'MODE1'} = ''; }
62 if ($outfwsettings{'POLICY'} eq 'MODE2'){ $selected{'POLICY'}{'MODE2'} = 'selected'; } else { $selected{'POLICY'}{'MODE2'} = ''; }
63
64 &Header::openpage('Ausgehende Firewall', 1, '');
65 &Header::openbigbox('100%', 'left', '', $errormessage);
66
67 ############################################################################################################################
68 ############################################################################################################################
69
70 if ($outfwsettings{'ACTION'} eq $Lang::tr{'reset'})
71 {
72 $outfwsettings{'POLICY'}='MODE0';
73 unlink $configfile;
74 system("/usr/bin/touch $configfile");
75 &General::writehash("${General::swroot}/outgoing/settings", \%outfwsettings);
76 }
77 if ($outfwsettings{'ACTION'} eq $Lang::tr{'save'})
78 {
79 &General::writehash("${General::swroot}/outgoing/settings", \%outfwsettings);
80 }
81 if ($outfwsettings{'ACTION'} eq 'enable')
82 {
83 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
84 @p2ps = <FILE>;
85 close FILE;
86 open( FILE, "> $p2pfile" ) or die "Unable to write $p2pfile";
87 foreach $p2pentry (sort @p2ps)
88 {
89 @p2pline = split( /\;/, $p2pentry );
90 if ($p2pline[1] eq $outfwsettings{'P2PROT'}) {
91 print FILE "$p2pline[0];$p2pline[1];on;\n";
92 } else {
93 print FILE "$p2pline[0];$p2pline[1];$p2pline[2];\n";
94 }
95 }
96 close FILE;
97 }
98 if ($outfwsettings{'ACTION'} eq 'disable')
99 {
100 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
101 @p2ps = <FILE>;
102 close FILE;
103 open( FILE, "> $p2pfile" ) or die "Unable to write $p2pfile";
104 foreach $p2pentry (sort @p2ps)
105 {
106 @p2pline = split( /\;/, $p2pentry );
107 if ($p2pline[1] eq $outfwsettings{'P2PROT'}) {
108 print FILE "$p2pline[0];$p2pline[1];off;\n";
109 } else {
110 print FILE "$p2pline[0];$p2pline[1];$p2pline[2];\n";
111 }
112 }
113 close FILE;
114 }
115 if ($outfwsettings{'ACTION'} eq $Lang::tr{'edit'})
116 {
117 open( FILE, "< $configfile" ) or die "Unable to read $configfile";
118 @configs = <FILE>;
119 close FILE;
120 open( FILE, "> $configfile" ) or die "Unable to write $configfile";
121 foreach $configentry (sort @configs)
122 {
123 @configline = split( /\;/, $configentry );
124 unless (($configline[0] eq $outfwsettings{'STATE'}) &&
125 ($configline[1] eq $outfwsettings{'ENABLED'}) &&
126 ($configline[2] eq $outfwsettings{'SNET'}) &&
127 ($configline[3] eq $outfwsettings{'PROT'}) &&
128 ($configline[4] eq $outfwsettings{'NAME'}) &&
129 ($configline[5] eq $outfwsettings{'SIP'}) &&
130 ($configline[6] eq $outfwsettings{'SMAC'}) &&
131 ($configline[7] eq $outfwsettings{'DIP'}) &&
132 ($configline[8] eq $outfwsettings{'DPORT'}))
133 {
134 print FILE $configentry;
135 }
136 }
137 close FILE;
138 $selected{'SNET'}{"$outfwsettings{'SNET'}"} = 'selected';
139 $selected{'PROT'}{"$outfwsettings{'PROT'}"} = 'selected';
140 &addrule();
141 &Header::closebigbox();
142 &Header::closepage();
143 exit
144 }
145 if ($outfwsettings{'ACTION'} eq $Lang::tr{'delete'})
146 {
147 open( FILE, "< $configfile" ) or die "Unable to read $configfile";
148 @configs = <FILE>;
149 close FILE;
150 open( FILE, "> $configfile" ) or die "Unable to write $configfile";
151 foreach $configentry (sort @configs)
152 {
153 @configline = split( /\;/, $configentry );
154 unless (($configline[0] eq $outfwsettings{'STATE'}) &&
155 ($configline[1] eq $outfwsettings{'ENABLED'}) &&
156 ($configline[2] eq $outfwsettings{'SNET'}) &&
157 ($configline[3] eq $outfwsettings{'PROT'}) &&
158 ($configline[4] eq $outfwsettings{'NAME'}) &&
159 ($configline[5] eq $outfwsettings{'SIP'}) &&
160 ($configline[6] eq $outfwsettings{'SMAC'}) &&
161 ($configline[7] eq $outfwsettings{'DIP'}) &&
162 ($configline[8] eq $outfwsettings{'DPORT'}))
163 {
164 print FILE $configentry;
165 }
166 }
167 close FILE;
168 }
169 if ($outfwsettings{'ACTION'} eq $Lang::tr{'add'})
170 {
171 if ( $outfwsettings{'VALID'} eq 'yes' ) {
172 open( FILE, ">> $configfile" ) or die "Unable to write $configfile";
173 print FILE <<END
174 $outfwsettings{'STATE'};$outfwsettings{'ENABLED'};$outfwsettings{'SNET'};$outfwsettings{'PROT'};$outfwsettings{'NAME'};$outfwsettings{'SIP'};$outfwsettings{'SMAC'};$outfwsettings{'DIP'};$outfwsettings{'DPORT'};
175 END
176 ;
177 close FILE;
178 } else {
179 $outfwsettings{'ACTION'} = 'Add rule';
180 }
181 }
182 if ($outfwsettings{'ACTION'} eq 'Add rule')
183 {
184 &addrule();
185 exit
186 }
187
188 &General::readhash("${General::swroot}/outgoing/settings", \%outfwsettings);
189
190 if ($errormessage) {
191 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
192 print "<class name='base'>$errormessage\n";
193 print "&nbsp;</class>\n";
194 &Header::closebox();
195 }
196
197 ############################################################################################################################
198 ############################################################################################################################
199
200 if ($outfwsettings{'POLICY'} ne 'MODE0'){
201 &Header::openbox('100%', 'center', 'Rules');
202 print <<END
203 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
204 <input type='submit' name='ACTION' value='Add rule'>
205 </form>
206 END
207 ;
208 open( FILE, "< $configfile" ) or die "Unable to read $configfile";
209 @configs = <FILE>;
210 close FILE;
211 if (@configs) {
212 print <<END
213 <hr>
214 <table border='0' width='100%' cellspacing='0'>
215 <tr bgcolor='white'>
216 <td width='14%'><b>Protokoll</b>
217 <td width='14%'><b>Netzwerk</b>
218 <td width='14%'><b>Ziel</b>
219 <td width='14%'><b>Anmerkung</b>
220 <td width='14%'><b>Politik</b>
221 <td width='30%'><b>Aktionen</b>
222 END
223 ;
224 foreach $configentry (sort @configs)
225 {
226 @configline = split( /\;/, $configentry );
227 $outfwsettings{'STATE'} = $configline[0];
228 $outfwsettings{'ENABLED'} = $configline[1];
229 $outfwsettings{'SNET'} = $configline[2];
230 $outfwsettings{'PROT'} = $configline[3];
231 $outfwsettings{'NAME'} = $configline[4];
232 $outfwsettings{'SIP'} = $configline[5];
233 $outfwsettings{'SMAC'} = $configline[6];
234 $outfwsettings{'DIP'} = $configline[7];
235 $outfwsettings{'DPORT'} = $configline[8];
236 if ($outfwsettings{'DIP'} eq ''){ $outfwsettings{'DISPLAY_DIP'} = 'ALL'; } else { $outfwsettings{'DISPLAY_DIP'} = $outfwsettings{'DIP'}; }
237 if ($outfwsettings{'DPORT'} eq ''){ $outfwsettings{'DISPLAY_DPORT'} = 'ALL'; } else { $outfwsettings{'DISPLAY_DPORT'} = $outfwsettings{'DPORT'}; }
238 if ($outfwsettings{'STATE'} eq 'DENY'){ $outfwsettings{'DISPLAY_STATE'} = "<img src='/images/stock_stop.png' alt='DENY'>"; }
239 if ($outfwsettings{'STATE'} eq 'ALLOW'){ $outfwsettings{'DISPLAY_STATE'} = "<img src='/images/stock_ok.png' alt='ALLOW'>"; }
240 if ((($outfwsettings{'POLICY'} eq 'MODE1') && ($outfwsettings{'STATE'} eq 'ALLOW')) || (($outfwsettings{'POLICY'} eq 'MODE2') && ($outfwsettings{'STATE'} eq 'DENY'))){
241 print <<END
242 <tr bgcolor='#F0F0F0'>
243 <td align='center'>$outfwsettings{'PROT'}
244 <td align='center'>$outfwsettings{'SNET'}
245 <td align='center'>$outfwsettings{'DISPLAY_DIP'}:$outfwsettings{'DISPLAY_DPORT'}
246 <td align='center'>$outfwsettings{'NAME'}
247 <td align='center'>$outfwsettings{'DISPLAY_STATE'}
248 <td align='right'>
249 <table border='0' cellpadding='0' cellspacing='0'><tr>
250 <td><form method='post' action='$ENV{'SCRIPT_NAME'}'>
251 <input type='hidden' name='PROT' value=$outfwsettings{'PROT'}>
252 <input type='hidden' name='STATE' value=$outfwsettings{'STATE'}>
253 <input type='hidden' name='SNET' value=$outfwsettings{'SNET'}>
254 <input type='hidden' name='DPORT' value=$outfwsettings{'DPORT'}>
255 <input type='hidden' name='DIP' value=$outfwsettings{'DIP'}>
256 <input type='hidden' name='SIP' value=$outfwsettings{'SIP'}>
257 <input type='hidden' name='NAME' value=$outfwsettings{'NAME'}>
258 <input type='hidden' name='SMAC' value=$outfwsettings{'SMAC'}>
259 <input type='hidden' name='ENABLED' value=$outfwsettings{'ENABLED'}>
260 <input type='hidden' name='ACTION' value=$Lang::tr{'edit'}>
261 <input type='image' src='/images/edit.gif' width="20" height="20" alt=$Lang::tr{'edit'}>
262 </form>
263 <td><form method='post' action='$ENV{'SCRIPT_NAME'}'>
264 <input type='hidden' name='PROT' value=$outfwsettings{'PROT'}>
265 <input type='hidden' name='STATE' value=$outfwsettings{'STATE'}>
266 <input type='hidden' name='SNET' value=$outfwsettings{'SNET'}>
267 <input type='hidden' name='DPORT' value=$outfwsettings{'DPORT'}>
268 <input type='hidden' name='DIP' value=$outfwsettings{'DIP'}>
269 <input type='hidden' name='SIP' value=$outfwsettings{'SIP'}>
270 <input type='hidden' name='NAME' value=$outfwsettings{'NAME'}>
271 <input type='hidden' name='SMAC' value=$outfwsettings{'SMAC'}>
272 <input type='hidden' name='ENABLED' value=$outfwsettings{'ENABLED'}>
273 <input type='hidden' name='ACTION' value=$Lang::tr{'delete'}>
274 <input type='image' src='/images/delete.gif' width="20" height="20" alt=$Lang::tr{'delete'}>
275 </form></table>
276 END
277 ;
278 if (($outfwsettings{'SIP'}) || ($outfwsettings{'SMAC'})) {
279 unless ($outfwsettings{'SIP'}) { $outfwsettings{'DISPLAY_SIP'} = 'ALL'; } else { $outfwsettings{'DISPLAY_SIP'} = $outfwsettings{'SIP'}; }
280 unless ($outfwsettings{'SMAC'}) { $outfwsettings{'DISPLAY_SMAC'} = 'ALL'; } else { $outfwsettings{'DISPLAY_SMAC'} = $outfwsettings{'SMAC'}; }
281 print <<END
282 <tr><td width='14%' align='right'>Quell-IP-Adresse:
283 <td width='14%' align='left'>$outfwsettings{'DISPLAY_SIP'}
284 <td width='14%' align='right'>Quell-MAC-Adresse:
285 <td width='14%' align='left'>$outfwsettings{'DISPLAY_SMAC'}
286 <td width='44%' colspan='2' align='center'>
287 END
288 ;
289 }
290 print <<END
291 </form>
292 END
293 ;
294 }
295 }
296 print <<END
297 </table>
298 END
299 ;
300
301 }
302 &Header::closebox();
303 }
304
305 if ($outfwsettings{'POLICY'} eq 'MODE2'){
306 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
307 @p2ps = <FILE>;
308 close FILE;
309 &Header::openbox('100%', 'center', 'P2P-Block');
310 print <<END
311 <table width='40%'>
312 <tr bgcolor='#FFFFFF'><td width='66%'><b>Protokoll</b>
313 <td width='33%'><b>Status</b>
314 END
315 ;
316 my $id = 1;
317 foreach $p2pentry (sort @p2ps)
318 {
319 @p2pline = split( /\;/, $p2pentry );
320 print <<END
321 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
322 END
323 ;
324 if ($id % 2) {
325 print "\t\t\t<tr bgcolor='#F0F0F0'>\n";
326 }
327 else {
328 print "\t\t\t<tr bgcolor='#FAFAFA'>\n";
329 }
330 $id++;
331 print <<END
332 <td width='66%' align='center'>$p2pline[0]:
333 <td width='33%' align='center'><input type='hidden' name='P2PROT' value=$p2pline[1]>
334 END
335 ;
336 if ($p2pline[2] eq 'on') {
337 print <<END
338 <input type='hidden' name='ACTION' value='disable'>
339 <input type='image' name='submit' src='/images/stock_ok.png' alt=''>
340 END
341 ;
342 } else {
343 print <<END
344 <input type='hidden' name='ACTION' value='enable'>
345 <input type='image' name='submit' src='/images/stock_stop.png' alt=''>
346 END
347 ;
348 }
349 print <<END
350 </form>
351 END
352 ;
353 }
354 print <<END
355 <tr><td colspan='2' align='center'>Klicken Sie auf die Symbole um das entsprechende P2P-Netz zu (de-)aktivieren.
356 </table>
357 END
358 ;
359 &Header::closebox();
360 }
361
362 &Header::openbox('100%', 'center', 'Policy');
363 print <<END
364 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
365 <table width='100%'>
366 <tr><td width='10%' align='right'><b>Modus 0:</b><td width='90%' align='left' colspan='2'>In diesem Modus ist es allen Rechnern im Netzwerk uneingeschraenkt moeglich Verbindungen ins Internet aufzubauen.
367 <tr><td width='10%' align='right'><b>Modus 1:</b><td width='90%' align='left' colspan='2'>In diesem Modus werden nur Verbindungen nach den oben definierten Regeln zugelassen.
368 <tr><td width='10%' align='right'><b>Modus 2:</b><td width='90%' align='left' colspan='2'>In diesem Modus werden saemtliche Verbindungen erlaubt, bis auf die oben definierten Block-Regeln.<br>Hier ist eine Besonderheit der P2P-Filter.
369 <tr><td colspan='3'><hr>
370 <tr><td width='10%' align='right'> <select name='POLICY'><option value='MODE0' $selected{'POLICY'}{'MODE0'}>Modus 0</option><option value='MODE1' $selected{'POLICY'}{'MODE1'}>Modus 1</option><option value='MODE2' $selected{'POLICY'}{'MODE2'}>Modus 2</option></select>
371 <td width='45%' align='left'><input type='submit' name='ACTION' value=$Lang::tr{'save'}>
372 <td width='45%' align='right'>
373 END
374 ;
375 if ($outfwsettings{'POLICY'} ne 'MODE0') {
376 print <<END
377 Alle Regeln loeschen: <input type='submit' name='ACTION' value=$Lang::tr{'reset'}>
378 END
379 ;
380 }
381 print <<END
382 </table>
383 </form>
384 END
385 ;
386 &Header::closebox();
387
388 &Header::closebigbox();
389 &Header::closepage();
390
391 ############################################################################################################################
392 ############################################################################################################################
393
394 sub addrule
395 {
396 &Header::openbox('100%', 'center', 'Rules hinzufuegen');
397 if ($outfwsettings{'EDIT'} eq 'no') { $selected{'ENABLED'} = 'checked'; }
398 print <<END
399 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
400 <table width='80%'>
401 <tr><td width='20%' align='right'>Anmerkung: <img src='/blob.gif'>
402 <td width='30%' align='left'><input type='text' name='NAME' maxlength='30' value='$outfwsettings{'NAME'}'>
403 <td width='20%' align='right'>Aktiviert:
404 <td width='30%' align='left'><input type='checkbox' name='ENABLED' $selected{'ENABLED'}>
405 <tr><td width='20%' align='right'>Protokoll:
406 <td width='30%' align='left'><select name='PROT'><option value='tcp' $selected{'PROT'}{'tcp'}>TCP</option><option value='tcp&udp' $selected{'PROT'}{'tcp&udp'}>TCP & UDP</option><option value='udp' $selected{'PROT'}{'udp'}>UDP</option></select>
407 <td width='20%' align='right'>Sicherheitspolitik:
408 <td width='30%' align='left'>
409 END
410 ;
411 if ($outfwsettings{'POLICY'} eq 'MODE1'){
412 print "\t\t\tALLOW<input type='hidden' name='STATE' value='ALLOW'>\n";
413 } elsif ($outfwsettings{'POLICY'} eq 'MODE2'){
414 print "\t\t\tDENY<input type='hidden' name='STATE' value='DENY'>\n";
415 }
416 print <<END
417 <tr><td width='20%' align='right'>Quellnetz:
418 <td width='30%' align='left'><select name='SNET'>
419 <option value='all' $selected{'SNET'}{'ALL'}>alle</option>
420 <option value='ip' $selected{'SNET'}{'ip'}>Quell-IP/MAC benutzen</option>
421 <option value='green' $selected{'SNET'}{'green'}>Gruen</option>
422 END
423 ;
424 if (&Header::blue_used()){
425 print "\t\t\t<option value='blue' $selected{'SNET'}{'blue'}>Blau</option>\n";
426 }
427 if (&Header::orange_used()){
428 print "\t\t\t<option value='orange' $selected{'SNET'}{'orange'}>Orange</option>\n";
429 }
430 print <<END
431 </select>
432 <td width='20%' align='right'>Quell-IP-Adresse: <img src='/blob.gif'>
433 <td width='30%' align='left'><input type='text' name='SIP' maxlength='15' value='$outfwsettings{'SIP'}'>
434 <tr><td width='50%' colspan='2'>&nbsp;
435 <td width='20%' align='right'>Quell-MAC-Adresse: <img src='/blob.gif'>
436 <td width='30%' align='left'><input type='text' name='SMAC' maxlength='23' value='$outfwsettings{'SMAC'}'>
437 <tr><td width='20%' align='right'>Ziel-IP-Adresse: <img src='/blob.gif'>
438 <td width='30%' align='left'><input type='text' name='DIP' maxlength='15' value='$outfwsettings{'DIP'}'>
439 <td width='20%' align='right'>Ziel-Port: <img src='/blob.gif'>
440 <td width='30%' align='left'><input type='text' name='DPORT' maxlength='11' value='$outfwsettings{'DPORT'}'>
441 <tr><td colspan='4'>
442 <tr><td width='40%' align='right' colspan='2'><img src='/blob.gif'> $Lang::tr{'this field may be blank'}
443 <td width='60%' align='left' colspan='2'><input type='submit' name='ACTION' value=$Lang::tr{'add'}>
444 </table></form>
445 END
446 ;
447 &Header::closebox();
448 }
449