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