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