Software /
code /
prosody
Annotate
plugins/mod_tls.lua @ 5988:177f233b5211
mod_tls: Let s2s_secure_auth override s2s_require_encryption and warn if they differ
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 15 Jan 2014 22:47:50 +0100 |
parent | 5987:bd90250ee1ee |
child | 5993:ef11b8bab405 |
rev | line source |
---|---|
1523
841d61be198f
Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents:
1219
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2877
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2877
diff
changeset
|
3 -- Copyright (C) 2008-2010 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 |
3583
ef86ba720f00
mod_tls: Let hosts without an 'ssl' option inherit it from their parent hosts.
Waqas Hussain <waqas20@gmail.com>
parents:
3574
diff
changeset
|
9 local config = require "core.configmanager"; |
3571
675d65036f31
certmanager, hostmanager, mod_tls: Move responsibility for creating per-host SSL contexts to mod_tls, meaning reloading certs is now as trivial as reloading mod_tls
Matthew Wild <mwild1@gmail.com>
parents:
3397
diff
changeset
|
10 local create_context = require "core.certmanager".create_context; |
69
5b664c8fef86
forgot to commit mod_tls, oops :)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local st = require "util.stanza"; |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
69
diff
changeset
|
12 |
5987
bd90250ee1ee
mod_tls: Rename variables to be less confusing
Kim Alvefur <zash@zash.se>
parents:
5976
diff
changeset
|
13 local c2s_require_encryption = module:get_option("c2s_require_encryption") or module:get_option("require_encryption"); |
bd90250ee1ee
mod_tls: Rename variables to be less confusing
Kim Alvefur <zash@zash.se>
parents:
5976
diff
changeset
|
14 local s2s_require_encryption = module:get_option("s2s_require_encryption"); |
2933 | 15 local allow_s2s_tls = module:get_option("s2s_allow_encryption") ~= false; |
5988
177f233b5211
mod_tls: Let s2s_secure_auth override s2s_require_encryption and warn if they differ
Kim Alvefur <zash@zash.se>
parents:
5987
diff
changeset
|
16 local s2s_secure_auth = module:get_option("s2s_secure_auth"); |
177f233b5211
mod_tls: Let s2s_secure_auth override s2s_require_encryption and warn if they differ
Kim Alvefur <zash@zash.se>
parents:
5987
diff
changeset
|
17 |
177f233b5211
mod_tls: Let s2s_secure_auth override s2s_require_encryption and warn if they differ
Kim Alvefur <zash@zash.se>
parents:
5987
diff
changeset
|
18 if s2s_secure_auth and s2s_require_encryption == false then |
177f233b5211
mod_tls: Let s2s_secure_auth override s2s_require_encryption and warn if they differ
Kim Alvefur <zash@zash.se>
parents:
5987
diff
changeset
|
19 module:log("warn", "s2s_secure_auth implies s2s_require_encryption, but s2s_require_encryption is set to false"); |
177f233b5211
mod_tls: Let s2s_secure_auth override s2s_require_encryption and warn if they differ
Kim Alvefur <zash@zash.se>
parents:
5987
diff
changeset
|
20 s2s_require_encryption = true; |
177f233b5211
mod_tls: Let s2s_secure_auth override s2s_require_encryption and warn if they differ
Kim Alvefur <zash@zash.se>
parents:
5987
diff
changeset
|
21 end |
1219
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
22 |
2605 | 23 local xmlns_starttls = 'urn:ietf:params:xml:ns:xmpp-tls'; |
24 local starttls_attr = { xmlns = xmlns_starttls }; | |
25 local starttls_proceed = st.stanza("proceed", starttls_attr); | |
26 local starttls_failure = st.stanza("failure", starttls_attr); | |
27 local c2s_feature = st.stanza("starttls", starttls_attr); | |
28 local s2s_feature = st.stanza("starttls", starttls_attr); | |
5987
bd90250ee1ee
mod_tls: Rename variables to be less confusing
Kim Alvefur <zash@zash.se>
parents:
5976
diff
changeset
|
29 if c2s_require_encryption then c2s_feature:tag("required"):up(); end |
bd90250ee1ee
mod_tls: Rename variables to be less confusing
Kim Alvefur <zash@zash.se>
parents:
5976
diff
changeset
|
30 if s2s_require_encryption then s2s_feature:tag("required"):up(); end |
2605 | 31 |
2542
0a78847b38e2
mod_tls: Update for new server SSL syntax
Matthew Wild <mwild1@gmail.com>
parents:
2108
diff
changeset
|
32 local global_ssl_ctx = prosody.global_ssl_ctx; |
1219
f14e08a0ae7f
mod_tls: Add <required/> to stream feature when TLS is required
Matthew Wild <mwild1@gmail.com>
parents:
1213
diff
changeset
|
33 |
5370
7838acadb0fa
mod_announce, mod_auth_anonymous, mod_c2s, mod_c2s, mod_component, mod_iq, mod_message, mod_presence, mod_tls: Access prosody.{hosts,bare_sessions,full_sessions} instead of the old globals
Kim Alvefur <zash@zash.se>
parents:
4475
diff
changeset
|
34 local hosts = prosody.hosts; |
2872
cdc292d201fc
mod_tls: Don't offer TLS on hosts that don't have any certs
Matthew Wild <mwild1@gmail.com>
parents:
2854
diff
changeset
|
35 local host = hosts[module.host]; |
cdc292d201fc
mod_tls: Don't offer TLS on hosts that don't have any certs
Matthew Wild <mwild1@gmail.com>
parents:
2854
diff
changeset
|
36 |
2625
03287c06d986
mod_tls: Refactor to simplify detection of whether we can do TLS on a connection
Matthew Wild <mwild1@gmail.com>
parents:
2623
diff
changeset
|
37 local function can_do_tls(session) |
03287c06d986
mod_tls: Refactor to simplify detection of whether we can do TLS on a connection
Matthew Wild <mwild1@gmail.com>
parents:
2623
diff
changeset
|
38 if session.type == "c2s_unauthed" then |
2636
d2805ad5b736
mod_tls: Ban TLS after auth, not before.
Waqas Hussain <waqas20@gmail.com>
parents:
2635
diff
changeset
|
39 return session.conn.starttls and host.ssl_ctx_in; |
2933 | 40 elseif session.type == "s2sin_unauthed" and allow_s2s_tls then |
2636
d2805ad5b736
mod_tls: Ban TLS after auth, not before.
Waqas Hussain <waqas20@gmail.com>
parents:
2635
diff
changeset
|
41 return session.conn.starttls and host.ssl_ctx_in; |
2933 | 42 elseif session.direction == "outgoing" and allow_s2s_tls then |
2654
07a6f5f9d4be
mod_tls: Only negotiate TLS on outgoing s2s connections if we have an SSL context (thanks Flo...)
Matthew Wild <mwild1@gmail.com>
parents:
2636
diff
changeset
|
43 return session.conn.starttls and host.ssl_ctx; |
2625
03287c06d986
mod_tls: Refactor to simplify detection of whether we can do TLS on a connection
Matthew Wild <mwild1@gmail.com>
parents:
2623
diff
changeset
|
44 end |
03287c06d986
mod_tls: Refactor to simplify detection of whether we can do TLS on a connection
Matthew Wild <mwild1@gmail.com>
parents:
2623
diff
changeset
|
45 return false; |
2932
d2816fb6c7ea
mod_tls: Add s2s_allow_encryption option which, when set to false, disabled TLS for s2s
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
46 end |
d2816fb6c7ea
mod_tls: Add s2s_allow_encryption option which, when set to false, disabled TLS for s2s
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
47 |
2605 | 48 -- Hook <starttls/> |
2600
1e6f3002e04f
mod_tls: Inlined some code.
Waqas Hussain <waqas20@gmail.com>
parents:
2596
diff
changeset
|
49 module:hook("stanza/urn:ietf:params:xml:ns:xmpp-tls:starttls", function(event) |
1e6f3002e04f
mod_tls: Inlined some code.
Waqas Hussain <waqas20@gmail.com>
parents:
2596
diff
changeset
|
50 local origin = event.origin; |
2625
03287c06d986
mod_tls: Refactor to simplify detection of whether we can do TLS on a connection
Matthew Wild <mwild1@gmail.com>
parents:
2623
diff
changeset
|
51 if can_do_tls(origin) then |
2605 | 52 (origin.sends2s or origin.send)(starttls_proceed); |
2600
1e6f3002e04f
mod_tls: Inlined some code.
Waqas Hussain <waqas20@gmail.com>
parents:
2596
diff
changeset
|
53 origin:reset_stream(); |
1e6f3002e04f
mod_tls: Inlined some code.
Waqas Hussain <waqas20@gmail.com>
parents:
2596
diff
changeset
|
54 local host = origin.to_host or origin.host; |
2596
187cd90860cb
mod_tls: Merged duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents:
2595
diff
changeset
|
55 local ssl_ctx = host and hosts[host].ssl_ctx_in or global_ssl_ctx; |
2600
1e6f3002e04f
mod_tls: Inlined some code.
Waqas Hussain <waqas20@gmail.com>
parents:
2596
diff
changeset
|
56 origin.conn:starttls(ssl_ctx); |
4157
1b5a8e071a80
mod_tls: Drop 'TLS negotiation started for ...' to debug level from info
Matthew Wild <mwild1@gmail.com>
parents:
3583
diff
changeset
|
57 origin.log("debug", "TLS negotiation started for %s...", origin.type); |
2600
1e6f3002e04f
mod_tls: Inlined some code.
Waqas Hussain <waqas20@gmail.com>
parents:
2596
diff
changeset
|
58 origin.secure = false; |
2595
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
59 else |
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
60 origin.log("warn", "Attempt to start TLS, but TLS is not available on this %s connection", origin.type); |
2605 | 61 (origin.sends2s or origin.send)(starttls_failure); |
2601
e64c6a4aa50b
mod_tls: Respond with proper error when TLS cannot be negotiated.
Waqas Hussain <waqas20@gmail.com>
parents:
2600
diff
changeset
|
62 origin:close(); |
2595
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
63 end |
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
64 return true; |
015934e20f03
mod_tls: Switched to new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2594
diff
changeset
|
65 end); |
2932
d2816fb6c7ea
mod_tls: Add s2s_allow_encryption option which, when set to false, disabled TLS for s2s
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
66 |
2605 | 67 -- Advertize stream feature |
2607
35a5d1c5ea28
mod_tls: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2605
diff
changeset
|
68 module:hook("stream-features", function(event) |
35a5d1c5ea28
mod_tls: Hook stream-features event using new events API.
Waqas Hussain <waqas20@gmail.com>
parents:
2605
diff
changeset
|
69 local origin, features = event.origin, event.features; |
2625
03287c06d986
mod_tls: Refactor to simplify detection of whether we can do TLS on a connection
Matthew Wild <mwild1@gmail.com>
parents:
2623
diff
changeset
|
70 if can_do_tls(origin) then |
2605 | 71 features:add_child(c2s_feature); |
72 end | |
73 end); | |
74 module:hook("s2s-stream-features", function(event) | |
2613
afa20941e098
s2smanager, mod_compression, mod_tls: Changed event.session to event.origin for s2s-stream-features event for consistency.
Waqas Hussain <waqas20@gmail.com>
parents:
2607
diff
changeset
|
75 local origin, features = event.origin, event.features; |
2625
03287c06d986
mod_tls: Refactor to simplify detection of whether we can do TLS on a connection
Matthew Wild <mwild1@gmail.com>
parents:
2623
diff
changeset
|
76 if can_do_tls(origin) then |
2605 | 77 features:add_child(s2s_feature); |
78 end | |
79 end); | |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
80 |
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
81 -- For s2sout connections, start TLS if we can |
2605 | 82 module:hook_stanza("http://etherx.jabber.org/streams", "features", function (session, stanza) |
83 module:log("debug", "Received features element"); | |
2654
07a6f5f9d4be
mod_tls: Only negotiate TLS on outgoing s2s connections if we have an SSL context (thanks Flo...)
Matthew Wild <mwild1@gmail.com>
parents:
2636
diff
changeset
|
84 if can_do_tls(session) and stanza:child_with_ns(xmlns_starttls) then |
4475
7341cc5c8da9
mod_tls: Fix log statement (thanks Zash)
Matthew Wild <mwild1@gmail.com>
parents:
4244
diff
changeset
|
85 module:log("debug", "%s is offering TLS, taking up the offer...", session.to_host); |
2605 | 86 session.sends2s("<starttls xmlns='"..xmlns_starttls.."'/>"); |
87 return true; | |
88 end | |
89 end, 500); | |
1875
334383faf77b
mod_tls: Advertise and handle TLS for s2s connections
Matthew Wild <mwild1@gmail.com>
parents:
1675
diff
changeset
|
90 |
2605 | 91 module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza) |
92 module:log("debug", "Proceeding with TLS on s2sout..."); | |
93 session:reset_stream(); | |
94 local ssl_ctx = session.from_host and hosts[session.from_host].ssl_ctx or global_ssl_ctx; | |
3397
f376f0bd1d1f
mod_tls: Remove extraneous flag to starttls() for s2sout connecections
Matthew Wild <mwild1@gmail.com>
parents:
2933
diff
changeset
|
95 session.conn:starttls(ssl_ctx); |
2605 | 96 session.secure = false; |
97 return true; | |
98 end); | |
3571
675d65036f31
certmanager, hostmanager, mod_tls: Move responsibility for creating per-host SSL contexts to mod_tls, meaning reloading certs is now as trivial as reloading mod_tls
Matthew Wild <mwild1@gmail.com>
parents:
3397
diff
changeset
|
99 |
5976
872ff4851c9b
mod_tls: Log error when TLS initialization fails
Matthew Wild <mwild1@gmail.com>
parents:
5378
diff
changeset
|
100 local function assert_log(ret, err) |
872ff4851c9b
mod_tls: Log error when TLS initialization fails
Matthew Wild <mwild1@gmail.com>
parents:
5378
diff
changeset
|
101 if not ret then |
872ff4851c9b
mod_tls: Log error when TLS initialization fails
Matthew Wild <mwild1@gmail.com>
parents:
5378
diff
changeset
|
102 module:log("error", "Unable to initialize TLS: %s", err); |
872ff4851c9b
mod_tls: Log error when TLS initialization fails
Matthew Wild <mwild1@gmail.com>
parents:
5378
diff
changeset
|
103 end |
872ff4851c9b
mod_tls: Log error when TLS initialization fails
Matthew Wild <mwild1@gmail.com>
parents:
5378
diff
changeset
|
104 return ret; |
872ff4851c9b
mod_tls: Log error when TLS initialization fails
Matthew Wild <mwild1@gmail.com>
parents:
5378
diff
changeset
|
105 end |
872ff4851c9b
mod_tls: Log error when TLS initialization fails
Matthew Wild <mwild1@gmail.com>
parents:
5378
diff
changeset
|
106 |
3571
675d65036f31
certmanager, hostmanager, mod_tls: Move responsibility for creating per-host SSL contexts to mod_tls, meaning reloading certs is now as trivial as reloading mod_tls
Matthew Wild <mwild1@gmail.com>
parents:
3397
diff
changeset
|
107 function module.load() |
5378
ec3accda44d3
mod_tls: More use of config sections removed
Kim Alvefur <zash@zash.se>
parents:
5370
diff
changeset
|
108 local ssl_config = config.rawget(module.host, "ssl"); |
4244
19c9bf88ec89
mod_tls: Fix for components to more reliably inherit SSL settings from their parenthost (thanks Link Mauve)
Matthew Wild <mwild1@gmail.com>
parents:
4157
diff
changeset
|
109 if not ssl_config then |
19c9bf88ec89
mod_tls: Fix for components to more reliably inherit SSL settings from their parenthost (thanks Link Mauve)
Matthew Wild <mwild1@gmail.com>
parents:
4157
diff
changeset
|
110 local base_host = module.host:match("%.(.*)"); |
5378
ec3accda44d3
mod_tls: More use of config sections removed
Kim Alvefur <zash@zash.se>
parents:
5370
diff
changeset
|
111 ssl_config = config.get(base_host, "ssl"); |
3583
ef86ba720f00
mod_tls: Let hosts without an 'ssl' option inherit it from their parent hosts.
Waqas Hussain <waqas20@gmail.com>
parents:
3574
diff
changeset
|
112 end |
5976
872ff4851c9b
mod_tls: Log error when TLS initialization fails
Matthew Wild <mwild1@gmail.com>
parents:
5378
diff
changeset
|
113 host.ssl_ctx = assert_log(create_context(host.host, "client", ssl_config)); -- for outgoing connections |
872ff4851c9b
mod_tls: Log error when TLS initialization fails
Matthew Wild <mwild1@gmail.com>
parents:
5378
diff
changeset
|
114 host.ssl_ctx_in = assert_log(create_context(host.host, "server", ssl_config)); -- for incoming connections |
3571
675d65036f31
certmanager, hostmanager, mod_tls: Move responsibility for creating per-host SSL contexts to mod_tls, meaning reloading certs is now as trivial as reloading mod_tls
Matthew Wild <mwild1@gmail.com>
parents:
3397
diff
changeset
|
115 end |
675d65036f31
certmanager, hostmanager, mod_tls: Move responsibility for creating per-host SSL contexts to mod_tls, meaning reloading certs is now as trivial as reloading mod_tls
Matthew Wild <mwild1@gmail.com>
parents:
3397
diff
changeset
|
116 |
675d65036f31
certmanager, hostmanager, mod_tls: Move responsibility for creating per-host SSL contexts to mod_tls, meaning reloading certs is now as trivial as reloading mod_tls
Matthew Wild <mwild1@gmail.com>
parents:
3397
diff
changeset
|
117 function module.unload() |
675d65036f31
certmanager, hostmanager, mod_tls: Move responsibility for creating per-host SSL contexts to mod_tls, meaning reloading certs is now as trivial as reloading mod_tls
Matthew Wild <mwild1@gmail.com>
parents:
3397
diff
changeset
|
118 host.ssl_ctx = nil; |
675d65036f31
certmanager, hostmanager, mod_tls: Move responsibility for creating per-host SSL contexts to mod_tls, meaning reloading certs is now as trivial as reloading mod_tls
Matthew Wild <mwild1@gmail.com>
parents:
3397
diff
changeset
|
119 host.ssl_ctx_in = nil; |
675d65036f31
certmanager, hostmanager, mod_tls: Move responsibility for creating per-host SSL contexts to mod_tls, meaning reloading certs is now as trivial as reloading mod_tls
Matthew Wild <mwild1@gmail.com>
parents:
3397
diff
changeset
|
120 end |