]>
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; |
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 | ||
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 SS |
55 | |
56 | # | |
57 | ## Tiny function to init the location database. | |
58 | # | |
59 | sub init () { | |
60 | # Init and open the database. | |
61 | my $db = &Location::init($database); | |
62 | ||
63 | # Return the database handle. | |
64 | return $db; | |
65 | } | |
00793c27 | 66 | |
8a64d10f SS |
67 | # |
68 | ## Function to verify the integrity of the location database. | |
69 | # | |
70 | sub verify ($) { | |
71 | my ($db_handle) = @_; | |
72 | ||
73 | # Verify the integrity of the database. | |
74 | if(&Location::verify($db_handle, $keyfile)) { | |
75 | # Success, return "1". | |
76 | return 1; | |
00793c27 MT |
77 | } |
78 | ||
8a64d10f SS |
79 | # If we got here, return nothing. |
80 | return; | |
81 | } | |
82 | ||
83 | # | |
84 | ## Function to the the country code of a given address. | |
85 | # | |
86 | sub lookup_country_code($$) { | |
87 | my ($db_handle, $address) = @_; | |
88 | ||
89 | # Lookup the given address. | |
90 | my $country_code = &Location::lookup_country_code($db_handle, $address); | |
91 | ||
0ca3baed | 92 | # Return the name of the country |
8a64d10f | 93 | return $country_code; |
00793c27 MT |
94 | } |
95 | ||
1dcd8715 SS |
96 | # Function to get the flag icon for a specified country code. |
97 | sub get_flag_icon($) { | |
98 | my ($input) = @_; | |
99 | ||
100 | # Webserver's root dir. (Required for generating full path) | |
101 | my $webroot = "/srv/web/ipfire/html"; | |
102 | ||
103 | # Directory which contains the flag icons. | |
104 | my $flagdir = "/images/flags"; | |
105 | ||
106 | # File extension of the country flags. | |
107 | my $ext = "png"; | |
108 | ||
109 | # Remove whitespaces. | |
110 | chomp($input); | |
111 | ||
dfbee171 SS |
112 | # Convert given country code to upper case. |
113 | my $ccode = uc($input); | |
1dcd8715 SS |
114 | |
115 | # Generate filename, based on the contry code in lower case | |
116 | # and the defined file extension. | |
117 | my $file = join('.', $ccode,$ext); | |
118 | ||
119 | # Generate path inside webroot to the previously generated file. | |
120 | my $flag_icon = join('/', $flagdir,$file); | |
121 | ||
122 | # Generate absolute path to the icon file. | |
123 | my $absolute_path = join('', $webroot,$flag_icon); | |
124 | ||
125 | # Check if the a icon file exists. | |
126 | if (-e "$absolute_path") { | |
127 | # Return content of flag_icon. | |
128 | return $flag_icon; | |
dfbee171 SS |
129 | } else { |
130 | # If no icon for the specified country exists, try to use | |
131 | # the icon for "unknown". | |
132 | my $ccode = "unknown"; | |
133 | ||
b1ad5b8b MT |
134 | # Redoing all the stuff from above for the "unknown" icon. |
135 | my $file = join('.', $ccode, $ext); | |
136 | my $flag_icon = join('/', $flagdir, $file); | |
137 | my $absolute_path = join('', $webroot, $flag_icon); | |
dfbee171 SS |
138 | |
139 | # Check if the icon is present. | |
140 | if (-e "$absolute_path") { | |
141 | # Return "unknown" icon. | |
142 | return $flag_icon; | |
143 | } | |
1dcd8715 SS |
144 | } |
145 | } | |
146 | ||
147 | # Function to get the county name by a given country code. | |
148 | sub get_full_country_name($) { | |
149 | my ($input) = @_; | |
150 | my $name; | |
151 | ||
152 | # Remove whitespaces. | |
153 | chomp($input); | |
154 | ||
8a64d10f | 155 | |
1dcd8715 SS |
156 | # Convert input into lower case format. |
157 | my $code = lc($input); | |
158 | ||
159 | # Handle country codes which are not in the list. | |
e34dbea7 SS |
160 | if ($not_iso_3166_location{$code}) { |
161 | # Grab location name from hash. | |
162 | $name = $not_iso_3166_location{$code}; | |
163 | } else { | |
1dcd8715 | 164 | # Use perl built-in module to get the country code. |
b013822b | 165 | $name = &Locale::Codes::Country::code2country($code); |
1dcd8715 SS |
166 | } |
167 | ||
168 | return $name; | |
169 | } | |
170 | ||
48152fae SS |
171 | # Function to get all available locations. |
172 | sub get_locations() { | |
79b564c8 SS |
173 | # Create libloc database handle. |
174 | my $db_handle = &init(); | |
51b6f07c | 175 | |
79b564c8 SS |
176 | # Get locations which are stored in the location database. |
177 | my @database_locations = &Location::database_countries($db_handle); | |
51b6f07c | 178 | |
79b564c8 SS |
179 | # Merge special locations array and the database locations array. |
180 | my @locations = (@special_locations, @database_locations); | |
30c59cbb | 181 | |
30c59cbb SS |
182 | # Sort locations array in alphabetical order. |
183 | my @sorted_locations = sort(@locations); | |
184 | ||
185 | # Return the array.. | |
186 | return @sorted_locations; | |
187 | } | |
188 | ||
1dcd8715 | 189 | 1; |