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