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