luacheckrc: Ignore assert helper functions used in tests/*
[prosody.git] / plugins / mod_bosh.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 module:set_global(); -- Global module
10
11 local hosts = _G.hosts;
12 local new_xmpp_stream = require "util.xmppstream".new;
13 local sm = require "core.sessionmanager";
14 local sm_destroy_session = sm.destroy_session;
15 local new_uuid = require "util.uuid".generate;
16 local core_process_stanza = prosody.core_process_stanza;
17 local st = require "util.stanza";
18 local logger = require "util.logger";
19 local log = logger.init("mod_bosh");
20 local initialize_filters = require "util.filters".initialize;
21 local math_min = math.min;
22 local xpcall, tostring, type = xpcall, tostring, type;
23 local traceback = debug.traceback;
24 local nameprep = require "util.encodings".stringprep.nameprep;
25
26 local xmlns_streams = "http://etherx.jabber.org/streams";
27 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
28 local xmlns_bosh = "http://jabber.org/protocol/httpbind"; -- (hard-coded into a literal in session.send)
29
30 local stream_callbacks = {
31         stream_ns = xmlns_bosh, stream_tag = "body", default_ns = "jabber:client" };
32
33 local BOSH_DEFAULT_HOLD = module:get_option_number("bosh_default_hold", 1);
34 local BOSH_DEFAULT_INACTIVITY = module:get_option_number("bosh_max_inactivity", 60);
35 local BOSH_DEFAULT_POLLING = module:get_option_number("bosh_max_polling", 5);
36 local BOSH_DEFAULT_REQUESTS = module:get_option_number("bosh_max_requests", 2);
37 local bosh_max_wait = module:get_option_number("bosh_max_wait", 120);
38
39 local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure");
40 local cross_domain = module:get_option("cross_domain_bosh", false);
41
42 if cross_domain == true then cross_domain = "*"; end
43 if type(cross_domain) == "table" then cross_domain = table.concat(cross_domain, ", "); end
44
45 local trusted_proxies = module:get_option_set("trusted_proxies", {"127.0.0.1"})._items;
46
47 local function get_ip_from_request(request)
48         local ip = request.conn:ip();
49         local forwarded_for = request.headers.x_forwarded_for;
50         if forwarded_for then
51                 forwarded_for = forwarded_for..", "..ip;
52                 for forwarded_ip in forwarded_for:gmatch("[^%s,]+") do
53                         if not trusted_proxies[forwarded_ip] then
54                                 ip = forwarded_ip;
55                         end
56                 end
57         end
58         return ip;
59 end
60
61 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
62 local os_time = os.time;
63
64 -- All sessions, and sessions that have no requests open
65 local sessions, inactive_sessions = module:shared("sessions", "inactive_sessions");
66
67 -- Used to respond to idle sessions (those with waiting requests)
68 local waiting_requests = module:shared("waiting_requests");
69 function on_destroy_request(request)
70         log("debug", "Request destroyed: %s", tostring(request));
71         waiting_requests[request] = nil;
72         local session = sessions[request.context.sid];
73         if session then
74                 local requests = session.requests;
75                 for i, r in ipairs(requests) do
76                         if r == request then
77                                 t_remove(requests, i);
78                                 break;
79                         end
80                 end
81
82                 -- If this session now has no requests open, mark it as inactive
83                 local max_inactive = session.bosh_max_inactive;
84                 if max_inactive and #requests == 0 then
85                         inactive_sessions[session] = os_time() + max_inactive;
86                         (session.log or log)("debug", "BOSH session marked as inactive (for %ds)", max_inactive);
87                 end
88         end
89 end
90
91 local function set_cross_domain_headers(response)
92         local headers = response.headers;
93         headers.access_control_allow_methods = "GET, POST, OPTIONS";
94         headers.access_control_allow_headers = "Content-Type";
95         headers.access_control_max_age = "7200";
96         headers.access_control_allow_origin = cross_domain;
97         return response;
98 end
99
100 function handle_OPTIONS(event)
101         if cross_domain and event.request.headers.origin then
102                 set_cross_domain_headers(event.response);
103         end
104         return "";
105 end
106
107 function handle_POST(event)
108         log("debug", "Handling new request %s: %s\n----------", tostring(event.request), tostring(event.request.body));
109
110         local request, response = event.request, event.response;
111         response.on_destroy = on_destroy_request;
112         local body = request.body;
113
114         local context = { request = request, response = response, notopen = true };
115         local stream = new_xmpp_stream(context, stream_callbacks);
116         response.context = context;
117
118         local headers = response.headers;
119         headers.content_type = "text/xml; charset=utf-8";
120
121         if cross_domain and event.request.headers.origin then
122                 set_cross_domain_headers(response);
123         end
124
125         -- stream:feed() calls the stream_callbacks, so all stanzas in
126         -- the body are processed in this next line before it returns.
127         -- In particular, the streamopened() stream callback is where
128         -- much of the session logic happens, because it's where we first
129         -- get to see the 'sid' of this request.
130         local ok, err = stream:feed(body);
131         if not ok then
132                 module:log("warn", "Error parsing BOSH payload; %s", err)
133                 local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
134                         ["xmlns:stream"] = xmlns_streams, condition = "bad-request" });
135                 return tostring(close_reply);
136         end
137
138         -- Stanzas (if any) in the request have now been processed, and
139         -- we take care of the high-level BOSH logic here, including
140         -- giving a response or putting the request "on hold".
141         local session = sessions[context.sid];
142         if session then
143                 -- Session was marked as inactive, since we have
144                 -- a request open now, unmark it
145                 if inactive_sessions[session] and #session.requests > 0 then
146                         inactive_sessions[session] = nil;
147                 end
148
149                 local r = session.requests;
150                 log("debug", "Session %s has %d out of %d requests open", context.sid, #r, session.bosh_hold);
151                 log("debug", "and there are %d things in the send_buffer:", #session.send_buffer);
152                 if #r > session.bosh_hold then
153                         -- We are holding too many requests, send what's in the buffer,
154                         log("debug", "We are holding too many requests, so...");
155                         if #session.send_buffer > 0 then
156                                 log("debug", "...sending what is in the buffer")
157                                 session.send(t_concat(session.send_buffer));
158                                 session.send_buffer = {};
159                         else
160                                 -- or an empty response
161                                 log("debug", "...sending an empty response");
162                                 session.send("");
163                         end
164                 elseif #session.send_buffer > 0 then
165                         log("debug", "Session has data in the send buffer, will send now..");
166                         local resp = t_concat(session.send_buffer);
167                         session.send_buffer = {};
168                         session.send(resp);
169                 end
170
171                 if not response.finished then
172                         -- We're keeping this request open, to respond later
173                         log("debug", "Have nothing to say, so leaving request unanswered for now");
174                         if session.bosh_wait then
175                                 waiting_requests[response] = os_time() + session.bosh_wait;
176                         end
177                 end
178
179                 if session.bosh_terminate then
180                         session.log("debug", "Closing session with %d requests open", #session.requests);
181                         session:close();
182                         return nil;
183                 else
184                         return true; -- Inform http server we shall reply later
185                 end
186         elseif response.finished then
187                 return; -- A response has been sent already
188         end
189         module:log("warn", "Unable to associate request with a session (incomplete request?)");
190         local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
191                 ["xmlns:stream"] = xmlns_streams, condition = "item-not-found" });
192         return tostring(close_reply) .. "\n";
193 end
194
195
196 local function bosh_reset_stream(session) session.notopen = true; end
197
198 local stream_xmlns_attr = { xmlns = "urn:ietf:params:xml:ns:xmpp-streams" };
199
200 local function bosh_close_stream(session, reason)
201         (session.log or log)("info", "BOSH client disconnected");
202
203         local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
204                 ["xmlns:stream"] = xmlns_streams });
205
206
207         if reason then
208                 close_reply.attr.condition = "remote-stream-error";
209                 if type(reason) == "string" then -- assume stream error
210                         close_reply:tag("stream:error")
211                                 :tag(reason, {xmlns = xmlns_xmpp_streams});
212                 elseif type(reason) == "table" then
213                         if reason.condition then
214                                 close_reply:tag("stream:error")
215                                         :tag(reason.condition, stream_xmlns_attr):up();
216                                 if reason.text then
217                                         close_reply:tag("text", stream_xmlns_attr):text(reason.text):up();
218                                 end
219                                 if reason.extra then
220                                         close_reply:add_child(reason.extra);
221                                 end
222                         elseif reason.name then -- a stanza
223                                 close_reply = reason;
224                         end
225                 end
226                 log("info", "Disconnecting client, <stream:error> is: %s", tostring(close_reply));
227         end
228
229         local response_body = tostring(close_reply);
230         for _, held_request in ipairs(session.requests) do
231                 held_request:send(response_body);
232         end
233         sessions[session.sid] = nil;
234         inactive_sessions[session] = nil;
235         sm_destroy_session(session);
236 end
237
238 -- Handle the <body> tag in the request payload.
239 function stream_callbacks.streamopened(context, attr)
240         local request, response = context.request, context.response;
241         local sid = attr.sid;
242         log("debug", "BOSH body open (sid: %s)", sid or "<none>");
243         if not sid then
244                 -- New session request
245                 context.notopen = nil; -- Signals that we accept this opening tag
246
247                 local to_host = nameprep(attr.to);
248                 local rid = tonumber(attr.rid);
249                 local wait = tonumber(attr.wait);
250                 if not to_host then
251                         log("debug", "BOSH client tried to connect to invalid host: %s", tostring(attr.to));
252                         local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
253                                 ["xmlns:stream"] = xmlns_streams, condition = "improper-addressing" });
254                         response:send(tostring(close_reply));
255                         return;
256                 elseif not hosts[to_host] then
257                         -- Unknown host
258                         log("debug", "BOSH client tried to connect to unknown host: %s", tostring(attr.to));
259                         local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
260                                 ["xmlns:stream"] = xmlns_streams, condition = "host-unknown" });
261                         response:send(tostring(close_reply));
262                         return;
263                 end
264                 if not rid or (not wait and attr.wait or wait < 0 or wait % 1 ~= 0) then
265                         log("debug", "BOSH client sent invalid rid or wait attributes: rid=%s, wait=%s", tostring(attr.rid), tostring(attr.wait));
266                         local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
267                                 ["xmlns:stream"] = xmlns_streams, condition = "bad-request" });
268                         response:send(tostring(close_reply));
269                         return;
270                 end
271
272                 rid = rid - 1;
273                 wait = math_min(wait, bosh_max_wait);
274
275                 -- New session
276                 sid = new_uuid();
277                 local session = {
278                         type = "c2s_unauthed", conn = {}, sid = sid, rid = rid, host = attr.to,
279                         bosh_version = attr.ver, bosh_wait = wait, streamid = sid,
280                         bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY,
281                         requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream,
282                         close = bosh_close_stream, dispatch_stanza = core_process_stanza, notopen = true,
283                         log = logger.init("bosh"..sid), secure = consider_bosh_secure or request.secure,
284                         ip = get_ip_from_request(request);
285                 };
286                 sessions[sid] = session;
287
288                 local filter = initialize_filters(session);
289
290                 session.log("debug", "BOSH session created for request from %s", session.ip);
291                 log("info", "New BOSH session, assigned it sid '%s'", sid);
292
293                 hosts[session.host].events.fire_event("bosh-session", { session = session, request = request });
294
295                 -- Send creation response
296                 local creating_session = true;
297
298                 local r = session.requests;
299                 function session.send(s)
300                         -- We need to ensure that outgoing stanzas have the jabber:client xmlns
301                         if s.attr and not s.attr.xmlns then
302                                 s = st.clone(s);
303                                 s.attr.xmlns = "jabber:client";
304                         end
305                         s = filter("stanzas/out", s);
306                         --log("debug", "Sending BOSH data: %s", tostring(s));
307                         if not s then return true end
308                         t_insert(session.send_buffer, tostring(s));
309
310                         local oldest_request = r[1];
311                         if oldest_request and not session.bosh_processing then
312                                 log("debug", "We have an open request, so sending on that");
313                                 local body_attr = { xmlns = "http://jabber.org/protocol/httpbind",
314                                         ["xmlns:stream"] = "http://etherx.jabber.org/streams";
315                                         type = session.bosh_terminate and "terminate" or nil;
316                                         sid = sid;
317                                 };
318                                 if creating_session then
319                                         creating_session = nil;
320                                         body_attr.inactivity = tostring(BOSH_DEFAULT_INACTIVITY);
321                                         body_attr.polling = tostring(BOSH_DEFAULT_POLLING);
322                                         body_attr.requests = tostring(BOSH_DEFAULT_REQUESTS);
323                                         body_attr.wait = tostring(session.bosh_wait);
324                                         body_attr.hold = tostring(session.bosh_hold);
325                                         body_attr.authid = sid;
326                                         body_attr.secure = "true";
327                                         body_attr.ver  = '1.6';
328                                         body_attr.from = session.host;
329                                         body_attr["xmlns:xmpp"] = "urn:xmpp:xbosh";
330                                         body_attr["xmpp:version"] = "1.0";
331                                 end
332                                 oldest_request:send(st.stanza("body", body_attr):top_tag()..t_concat(session.send_buffer).."</body>");
333                                 session.send_buffer = {};
334                         end
335                         return true;
336                 end
337                 request.sid = sid;
338         end
339
340         local session = sessions[sid];
341         if not session then
342                 -- Unknown sid
343                 log("info", "Client tried to use sid '%s' which we don't know about", sid);
344                 response:send(tostring(st.stanza("body", { xmlns = xmlns_bosh, type = "terminate", condition = "item-not-found" })));
345                 context.notopen = nil;
346                 return;
347         end
348
349         if session.rid then
350                 local rid = tonumber(attr.rid);
351                 local diff = rid - session.rid;
352                 if diff > 1 then
353                         session.log("warn", "rid too large (means a request was lost). Last rid: %d New rid: %s", session.rid, attr.rid);
354                 elseif diff <= 0 then
355                         -- Repeated, ignore
356                         session.log("debug", "rid repeated, ignoring: %s (diff %d)", session.rid, diff);
357                         context.notopen = nil;
358                         context.ignore = true;
359                         context.sid = sid;
360                         t_insert(session.requests, response);
361                         return;
362                 end
363                 session.rid = rid;
364         end
365
366         if attr.type == "terminate" then
367                 -- Client wants to end this session, which we'll do
368                 -- after processing any stanzas in this request
369                 session.bosh_terminate = true;
370         end
371
372         context.notopen = nil; -- Signals that we accept this opening tag
373         t_insert(session.requests, response);
374         context.sid = sid;
375         session.bosh_processing = true; -- Used to suppress replies until processing of this request is done
376
377         if session.notopen then
378                 local features = st.stanza("stream:features");
379                 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
380                 session.send(features);
381                 session.notopen = nil;
382         end
383 end
384
385 local function handleerr(err) log("error", "Traceback[bosh]: %s", traceback(tostring(err), 2)); end
386 function stream_callbacks.handlestanza(context, stanza)
387         if context.ignore then return; end
388         log("debug", "BOSH stanza received: %s\n", stanza:top_tag());
389         local session = sessions[context.sid];
390         if session then
391                 if stanza.attr.xmlns == xmlns_bosh then
392                         stanza.attr.xmlns = nil;
393                 end
394                 stanza = session.filter("stanzas/in", stanza);
395                 if stanza then
396                         return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
397                 end
398         end
399 end
400
401 function stream_callbacks.streamclosed(context)
402         local session = sessions[context.sid];
403         if session then
404                 session.bosh_processing = false;
405                 if #session.send_buffer > 0 then
406                         session.send("");
407                 end
408         end
409 end
410
411 function stream_callbacks.error(context, error)
412         log("debug", "Error parsing BOSH request payload; %s", error);
413         if not context.sid then
414                 local response = context.response;
415                 local close_reply = st.stanza("body", { xmlns = xmlns_bosh, type = "terminate",
416                         ["xmlns:stream"] = xmlns_streams, condition = "bad-request" });
417                 response:send(tostring(close_reply));
418                 return;
419         end
420
421         local session = sessions[context.sid];
422         if error == "stream-error" then -- Remote stream error, we close normally
423                 session:close();
424         else
425                 session:close({ condition = "bad-format", text = "Error processing stream" });
426         end
427 end
428
429 local dead_sessions = module:shared("dead_sessions");
430 function on_timer()
431         -- log("debug", "Checking for requests soon to timeout...");
432         -- Identify requests timing out within the next few seconds
433         local now = os_time() + 3;
434         for request, reply_before in pairs(waiting_requests) do
435                 if reply_before <= now then
436                         log("debug", "%s was soon to timeout (at %d, now %d), sending empty response", tostring(request), reply_before, now);
437                         -- Send empty response to let the
438                         -- client know we're still here
439                         if request.conn then
440                                 sessions[request.context.sid].send("");
441                         end
442                 end
443         end
444
445         now = now - 3;
446         local n_dead_sessions = 0;
447         for session, close_after in pairs(inactive_sessions) do
448                 if close_after < now then
449                         (session.log or log)("debug", "BOSH client inactive too long, destroying session at %d", now);
450                         sessions[session.sid]  = nil;
451                         inactive_sessions[session] = nil;
452                         n_dead_sessions = n_dead_sessions + 1;
453                         dead_sessions[n_dead_sessions] = session;
454                 end
455         end
456
457         for i=1,n_dead_sessions do
458                 local session = dead_sessions[i];
459                 dead_sessions[i] = nil;
460                 sm_destroy_session(session, "BOSH client silent for over "..session.bosh_max_inactive.." seconds");
461         end
462         return 1;
463 end
464 module:add_timer(1, on_timer);
465
466
467 local GET_response = {
468         headers = {
469                 content_type = "text/html";
470         };
471         body = [[<html><body>
472         <p>It works! Now point your BOSH client to this URL to connect to Prosody.</p>
473         <p>For more information see <a href="http://prosody.im/doc/setting_up_bosh">Prosody: Setting up BOSH</a>.</p>
474         </body></html>]];
475 };
476
477 function module.add_host(module)
478         module:depends("http");
479         module:provides("http", {
480                 default_path = "/http-bind";
481                 route = {
482                         ["GET"] = GET_response;
483                         ["GET /"] = GET_response;
484                         ["OPTIONS"] = handle_OPTIONS;
485                         ["OPTIONS /"] = handle_OPTIONS;
486                         ["POST"] = handle_POST;
487                         ["POST /"] = handle_POST;
488                 };
489         });
490 end