Merging SASL buggy client workaround with current tip.
[prosody.git] / core / stanza_router.lua
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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
10
11 local log = require "util.logger".init("stanzarouter")
12
13 local hosts = _G.hosts;
14
15 local st = require "util.stanza";
16 local send_s2s = require "core.s2smanager".send_to_host;
17 local user_exists = require "core.usermanager".user_exists;
18
19 local rostermanager = require "core.rostermanager";
20 local sessionmanager = require "core.sessionmanager";
21 local offlinemanager = require "core.offlinemanager";
22
23 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
24 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
25
26 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
27 local component_handle_stanza = require "core.componentmanager".handle_stanza;
28
29 local handle_outbound_presence_subscriptions_and_probes = function()end;--require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
30 local handle_inbound_presence_subscriptions_and_probes = function()end;--require "core.presencemanager".handle_inbound_presence_subscriptions_and_probes;
31 local handle_normal_presence = function()end;--require "core.presencemanager".handle_normal_presence;
32
33 local format = string.format;
34 local tostring = tostring;
35 local t_concat = table.concat;
36 local t_insert = table.insert;
37 local tonumber = tonumber;
38 local s_find = string.find;
39 local pairs = pairs;
40 local ipairs = ipairs;
41
42 local jid_split = require "util.jid".split;
43 local jid_prepped_split = require "util.jid".prepped_split;
44 local print = print;
45 local fire_event = require "core.eventmanager2".fire_event;
46 local function checked_error_reply(origin, stanza)
47         if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
48                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
49         end
50 end
51
52 function core_process_stanza(origin, stanza)
53         (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag())
54
55         if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling.
56         -- TODO verify validity of stanza (as well as JID validity)
57         if stanza.attr.type == "error" and #stanza.tags == 0 then return; end -- TODO invalid stanza, log
58         if stanza.name == "iq" then
59                 if (stanza.attr.type == "set" or stanza.attr.type == "get") and #stanza.tags ~= 1 then
60                         origin.send(st.error_reply(stanza, "modify", "bad-request"));
61                         return;
62                 end
63         end
64
65         if origin.type == "c2s" and not origin.full_jid
66                 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
67                                 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
68                 error("Client MUST bind resource after auth");
69         end
70
71         -- TODO also, stanzas should be returned to their original state before the function ends
72         if origin.type == "c2s" then
73                 stanza.attr.from = origin.full_jid;
74         end
75         local to, xmlns = stanza.attr.to, stanza.attr.xmlns;
76         local from = stanza.attr.from;
77         local node, host, resource;
78         local from_node, from_host, from_resource;
79         local to_bare, from_bare;
80         if to then
81                 node, host, resource = jid_prepped_split(to);
82                 if not host then
83                         log("warn", "Received stanza with invalid destination JID: %s", to);
84                         origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The destination address is invalid: "..to));
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         if from then
92                 -- We only stamp the 'from' on c2s stanzas, so we still need to check validity
93                 from_node, from_host, from_resource = jid_prepped_split(from);
94                 if not from_host then
95                         log("warn", "Received stanza with invalid source JID: %s", from);
96                         origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The source address is invalid: "..from));
97                         return;
98                 end
99                 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
100                 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
101                 stanza.attr.from = from;
102         end
103
104         --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
105                 log("warn", "stanza recieved for a non-local server");
106                 return; -- FIXME what should we do here?
107         end]] -- FIXME
108
109         if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and (not xmlns or xmlns == "jabber:server" or xmlns == "jabber:client") then                  
110                 local event_data = {origin=origin, stanza=stanza};
111                 if fire_event(tostring(host or origin.host).."/"..stanza.name, event_data) then return; end
112                 if origin.type == "s2sin" and not origin.dummy then
113                         local host_status = origin.hosts[from_host];
114                         if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
115                                 log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from_host, origin.from_host);
116                                 return; -- FIXME what should we do here? does this work with subdomains?
117                         end
118                 end
119                 if not to then
120                         core_handle_stanza(origin, stanza);
121                 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
122                         core_handle_stanza(origin, stanza);
123                 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
124                         modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
125                 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
126                         component_handle_stanza(origin, stanza);
127                 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
128                         component_handle_stanza(origin, stanza);
129                 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
130                         component_handle_stanza(origin, stanza);
131                 elseif hosts[host] and hosts[host].type == "local" and stanza.name == "iq" and not resource then -- directed at bare JID
132                         core_handle_stanza(origin, stanza);
133                 else
134                         core_route_stanza(origin, stanza);
135                 end
136         else
137                 core_handle_stanza(origin, stanza);
138         end
139 end
140
141 -- This function handles stanzas which are not routed any further,
142 -- that is, they are handled by this server
143 function core_handle_stanza(origin, stanza)
144         -- Handlers
145         if modules_handle_stanza(select(2, jid_split(stanza.attr.to)) or origin.host or origin.to_host, origin, stanza) then return; end
146         if origin.type == "c2s" or origin.type == "s2sin" then
147                 if origin.type == "c2s" then
148                         if stanza.name == "presence" and origin.roster then
149                                 if stanza.attr.type == nil or stanza.attr.type == "unavailable" and stanza.attr.type ~= "error" then
150                                         handle_normal_presence(origin, stanza, core_route_stanza);
151                                 else
152                                         log("warn", "Unhandled c2s presence: %s", tostring(stanza));
153                                         checked_error_reply(origin, stanza);
154                                 end
155                         else
156                                 log("warn", "Unhandled c2s stanza: %s", tostring(stanza));
157                                 checked_error_reply(origin, stanza);
158                         end
159                 else -- s2s stanzas
160                         log("warn", "Unhandled s2s stanza: %s", tostring(stanza));
161                         checked_error_reply(origin, stanza);
162                 end
163         else
164                 log("warn", "Unhandled %s stanza: %s", origin.type, tostring(stanza));
165                 checked_error_reply(origin, stanza);
166         end
167 end
168
169 function core_route_stanza(origin, stanza)
170         -- Hooks
171         --- ...later
172
173         -- Deliver
174         local to = stanza.attr.to;
175         local node, host, resource = jid_split(to);
176         local to_bare = node and (node.."@"..host) or host; -- bare JID
177         local from = stanza.attr.from;
178         local from_node, from_host, from_resource = jid_split(from);
179         local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
180
181         -- Auto-detect origin if not specified
182         origin = origin or hosts[from_host];
183         if not origin then return false; end
184         
185         if hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
186                 return component_handle_stanza(origin, stanza);
187         elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
188                 return component_handle_stanza(origin, stanza);
189         elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
190                 return component_handle_stanza(origin, stanza);
191         end
192
193         if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error") then resource = nil; end
194
195         local host_session = hosts[host]
196         if host_session and host_session.type == "local" then
197                 -- Local host
198                 local user = host_session.sessions[node];
199                 if user then
200                         local res = user.sessions[resource];
201                         if not res then
202                                 -- if we get here, resource was not specified or was unavailable
203                                 if stanza.name == "presence" then
204                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
205                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
206                                         elseif not resource then -- sender is available or unavailable or error
207                                                 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
208                                                         if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
209                                                                 stanza.attr.to = session.full_jid; -- reset at the end of function
210                                                                 session.send(stanza);
211                                                         end
212                                                 end
213                                         end
214                                 elseif stanza.name == "message" then -- select a resource to recieve message
215                                         stanza.attr.to = to_bare;
216                                         if stanza.attr.type == 'headline' then
217                                                 for _, session in pairs(user.sessions) do -- find resource with greatest priority
218                                                         if session.presence and session.priority >= 0 then
219                                                                 session.send(stanza);
220                                                         end
221                                                 end
222                                         elseif resource and stanza.attr.type == 'groupchat' then
223                                                 -- Groupchat message sent to offline resource
224                                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
225                                         else
226                                                 local priority = 0;
227                                                 local recipients = {};
228                                                 for _, session in pairs(user.sessions) do -- find resource with greatest priority
229                                                         if session.presence then
230                                                                 local p = session.priority;
231                                                                 if p > priority then
232                                                                         priority = p;
233                                                                         recipients = {session};
234                                                                 elseif p == priority then
235                                                                         t_insert(recipients, session);
236                                                                 end
237                                                         end
238                                                 end
239                                                 local count = 0;
240                                                 for _, session in ipairs(recipients) do
241                                                         session.send(stanza);
242                                                         count = count + 1;
243                                                 end
244                                                 if count == 0 and (stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type) then
245                                                         offlinemanager.store(node, host, stanza);
246                                                         -- TODO deal with storage errors
247                                                 end
248                                         end
249                                 elseif stanza.attr.type == "get" or stanza.attr.type == "set" then
250                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
251                                 end
252                         else
253                                 -- User + resource is online...
254                                 stanza.attr.to = res.full_jid; -- reset at the end of function
255                                 res.send(stanza); -- Yay \o/
256                         end
257                 else
258                         -- user not online
259                         if user_exists(node, host) then
260                                 if stanza.name == "presence" then
261                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then
262                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
263                                         else
264                                                 -- TODO send unavailable presence or unsubscribed
265                                         end
266                                 elseif stanza.name == "message" then -- FIXME if full jid, then send out to resources with highest priority
267                                         stanza.attr.to = to_bare; -- TODO not in RFC, but seems obvious. Should discuss on the mailing list.
268                                         if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
269                                                 offlinemanager.store(node, host, stanza);
270                                                 -- FIXME don't store messages with only chat state notifications
271                                         elseif stanza.attr.type == "groupchat" then
272                                                 local reply = st.error_reply(stanza, "cancel", "service-unavailable");
273                                                 reply.attr.from = to;
274                                                 origin.send(reply);
275                                         end
276                                         -- TODO allow configuration of offline storage
277                                         -- TODO send error if not storing offline
278                                 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
279                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
280                                 end
281                         else -- user does not exist
282                                 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
283                                 if stanza.name == "presence" then
284                                         local t = stanza.attr.type;
285                                         if t == "subscribe" or t == "probe" then
286                                                 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
287                                         end
288                                         -- else ignore
289                                 elseif stanza.attr.type ~= "error" and (stanza.name ~= "iq" or stanza.attr.type ~= "result") then
290                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
291                                 end
292                         end
293                 end
294         elseif origin.type == "c2s" then
295                 -- Remote host
296                 local xmlns = stanza.attr.xmlns;
297                 --stanza.attr.xmlns = "jabber:server";
298                 stanza.attr.xmlns = nil;
299                 log("debug", "sending s2s stanza: %s", tostring(stanza));
300                 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
301                 stanza.attr.xmlns = xmlns; -- reset
302         elseif origin.type == "component" or origin.type == "local" then
303                 -- Route via s2s for components and modules
304                 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
305                 send_s2s(origin.host, host, stanza);
306         else
307                 log("warn", "received stanza from unhandled connection type: %s", origin.type);
308         end
309         stanza.attr.to = to; -- reset
310 end