]>
Commit | Line | Data |
---|---|---|
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 | ||
24 | package GeoIP; | |
25 | ||
8a64d10f | 26 | use Location; |
b013822b | 27 | use 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. | |
31 | my %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", | |
decef80c | 38 | "fx" => "France, Metropolitan" |
e34dbea7 SS |
39 | ); |
40 | ||
8a64d10f | 41 | # Directory where the libloc database and keyfile lives. |
a3afe905 | 42 | our $location_dir = "/var/lib/location/"; |
8a64d10f SS |
43 | |
44 | # Libloc database file. | |
9b2594d8 | 45 | our $database = "$location_dir/database.db"; |
8a64d10f SS |
46 | |
47 | # Libloc keyfile to verify the database. | |
9b2594d8 SS |
48 | our $keyfile = "$location_dir/signing-key.pem"; |
49 | ||
50 | # Directory which contains the exported databases. | |
51 | our $xt_geoip_db_directory = "/usr/share/xt_geoip/"; | |
8a64d10f SS |
52 | |
53 | # | |
54 | ## Tiny function to init the location database. | |
55 | # | |
56 | sub init () { | |
57 | # Init and open the database. | |
58 | my $db = &Location::init($database); | |
59 | ||
60 | # Return the database handle. | |
61 | return $db; | |
62 | } | |
00793c27 | 63 | |
8a64d10f SS |
64 | # |
65 | ## Function to verify the integrity of the location database. | |
66 | # | |
67 | sub verify ($) { | |
68 | my ($db_handle) = @_; | |
69 | ||
70 | # Verify the integrity of the database. | |
71 | if(&Location::verify($db_handle, $keyfile)) { | |
72 | # Success, return "1". | |
73 | return 1; | |
00793c27 MT |
74 | } |
75 | ||
8a64d10f SS |
76 | # If we got here, return nothing. |
77 | return; | |
78 | } | |
79 | ||
80 | # | |
81 | ## Function to the the country code of a given address. | |
82 | # | |
83 | sub lookup_country_code($$) { | |
84 | my ($db_handle, $address) = @_; | |
85 | ||
86 | # Lookup the given address. | |
87 | my $country_code = &Location::lookup_country_code($db_handle, $address); | |
88 | ||
0ca3baed | 89 | # Return the name of the country |
8a64d10f | 90 | return $country_code; |
00793c27 MT |
91 | } |
92 | ||
1dcd8715 SS |
93 | # Function to get the flag icon for a specified country code. |
94 | sub get_flag_icon($) { | |
95 | my ($input) = @_; | |
96 | ||
97 | # Webserver's root dir. (Required for generating full path) | |
98 | my $webroot = "/srv/web/ipfire/html"; | |
99 | ||
100 | # Directory which contains the flag icons. | |
101 | my $flagdir = "/images/flags"; | |
102 | ||
103 | # File extension of the country flags. | |
104 | my $ext = "png"; | |
105 | ||
106 | # Remove whitespaces. | |
107 | chomp($input); | |
108 | ||
dfbee171 SS |
109 | # Convert given country code to upper case. |
110 | my $ccode = uc($input); | |
1dcd8715 SS |
111 | |
112 | # Generate filename, based on the contry code in lower case | |
113 | # and the defined file extension. | |
114 | my $file = join('.', $ccode,$ext); | |
115 | ||
116 | # Generate path inside webroot to the previously generated file. | |
117 | my $flag_icon = join('/', $flagdir,$file); | |
118 | ||
119 | # Generate absolute path to the icon file. | |
120 | my $absolute_path = join('', $webroot,$flag_icon); | |
121 | ||
122 | # Check if the a icon file exists. | |
123 | if (-e "$absolute_path") { | |
124 | # Return content of flag_icon. | |
125 | return $flag_icon; | |
dfbee171 SS |
126 | } else { |
127 | # If no icon for the specified country exists, try to use | |
128 | # the icon for "unknown". | |
129 | my $ccode = "unknown"; | |
130 | ||
b1ad5b8b MT |
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); | |
dfbee171 SS |
135 | |
136 | # Check if the icon is present. | |
137 | if (-e "$absolute_path") { | |
138 | # Return "unknown" icon. | |
139 | return $flag_icon; | |
140 | } | |
1dcd8715 SS |
141 | } |
142 | } | |
143 | ||
144 | # Function to get the county name by a given country code. | |
145 | sub get_full_country_name($) { | |
146 | my ($input) = @_; | |
147 | my $name; | |
148 | ||
149 | # Remove whitespaces. | |
150 | chomp($input); | |
151 | ||
8a64d10f | 152 | |
1dcd8715 SS |
153 | # Convert input into lower case format. |
154 | my $code = lc($input); | |
155 | ||
156 | # Handle country codes which are not in the list. | |
e34dbea7 SS |
157 | if ($not_iso_3166_location{$code}) { |
158 | # Grab location name from hash. | |
159 | $name = $not_iso_3166_location{$code}; | |
160 | } else { | |
1dcd8715 | 161 | # Use perl built-in module to get the country code. |
b013822b | 162 | $name = &Locale::Codes::Country::code2country($code); |
1dcd8715 SS |
163 | } |
164 | ||
165 | return $name; | |
166 | } | |
167 | ||
30c59cbb SS |
168 | # Function to get all available GeoIP locations. |
169 | sub get_geoip_locations() { | |
abe21498 | 170 | my @locations = (); |
30c59cbb | 171 | |
e34dbea7 | 172 | # Get listed country codes from ISO 3166-1. |
51b6f07c SS |
173 | my @locations_lc = &Locale::Codes::Country::all_country_codes(); |
174 | ||
175 | # The Codes::Country module provides the country codes only in lower case. | |
176 | # So we have to loop over the array and convert them into upper case format. | |
177 | foreach my $ccode (@locations_lc) { | |
178 | # Convert the country code to uppercase. | |
179 | my $ccode_uc = uc($ccode); | |
180 | ||
181 | # Add the converted ccode to the locations array. | |
182 | push(@locations, $ccode_uc); | |
183 | } | |
30c59cbb | 184 | |
e34dbea7 SS |
185 | # Add locations from not_iso_3166_locations. |
186 | foreach my $location (keys %not_iso_3166_location) { | |
51b6f07c SS |
187 | # Convert the location into uppercase. |
188 | my $location_uc = uc($location); | |
189 | ||
190 | # Add the location to the locations array. | |
191 | push(@locations, $location_uc); | |
30c59cbb SS |
192 | } |
193 | ||
30c59cbb SS |
194 | # Sort locations array in alphabetical order. |
195 | my @sorted_locations = sort(@locations); | |
196 | ||
197 | # Return the array.. | |
198 | return @sorted_locations; | |
199 | } | |
200 | ||
1dcd8715 | 201 | 1; |