]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/cfgroot/geoip-functions.pl
libloc: Update to 0.9.0 (Git rev: cd022c)
[people/pmueller/ipfire-2.x.git] / config / cfgroot / geoip-functions.pl
CommitLineData
1dcd8715
SS
1#!/usr/bin/perl -w
2############################################################################
3# #
4# This file is part of the IPFire Firewall. #
5# #
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. #
10# #
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. #
15# #
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 #
19# #
20# Copyright (C) 2015 IPFire Team <info@ipfire.org>. #
21# #
22############################################################################
23
24package GeoIP;
25
8a64d10f 26use Location;
b013822b 27use Locale::Codes::Country;
1dcd8715 28
e34dbea7
SS
29# Hash which contains country codes and their names which are special or not
30# part of ISO 3166-1.
31my %not_iso_3166_location = (
32 "a1" => "Anonymous Proxy",
33 "a2" => "Satellite Provider",
8a64d10f 34 "a3" => "Worldwide Anycast Instance",
e34dbea7
SS
35 "an" => "Netherlands Antilles",
36 "ap" => "Asia/Pacific Region",
37 "eu" => "Europe",
38 "fx" => "France, Metropolitan",
39 "o1" => "Other Country",
40 "yu" => "Yugoslavia"
41);
42
8a64d10f 43# Directory where the libloc database and keyfile lives.
9b2594d8 44our $location_dir = "/usr/share/location/";
8a64d10f
SS
45
46# Libloc database file.
9b2594d8 47our $database = "$location_dir/database.db";
8a64d10f
SS
48
49# Libloc keyfile to verify the database.
9b2594d8
SS
50our $keyfile = "$location_dir/signing-key.pem";
51
52# Directory which contains the exported databases.
53our $xt_geoip_db_directory = "/usr/share/xt_geoip/";
8a64d10f
SS
54
55#
56## Tiny function to init the location database.
57#
58sub init () {
59 # Init and open the database.
60 my $db = &Location::init($database);
61
62 # Return the database handle.
63 return $db;
64}
00793c27 65
8a64d10f
SS
66#
67## Function to verify the integrity of the location database.
68#
69sub verify ($) {
70 my ($db_handle) = @_;
71
72 # Verify the integrity of the database.
73 if(&Location::verify($db_handle, $keyfile)) {
74 # Success, return "1".
75 return 1;
00793c27
MT
76 }
77
8a64d10f
SS
78 # If we got here, return nothing.
79 return;
80}
81
82#
83## Function to the the country code of a given address.
84#
85sub lookup_country_code($$) {
86 my ($db_handle, $address) = @_;
87
88 # Lookup the given address.
89 my $country_code = &Location::lookup_country_code($db_handle, $address);
90
0ca3baed 91 # Return the name of the country
8a64d10f 92 return $country_code;
00793c27
MT
93}
94
1dcd8715
SS
95# Function to get the flag icon for a specified country code.
96sub get_flag_icon($) {
97 my ($input) = @_;
98
99 # Webserver's root dir. (Required for generating full path)
100 my $webroot = "/srv/web/ipfire/html";
101
102 # Directory which contains the flag icons.
103 my $flagdir = "/images/flags";
104
105 # File extension of the country flags.
106 my $ext = "png";
107
108 # Remove whitespaces.
109 chomp($input);
110
dfbee171
SS
111 # Convert given country code to upper case.
112 my $ccode = uc($input);
1dcd8715
SS
113
114 # Generate filename, based on the contry code in lower case
115 # and the defined file extension.
116 my $file = join('.', $ccode,$ext);
117
118 # Generate path inside webroot to the previously generated file.
119 my $flag_icon = join('/', $flagdir,$file);
120
121 # Generate absolute path to the icon file.
122 my $absolute_path = join('', $webroot,$flag_icon);
123
124 # Check if the a icon file exists.
125 if (-e "$absolute_path") {
126 # Return content of flag_icon.
127 return $flag_icon;
dfbee171
SS
128 } else {
129 # If no icon for the specified country exists, try to use
130 # the icon for "unknown".
131 my $ccode = "unknown";
132
b1ad5b8b
MT
133 # Redoing all the stuff from above for the "unknown" icon.
134 my $file = join('.', $ccode, $ext);
135 my $flag_icon = join('/', $flagdir, $file);
136 my $absolute_path = join('', $webroot, $flag_icon);
dfbee171
SS
137
138 # Check if the icon is present.
139 if (-e "$absolute_path") {
140 # Return "unknown" icon.
141 return $flag_icon;
142 }
1dcd8715
SS
143 }
144}
145
146# Function to get the county name by a given country code.
147sub get_full_country_name($) {
148 my ($input) = @_;
149 my $name;
150
151 # Remove whitespaces.
152 chomp($input);
153
8a64d10f 154
1dcd8715
SS
155 # Convert input into lower case format.
156 my $code = lc($input);
157
158 # Handle country codes which are not in the list.
e34dbea7
SS
159 if ($not_iso_3166_location{$code}) {
160 # Grab location name from hash.
161 $name = $not_iso_3166_location{$code};
162 } else {
1dcd8715 163 # Use perl built-in module to get the country code.
b013822b 164 $name = &Locale::Codes::Country::code2country($code);
1dcd8715
SS
165 }
166
167 return $name;
168}
169
30c59cbb
SS
170# Function to get all available GeoIP locations.
171sub get_geoip_locations() {
abe21498 172 my @locations = ();
30c59cbb 173
e34dbea7
SS
174 # Get listed country codes from ISO 3166-1.
175 @locations = &Locale::Codes::Country::all_country_codes();
30c59cbb 176
e34dbea7
SS
177 # Add locations from not_iso_3166_locations.
178 foreach my $location (keys %not_iso_3166_location) {
179 push(@locations, $location);
30c59cbb
SS
180 }
181
30c59cbb
SS
182 # Sort locations array in alphabetical order.
183 my @sorted_locations = sort(@locations);
184
185 # Return the array..
186 return @sorted_locations;
187}
188
e758c763
SS
189# Function to flush all exported GeoIP locations.
190sub flush_exported_locations () {
191 # Check if the xt_geoip_db_directory exists.
192 if (-e $xt_geoip_db_directory) {
193 # Perform a direcory listing.
194 opendir (DIR, $xt_geoip_db_directory) or die "Could not open $xt_geoip_db_directory. $!\n";
195
196 # Loop through the files.
197 while (my $file = readdir(DIR)) {
198 # Check if the element is a file.
199 if (-f "$xt_geoip_db_directory/$file") {
200 # Delete it.
201 unlink("$xt_geoip_db_directory/$file");
202 }
203 }
204 }
205}
206
207# Function which calls location-exporter to export a given array
208# of locations.
209sub export_locations (\@) {
210 my @locations = @{ shift() };
211
212 # String to store the given locations and pass it to the exporter tool.
213 my $locations_string;
214
215 # Only export IPv4 addresses.
216 my $family = "--family=ipv4";
217
218 # Specify xt_geoip as output format.
219 my $format = "--format=xt_geoip";
220
221 # Location export command.
222 my @command = ("/usr/bin/location-exporter", "--directory=$xt_geoip_db_directory", "$format", "$family");
223
224 # Check if the export directory exists, otherwise create it.
225 unless (-d $xt_geoip_db_directory) { mkdir $xt_geoip_db_directory };
226
227 # Loop through the array of locations which needs to be exported.
228 foreach my $location (@locations) {
229 # Add location to the command array.
230 push(@command, $location);
231 }
232
233 # Execute location-exporter to export the requested country codes.
234 system(@command) == 0
235 or die "@command failed: $?";
236}
30c59cbb 237
1dcd8715 2381;