2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local add_task = require "util.timer".add_task;
12 local new_xmpp_stream = require "util.xmppstream".new;
13 local nameprep = require "util.encodings".stringprep.nameprep;
14 local portmanager = require "core.portmanager";
15 local sessionmanager = require "core.sessionmanager";
16 local st = require "util.stanza";
17 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
18 local uuid_generate = require "util.uuid".generate;
20 local xpcall, tostring, type = xpcall, tostring, type;
21 local format = string.format;
22 local traceback = debug.traceback;
24 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
26 local log = module._log;
28 local c2s_timeout = module:get_option_number("c2s_timeout");
29 local opt_keepalives = module:get_option_boolean("tcp_keepalives", false);
31 local sessions = module:shared("sessions");
33 local stream_callbacks = { default_ns = "jabber:client", handlestanza = core_process_stanza };
36 --- Stream events handlers
37 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
38 local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" };
40 function stream_callbacks.streamopened(session, attr)
41 local send = session.send;
42 session.host = nameprep(attr.to);
43 if not session.host then
44 session:close{ condition = "improper-addressing",
45 text = "A valid 'to' attribute is required on stream headers" };
48 session.version = tonumber(attr.version) or 0;
49 session.streamid = uuid_generate();
50 (session.log or session)("debug", "Client sent opening <stream:stream> to %s", session.host);
52 if not hosts[session.host] then
53 -- We don't serve this host...
54 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)};
58 send("<?xml version='1.0'?>");
59 send(format("<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s' version='1.0' xml:lang='en'>", session.streamid, session.host));
61 (session.log or log)("debug", "Sent reply <stream:stream> to client");
62 session.notopen = nil;
64 -- If session.secure is *false* (not nil) then it means we /were/ encrypting
65 -- since we now have a new stream header, session is secured
66 if session.secure == false then
67 session.secure = true;
70 local features = st.stanza("stream:features");
71 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
72 module:fire_event("stream-features", session, features);
77 function stream_callbacks.streamclosed(session)
78 session.log("debug", "Received </stream:stream>");
82 function stream_callbacks.error(session, error, data)
83 if error == "no-stream" then
84 session.log("debug", "Invalid opening stream header");
85 session:close("invalid-namespace");
86 elseif error == "parse-error" then
87 (session.log or log)("debug", "Client XML parse error: %s", tostring(data));
88 session:close("not-well-formed");
89 elseif error == "stream-error" then
90 local condition, text = "undefined-condition";
91 for child in data:children() do
92 if child.attr.xmlns == xmlns_xmpp_streams then
93 if child.name ~= "text" then
94 condition = child.name;
96 text = child:get_text();
98 if condition ~= "undefined-condition" and text then
103 text = condition .. (text and (" ("..text..")") or "");
104 session.log("info", "Session closed by remote with error: %s", text);
105 session:close(nil, text);
109 local function handleerr(err) log("error", "Traceback[c2s]: %s: %s", tostring(err), traceback()); end
110 function stream_callbacks.handlestanza(session, stanza)
111 stanza = session.filter("stanzas/in", stanza);
113 return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
118 local function session_close(session, reason)
119 local log = session.log or log;
121 if session.notopen then
122 session.send("<?xml version='1.0'?>");
123 session.send(st.stanza("stream:stream", default_stream_attr):top_tag());
126 if type(reason) == "string" then -- assume stream error
127 log("info", "Disconnecting client, <stream:error> is: %s", reason);
128 session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }));
129 elseif type(reason) == "table" then
130 if reason.condition then
131 local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up();
133 stanza:tag("text", stream_xmlns_attr):text(reason.text):up();
136 stanza:add_child(reason.extra);
138 log("info", "Disconnecting client, <stream:error> is: %s", tostring(stanza));
139 session.send(stanza);
140 elseif reason.name then -- a stanza
141 log("info", "Disconnecting client, <stream:error> is: %s", tostring(reason));
142 session.send(reason);
146 session.send("</stream:stream>");
147 session.conn:close();
148 listener.ondisconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed");
153 function listener.onconnect(conn)
154 local session = sm_new_session(conn);
155 sessions[conn] = session;
157 session.log("info", "Client connected");
159 -- Client is using legacy SSL (otherwise mod_tls sets this flag)
161 session.secure = true;
164 if opt_keepalives then
165 conn:setoption("keepalive", opt_keepalives);
168 session.close = session_close;
170 local stream = new_xmpp_stream(session, stream_callbacks);
171 session.stream = stream;
172 session.notopen = true;
174 function session.reset_stream()
175 session.notopen = true;
176 session.stream:reset();
179 local filter = session.filter;
180 function session.data(data)
181 data = filter("bytes/in", data);
183 local ok, err = stream:feed(data);
184 if ok then return; end
185 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
186 session:close("not-well-formed");
192 add_task(c2s_timeout, function ()
193 if session.type == "c2s_unauthed" then
194 session:close("connection-timeout");
199 session.dispatch_stanza = stream_callbacks.handlestanza;
202 function listener.onincoming(conn, data)
203 local session = sessions[conn];
209 function listener.ondisconnect(conn, err)
210 local session = sessions[conn];
212 (session.log or log)("info", "Client disconnected: %s", err);
213 sm_destroy_session(session, err);
214 sessions[conn] = nil;
219 function listener.associate_session(conn, session)
220 sessions[conn] = session;
223 module:add_item("net-provider", {
227 encryption = "starttls";
229 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:client%1.*>";
233 module:add_item("net-provider", {
238 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:client%1.*>";