]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/location-block.cgi
suricata: Change midstream policy to "pass-flow"
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / location-block.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2020 IPFire Team <info@ipfire.org> #
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}/location-functions.pl";
29 require "${General::swroot}/lang.pl";
30 require "${General::swroot}/header.pl";
31 require "/usr/lib/firewall/firewall-lib.pl";
32
33 my $notice;
34 my $settingsfile = "${General::swroot}/firewall/locationblock";
35
36 my %color = ();
37 my %mainsettings = ();
38 my %settings = ();
39 my %cgiparams = ();
40
41 # Read configuration file.
42 &General::readhash("$settingsfile", \%settings);
43
44 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
45 &General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
46
47 &Header::showhttpheaders();
48
49 #Get GUI values
50 &Header::getcgihash(\%cgiparams);
51
52 # Call subfunction to get all available locations.
53 my @locations = &Location::Functions::get_locations();
54
55 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
56 # Check if we want to disable locationblock.
57 if (exists $cgiparams{'LOCATIONBLOCK_ENABLED'}) {
58 $settings{'LOCATIONBLOCK_ENABLED'} = "on";
59 } else {
60 $settings{'LOCATIONBLOCK_ENABLED'} = "off";
61 }
62
63 # Loop through our locations array to prevent from
64 # non existing countries or code.
65 foreach my $cn (@locations) {
66 # Check if blocking for this country should be enabled/disabled.
67 if (exists $cgiparams{$cn}) {
68 $settings{$cn} = "on";
69 } else {
70 $settings{$cn} = "off";
71 }
72 }
73
74 &General::writehash("$settingsfile", \%settings);
75
76 # Mark the firewall config as changed.
77 &General::firewall_config_changed();
78
79 # Assign reload notice.
80 $notice = $Lang::tr{'fw rules reload notice'};
81 }
82
83 &Header::openpage($Lang::tr{'locationblock configuration'}, 1, '');
84
85 # Print notice that a firewall reload is required.
86 if ($notice) {
87 &Header::openbox('100%', 'left', $Lang::tr{'notice'});
88 print "<font class='base'>$notice</font>";
89 &Header::closebox();
90 }
91
92 # Checkbox pre-selection.
93 my $checked;
94 if ($settings{'LOCATIONBLOCK_ENABLED'} eq "on") {
95 $checked = "checked='checked'";
96 }
97
98 # Print box to enable/disable locationblock.
99 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
100
101 &Header::openbox('100%', 'center', $Lang::tr{'locationblock'});
102 print <<END;
103 <table width='95%'>
104 <tr>
105 <td width='50%' class='base'>$Lang::tr{'locationblock enable feature'}
106 <td><input type='checkbox' name='LOCATIONBLOCK_ENABLED' $checked></td>
107 </tr>
108 <tr>
109 <td colspan='2'><br></td>
110 </tr>
111 </table>
112
113 <hr>
114
115 <table width='95%'>
116 <tr>
117 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
118 </tr>
119 </table>
120 END
121
122 &Header::closebox();
123
124 &Header::openbox('100%', 'center', $Lang::tr{'locationblock block countries'});
125 ### JAVA SCRIPT ###
126 print <<END;
127 <script>
128 // Function to allow checking all checkboxes at once.
129 function check_all() {
130 \$("#countries").find(":checkbox").prop("checked", true);
131 }
132
133 function uncheck_all() {
134 \$("#countries").find(":checkbox").prop("checked", false);
135 }
136 </script>
137
138 <table width='95%' class='tbl' id="countries">
139 <tr>
140 <td width='5%' align='center' bgcolor='$color{'color20'}'></td>
141 <td width='5%' align='center' bgcolor='$color{'color20'}'>
142 <b>$Lang::tr{'flag'}</b>
143 </td>
144 <td width='5%' align='center' bgcolor='$color{'color20'}'>
145 <b>$Lang::tr{'countrycode'}</b>
146 </td>
147 <td with='35%' align='left' bgcolor='$color{'color20'}'>
148 <b>$Lang::tr{'country'}</b>
149 </td>
150
151 <td width='5%' bgcolor='$color{'color20'}'>&nbsp;</td>
152
153 <td width='5%' align='center' bgcolor='$color{'color20'}'></td>
154 <td width='5%' align='center' bgcolor='$color{'color20'}'>
155 <b>$Lang::tr{'flag'}</b>
156 </td>
157 <td width='5%' align='center' bgcolor='$color{'color20'}'>
158 <b>$Lang::tr{'countrycode'}</b>
159 </td>
160 <td with='35%' align='left' bgcolor='$color{'color20'}'>
161 <b>$Lang::tr{'country'}</b>
162 </td>
163 </tr>
164 END
165
166 my $lines;
167 my $lines2;
168 my $col;
169 foreach my $location (@locations) {
170 # Country code in upper case. (DE)
171 my $ccode_uc = $location;
172
173 # County code in lower case. (de)
174 my $ccode_lc = lc($location);
175
176 # Full name of the country based on the country code.
177 my $cname = &Location::Functions::get_full_country_name($ccode_lc);
178
179 # Get flag icon for of the country.
180 my $flag_icon = &Location::Functions::get_flag_icon($ccode_uc);
181
182 my $flag;
183 # Check if a flag for the country is available.
184 if ($flag_icon) {
185 $flag="<img src='$flag_icon' alt='$ccode_uc' title='$ccode_uc'>";
186 } else {
187 $flag="<b>N/A</b>";
188 }
189
190 # Checkbox pre-selection.
191 my $checked;
192 if ($settings{$ccode_uc} eq "on") {
193 $checked = "checked='checked'";
194 }
195
196 # Colour lines.
197 if ($lines % 2) {
198 $col="bgcolor='$color{'color20'}'";
199 } else {
200 $col="bgcolor='$color{'color22'}'";
201 }
202
203 # Grouping elements.
204 my $line_start;
205 my $line_end;
206 if ($lines2 % 2) {
207 # Increase lines (background color by once.
208 $lines++;
209
210 # Add empty column in front.
211 $line_start="<td $col>&nbsp;</td>";
212
213 # When the line number can be diveded by "2",
214 # we are going to close the line.
215 $line_end="</tr>";
216 } else {
217 # When the line number is not divideable by "2",
218 # we are starting a new line.
219 $line_start="<tr>";
220 $line_end;
221 }
222
223 print "$line_start<td align='center' $col><input type='checkbox' name='$ccode_uc' $checked></td>\n";
224 print "<td align='center' $col>$flag</td>\n";
225 print "<td align='center' $col>$ccode_uc</td>\n";
226 print "<td align='left' $col>$cname</td>$line_end\n";
227
228 # Finish column when the last element in the array has passed and we have an uneven amount of items.
229 if(! ($lines2 % 2) && ($location eq $locations[-1] )) {
230 print "<td $col>&nbsp;</td>\n";
231 print "<td $col>&nbsp;</td>\n";
232 print "<td $col>&nbsp;</td>\n";
233 print "<td $col>&nbsp;</td>\n";
234 print "<td $col>&nbsp;</td></tr>\n";
235 }
236
237 $lines2++;
238 }
239
240 print <<END;
241 </table>
242
243 <table width='95%'>
244 <tr>
245 <td align='right'>
246 <a href="javascript:check_all()">$Lang::tr{'check all'}</a> /
247 <a href="javascript:uncheck_all()">$Lang::tr{'uncheck all'}</a>
248 </td>
249 </tr>
250 <tr>
251 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
252 </tr>
253 </table>
254
255 <hr>
256
257 <table width='70%'>
258 <tr>
259 <td width='5%'><img src='/images/on.gif'></td>
260 <td>$Lang::tr{'locationblock country is blocked'}</td>
261 <td width='5%'><img src='/images/off.gif'></td>
262 <td>$Lang::tr{'locationblock country is allowed'}</td>
263 </tr>
264 </table>
265 END
266
267 &Header::closebox();
268 print"</form>\n";
269
270 &Header::closebigbox();
271 &Header::closepage();