2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local log = require "util.logger".init("stanzarouter")
11 local hosts = _G.prosody.hosts;
12 local tostring = tostring;
13 local st = require "util.stanza";
14 local jid_split = require "util.jid".split;
15 local jid_prepped_split = require "util.jid".prepped_split;
17 local full_sessions = _G.prosody.full_sessions;
18 local bare_sessions = _G.prosody.bare_sessions;
20 local function handle_unhandled_stanza(host, origin, stanza)
21 local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns or "jabber:client", origin.type;
22 if name == "iq" and xmlns == "jabber:client" then
23 if stanza.attr.type == "get" or stanza.attr.type == "set" then
24 xmlns = stanza.tags[1].attr.xmlns or "jabber:client";
25 log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
27 log("debug", "Discarding %s from %s of type: %s", name, origin_type, stanza.attr.type);
31 if stanza.attr.xmlns == nil and origin.send then
32 log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin.type, stanza.name, xmlns); -- we didn't handle it
33 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
34 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
36 elseif not((name == "features" or name == "error") and xmlns == "http://etherx.jabber.org/streams") then -- FIXME remove check once we handle S2S features
37 log("warn", "Unhandled %s stream element or stanza: %s; xmlns=%s: %s", origin.type, stanza.name, xmlns, tostring(stanza)); -- we didn't handle it
38 origin:close("unsupported-stanza-type");
42 local iq_types = { set=true, get=true, result=true, error=true };
43 function core_process_stanza(origin, stanza)
44 (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag())
46 -- TODO verify validity of stanza (as well as JID validity)
47 if stanza.attr.type == "error" and #stanza.tags == 0 then return; end -- TODO invalid stanza, log
48 if stanza.name == "iq" then
49 if not stanza.attr.id then stanza.attr.id = ""; end -- COMPAT Jabiru doesn't send the id attribute on roster requests
50 if not iq_types[stanza.attr.type] or ((stanza.attr.type == "set" or stanza.attr.type == "get") and (#stanza.tags ~= 1)) then
51 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid IQ type or incorrect number of children"));
56 if origin.type == "c2s" and not stanza.attr.xmlns then
57 if not origin.full_jid
58 and not(stanza.name == "iq" and stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "bind"
59 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
60 -- authenticated client isn't bound and current stanza is not a bind request
61 if stanza.attr.type ~= "result" and stanza.attr.type ~= "error" then
62 origin.send(st.error_reply(stanza, "auth", "not-authorized")); -- FIXME maybe allow stanzas to account or server
67 -- TODO also, stanzas should be returned to their original state before the function ends
68 stanza.attr.from = origin.full_jid;
70 local to, xmlns = stanza.attr.to, stanza.attr.xmlns;
71 local from = stanza.attr.from;
72 local node, host, resource;
73 local from_node, from_host, from_resource;
74 local to_bare, from_bare;
76 if full_sessions[to] or bare_sessions[to] or hosts[to] then
77 node, host = jid_split(to); -- TODO only the host is needed, optimize
79 node, host, resource = jid_prepped_split(to);
81 log("warn", "Received stanza with invalid destination JID: %s", to);
82 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
83 origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The destination address is invalid: "..to));
87 to_bare = node and (node.."@"..host) or host; -- bare JID
88 if resource then to = to_bare.."/"..resource; else to = to_bare; end
92 if from and not origin.full_jid then
93 -- We only stamp the 'from' on c2s stanzas, so we still need to check validity
94 from_node, from_host, from_resource = jid_prepped_split(from);
96 log("warn", "Received stanza with invalid source JID: %s", from);
97 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
98 origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The source address is invalid: "..from));
102 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
103 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
104 stanza.attr.from = from;
107 if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and xmlns == nil then
108 if origin.type == "s2sin" and not origin.dummy then
109 local host_status = origin.hosts[from_host];
110 if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
111 log("warn", "Received a stanza claiming to be from %s, over a stream authed for %s!", from_host, origin.from_host);
112 origin:close("not-authorized");
114 elseif not hosts[host] then
115 log("warn", "Remote server %s sent us a stanza for %s, closing stream", origin.from_host, host);
116 origin:close("host-unknown");
120 core_post_stanza(origin, stanza, origin.full_jid);
122 local h = hosts[stanza.attr.to or origin.host or origin.to_host];
126 if stanza.name == "iq" and (stanza.attr.type == "set" or stanza.attr.type == "get") then
127 event = "stanza/iq/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name;
129 event = "stanza/"..stanza.name;
132 event = "stanza/"..xmlns..":"..stanza.name;
134 if h.events.fire_event(event, {origin = origin, stanza = stanza}) then return; end
136 if host and not hosts[host] then host = nil; end -- COMPAT: workaround for a Pidgin bug which sets 'to' to the SRV result
137 handle_unhandled_stanza(host or origin.host or origin.to_host, origin, stanza);
141 function core_post_stanza(origin, stanza, preevents)
142 local to = stanza.attr.to;
143 local node, host, resource = jid_split(to);
144 local to_bare = node and (node.."@"..host) or host; -- bare JID
146 local to_type, to_self;
152 if node == origin.username and host == origin.host then
153 stanza.attr.to = nil;
166 local event_data = {origin=origin, stanza=stanza};
167 if preevents then -- c2s connection
168 if hosts[origin.host].events.fire_event('pre-'..stanza.name..to_type, event_data) then return; end -- do preprocessing
170 local h = hosts[to_bare] or hosts[host or origin.host];
172 if h.events.fire_event(stanza.name..to_type, event_data) then return; end -- do processing
173 if to_self and h.events.fire_event(stanza.name..'/self', event_data) then return; end -- do processing
174 handle_unhandled_stanza(h.host, origin, stanza);
176 core_route_stanza(origin, stanza);
180 function core_route_stanza(origin, stanza)
181 local node, host, resource = jid_split(stanza.attr.to);
182 local from_node, from_host, from_resource = jid_split(stanza.attr.from);
184 -- Auto-detect origin if not specified
185 origin = origin or hosts[from_host];
186 if not origin then return false; end
189 -- old stanza routing code removed
190 core_post_stanza(origin, stanza);
192 log("debug", "Routing to remote...");
193 local host_session = hosts[from_host];
194 if not host_session then
195 log("error", "No hosts[from_host] (please report): %s", tostring(stanza));
197 local xmlns = stanza.attr.xmlns;
198 stanza.attr.xmlns = nil;
199 local routed = host_session.events.fire_event("route/remote", { origin = origin, stanza = stanza, from_host = from_host, to_host = host });
200 stanza.attr.xmlns = xmlns; -- reset
202 log("debug", "... no, just kidding.");
203 if stanza.attr.type == "error" or (stanza.name == "iq" and stanza.attr.type == "result") then return; end
204 core_route_stanza(host_session, st.error_reply(stanza, "cancel", "not-allowed", "Communication with remote domains is not enabled"));
209 prosody.core_process_stanza = core_process_stanza;
210 prosody.core_post_stanza = core_post_stanza;
211 prosody.core_route_stanza = core_route_stanza;