File

plugins/mod_account_activity.lua @ 13801:a5d5fefb8b68 13.0

mod_tls: Enable Prosody's certificate checking for incoming s2s connections (fixes #1916) (thanks Damian, Zash) Various options in Prosody allow control over the behaviour of the certificate verification process For example, some deployments choose to allow falling back to traditional "dialback" authentication (XEP-0220), while others verify via DANE, hard-coded fingerprints, or other custom plugins. Implementing this flexibility requires us to override OpenSSL's default certificate verification, to allow Prosody to verify the certificate itself, apply custom policies and make decisions based on the outcome. To enable our custom logic, we have to suppress OpenSSL's default behaviour of aborting the connection with a TLS alert message. With LuaSec, this can be achieved by using the verifyext "lsec_continue" flag. We also need to use the lsec_ignore_purpose flag, because XMPP s2s uses server certificates as "client" certificates (for mutual TLS verification in outgoing s2s connections). Commit 99d2100d2918 moved these settings out of the defaults and into mod_s2s, because we only really need these changes for s2s, and they should be opt-in, rather than automatically applied to all TLS services we offer. That commit was incomplete, because it only added the flags for incoming direct TLS connections. StartTLS connections are handled by mod_tls, which was not applying the lsec_* flags. It previously worked because they were already in the defaults. This resulted in incoming s2s connections with "invalid" certificates being aborted early by OpenSSL, even if settings such as `s2s_secure_auth = false` or DANE were present in the config. Outgoing s2s connections inherit verify "none" from the defaults, which means OpenSSL will receive the cert but will not terminate the connection when it is deemed invalid. This means we don't need lsec_continue there, and we also don't need lsec_ignore_purpose (because the remote peer is a "server"). Wondering why we can't just use verify "none" for incoming s2s? It's because in that mode, OpenSSL won't request a certificate from the peer for incoming connections. Setting verify "peer" is how you ask OpenSSL to request a certificate from the client, but also what triggers its built-in verification.
author Matthew Wild <mwild1@gmail.com>
date Tue, 01 Apr 2025 17:26:56 +0100
parent 13682:0055c177a54c
line wrap: on
line source

local jid = require "prosody.util.jid";
local time = os.time;

local store = module:open_store(nil, "keyval+");

module:hook("authentication-success", function(event)
	local session = event.session;
	if session.username then
		store:set_key(session.username, "timestamp", time());
	end
end);

module:hook("resource-unbind", function(event)
	local session = event.session;
	if session.username then
		store:set_key(session.username, "timestamp", time());
	end
end);

local user_sessions = prosody.hosts[module.host].sessions;
function get_last_active(username) --luacheck: ignore 131/get_last_active
	if user_sessions[username] then
		return os.time(), true; -- Currently connected
	else
		local last_activity = store:get(username);
		if not last_activity then return nil; end
		return last_activity.timestamp;
	end
end

module:add_item("shell-command", {
	section = "user";
	section_desc = "View user activity data";
	name = "activity";
	desc = "View the last recorded user activity for an account";
	args = { { name = "jid"; type = "string" } };
	host_selector = "jid";
	handler = function(self, userjid) --luacheck: ignore 212/self
		local username = jid.prepped_split(userjid);
		local last_timestamp, is_online = get_last_active(username);
		if not last_timestamp then
			return true, "No activity";
		end

		return true, ("%s (%s)"):format(os.date("%Y-%m-%d %H:%M:%S", last_timestamp), (is_online and "online" or "offline"));
	end;
});

module:add_item("shell-command", {
	section = "user";
	section_desc = "View user activity data";
	name = "list_inactive";
	desc = "List inactive user accounts";
	args = {
		{ name = "host"; type = "string" };
		{ name = "duration"; type = "string" };
	};
	host_selector = "host";
	handler = function(self, host, duration) --luacheck: ignore 212/self
		local um = require "prosody.core.usermanager";
		local duration_sec = require "prosody.util.human.io".parse_duration(duration or "");
		if not duration_sec then
			return false, ("Invalid duration %q - try something like \"30d\""):format(duration);
		end

		local now = os.time();
		local n_inactive, n_unknown = 0, 0;

		for username in um.users(host) do
			local last_active = store:get_key(username, "timestamp");
			if not last_active then
				local created_at = um.get_account_info(username, host).created;
				if created_at and (now - created_at) > duration_sec then
					self.session.print(username, "");
					n_inactive = n_inactive + 1;
				elseif not created_at then
					n_unknown = n_unknown + 1;
				end
			elseif (now - last_active) > duration_sec then
				self.session.print(username, os.date("%Y-%m-%dT%T", last_active));
				n_inactive = n_inactive + 1;
			end
		end

		if n_unknown > 0 then
			return true, ("%d accounts inactive since %s (%d unknown)"):format(n_inactive, os.date("%Y-%m-%dT%T", now - duration_sec), n_unknown);
		end
		return true, ("%d accounts inactive since %s"):format(n_inactive, os.date("%Y-%m-%dT%T", now - duration_sec));
	end;
});

module:add_item("shell-command", {
	section = "migrate";
	section_desc = "Perform data migrations";
	name = "account_activity_lastlog2";
	desc = "Migrate account activity information from mod_lastlog2";
	args = { { name = "host"; type = "string" } };
	host_selector = "host";
	handler = function(self, host) --luacheck: ignore 212/host
		local lastlog2 = module:open_store("lastlog2", "keyval+");
		local n_updated, n_errors, n_skipped = 0, 0, 0;

		local async = require "prosody.util.async";

		local p = require "prosody.util.promise".new(function (resolve)
			local async_runner = async.runner(function ()
				local n = 0;
				for username in lastlog2:items() do
					n = n + 1;
					if n % 100 == 0 then
						self.session.print(("Processed %d..."):format(n));
						async.sleep(0);
					end
					local lastlog2_data = lastlog2:get(username);
					if lastlog2_data then
						local current_data, err = store:get(username);
						if not current_data then
							if not err then
								current_data = {};
							else
								n_errors = n_errors + 1;
							end
						end
						if current_data then
							local imported_timestamp = current_data.timestamp;
							local latest;
							for k, v in pairs(lastlog2_data) do
								if k ~= "registered" and (not latest or v.timestamp > latest) then
									latest = v.timestamp;
								end
							end
							if latest and (not imported_timestamp or imported_timestamp < latest) then
								local ok, err = store:set_key(username, "timestamp", latest);
								if ok then
									n_updated = n_updated + 1;
								else
									self.session.print(("WW: Failed to import %q: %s"):format(username, err));
									n_errors = n_errors + 1;
								end
							else
								n_skipped = n_skipped + 1;
							end
						end
					end
				end
				return resolve(("%d accounts imported, %d errors, %d skipped"):format(n_updated, n_errors, n_skipped));
			end);
			async_runner:run(true);
		end);
		return p;
	end;
});