]>
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 | # # | |
48152fae | 20 | # Copyright (C) 2015 - 2020 IPFire Team <info@ipfire.org>. # |
1dcd8715 SS |
21 | # # |
22 | ############################################################################ | |
23 | ||
48152fae | 24 | package Location::Functions; |
1dcd8715 | 25 | |
8a64d10f | 26 | use Location; |
1dcd8715 | 27 | |
e34dbea7 SS |
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 = ( | |
b868abd2 SS |
31 | "A1" => "Anonymous Proxy", |
32 | "A2" => "Satellite Provider", | |
33 | "A3" => "Worldwide Anycast Instance", | |
e34dbea7 SS |
34 | ); |
35 | ||
d443f504 SS |
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 | ||
79b564c8 SS |
43 | # Array which contains special country codes. |
44 | my @special_locations = ( "A1", "A2", "A3" ); | |
45 | ||
8a64d10f | 46 | # Directory where the libloc database and keyfile lives. |
a3afe905 | 47 | our $location_dir = "/var/lib/location/"; |
8a64d10f SS |
48 | |
49 | # Libloc database file. | |
9b2594d8 | 50 | our $database = "$location_dir/database.db"; |
8a64d10f SS |
51 | |
52 | # Libloc keyfile to verify the database. | |
9b2594d8 SS |
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/"; | |
8a64d10f SS |
57 | |
58 | # | |
59 | ## Tiny function to init the location database. | |
60 | # | |
61 | sub init () { | |
62 | # Init and open the database. | |
63 | my $db = &Location::init($database); | |
64 | ||
65 | # Return the database handle. | |
66 | return $db; | |
67 | } | |
00793c27 | 68 | |
8a64d10f SS |
69 | # |
70 | ## Function to verify the integrity of the location database. | |
71 | # | |
72 | sub verify ($) { | |
73 | my ($db_handle) = @_; | |
74 | ||
75 | # Verify the integrity of the database. | |
76 | if(&Location::verify($db_handle, $keyfile)) { | |
77 | # Success, return "1". | |
78 | return 1; | |
00793c27 MT |
79 | } |
80 | ||
8a64d10f SS |
81 | # If we got here, return nothing. |
82 | return; | |
83 | } | |
84 | ||
85 | # | |
86 | ## Function to the the country code of a given address. | |
87 | # | |
88 | sub lookup_country_code($$) { | |
89 | my ($db_handle, $address) = @_; | |
90 | ||
91 | # Lookup the given address. | |
92 | my $country_code = &Location::lookup_country_code($db_handle, $address); | |
93 | ||
0ca3baed | 94 | # Return the name of the country |
8a64d10f | 95 | return $country_code; |
00793c27 MT |
96 | } |
97 | ||
1dcd8715 SS |
98 | # Function to get the flag icon for a specified country code. |
99 | sub get_flag_icon($) { | |
100 | my ($input) = @_; | |
101 | ||
102 | # Webserver's root dir. (Required for generating full path) | |
103 | my $webroot = "/srv/web/ipfire/html"; | |
104 | ||
105 | # Directory which contains the flag icons. | |
106 | my $flagdir = "/images/flags"; | |
107 | ||
108 | # File extension of the country flags. | |
109 | my $ext = "png"; | |
110 | ||
111 | # Remove whitespaces. | |
112 | chomp($input); | |
113 | ||
dfbee171 SS |
114 | # Convert given country code to upper case. |
115 | my $ccode = uc($input); | |
1dcd8715 SS |
116 | |
117 | # Generate filename, based on the contry code in lower case | |
118 | # and the defined file extension. | |
119 | my $file = join('.', $ccode,$ext); | |
120 | ||
121 | # Generate path inside webroot to the previously generated file. | |
122 | my $flag_icon = join('/', $flagdir,$file); | |
123 | ||
124 | # Generate absolute path to the icon file. | |
125 | my $absolute_path = join('', $webroot,$flag_icon); | |
126 | ||
127 | # Check if the a icon file exists. | |
128 | if (-e "$absolute_path") { | |
129 | # Return content of flag_icon. | |
130 | return $flag_icon; | |
dfbee171 SS |
131 | } else { |
132 | # If no icon for the specified country exists, try to use | |
133 | # the icon for "unknown". | |
134 | my $ccode = "unknown"; | |
135 | ||
b1ad5b8b MT |
136 | # Redoing all the stuff from above for the "unknown" icon. |
137 | my $file = join('.', $ccode, $ext); | |
138 | my $flag_icon = join('/', $flagdir, $file); | |
139 | my $absolute_path = join('', $webroot, $flag_icon); | |
dfbee171 SS |
140 | |
141 | # Check if the icon is present. | |
142 | if (-e "$absolute_path") { | |
143 | # Return "unknown" icon. | |
144 | return $flag_icon; | |
145 | } | |
1dcd8715 SS |
146 | } |
147 | } | |
148 | ||
149 | # Function to get the county name by a given country code. | |
150 | sub get_full_country_name($) { | |
151 | my ($input) = @_; | |
152 | my $name; | |
153 | ||
154 | # Remove whitespaces. | |
155 | chomp($input); | |
156 | ||
b868abd2 SS |
157 | # Convert input into upper case format. |
158 | my $code = uc($input); | |
8a64d10f | 159 | |
b868abd2 | 160 | # Handle country codes which are special or not part of the list. |
e34dbea7 SS |
161 | if ($not_iso_3166_location{$code}) { |
162 | # Grab location name from hash. | |
163 | $name = $not_iso_3166_location{$code}; | |
164 | } else { | |
b868abd2 SS |
165 | # Init libloc database connection. |
166 | my $db_handle = &init(); | |
167 | ||
168 | # Get the country name by using the location module. | |
169 | $name = &Location::get_country_name($db_handle, $code); | |
1dcd8715 SS |
170 | } |
171 | ||
172 | return $name; | |
173 | } | |
174 | ||
48152fae SS |
175 | # Function to get all available locations. |
176 | sub get_locations() { | |
79b564c8 SS |
177 | # Create libloc database handle. |
178 | my $db_handle = &init(); | |
51b6f07c | 179 | |
79b564c8 SS |
180 | # Get locations which are stored in the location database. |
181 | my @database_locations = &Location::database_countries($db_handle); | |
51b6f07c | 182 | |
79b564c8 SS |
183 | # Merge special locations array and the database locations array. |
184 | my @locations = (@special_locations, @database_locations); | |
30c59cbb | 185 | |
30c59cbb SS |
186 | # Sort locations array in alphabetical order. |
187 | my @sorted_locations = sort(@locations); | |
188 | ||
189 | # Return the array.. | |
190 | return @sorted_locations; | |
191 | } | |
192 | ||
33975f57 SS |
193 | # Function to check if a given address has one ore more special flags. |
194 | sub address_has_flags($) { | |
d443f504 SS |
195 | my ($address) = @_; |
196 | ||
33975f57 SS |
197 | # Array to store the flags of the address. |
198 | my @flags; | |
199 | ||
d443f504 SS |
200 | # Init libloc database handle. |
201 | my $db_handle = &init(); | |
202 | ||
203 | # Loop through the hash of possible network flags. | |
204 | foreach my $flag (keys(%network_flags)) { | |
205 | # Check if the address has the current flag. | |
206 | if (&Location::lookup_network_has_flag($db_handle, $address, $flag)) { | |
207 | # The given address has the requested flag. | |
208 | # | |
209 | # Grab the mapped location code for this flag. | |
210 | $mapped_code = $network_flags{$flag}; | |
211 | ||
33975f57 SS |
212 | # Add the mapped code to the array of flags. |
213 | push(@flags, $mapped_code); | |
d443f504 SS |
214 | } |
215 | } | |
33975f57 SS |
216 | |
217 | # Sort the array of flags. | |
218 | @flags = sort(@flags); | |
219 | ||
220 | # Return the array of flags. | |
221 | return @flags; | |
d443f504 SS |
222 | } |
223 | ||
1dcd8715 | 224 | 1; |