require_encryption deprecated, use c2s_require_encryption instead
[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("require_s2s_encryption");
16
17 module:add_handler("c2s_unauthed", "starttls", xmlns_starttls,
18                 function (session, stanza)
19                         if session.conn.starttls then
20                                 session.send(st.stanza("proceed", { xmlns = xmlns_starttls }));
21                                 session:reset_stream();
22                                 session.conn.starttls();
23                                 session.log("info", "TLS negotiation started...");
24                                 session.secure = false;
25                         else
26                                 -- FIXME: What reply?
27                                 session.log("warn", "Attempt to start TLS, but TLS is not available on this connection");
28                         end
29                 end);
30                 
31 module:add_handler("s2sin_unauthed", "starttls", xmlns_starttls,
32                 function (session, stanza)
33                         if session.conn.starttls then
34                                 session.sends2s(st.stanza("proceed", { xmlns = xmlns_starttls }));
35                                 session:reset_stream();
36                                 session.conn.starttls();
37                                 session.log("info", "TLS negotiation started for incoming s2s...");
38                                 session.secure = false;
39                         else
40                                 -- FIXME: What reply?
41                                 session.log("warn", "Attempt to start TLS, but TLS is not available on this s2s connection");
42                         end
43                 end);
44
45
46 local starttls_attr = { xmlns = xmlns_starttls };
47 module:add_event_hook("stream-features", 
48                 function (session, features)
49                         if 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:add_event_hook("s2s-stream-features", 
60                 function (session, features)
61                         -- This hook is possibly called once per host (at least if the
62                         -- remote server does not specify a to/from.
63                         if session.to_host and session.conn.starttls and not features:child_with_ns(xmlns_starttls) then
64                                 features:tag("starttls", starttls_attr):up();
65                                 if secure_s2s_only then
66                                         features:tag("required"):up():up();
67                                 else
68                                         features:up();
69                                 end
70                         end
71                 end);
72
73 -- For s2sout connections, start TLS if we can
74 module:hook_stanza(xmlns_stream, "features",
75                 function (session, stanza)
76                         module:log("debug", "Received features element");
77                         if stanza:child_with_ns(xmlns_starttls) then
78                                 module:log("%s is offering TLS, taking up the offer...", session.to_host);
79                                 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>");
80                                 return true;
81                         end
82                 end, 500);
83
84 module:hook_stanza(xmlns_starttls, "proceed",
85                 function (session, stanza)
86                         module:log("debug", "Proceeding with TLS on s2sout...");
87                         local format, to_host, from_host = string.format, session.to_host, session.from_host;
88                         session:reset_stream();
89                         session.conn.starttls(true);
90                         return true;
91                 end);