Software /
code /
prosody
Annotate
plugins/mod_tls.lua @ 1910:14c043d7fb77
mod_tls: Mark session as not secure before negotiating TLS
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 05 Oct 2009 14:51:53 +0100 |
parent | 1903:b7552b1e267d |
child | 1911:bfe120db1ec4 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1219
diff
changeset
|
1 -- Prosody IM |
760
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | |
519
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
7 -- |
cccd610a0ef9
Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents:
438
diff
changeset
|
8 |
69
5b664c8fef86
forgot to commit mod_tls, oops :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local st = require "util.stanza"; |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
69
diff
changeset
|
10 |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
11 local xmlns_stream = 'http://etherx.jabber.org/streams'; |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
12 local xmlns_starttls = 'urn:ietf:params:xml:ns:xmpp-tls'; |
69
5b664c8fef86
forgot to commit mod_tls, oops :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
1652
ce6b91c78b9f
mod_tls: Updated to use module:get_option instead of configmanager
Waqas Hussain <waqas20@gmail.com>
parents:
1523
diff
changeset
|
14 local secure_auth_only = module:get_option("require_encryption"); |
1219
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
15 |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
357
diff
changeset
|
16 module:add_handler("c2s_unauthed", "starttls", xmlns_starttls, |
69
5b664c8fef86
forgot to commit mod_tls, oops :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 function (session, stanza) |
5b664c8fef86
forgot to commit mod_tls, oops :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 if session.conn.starttls then |
316
13e2bd256a20
Fixed mod_tls to use session.send for sending stanzas
Waqas Hussain <waqas20@gmail.com>
parents:
303
diff
changeset
|
19 session.send(st.stanza("proceed", { xmlns = xmlns_starttls })); |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
69
diff
changeset
|
20 session:reset_stream(); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
69
diff
changeset
|
21 session.conn.starttls(); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
69
diff
changeset
|
22 session.log("info", "TLS negotiation started..."); |
1213
de66fa750daf
sessionmanager, mod_tls: Mark a session as secure when TLS is active
Matthew Wild <mwild1@gmail.com>
parents:
896
diff
changeset
|
23 session.secure = false; |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
69
diff
changeset
|
24 else |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
69
diff
changeset
|
25 -- FIXME: What reply? |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
69
diff
changeset
|
26 session.log("warn", "Attempt to start TLS, but TLS is not available on this connection"); |
69
5b664c8fef86
forgot to commit mod_tls, oops :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 end |
5b664c8fef86
forgot to commit mod_tls, oops :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end); |
5b664c8fef86
forgot to commit mod_tls, oops :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
30 module:add_handler("s2sin_unauthed", "starttls", xmlns_starttls, |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
31 function (session, stanza) |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
32 if session.conn.starttls then |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
33 session.sends2s(st.stanza("proceed", { xmlns = xmlns_starttls })); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
34 session:reset_stream(); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
35 session.conn.starttls(); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
36 session.log("info", "TLS negotiation started for incoming s2s..."); |
1910
14c043d7fb77
mod_tls: Mark session as not secure before negotiating TLS
Matthew Wild <mwild1@gmail.com>
parents:
1903
diff
changeset
|
37 session.secure = false; |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
38 else |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
39 -- FIXME: What reply? |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
40 session.log("warn", "Attempt to start TLS, but TLS is not available on this s2s connection"); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
41 end |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
42 end); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
43 |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
44 |
357
17bcecb06420
Use a stanza for c2s stream features instead of an array of strings. Removes a FIXME.
Matthew Wild <mwild1@gmail.com>
parents:
316
diff
changeset
|
45 local starttls_attr = { xmlns = xmlns_starttls }; |
438
193f9dd64f17
Bumper commit for the new modulemanager API \o/ Updates all the modules, though some more changes may be in store.
Matthew Wild <mwild1@gmail.com>
parents:
357
diff
changeset
|
46 module:add_event_hook("stream-features", |
1675
bddd5ef9565e
Another unwanted spaces at the end of a line.
Tobias Markmann <tm@ayena.de>
parents:
1652
diff
changeset
|
47 function (session, features) |
705
11afa1d88c55
mod_saslauth, mod_tls: minor code cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
622
diff
changeset
|
48 if session.conn.starttls then |
1219
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
49 features:tag("starttls", starttls_attr); |
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
50 if secure_auth_only then |
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
51 features:tag("required"):up():up(); |
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
52 else |
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
53 features:up(); |
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
54 end |
705
11afa1d88c55
mod_saslauth, mod_tls: minor code cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
622
diff
changeset
|
55 end |
11afa1d88c55
mod_saslauth, mod_tls: minor code cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
622
diff
changeset
|
56 end); |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
57 |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
58 module:add_event_hook("s2s-stream-features", |
1894
53f34ba6f6d6
Minor changes; outgoing TLS works.
Paul Aurich <paul@darkrain42.org>
parents:
1875
diff
changeset
|
59 function (session, features) |
53f34ba6f6d6
Minor changes; outgoing TLS works.
Paul Aurich <paul@darkrain42.org>
parents:
1875
diff
changeset
|
60 -- This hook is possibly called once per host (at least if the |
53f34ba6f6d6
Minor changes; outgoing TLS works.
Paul Aurich <paul@darkrain42.org>
parents:
1875
diff
changeset
|
61 -- remote server does not specify a to/from. |
1903
b7552b1e267d
mod_tls: Only advertise TLS if the server told us which host they are connecting to
Matthew Wild <mwild1@gmail.com>
parents:
1896
diff
changeset
|
62 if session.to_host and session.conn.starttls and not features:child_with_ns(xmlns_starttls) then |
1896
e7ac9be5c90c
mod_tls: :up() out of the starttls tag in stream:features
Matthew Wild <mwild1@gmail.com>
parents:
1894
diff
changeset
|
63 features:tag("starttls", starttls_attr):up(); |
1894
53f34ba6f6d6
Minor changes; outgoing TLS works.
Paul Aurich <paul@darkrain42.org>
parents:
1875
diff
changeset
|
64 -- TODO: Make this optional :P |
53f34ba6f6d6
Minor changes; outgoing TLS works.
Paul Aurich <paul@darkrain42.org>
parents:
1875
diff
changeset
|
65 --features:tag("required"):up():up(); |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
66 end |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
67 end); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
68 |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
69 -- For s2sout connections, start TLS if we can |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
70 module:hook_stanza(xmlns_stream, "features", |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
71 function (session, stanza) |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
72 module:log("debug", "Received features element"); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
73 if stanza:child_with_ns(xmlns_starttls) then |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
74 module:log("%s is offering TLS, taking up the offer...", session.to_host); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
75 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>"); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
76 return true; |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
77 end |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
78 end, 500); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
79 |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
80 module:hook_stanza(xmlns_starttls, "proceed", |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
81 function (session, stanza) |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
82 module:log("debug", "Proceeding with TLS on s2sout..."); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
83 local format, to_host, from_host = string.format, session.to_host, session.from_host; |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
84 session:reset_stream(); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
85 session.conn.starttls(true); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
86 return true; |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
87 end); |