changeset 5298:12f7d8b901e0

mod_audit: Support for adding location (GeoIP) to audit events This can be more privacy-friendly than logging full IP addresses, and also more informative to a user - IP addresses don't mean much to the average person, however if they see activity from outside their expected country, they can immediately identify suspicious activity. As with IPs, this field is configurable for deployments that would like to disable it. Location is also not logged when the geoip library is not available.
author Matthew Wild <mwild1@gmail.com>
date Sat, 01 Apr 2023 13:11:53 +0100
parents 4bba2d27ffaf
children e3a3a6c86a9f
files mod_audit/mod_audit.lua
diffstat 1 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mod_audit/mod_audit.lua	Sat Apr 01 12:10:56 2023 +0200
+++ b/mod_audit/mod_audit.lua	Sat Apr 01 13:11:53 2023 +0100
@@ -7,6 +7,15 @@
 local attach_ipv4_prefix = module:get_option_number("audit_log_ipv4_prefix", nil);
 local attach_ipv6_prefix = module:get_option_number("audit_log_ipv6_prefix", nil);
 
+local have_geoip, geoip = pcall(require, "geoip.country");
+local attach_location = have_geoip and module:get_option_boolean("audit_log_location", true);
+
+local geoip4_country, geoip6_country;
+if have_geoip and attach_location then
+	geoip4_country = geoip.open(module:get_option_string("geoip_ipv4_country", "/usr/share/GeoIP/GeoIP.dat"));
+	geoip6_country = geoip.open(module:get_option_string("geoip_ipv6_country", "/usr/share/GeoIP/GeoIPv6.dat"));
+end
+
 local time_now = os.time;
 local ip = require "util.ip";
 local st = require "util.stanza";
@@ -58,6 +67,13 @@
 		end
 		stanza:text_tag("remote-ip", network or remote_ip);
 	end
+	if attach_location and session.ip then
+		local remote_ip = ip.new(session.ip);
+		local geoip_country = ip.proto == "IPv6" and geoip6_country or geoip4_country;
+		stanza:tag("location", {
+			country = geoip_country:query_by_addr(remote_ip.normal);
+		}):up();
+	end
 	if session.client_id then
 		stanza:text_tag("client", session.client_id);
 	end