]>
git.ipfire.org Git - ipfire-2.x.git/blob - config/cfgroot/geoip-functions.pl
2 ############################################################################
4 # This file is part of the IPFire Firewall. #
6 # IPFire is free software; you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation; either version 2 of the License, or #
9 # (at your option) any later version. #
11 # IPFire is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with IPFire; if not, write to the Free Software #
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
20 # Copyright (C) 2015 IPFire Team <info@ipfire.org>. #
22 ############################################################################
27 use Locale
::Codes
::Country
;
29 # Hash which contains country codes and their names which are special or not
31 my %not_iso_3166_location = (
32 "a1" => "Anonymous Proxy",
33 "a2" => "Satellite Provider",
34 "a3" => "Worldwide Anycast Instance",
35 "an" => "Netherlands Antilles",
36 "ap" => "Asia/Pacific Region",
38 "fx" => "France, Metropolitan"
41 # Directory where the libloc database and keyfile lives.
42 our $location_dir = "/var/lib/location/";
44 # Libloc database file.
45 our $database = "$location_dir/database.db";
47 # Libloc keyfile to verify the database.
48 our $keyfile = "$location_dir/signing-key.pem";
50 # Directory which contains the exported databases.
51 our $xt_geoip_db_directory = "/usr/share/xt_geoip/";
54 ## Tiny function to init the location database.
57 # Init and open the database.
58 my $db = &Location
::init
($database);
60 # Return the database handle.
65 ## Function to verify the integrity of the location database.
70 # Verify the integrity of the database.
71 if(&Location
::verify
($db_handle, $keyfile)) {
72 # Success, return "1".
76 # If we got here, return nothing.
81 ## Function to the the country code of a given address.
83 sub lookup_country_code
($$) {
84 my ($db_handle, $address) = @_;
86 # Lookup the given address.
87 my $country_code = &Location
::lookup_country_code
($db_handle, $address);
89 # Return the name of the country
93 # Function to get the flag icon for a specified country code.
94 sub get_flag_icon
($) {
97 # Webserver's root dir. (Required for generating full path)
98 my $webroot = "/srv/web/ipfire/html";
100 # Directory which contains the flag icons.
101 my $flagdir = "/images/flags";
103 # File extension of the country flags.
106 # Remove whitespaces.
109 # Convert given country code to upper case.
110 my $ccode = uc($input);
112 # Generate filename, based on the contry code in lower case
113 # and the defined file extension.
114 my $file = join('.', $ccode,$ext);
116 # Generate path inside webroot to the previously generated file.
117 my $flag_icon = join('/', $flagdir,$file);
119 # Generate absolute path to the icon file.
120 my $absolute_path = join('', $webroot,$flag_icon);
122 # Check if the a icon file exists.
123 if (-e
"$absolute_path") {
124 # Return content of flag_icon.
127 # If no icon for the specified country exists, try to use
128 # the icon for "unknown".
129 my $ccode = "unknown";
131 # Redoing all the stuff from above for the "unknown" icon.
132 my $file = join('.', $ccode, $ext);
133 my $flag_icon = join('/', $flagdir, $file);
134 my $absolute_path = join('', $webroot, $flag_icon);
136 # Check if the icon is present.
137 if (-e
"$absolute_path") {
138 # Return "unknown" icon.
144 # Function to get the county name by a given country code.
145 sub get_full_country_name
($) {
149 # Remove whitespaces.
153 # Convert input into lower case format.
154 my $code = lc($input);
156 # Handle country codes which are not in the list.
157 if ($not_iso_3166_location{$code}) {
158 # Grab location name from hash.
159 $name = $not_iso_3166_location{$code};
161 # Use perl built-in module to get the country code.
162 $name = &Locale
::Codes
::Country
::code2country
($code);
168 # Function to get all available GeoIP locations.
169 sub get_geoip_locations
() {
172 # Get listed country codes from ISO 3166-1.
173 @locations = &Locale
::Codes
::Country
::all_country_codes
();
175 # Add locations from not_iso_3166_locations.
176 foreach my $location (keys %not_iso_3166_location) {
177 push(@locations, $location);
180 # Sort locations array in alphabetical order.
181 my @sorted_locations = sort(@locations);
184 return @sorted_locations;
187 # Function to get the continent code of a given country code.
188 sub get_continent_code
($$) {
189 my ($db_handle, $ccode) = @_;
191 # Omit the continent code.
192 my $continent_code = &Location
::get_continent_code
($db_handle, $ccode);
194 return $continent_code;
197 # Function to flush all exported GeoIP locations.
198 sub flush_exported_locations
() {
199 # Check if the xt_geoip_db_directory exists.
200 if (-e
$xt_geoip_db_directory) {
201 # Perform a direcory listing.
202 opendir (DIR
, $xt_geoip_db_directory) or die "Could not open $xt_geoip_db_directory. $!\n";
204 # Loop through the files.
205 while (my $file = readdir(DIR
)) {
206 # Check if the element is a file.
207 if (-f
"$xt_geoip_db_directory/$file") {
209 unlink("$xt_geoip_db_directory/$file");
215 # Function which calls location-exporter to export a given array
217 sub export_locations
(\@
) {
218 my @locations = @
{ shift() };
220 # String to store the given locations and pass it to the exporter tool.
221 my $locations_string;
223 # Only export IPv4 addresses.
224 my $family = "--family=ipv4";
226 # Specify xt_geoip as output format.
227 my $format = "--format=xt_geoip";
229 # Location export command.
230 my @command = ("/usr/bin/location-exporter", "--directory=$xt_geoip_db_directory", "$format", "$family");
232 # Check if the export directory exists, otherwise create it.
233 unless (-d
$xt_geoip_db_directory) { mkdir $xt_geoip_db_directory };
235 # Loop through the array of locations which needs to be exported.
236 foreach my $location (@locations) {
237 # Add location to the command array.
238 push(@command, $location);
241 # Execute location-exporter to export the requested country codes.
242 system(@command) == 0
243 or die "@command failed: $?";