annotate mod_restrict_federation/mod_restrict_federation.lua @ 6544:981c14bb8975

mod_restrict_federation: Allow some ad-hoc commands used by push services This fixes push registration for Siskin, Conversations and Snikket Android+iOS when used by restricted accounts.
author Matthew Wild <mwild1@gmail.com>
date Thu, 21 May 2026 18:32:31 +0100
parents ba2c344e1bdd
children cfcd2d2f7119
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6325
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local jid_node = require "prosody.util.jid".node;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local st = require "prosody.util.stanza";
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local get_user_role = require "prosody.core.usermanager".get_user_role;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 function check_outgoing_stanza(event)
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local origin, stanza = event.origin, event.stanza;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if not origin or origin.type ~= "c2s" then
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 -- We only filter user-originated traffic, so
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 -- pass this through.
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 return;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 if module:may("xmpp:federate", event) then
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 -- Pass through
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 return;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
6544
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
20 if stanza.name == "iq" then
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
21 if stanza.attr.type == "result" or stanza.attr.type == "error" then
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
22 -- Allow responses to iqs
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
23 return;
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
24 end
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
25 local payload = stanza.tags[1];
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
26 -- Allow push notification registration
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
27 if payload.name == "command" and payload.attr.xmlns == "http://jabber.org/protocol/commands" then
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
28 -- This should allow Tigase and p2 ad-hoc commands to pass through to push gateways
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
29 if node:match("^register%-") or node:match("^unregister%-") then
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
30 return;
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
31 end
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
32 end
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
33 end
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
34
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
35
6325
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 -- Block
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 module:log("debug", "Forbidding outgoing %s stanza from <%s> to <%s>", stanza.name, stanza.attr.from, stanza.attr.to);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local err_reply = st.error_reply(event.stanza, "auth", "policy-violation", "Communication with remote domains is not permitted");
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 origin.send(err_reply);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 return true;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 function check_incoming_stanza(event)
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local origin, stanza = event.origin, event.stanza;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if origin.type ~= "s2sin" then
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 -- We only filter incoming traffic from remote domains
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 -- Pass through
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 return;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
6544
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
52 if stanza.name == "iq" then
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
53 if stanza.attr.type == "result" or stanza.attr.type == "error" then
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
54 -- Allow responses to iqs
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
55 return;
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
56 end
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
57 end
981c14bb8975 mod_restrict_federation: Allow some ad-hoc commands used by push services
Matthew Wild <mwild1@gmail.com>
parents: 6325
diff changeset
58
6325
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local recipient_username = jid_node(stanza.attr.to);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 if not recipient_username then
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 return;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local recipient_role, role_err = get_user_role(recipient_username, module.host);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if not recipient_role then
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 module:log("warn", "Unable to determine recipient role: %s", role_err);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 -- No idea what the role is, we'll pass it through
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 return;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 if recipient_role:may("xmpp:federate") then
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 -- Allowed, pass through
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 return;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 -- Block
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 module:log("debug", "Forbidding incoming %s stanza from <%s> to <%s>", stanza.name, stanza.attr.from, stanza.attr.to);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 local err_reply = st.error_reply(event.stanza, "cancel", "service-unavailable");
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 origin.send(err_reply);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 return true;
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 module:hook("message/bare", check_incoming_stanza, 500);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 module:hook("message/full", check_incoming_stanza, 500);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 module:hook("presence/bare", check_incoming_stanza, 500);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 module:hook("presence/full", check_incoming_stanza, 500);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 module:hook("iq/bare", check_incoming_stanza, 500);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 module:hook("iq/full", check_incoming_stanza, 500);
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
ba2c344e1bdd mod_restrict_federation: Yet another module for restricting s2s for (some) users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 module:hook("route/remote", check_outgoing_stanza, 500);