stanza_router: Don't log full stanzas destined for s2s
[prosody.git] / core / sessionmanager.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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
10
11 local tonumber, tostring = tonumber, tostring;
12 local ipairs, pairs, print, next= ipairs, pairs, print, next;
13 local collectgarbage = collectgarbage;
14 local format = import("string", "format");
15
16 local hosts = hosts;
17 local full_sessions = full_sessions;
18 local bare_sessions = bare_sessions;
19
20 local modulemanager = require "core.modulemanager";
21 local logger = require "util.logger";
22 local log = logger.init("sessionmanager");
23 local error = error;
24 local uuid_generate = require "util.uuid".generate;
25 local rm_load_roster = require "core.rostermanager".load_roster;
26 local config_get = require "core.configmanager".get;
27 local nameprep = require "util.encodings".stringprep.nameprep;
28
29 local fire_event = require "core.eventmanager".fire_event;
30 local add_task = require "util.timer".add_task;
31 local gettime = require "socket".gettime;
32
33 local st = require "util.stanza";
34
35 local c2s_timeout = config_get("*", "core", "c2s_timeout");
36
37 local newproxy = newproxy;
38 local getmetatable = getmetatable;
39
40 module "sessionmanager"
41
42 local open_sessions = 0;
43
44 function new_session(conn)
45         local session = { conn = conn, type = "c2s_unauthed", conntime = gettime() };
46         if true then
47                 session.trace = newproxy(true);
48                 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
49         end
50         open_sessions = open_sessions + 1;
51         log("debug", "open sessions now: ".. open_sessions);
52         local w = conn.write;
53         session.send = function (t) w(tostring(t)); end
54         session.ip = conn.ip();
55         local conn_name = "c2s"..tostring(conn):match("[a-f0-9]+$");
56         session.log = logger.init(conn_name);
57         
58         if c2s_timeout then
59                 add_task(c2s_timeout, function ()
60                         if session.type == "c2s_unauthed" then
61                                 session:close("connection-timeout");
62                         end
63                 end);
64         end
65                 
66         return session;
67 end
68
69 function destroy_session(session, err)
70         (session.log or log)("info", "Destroying session for %s (%s@%s)", session.full_jid or "(unknown)", session.username or "(unknown)", session.host or "(unknown)");
71         
72         -- Remove session/resource from user's session list
73         if session.full_jid then
74                 hosts[session.host].events.fire_event("resource-unbind", {session=session, error=err});
75
76                 hosts[session.host].sessions[session.username].sessions[session.resource] = nil;
77                 full_sessions[session.full_jid] = nil;
78                         
79                 if not next(hosts[session.host].sessions[session.username].sessions) then
80                         log("debug", "All resources of %s are now offline", session.username);
81                         hosts[session.host].sessions[session.username] = nil;
82                         bare_sessions[session.username..'@'..session.host] = nil;
83                 end
84         end
85         
86         for k in pairs(session) do
87                 if k ~= "trace" then
88                         session[k] = nil;
89                 end
90         end
91 end
92
93 function make_authenticated(session, username)
94         session.username = username;
95         if session.type == "c2s_unauthed" then
96                 session.type = "c2s";
97         end
98         session.log("info", "Authenticated as %s@%s", username or "(unknown)", session.host or "(unknown)");
99         return true;
100 end
101
102 -- returns true, nil on success
103 -- returns nil, err_type, err, err_message on failure
104 function bind_resource(session, resource)
105         if not session.username then return nil, "auth", "not-authorized", "Cannot bind resource before authentication"; end
106         if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end
107         -- We don't support binding multiple resources
108
109         resource = resource or uuid_generate();
110         --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing
111         
112         if not hosts[session.host].sessions[session.username] then
113                 local sessions = { sessions = {} };
114                 hosts[session.host].sessions[session.username] = sessions;
115                 bare_sessions[session.username..'@'..session.host] = sessions;
116         else
117                 local sessions = hosts[session.host].sessions[session.username].sessions;
118                 local limit = config_get(session.host, "core", "max_resources") or 10;
119                 if #sessions >= limit then
120                         return nil, "cancel", "conflict", "Resource limit reached; only "..limit.." resources allowed";
121                 end
122                 if sessions[resource] then
123                         -- Resource conflict
124                         local policy = config_get(session.host, "core", "conflict_resolve");
125                         local increment;
126                         if policy == "random" then
127                                 resource = uuid_generate();
128                                 increment = true;
129                         elseif policy == "increment" then
130                                 increment = true; -- TODO ping old resource
131                         elseif policy == "kick_new" then
132                                 return nil, "cancel", "conflict", "Resource already exists";
133                         else -- if policy == "kick_old" then
134                                 sessions[resource]:close {
135                                         condition = "conflict";
136                                         text = "Replaced by new connection";
137                                 };
138                                 if not next(sessions) then
139                                         hosts[session.host].sessions[session.username] = { sessions = sessions };
140                                         bare_sessions[session.username.."@"..session.host] = hosts[session.host].sessions[session.username];
141                                 end
142                         end
143                         if increment and sessions[resource] then
144                                 local count = 1;
145                                 while sessions[resource.."#"..count] do
146                                         count = count + 1;
147                                 end
148                                 resource = resource.."#"..count;
149                         end
150                 end
151         end
152         
153         session.resource = resource;
154         session.full_jid = session.username .. '@' .. session.host .. '/' .. resource;
155         hosts[session.host].sessions[session.username].sessions[resource] = session;
156         full_sessions[session.full_jid] = session;
157         
158         session.roster = rm_load_roster(session.username, session.host);
159         
160         hosts[session.host].events.fire_event("resource-bind", {session=session});
161         
162         return true;
163 end
164
165 function streamopened(session, attr)
166         local send = session.send;
167         session.host = attr.to or error("Client failed to specify destination hostname");
168         session.host = nameprep(session.host);
169         session.version = tonumber(attr.version) or 0;
170         session.streamid = uuid_generate();
171         (session.log or session)("debug", "Client sent opening <stream:stream> to %s", session.host);
172
173         if not hosts[session.host] then
174                 -- We don't serve this host...
175                 session:close{ condition = "host-unknown", text = "This server does not serve "..tostring(session.host)};
176                 return;
177         end
178
179         send("<?xml version='1.0'?>");
180         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));
181
182         (session.log or log)("debug", "Sent reply <stream:stream> to client");
183         session.notopen = nil;
184
185         -- If session.secure is *false* (not nil) then it means we /were/ encrypting
186         -- since we now have a new stream header, session is secured
187         if session.secure == false then
188                 session.secure = true;
189         end
190
191         local features = st.stanza("stream:features");
192         fire_event("stream-features", session, features);
193
194         send(features);
195
196 end
197
198 function streamclosed(session)
199         session.send("</stream:stream>");
200         session.notopen = true;
201 end
202
203 function send_to_available_resources(user, host, stanza)
204         local count = 0;
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];
210                 if u then
211                         for k, session in pairs(u.sessions) do
212                                 if session.presence then
213                                         session.send(stanza);
214                                         count = count + 1;
215                                 end
216                         end
217                 end
218         end
219         stanza.attr.to = to;
220         return count;
221 end
222
223 return _M;