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