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