Added the ability to edit existing firewall groups and persist changes.
[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 "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 'editipgroup')
82 {
83         &editipgroup();
84 } elsif ($outgrpsettings{'ACTION'} eq 'deleteipgroup' ) {
85         unlink("$configpath/ipgroups/$outgrpsettings{'ipgroup'}");
86 } elsif ($outgrpsettings{'ACTION'} eq 'addipgroup') {
87
88         if ( -e "$configpath/macgroups/$outgrpsettings{'ipgroup'}" ){
89                 $errormessage =  "$Lang::tr{'outgoing firewall group error'}";
90         } elsif ( $outgrpsettings{'ipgroup'} eq "all" || $outgrpsettings{'ipgroup'} eq "red" || $outgrpsettings{'ipgroup'} eq "blue" ||
91                         $outgrpsettings{'ipgroup'} eq "green" || $outgrpsettings{'ipgroup'} eq "orange" || $outgrpsettings{'ipgroup'} eq "ip" ||
92                         $outgrpsettings{'ipgroup'} eq "mac" || $outgrpsettings{'ipgroup'} eq "ovpn" || $outgrpsettings{'ipgroup'} eq "ipsec" ) {
93                 $errormessage =  "$Lang::tr{'outgoing firewall reserved groupname'}";
94         } else {
95                 open (FILE, ">$configpath/ipgroups/$outgrpsettings{'ipgroup'}") or die "Can't save $outgrpsettings{'ipgroup'} settings $!";
96                 $outgrpsettings{'ipgroupcontent'} =~ s/\s*$//;
97                 flock (FILE, 2);
98                 print FILE $outgrpsettings{'ipgroupcontent'}."\n";
99                 close FILE;
100         }
101 }
102
103 if ($outgrpsettings{'ACTION'} eq 'newmacgroup')
104 {
105         &newmacgroup();
106 }elsif ($outgrpsettings{'ACTION'} eq 'editmacgroup')
107 {
108         &editmacgroup();
109 }elsif ($outgrpsettings{'ACTION'} eq 'deletemacgroup' ) {
110         unlink("$configpath/macgroups/$outgrpsettings{'macgroup'}");
111 } elsif ($outgrpsettings{'ACTION'} eq 'addmacgroup') {
112
113         if ( -e "$configpath/ipgroups/$outgrpsettings{'macgroup'}" ){
114                 $errormessage =  "$Lang::tr{'outgoing firewall group error'}";
115         } elsif ( $outgrpsettings{'macgroup'} eq "all" || $outgrpsettings{'macgroup'} eq "red" || $outgrpsettings{'macgroup'} eq "blue" ||
116                         $outgrpsettings{'macgroup'} eq "green" || $outgrpsettings{'macgroup'} eq "orange" || $outgrpsettings{'macgroup'} eq "ip" ||
117                         $outgrpsettings{'macgroup'} eq "mac" || $outgrpsettings{'macgroup'} eq "ovpn" || $outgrpsettings{'macgroup'} eq "ipsec" ) {
118                 $errormessage =  "$Lang::tr{'outgoing firewall reserved groupname'}";
119         } else {
120                 open (FILE, ">$configpath/macgroups/$outgrpsettings{'macgroup'}") or die "Can't save $outgrpsettings{'macgroup'} settings $!";
121                 $outgrpsettings{'macgroupcontent'} =~ s/\s*$//;
122                 flock (FILE, 2);
123                 print FILE $outgrpsettings{'macgroupcontent'}."\n";
124                 close FILE;
125         }
126 }
127
128 if ($errormessage)
129 {
130         &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
131         print "<class name='base'><font color=red>$errormessage\n</font>";
132         print "&nbsp;</class>\n";
133         &Header::closebox();
134 }
135
136 ############################################################################################################################
137 ############################################################################################################################
138
139 my @ipgroups = qx(ls $configpath/ipgroups/);
140 if ($outgrpsettings{'ipgroup'} eq "none" and $#ipgroups >= 0 ){ $outgrpsettings{'ipgroup'} = $ipgroups[0];}
141
142 my $ipgroupcontent = `cat $configpath/ipgroups/$outgrpsettings{'ipgroup'} 2>/dev/null`;
143 $ipgroupcontent =~ s/\n/<br \/>/g;
144
145 &Header::openbox('100%', 'center', $Lang::tr{'outgoing firewall ip groups'});
146
147 print <<END
148 <a name="outgoing showipgroup"></a>
149 <br />
150 <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showipgroup'>
151 <table width='95%' cellspacing='0'>
152         <tr>
153                 <td bgcolor='$color{'color20'}' colspan='3' align='left'><b>$Lang::tr{'outgoing firewall ip groups'}</b></td>
154         </tr>
155         <tr>
156                 <td colspan='3'  align='left'><br /></td>
157         </tr>
158         <tr>
159                 <td  align='left' colspan='2'><select name='ipgroup' style="width: 200px">
160 END
161 ;
162 foreach my $member (@ipgroups) {chomp $member;print"                    <option value='$member' $selected{'ipgroup'}{$member}>$member</option>\n";}
163 print <<END
164                 </select></td>
165                 <td  align='left'>
166                                 <input type='hidden' name='ACTION' value='showipgroup' />
167                                 <input type='image' alt='$Lang::tr{'outgoing firewall view group'}' title='$Lang::tr{'outgoing firewall view group'}' src='/images/format-justify-fill.png' />
168                 </td>
169         </tr>
170         <tr>
171                 <td colspan='3' align='left'><br /></td>
172         </tr>
173         <tr>
174                 <td colspan='3' align='left'><font size=1>$ipgroupcontent</font></td>
175         </tr>
176         <tr>
177                 <td colspan='3' align='left'><br /></td>
178         </tr>
179         <tr>
180                 <td colspan='3' align='center'><font size=1>$Lang::tr{'outgoing firewall ip groups'} - $outgrpsettings{'ipgroup'}</font></td>
181         </tr>
182 </table>
183 </form>
184 <table width='10%' cellspacing='0'>
185 <tr>
186         <td align='center'>
187                 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
188                         <input type='hidden' name='ACTION' value='editipgroup' />
189                         <input type='hidden' name='ipgroup' value='$outgrpsettings{'ipgroup'}' />
190                         <input type='image' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' src='/images/edit.gif' />
191                 </form>
192         </td>
193         <td align='center'>
194                 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
195                         <input type='hidden' name='ACTION' value='newipgroup' />
196                         <input type='image' alt='$Lang::tr{'new'}' title='$Lang::tr{'new'}' src='/images/list-add.png' />
197                 </form>
198         </td>
199         <td align='center'>
200                 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
201                         <input type='hidden' name='ACTION' value='saveipgroup' />
202                         <input type='hidden' name='ipgroup' value='$outgrpsettings{'ipgroup'}' />
203                         <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' />
204                 </form>
205         </td>
206         <td align='center'>
207                 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
208                         <input type='hidden' name='ACTION' value='deleteipgroup' />
209                         <input type='hidden' name='ipgroup' value='$outgrpsettings{'ipgroup'}' />
210                         <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
211                 </form>
212         </td>
213 </tr>
214 </table>
215
216 END
217 ;
218 &Header::closebox();
219
220 ############################################################################################################################
221 ############################################################################################################################
222
223 my @macgroups = qx(ls $configpath/macgroups/);
224 if ($outgrpsettings{'macgroup'} eq "none" and $#macgroups >= 0 ){ $outgrpsettings{'macgroup'} = $macgroups[0];}
225
226 my $macgroupcontent = `cat $configpath/macgroups/$outgrpsettings{'macgroup'} 2>/dev/null`;
227 $macgroupcontent =~ s/\n/<br \/>/g;
228
229 &Header::openbox('100%', 'center', $Lang::tr{'outgoing firewall mac groups'});
230
231 print <<END
232 <a name="outgoing showmacgroup"></a>
233 <br />
234 <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showmacgroup'>
235 <table width='95%' cellspacing='0'>
236         <tr>
237                 <td bgcolor='$color{'color20'}' colspan='3' align='left'><b>$Lang::tr{'outgoing firewall mac groups'}</b></td>
238         </tr>
239         <tr>
240                 <td colspan='3'  align='left'><br /></td>
241         </tr>
242         <tr>
243                 <td  align='left' colspan='2'><select name='macgroup' style="width: 200px">
244 END
245 ;
246 foreach my $member (@macgroups) {chomp $member;print"                   <option value='$member' $selected{'macgroup'}{$member}>$member</option>\n";}
247 print <<END
248                 </select></td>
249                 <td  align='left'>
250                                 <input type='hidden' name='ACTION' value='showmacgroup' />
251                                 <input type='image' alt='$Lang::tr{'outgoing firewall view group'}' title='$Lang::tr{'outgoing firewall view group'}' src='/images/format-justify-fill.png' />
252                 </td>
253         </tr>
254         <tr>
255                 <td colspan='3' align='left'><br /></td>
256         </tr>
257         <tr>
258                 <td colspan='3' align='left'><font size=1>$macgroupcontent</font></td>
259         </tr>
260         <tr>
261                 <td colspan='3' align='left'><br /></td>
262         </tr>
263         <tr>
264                 <td colspan='3' align='center'><font size=1>$Lang::tr{'outgoing firewall mac groups'} - $outgrpsettings{'macgroup'}</font></td>
265         </tr>
266 </table>
267 </form>
268 <table width='10%' cellspacing='0'>
269 <tr>
270         <td align='center'>
271                 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
272                         <input type='hidden' name='ACTION' value='editmacgroup' />
273                         <input type='hidden' name='macgroup' value='$outgrpsettings{'macgroup'}' />
274                         <input type='image' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' src='/images/edit.gif' />
275                 </form>
276         </td>
277         <td align='center'>
278                 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
279                         <input type='hidden' name='ACTION' value='newmacgroup' />
280                         <input type='image' alt='$Lang::tr{'new'}' title='$Lang::tr{'new'}' src='/images/list-add.png' />
281                 </form>
282         </td>
283         <td align='center'>
284                 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
285                         <input type='hidden' name='ACTION' value='savemacgroup' />
286                         <input type='hidden' name='macgroup' value='$outgrpsettings{'macgroup'}' />
287                         <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' />
288                 </form>
289         </td>
290         <td align='center'>
291                 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
292                         <input type='hidden' name='ACTION' value='deletemacgroup' />
293                         <input type='hidden' name='macgroup' value='$outgrpsettings{'macgroup'}' />
294                         <input type='image' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' src='/images/user-trash.png' />
295                 </form>
296         </td>
297 </tr>
298 </table>
299
300 END
301 ;
302 &Header::closebox();
303
304 &Header::closebigbox();
305 &Header::closepage();
306
307
308 ############################################################################################################################
309 ############################################################################################################################
310
311
312 sub newipgroup
313 {
314         &Header::openbox('100%', 'center', $Lang::tr{'outgoing firewall add ip group'});
315
316 print <<END
317         <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showipgroup'>
318         <table width='95%' cellspacing='0'>
319                 <tr>
320                         <td bgcolor='$color{'color20'}' colspan='3' align='left'><b>$Lang::tr{'outgoing firewall add ip group'}</b></td>
321                 </tr>
322                 <tr>
323                         <td colspan='3'  align='left'><br /></td>
324                 </tr>
325                 <tr>
326                         <td  align='left' colspan='2'>
327                                 <input type='text' name='ipgroup' value='newipgroup' size="30" />
328                         </td>
329                         <td  align='left'>
330                                 <input type='hidden' name='ACTION' value='addipgroup' />
331                                 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' />
332                         </td>
333                 </tr>
334                 <tr>
335                         <td  align='left' colspan='3'>
336                                 <textarea name="ipgroupcontent" cols="40" 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>
337                         </td>
338                 </tr>
339         </table>
340         </form>
341 END
342 ;
343         &Header::closebox();
344         &Header::closebigbox();
345         &Header::closepage();
346         exit 0;
347 }
348
349 sub editipgroup
350 {
351 my $ipgroupcontent = `cat $configpath/ipgroups/$outgrpsettings{'ipgroup'} 2>/dev/null`;
352
353         &Header::openbox('100%', 'center', $Lang::tr{'outgoing firewall edit ip group'});
354
355 print <<END
356         <form method='post' action='$ENV{'SCRIPT_NAME'}'>
357         <table width='95%' cellspacing='0'>
358                 <tr>
359                         <td bgcolor='$color{'color20'}' colspan='3' align='left'><b>$Lang::tr{'outgoing firewall edit ip group'}</b></td>
360                 </tr>
361                 <tr>
362                         <td colspan='3'  align='left'><br /></td>
363                 </tr>
364                 <tr>
365                         <td  align='left' colspan='2'>
366                                 <input type='text' name='ipgroup' value='$outgrpsettings{'ipgroup'}' size='30' readonly style='color:$color{'color20'}'/>
367                         </td>
368                         <td  align='left'>
369                                 <input type='hidden' name='ACTION' value='addipgroup' />
370                                 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' />
371                         </td>
372                 </tr>
373                 <tr>
374                         <td  align='left' colspan='3'>
375                                 <textarea name="ipgroupcontent" cols="40" rows="5" Wrap="off">$ipgroupcontent</textarea>
376                         </td>
377                 </tr>
378         </table>
379         </form>
380 END
381 ;
382         &Header::closebox();
383         &Header::closebigbox();
384         &Header::closepage();
385         exit 0;
386 }
387
388 sub newmacgroup
389 {
390         &Header::openbox('100%', 'center', $Lang::tr{'outgoing firewall add mac group'});
391
392 print <<END
393         <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing showmacgroup'>
394         <table width='95%' cellspacing='0'>
395                 <tr>
396                         <td bgcolor='$color{'color20'}' colspan='3' align='left'><b>$Lang::tr{'outgoing firewall add mac group'}</b></td>
397                 </tr>
398                 <tr>
399                         <td colspan='3'  align='left'><br /></td>
400                 </tr>
401                 <tr>
402                         <td  align='left' colspan='2'>
403                                 <input type='text' name='macgroup' value='newmacgroup' size="30" />
404                         </td>
405                         <td  align='left'>
406                                 <input type='hidden' name='ACTION' value='addmacgroup' />
407                                 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' />
408                         </td>
409                 </tr>
410                 <tr>
411                         <td  align='left' colspan='3'>
412                                 <textarea name="macgroupcontent" cols="20" rows="5" Wrap="off">00:24:F6:04:5F:2b\n14:26:36:5A:5F:2B\n</textarea>
413                         </td>
414                 </tr>
415         </table>
416         </form>
417 END
418 ;
419         &Header::closebox();
420         &Header::closebigbox();
421         &Header::closepage();
422         exit 0;
423 }
424
425 sub editmacgroup
426 {
427 my $macgroupcontent = `cat $configpath/macgroups/$outgrpsettings{'macgroup'} 2>/dev/null`;
428
429         &Header::openbox('100%', 'center', $Lang::tr{'outgoing firewall edit mac group'});
430
431 print <<END
432         <form method='post' action='$ENV{'SCRIPT_NAME'}#outgoing editmacgroup'>
433         <table width='95%' cellspacing='0'>
434                 <tr>
435                         <td bgcolor='$color{'color20'}' colspan='3' align='left'><b>$Lang::tr{'outgoing firewall edit mac group'}</b></td>
436                 </tr>
437                 <tr>
438                         <td colspan='3'  align='left'><br /></td>
439                 </tr>
440                 <tr>
441                         <td  align='left' colspan='2'>
442                                 <input type='text' name='macgroup' value='$outgrpsettings{'macgroup'}' size='30' readonly style='color:$color{'color20'}'/>
443                         </td>
444                         <td  align='left'>
445                                 <input type='hidden' name='ACTION' value='addmacgroup' />
446                                 <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' />
447                         </td>
448                 </tr>
449                 <tr>
450                         <td  align='left' colspan='3'>
451                                 <textarea name="macgroupcontent" cols="20" rows="5" Wrap="off">$macgroupcontent</textarea>
452                         </td>
453                 </tr>
454         </table>
455         </form>
456 END
457 ;
458         &Header::closebox();
459         &Header::closebigbox();
460         &Header::closepage();
461         exit 0;
462 }