certmanager, hostmanager, mod_tls: Move responsibility for creating per-host SSL...
[prosody.git] / plugins / mod_tls.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 local create_context = require "core.certmanager".create_context;
10 local st = require "util.stanza";
11
12 local secure_auth_only = module:get_option("c2s_require_encryption") or module:get_option("require_encryption");
13 local secure_s2s_only = module:get_option("s2s_require_encryption");
14 local allow_s2s_tls = module:get_option("s2s_allow_encryption") ~= false;
15
16 local xmlns_starttls = 'urn:ietf:params:xml:ns:xmpp-tls';
17 local starttls_attr = { xmlns = xmlns_starttls };
18 local starttls_proceed = st.stanza("proceed", starttls_attr);
19 local starttls_failure = st.stanza("failure", starttls_attr);
20 local c2s_feature = st.stanza("starttls", starttls_attr);
21 local s2s_feature = st.stanza("starttls", starttls_attr);
22 if secure_auth_only then c2s_feature:tag("required"):up(); end
23 if secure_s2s_only then s2s_feature:tag("required"):up(); end
24
25 local global_ssl_ctx = prosody.global_ssl_ctx;
26
27 local host = hosts[module.host];
28
29 local function can_do_tls(session)
30         if session.type == "c2s_unauthed" then
31                 return session.conn.starttls and host.ssl_ctx_in;
32         elseif session.type == "s2sin_unauthed" and allow_s2s_tls then
33                 return session.conn.starttls and host.ssl_ctx_in;
34         elseif session.direction == "outgoing" and allow_s2s_tls then
35                 return session.conn.starttls and host.ssl_ctx;
36         end
37         return false;
38 end
39
40 -- Hook <starttls/>
41 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event)
42         local origin = event.origin;
43         if can_do_tls(origin) then
44                 (origin.sends2s or origin.send)(starttls_proceed);
45                 origin:reset_stream();
46                 local host = origin.to_host or origin.host;
47                 local ssl_ctx = host and hosts[host].ssl_ctx_in or global_ssl_ctx;
48                 origin.conn:starttls(ssl_ctx);
49                 origin.log("info", "TLS negotiation started for %s...", origin.type);
50                 origin.secure = false;
51         else
52                 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type);
53                 (origin.sends2s or origin.send)(starttls_failure);
54                 origin:close();
55         end
56         return true;
57 end);
58
59 -- Advertize stream feature
60 module:hook("stream-features", function(event)
61         local origin, features = event.origin, event.features;
62         if can_do_tls(origin) then
63                 features:add_child(c2s_feature);
64         end
65 end);
66 module:hook("s2s-stream-features", function(event)
67         local origin, features = event.origin, event.features;
68         if can_do_tls(origin) then
69                 features:add_child(s2s_feature);
70         end
71 end);
72
73 -- For s2sout connections, start TLS if we can
74 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
75         module:log("debug", "Received features element");
76         if can_do_tls(session) and stanza:child_with_ns(xmlns_starttls) then
77                 module:log("%s is offering TLS, taking up the offer...", session.to_host);
78                 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>");
79                 return true;
80         end
81 end, 500);
82
83 module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza)
84         module:log("debug", "Proceeding with TLS on s2sout...");
85         session:reset_stream();
86         local ssl_ctx = session.from_host and hosts[session.from_host].ssl_ctx or global_ssl_ctx;
87         session.conn:starttls(ssl_ctx);
88         session.secure = false;
89         return true;
90 end);
91
92 function module.load()
93         local ssl_config = module:get_option("ssl");
94         host.ssl_ctx = create_context(host, "client", ssl_config); -- for outgoing connections
95         host.ssl_ctx_in = create_context(host, "server", ssl_config); -- for incoming connections
96 end
97
98 function module.unload()
99         host.ssl_ctx = nil;
100         host.ssl_ctx_in = nil;
101 end