mod_tls: Pass the hostname rather than host session to certmanager.create_context...
[prosody.git] / plugins / mod_tls.lua
index b30ad3f324c6a6a60d8f807c16d50b815892c050..a80f230e527ade1658b1c8074c0de4a4a464b903 100644 (file)
@@ -6,10 +6,12 @@
 -- COPYING file in the source package for more information.
 --
 
+local create_context = require "core.certmanager".create_context;
 local st = require "util.stanza";
 
 local secure_auth_only = module:get_option("c2s_require_encryption") or module:get_option("require_encryption");
 local secure_s2s_only = module:get_option("s2s_require_encryption");
+local allow_s2s_tls = module:get_option("s2s_allow_encryption") ~= false;
 
 local xmlns_starttls = 'urn:ietf:params:xml:ns:xmpp-tls';
 local starttls_attr = { xmlns = xmlns_starttls };
@@ -27,9 +29,9 @@ local host = hosts[module.host];
 local function can_do_tls(session)
        if session.type == "c2s_unauthed" then
                return session.conn.starttls and host.ssl_ctx_in;
-       elseif session.type == "s2sin_unauthed" then
+       elseif session.type == "s2sin_unauthed" and allow_s2s_tls then
                return session.conn.starttls and host.ssl_ctx_in;
-       elseif session.direction == "outgoing" then
+       elseif session.direction == "outgoing" and allow_s2s_tls then
                return session.conn.starttls and host.ssl_ctx;
        end
        return false;
@@ -82,7 +84,18 @@ module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza)
        module:log("debug", "Proceeding with TLS on s2sout...");
        session:reset_stream();
        local ssl_ctx = session.from_host and hosts[session.from_host].ssl_ctx or global_ssl_ctx;
-       session.conn:starttls(ssl_ctx, true);
+       session.conn:starttls(ssl_ctx);
        session.secure = false;
        return true;
 end);
+
+function module.load()
+       local ssl_config = module:get_option("ssl");
+       host.ssl_ctx = create_context(host.host, "client", ssl_config); -- for outgoing connections
+       host.ssl_ctx_in = create_context(host.host, "server", ssl_config); -- for incoming connections
+end
+
+function module.unload()
+       host.ssl_ctx = nil;
+       host.ssl_ctx_in = nil;
+end