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