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