geoip-block.cgi: New CGI for managing geoip blocking.
[ipfire-2.x.git] / html / cgi-bin / geoip-block.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 #                                                                             #
4 # IPFire.org - A linux based firewall                                         #
5 # Copyright (C) 2014 IPFire Developemnt 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}/lang.pl";
29 require "${General::swroot}/header.pl";
30
31 # Directory which contains flag icons.
32 my $flagdir = "/srv/web/ipfire/html/images/flags";
33 # File extension of the country flags.
34 my $extension = "png";
35
36 my $settingsfile = "${General::swroot}/firewall/geoipblock";
37
38 my %color = ();
39 my %mainsettings = ();
40 my %settings = ();
41 my %cgiparams = ();
42
43 # Read configuration file.
44 &General::readhash("$settingsfile", \%settings);
45
46 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
47 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
48
49 &Header::showhttpheaders();
50
51 #Get GUI values
52 &Header::getcgihash(\%cgiparams);
53
54 # Call subfunction to get all available locations.
55 my @locations = &get_geoip_locations();
56
57 if ($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
58         # Check if we want to disable geoipblock.
59         if (exists $cgiparams{'GEOIPBLOCK_ENABLED'}) {
60                 $settings{'GEOIPBLOCK_ENABLED'} = "on";
61         } else {
62                 $settings{'GEOIPBLOCK_ENABLED'} = "off";
63         }
64
65         # Loop through our locations array to prevent from
66         # non existing countries or code.
67         foreach my $cn (@locations) {
68                 # Check if blocking for this country should be enabled/disabled.
69                 if (exists $cgiparams{$cn}) {
70                         $settings{$cn} = "on";
71                 } else {
72                         $settings{$cn} = "off";
73                 }
74         }
75
76         &General::writehash("$settingsfile", \%settings);
77
78 #       &General::firewall_config_changed();
79 #
80 #       $notice = $Lang::tr{'p2p block save notice'};
81 }
82
83 &Header::openpage($Lang::tr{'geoipblock configuration'}, 1, '');
84
85 # Checkbox pre-selection.
86 my $checked;
87 if ($settings{'GEOIPBLOCK_ENABLED'} eq "on") {
88         $checked = "checked='checked'";
89 }
90
91 # Print box to enable/disable geoipblock.
92 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
93
94 &Header::openbox('100%', 'center', $Lang::tr{'geoipblock'});
95 print <<END;
96         <table width='95%'>
97                 <tr>
98                         <td width='25%' class='base'>$Lang::tr{'geoipblock enable feature'}
99                         <td><input type='checkbox' name='GEOIPBLOCK_ENABLED' $checked></td>
100                 </tr>
101                 <tr>
102                         <td colspan='2'><br></td>
103                 </tr>
104         </table>
105
106         <hr>
107
108         <table width='95%'>
109                 <tr>
110                         <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
111                 </tr>
112         </table>
113 END
114
115 &Header::closebox();
116
117 &Header::openbox('100%', 'center', $Lang::tr{'geoipblock block countries'});
118 ### JAVA SCRIPT ###
119 print <<END;
120 <script>
121         // Function to allow checking all checkboxes at once.
122         function check_all() {
123                 \$("#countries").find(":checkbox").prop("checked", true);
124         }
125
126         function uncheck_all() {
127                 \$("#countries").find(":checkbox").prop("checked", false);
128         }
129 </script>
130
131 <table width='95%' class='tbl' id="countries">
132         <tr>
133                 <td width='5%' align='center' bgcolor='$color{'color20'}'></td>
134                 <td width='5%' align='center' bgcolor='$color{'color20'}'>
135                         <b>$Lang::tr{'flag'}</b>
136                 </td>
137                 <td width='5%' align='center' bgcolor='$color{'color20'}'>
138                         <b>$Lang::tr{'countrycode'}</b>
139                 </td>
140                 <td with='35%' align='left' bgcolor='$color{'color20'}'>
141                         <b>$Lang::tr{'country'}</b>
142                 </td>
143
144                 <td width='5%' bgcolor='$color{'color20'}'>&nbsp;</td>
145
146                 <td width='5%' align='center' bgcolor='$color{'color20'}'></td>
147                 <td width='5%' align='center' bgcolor='$color{'color20'}'>
148                         <b>$Lang::tr{'flag'}</b>
149                 </td>
150                 <td width='5%' align='center' bgcolor='$color{'color20'}'>
151                         <b>$Lang::tr{'countrycode'}</b>
152                 </td>
153                 <td with='35%' align='left' bgcolor='$color{'color20'}'>
154                         <b>$Lang::tr{'country'}</b>
155                 </td>
156         </tr>
157 END
158
159 my $lines;
160 my $lines2;
161 my $col;
162 foreach my $location (@locations) {
163         # Country code in upper case. (DE)
164         my $ccode_uc = $location;
165
166         # County code in lower case. (de)
167         my $ccode_lc = lc($location);
168
169         # Full name of the country based on the country code.
170         my $cname = &General::get_full_country_name($ccode_lc);
171
172         # Generate flag filename, based on the lower case written contry code
173         # and the defined file extension of the image files. (de.png)
174         my $flagfile = join('.', $ccode_lc,$extension);
175
176         # Generate the full path to the flagfile, based on the given path and
177         # the previously generated filename.
178         my $flagpath = join('/', $flagdir,$flagfile);
179
180         my $flag;
181         # Check if a flag for the country is available.
182         if (-e "$flagpath") {
183                 $flag="<img src='/images/flags/$flagfile' alt='$ccode_uc' title='$ccode_uc'>";
184         } else {
185                 $flag="<b>N/A</b>";
186         }
187
188         # Checkbox pre-selection.
189         my $checked;
190         if ($settings{$ccode_uc} eq "on") {
191                 $checked = "checked='checked'";
192         }
193
194         # Colour lines.
195         if ($lines % 2) {
196                 $col="bgcolor='$color{'color20'}'";
197         } else {
198                 $col="bgcolor='$color{'color22'}'";
199         }
200
201         # Grouping elements.
202         my $line_start;
203         my $line_end;
204         if ($lines2 % 2) {
205                 # Increase lines (background color by once.
206                 $lines++;
207
208                 # Add empty column in front.
209                 $line_start="<td $col>&nbsp;</td>";
210
211                 # When the line number can be diveded by "2",
212                 # we are going to close the line.
213                 $line_end="</tr>";
214         } else {
215                 # When the line number is  not divideable by "2",
216                 # we are starting a new line.
217                 $line_start="<tr>";
218                 $line_end;
219         }
220
221         print "$line_start<td align='center' $col><input type='checkbox' name='$ccode_uc' $checked></td>\n";
222         print "<td align='center' $col>$flag</td>\n";
223         print "<td align='center' $col>$ccode_uc</td>\n";
224         print "<td align='left' $col>$cname</td>$line_end\n";
225                         
226 $lines2++;
227 }
228
229 print <<END;
230 </table>
231
232 <table width='95%'>
233         <tr>
234                 <td align='right'>
235                         <a href="javascript:check_all()">$Lang::tr{'check all'}</a> /
236                         <a href="javascript:uncheck_all()">$Lang::tr{'uncheck all'}</a>
237                 </td>
238         </tr>
239         <tr>
240                 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}'></td>
241         </tr>
242 </table>
243
244 <hr>
245
246 <table width='70%'>
247         <tr>
248                 <td width='5%'><img src='/images/on.gif'></td>
249                 <td>$Lang::tr{'geoipblock country is blocked'}</td>
250                 <td width='5%'><img src='/images/off.gif'></td>
251                 <td>$Lang::tr{'geoipblock country is allowed'}</td>
252         </tr>
253 </table>
254 END
255
256 &Header::closebox();
257 print"</form>\n";
258
259 &Header::closebigbox();
260 &Header::closepage();
261
262 sub get_geoip_locations() {
263         # Path to the directory which contains the binary geoip
264         # databases.
265         my $directory="/usr/share/xt_geoip/BE";
266
267         # Array with the final contry codes list.
268         my @contry_codes;
269
270         # Open location and do a directory listing.
271         opendir(DIR, "$directory");
272         my @locations = readdir(DIR);
273         closedir(DIR);
274
275         # Loop through the directory listing, and cut of the file extensions.
276         foreach my $location (sort @locations) {        
277                 # skip . and ..
278                 next if($location =~ /^\.$/);
279                 next if($location =~ /^\.\.$/);
280
281                 # Remove whitespaces.
282                 chomp($location);
283
284                 # Cut-off file extension.
285                 my ($contry_code, $extension) = split(/\./, $location);
286
287                 # Add country code to array.
288                 push(@contry_codes, $contry_code);
289         }
290
291         return @contry_codes;
292 }