]>
Commit | Line | Data |
---|---|---|
1dcd8715 | 1 | #!/usr/bin/perl -w |
7b82ca1f PM |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2007-2020 IPFire 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 2 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 | ############################################################################### | |
1dcd8715 | 21 | |
48152fae | 22 | package Location::Functions; |
1dcd8715 | 23 | |
8a64d10f | 24 | use Location; |
1dcd8715 | 25 | |
e34dbea7 SS |
26 | # Hash which contains country codes and their names which are special or not |
27 | # part of ISO 3166-1. | |
28 | my %not_iso_3166_location = ( | |
b868abd2 SS |
29 | "A1" => "Anonymous Proxy", |
30 | "A2" => "Satellite Provider", | |
31 | "A3" => "Worldwide Anycast Instance", | |
e34dbea7 SS |
32 | ); |
33 | ||
d443f504 SS |
34 | # Hash which contains possible network flags and their mapped location codes. |
35 | my %network_flags = ( | |
36 | "LOC_NETWORK_FLAG_ANONYMOUS_PROXY" => "A1", | |
37 | "LOC_NETWORK_FLAG_SATELLITE_PROVIDER" => "A2", | |
38 | "LOC_NETWORK_FLAG_ANYCAST" => "A3", | |
39 | ); | |
40 | ||
79b564c8 SS |
41 | # Array which contains special country codes. |
42 | my @special_locations = ( "A1", "A2", "A3" ); | |
43 | ||
8a64d10f | 44 | # Directory where the libloc database and keyfile lives. |
a3afe905 | 45 | our $location_dir = "/var/lib/location/"; |
8a64d10f SS |
46 | |
47 | # Libloc database file. | |
9b2594d8 | 48 | our $database = "$location_dir/database.db"; |
8a64d10f SS |
49 | |
50 | # Libloc keyfile to verify the database. | |
9b2594d8 SS |
51 | our $keyfile = "$location_dir/signing-key.pem"; |
52 | ||
53 | # Directory which contains the exported databases. | |
54 | our $xt_geoip_db_directory = "/usr/share/xt_geoip/"; | |
8a64d10f | 55 | |
b62d7e0c SS |
56 | # Create libloc database handle. |
57 | my $db_handle = &init(); | |
58 | ||
8a64d10f SS |
59 | # |
60 | ## Tiny function to init the location database. | |
61 | # | |
62 | sub init () { | |
63 | # Init and open the database. | |
64 | my $db = &Location::init($database); | |
65 | ||
66 | # Return the database handle. | |
67 | return $db; | |
68 | } | |
00793c27 | 69 | |
8a64d10f SS |
70 | # |
71 | ## Function to verify the integrity of the location database. | |
72 | # | |
73 | sub verify ($) { | |
74 | my ($db_handle) = @_; | |
75 | ||
76 | # Verify the integrity of the database. | |
77 | if(&Location::verify($db_handle, $keyfile)) { | |
78 | # Success, return "1". | |
79 | return 1; | |
00793c27 MT |
80 | } |
81 | ||
8a64d10f SS |
82 | # If we got here, return nothing. |
83 | return; | |
84 | } | |
85 | ||
86 | # | |
7b82ca1f | 87 | ## Function to get the country code of a given address. |
8a64d10f SS |
88 | # |
89 | sub lookup_country_code($$) { | |
b62d7e0c | 90 | my ($address) = @_; |
8a64d10f SS |
91 | |
92 | # Lookup the given address. | |
93 | my $country_code = &Location::lookup_country_code($db_handle, $address); | |
94 | ||
0ca3baed | 95 | # Return the name of the country |
8a64d10f | 96 | return $country_code; |
00793c27 MT |
97 | } |
98 | ||
1dcd8715 SS |
99 | # Function to get the flag icon for a specified country code. |
100 | sub get_flag_icon($) { | |
101 | my ($input) = @_; | |
102 | ||
103 | # Webserver's root dir. (Required for generating full path) | |
104 | my $webroot = "/srv/web/ipfire/html"; | |
105 | ||
106 | # Directory which contains the flag icons. | |
107 | my $flagdir = "/images/flags"; | |
108 | ||
109 | # File extension of the country flags. | |
110 | my $ext = "png"; | |
111 | ||
112 | # Remove whitespaces. | |
113 | chomp($input); | |
114 | ||
dfbee171 SS |
115 | # Convert given country code to upper case. |
116 | my $ccode = uc($input); | |
1dcd8715 SS |
117 | |
118 | # Generate filename, based on the contry code in lower case | |
119 | # and the defined file extension. | |
120 | my $file = join('.', $ccode,$ext); | |
121 | ||
122 | # Generate path inside webroot to the previously generated file. | |
123 | my $flag_icon = join('/', $flagdir,$file); | |
124 | ||
125 | # Generate absolute path to the icon file. | |
126 | my $absolute_path = join('', $webroot,$flag_icon); | |
127 | ||
128 | # Check if the a icon file exists. | |
129 | if (-e "$absolute_path") { | |
130 | # Return content of flag_icon. | |
131 | return $flag_icon; | |
dfbee171 SS |
132 | } else { |
133 | # If no icon for the specified country exists, try to use | |
134 | # the icon for "unknown". | |
135 | my $ccode = "unknown"; | |
136 | ||
b1ad5b8b MT |
137 | # Redoing all the stuff from above for the "unknown" icon. |
138 | my $file = join('.', $ccode, $ext); | |
139 | my $flag_icon = join('/', $flagdir, $file); | |
140 | my $absolute_path = join('', $webroot, $flag_icon); | |
dfbee171 SS |
141 | |
142 | # Check if the icon is present. | |
143 | if (-e "$absolute_path") { | |
144 | # Return "unknown" icon. | |
145 | return $flag_icon; | |
146 | } | |
1dcd8715 SS |
147 | } |
148 | } | |
149 | ||
150 | # Function to get the county name by a given country code. | |
151 | sub get_full_country_name($) { | |
152 | my ($input) = @_; | |
153 | my $name; | |
154 | ||
155 | # Remove whitespaces. | |
156 | chomp($input); | |
157 | ||
b868abd2 SS |
158 | # Convert input into upper case format. |
159 | my $code = uc($input); | |
8a64d10f | 160 | |
b868abd2 | 161 | # Handle country codes which are special or not part of the list. |
e34dbea7 SS |
162 | if ($not_iso_3166_location{$code}) { |
163 | # Grab location name from hash. | |
164 | $name = $not_iso_3166_location{$code}; | |
165 | } else { | |
b868abd2 SS |
166 | # Init libloc database connection. |
167 | my $db_handle = &init(); | |
168 | ||
169 | # Get the country name by using the location module. | |
170 | $name = &Location::get_country_name($db_handle, $code); | |
1dcd8715 SS |
171 | } |
172 | ||
173 | return $name; | |
174 | } | |
175 | ||
48152fae SS |
176 | # Function to get all available locations. |
177 | sub get_locations() { | |
427190d5 SS |
178 | my ($mode) = @_; |
179 | ||
180 | # Set default mode to add_special_locations. | |
181 | $mode = $mode ? $mode : "add_special_locations"; | |
182 | ||
79b564c8 | 183 | # Get locations which are stored in the location database. |
427190d5 | 184 | my @locations = &Location::database_countries($db_handle); |
51b6f07c | 185 | |
427190d5 SS |
186 | # Check if the special locations should be added. |
187 | if ($mode ne "no_special_locations") { | |
188 | # Merge special locations array and the database locations array. | |
189 | @locations = (@special_locations, @locations); | |
190 | } | |
30c59cbb | 191 | |
30c59cbb SS |
192 | # Sort locations array in alphabetical order. |
193 | my @sorted_locations = sort(@locations); | |
194 | ||
427190d5 | 195 | # Return the array. |
30c59cbb SS |
196 | return @sorted_locations; |
197 | } | |
198 | ||
5bf91fe1 SS |
199 | # Function to get the continent code of a given country code. |
200 | sub get_continent_code($) { | |
201 | my ($country_code) = @_; | |
202 | ||
203 | # Use location module to grab the continent code. | |
204 | my $continent_code = &Location::get_continent_code($db_handle, $country_code); | |
205 | ||
206 | return $continent_code; | |
207 | } | |
208 | ||
33975f57 SS |
209 | # Function to check if a given address has one ore more special flags. |
210 | sub address_has_flags($) { | |
d443f504 SS |
211 | my ($address) = @_; |
212 | ||
33975f57 SS |
213 | # Array to store the flags of the address. |
214 | my @flags; | |
215 | ||
d443f504 SS |
216 | # Loop through the hash of possible network flags. |
217 | foreach my $flag (keys(%network_flags)) { | |
218 | # Check if the address has the current flag. | |
219 | if (&Location::lookup_network_has_flag($db_handle, $address, $flag)) { | |
220 | # The given address has the requested flag. | |
221 | # | |
222 | # Grab the mapped location code for this flag. | |
223 | $mapped_code = $network_flags{$flag}; | |
224 | ||
33975f57 SS |
225 | # Add the mapped code to the array of flags. |
226 | push(@flags, $mapped_code); | |
d443f504 SS |
227 | } |
228 | } | |
33975f57 SS |
229 | |
230 | # Sort the array of flags. | |
231 | @flags = sort(@flags); | |
232 | ||
233 | # Return the array of flags. | |
234 | return @flags; | |
d443f504 SS |
235 | } |
236 | ||
7b82ca1f PM |
237 | # |
238 | ## Function to get the Autonomous System Number of a given address. | |
239 | # | |
240 | sub lookup_asn($) { | |
241 | my ($address) = @_; | |
242 | ||
243 | # Lookup the given address. | |
244 | my $asn = &Location::lookup_asn($db_handle, $address); | |
245 | ||
246 | # Return the number of the Autonomous System | |
247 | return $asn; | |
248 | } | |
249 | ||
250 | # | |
251 | ## Function to get the name of an Autonomous System. | |
252 | # | |
253 | sub get_as_name($) { | |
254 | my ($asn) = @_; | |
255 | ||
256 | # Fetch the name of this AS... | |
257 | my $as_name = &Location::get_as_name($db_handle, $asn); | |
258 | ||
259 | # Return the name of the Autonomous System | |
260 | return $as_name; | |
261 | } | |
262 | ||
f46fd078 SS |
263 | # Custom END declaration which will be executed when perl |
264 | # ends, to release the database handle to libloc. | |
265 | END { | |
266 | # Check if a database handle exists. | |
267 | if ($db_handle) { | |
268 | # Destroy libloc database handle. | |
269 | &Location::DESTROY($db_handle); | |
270 | } | |
271 | } | |
272 | ||
1dcd8715 | 273 | 1; |