Mercurial > prosody-hg
comparison core/s2smanager.lua @ 4797:e239668aa6d2
Merge 0.9->trunk
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sun, 29 Apr 2012 02:10:55 +0100 |
| parents | dc70c4ffb66d |
| children | 5ef05f32bc42 da2c49a9ab99 |
comparison
equal
deleted
inserted
replaced
| 4796:04a34287dc12 | 4797:e239668aa6d2 |
|---|---|
| 7 -- | 7 -- |
| 8 | 8 |
| 9 | 9 |
| 10 | 10 |
| 11 local hosts = hosts; | 11 local hosts = hosts; |
| 12 local sessions = sessions; | |
| 13 local core_process_stanza = function(a, b) core_process_stanza(a, b); end | 12 local core_process_stanza = function(a, b) core_process_stanza(a, b); end |
| 14 local add_task = require "util.timer".add_task; | |
| 15 local socket = require "socket"; | |
| 16 local format = string.format; | 13 local format = string.format; |
| 17 local t_insert, t_sort = table.insert, table.sort; | 14 local t_insert, t_sort = table.insert, table.sort; |
| 18 local get_traceback = debug.traceback; | 15 local get_traceback = debug.traceback; |
| 19 local tostring, pairs, ipairs, getmetatable, newproxy, error, tonumber, setmetatable | 16 local tostring, pairs, ipairs, getmetatable, newproxy, type, error, tonumber, setmetatable |
| 20 = tostring, pairs, ipairs, getmetatable, newproxy, error, tonumber, setmetatable; | 17 = tostring, pairs, ipairs, getmetatable, newproxy, type, error, tonumber, setmetatable; |
| 21 | 18 |
| 22 local idna_to_ascii = require "util.encodings".idna.to_ascii; | |
| 23 local connlisteners_get = require "net.connlisteners".get; | |
| 24 local initialize_filters = require "util.filters".initialize; | 19 local initialize_filters = require "util.filters".initialize; |
| 25 local wrapclient = require "net.server".wrapclient; | 20 local wrapclient = require "net.server".wrapclient; |
| 26 local modulemanager = require "core.modulemanager"; | |
| 27 local st = require "stanza"; | 21 local st = require "stanza"; |
| 28 local stanza = st.stanza; | 22 local stanza = st.stanza; |
| 29 local nameprep = require "util.encodings".stringprep.nameprep; | 23 local nameprep = require "util.encodings".stringprep.nameprep; |
| 30 local cert_verify_identity = require "util.x509".verify_identity; | 24 local cert_verify_identity = require "util.x509".verify_identity; |
| 25 local new_ip = require "util.ip".new_ip; | |
| 26 local rfc3484_dest = require "util.rfc3484".destination; | |
| 31 | 27 |
| 32 local fire_event = prosody.events.fire_event; | 28 local fire_event = prosody.events.fire_event; |
| 33 local uuid_gen = require "util.uuid".generate; | 29 local uuid_gen = require "util.uuid".generate; |
| 34 | 30 |
| 35 local logger_init = require "util.logger".init; | 31 local logger_init = require "util.logger".init; |
| 38 | 34 |
| 39 local sha256_hash = require "util.hashes".sha256; | 35 local sha256_hash = require "util.hashes".sha256; |
| 40 | 36 |
| 41 local adns, dns = require "net.adns", require "net.dns"; | 37 local adns, dns = require "net.adns", require "net.dns"; |
| 42 local config = require "core.configmanager"; | 38 local config = require "core.configmanager"; |
| 43 local connect_timeout = config.get("*", "core", "s2s_timeout") or 60; | |
| 44 local dns_timeout = config.get("*", "core", "dns_timeout") or 15; | 39 local dns_timeout = config.get("*", "core", "dns_timeout") or 15; |
| 45 local max_dns_depth = config.get("*", "core", "dns_max_depth") or 3; | 40 local cfg_sources = config.get("*", "core", "s2s_interface") |
| 41 or config.get("*", "core", "interface"); | |
| 42 local sources; | |
| 46 | 43 |
| 44 --FIXME: s2sout should create its own resolver w/ timeout | |
| 47 dns.settimeout(dns_timeout); | 45 dns.settimeout(dns_timeout); |
| 48 | 46 |
| 49 local prosody = _G.prosody; | 47 local prosody = _G.prosody; |
| 50 incoming_s2s = {}; | 48 incoming_s2s = {}; |
| 51 prosody.incoming_s2s = incoming_s2s; | 49 prosody.incoming_s2s = incoming_s2s; |
| 52 local incoming_s2s = incoming_s2s; | 50 local incoming_s2s = incoming_s2s; |
| 53 | 51 |
| 54 module "s2smanager" | 52 module "s2smanager" |
| 55 | |
| 56 function compare_srv_priorities(a,b) | |
| 57 return a.priority < b.priority or (a.priority == b.priority and a.weight > b.weight); | |
| 58 end | |
| 59 | |
| 60 local bouncy_stanzas = { message = true, presence = true, iq = true }; | |
| 61 local function bounce_sendq(session, reason) | |
| 62 local sendq = session.sendq; | |
| 63 if sendq then | |
| 64 session.log("info", "sending error replies for "..#sendq.." queued stanzas because of failed outgoing connection to "..tostring(session.to_host)); | |
| 65 local dummy = { | |
| 66 type = "s2sin"; | |
| 67 send = function(s) | |
| 68 (session.log or log)("error", "Replying to to an s2s error reply, please report this! Traceback: %s", get_traceback()); | |
| 69 end; | |
| 70 dummy = true; | |
| 71 }; | |
| 72 for i, data in ipairs(sendq) do | |
| 73 local reply = data[2]; | |
| 74 if reply and not(reply.attr.xmlns) and bouncy_stanzas[reply.name] then | |
| 75 reply.attr.type = "error"; | |
| 76 reply:tag("error", {type = "cancel"}) | |
| 77 :tag("remote-server-not-found", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}):up(); | |
| 78 if reason then | |
| 79 reply:tag("text", {xmlns = "urn:ietf:params:xml:ns:xmpp-stanzas"}) | |
| 80 :text("Server-to-server connection failed: "..reason):up(); | |
| 81 end | |
| 82 core_process_stanza(dummy, reply); | |
| 83 end | |
| 84 sendq[i] = nil; | |
| 85 end | |
| 86 session.sendq = nil; | |
| 87 end | |
| 88 end | |
| 89 | |
| 90 function send_to_host(from_host, to_host, data) | |
| 91 if not hosts[from_host] then | |
| 92 log("warn", "Attempt to send stanza from %s - a host we don't serve", from_host); | |
| 93 return false; | |
| 94 end | |
| 95 local host = hosts[from_host].s2sout[to_host]; | |
| 96 if host then | |
| 97 -- We have a connection to this host already | |
| 98 if host.type == "s2sout_unauthed" and (data.name ~= "db:verify" or not host.dialback_key) then | |
| 99 (host.log or log)("debug", "trying to send over unauthed s2sout to "..to_host); | |
| 100 | |
| 101 -- Queue stanza until we are able to send it | |
| 102 if host.sendq then t_insert(host.sendq, {tostring(data), data.attr.type ~= "error" and data.attr.type ~= "result" and st.reply(data)}); | |
| 103 else host.sendq = { {tostring(data), data.attr.type ~= "error" and data.attr.type ~= "result" and st.reply(data)} }; end | |
| 104 host.log("debug", "stanza [%s] queued ", data.name); | |
| 105 elseif host.type == "local" or host.type == "component" then | |
| 106 log("error", "Trying to send a stanza to ourselves??") | |
| 107 log("error", "Traceback: %s", get_traceback()); | |
| 108 log("error", "Stanza: %s", tostring(data)); | |
| 109 return false; | |
| 110 else | |
| 111 (host.log or log)("debug", "going to send stanza to "..to_host.." from "..from_host); | |
| 112 -- FIXME | |
| 113 if host.from_host ~= from_host then | |
| 114 log("error", "WARNING! This might, possibly, be a bug, but it might not..."); | |
| 115 log("error", "We are going to send from %s instead of %s", tostring(host.from_host), tostring(from_host)); | |
| 116 end | |
| 117 host.sends2s(data); | |
| 118 host.log("debug", "stanza sent over "..host.type); | |
| 119 end | |
| 120 else | |
| 121 log("debug", "opening a new outgoing connection for this stanza"); | |
| 122 local host_session = new_outgoing(from_host, to_host); | |
| 123 | |
| 124 -- Store in buffer | |
| 125 host_session.sendq = { {tostring(data), data.attr.type ~= "error" and data.attr.type ~= "result" and st.reply(data)} }; | |
| 126 log("debug", "stanza [%s] queued until connection complete", tostring(data.name)); | |
| 127 if (not host_session.connecting) and (not host_session.conn) then | |
| 128 log("warn", "Connection to %s failed already, destroying session...", to_host); | |
| 129 if not destroy_session(host_session, "Connection failed") then | |
| 130 -- Already destroyed, we need to bounce our stanza | |
| 131 bounce_sendq(host_session, host_session.destruction_reason); | |
| 132 end | |
| 133 return false; | |
| 134 end | |
| 135 end | |
| 136 return true; | |
| 137 end | |
| 138 | 53 |
| 139 local open_sessions = 0; | 54 local open_sessions = 0; |
| 140 | 55 |
| 141 function new_incoming(conn) | 56 function new_incoming(conn) |
| 142 local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} }; | 57 local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} }; |
| 143 if true then | 58 if true then |
| 144 session.trace = newproxy(true); | 59 session.trace = newproxy(true); |
| 145 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end; | 60 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; end; |
| 146 end | 61 end |
| 147 open_sessions = open_sessions + 1; | 62 open_sessions = open_sessions + 1; |
| 148 local w, log = conn.write, logger_init("s2sin"..tostring(conn):match("[a-f0-9]+$")); | 63 session.log = logger_init("s2sin"..tostring(conn):match("[a-f0-9]+$")); |
| 149 session.log = log; | |
| 150 local filter = initialize_filters(session); | |
| 151 session.sends2s = function (t) | |
| 152 log("debug", "sending: %s", t.top_tag and t:top_tag() or t:match("^([^>]*>?)")); | |
| 153 if t.name then | |
| 154 t = filter("stanzas/out", t); | |
| 155 end | |
| 156 if t then | |
| 157 t = filter("bytes/out", tostring(t)); | |
| 158 if t then | |
| 159 return w(conn, t); | |
| 160 end | |
| 161 end | |
| 162 end | |
| 163 incoming_s2s[session] = true; | 64 incoming_s2s[session] = true; |
| 164 add_task(connect_timeout, function () | |
| 165 if session.conn ~= conn or | |
| 166 session.type == "s2sin" then | |
| 167 return; -- Ok, we're connect[ed|ing] | |
| 168 end | |
| 169 -- Not connected, need to close session and clean up | |
| 170 (session.log or log)("debug", "Destroying incomplete session %s->%s due to inactivity", | |
| 171 session.from_host or "(unknown)", session.to_host or "(unknown)"); | |
| 172 session:close("connection-timeout"); | |
| 173 end); | |
| 174 return session; | 65 return session; |
| 175 end | 66 end |
| 176 | 67 |
| 177 function new_outgoing(from_host, to_host, connect) | 68 function new_outgoing(from_host, to_host, connect) |
| 178 local host_session = { to_host = to_host, from_host = from_host, host = from_host, | 69 local host_session = { to_host = to_host, from_host = from_host, host = from_host, |
| 179 notopen = true, type = "s2sout_unauthed", direction = "outgoing", | 70 notopen = true, type = "s2sout_unauthed", direction = "outgoing" }; |
| 180 open_stream = session_open_stream }; | 71 hosts[from_host].s2sout[to_host] = host_session; |
| 181 | 72 local conn_name = "s2sout"..tostring(host_session):match("[a-f0-9]*$"); |
| 182 hosts[from_host].s2sout[to_host] = host_session; | 73 host_session.log = logger_init(conn_name); |
| 183 | 74 return host_session; |
| 184 host_session.close = destroy_session; -- This gets replaced by xmppserver_listener later | |
| 185 | |
| 186 local log; | |
| 187 do | |
| 188 local conn_name = "s2sout"..tostring(host_session):match("[a-f0-9]*$"); | |
| 189 log = logger_init(conn_name); | |
| 190 host_session.log = log; | |
| 191 end | |
| 192 | |
| 193 initialize_filters(host_session); | |
| 194 | |
| 195 if connect ~= false then | |
| 196 -- Kick the connection attempting machine into life | |
| 197 if not attempt_connection(host_session) then | |
| 198 -- Intentionally not returning here, the | |
| 199 -- session is needed, connected or not | |
| 200 destroy_session(host_session); | |
| 201 end | |
| 202 end | |
| 203 | |
| 204 if not host_session.sends2s then | |
| 205 -- A sends2s which buffers data (until the stream is opened) | |
| 206 -- note that data in this buffer will be sent before the stream is authed | |
| 207 -- and will not be ack'd in any way, successful or otherwise | |
| 208 local buffer; | |
| 209 function host_session.sends2s(data) | |
| 210 if not buffer then | |
| 211 buffer = {}; | |
| 212 host_session.send_buffer = buffer; | |
| 213 end | |
| 214 log("debug", "Buffering data on unconnected s2sout to %s", to_host); | |
| 215 buffer[#buffer+1] = data; | |
| 216 log("debug", "Buffered item %d: %s", #buffer, tostring(data)); | |
| 217 end | |
| 218 end | |
| 219 | |
| 220 return host_session; | |
| 221 end | |
| 222 | |
| 223 | |
| 224 function attempt_connection(host_session, err) | |
| 225 local from_host, to_host = host_session.from_host, host_session.to_host; | |
| 226 local connect_host, connect_port = to_host and idna_to_ascii(to_host), 5269; | |
| 227 | |
| 228 if not connect_host then | |
| 229 return false; | |
| 230 end | |
| 231 | |
| 232 if not err then -- This is our first attempt | |
| 233 log("debug", "First attempt to connect to %s, starting with SRV lookup...", to_host); | |
| 234 host_session.connecting = true; | |
| 235 local handle; | |
| 236 handle = adns.lookup(function (answer) | |
| 237 handle = nil; | |
| 238 host_session.connecting = nil; | |
| 239 if answer then | |
| 240 log("debug", to_host.." has SRV records, handling..."); | |
| 241 local srv_hosts = {}; | |
| 242 host_session.srv_hosts = srv_hosts; | |
| 243 for _, record in ipairs(answer) do | |
| 244 t_insert(srv_hosts, record.srv); | |
| 245 end | |
| 246 t_sort(srv_hosts, compare_srv_priorities); | |
| 247 | |
| 248 local srv_choice = srv_hosts[1]; | |
| 249 host_session.srv_choice = 1; | |
| 250 if srv_choice then | |
| 251 connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port; | |
| 252 log("debug", "Best record found, will connect to %s:%d", connect_host, connect_port); | |
| 253 end | |
| 254 else | |
| 255 log("debug", to_host.." has no SRV records, falling back to A"); | |
| 256 end | |
| 257 -- Try with SRV, or just the plain hostname if no SRV | |
| 258 local ok, err = try_connect(host_session, connect_host, connect_port); | |
| 259 if not ok then | |
| 260 if not attempt_connection(host_session, err) then | |
| 261 -- No more attempts will be made | |
| 262 destroy_session(host_session, err); | |
| 263 end | |
| 264 end | |
| 265 end, "_xmpp-server._tcp."..connect_host..".", "SRV"); | |
| 266 | |
| 267 return true; -- Attempt in progress | |
| 268 elseif host_session.srv_hosts and #host_session.srv_hosts > host_session.srv_choice then -- Not our first attempt, and we also have SRV | |
| 269 host_session.srv_choice = host_session.srv_choice + 1; | |
| 270 local srv_choice = host_session.srv_hosts[host_session.srv_choice]; | |
| 271 connect_host, connect_port = srv_choice.target or to_host, srv_choice.port or connect_port; | |
| 272 host_session.log("info", "Connection failed (%s). Attempt #%d: This time to %s:%d", tostring(err), host_session.srv_choice, connect_host, connect_port); | |
| 273 else | |
| 274 host_session.log("info", "Out of connection options, can't connect to %s", tostring(host_session.to_host)); | |
| 275 -- We're out of options | |
| 276 return false; | |
| 277 end | |
| 278 | |
| 279 if not (connect_host and connect_port) then | |
| 280 -- Likely we couldn't resolve DNS | |
| 281 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)); | |
| 282 return false; | |
| 283 end | |
| 284 | |
| 285 return try_connect(host_session, connect_host, connect_port); | |
| 286 end | |
| 287 | |
| 288 function try_connect(host_session, connect_host, connect_port) | |
| 289 host_session.connecting = true; | |
| 290 local handle; | |
| 291 handle = adns.lookup(function (reply, err) | |
| 292 handle = nil; | |
| 293 host_session.connecting = nil; | |
| 294 | |
| 295 -- COMPAT: This is a compromise for all you CNAME-(ab)users :) | |
| 296 if not (reply and reply[#reply] and reply[#reply].a) then | |
| 297 local count = max_dns_depth; | |
| 298 reply = dns.peek(connect_host, "CNAME", "IN"); | |
| 299 while count > 0 and reply and reply[#reply] and not reply[#reply].a and reply[#reply].cname do | |
| 300 log("debug", "Looking up %s (DNS depth is %d)", tostring(reply[#reply].cname), count); | |
| 301 reply = dns.peek(reply[#reply].cname, "A", "IN") or dns.peek(reply[#reply].cname, "CNAME", "IN"); | |
| 302 count = count - 1; | |
| 303 end | |
| 304 end | |
| 305 -- end of CNAME resolving | |
| 306 | |
| 307 if reply and reply[#reply] and reply[#reply].a then | |
| 308 log("debug", "DNS reply for %s gives us %s", connect_host, reply[#reply].a); | |
| 309 local ok, err = make_connect(host_session, reply[#reply].a, connect_port); | |
| 310 if not ok then | |
| 311 if not attempt_connection(host_session, err or "closed") then | |
| 312 err = err and (": "..err) or ""; | |
| 313 destroy_session(host_session, "Connection failed"..err); | |
| 314 end | |
| 315 end | |
| 316 else | |
| 317 log("debug", "DNS lookup failed to get a response for %s", connect_host); | |
| 318 if not attempt_connection(host_session, "name resolution failed") then -- Retry if we can | |
| 319 log("debug", "No other records to try for %s - destroying", host_session.to_host); | |
| 320 err = err and (": "..err) or ""; | |
| 321 destroy_session(host_session, "DNS resolution failed"..err); -- End of the line, we can't | |
| 322 end | |
| 323 end | |
| 324 end, connect_host, "A", "IN"); | |
| 325 | |
| 326 return true; | |
| 327 end | |
| 328 | |
| 329 function make_connect(host_session, connect_host, connect_port) | |
| 330 (host_session.log or log)("info", "Beginning new connection attempt to %s (%s:%d)", host_session.to_host, connect_host, connect_port); | |
| 331 -- Ok, we're going to try to connect | |
| 332 | |
| 333 local from_host, to_host = host_session.from_host, host_session.to_host; | |
| 334 | |
| 335 local conn, handler = socket.tcp(); | |
| 336 | |
| 337 if not conn then | |
| 338 log("warn", "Failed to create outgoing connection, system error: %s", handler); | |
| 339 return false, handler; | |
| 340 end | |
| 341 | |
| 342 conn:settimeout(0); | |
| 343 local success, err = conn:connect(connect_host, connect_port); | |
| 344 if not success and err ~= "timeout" then | |
| 345 log("warn", "s2s connect() to %s (%s:%d) failed: %s", host_session.to_host, connect_host, connect_port, err); | |
| 346 return false, err; | |
| 347 end | |
| 348 | |
| 349 local cl = connlisteners_get("xmppserver"); | |
| 350 conn = wrapclient(conn, connect_host, connect_port, cl, cl.default_mode or 1 ); | |
| 351 host_session.conn = conn; | |
| 352 | |
| 353 local filter = initialize_filters(host_session); | |
| 354 local w, log = conn.write, host_session.log; | |
| 355 host_session.sends2s = function (t) | |
| 356 log("debug", "sending: %s", (t.top_tag and t:top_tag()) or t:match("^[^>]*>?")); | |
| 357 if t.name then | |
| 358 t = filter("stanzas/out", t); | |
| 359 end | |
| 360 if t then | |
| 361 t = filter("bytes/out", tostring(t)); | |
| 362 if t then | |
| 363 return w(conn, tostring(t)); | |
| 364 end | |
| 365 end | |
| 366 end | |
| 367 | |
| 368 -- Register this outgoing connection so that xmppserver_listener knows about it | |
| 369 -- otherwise it will assume it is a new incoming connection | |
| 370 cl.register_outgoing(conn, host_session); | |
| 371 | |
| 372 host_session:open_stream(from_host, to_host); | |
| 373 | |
| 374 log("debug", "Connection attempt in progress..."); | |
| 375 add_task(connect_timeout, function () | |
| 376 if host_session.conn ~= conn or | |
| 377 host_session.type == "s2sout" or | |
| 378 host_session.connecting then | |
| 379 return; -- Ok, we're connect[ed|ing] | |
| 380 end | |
| 381 -- Not connected, need to close session and clean up | |
| 382 (host_session.log or log)("warn", "Destroying incomplete session %s->%s due to inactivity", | |
| 383 host_session.from_host or "(unknown)", host_session.to_host or "(unknown)"); | |
| 384 host_session:close("connection-timeout"); | |
| 385 end); | |
| 386 return true; | |
| 387 end | |
| 388 | |
| 389 function session_open_stream(session, from, to) | |
| 390 session.sends2s(st.stanza("stream:stream", { | |
| 391 xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', | |
| 392 ["xmlns:stream"]='http://etherx.jabber.org/streams', | |
| 393 from=from, to=to, version='1.0', ["xml:lang"]='en'}):top_tag()); | |
| 394 end | |
| 395 | |
| 396 local function check_cert_status(session) | |
| 397 local conn = session.conn:socket() | |
| 398 local cert | |
| 399 if conn.getpeercertificate then | |
| 400 cert = conn:getpeercertificate() | |
| 401 end | |
| 402 | |
| 403 if cert then | |
| 404 local chain_valid, errors = conn:getpeerverification() | |
| 405 -- Is there any interest in printing out all/the number of errors here? | |
| 406 if not chain_valid then | |
| 407 (session.log or log)("debug", "certificate chain validation result: invalid"); | |
| 408 session.cert_chain_status = "invalid"; | |
| 409 else | |
| 410 (session.log or log)("debug", "certificate chain validation result: valid"); | |
| 411 session.cert_chain_status = "valid"; | |
| 412 | |
| 413 local host; | |
| 414 if session.direction == "incoming" then | |
| 415 host = session.from_host; | |
| 416 else | |
| 417 host = session.to_host; | |
| 418 end | |
| 419 | |
| 420 -- We'll go ahead and verify the asserted identity if the | |
| 421 -- connecting server specified one. | |
| 422 if host then | |
| 423 if cert_verify_identity(host, "xmpp-server", cert) then | |
| 424 session.cert_identity_status = "valid" | |
| 425 else | |
| 426 session.cert_identity_status = "invalid" | |
| 427 end | |
| 428 end | |
| 429 end | |
| 430 end | |
| 431 end | |
| 432 | |
| 433 function streamopened(session, attr) | |
| 434 local send = session.sends2s; | |
| 435 | |
| 436 -- TODO: #29: SASL/TLS on s2s streams | |
| 437 session.version = tonumber(attr.version) or 0; | |
| 438 | |
| 439 -- TODO: Rename session.secure to session.encrypted | |
| 440 if session.secure == false then | |
| 441 session.secure = true; | |
| 442 end | |
| 443 | |
| 444 if session.direction == "incoming" then | |
| 445 -- Send a reply stream header | |
| 446 session.to_host = attr.to and nameprep(attr.to); | |
| 447 session.from_host = attr.from and nameprep(attr.from); | |
| 448 | |
| 449 session.streamid = uuid_gen(); | |
| 450 (session.log or log)("debug", "Incoming s2s received %s", st.stanza("stream:stream", attr):top_tag()); | |
| 451 if session.to_host then | |
| 452 if not hosts[session.to_host] then | |
| 453 -- Attempting to connect to a host we don't serve | |
| 454 session:close({ | |
| 455 condition = "host-unknown"; | |
| 456 text = "This host does not serve "..session.to_host | |
| 457 }); | |
| 458 return; | |
| 459 elseif hosts[session.to_host].disallow_s2s then | |
| 460 -- Attempting to connect to a host that disallows s2s | |
| 461 session:close({ | |
| 462 condition = "policy-violation"; | |
| 463 text = "Server-to-server communication is not allowed to this host"; | |
| 464 }); | |
| 465 return; | |
| 466 end | |
| 467 end | |
| 468 | |
| 469 if session.secure and not session.cert_chain_status then check_cert_status(session); end | |
| 470 | |
| 471 send("<?xml version='1.0'?>"); | |
| 472 send(stanza("stream:stream", { xmlns='jabber:server', ["xmlns:db"]='jabber:server:dialback', | |
| 473 ["xmlns:stream"]='http://etherx.jabber.org/streams', id=session.streamid, from=session.to_host, to=session.from_host, version=(session.version > 0 and "1.0" or nil) }):top_tag()); | |
| 474 if session.version >= 1.0 then | |
| 475 local features = st.stanza("stream:features"); | |
| 476 | |
| 477 if session.to_host then | |
| 478 hosts[session.to_host].events.fire_event("s2s-stream-features", { origin = session, features = features }); | |
| 479 else | |
| 480 (session.log or log)("warn", "No 'to' on stream header from %s means we can't offer any features", session.from_host or "unknown host"); | |
| 481 end | |
| 482 | |
| 483 log("debug", "Sending stream features: %s", tostring(features)); | |
| 484 send(features); | |
| 485 end | |
| 486 elseif session.direction == "outgoing" then | |
| 487 -- If we are just using the connection for verifying dialback keys, we won't try and auth it | |
| 488 if not attr.id then error("stream response did not give us a streamid!!!"); end | |
| 489 session.streamid = attr.id; | |
| 490 | |
| 491 if session.secure and not session.cert_chain_status then check_cert_status(session); end | |
| 492 | |
| 493 -- Send unauthed buffer | |
| 494 -- (stanzas which are fine to send before dialback) | |
| 495 -- Note that this is *not* the stanza queue (which | |
| 496 -- we can only send if auth succeeds) :) | |
| 497 local send_buffer = session.send_buffer; | |
| 498 if send_buffer and #send_buffer > 0 then | |
| 499 log("debug", "Sending s2s send_buffer now..."); | |
| 500 for i, data in ipairs(send_buffer) do | |
| 501 session.sends2s(tostring(data)); | |
| 502 send_buffer[i] = nil; | |
| 503 end | |
| 504 end | |
| 505 session.send_buffer = nil; | |
| 506 | |
| 507 -- If server is pre-1.0, don't wait for features, just do dialback | |
| 508 if session.version < 1.0 then | |
| 509 if not session.dialback_verifying then | |
| 510 log("debug", "Initiating dialback..."); | |
| 511 initiate_dialback(session); | |
| 512 else | |
| 513 mark_connected(session); | |
| 514 end | |
| 515 end | |
| 516 end | |
| 517 session.notopen = nil; | |
| 518 end | |
| 519 | |
| 520 function streamclosed(session) | |
| 521 (session.log or log)("debug", "Received </stream:stream>"); | |
| 522 session:close(); | |
| 523 end | |
| 524 | |
| 525 function initiate_dialback(session) | |
| 526 -- generate dialback key | |
| 527 session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host); | |
| 528 session.sends2s(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key)); | |
| 529 session.log("info", "sent dialback key on outgoing s2s stream"); | |
| 530 end | |
| 531 | |
| 532 function generate_dialback(id, to, from) | |
| 533 return sha256_hash(id..to..from..hosts[from].dialback_secret, true); | |
| 534 end | |
| 535 | |
| 536 function verify_dialback(id, to, from, key) | |
| 537 return key == generate_dialback(id, to, from); | |
| 538 end | 75 end |
| 539 | 76 |
| 540 function make_authenticated(session, host) | 77 function make_authenticated(session, host) |
| 541 if not session.secure then | 78 if not session.secure then |
| 542 local local_host = session.direction == "incoming" and session.to_host or session.from_host; | 79 local local_host = session.direction == "incoming" and session.to_host or session.from_host; |
| 574 local sendq, send = session.sendq, session.sends2s; | 111 local sendq, send = session.sendq, session.sends2s; |
| 575 | 112 |
| 576 local from, to = session.from_host, session.to_host; | 113 local from, to = session.from_host, session.to_host; |
| 577 | 114 |
| 578 session.log("info", session.direction.." s2s connection "..from.."->"..to.." complete"); | 115 session.log("info", session.direction.." s2s connection "..from.."->"..to.." complete"); |
| 579 | 116 |
| 580 local send_to_host = send_to_host; | |
| 581 function session.send(data) return send_to_host(to, from, data); end | |
| 582 | |
| 583 local event_data = { session = session }; | 117 local event_data = { session = session }; |
| 584 if session.type == "s2sout" then | 118 if session.type == "s2sout" then |
| 585 prosody.events.fire_event("s2sout-established", event_data); | 119 prosody.events.fire_event("s2sout-established", event_data); |
| 586 hosts[session.from_host].events.fire_event("s2sout-established", event_data); | 120 hosts[session.from_host].events.fire_event("s2sout-established", event_data); |
| 587 else | 121 else |
| 597 sendq[i] = nil; | 131 sendq[i] = nil; |
| 598 end | 132 end |
| 599 session.sendq = nil; | 133 session.sendq = nil; |
| 600 end | 134 end |
| 601 | 135 |
| 136 session.ip_hosts = nil; | |
| 602 session.srv_hosts = nil; | 137 session.srv_hosts = nil; |
| 603 end | 138 end |
| 604 end | 139 end |
| 605 | 140 |
| 606 local resting_session = { -- Resting, not dead | 141 local resting_session = { -- Resting, not dead |
| 634 if session.destroyed then return; end | 169 if session.destroyed then return; end |
| 635 (session.log or log)("debug", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)..(reason and (": "..reason) or "")); | 170 (session.log or log)("debug", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)..(reason and (": "..reason) or "")); |
| 636 | 171 |
| 637 if session.direction == "outgoing" then | 172 if session.direction == "outgoing" then |
| 638 hosts[session.from_host].s2sout[session.to_host] = nil; | 173 hosts[session.from_host].s2sout[session.to_host] = nil; |
| 639 bounce_sendq(session, reason); | 174 session:bounce_sendq(reason); |
| 640 elseif session.direction == "incoming" then | 175 elseif session.direction == "incoming" then |
| 641 incoming_s2s[session] = nil; | 176 incoming_s2s[session] = nil; |
| 642 end | 177 end |
| 643 | 178 |
| 644 local event_data = { session = session, reason = reason }; | 179 local event_data = { session = session, reason = reason }; |
