mod_privacy: Fix selecting the top resource (fixes #694)
[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 config = require "core.configmanager";
10 local create_context = require "core.certmanager".create_context;
11 local st = require "util.stanza";
12
13 local c2s_require_encryption = module:get_option("c2s_require_encryption") or 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_proceed = st.stanza("proceed", starttls_attr);
26 local starttls_failure = st.stanza("failure", starttls_attr);
27 local c2s_feature = st.stanza("starttls", starttls_attr);
28 local s2s_feature = st.stanza("starttls", starttls_attr);
29 if c2s_require_encryption then c2s_feature:tag("required"):up(); end
30 if s2s_require_encryption then s2s_feature:tag("required"):up(); end
31
32 local global_ssl_ctx = prosody.global_ssl_ctx;
33
34 local hosts = prosody.hosts;
35 local host = hosts[module.host];
36
37 local function can_do_tls(session)
38         if session.type == "c2s_unauthed" then
39                 return session.conn.starttls and host.ssl_ctx_in;
40         elseif session.type == "s2sin_unauthed" and allow_s2s_tls then
41                 return session.conn.starttls and host.ssl_ctx_in;
42         elseif session.direction == "outgoing" and allow_s2s_tls then
43                 return session.conn.starttls and host.ssl_ctx;
44         end
45         return false;
46 end
47
48 -- Hook <starttls/>
49 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event)
50         local origin = event.origin;
51         if can_do_tls(origin) then
52                 (origin.sends2s or origin.send)(starttls_proceed);
53                 origin:reset_stream();
54                 local host = origin.to_host or origin.host;
55                 local ssl_ctx = host and hosts[host].ssl_ctx_in or global_ssl_ctx;
56                 origin.conn:starttls(ssl_ctx);
57                 origin.log("debug", "TLS negotiation started for %s...", origin.type);
58                 origin.secure = false;
59         else
60                 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type);
61                 (origin.sends2s or origin.send)(starttls_failure);
62                 origin:close();
63         end
64         return true;
65 end);
66
67 -- Advertize stream feature
68 module:hook("stream-features", function(event)
69         local origin, features = event.origin, event.features;
70         if can_do_tls(origin) then
71                 features:add_child(c2s_feature);
72         end
73 end);
74 module:hook("s2s-stream-features", function(event)
75         local origin, features = event.origin, event.features;
76         if can_do_tls(origin) then
77                 features:add_child(s2s_feature);
78         end
79 end);
80
81 -- For s2sout connections, start TLS if we can
82 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
83         module:log("debug", "Received features element");
84         if can_do_tls(session) and stanza:child_with_ns(xmlns_starttls) then
85                 module:log("debug", "%s is offering TLS, taking up the offer...", session.to_host);
86                 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>");
87                 return true;
88         end
89 end, 500);
90
91 module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza)
92         module:log("debug", "Proceeding with TLS on s2sout...");
93         session:reset_stream();
94         local ssl_ctx = session.from_host and hosts[session.from_host].ssl_ctx or global_ssl_ctx;
95         session.conn:starttls(ssl_ctx);
96         session.secure = false;
97         return true;
98 end);
99
100 local function assert_log(ret, err)
101         if not ret then
102                 module:log("error", "Unable to initialize TLS: %s", err);
103         end
104         return ret;
105 end
106
107 function module.load()
108         local ssl_config = config.rawget(module.host, "ssl");
109         if not ssl_config then
110                 local base_host = module.host:match("%.(.*)");
111                 ssl_config = config.get(base_host, "ssl");
112         end
113         host.ssl_ctx = assert_log(create_context(host.host, "client", ssl_config)); -- for outgoing connections
114         host.ssl_ctx_in = assert_log(create_context(host.host, "server", ssl_config)); -- for incoming connections
115 end
116
117 function module.unload()
118         host.ssl_ctx = nil;
119         host.ssl_ctx_in = nil;
120 end