Uncertain merge with 0.5's SASL
[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_starttls ='urn:ietf:params:xml:ns:xmpp-tls';
12
13 local secure_auth_only = module:get_option("require_encryption");
14
15 module:add_handler("c2s_unauthed", "starttls", xmlns_starttls,
16                 function (session, stanza)
17                         if session.conn.starttls then
18                                 session.send(st.stanza("proceed", { xmlns = xmlns_starttls }));
19                                 session:reset_stream();
20                                 session.conn.starttls();
21                                 session.log("info", "TLS negotiation started...");
22                                 session.secure = false;
23                         else
24                                 -- FIXME: What reply?
25                                 session.log("warn", "Attempt to start TLS, but TLS is not available on this connection");
26                         end
27                 end);
28                 
29 local starttls_attr = { xmlns = xmlns_starttls };
30 module:add_event_hook("stream-features", 
31                 function (session, features)
32                         if session.conn.starttls then
33                                 features:tag("starttls", starttls_attr);
34                                 if secure_auth_only then
35                                         features:tag("required"):up():up();
36                                 else
37                                         features:up();
38                                 end
39                         end
40                 end);