2 -- The code in this file should be self-explanatory, though the logic is horrible
3 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
4 -- the rules from the RFCs (mainly 3921)
6 require "core.servermanager"
8 local log = require "util.logger".init("stanzarouter")
10 local st = require "util.stanza";
11 local send_s2s = require "core.s2smanager".send_to_host;
12 local user_exists = require "core.usermanager".user_exists;
14 local rostermanager = require "core.rostermanager";
15 local sessionmanager = require "core.sessionmanager";
17 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
18 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
19 local format = string.format;
20 local tostring = tostring;
22 local jid_split = require "util.jid".split;
25 function core_process_stanza(origin, stanza)
26 log("debug", "Received: "..tostring(stanza))
27 -- TODO verify validity of stanza (as well as JID validity)
28 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
29 if stanza.attr.type == "set" or stanza.attr.type == "get" then
31 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then
36 if origin.type == "c2s" and not origin.full_jid
37 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
38 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
39 error("Client MUST bind resource after auth");
42 local to = stanza.attr.to;
43 -- TODO also, stazas should be returned to their original state before the function ends
44 if origin.type == "c2s" then
45 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
49 core_handle_stanza(origin, stanza);
50 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
51 local node, host = jid_split(stanza.attr.to);
52 local to_bare = node and (node.."@"..host) or host; -- bare JID
53 local from_node, from_host = jid_split(stanza.attr.from);
54 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
55 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
56 elseif hosts[to] and hosts[to].type == "local" then
57 core_handle_stanza(origin, stanza);
58 elseif stanza.name == "iq" and not select(3, jid_split(to)) then
59 core_handle_stanza(origin, stanza);
60 elseif origin.type == "c2s" or origin.type == "s2sin" then
61 core_route_stanza(origin, stanza);
65 -- This function handles stanzas which are not routed any further,
66 -- that is, they are handled by this server
67 function core_handle_stanza(origin, stanza)
69 if origin.type == "c2s" or origin.type == "c2s_unauthed" then
70 local session = origin;
72 if stanza.name == "presence" and origin.roster then
73 if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
74 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
75 local subscription = origin.roster[jid].subscription;
76 if subscription == "both" or subscription == "from" then
78 core_route_stanza(origin, stanza);
81 local node, host = jid_split(stanza.attr.from);
82 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
83 if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
84 stanza.attr.to = res.full_jid;
85 core_route_stanza(origin, stanza);
88 if not origin.presence then -- presence probes on initial presence -- FIXME does unavailable qualify as initial presence?
89 local probe = st.presence({from = origin.full_jid, type = "probe"});
90 for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
91 local subscription = origin.roster[jid].subscription;
92 if subscription == "both" or subscription == "to" then
94 core_route_stanza(origin, probe);
97 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all resources
98 if res ~= origin and stanza.attr.type ~= "unavailable" and res.presence then -- FIXME does unavailable qualify as initial presence?
99 res.presence.attr.to = origin.full_jid;
100 core_route_stanza(res, res.presence);
101 res.presence.attr.to = nil;
104 -- TODO resend subscription requests
106 origin.presence = stanza;
107 stanza.attr.to = nil; -- reset it
109 -- TODO error, bad type
112 log("debug", "Routing stanza to local");
113 handle_stanza(session, stanza);
115 elseif origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
116 if stanza.attr.xmlns == "jabber:server:dialback" then
117 if stanza.name == "verify" then
118 -- We are being asked to verify the key, to ensure it was generated by us
119 log("debug", "verifying dialback key...");
120 local attr = stanza.attr;
121 print(tostring(attr.to), tostring(attr.from))
122 print(tostring(origin.to_host), tostring(origin.from_host))
123 -- FIXME: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
124 --if attr.from ~= origin.to_host then error("invalid-from"); end
126 if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
130 log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
132 origin.sends2s(format("<db:verify from='%s' to='%s' id='%s' type='%s'>%s</db:verify>", attr.to, attr.from, attr.id, type, stanza[1]));
133 elseif stanza.name == "result" and origin.type == "s2sin_unauthed" then
134 -- he wants to be identified through dialback
135 -- We need to check the key with the Authoritative server
136 local attr = stanza.attr;
137 origin.from_host = attr.from;
138 origin.to_host = attr.to;
139 origin.dialback_key = stanza[1];
140 log("debug", "asking %s if key %s belongs to them", attr.from, stanza[1]);
141 send_s2s(attr.to, attr.from, format("<db:verify from='%s' to='%s' id='%s'>%s</db:verify>", attr.to, attr.from, origin.streamid, stanza[1]));
142 hosts[attr.from].dialback_verifying = origin;
145 elseif origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
146 if stanza.attr.xmlns == "jabber:server:dialback" then
147 if stanza.name == "result" then
148 if stanza.attr.type == "valid" then
149 s2s_make_authenticated(origin);
152 error("dialback failed!");
154 elseif stanza.name == "verify" and origin.dialback_verifying then
156 local attr = stanza.attr;
157 if attr.type == "valid" then
158 s2s_make_authenticated(origin.dialback_verifying);
161 -- Warn the original connection that is was not verified successfully
162 log("warn", "dialback for "..(origin.dialback_verifying.from_host or "(unknown)").." failed");
165 origin.dialback_verifying.sends2s(format("<db:result from='%s' to='%s' id='%s' type='%s'>%s</db:result>", attr.from, attr.to, attr.id, valid, origin.dialback_verifying.dialback_key));
169 log("warn", "Unhandled origin: %s", origin.type);
173 function send_presence_of_available_resources(user, host, jid, recipient_session)
174 local h = hosts[host];
176 if h and h.type == "local" then
177 local u = h.sessions[user];
179 for k, session in pairs(u.sessions) do
180 local pres = session.presence;
183 pres.attr.from = session.full_jid;
184 recipient_session.send(pres);
186 pres.attr.from = nil;
195 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
196 local node, host = jid_split(from_bare);
197 local st_from, st_to = stanza.attr.from, stanza.attr.to;
198 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
199 if stanza.attr.type == "subscribe" then
200 log("debug", "outbound subscribe from "..from_bare.." for "..to_bare);
202 -- 2. roster push (subscription = none, ask = subscribe)
203 if rostermanager.set_contact_pending_out(node, host, to_bare) then
204 rostermanager.roster_push(node, host, to_bare);
205 end -- else file error
206 core_route_stanza(origin, stanza);
207 elseif stanza.attr.type == "unsubscribe" then
208 log("debug", "outbound unsubscribe from "..from_bare.." for "..to_bare);
210 -- 2. roster push (subscription = none or from)
211 if rostermanager.unsubscribe(node, host, to_bare) then
212 rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
213 end -- else file error
214 core_route_stanza(origin, stanza);
215 elseif stanza.attr.type == "subscribed" then
216 log("debug", "outbound subscribed from "..from_bare.." for "..to_bare);
219 -- 3. send_presence_of_available_resources
220 if rostermanager.subscribed(node, host, to_bare) then
221 rostermanager.roster_push(node, host, to_bare);
222 core_route_stanza(origin, stanza);
223 send_presence_of_available_resources(node, host, to_bare, origin);
225 elseif stanza.attr.type == "unsubscribed" then
226 log("debug", "outbound unsubscribed from "..from_bare.." for "..to_bare);
228 -- 2. roster push (subscription = none or to)
229 if rostermanager.unsubscribed(node, host, to_bare) then
230 rostermanager.roster_push(node, host, to_bare);
231 core_route_stanza(origin, stanza);
234 stanza.attr.from, stanza.attr.to = st_from, st_to;
237 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
238 local node, host = jid_split(to_bare);
239 local st_from, st_to = stanza.attr.from, stanza.attr.to;
240 stanza.attr.from, stanza.attr.to = from_bare, to_bare;
241 if stanza.attr.type == "probe" then
242 if rostermanager.is_contact_subscribed(node, host, from_bare) then
243 if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
244 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
247 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
249 elseif stanza.attr.type == "subscribe" then
250 log("debug", "inbound subscribe from "..from_bare.." for "..to_bare);
251 if rostermanager.is_contact_subscribed(node, host, from_bare) then
252 core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
254 if not rostermanager.is_contact_pending_in(node, host, from_bare) then
255 if rostermanager.set_contact_pending_in(node, host, from_bare) then
256 sessionmanager.send_to_available_resources(node, host, stanza);
257 end -- TODO else return error, unable to save
260 elseif stanza.attr.type == "unsubscribe" then
261 log("debug", "inbound unsubscribe from "..from_bare.." for "..to_bare);
262 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
263 rostermanager.roster_push(node, host, from_bare);
265 elseif stanza.attr.type == "subscribed" then
266 log("debug", "inbound subscribed from "..from_bare.." for "..to_bare);
267 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
268 rostermanager.roster_push(node, host, from_bare);
270 elseif stanza.attr.type == "unsubscribed" then
271 log("debug", "inbound unsubscribed from "..from_bare.." for "..to_bare);
272 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
273 rostermanager.roster_push(node, host, from_bare);
275 end -- discard any other type
276 stanza.attr.from, stanza.attr.to = st_from, st_to;
279 function core_route_stanza(origin, stanza)
284 local to = stanza.attr.to;
285 local node, host, resource = jid_split(to);
286 local to_bare = node and (node.."@"..host) or host; -- bare JID
287 local from = stanza.attr.from;
288 local from_node, from_host, from_resource = jid_split(from);
289 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
291 if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
293 local host_session = hosts[host]
294 if host_session and host_session.type == "local" then
296 local user = host_session.sessions[node];
298 local res = user.sessions[resource];
300 -- if we get here, resource was not specified or was unavailable
301 if stanza.name == "presence" then
302 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
303 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
304 else -- sender is available or unavailable
305 for k in pairs(user.sessions) do -- presence broadcast to all user resources. FIXME should this be just for available resources? Do we need to check subscription?
306 if user.sessions[k].full_jid then
307 stanza.attr.to = user.sessions[k].full_jid; -- reset at the end of function
308 user.sessions[k].send(stanza);
312 elseif stanza.name == "message" then -- select a resource to recieve message
313 for k in pairs(user.sessions) do
314 if user.sessions[k].full_jid then
315 res = user.sessions[k];
319 -- TODO find resource with greatest priority
322 -- TODO send IQ error
325 -- User + resource is online...
326 stanza.attr.to = res.full_jid; -- reset at the end of function
327 res.send(stanza); -- Yay \o/
331 if user_exists(node, host) then
332 if stanza.name == "presence" then
333 if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
334 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
336 -- TODO send unavailable presence or unsubscribed
338 elseif stanza.name == "message" then
339 -- TODO send message error, or store offline messages
340 elseif stanza.name == "iq" then
341 -- TODO send IQ error
343 else -- user does not exist
344 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
345 if stanza.name == "presence" then
346 if stanza.attr.type == "probe" then
347 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
351 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
355 elseif origin.type == "c2s" then
357 local xmlns = stanza.attr.xmlns;
358 --stanza.attr.xmlns = "jabber:server";
359 stanza.attr.xmlns = nil;
360 log("debug", "sending s2s stanza: %s", tostring(stanza));
361 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
362 stanza.attr.xmlns = xmlns; -- reset
364 log("warn", "received stanza from unhandled connection type: %s", origin.type);
366 stanza.attr.to = to; -- reset
369 function handle_stanza_toremote(stanza)
370 log("error", "Stanza bound for remote host, but s2s is not implemented");