Software /
code /
prosody
Annotate
plugins/mod_tls.lua @ 2596:187cd90860cb
mod_tls: Merged duplicate code.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 12 Feb 2010 02:15:54 +0500 |
parent | 2595:015934e20f03 |
child | 2600:1e6f3002e04f |
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 |
1912
126401a7159f
require_encryption deprecated, use c2s_require_encryption instead
Matthew Wild <mwild1@gmail.com>
parents:
1911
diff
changeset
|
14 local secure_auth_only = module:get_option("c2s_require_encryption") or module:get_option("require_encryption"); |
1913
da49a59dff7c
mod_tls: require_s2s_encryption -> s2s_require_encryption
Matthew Wild <mwild1@gmail.com>
parents:
1912
diff
changeset
|
15 local secure_s2s_only = module:get_option("s2s_require_encryption"); |
1219
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
16 |
2542
0a78847b38e2
mod_tls: Update for new server SSL syntax
Matthew Wild <mwild1@gmail.com>
parents:
2108
diff
changeset
|
17 local global_ssl_ctx = prosody.global_ssl_ctx; |
0a78847b38e2
mod_tls: Update for new server SSL syntax
Matthew Wild <mwild1@gmail.com>
parents:
2108
diff
changeset
|
18 |
2596
187cd90860cb
mod_tls: Merged duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents:
2595
diff
changeset
|
19 function starttls_handler(session, stanza) |
2594
ab52fdd9f5d0
mod_tls: Slight refactoring.
Waqas Hussain <waqas20@gmail.com>
parents:
2576
diff
changeset
|
20 if session.conn.starttls then |
2596
187cd90860cb
mod_tls: Merged duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents:
2595
diff
changeset
|
21 (session.sends2s or session.send)(st.stanza("proceed", { xmlns = xmlns_starttls })); |
2594
ab52fdd9f5d0
mod_tls: Slight refactoring.
Waqas Hussain <waqas20@gmail.com>
parents:
2576
diff
changeset
|
22 session:reset_stream(); |
2596
187cd90860cb
mod_tls: Merged duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents:
2595
diff
changeset
|
23 local host = session.to_host or session.host; |
187cd90860cb
mod_tls: Merged duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents:
2595
diff
changeset
|
24 local ssl_ctx = host and hosts[host].ssl_ctx_in or global_ssl_ctx; |
2594
ab52fdd9f5d0
mod_tls: Slight refactoring.
Waqas Hussain <waqas20@gmail.com>
parents:
2576
diff
changeset
|
25 session.conn:starttls(ssl_ctx); |
2596
187cd90860cb
mod_tls: Merged duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents:
2595
diff
changeset
|
26 session.log("info", "TLS negotiation started for %s...", session.type); |
2594
ab52fdd9f5d0
mod_tls: Slight refactoring.
Waqas Hussain <waqas20@gmail.com>
parents:
2576
diff
changeset
|
27 session.secure = false; |
ab52fdd9f5d0
mod_tls: Slight refactoring.
Waqas Hussain <waqas20@gmail.com>
parents:
2576
diff
changeset
|
28 else |
ab52fdd9f5d0
mod_tls: Slight refactoring.
Waqas Hussain <waqas20@gmail.com>
parents:
2576
diff
changeset
|
29 -- FIXME: What reply? |
2596
187cd90860cb
mod_tls: Merged duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents:
2595
diff
changeset
|
30 session.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", session.type); |
2594
ab52fdd9f5d0
mod_tls: Slight refactoring.
Waqas Hussain <waqas20@gmail.com>
parents:
2576
diff
changeset
|
31 end |
ab52fdd9f5d0
mod_tls: Slight refactoring.
Waqas Hussain <waqas20@gmail.com>
parents:
2576
diff
changeset
|
32 end |
ab52fdd9f5d0
mod_tls: Slight refactoring.
Waqas Hussain <waqas20@gmail.com>
parents:
2576
diff
changeset
|
33 |
2595
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
34 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event) |
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
35 local origin, stanza = event.origin, event.stanza; |
2596
187cd90860cb
mod_tls: Merged duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents:
2595
diff
changeset
|
36 if origin.type == "c2s_unauthed" or origin.type == "s2sin_unauthed" then |
187cd90860cb
mod_tls: Merged duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents:
2595
diff
changeset
|
37 starttls_handler(origin, stanza); |
2595
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
38 else |
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
39 -- FIXME: What reply? |
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
40 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type); |
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
41 end |
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
42 return true; |
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
43 end); |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
44 |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
45 |
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
|
46 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
|
47 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
|
48 function (session, features) |
2576
b70e73872c4d
mod_tls: Don't advertise TLS after authentication.
Waqas Hussain <waqas20@gmail.com>
parents:
2544
diff
changeset
|
49 if not session.username and 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
|
50 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
|
51 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
|
52 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
|
53 else |
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
54 features:up(); |
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
55 end |
705
11afa1d88c55
mod_saslauth, mod_tls: minor code cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
622
diff
changeset
|
56 end |
11afa1d88c55
mod_saslauth, mod_tls: minor code cleanup
Waqas Hussain <waqas20@gmail.com>
parents:
622
diff
changeset
|
57 end); |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
58 |
1938
82342e7f1b84
mod_tls: Catch s2s-stream-features and add starttls feature if possible
Matthew Wild <mwild1@gmail.com>
parents:
1926
diff
changeset
|
59 module:hook("s2s-stream-features", |
82342e7f1b84
mod_tls: Catch s2s-stream-features and add starttls feature if possible
Matthew Wild <mwild1@gmail.com>
parents:
1926
diff
changeset
|
60 function (data) |
82342e7f1b84
mod_tls: Catch s2s-stream-features and add starttls feature if possible
Matthew Wild <mwild1@gmail.com>
parents:
1926
diff
changeset
|
61 local session, features = data.session, data.features; |
2576
b70e73872c4d
mod_tls: Don't advertise TLS after authentication.
Waqas Hussain <waqas20@gmail.com>
parents:
2544
diff
changeset
|
62 if session.to_host and session.type ~= "s2sin" and session.conn.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(); |
1911
bfe120db1ec4
mod_tls: Mark starttls feature as <required/> if require_s2s_encryption is enabled
Matthew Wild <mwild1@gmail.com>
parents:
1910
diff
changeset
|
64 if secure_s2s_only then |
bfe120db1ec4
mod_tls: Mark starttls feature as <required/> if require_s2s_encryption is enabled
Matthew Wild <mwild1@gmail.com>
parents:
1910
diff
changeset
|
65 features:tag("required"):up():up(); |
bfe120db1ec4
mod_tls: Mark starttls feature as <required/> if require_s2s_encryption is enabled
Matthew Wild <mwild1@gmail.com>
parents:
1910
diff
changeset
|
66 else |
bfe120db1ec4
mod_tls: Mark starttls feature as <required/> if require_s2s_encryption is enabled
Matthew Wild <mwild1@gmail.com>
parents:
1910
diff
changeset
|
67 features:up(); |
bfe120db1ec4
mod_tls: Mark starttls feature as <required/> if require_s2s_encryption is enabled
Matthew Wild <mwild1@gmail.com>
parents:
1910
diff
changeset
|
68 end |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
69 end |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
70 end); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
71 |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
72 -- 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
|
73 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
|
74 function (session, stanza) |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
75 module:log("debug", "Received features element"); |
1943
3d4c703d9333
mod_tls: Don't try to start TLS if we can't actually do it (thanks Florob)
Matthew Wild <mwild1@gmail.com>
parents:
1938
diff
changeset
|
76 if session.conn.starttls and stanza:child_with_ns(xmlns_starttls) then |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
77 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
|
78 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
|
79 return true; |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
80 end |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
81 end, 500); |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
82 |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
83 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
|
84 function (session, stanza) |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
85 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
|
86 session:reset_stream(); |
2543
da16a5c0df85
mod_tls: Set the sslctx on outgoing connections (possibly the cause of outgoing s2s connections not being encrypted)
Matthew Wild <mwild1@gmail.com>
parents:
2542
diff
changeset
|
87 local ssl_ctx = session.from_host and hosts[session.from_host].ssl_ctx or global_ssl_ctx; |
da16a5c0df85
mod_tls: Set the sslctx on outgoing connections (possibly the cause of outgoing s2s connections not being encrypted)
Matthew Wild <mwild1@gmail.com>
parents:
2542
diff
changeset
|
88 session.conn:starttls(ssl_ctx, true); |
1926
e1c5b537f240
mod_tls: Mark sessions as not secure when negotiating outward TLS, so they get marked secure later. Fixes missing (encrypted) for outgoing sessions in s2s:show(). Thanks albert, McKael :)
Matthew Wild <mwild1@gmail.com>
parents:
1913
diff
changeset
|
89 session.secure = false; |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
90 return true; |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
91 end); |