Merge from waqas
[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 format = string.format;
25 local tostring = tostring;
26 local t_concat = table.concat;
27 local t_insert = table.insert;
28 local tonumber = tonumber;
29 local s_find = string.find;
30
31 local jid_split = require "util.jid".split;
32 local print = print;
33
34 function core_process_stanza(origin, stanza)
35         log("debug", "Received[%s]: %s", origin.type, stanza:pretty_top_tag())
36
37         if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling.
38         -- TODO verify validity of stanza (as well as JID validity)
39         if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
40                 if stanza.attr.type == "set" or stanza.attr.type == "get" then
41                         error("Invalid IQ");
42                 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then
43                         error("Invalid IQ");
44                 end
45         end
46
47         if origin.type == "c2s" and not origin.full_jid
48                 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
49                                 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
50                 error("Client MUST bind resource after auth");
51         end
52
53         -- TODO also, stazas should be returned to their original state before the function ends
54         if origin.type == "c2s" then
55                 stanza.attr.from = origin.full_jid;
56         end
57         local to = stanza.attr.to;
58         local node, host, resource = jid_split(to);
59         local to_bare = node and (node.."@"..host) or host; -- bare JID
60         local from = stanza.attr.from;
61         local from_node, from_host, from_resource = jid_split(from);
62         local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
63
64         if origin.type == "s2sin" then
65                 if origin.from_host ~= from_host then -- remote server trying to impersonate some other server?
66                         log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from, origin.from_host);
67                         return; -- FIXME what should we do here? does this work with subdomains?
68                 end
69         end
70         --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
71                 log("warn", "stanza recieved for a non-local server");
72                 return; -- FIXME what should we do here?
73         end]] -- FIXME
74
75         -- FIXME do stanzas not of jabber:client get handled by components?
76         if origin.type == "s2sin" or origin.type == "c2s" then
77                 if not to then
78                         core_handle_stanza(origin, stanza);
79                 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
80                         core_handle_stanza(origin, stanza);
81                 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
82                         modules_handle_stanza(origin, stanza);
83                 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
84                         component_handle_stanza(origin, stanza);
85                 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
86                         component_handle_stanza(origin, stanza);
87                 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
88                         component_handle_stanza(origin, stanza);
89                 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
90                         handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
91                 elseif stanza.name == "iq" and not resource then -- directed at bare JID
92                         core_handle_stanza(origin, stanza);
93                 else
94                         core_route_stanza(origin, stanza);
95                 end
96         else
97                 core_handle_stanza(origin, stanza);
98         end
99 end
100
101 -- This function handles stanzas which are not routed any further,
102 -- that is, they are handled by this server
103 function core_handle_stanza(origin, stanza)
104         -- Handlers
105         if modules_handle_stanza(origin, stanza) then return; end
106         if origin.type == "c2s" or origin.type == "c2s_unauthed" then
107                 local session = origin;
108
109                 if stanza.name == "presence" and origin.roster then
110                         if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
111                                 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
112                                         local subscription = origin.roster[jid].subscription;
113                                         if subscription == "both" or subscription == "from" then
114                                                 stanza.attr.to = jid;
115                                                 core_route_stanza(origin, stanza);
116                                         end
117                                 end
118                                 local node, host = jid_split(stanza.attr.from);
119                                 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
120                                         if res ~= origin and res.full_jid then -- to resource. FIXME is res.full_jid the correct check? Maybe it should be res.presence
121                                                 stanza.attr.to = res.full_jid;
122                                                 core_route_stanza(origin, stanza);
123                                         end
124                                 end
125                                 if stanza.attr.type == nil and not origin.presence then -- initial presence
126                                         local probe = st.presence({from = origin.full_jid, type = "probe"});
127                                         for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to
128                                                 local subscription = origin.roster[jid].subscription;
129                                                 if subscription == "both" or subscription == "to" then
130                                                         probe.attr.to = jid;
131                                                         core_route_stanza(origin, probe);
132                                                 end
133                                         end
134                                         for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources
135                                                 if res ~= origin and res.presence then
136                                                         res.presence.attr.to = origin.full_jid;
137                                                         core_route_stanza(res, res.presence);
138                                                         res.presence.attr.to = nil;
139                                                 end
140                                         end
141                                         if origin.roster.pending then -- resend incoming subscription requests
142                                                 for jid in pairs(origin.roster.pending) do
143                                                         origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?
144                                                 end
145                                         end
146                                         local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});
147                                         for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests
148                                                 if item.ask then
149                                                         request.attr.to = jid;
150                                                         core_route_stanza(origin, request);
151                                                 end
152                                         end
153                                         for _, msg in ipairs(offlinemanager.load(node, host) or {}) do
154                                                 origin.send(msg); -- FIXME do we need to modify to/from in any way?
155                                         end
156                                         offlinemanager.deleteAll(node, host);
157                                 end
158                                 origin.priority = 0;
159                                 if stanza.attr.type == "unavailable" then
160                                         origin.presence = nil;
161                                 else
162                                         origin.presence = stanza;
163                                         local priority = stanza:child_with_name("priority");
164                                         if priority and #priority > 0 then
165                                                 priority = t_concat(priority);
166                                                 if s_find(priority, "^[+-]?[0-9]+$") then
167                                                         priority = tonumber(priority);
168                                                         if priority < -128 then priority = -128 end
169                                                         if priority > 127 then priority = 127 end
170                                                         origin.priority = priority;
171                                                 end
172                                         end
173                                 end
174                                 stanza.attr.to = nil; -- reset it
175                         else
176                                 log("warn", "Unhandled c2s presence: %s", tostring(stanza));
177                                 if stanza.attr.type ~= "error" then
178                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
179                                 end
180                         end
181                 else
182                         log("warn", "Unhandled c2s stanza: %s", tostring(stanza));
183                         if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
184                                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
185                         end
186                 end -- TODO handle other stanzas
187         else
188                 log("warn", "Unhandled origin: %s", origin.type);
189                 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
190                         -- s2s stanzas can get here
191                         (origin.sends2s or origin.send)(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
192                 end
193         end
194 end
195
196 function send_presence_of_available_resources(user, host, jid, recipient_session)
197         local h = hosts[host];
198         local count = 0;
199         if h and h.type == "local" then
200                 local u = h.sessions[user];
201                 if u then
202                         for k, session in pairs(u.sessions) do
203                                 local pres = session.presence;
204                                 if pres then
205                                         pres.attr.to = jid;
206                                         pres.attr.from = session.full_jid;
207                                         recipient_session.send(pres);
208                                         pres.attr.to = nil;
209                                         pres.attr.from = nil;
210                                         count = count + 1;
211                                 end
212                         end
213                 end
214         end
215         return count;
216 end
217
218 function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
219         local node, host = jid_split(from_bare);
220         local st_from, st_to = stanza.attr.from, stanza.attr.to;
221         stanza.attr.from, stanza.attr.to = from_bare, to_bare;
222         if stanza.attr.type == "subscribe" then
223                 log("debug", "outbound subscribe from "..from_bare.." for "..to_bare);
224                 -- 1. route stanza
225                 -- 2. roster push (subscription = none, ask = subscribe)
226                 if rostermanager.set_contact_pending_out(node, host, to_bare) then
227                         rostermanager.roster_push(node, host, to_bare);
228                 end -- else file error
229                 core_route_stanza(origin, stanza);
230         elseif stanza.attr.type == "unsubscribe" then
231                 log("debug", "outbound unsubscribe from "..from_bare.." for "..to_bare);
232                 -- 1. route stanza
233                 -- 2. roster push (subscription = none or from)
234                 if rostermanager.unsubscribe(node, host, to_bare) then
235                         rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?
236                 end -- else file error
237                 core_route_stanza(origin, stanza);
238         elseif stanza.attr.type == "subscribed" then
239                 log("debug", "outbound subscribed from "..from_bare.." for "..to_bare);
240                 -- 1. route stanza
241                 -- 2. roster_push ()
242                 -- 3. send_presence_of_available_resources
243                 if rostermanager.subscribed(node, host, to_bare) then
244                         rostermanager.roster_push(node, host, to_bare);
245                         core_route_stanza(origin, stanza);
246                         send_presence_of_available_resources(node, host, to_bare, origin);
247                 end
248         elseif stanza.attr.type == "unsubscribed" then
249                 log("debug", "outbound unsubscribed from "..from_bare.." for "..to_bare);
250                 -- 1. route stanza
251                 -- 2. roster push (subscription = none or to)
252                 if rostermanager.unsubscribed(node, host, to_bare) then
253                         rostermanager.roster_push(node, host, to_bare);
254                         core_route_stanza(origin, stanza);
255                 end
256         end
257         stanza.attr.from, stanza.attr.to = st_from, st_to;
258 end
259
260 function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare)
261         local node, host = jid_split(to_bare);
262         local st_from, st_to = stanza.attr.from, stanza.attr.to;
263         stanza.attr.from, stanza.attr.to = from_bare, to_bare;
264         if stanza.attr.type == "probe" then
265                 log("debug", "inbound probe from "..from_bare.." for "..to_bare);
266                 if rostermanager.is_contact_subscribed(node, host, from_bare) then
267                         if 0 == send_presence_of_available_resources(node, host, from_bare, origin) then
268                                 -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)
269                         end
270                 else
271                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));
272                 end
273         elseif stanza.attr.type == "subscribe" then
274                 log("debug", "inbound subscribe from "..from_bare.." for "..to_bare);
275                 if rostermanager.is_contact_subscribed(node, host, from_bare) then
276                         core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed
277                 else
278                         if not rostermanager.is_contact_pending_in(node, host, from_bare) then
279                                 if rostermanager.set_contact_pending_in(node, host, from_bare) then
280                                         sessionmanager.send_to_available_resources(node, host, stanza);
281                                 end -- TODO else return error, unable to save
282                         end
283                 end
284         elseif stanza.attr.type == "unsubscribe" then
285                 log("debug", "inbound unsubscribe from "..from_bare.." for "..to_bare);
286                 if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then
287                         rostermanager.roster_push(node, host, from_bare);
288                 end
289         elseif stanza.attr.type == "subscribed" then
290                 log("debug", "inbound subscribed from "..from_bare.." for "..to_bare);
291                 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
292                         rostermanager.roster_push(node, host, from_bare);
293                 end
294         elseif stanza.attr.type == "unsubscribed" then
295                 log("debug", "inbound unsubscribed from "..from_bare.." for "..to_bare);
296                 if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then
297                         rostermanager.roster_push(node, host, from_bare);
298                 end
299         end -- discard any other type
300         stanza.attr.from, stanza.attr.to = st_from, st_to;
301 end
302
303 function core_route_stanza(origin, stanza)
304         -- Hooks
305         --- ...later
306
307         -- Deliver
308         local to = stanza.attr.to;
309         local node, host, resource = jid_split(to);
310         local to_bare = node and (node.."@"..host) or host; -- bare JID
311         local from = stanza.attr.from;
312         local from_node, from_host, from_resource = jid_split(from);
313         local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
314
315         if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
316
317         local host_session = hosts[host]
318         if host_session and host_session.type == "local" then
319                 -- Local host
320                 local user = host_session.sessions[node];
321                 if user then
322                         local res = user.sessions[resource];
323                         if not res then
324                                 -- if we get here, resource was not specified or was unavailable
325                                 if stanza.name == "presence" then
326                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
327                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
328                                         else -- sender is available or unavailable
329                                                 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
330                                                         if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
331                                                                 stanza.attr.to = session.full_jid; -- reset at the end of function
332                                                                 session.send(stanza);
333                                                         end
334                                                 end
335                                         end
336                                 elseif stanza.name == "message" then -- select a resource to recieve message
337                                         local priority = 0;
338                                         local recipients = {};
339                                         for _, session in pairs(user.sessions) do -- find resource with greatest priority
340                                                 local p = session.priority;
341                                                 if p > priority then
342                                                         priority = p;
343                                                         recipients = {session};
344                                                 elseif p == priority then
345                                                         t_insert(recipients, session);
346                                                 end
347                                         end
348                                         local count = 0;
349                                         for _, session in pairs(recipients) do
350                                                 session.send(stanza);
351                                                 count = count + 1;
352                                         end
353                                         if count == 0 then
354                                                 offlinemanager.store(node, host, stanza);
355                                                 -- TODO deal with storage errors
356                                         end
357                                 else
358                                         -- TODO send IQ error
359                                 end
360                         else
361                                 -- User + resource is online...
362                                 stanza.attr.to = res.full_jid; -- reset at the end of function
363                                 res.send(stanza); -- Yay \o/
364                         end
365                 else
366                         -- user not online
367                         if user_exists(node, host) then
368                                 if stanza.name == "presence" then
369                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
370                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare);
371                                         else
372                                                 -- TODO send unavailable presence or unsubscribed
373                                         end
374                                 elseif stanza.name == "message" then
375                                         if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
376                                                 offlinemanager.store(node, host, stanza);
377                                                 -- FIXME don't store messages with only chat state notifications
378                                         end
379                                         -- TODO allow configuration of offline storage
380                                         -- TODO send error if not storing offline
381                                 elseif stanza.name == "iq" then
382                                         -- TODO send IQ error
383                                 end
384                         else -- user does not exist
385                                 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
386                                 if stanza.name == "presence" then
387                                         if stanza.attr.type == "probe" then
388                                                 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
389                                         end
390                                         -- else ignore
391                                 else
392                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
393                                 end
394                         end
395                 end
396         elseif origin.type == "c2s" then
397                 -- Remote host
398                 local xmlns = stanza.attr.xmlns;
399                 --stanza.attr.xmlns = "jabber:server";
400                 stanza.attr.xmlns = nil;
401                 log("debug", "sending s2s stanza: %s", tostring(stanza));
402                 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
403                 stanza.attr.xmlns = xmlns; -- reset
404         elseif origin.type == "component" or origin.type == "local" then
405                 -- Route via s2s for components and modules
406                 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
407                 for k,v in pairs(origin) do print("origin:", tostring(k), tostring(v)); end
408                 print(tostring(host), tostring(from_host))
409                 send_s2s(origin.host, host, stanza);
410         else
411                 log("warn", "received stanza from unhandled connection type: %s", origin.type);
412         end
413         stanza.attr.to = to; -- reset
414 end
415
416 function handle_stanza_toremote(stanza)
417         log("error", "Stanza bound for remote host, but s2s is not implemented");
418 end
419
420