Merge 0.9->0.10
[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 core_post_stanza, core_process_stanza, core_route_stanza;
21
22 function deprecated_warning(f)
23         _G[f] = function(...)
24                 log("warn", "Using the global %s() is deprecated, use module:send() or prosody.%s(). %s", f, f, debug.traceback());
25                 return prosody[f](...);
26         end
27 end
28 deprecated_warning"core_post_stanza";
29 deprecated_warning"core_process_stanza";
30 deprecated_warning"core_route_stanza";
31
32 local function handle_unhandled_stanza(host, origin, stanza)
33         local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns or "jabber:client", origin.type;
34         if name == "iq" and xmlns == "jabber:client" then
35                 if stanza.attr.type == "get" or stanza.attr.type == "set" then
36                         xmlns = stanza.tags[1].attr.xmlns or "jabber:client";
37                         log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
38                 else
39                         log("debug", "Discarding %s from %s of type: %s", name, origin_type, stanza.attr.type);
40                         return true;
41                 end
42         end
43         if stanza.attr.xmlns == nil and origin.send then
44                 log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin.type, stanza.name, xmlns); -- we didn't handle it
45                 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
46                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
47                 end
48         elseif not((name == "features" or name == "error") and xmlns == "http://etherx.jabber.org/streams") then -- FIXME remove check once we handle S2S features
49                 log("warn", "Unhandled %s stream element or stanza: %s; xmlns=%s: %s", origin.type, stanza.name, xmlns, tostring(stanza)); -- we didn't handle it
50                 origin:close("unsupported-stanza-type");
51         end
52 end
53
54 local iq_types = { set=true, get=true, result=true, error=true };
55 function core_process_stanza(origin, stanza)
56         (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag())
57
58         -- TODO verify validity of stanza (as well as JID validity)
59         if stanza.attr.type == "error" and #stanza.tags == 0 then return; end -- TODO invalid stanza, log
60         if stanza.name == "iq" then
61                 if not stanza.attr.id then stanza.attr.id = ""; end -- COMPAT Jabiru doesn't send the id attribute on roster requests
62                 if not iq_types[stanza.attr.type] or ((stanza.attr.type == "set" or stanza.attr.type == "get") and (#stanza.tags ~= 1)) then
63                         origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid IQ type or incorrect number of children"));
64                         return;
65                 end
66         end
67
68         if origin.type == "c2s" and not stanza.attr.xmlns then
69                 if not origin.full_jid
70                         and not(stanza.name == "iq" and stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "bind"
71                                         and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
72                         -- authenticated client isn't bound and current stanza is not a bind request
73                         if stanza.attr.type ~= "result" and stanza.attr.type ~= "error" then
74                                 origin.send(st.error_reply(stanza, "auth", "not-authorized")); -- FIXME maybe allow stanzas to account or server
75                         end
76                         return;
77                 end
78
79                 -- TODO also, stanzas should be returned to their original state before the function ends
80                 stanza.attr.from = origin.full_jid;
81         end
82         local to, xmlns = stanza.attr.to, stanza.attr.xmlns;
83         local from = stanza.attr.from;
84         local node, host, resource;
85         local from_node, from_host, from_resource;
86         local to_bare, from_bare;
87         if to then
88                 if full_sessions[to] or bare_sessions[to] or hosts[to] then
89                         node, host = jid_split(to); -- TODO only the host is needed, optimize
90                 else
91                         node, host, resource = jid_prepped_split(to);
92                         if not host then
93                                 log("warn", "Received stanza with invalid destination JID: %s", to);
94                                 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
95                                         origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The destination address is invalid: "..to));
96                                 end
97                                 return;
98                         end
99                         to_bare = node and (node.."@"..host) or host; -- bare JID
100                         if resource then to = to_bare.."/"..resource; else to = to_bare; end
101                         stanza.attr.to = to;
102                 end
103         end
104         if from and not origin.full_jid then
105                 -- We only stamp the 'from' on c2s stanzas, so we still need to check validity
106                 from_node, from_host, from_resource = jid_prepped_split(from);
107                 if not from_host then
108                         log("warn", "Received stanza with invalid source JID: %s", from);
109                         if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
110                                 origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The source address is invalid: "..from));
111                         end
112                         return;
113                 end
114                 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
115                 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
116                 stanza.attr.from = from;
117         end
118
119         if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and xmlns == nil then
120                 if origin.type == "s2sin" and not origin.dummy then
121                         local host_status = origin.hosts[from_host];
122                         if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
123                                 log("warn", "Received a stanza claiming to be from %s, over a stream authed for %s!", from_host, origin.from_host);
124                                 origin:close("not-authorized");
125                                 return;
126                         elseif not hosts[host] then
127                                 log("warn", "Remote server %s sent us a stanza for %s, closing stream", origin.from_host, host);
128                                 origin:close("host-unknown");
129                                 return;
130                         end
131                 end
132                 core_post_stanza(origin, stanza, origin.full_jid);
133         else
134                 local h = hosts[stanza.attr.to or origin.host or origin.to_host];
135                 if h then
136                         local event;
137                         if xmlns == nil then
138                                 if stanza.name == "iq" and (stanza.attr.type == "set" or stanza.attr.type == "get") then
139                                         event = "stanza/iq/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name;
140                                 else
141                                         event = "stanza/"..stanza.name;
142                                 end
143                         else
144                                 event = "stanza/"..xmlns..":"..stanza.name;
145                         end
146                         if h.events.fire_event(event, {origin = origin, stanza = stanza}) then return; end
147                 end
148                 if host and not hosts[host] then host = nil; end -- COMPAT: workaround for a Pidgin bug which sets 'to' to the SRV result
149                 handle_unhandled_stanza(host or origin.host or origin.to_host, origin, stanza);
150         end
151 end
152
153 function core_post_stanza(origin, stanza, preevents)
154         local to = stanza.attr.to;
155         local node, host, resource = jid_split(to);
156         local to_bare = node and (node.."@"..host) or host; -- bare JID
157
158         local to_type, to_self;
159         if node then
160                 if resource then
161                         to_type = '/full';
162                 else
163                         to_type = '/bare';
164                         if node == origin.username and host == origin.host then
165                                 stanza.attr.to = nil;
166                                 to_self = true;
167                         end
168                 end
169         else
170                 if host then
171                         to_type = '/host';
172                 else
173                         to_type = '/bare';
174                         to_self = true;
175                 end
176         end
177
178         local event_data = {origin=origin, stanza=stanza};
179         if preevents then -- c2s connection
180                 if hosts[origin.host].events.fire_event('pre-'..stanza.name..to_type, event_data) then return; end -- do preprocessing
181         end
182         local h = hosts[to_bare] or hosts[host or origin.host];
183         if h then
184                 if h.events.fire_event(stanza.name..to_type, event_data) then return; end -- do processing
185                 if to_self and h.events.fire_event(stanza.name..'/self', event_data) then return; end -- do processing
186                 handle_unhandled_stanza(h.host, origin, stanza);
187         else
188                 core_route_stanza(origin, stanza);
189         end
190 end
191
192 function core_route_stanza(origin, stanza)
193         local node, host, resource = jid_split(stanza.attr.to);
194         local from_node, from_host, from_resource = jid_split(stanza.attr.from);
195
196         -- Auto-detect origin if not specified
197         origin = origin or hosts[from_host];
198         if not origin then return false; end
199
200         if hosts[host] then
201                 -- old stanza routing code removed
202                 core_post_stanza(origin, stanza);
203         else
204                 log("debug", "Routing to remote...");
205                 local host_session = hosts[from_host];
206                 if not host_session then
207                         log("error", "No hosts[from_host] (please report): %s", tostring(stanza));
208                 else
209                         local xmlns = stanza.attr.xmlns;
210                         stanza.attr.xmlns = nil;
211                         local routed = host_session.events.fire_event("route/remote", { origin = origin, stanza = stanza, from_host = from_host, to_host = host });
212                         stanza.attr.xmlns = xmlns; -- reset
213                         if not routed then
214                                 log("debug", "... no, just kidding.");
215                                 if stanza.attr.type == "error" or (stanza.name == "iq" and stanza.attr.type == "result") then return; end
216                                 core_route_stanza(host_session, st.error_reply(stanza, "cancel", "not-allowed", "Communication with remote domains is not enabled"));
217                         end
218                 end
219         end
220 end
221 prosody.core_process_stanza = core_process_stanza;
222 prosody.core_post_stanza = core_post_stanza;
223 prosody.core_route_stanza = core_route_stanza;