2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 local tonumber, tostring = tonumber, tostring;
12 local ipairs, pairs, print, next= ipairs, pairs, print, next;
13 local collectgarbage = collectgarbage;
14 local m_random = import("math", "random");
15 local format = import("string", "format");
18 local full_sessions = full_sessions;
19 local bare_sessions = bare_sessions;
21 local modulemanager = require "core.modulemanager";
22 local logger = require "util.logger";
23 local log = logger.init("sessionmanager");
25 local uuid_generate = require "util.uuid".generate;
26 local rm_load_roster = require "core.rostermanager".load_roster;
27 local config_get = require "core.configmanager".get;
28 local nameprep = require "util.encodings".stringprep.nameprep;
30 local fire_event = require "core.eventmanager".fire_event;
31 local add_task = require "util.timer".add_task;
32 local gettime = require "socket".gettime;
34 local st = require "util.stanza";
36 local c2s_timeout = config_get("*", "core", "c2s_timeout");
38 local newproxy = newproxy;
39 local getmetatable = getmetatable;
41 module "sessionmanager"
43 local open_sessions = 0;
45 function new_session(conn)
46 local session = { conn = conn, type = "c2s_unauthed", conntime = gettime() };
48 session.trace = newproxy(true);
49 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
51 open_sessions = open_sessions + 1;
52 log("debug", "open sessions now: ".. open_sessions);
54 session.send = function (t) w(tostring(t)); end
55 session.ip = conn.ip();
56 local conn_name = "c2s"..tostring(conn):match("[a-f0-9]+$");
57 session.log = logger.init(conn_name);
60 add_task(c2s_timeout, function ()
61 if session.type == "c2s_unauthed" then
62 session:close("connection-timeout");
70 function destroy_session(session, err)
71 (session.log or log)("info", "Destroying session for %s (%s@%s)", session.full_jid or "(unknown)", session.username or "(unknown)", session.host or "(unknown)");
73 -- Remove session/resource from user's session list
74 if session.full_jid then
75 hosts[session.host].events.fire_event("resource-unbind", {session=session, error=err});
77 hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
78 full_sessions[session.full_jid] = nil;
80 if not next(hosts[session.host].sessions[session.username].sessions) then
81 log("debug", "All resources of %s are now offline", session.username);
82 hosts[session.host].sessions[session.username] = nil;
83 bare_sessions[session.username..'@'..session.host] = nil;
87 for k in pairs(session) do
94 function make_authenticated(session, username)
95 session.username = username;
96 if session.type == "c2s_unauthed" then
99 session.log("info", "Authenticated as %s@%s", username or "(unknown)", session.host or "(unknown)");
103 -- returns true, nil on success
104 -- returns nil, err_type, err, err_message on failure
105 function bind_resource(session, resource)
106 if not session.username then return nil, "auth", "not-authorized", "Cannot bind resource before authentication"; end
107 if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end
108 -- We don't support binding multiple resources
110 resource = resource or uuid_generate();
111 --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
113 if not hosts[session.host].sessions[session.username] then
114 local sessions = { sessions = {} };
115 hosts[session.host].sessions[session.username] = sessions;
116 bare_sessions[session.username..'@'..session.host] = sessions;
118 local sessions = hosts[session.host].sessions[session.username].sessions;
119 local limit = config_get(session.host, "core", "max_resources") or 10;
120 if #sessions >= limit then
121 return nil, "cancel", "conflict", "Resource limit reached; only "..limit.." resources allowed";
123 if sessions[resource] then
125 local policy = config_get(session.host, "core", "conflict_resolve");
127 if policy == "random" then
128 resource = uuid_generate();
130 elseif policy == "increment" then
131 increment = true; -- TODO ping old resource
132 elseif policy == "kick_new" then
133 return nil, "cancel", "conflict", "Resource already exists";
134 else -- if policy == "kick_old" then
135 sessions[resource]:close {
136 condition = "conflict";
137 text = "Replaced by new connection";
139 if not next(sessions) then
140 hosts[session.host].sessions[session.username] = { sessions = sessions };
141 bare_sessions[session.username.."@"..session.host] = hosts[session.host].sessions[session.username];
144 if increment and sessions[resource] then
146 while sessions[resource.."#"..count] do
149 resource = resource.."#"..count;
154 session.resource = resource;
155 session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
156 hosts[session.host].sessions[session.username].sessions[resource] = session;
157 full_sessions[session.full_jid] = session;
159 session.roster = rm_load_roster(session.username, session.host);
161 hosts[session.host].events.fire_event("resource-bind", {session=session});
166 function streamopened(session, attr)
167 local send = session.send;
168 session.host = attr.to or error("Client failed to specify destination hostname");
169 session.host = nameprep(session.host);
170 session.version = tonumber(attr.version) or 0;
171 session.streamid = m_random(1000000, 99999999);
172 (session.log or session)("debug", "Client sent opening <stream:stream> to %s", session.host);
174 send("<?xml version='1.0'?>");
175 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));
177 if not hosts[session.host] then
178 -- We don't serve this host...
179 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)};
183 -- If session.secure is *false* (not nil) then it means we /were/ encrypting
184 -- since we now have a new stream header, session is secured
185 if session.secure == false then
186 session.secure = true;
189 local features = st.stanza("stream:features");
190 fire_event("stream-features", session, features);
194 (session.log or log)("debug", "Sent reply <stream:stream> to client");
195 session.notopen = nil;
198 function streamclosed(session)
199 session.send("</stream:stream>");
200 session.notopen = true;
203 function send_to_available_resources(user, host, stanza)
205 local to = stanza.attr.to;
206 stanza.attr.to = nil;
207 local h = hosts[host];
208 if h and h.type == "local" then
209 local u = h.sessions[user];
211 for k, session in pairs(u.sessions) do
212 if session.presence then
213 session.send(stanza);