Merge 0.9->0.10
[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 rawgetopt = require"core.configmanager".rawget;
11 local st = require "util.stanza";
12
13 local c2s_require_encryption = module:get_option("c2s_require_encryption", module:get_option("require_encryption"));
14 local s2s_require_encryption = module:get_option("s2s_require_encryption");
15 local allow_s2s_tls = module:get_option("s2s_allow_encryption") ~= false;
16 local s2s_secure_auth = module:get_option("s2s_secure_auth");
17
18 if s2s_secure_auth and s2s_require_encryption == false then
19         module:log("warn", "s2s_secure_auth implies s2s_require_encryption, but s2s_require_encryption is set to false");
20         s2s_require_encryption = true;
21 end
22
23 local xmlns_starttls = 'urn:ietf:params:xml:ns:xmpp-tls';
24 local starttls_attr = { xmlns = xmlns_starttls };
25 local starttls_initiate= st.stanza("starttls", starttls_attr);
26 local starttls_proceed = st.stanza("proceed", starttls_attr);
27 local starttls_failure = st.stanza("failure", starttls_attr);
28 local c2s_feature = st.stanza("starttls", starttls_attr);
29 local s2s_feature = st.stanza("starttls", starttls_attr);
30 if c2s_require_encryption then c2s_feature:tag("required"):up(); end
31 if s2s_require_encryption then s2s_feature:tag("required"):up(); end
32
33 local hosts = prosody.hosts;
34 local host = hosts[module.host];
35
36 local ssl_ctx_c2s, ssl_ctx_s2sout, ssl_ctx_s2sin;
37 local ssl_cfg_c2s, ssl_cfg_s2sout, ssl_cfg_s2sin;
38 do
39         local NULL, err = {};
40         local modhost = module.host;
41         local parent = modhost:match("%.(.*)$");
42
43         local global_ssl = rawgetopt("*",     "ssl") or NULL;
44         local parent_ssl = rawgetopt(parent,  "ssl") or NULL;
45         local host_ssl   = rawgetopt(modhost, "ssl") or parent_ssl;
46
47         local global_c2s = rawgetopt("*",     "c2s_ssl") or NULL;
48         local parent_c2s = rawgetopt(parent,  "c2s_ssl") or NULL;
49         local host_c2s   = rawgetopt(modhost, "c2s_ssl") or parent_ssl;
50
51         local global_s2s = rawgetopt("*",     "s2s_ssl") or NULL;
52         local parent_s2s = rawgetopt(parent,  "s2s_ssl") or NULL;
53         local host_s2s   = rawgetopt(modhost, "s2s_ssl") or parent_ssl;
54
55         ssl_ctx_c2s, err, ssl_cfg_c2s = create_context(host.host, "server", host_c2s, host_ssl, global_c2s); -- for incoming client connections
56         if not ssl_ctx_c2s then module:log("error", "Error creating context for c2s: %s", err); end
57
58         ssl_ctx_s2sout, err, ssl_cfg_s2sout = create_context(host.host, "client", host_s2s, host_ssl, global_s2s); -- for outgoing server connections
59         if not ssl_ctx_s2sout then module:log("error", "Error creating contexts for s2sout: %s", err); end
60
61         ssl_ctx_s2sin, err, ssl_cfg_s2sin = create_context(host.host, "server", host_s2s, host_ssl, global_s2s); -- for incoming server connections
62         if not ssl_ctx_s2sin then module:log("error", "Error creating contexts for s2sin: %s", err); end
63 end
64
65 local function can_do_tls(session)
66         if session.ssl_ctx == false or not session.conn.starttls then
67                 return false;
68         elseif session.ssl_ctx then
69                 return true;
70         end
71         if session.type == "c2s_unauthed" then
72                 session.ssl_ctx = ssl_ctx_c2s;
73                 session.ssl_cfg = ssl_cfg_c2s;
74         elseif session.type == "s2sin_unauthed" and allow_s2s_tls then
75                 session.ssl_ctx = ssl_ctx_s2sin;
76                 session.ssl_cfg = ssl_cfg_s2sin;
77         elseif session.direction == "outgoing" and allow_s2s_tls then
78                 session.ssl_ctx = ssl_ctx_s2sout;
79                 session.ssl_cfg = ssl_cfg_s2sout;
80         else
81                 return false;
82         end
83         return session.ssl_ctx;
84 end
85
86 -- Hook <starttls/>
87 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event)
88         local origin = event.origin;
89         if can_do_tls(origin) then
90                 (origin.sends2s or origin.send)(starttls_proceed);
91                 origin:reset_stream();
92                 origin.conn:starttls(origin.ssl_ctx);
93                 origin.log("debug", "TLS negotiation started for %s...", origin.type);
94                 origin.secure = false;
95         else
96                 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type);
97                 (origin.sends2s or origin.send)(starttls_failure);
98                 origin:close();
99         end
100         return true;
101 end);
102
103 -- Advertize stream feature
104 module:hook("stream-features", function(event)
105         local origin, features = event.origin, event.features;
106         if can_do_tls(origin) then
107                 features:add_child(c2s_feature);
108         end
109 end);
110 module:hook("s2s-stream-features", function(event)
111         local origin, features = event.origin, event.features;
112         if can_do_tls(origin) then
113                 features:add_child(s2s_feature);
114         end
115 end);
116
117 -- For s2sout connections, start TLS if we can
118 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
119         module:log("debug", "Received features element");
120         if can_do_tls(session) and stanza:get_child("starttls", xmlns_starttls) then
121                 module:log("debug", "%s is offering TLS, taking up the offer...", session.to_host);
122                 session.sends2s(starttls_initiate);
123                 return true;
124         end
125 end, 500);
126
127 module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza)
128         module:log("debug", "Proceeding with TLS on s2sout...");
129         session:reset_stream();
130         session.conn:starttls(session.ssl_ctx);
131         session.secure = false;
132         return true;
133 end);