Debian Codegraph @

plugins/ident/geoip

raw
1 #!perl -w
2
3 =head1 NAME
4
5 geoip - provide geographic information about mail senders.
6
7 =head1 SYNOPSIS
8
9 Use MaxMind's GeoIP databases and the Geo::IP perl module to report geographic
10 information about incoming connections.
11
12 =head1 DESCRIPTION
13
14 This plugin saves geographic information in the following connection notes:
15
16 geoip_country - 2 char country code
17 geoip_country_name - english name of country
18 geoip_continent - 2 char continent code
19 geoip_city - english name of city
20 geoip_distance - distance in kilometers
21
22 And adds entries like this to your logs:
23
24 (connect) ident::geoip: NA, US, United States, 1319 km
25 (connect) ident::geoip: AS, IN, India, 13862 km
26 (connect) ident::geoip: fail: no results
27 (connect) ident::geoip: NA, CA, Canada, 2464 km
28 (connect) ident::geoip: NA, US, United States, 2318 km
29 (connect) ident::geoip: AS, PK, Pakistan, 12578 km
30 (connect) ident::geoip: AS, TJ, Tajikistan, 11965 km
31 (connect) ident::geoip: EU, AT, Austria, 8745 km
32 (connect) ident::geoip: AS, IR, Iran, Islamic Republic of, 12180 km
33 (connect) ident::geoip: EU, BY, Belarus, 9030 km
34 (connect) ident::geoip: AS, CN, China, 11254 km
35 (connect) ident::geoip: NA, PA, Panama, 3163 km
36
37 Calculating the distance has three prerequsites:
38
39 1. The MaxMind city database (free or subscription)
40 2. The Math::Complex perl module
41 3. The IP address of this mail server (see CONFIG)
42
43 Other plugins can utilize the geographic notes to alter the
44 connection, reject, greylist, etc.
45
46 =head1 CONFIG
47
48 The following options can be appended in this plugins config/plugins entry.
49
50 =head2 distance <IP Address>
51
52 Enables geodesic distance calculation. Will calculate the distance "as the
53 crow flies" from the remote mail server. Accepts a single argument, the IP
54 address to calculate the distance from. This will typically be the public
55 IP of your mail server.
56
57 ident/geoip [ distance 192.0.1.5 ]
58
59 Default: none. (no distance calculations)
60
61 =head2 too_far <distance in km>
62
63 Assign negative karma to connections further than this many km.
64
65 Default: none
66
67 =head2 db_dir </path/to/GeoIP>
68
69 The path to the GeoIP database directory.
70
71 ident/geoip [ db_dir /etc/GeoIP ]
72
73 Default: /usr/local/share/GeoIP
74
75 =head1 LIMITATIONS
76
77 The distance calculations are more concerned with being fast than accurate.
78 The MaxMind location data is collected from whois and is of limited accuracy.
79 MaxMind offers more accurate data for a fee.
80
81 For distance calculations, the earth is considered a perfect sphere. In
82 reality, it is not. Accuracy should be within 1%.
83
84 This plugin does not update the GeoIP databases. You may want to.
85
86 =head1 CHANGES
87
88 2012-06 - Matt Simerson - added GeoIP City support, continent, distance
89
90 2012-05 - Matt Simerson - added geoip_country_name note, added tests
91
92 =head1 SEE ALSO
93
94 MaxMind: http://www.maxmind.com/
95
96 Databases: http://geolite.maxmind.com/download/geoip/database
97
98 It may become worth adding support for Geo::IPfree, which uses another
99 data source: http://software77.net/geo-ip/
100
101 =head1 ACKNOWLEDGEMENTS
102
103 Stevan Bajic, the DSPAM author, who suggested SNARE, which describes using
104 geodesic distance to determine spam probability. The research paper on SNARE
105 can be found here:
106 http://smartech.gatech.edu/bitstream/handle/1853/25135/GT-CSE-08-02.pdf
107
108 =cut
109
110 use strict;
111 use warnings;
112
113 use Qpsmtpd::Constants;
114
115 #use Geo::IP; # eval'ed in register()
116 #use Math::Trig; # eval'ed in set_distance_gc
117
118 sub register {
119 my ($self, $qp, @args) = @_;
120
121 $self->log(LOGERROR, "Bad arguments") if @args % 2;
122 $self->{_args} = {@args};
123 $self->{_args}{db_dir} ||= '/usr/local/share/GeoIP';
124
125 eval 'use Geo::IP';
126 if ($@) {
127 warn "could not load Geo::IP";
128 $self->log(LOGERROR, "could not load Geo::IP");
129 return;
130 }
131
132 # Note that opening the GeoIP DB only in register has caused problems before:
133 # https://github.com/smtpd/qpsmtpd/commit/29ea9516806e9a8ca6519fcf987dbd684793ebdd#plugins/ident/geoip
134 # Opening the DB anew for every connection is horribly inefficient.
135 # Instead, attempt to reopen upon connect if the DB connection fails.
136 $self->open_geoip_db();
137
138 $self->init_my_country_code();
139
140 $self->register_hook('connect', 'connect_handler');
141 }
142
143 sub connect_handler {
144 my $self = shift;
145
146 # reopen the DB if Geo::IP failed due to DB update
147 $self->open_geoip_db();
148
149 my $c_code = $self->set_country_code() or do {
150 $self->log(LOGINFO, "skip, no results");
151 return DECLINED;
152 };
153 $self->qp->connection->notes('geoip_country', $c_code);
154
155 my $c_name = $self->set_country_name();
156 my ($city, $continent_code, $distance) = '';
157
158 if ($self->{_my_country_code}) {
159 $continent_code = $self->set_continent($c_code);
160 $city = $self->set_city_gc();
161 $distance = $self->set_distance_gc();
162 }
163
164 my @msg_parts;
165 push @msg_parts, $continent_code
166 if $continent_code && $continent_code ne '--';
167 push @msg_parts, $c_code if $c_code;
168
169 #push @msg_parts, $c_name if $c_name;
170 push @msg_parts, $city if $city;
171 if ($distance) {
172 push @msg_parts, "\t$distance km";
173 if ($self->{_args}{too_far} && $distance > $self->{_args}{too_far}) {
174 $self->adjust_karma(-1);
175 }
176 }
177 $self->log(LOGINFO, join(", ", @msg_parts));
178
179 return DECLINED;
180 }
181
182 sub open_geoip_db {
183 my $self = shift;
184
185 # this might detect if the DB connection failed. If not, this is where
186 # to add more code to do it.
187 return if (defined $self->{_geoip_city} || defined $self->{_geoip});
188
189 # The methods for using GeoIP work differently for the City vs Country DB
190 # save the handles in different locations
191 my $db_dir = $self->{_args}{db_dir};
192 foreach my $db (qw/ GeoIPCity GeoLiteCity /) {
193 if (-f "$db_dir/$db.dat") {
194 $self->log(LOGDEBUG, "using db $db");
195 $self->{_geoip_city} = Geo::IP->open("$db_dir/$db.dat");
196 }
197 }
198
199 # can't think of a good reason to load country if city data is present
200 if (!$self->{_geoip_city}) {
201 $self->log(LOGDEBUG, "using default db");
202 $self->{_geoip} = Geo::IP->new(); # loads default Country DB
203 }
204 }
205
206 sub init_my_country_code {
207 my $self = shift;
208 my $ip = $self->{_args}{distance} or return;
209 $self->{_my_country_code} = $self->get_country_code($ip);
210 }
211
212 sub set_country_code {
213 my $self = shift;
214 return $self->get_country_code_gc() if $self->{_geoip_city};
215 my $remote_ip = $self->qp->connection->remote_ip;
216 my $code = $self->get_country_code();
217 $self->qp->connection->notes('geoip_country', $code);
218 return $code;
219 }
220
221 sub get_country_code {
222 my $self = shift;
223 my $ip = shift || $self->qp->connection->remote_ip;
224 return $self->get_country_code_gc($ip) if $self->{_geoip_city};
225 return $self->{_geoip}->country_code_by_addr($ip);
226 }
227
228 sub get_country_code_gc {
229 my $self = shift;
230 my $ip = shift || $self->qp->connection->remote_ip;
231 $self->{_geoip_record} = $self->{_geoip_city}->record_by_addr($ip)
232 or return;
233 return $self->{_geoip_record}->country_code;
234 }
235
236 sub set_country_name {
237 my $self = shift;
238 return $self->set_country_name_gc() if $self->{_geoip_city};
239 my $remote_ip = $self->qp->connection->remote_ip;
240 my $name = $self->{_geoip}->country_name_by_addr($remote_ip) or return;
241 $self->qp->connection->notes('geoip_country_name', $name);
242 return $name;
243 }
244
245 sub set_country_name_gc {
246 my $self = shift;
247 return if !$self->{_geoip_record};
248 my $remote_ip = $self->qp->connection->remote_ip;
249 my $name = $self->{_geoip_record}->country_name() or return;
250 $self->qp->connection->notes('geoip_country_name', $name);
251 return $name;
252 }
253
254 sub set_continent {
255 my $self = shift;
256 return $self->set_continent_gc() if $self->{_geoip_city};
257 my $c_code = shift or return;
258 my $continent = $self->{_geoip}->continent_code_by_country_code($c_code)
259 or return;
260 $self->qp->connection->notes('geoip_continent', $continent);
261 return $continent;
262 }
263
264 sub set_continent_gc {
265 my $self = shift;
266 return if !$self->{_geoip_record};
267 my $continent = $self->{_geoip_record}->continent_code() or return;
268 $self->qp->connection->notes('geoip_continent', $continent);
269 return $continent;
270 }
271
272 sub set_city_gc {
273 my $self = shift;
274 return if !$self->{_geoip_record};
275 my $remote_ip = $self->qp->connection->remote_ip;
276 my $city = $self->{_geoip_record}->city() or return;
277 $self->qp->connection->notes('geoip_city', $city);
278 return $city;
279 }
280
281 sub set_distance_gc {
282 my $self = shift;
283 return if !$self->{_geoip_record};
284
285 my ($self_lat, $self_lon) = $self->get_my_lat_lon() or return;
286 my ($sender_lat, $sender_lon) = $self->get_sender_lat_lon() or return;
287
288 eval 'use Math::Trig qw(great_circle_distance deg2rad)';
289 if ($@) {
290 $self->log(LOGERROR,
291 "can't calculate distance, Math::Trig not installed");
292 return;
293 }
294
295 # Notice the 90 - latitude: phi zero is at the North Pole.
296 sub NESW { deg2rad($_[0]), deg2rad(90 - $_[1]) }
297 my @me = NESW($self_lon, $self_lat);
298 my @sender = NESW($sender_lon, $sender_lat);
299 my $km = great_circle_distance(@me, @sender, 6378);
300 $km = sprintf("%.0f", $km);
301
302 $self->qp->connection->notes('geoip_distance', $km);
303
304 #$self->log( LOGINFO, "distance $km km");
305 return $km;
306 }
307
308 sub get_my_lat_lon {
309 my $self = shift;
310 return if !$self->{_geoip_city};
311
312 if ($self->{_latitude} && $self->{_longitude}) {
313 return ($self->{_latitude}, $self->{_longitude}); # cached
314 }
315
316 my $ip = $self->{_args}{distance} or return;
317 my $record = $self->{_geoip_city}->record_by_addr($ip) or do {
318 $self->log(LOGERROR, "no record for my Geo::IP location");
319 return;
320 };
321
322 $self->{_latitude} = $record->latitude();
323 $self->{_longitude} = $record->longitude();
324
325 if (!$self->{_latitude} || !$self->{_longitude}) {
326 $self->log(LOGNOTICE, "could not get my lat/lon");
327 }
328 return ($self->{_latitude}, $self->{_longitude});
329 }
330
331 sub get_sender_lat_lon {
332 my $self = shift;
333
334 my $lat = $self->{_geoip_record}->latitude();
335 my $lon = $self->{_geoip_record}->longitude();
336 if (!$lat || !$lon) {
337 $self->log(LOGNOTICE, "could not get sender lat/lon");
338 return;
339 }
340 return ($lat, $lon);
341 }
342