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