Readded the mac filter to the outgoing firewall. Added mac groups.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / outgoinggrp.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 #                                                                             #
4 # IPFire.org - A linux based firewall                                         #
5 # Copyright (C) 2005-2010  IPFire 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 # enable only the following on debugging purpose
24 #use warnings;
25 #use CGI::Carp 'fatalsToBrowser';
26
27 require '/var/ipfire/general-functions.pl';
28 require "${General::swroot}/lang.pl";
29 require "${General::swroot}/header.pl";
30
31 my %outgrpsettings = ();
32 my %netsettings = ();
33 my %selected= () ;
34 my $errormessage = "";
35
36 my $configpath = "/var/ipfire/outgoing/groups/";
37 my $servicefile = "/var/ipfire/outgoing/defaultservices";
38
39 my %color = ();
40 my %mainsettings = ();
41 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
42 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
43
44 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
45
46 &Header::showhttpheaders();
47
48 ### Values that have to be initialized
49 $outgrpsettings{'ACTION'} = '';
50 $outgrpsettings{'ipgroup'} = 'none';
51 $outgrpsettings{'macgroup'} = 'none';
52
53 &Header::getcgihash(\%outgrpsettings);
54 delete $outgrpsettings{'__CGI__'};delete $outgrpsettings{'x'};delete $outgrpsettings{'y'};
55
56 $selected{'ipgroup'}{$outgrpsettings{'ipgroup'}} = "selected='selected'";
57 $selected{'macgroup'}{$outgrpsettings{'macgroup'}} = "selected='selected'";
58
59 &Header::openpage($Lang::tr{'outgoing firewall groups'}, 1, '');
60 &Header::openbigbox('100%', 'left', '', $errormessage);
61
62 ###############
63 # DEBUG DEBUG
64 # &Header::openbox('100%', 'left', 'DEBUG');
65 # my $debugCount = 0;
66 # foreach my $line (sort keys %outgrpsettings) {
67 # print "$line = $outgrpsettings{$line}<br />\n";
68  # $debugCount++;
69 # }
70 # print "&nbsp;Count: $debugCount\n";
71 # &Header::closebox();
72 # DEBUG DEBUG
73 ###############
74
75 ############################################################################################################################
76 ############################################################################################################################
77
78 if ($outgrpsettings{'ACTION'} eq 'newipgroup')
79 {
80         &newipgroup();
81 } elsif ($outgrpsettings{'ACTION'} eq 'deleteipgroup' ) {
82         unlink("$configpath/ipgroups/$outgrpsettings{'ipgroup'}");
83 } elsif ($outgrpsettings{'ACTION'} eq 'addipgroup') {
84
85         if ( -e "$configpath/macgroups/$outgrpsettings{'ipgroup'}" ){
86                 $errormessage =  "$Lang::tr{'outgoing firewall group error'}";
87         } else {
88                 open (FILE, ">$configpath/ipgroups/$outgrpsettings{'ipgroup'}") or die "Can't save $outgrpsettings{'ipgroup'} settings $!";
89                 flock (FILE, 2);
90                 print FILE $outgrpsettings{'ipgroupcontent'}."\n";
91                 close FILE;
92         }
93 }
94
95 if ($outgrpsettings{'ACTION'} eq 'newmacgroup')
96 {
97         &newmacgroup();
98 } elsif ($outgrpsettings{'ACTION'} eq 'deletemacgroup' ) {
99         unlink("$configpath/macgroups/$outgrpsettings{'macgroup'}");
100 } elsif ($outgrpsettings{'ACTION'} eq 'addmacgroup') {
101
102         if ( -e "$configpath/ipgroups/$outgrpsettings{'macgroup'}" ){
103                 $errormessage =  "$Lang::tr{'outgoing firewall group error'}";
104         } else {
105                 open (FILE, ">$configpath/macgroups/$outgrpsettings{'macgroup'}") or die "Can't save $outgrpsettings{'macgroup'} settings $!";
106                 flock (FILE, 2);
107                 print FILE $outgrpsettings{'macgroupcontent'}."\n";
108                 close FILE;
109         }
110 }
111
112 if ($errormessage)
113 {
114         &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
115         print "<class name='base'><font color=red>$errormessage\n</font>";
116         print "&nbsp;</class>\n";
117         &Header::closebox();
118 }
119
120 ############################################################################################################################
121 ############################################################################################################################
122
123 my @ipgroups = qx(ls $configpath/ipgroups/);
124 if ($outgrpsettings{'ipgroup'} eq "none" and $#ipgroups >= 0 ){ $outgrpsettings{'ipgroup'} = $ipgroups[0];}
125
126 my $ipgroupcontent = `cat $configpath/ipgroups/$outgrpsettings{'ipgroup'} 2>/dev/null`;
127 $ipgroupcontent =~ s/\n/<br \/>/g;
128
129 &Header::openbox('100%', 'center', $Lang::tr{'outgoing firewall ip groups'});
130
131 print <<END
132 <a name="outgoing showipgroup"</a>
133 <br />
134 <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showipgroup'>
135 <table width='95%' cellspacing='0'>
136         <tr>
137                 <td bgcolor='$color{'color20'}' colspan='3' align='left'><b>$Lang::tr{'outgoing firewall ip groups'}</b></td>
138         </tr>
139         <tr>
140                 <td colspan='3'  align='left'><br /></td>
141         </tr>
142         <tr>
143                 <td  align='left' colspan='2'><select name='ipgroup' style="width: 200px">
144 END
145 ;
146 foreach my $member (@ipgroups) {chomp $member;print"                    <option value='$member' $selected{'ipgroup'}{$member}>$member</option>\n";}
147 print <<END
148                 </select></td>
149                 <td  align='left'>
150                                 <input type='hidden' name='ACTION' value='showipgroup' />
151                                 <input type='image' alt='$Lang::tr{'outgoing firewall view group'}' title='$Lang::tr{'outgoing firewall view group'}' src='/images/format-justify-fill.png' />
152                 </td>
153         </tr>
154         <tr>
155                 <td colspan='3' align='left'><br /></td>
156         </tr>
157         <tr>
158                 <td colspan='3' align='left'><font size=1>$ipgroupcontent</font></td>
159         </tr>
160         <tr>
161                 <td colspan='3' align='left'><br /></td>
162         </tr>
163         <tr>
164                 <td colspan='3' align='center'><font size=1>$Lang::tr{'outgoing firewall ip groups'} - $outgrpsettings{'ipgroup'}</font></td>
165         </tr>
166 </table>
167 </form>
168 <table width='10%' cellspacing='0'>
169 <tr>
170         <td align='center'>
171                 <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showipgroup'>
172                         <input type='hidden' name='ACTION' value='newipgroup' />
173                         <input type='image' alt='$Lang::tr{'new'}' title='$Lang::tr{'new'}' src='/images/list-add.png' />
174                 </form>
175         </td>
176         <td align='center'>
177                 <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showipgroup'>
178                         <input type='hidden' name='ACTION' value='saveipgroup' />
179                         <input type='hidden' name='ipgroup' value='$outgrpsettings{'ipgroup'}' />
180                         <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' />
181                 </form>
182         </td>
183         <td align='center'>
184                 <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showipgroup'>
185                         <input type='hidden' name='ACTION' value='deleteipgroup' />
186                         <input type='hidden' name='ipgroup' value='$outgrpsettings{'ipgroup'}' />
187                         <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
188                 </form>
189         </td>
190 </tr>
191 </table>
192
193 END
194 ;
195 &Header::closebox();
196
197 ############################################################################################################################
198 ############################################################################################################################
199
200 my @macgroups = qx(ls $configpath/macgroups/);
201 if ($outgrpsettings{'macgroup'} eq "none" and $#macgroups >= 0 ){ $outgrpsettings{'macgroup'} = $macgroups[0];}
202
203 my $macgroupcontent = `cat $configpath/macgroups/$outgrpsettings{'macgroup'} 2>/dev/null`;
204 $macgroupcontent =~ s/\n/<br \/>/g;
205
206 &Header::openbox('100%', 'center', $Lang::tr{'outgoing firewall mac groups'});
207
208 print <<END
209 <a name="outgoing showmacgroup"</a>
210 <br />
211 <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showmacgroup'>
212 <table width='95%' cellspacing='0'>
213         <tr>
214                 <td bgcolor='$color{'color20'}' colspan='3' align='left'><b>$Lang::tr{'outgoing firewall mac groups'}</b></td>
215         </tr>
216         <tr>
217                 <td colspan='3'  align='left'><br /></td>
218         </tr>
219         <tr>
220                 <td  align='left' colspan='2'><select name='macgroup' style="width: 200px">
221 END
222 ;
223 foreach my $member (@macgroups) {chomp $member;print"                   <option value='$member' $selected{'macgroup'}{$member}>$member</option>\n";}
224 print <<END
225                 </select></td>
226                 <td  align='left'>
227                                 <input type='hidden' name='ACTION' value='showmacgroup' />
228                                 <input type='image' alt='$Lang::tr{'outgoing firewall view group'}' title='$Lang::tr{'outgoing firewall view group'}' src='/images/format-justify-fill.png' />
229                 </td>
230         </tr>
231         <tr>
232                 <td colspan='3' align='left'><br /></td>
233         </tr>
234         <tr>
235                 <td colspan='3' align='left'><font size=1>$macgroupcontent</font></td>
236         </tr>
237         <tr>
238                 <td colspan='3' align='left'><br /></td>
239         </tr>
240         <tr>
241                 <td colspan='3' align='center'><font size=1>$Lang::tr{'outgoing firewall mac groups'} - $outgrpsettings{'macgroup'}</font></td>
242         </tr>
243 </table>
244 </form>
245 <table width='10%' cellspacing='0'>
246 <tr>
247         <td align='center'>
248                 <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showmacgroup'>
249                         <input type='hidden' name='ACTION' value='newmacgroup' />
250                         <input type='image' alt='$Lang::tr{'new'}' title='$Lang::tr{'new'}' src='/images/list-add.png' />
251                 </form>
252         </td>
253         <td align='center'>
254                 <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showmacgroup'>
255                         <input type='hidden' name='ACTION' value='savemacgroup' />
256                         <input type='hidden' name='macgroup' value='$outgrpsettings{'macgroup'}' />
257                         <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' />
258                 </form>
259         </td>
260         <td align='center'>
261                 <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showmacgroup'>
262                         <input type='hidden' name='ACTION' value='deletemacgroup' />
263                         <input type='hidden' name='macgroup' value='$outgrpsettings{'macgroup'}' />
264                         <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
265                 </form>
266         </td>
267 </tr>
268 </table>
269
270 END
271 ;
272 &Header::closebox();
273
274 &Header::closebigbox();
275 &Header::closepage();
276
277
278 ############################################################################################################################
279 ############################################################################################################################
280
281
282 sub newipgroup
283 {
284         &Header::openbox('100%', 'center', $Lang::tr{'outgoing firewall add ip group'});
285
286 print <<END
287         <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showipgroup'>
288         <table width='95%' cellspacing='0'>
289                 <tr>
290                         <td bgcolor='$color{'color20'}' colspan='3' align='left'><b>$Lang::tr{'outgoing firewall add ip group'}</b></td>
291                 </tr>
292                 <tr>
293                         <td colspan='3'  align='left'><br /></td>
294                 </tr>
295                 <tr>
296                         <td  align='left' colspan='2'>
297                                 <input type='text' name='ipgroup' value='newgroup' size="30" />
298                         </td>
299                         <td  align='left'>
300                                 <input type='hidden' name='ACTION' value='addipgroup' />
301                                 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' />
302                         </td>
303                 </tr>
304                 <tr>
305                         <td  align='left' colspan='3'>
306                                 <textarea name="ipgroupcontent" cols="20" rows="5" Wrap="off">192.168.1.0/24\n192.168.3.0/255.255.255.0\n192.168.0.1\n192.168.0.2\n</textarea>
307                         </td>
308                 </tr>
309         </table>
310         </form>
311 END
312 ;
313         &Header::closebox();
314         &Header::closebigbox();
315         &Header::closepage();
316         exit 0;
317 }
318
319 sub newmacgroup
320 {
321         &Header::openbox('100%', 'center', $Lang::tr{'outgoing firewall add mac group'});
322
323 print <<END
324         <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showmacgroup'>
325         <table width='95%' cellspacing='0'>
326                 <tr>
327                         <td bgcolor='$color{'color20'}' colspan='3' align='left'><b>$Lang::tr{'outgoing firewall add mac group'}</b></td>
328                 </tr>
329                 <tr>
330                         <td colspan='3'  align='left'><br /></td>
331                 </tr>
332                 <tr>
333                         <td  align='left' colspan='2'>
334                                 <input type='text' name='macgroup' value='newgroup' size="30" />
335                         </td>
336                         <td  align='left'>
337                                 <input type='hidden' name='ACTION' value='addmacgroup' />
338                                 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' />
339                         </td>
340                 </tr>
341                 <tr>
342                         <td  align='left' colspan='3'>
343                                 <textarea name="macgroupcontent" cols="20" rows="5" Wrap="off">00:24:F6:04:5F:2b\n14:26:36:5A:5F:2B\n</textarea>
344                         </td>
345                 </tr>
346         </table>
347         </form>
348 END
349 ;
350         &Header::closebox();
351         &Header::closebigbox();
352         &Header::closepage();
353         exit 0;
354 }