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