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