6049e12eb5f8305da2dd3022fbaad008458468c9
[prosody.git] / core / s2smanager.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
10
11 local hosts = hosts;
12 local tostring, pairs, ipairs, getmetatable, newproxy, setmetatable
13     = tostring, pairs, ipairs, getmetatable, newproxy, setmetatable;
14
15 local fire_event = prosody.events.fire_event;
16 local logger_init = require "util.logger".init;
17
18 local log = logger_init("s2smanager");
19
20 local config = require "core.configmanager";
21
22 local prosody = _G.prosody;
23 incoming_s2s = {};
24 prosody.incoming_s2s = incoming_s2s;
25 local incoming_s2s = incoming_s2s;
26
27 module "s2smanager"
28
29 local open_sessions = 0;
30
31 function new_incoming(conn)
32         local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} };
33         if true then
34                 session.trace = newproxy(true);
35                 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end;
36         end
37         open_sessions = open_sessions + 1;
38         session.log = logger_init("s2sin"..tostring(session):match("[a-f0-9]+$"));
39         incoming_s2s[session] = true;
40         return session;
41 end
42
43 function new_outgoing(from_host, to_host, connect)
44         local host_session = { to_host = to_host, from_host = from_host, host = from_host,
45                                notopen = true, type = "s2sout_unauthed", direction = "outgoing" };
46         hosts[from_host].s2sout[to_host] = host_session;
47         local conn_name = "s2sout"..tostring(host_session):match("[a-f0-9]*$");
48         host_session.log = logger_init(conn_name);
49         return host_session;
50 end
51
52 function make_authenticated(session, host)
53         if not session.secure then
54                 local local_host = session.direction == "incoming" and session.to_host or session.from_host;
55                 if config.get(local_host, "core", "s2s_require_encryption") then
56                         session:close({
57                                 condition = "policy-violation",
58                                 text = "Encrypted server-to-server communication is required but was not "
59                                        ..((session.direction == "outgoing" and "offered") or "used")
60                         });
61                 end
62         end
63         if session.type == "s2sout_unauthed" then
64                 session.type = "s2sout";
65         elseif session.type == "s2sin_unauthed" then
66                 session.type = "s2sin";
67                 if host then
68                         if not session.hosts[host] then session.hosts[host] = {}; end
69                         session.hosts[host].authed = true;
70                 end
71         elseif session.type == "s2sin" and host then
72                 if not session.hosts[host] then session.hosts[host] = {}; end
73                 session.hosts[host].authed = true;
74         else
75                 return false;
76         end
77         session.log("debug", "connection %s->%s is now authenticated for %s", session.from_host, session.to_host, host);
78         
79         mark_connected(session);
80         
81         return true;
82 end
83
84 -- Stream is authorised, and ready for normal stanzas
85 function mark_connected(session)
86         local sendq, send = session.sendq, session.sends2s;
87         
88         local from, to = session.from_host, session.to_host;
89         
90         session.log("info", "%s s2s connection %s->%s complete", session.direction, from, to);
91
92         local event_data = { session = session };
93         if session.type == "s2sout" then
94                 prosody.events.fire_event("s2sout-established", event_data);
95                 hosts[from].events.fire_event("s2sout-established", event_data);
96         else
97                 local host_session = hosts[to];
98                 session.send = function(stanza)
99                         return host_session.events.fire_event("route/remote", { from_host = to, to_host = from, stanza = stanza });
100                 end;
101
102                 prosody.events.fire_event("s2sin-established", event_data);
103                 hosts[to].events.fire_event("s2sin-established", event_data);
104         end
105         
106         if session.direction == "outgoing" then
107                 if sendq then
108                         session.log("debug", "sending %d queued stanzas across new outgoing connection to %s", #sendq, session.to_host);
109                         for i, data in ipairs(sendq) do
110                                 send(data[1]);
111                                 sendq[i] = nil;
112                         end
113                         session.sendq = nil;
114                 end
115                 
116                 session.ip_hosts = nil;
117                 session.srv_hosts = nil;
118         end
119 end
120
121 local resting_session = { -- Resting, not dead
122                 destroyed = true;
123                 type = "s2s_destroyed";
124                 open_stream = function (session)
125                         session.log("debug", "Attempt to open stream on resting session");
126                 end;
127                 close = function (session)
128                         session.log("debug", "Attempt to close already-closed session");
129                 end;
130                 filter = function (type, data) return data; end;
131         }; resting_session.__index = resting_session;
132
133 function retire_session(session, reason)
134         local log = session.log or log;
135         for k in pairs(session) do
136                 if k ~= "trace" and k ~= "log" and k ~= "id" and k ~= "conn" then
137                         session[k] = nil;
138                 end
139         end
140
141         session.destruction_reason = reason;
142
143         function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); end
144         function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end
145         return setmetatable(session, resting_session);
146 end
147
148 function destroy_session(session, reason)
149         if session.destroyed then return; end
150         (session.log or log)("debug", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)..(reason and (": "..reason) or ""));
151         
152         if session.direction == "outgoing" then
153                 hosts[session.from_host].s2sout[session.to_host] = nil;
154                 session:bounce_sendq(reason);
155         elseif session.direction == "incoming" then
156                 incoming_s2s[session] = nil;
157         end
158         
159         local event_data = { session = session, reason = reason };
160         if session.type == "s2sout" then
161                 prosody.events.fire_event("s2sout-destroyed", event_data);
162                 if hosts[session.from_host] then
163                         hosts[session.from_host].events.fire_event("s2sout-destroyed", event_data);
164                 end
165         elseif session.type == "s2sin" then
166                 prosody.events.fire_event("s2sin-destroyed", event_data);
167                 if hosts[session.to_host] then
168                         hosts[session.to_host].events.fire_event("s2sin-destroyed", event_data);
169                 end
170         end
171         
172         retire_session(session, reason); -- Clean session until it is GC'd
173         return true;
174 end
175
176 return _M;