]> git.ipfire.org Git - ipfire-2.x.git/blame - config/outgoingfw/outgoingfw.pl
Update:
[ipfire-2.x.git] / config / outgoingfw / outgoingfw.pl
CommitLineData
ebb9187c
MT
1#!/usr/bin/perl
2#
3# IPFire Scripts
4#
5# This code is distributed under the terms of the GPL
6#
7# (c) The IPFire Team
8#
9
10use strict;
11# enable only the following on debugging purpose
12#use warnings;
13
14require '/var/ipfire/general-functions.pl';
15
16my %outfwsettings = ();
17my %checked = ();
18my %selected= () ;
19my %netsettings = ();
20my $errormessage = "";
21my $configentry = "";
22my @configs = ();
23my @configline = ();
24my $p2pentry = "";
25my @p2ps = ();
26my @p2pline = ();
27my @protos = ();
28my $CMD = "";
29my $DEBUG = 0;
30
31my $configfile = "/var/ipfire/outgoing/rules";
32my $p2pfile = "/var/ipfire/outgoing/p2protocols";
33
34&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
35
36### Values that have to be initialized
37$outfwsettings{'ACTION'} = '';
38$outfwsettings{'VALID'} = 'yes';
39$outfwsettings{'EDIT'} = 'no';
40$outfwsettings{'NAME'} = '';
41$outfwsettings{'SNET'} = '';
42$outfwsettings{'SIP'} = '';
43$outfwsettings{'SPORT'} = '';
44$outfwsettings{'SMAC'} = '';
45$outfwsettings{'DIP'} = '';
46$outfwsettings{'DPORT'} = '';
47$outfwsettings{'PROT'} = '';
48$outfwsettings{'STATE'} = '';
49$outfwsettings{'DISPLAY_DIP'} = '';
50$outfwsettings{'DISPLAY_DPORT'} = '';
51$outfwsettings{'DISPLAY_SMAC'} = '';
52$outfwsettings{'DISPLAY_SIP'} = '';
53$outfwsettings{'POLICY'} = 'MODE0';
54my $SOURCE = "";
55my $DESTINATION = "";
56my $PROTO = "";
57my $DPORT = "";
58my $DEV = "";
59my $MAC = "";
60my $POLICY = "";
61my $DO = "";
62
63# read files
64&General::readhash("${General::swroot}/outgoing/settings", \%outfwsettings);
65&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
66
67open( FILE, "< $configfile" ) or die "Unable to read $configfile";
68@configs = <FILE>;
69close FILE;
70
71# Say hello!
72print "Outgoing firewall for IPFire - $outfwsettings{'POLICY'}\n";
73if ($DEBUG) { print "Debugging mode!\n"; }
74print "\n";
75
76
77if ( $outfwsettings{'POLICY'} eq 'MODE0' ) {
78 system("/sbin/iptables --flush OUTGOINGFW >/dev/null 2>&1");
79 system("/sbin/iptables --delete-chain OUTGOINGFW >/dev/null 2>&1");
80
81 exit 0
82} elsif ( $outfwsettings{'POLICY'} eq 'MODE1' ) {
83 $outfwsettings{'STATE'} = "ALLOW";
84 $POLICY = "DROP";
85 $DO = "ACCEPT";
86} elsif ( $outfwsettings{'POLICY'} eq 'MODE2' ) {
87 $outfwsettings{'STATE'} = "DENY";
88 $POLICY = "ACCEPT";
89 $DO = "DROP";
90}
91
92### Initialize IPTables
93system("/sbin/iptables --flush OUTGOINGFW >/dev/null 2>&1");
94system("/sbin/iptables --delete-chain OUTGOINGFW >/dev/null 2>&1");
95system("/sbin/iptables -N OUTGOINGFW >/dev/null 2>&1");
96
97foreach $configentry (sort @configs)
98{
99 $SOURCE = "";
100 $DESTINATION = "";
101 $PROTO = "";
102 $DPORT = "";
103 $DEV = "";
104 $MAC = "";
105 @configline = split( /\;/, $configentry );
106 if ($outfwsettings{'STATE'} eq $configline[0]) {
107 if ($configline[2] eq 'green') {
108 $SOURCE = "$netsettings{'GREEN_NETADDRESS'}/$netsettings{'GREEN_NETMASK'}";
109 $DEV = $netsettings{'GREEN_DEV'};
110 } elsif ($configline[2] eq 'blue') {
111 $SOURCE = "$netsettings{'BLUE_NETADDRESS'}/$netsettings{'BLUE_NETMASK'}";
112 $DEV = $netsettings{'BLUE_DEV'};
113 } elsif ($configline[2] eq 'orange') {
114 $SOURCE = "$netsettings{'ORANGE_NETADDRESS'}/$netsettings{'ORANGE_NETMASK'}";
115 $DEV = $netsettings{'ORANGE_DEV'};
116 } elsif ($configline[2] eq 'ip') {
117 $SOURCE = "$configline[5]";
118 $DEV = "";
119 } else {
120 $SOURCE = "0/0";
121 $DEV = "";
122 }
123
124 if ($configline[7]) { $DESTINATION = "$configline[7]"; } else { $DESTINATION = "0/0"; }
125
126 $CMD = "/sbin/iptables -A OUTGOINGFW -s $SOURCE -d $DESTINATION";
127
128 if ($configline[3] ne 'tcp&udp') {
129 $PROTO = "$configline[3]";
130 $CMD = "$CMD -p $PROTO";
131 if ($configline[8]) {
132 $DPORT = "$configline[8]";
133 $CMD = "$CMD --dport $DPORT";
134 }
135 }
136
137 if ($DEV) {
138 $CMD = "$CMD -i $DEV";
139 }
140
141 if ($configline[6]) {
142 $MAC = "$configline[6]";
143 $CMD = "$CMD -m mac --mac-source $MAC";
144 }
145
146 $CMD = "$CMD -o $netsettings{'RED_DEV'}";
147 if ($DEBUG) { print "$CMD -j $DO\n"; } else { system("$CMD -j $DO"); }
148
149 if ($configline[9] eq "log") {
150 if ($DEBUG) { print "$CMD -m state --state NEW -j LOG --log-prefix 'OUTGOINGFW '\n"; } else { system("$CMD -m state --state NEW -j LOG --log-prefix 'OUTGOINGFW '"); }
151 }
152
153 }
154}