Comparison

plugins/mod_tls.lua @ 2605:ade70495fe7f

mod_tls: Cleanup.
author Waqas Hussain <waqas20@gmail.com>
date Fri, 12 Feb 2010 03:46:48 +0500
parent 2604:ed32f7bad620
child 2607:35a5d1c5ea28
comparison
equal deleted inserted replaced
2604:ed32f7bad620 2605:ade70495fe7f
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local st = require "util.stanza"; 9 local st = require "util.stanza";
10 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"); 11 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"); 12 local secure_s2s_only = module:get_option("s2s_require_encryption");
16 13
14 local xmlns_starttls = 'urn:ietf:params:xml:ns:xmpp-tls';
15 local starttls_attr = { xmlns = xmlns_starttls };
16 local starttls_proceed = st.stanza("proceed", starttls_attr);
17 local starttls_failure = st.stanza("failure", starttls_attr);
18 local c2s_feature = st.stanza("starttls", starttls_attr);
19 local s2s_feature = st.stanza("starttls", starttls_attr);
20 if secure_auth_only then c2s_feature:tag("required"):up(); end
21 if secure_s2s_only then s2s_feature:tag("required"):up(); end
22
17 local global_ssl_ctx = prosody.global_ssl_ctx; 23 local global_ssl_ctx = prosody.global_ssl_ctx;
18 24
25 -- Hook <starttls/>
19 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event) 26 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event)
20 local origin = event.origin; 27 local origin = event.origin;
21 if origin.conn.starttls then 28 if origin.conn.starttls then
22 (origin.sends2s or origin.send)(st.stanza("proceed", { xmlns = xmlns_starttls })); 29 (origin.sends2s or origin.send)(starttls_proceed);
23 origin:reset_stream(); 30 origin:reset_stream();
24 local host = origin.to_host or origin.host; 31 local host = origin.to_host or origin.host;
25 local ssl_ctx = host and hosts[host].ssl_ctx_in or global_ssl_ctx; 32 local ssl_ctx = host and hosts[host].ssl_ctx_in or global_ssl_ctx;
26 origin.conn:starttls(ssl_ctx); 33 origin.conn:starttls(ssl_ctx);
27 origin.log("info", "TLS negotiation started for %s...", origin.type); 34 origin.log("info", "TLS negotiation started for %s...", origin.type);
28 origin.secure = false; 35 origin.secure = false;
29 else 36 else
30 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type); 37 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type);
31 (origin.sends2s or origin.send)(st.stanza("failure", { xmlns = xmlns_starttls })); 38 (origin.sends2s or origin.send)(starttls_failure);
32 origin:close(); 39 origin:close();
33 end 40 end
34 return true; 41 return true;
35 end); 42 end);
36 43
37 44 -- Advertize stream feature
38 local starttls_attr = { xmlns = xmlns_starttls }; 45 module:add_event_hook("stream-features", function(session, features)
39 module:add_event_hook("stream-features", 46 if not session.username and session.conn.starttls then
40 function (session, features) 47 features:add_child(c2s_feature);
41 if not session.username and session.conn.starttls then 48 end
42 features:tag("starttls", starttls_attr); 49 end);
43 if secure_auth_only then 50 module:hook("s2s-stream-features", function(event)
44 features:tag("required"):up():up(); 51 local session, features = event.session, event.features;
45 else 52 if session.to_host and session.type ~= "s2sin" and session.conn.starttls then
46 features:up(); 53 features:add_child(s2s_feature);
47 end 54 end
48 end 55 end);
49 end);
50
51 module:hook("s2s-stream-features",
52 function (data)
53 local session, features = data.session, data.features;
54 if session.to_host and session.type ~= "s2sin" and session.conn.starttls then
55 features:tag("starttls", starttls_attr)
56 if secure_s2s_only then
57 features:tag("required"):up():up();
58 else
59 features:up();
60 end
61 end
62 end);
63 56
64 -- For s2sout connections, start TLS if we can 57 -- For s2sout connections, start TLS if we can
65 module:hook_stanza(xmlns_stream, "features", 58 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza)
66 function (session, stanza) 59 module:log("debug", "Received features element");
67 module:log("debug", "Received features element"); 60 if session.conn.starttls and stanza:child_with_ns(xmlns_starttls) then
68 if session.conn.starttls and stanza:child_with_ns(xmlns_starttls) then 61 module:log("%s is offering TLS, taking up the offer...", session.to_host);
69 module:log("%s is offering TLS, taking up the offer...", session.to_host); 62 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>");
70 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>"); 63 return true;
71 return true; 64 end
72 end 65 end, 500);
73 end, 500); 66 module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza)
74 67 module:log("debug", "Proceeding with TLS on s2sout...");
75 module:hook_stanza(xmlns_starttls, "proceed", 68 session:reset_stream();
76 function (session, stanza) 69 local ssl_ctx = session.from_host and hosts[session.from_host].ssl_ctx or global_ssl_ctx;
77 module:log("debug", "Proceeding with TLS on s2sout..."); 70 session.conn:starttls(ssl_ctx, true);
78 session:reset_stream(); 71 session.secure = false;
79 local ssl_ctx = session.from_host and hosts[session.from_host].ssl_ctx or global_ssl_ctx; 72 return true;
80 session.conn:starttls(ssl_ctx, true); 73 end);
81 session.secure = false;
82 return true;
83 end);