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