Merge 0.10->trunk
[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 parent_ssl = rawgetopt(parent,  "ssl") or NULL;
44         local host_ssl   = rawgetopt(modhost, "ssl") or parent_ssl;
45
46         local global_c2s = rawgetopt("*",     "c2s_ssl") or NULL;
47         local parent_c2s = rawgetopt(parent,  "c2s_ssl") or NULL;
48         local host_c2s   = rawgetopt(modhost, "c2s_ssl") or parent_c2s;
49
50         local global_s2s = rawgetopt("*",     "s2s_ssl") or NULL;
51         local parent_s2s = rawgetopt(parent,  "s2s_ssl") or NULL;
52         local host_s2s   = rawgetopt(modhost, "s2s_ssl") or parent_s2s;
53
54         ssl_ctx_c2s, err, ssl_cfg_c2s = create_context(host.host, "server", host_c2s, host_ssl, global_c2s); -- for incoming client connections
55         if not ssl_ctx_c2s then module:log("error", "Error creating context for c2s: %s", err); end
56
57         ssl_ctx_s2sout, err, ssl_cfg_s2sout = create_context(host.host, "client", host_s2s, host_ssl, global_s2s); -- for outgoing server connections
58         if not ssl_ctx_s2sout then module:log("error", "Error creating contexts for s2sout: %s", err); end
59
60         ssl_ctx_s2sin, err, ssl_cfg_s2sin = create_context(host.host, "server", host_s2s, host_ssl, global_s2s); -- for incoming server connections
61         if not ssl_ctx_s2sin then module:log("error", "Error creating contexts for s2sin: %s", err); end
62 end
63
64 local function can_do_tls(session)
65         if session.ssl_ctx == false or not session.conn.starttls then
66                 return false;
67         elseif session.ssl_ctx then
68                 return true;
69         end
70         if session.type == "c2s_unauthed" then
71                 session.ssl_ctx = ssl_ctx_c2s;
72                 session.ssl_cfg = ssl_cfg_c2s;
73         elseif session.type == "s2sin_unauthed" and allow_s2s_tls then
74                 session.ssl_ctx = ssl_ctx_s2sin;
75                 session.ssl_cfg = ssl_cfg_s2sin;
76         elseif session.direction == "outgoing" and allow_s2s_tls then
77                 session.ssl_ctx = ssl_ctx_s2sout;
78                 session.ssl_cfg = ssl_cfg_s2sout;
79         else
80                 return false;
81         end
82         return session.ssl_ctx;
83 end
84
85 -- Hook <starttls/>
86 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event)
87         local origin = event.origin;
88         if can_do_tls(origin) then
89                 (origin.sends2s or origin.send)(starttls_proceed);
90                 origin:reset_stream();
91                 origin.conn:starttls(origin.ssl_ctx);
92                 origin.log("debug", "TLS negotiation started for %s...", origin.type);
93                 origin.secure = false;
94         else
95                 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type);
96                 (origin.sends2s or origin.send)(starttls_failure);
97                 origin:close();
98         end
99         return true;
100 end);
101
102 -- Advertize stream feature
103 module:hook("stream-features", function(event)
104         local origin, features = event.origin, event.features;
105         if can_do_tls(origin) then
106                 features:add_child(c2s_feature);
107         end
108 end);
109 module:hook("s2s-stream-features", function(event)
110         local origin, features = event.origin, event.features;
111         if can_do_tls(origin) then
112                 features:add_child(s2s_feature);
113         end
114 end);
115
116 -- For s2sout connections, start TLS if we can
117 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
118         module:log("debug", "Received features element");
119         if can_do_tls(session) and stanza:get_child("starttls", xmlns_starttls) then
120                 module:log("debug", "%s is offering TLS, taking up the offer...", session.to_host);
121                 session.sends2s(starttls_initiate);
122                 return true;
123         end
124 end, 500);
125
126 module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza)
127         module:log("debug", "Proceeding with TLS on s2sout...");
128         session:reset_stream();
129         session.conn:starttls(session.ssl_ctx);
130         session.secure = false;
131         return true;
132 end);