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