mod_s2s, mod_saslauth, mod_compression: Refactor to have common code for opening...
[prosody.git] / plugins / mod_s2s / s2sout.lib.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 containing all the logic for connecting to a remote server
10
11 local portmanager = require "core.portmanager";
12 local wrapclient = require "net.server".wrapclient;
13 local initialize_filters = require "util.filters".initialize;
14 local idna_to_ascii = require "util.encodings".idna.to_ascii;
15 local new_ip = require "util.ip".new_ip;
16 local rfc3484_dest = require "util.rfc3484".destination;
17 local socket = require "socket";
18 local adns = require "net.adns";
19 local dns = require "net.dns";
20 local t_insert, t_sort, ipairs = table.insert, table.sort, ipairs;
21 local st = require "util.stanza";
22
23 local s2s_destroy_session = require "core.s2smanager".destroy_session;
24
25 local log = module._log;
26
27 local sources = {};
28 local has_ipv4, has_ipv6;
29
30 local dns_timeout = module:get_option_number("dns_timeout", 15);
31 dns.settimeout(dns_timeout);
32 local max_dns_depth = module:get_option_number("dns_max_depth", 3);
33
34 local s2sout = {};
35
36 local s2s_listener;
37
38
39 function s2sout.set_listener(listener)
40         s2s_listener = listener;
41 end
42
43 local function compare_srv_priorities(a,b)
44         return a.priority < b.priority or (a.priority == b.priority and a.weight > b.weight);
45 end
46
47 function s2sout.initiate_connection(host_session)
48         initialize_filters(host_session);
49         host_session.version = 1;
50         host_session.open_stream = session_open_stream;
51         
52         -- Kick the connection attempting machine into life
53         if not s2sout.attempt_connection(host_session) then
54                 -- Intentionally not returning here, the
55                 -- session is needed, connected or not
56                 s2s_destroy_session(host_session);
57         end
58         
59         if not host_session.sends2s then
60                 -- A sends2s which buffers data (until the stream is opened)
61                 -- note that data in this buffer will be sent before the stream is authed
62                 -- and will not be ack'd in any way, successful or otherwise
63                 local buffer;
64                 function host_session.sends2s(data)
65                         if not buffer then
66                                 buffer = {};
67                                 host_session.send_buffer = buffer;
68                         end
69                         log("debug", "Buffering data on unconnected s2sout to %s", tostring(host_session.to_host));
70                         buffer[#buffer+1] = data;
71                         log("debug", "Buffered item %d: %s", #buffer, tostring(data));
72                 end
73         end
74 end
75
76 function s2sout.attempt_connection(host_session, err)
77         local from_host, to_host = host_session.from_host, host_session.to_host;
78         local connect_host, connect_port = to_host and idna_to_ascii(to_host), 5269;
79         
80         if not connect_host then
81                 return false;
82         end
83         
84         if not err then -- This is our first attempt
85                 log("debug", "First attempt to connect to %s, starting with SRV lookup...", to_host);
86                 host_session.connecting = true;
87                 local handle;
88                 handle = adns.lookup(function (answer)
89                         handle = nil;
90                         host_session.connecting = nil;
91                         if answer and #answer > 0 then
92                                 log("debug", "%s has SRV records, handling...", to_host);
93                                 local srv_hosts = {};
94                                 host_session.srv_hosts = srv_hosts;
95                                 for _, record in ipairs(answer) do
96                                         t_insert(srv_hosts, record.srv);
97                                 end
98                                 if #srv_hosts == 1 and srv_hosts[1].target == "." then
99                                         log("debug", "%s does not provide a XMPP service", to_host);
100                                         s2s_destroy_session(host_session, err); -- Nothing to see here
101                                         return;
102                                 end
103                                 t_sort(srv_hosts, compare_srv_priorities);
104                                 
105                                 local srv_choice = srv_hosts[1];
106                                 host_session.srv_choice = 1;
107                                 if srv_choice then
108                                         connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port;
109                                         log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port);
110                                 end
111                         else
112                                 log("debug", "%s has no SRV records, falling back to A/AAAA", to_host);
113                         end
114                         -- Try with SRV, or just the plain hostname if no SRV
115                         local ok, err = s2sout.try_connect(host_session, connect_host, connect_port);
116                         if not ok then
117                                 if not s2sout.attempt_connection(host_session, err) then
118                                         -- No more attempts will be made
119                                         s2s_destroy_session(host_session, err);
120                                 end
121                         end
122                 end, "_xmpp-server._tcp."..connect_host..".", "SRV");
123                 
124                 return true; -- Attempt in progress
125         elseif host_session.ip_hosts then
126                 return s2sout.try_connect(host_session, connect_host, connect_port, err);
127         elseif host_session.srv_hosts and #host_session.srv_hosts > host_session.srv_choice then -- Not our first attempt, and we also have SRV
128                 host_session.srv_choice = host_session.srv_choice + 1;
129                 local srv_choice = host_session.srv_hosts[host_session.srv_choice];
130                 connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port;
131                 host_session.log("info", "Connection failed (%s). Attempt #%d: This time to %s:%d", tostring(err), host_session.srv_choice, connect_host, connect_port);
132         else
133                 host_session.log("info", "Out of connection options, can't connect to %s", tostring(host_session.to_host));
134                 -- We're out of options
135                 return false;
136         end
137         
138         if not (connect_host and connect_port) then
139                 -- Likely we couldn't resolve DNS
140                 log("warn", "Hmm, we're without a host (%s) and port (%s) to connect to for %s, giving up :(", tostring(connect_host), tostring(connect_port), tostring(to_host));
141                 return false;
142         end
143
144         return s2sout.try_connect(host_session, connect_host, connect_port);
145 end
146
147 function s2sout.try_next_ip(host_session)
148         host_session.connecting = nil;
149         host_session.ip_choice = host_session.ip_choice + 1;
150         local ip = host_session.ip_hosts[host_session.ip_choice];
151         local ok, err= s2sout.make_connect(host_session, ip.ip, ip.port);
152         if not ok then
153                 if not s2sout.attempt_connection(host_session, err or "closed") then
154                         err = err and (": "..err) or "";
155                         s2s_destroy_session(host_session, "Connection failed"..err);
156                 end
157         end
158 end
159
160 function s2sout.try_connect(host_session, connect_host, connect_port, err)
161         host_session.connecting = true;
162
163         if not err then
164                 local IPs = {};
165                 host_session.ip_hosts = IPs;
166                 local handle4, handle6;
167                 local have_other_result = not(has_ipv4) or not(has_ipv6) or false;
168
169                 if has_ipv4 then
170                         handle4 = adns.lookup(function (reply, err)
171                                 handle4 = nil;
172
173                                 -- COMPAT: This is a compromise for all you CNAME-(ab)users :)
174                                 if not (reply and reply[#reply] and reply[#reply].a) then
175                                         local count = max_dns_depth;
176                                         reply = dns.peek(connect_host, "CNAME", "IN");
177                                         while count > 0 and reply and reply[#reply] and not reply[#reply].a and reply[#reply].cname do
178                                                 log("debug", "Looking up %s (DNS depth is %d)", tostring(reply[#reply].cname), count);
179                                                 reply = dns.peek(reply[#reply].cname, "A", "IN") or dns.peek(reply[#reply].cname, "CNAME", "IN");
180                                                 count = count - 1;
181                                         end
182                                 end
183                                 -- end of CNAME resolving
184
185                                 if reply and reply[#reply] and reply[#reply].a then
186                                         for _, ip in ipairs(reply) do
187                                                 log("debug", "DNS reply for %s gives us %s", connect_host, ip.a);
188                                                 IPs[#IPs+1] = new_ip(ip.a, "IPv4");
189                                         end
190                                 end
191
192                                 if have_other_result then
193                                         if #IPs > 0 then
194                                                 rfc3484_dest(host_session.ip_hosts, sources);
195                                                 for i = 1, #IPs do
196                                                         IPs[i] = {ip = IPs[i], port = connect_port};
197                                                 end
198                                                 host_session.ip_choice = 0;
199                                                 s2sout.try_next_ip(host_session);
200                                         else
201                                                 log("debug", "DNS lookup failed to get a response for %s", connect_host);
202                                                 host_session.ip_hosts = nil;
203                                                 if not s2sout.attempt_connection(host_session, "name resolution failed") then -- Retry if we can
204                                                         log("debug", "No other records to try for %s - destroying", host_session.to_host);
205                                                         err = err and (": "..err) or "";
206                                                         s2s_destroy_session(host_session, "DNS resolution failed"..err); -- End of the line, we can't
207                                                 end
208                                         end
209                                 else
210                                         have_other_result = true;
211                                 end
212                         end, connect_host, "A", "IN");
213                 else
214                         have_other_result = true;
215                 end
216
217                 if has_ipv6 then
218                         handle6 = adns.lookup(function (reply, err)
219                                 handle6 = nil;
220
221                                 if reply and reply[#reply] and reply[#reply].aaaa then
222                                         for _, ip in ipairs(reply) do
223                                                 log("debug", "DNS reply for %s gives us %s", connect_host, ip.aaaa);
224                                                 IPs[#IPs+1] = new_ip(ip.aaaa, "IPv6");
225                                         end
226                                 end
227
228                                 if have_other_result then
229                                         if #IPs > 0 then
230                                                 rfc3484_dest(host_session.ip_hosts, sources);
231                                                 for i = 1, #IPs do
232                                                         IPs[i] = {ip = IPs[i], port = connect_port};
233                                                 end
234                                                 host_session.ip_choice = 0;
235                                                 s2sout.try_next_ip(host_session);
236                                         else
237                                                 log("debug", "DNS lookup failed to get a response for %s", connect_host);
238                                                 host_session.ip_hosts = nil;
239                                                 if not s2sout.attempt_connection(host_session, "name resolution failed") then -- Retry if we can
240                                                         log("debug", "No other records to try for %s - destroying", host_session.to_host);
241                                                         err = err and (": "..err) or "";
242                                                         s2s_destroy_session(host_session, "DNS resolution failed"..err); -- End of the line, we can't
243                                                 end
244                                         end
245                                 else
246                                         have_other_result = true;
247                                 end
248                         end, connect_host, "AAAA", "IN");
249                 else
250                         have_other_result = true;
251                 end
252                 return true;
253         elseif host_session.ip_hosts and #host_session.ip_hosts > host_session.ip_choice then -- Not our first attempt, and we also have IPs left to try
254                 s2sout.try_next_ip(host_session);
255         else
256                 host_session.ip_hosts = nil;
257                 if not s2sout.attempt_connection(host_session, "out of IP addresses") then -- Retry if we can
258                         log("debug", "No other records to try for %s - destroying", host_session.to_host);
259                         err = err and (": "..err) or "";
260                         s2s_destroy_session(host_session, "Connecting failed"..err); -- End of the line, we can't
261                         return false;
262                 end
263         end
264
265         return true;
266 end
267
268 function s2sout.make_connect(host_session, connect_host, connect_port)
269         (host_session.log or log)("info", "Beginning new connection attempt to %s ([%s]:%d)", host_session.to_host, connect_host.addr, connect_port);
270         -- Ok, we're going to try to connect
271         
272         local from_host, to_host = host_session.from_host, host_session.to_host;
273         
274         local conn, handler;
275         if connect_host.proto == "IPv4" then
276                 conn, handler = socket.tcp();
277         else
278                 conn, handler = socket.tcp6();
279         end
280         
281         if not conn then
282                 log("warn", "Failed to create outgoing connection, system error: %s", handler);
283                 return false, handler;
284         end
285
286         conn:settimeout(0);
287         local success, err = conn:connect(connect_host.addr, connect_port);
288         if not success and err ~= "timeout" then
289                 log("warn", "s2s connect() to %s (%s:%d) failed: %s", host_session.to_host, connect_host.addr, connect_port, err);
290                 return false, err;
291         end
292         
293         conn = wrapclient(conn, connect_host.addr, connect_port, s2s_listener, "*a");
294         host_session.conn = conn;
295         
296         local filter = initialize_filters(host_session);
297         local w, log = conn.write, host_session.log;
298         host_session.sends2s = function (t)
299                 log("debug", "sending: %s", (t.top_tag and t:top_tag()) or t:match("^[^>]*>?"));
300                 if t.name then
301                         t = filter("stanzas/out", t);
302                 end
303                 if t then
304                         t = filter("bytes/out", tostring(t));
305                         if t then
306                                 return w(conn, tostring(t));
307                         end
308                 end
309         end
310         
311         -- Register this outgoing connection so that xmppserver_listener knows about it
312         -- otherwise it will assume it is a new incoming connection
313         s2s_listener.register_outgoing(conn, host_session);
314         
315         log("debug", "Connection attempt in progress...");
316         return true;
317 end
318
319 module:hook_global("service-added", function (event)
320         if event.name ~= "s2s" then return end
321
322         local s2s_sources = portmanager.get_active_services():get("s2s");
323         if not s2s_sources then
324                 module:log("warn", "s2s not listening on any ports, outgoing connections may fail");
325                 return;
326         end
327         for source, _ in pairs(s2s_sources) do
328                 if source == "*" or source == "0.0.0.0" then
329                         if not socket.local_addresses then
330                                 sources[#sources + 1] = new_ip("0.0.0.0", "IPv4");
331                         else
332                                 for _, addr in ipairs(socket.local_addresses("ipv4", true)) do
333                                         sources[#sources + 1] = new_ip(addr, "IPv4");
334                                 end
335                         end
336                 elseif source == "::" then
337                         if not socket.local_addresses then
338                                 sources[#sources + 1] = new_ip("::", "IPv6");
339                         else
340                                 for _, addr in ipairs(socket.local_addresses("ipv6", true)) do
341                                         sources[#sources + 1] = new_ip(addr, "IPv6");
342                                 end
343                         end
344                 else
345                         sources[#sources + 1] = new_ip(source, (source:find(":") and "IPv6") or "IPv4");
346                 end
347         end
348         for i = 1,#sources do
349                 if sources[i].proto == "IPv6" then
350                         has_ipv6 = true;
351                 elseif sources[i].proto == "IPv4" then
352                         has_ipv4 = true;
353                 end
354         end
355 end);
356
357 return s2sout;