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