]> git.ipfire.org Git - ipfire-2.x.git/blob - config/cfgroot/geoip-functions.pl
geoip-functions.pl: Export variables.
[ipfire-2.x.git] / config / cfgroot / geoip-functions.pl
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
24 package GeoIP;
25
26 use Location;
27 use Locale::Codes::Country;
28
29 # Hash which contains country codes and their names which are special or not
30 # part of ISO 3166-1.
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",
37 "eu" => "Europe",
38 "fx" => "France, Metropolitan",
39 "o1" => "Other Country",
40 "yu" => "Yugoslavia"
41 );
42
43 # Directory where the libloc database and keyfile lives.
44 our $location_dir = "/usr/share/location/";
45
46 # Libloc database file.
47 our $database = "$location_dir/database.db";
48
49 # Libloc keyfile to verify the database.
50 our $keyfile = "$location_dir/signing-key.pem";
51
52 # Directory which contains the exported databases.
53 our $xt_geoip_db_directory = "/usr/share/xt_geoip/";
54
55 #
56 ## Tiny function to init the location database.
57 #
58 sub init () {
59 # Init and open the database.
60 my $db = &Location::init($database);
61
62 # Return the database handle.
63 return $db;
64 }
65
66 #
67 ## Function to verify the integrity of the location database.
68 #
69 sub 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;
76 }
77
78 # If we got here, return nothing.
79 return;
80 }
81
82 #
83 ## Function to the the country code of a given address.
84 #
85 sub 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
91 # Return the name of the country
92 return $country_code;
93 }
94
95 # Function to get the flag icon for a specified country code.
96 sub 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
111 # Convert given country code to upper case.
112 my $ccode = uc($input);
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;
128 } else {
129 # If no icon for the specified country exists, try to use
130 # the icon for "unknown".
131 my $ccode = "unknown";
132
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);
137
138 # Check if the icon is present.
139 if (-e "$absolute_path") {
140 # Return "unknown" icon.
141 return $flag_icon;
142 }
143 }
144 }
145
146 # Function to get the county name by a given country code.
147 sub get_full_country_name($) {
148 my ($input) = @_;
149 my $name;
150
151 # Remove whitespaces.
152 chomp($input);
153
154
155 # Convert input into lower case format.
156 my $code = lc($input);
157
158 # Handle country codes which are not in the list.
159 if ($not_iso_3166_location{$code}) {
160 # Grab location name from hash.
161 $name = $not_iso_3166_location{$code};
162 } else {
163 # Use perl built-in module to get the country code.
164 $name = &Locale::Codes::Country::code2country($code);
165 }
166
167 return $name;
168 }
169
170 # Function to get all available GeoIP locations.
171 sub get_geoip_locations() {
172 my @locations = ();
173
174 # Get listed country codes from ISO 3166-1.
175 @locations = &Locale::Codes::Country::all_country_codes();
176
177 # Add locations from not_iso_3166_locations.
178 foreach my $location (keys %not_iso_3166_location) {
179 push(@locations, $location);
180 }
181
182 # Sort locations array in alphabetical order.
183 my @sorted_locations = sort(@locations);
184
185 # Return the array..
186 return @sorted_locations;
187 }
188
189
190 1;