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