stanza_router: Replace s2s send logic with firing of a 'route/remote' event
[prosody.git] / core / stanza_router.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local log = require "util.logger".init("stanzarouter")
10
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;
16
17 local full_sessions = _G.prosody.full_sessions;
18 local bare_sessions = _G.prosody.bare_sessions;
19
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);
26                 else
27                         log("debug", "Discarding %s from %s of type: %s", name, origin_type, stanza.attr.type);
28                         return true;
29                 end
30         end
31         if stanza.attr.xmlns == nil 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"));
35                 end
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: %s; xmlns=%s: %s", origin.type, stanza.name, xmlns, tostring(stanza)); -- we didn't handle it
38                 origin:close("unsupported-stanza-type");
39         end
40 end
41
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())
45
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"));
52                         return;
53                 end
54         end
55
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
63                         end
64                         return;
65                 end
66
67                 -- TODO also, stanzas should be returned to their original state before the function ends
68                 stanza.attr.from = origin.full_jid;
69         end
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;
75         if to then
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
78                 else
79                         node, host, resource = jid_prepped_split(to);
80                         if not host then
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));
84                                 end
85                                 return;
86                         end
87                         to_bare = node and (node.."@"..host) or host; -- bare JID
88                         if resource then to = to_bare.."/"..resource; else to = to_bare; end
89                         stanza.attr.to = to;
90                 end
91         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);
95                 if not from_host then
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));
99                         end
100                         return;
101                 end
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;
105         end
106
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                                 return; -- FIXME what should we do here? does this work with subdomains?
113                         end
114                 end
115                 core_post_stanza(origin, stanza, origin.full_jid);
116         else
117                 local h = hosts[stanza.attr.to or origin.host or origin.to_host];
118                 if h then
119                         local event;
120                         if xmlns == nil then
121                                 if stanza.name == "iq" and (stanza.attr.type == "set" or stanza.attr.type == "get") then
122                                         event = "stanza/iq/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name;
123                                 else
124                                         event = "stanza/"..stanza.name;
125                                 end
126                         else
127                                 event = "stanza/"..xmlns..":"..stanza.name;
128                         end
129                         if h.events.fire_event(event, {origin = origin, stanza = stanza}) then return; end
130                 end
131                 if host and not hosts[host] then host = nil; end -- COMPAT: workaround for a Pidgin bug which sets 'to' to the SRV result
132                 handle_unhandled_stanza(host or origin.host or origin.to_host, origin, stanza);
133         end
134 end
135
136 function core_post_stanza(origin, stanza, preevents)
137         local to = stanza.attr.to;
138         local node, host, resource = jid_split(to);
139         local to_bare = node and (node.."@"..host) or host; -- bare JID
140
141         local to_type, to_self;
142         if node then
143                 if resource then
144                         to_type = '/full';
145                 else
146                         to_type = '/bare';
147                         if node == origin.username and host == origin.host then
148                                 stanza.attr.to = nil;
149                                 to_self = true;
150                         end
151                 end
152         else
153                 if host then
154                         to_type = '/host';
155                 else
156                         to_type = '/bare';
157                         to_self = true;
158                 end
159         end
160
161         local event_data = {origin=origin, stanza=stanza};
162         if preevents then -- c2s connection
163                 if hosts[origin.host].events.fire_event('pre-'..stanza.name..to_type, event_data) then return; end -- do preprocessing
164         end
165         local h = hosts[to_bare] or hosts[host or origin.host];
166         if h then
167                 if h.events.fire_event(stanza.name..to_type, event_data) then return; end -- do processing
168                 if to_self and h.events.fire_event(stanza.name..'/self', event_data) then return; end -- do processing
169                 handle_unhandled_stanza(h.host, origin, stanza);
170         else
171                 core_route_stanza(origin, stanza);
172         end
173 end
174
175 function core_route_stanza(origin, stanza)
176         local node, host, resource = jid_split(stanza.attr.to);
177         local from_node, from_host, from_resource = jid_split(stanza.attr.from);
178
179         -- Auto-detect origin if not specified
180         origin = origin or hosts[from_host];
181         if not origin then return false; end
182         
183         if hosts[host] then
184                 -- old stanza routing code removed
185                 core_post_stanza(origin, stanza);
186         else
187                 log("debug", "Routing to remote...");
188                 if not hosts[from_host] then
189                         log("error", "No hosts[from_host] (please report): %s", tostring(stanza));
190                 else
191                         local xmlns = stanza.attr.xmlns;
192                         stanza.attr.xmlns = nil;
193                         local routed = prosody.events.fire_event("route/remote", { origin = origin, stanza = stanza, from_host = from_host, to_host = host }); --FIXME: Should be per-host (shared modules!)
194                         stanza.attr.xmlns = xmlns; -- reset
195                         if routed == nil then
196                                 core_route_stanza(hosts[from_host], st.error_reply(stanza, "cancel", "not-allowed", "Communication with remote domains is not enabled"));
197                         end
198                 end
199         end
200 end