]> git.ipfire.org Git - ipfire-2.x.git/blame - config/cfgroot/location-functions.pl
suricata: Change midstream policy to "pass-flow"
[ipfire-2.x.git] / config / cfgroot / location-functions.pl
CommitLineData
1dcd8715 1#!/usr/bin/perl -w
7b82ca1f
PM
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
970e8547 5# Copyright (C) 2007-2021 IPFire Team <info@ipfire.org> #
7b82ca1f
PM
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 22package Location::Functions;
1dcd8715 23
8a64d10f 24use 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.
28my %not_iso_3166_location = (
b868abd2
SS
29 "A1" => "Anonymous Proxy",
30 "A2" => "Satellite Provider",
31 "A3" => "Worldwide Anycast Instance",
970e8547 32 "XD" => "Hostile networks safe to drop",
e34dbea7
SS
33);
34
d443f504
SS
35# Hash which contains possible network flags and their mapped location codes.
36my %network_flags = (
37 "LOC_NETWORK_FLAG_ANONYMOUS_PROXY" => "A1",
38 "LOC_NETWORK_FLAG_SATELLITE_PROVIDER" => "A2",
39 "LOC_NETWORK_FLAG_ANYCAST" => "A3",
970e8547 40 "LOC_NETWORK_FLAG_DROP" => "XD",
d443f504
SS
41);
42
79b564c8 43# Array which contains special country codes.
970e8547 44my @special_locations = ( "A1", "A2", "A3", "XD" );
79b564c8 45
8a64d10f 46# Directory where the libloc database and keyfile lives.
19e5c03f 47our $location_dir = "/var/lib/location";
8a64d10f
SS
48
49# Libloc database file.
9b2594d8 50our $database = "$location_dir/database.db";
8a64d10f
SS
51
52# Libloc keyfile to verify the database.
9b2594d8
SS
53our $keyfile = "$location_dir/signing-key.pem";
54
55# Directory which contains the exported databases.
a5f22bf0 56our $ipset_db_directory = "$location_dir/ipset";
8a64d10f 57
b62d7e0c
SS
58# Create libloc database handle.
59my $db_handle = &init();
60
8a64d10f
SS
61#
62## Tiny function to init the location database.
63#
64sub init () {
65 # Init and open the database.
66 my $db = &Location::init($database);
67
68 # Return the database handle.
69 return $db;
70}
00793c27 71
8a64d10f
SS
72#
73## Function to verify the integrity of the location database.
74#
75sub 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;
00793c27
MT
82 }
83
8a64d10f
SS
84 # If we got here, return nothing.
85 return;
86}
87
88#
7b82ca1f 89## Function to get the country code of a given address.
8a64d10f
SS
90#
91sub lookup_country_code($$) {
b62d7e0c 92 my ($address) = @_;
8a64d10f
SS
93
94 # Lookup the given address.
95 my $country_code = &Location::lookup_country_code($db_handle, $address);
96
0ca3baed 97 # Return the name of the country
8a64d10f 98 return $country_code;
00793c27
MT
99}
100
1dcd8715
SS
101# Function to get the flag icon for a specified country code.
102sub 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
dfbee171
SS
117 # Convert given country code to upper case.
118 my $ccode = uc($input);
1dcd8715
SS
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;
dfbee171
SS
134 } else {
135 # If no icon for the specified country exists, try to use
136 # the icon for "unknown".
137 my $ccode = "unknown";
138
b1ad5b8b
MT
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);
dfbee171
SS
143
144 # Check if the icon is present.
145 if (-e "$absolute_path") {
146 # Return "unknown" icon.
147 return $flag_icon;
148 }
1dcd8715
SS
149 }
150}
151
152# Function to get the county name by a given country code.
153sub get_full_country_name($) {
154 my ($input) = @_;
155 my $name;
156
157 # Remove whitespaces.
158 chomp($input);
159
b868abd2
SS
160 # Convert input into upper case format.
161 my $code = uc($input);
8a64d10f 162
b868abd2 163 # Handle country codes which are special or not part of the list.
e34dbea7
SS
164 if ($not_iso_3166_location{$code}) {
165 # Grab location name from hash.
166 $name = $not_iso_3166_location{$code};
167 } else {
b868abd2
SS
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.
176sub get_locations() {
427190d5
SS
177 my ($mode) = @_;
178
179 # Set default mode to add_special_locations.
180 $mode = $mode ? $mode : "add_special_locations";
181
79b564c8 182 # Get locations which are stored in the location database.
427190d5 183 my @locations = &Location::database_countries($db_handle);
51b6f07c 184
427190d5
SS
185 # Check if the special locations should be added.
186 if ($mode ne "no_special_locations") {
187 # Merge special locations array and the database locations array.
188 @locations = (@special_locations, @locations);
189 }
30c59cbb 190
30c59cbb
SS
191 # Sort locations array in alphabetical order.
192 my @sorted_locations = sort(@locations);
193
427190d5 194 # Return the array.
30c59cbb
SS
195 return @sorted_locations;
196}
197
5bf91fe1
SS
198# Function to get the continent code of a given country code.
199sub get_continent_code($) {
200 my ($country_code) = @_;
201
202 # Use location module to grab the continent code.
203 my $continent_code = &Location::get_continent_code($db_handle, $country_code);
204
205 return $continent_code;
206}
207
33975f57
SS
208# Function to check if a given address has one ore more special flags.
209sub address_has_flags($) {
d443f504
SS
210 my ($address) = @_;
211
33975f57
SS
212 # Array to store the flags of the address.
213 my @flags;
214
d443f504
SS
215 # Loop through the hash of possible network flags.
216 foreach my $flag (keys(%network_flags)) {
217 # Check if the address has the current flag.
218 if (&Location::lookup_network_has_flag($db_handle, $address, $flag)) {
219 # The given address has the requested flag.
220 #
221 # Grab the mapped location code for this flag.
222 $mapped_code = $network_flags{$flag};
223
33975f57
SS
224 # Add the mapped code to the array of flags.
225 push(@flags, $mapped_code);
d443f504
SS
226 }
227 }
33975f57
SS
228
229 # Sort the array of flags.
230 @flags = sort(@flags);
231
232 # Return the array of flags.
233 return @flags;
d443f504
SS
234}
235
7b82ca1f
PM
236#
237## Function to get the Autonomous System Number of a given address.
238#
239sub lookup_asn($) {
240 my ($address) = @_;
241
242 # Lookup the given address.
243 my $asn = &Location::lookup_asn($db_handle, $address);
244
245 # Return the number of the Autonomous System
246 return $asn;
247}
248
249#
250## Function to get the name of an Autonomous System.
251#
252sub get_as_name($) {
253 my ($asn) = @_;
254
255 # Fetch the name of this AS...
256 my $as_name = &Location::get_as_name($db_handle, $asn);
257
258 # Return the name of the Autonomous System
259 return $as_name;
260}
261
f46fd078
SS
262# Custom END declaration which will be executed when perl
263# ends, to release the database handle to libloc.
264END {
265 # Check if a database handle exists.
266 if ($db_handle) {
267 # Destroy libloc database handle.
268 &Location::DESTROY($db_handle);
269 }
270}
271
1dcd8715 2721;