mod_dialback: Short-circuit dialback auth if certificate is considered valid
[prosody.git] / plugins / mod_dialback.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 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 hosts = _G.hosts;
10
11 local log = module._log;
12
13 local st = require "util.stanza";
14 local sha256_hash = require "util.hashes".sha256;
15 local nameprep = require "util.encodings".stringprep.nameprep;
16 local check_cert_status = module:depends"s2s".check_cert_status;
17 local uuid_gen = require"util.uuid".generate;
18
19 local xmlns_stream = "http://etherx.jabber.org/streams";
20
21 local dialback_requests = setmetatable({}, { __mode = 'v' });
22
23 local dialback_secret = module.host .. module:get_option_string("dialback_secret", uuid_gen());
24 local dwd = module:get_option_boolean("dialback_without_dialback", false);
25
26 function module.save()
27         return { dialback_secret = dialback_secret };
28 end
29
30 function module.restore(state)
31         dialback_secret = state.dialback_secret;
32 end
33
34 function generate_dialback(id, to, from)
35         return sha256_hash(id..to..dialback_secret, true);
36 end
37
38 function initiate_dialback(session)
39         -- generate dialback key
40         session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host);
41         session.sends2s(st.stanza("db:result", { from = session.from_host, to = session.to_host }):text(session.dialback_key));
42         session.log("debug", "sent dialback key on outgoing s2s stream");
43 end
44
45 function verify_dialback(id, to, from, key)
46         return key == generate_dialback(id, to, from);
47 end
48
49 module:hook("stanza/jabber:server:dialback:verify", function(event)
50         local origin, stanza = event.origin, event.stanza;
51
52         if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
53                 -- We are being asked to verify the key, to ensure it was generated by us
54                 origin.log("debug", "verifying that dialback key is ours...");
55                 local attr = stanza.attr;
56                 if attr.type then
57                         module:log("warn", "Ignoring incoming session from %s claiming a dialback key for %s is %s",
58                                 origin.from_host or "(unknown)", attr.from or "(unknown)", attr.type);
59                         return true;
60                 end
61                 -- COMPAT: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
62                 --if attr.from ~= origin.to_host then error("invalid-from"); end
63                 local type;
64                 if verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
65                         type = "valid"
66                 else
67                         type = "invalid"
68                         origin.log("warn", "Asked to verify a dialback key that was incorrect. An imposter is claiming to be %s?", attr.to);
69                 end
70                 origin.log("debug", "verified dialback key... it is %s", type);
71                 origin.sends2s(st.stanza("db:verify", { from = attr.to, to = attr.from, id = attr.id, type = type }):text(stanza[1]));
72                 return true;
73         end
74 end);
75
76 module:hook("stanza/jabber:server:dialback:result", function(event)
77         local origin, stanza = event.origin, event.stanza;
78
79         if origin.type == "s2sin_unauthed" or origin.type == "s2sin" then
80                 -- he wants to be identified through dialback
81                 -- We need to check the key with the Authoritative server
82                 local attr = stanza.attr;
83                 local to, from = nameprep(attr.to), nameprep(attr.from);
84
85                 if check_cert_status(origin, from) == false then
86                         return
87                 elseif origin.cert_chain_status == "valid" and origin.cert_identity_status == "valid" then
88                         origin.sends2s(st.stanza("db:result", { to = from, from = to, id = attr.id, type = "valid" }));
89                         module:fire_event("s2s-authenticated", { session = origin, host = from });
90                         return true;
91                 end
92
93                 if not hosts[to] then
94                         -- Not a host that we serve
95                         origin.log("warn", "%s tried to connect to %s, which we don't serve", from, to);
96                         origin:close("host-unknown");
97                         return true;
98                 elseif not from then
99                         origin:close("improper-addressing");
100                 end
101
102                 origin.hosts[from] = { dialback_key = stanza[1] };
103
104                 dialback_requests[from.."/"..origin.streamid] = origin;
105
106                 -- COMPAT: ejabberd, gmail and perhaps others do not always set 'to' and 'from'
107                 -- on streams. We fill in the session's to/from here instead.
108                 if not origin.from_host then
109                         origin.from_host = from;
110                 end
111                 if not origin.to_host then
112                         origin.to_host = to;
113                 end
114
115                 origin.log("debug", "asking %s if key %s belongs to them", from, stanza[1]);
116                 module:fire_event("route/remote", {
117                         from_host = to, to_host = from;
118                         stanza = st.stanza("db:verify", { from = to, to = from, id = origin.streamid }):text(stanza[1]);
119                 });
120                 return true;
121         end
122 end);
123
124 module:hook("stanza/jabber:server:dialback:verify", function(event)
125         local origin, stanza = event.origin, event.stanza;
126
127         if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
128                 local attr = stanza.attr;
129                 local dialback_verifying = dialback_requests[attr.from.."/"..(attr.id or "")];
130                 if dialback_verifying and attr.from == origin.to_host then
131                         local valid;
132                         if attr.type == "valid" then
133                                 module:fire_event("s2s-authenticated", { session = dialback_verifying, host = attr.from });
134                                 valid = "valid";
135                         else
136                                 -- Warn the original connection that is was not verified successfully
137                                 log("warn", "authoritative server for %s denied the key", attr.from or "(unknown)");
138                                 valid = "invalid";
139                         end
140                         if dialback_verifying.destroyed then
141                                 log("warn", "Incoming s2s session %s was closed in the meantime, so we can't notify it of the db result", tostring(dialback_verifying):match("%w+$"));
142                         else
143                                 dialback_verifying.sends2s(
144                                                 st.stanza("db:result", { from = attr.to, to = attr.from, id = attr.id, type = valid })
145                                                                 :text(dialback_verifying.hosts[attr.from].dialback_key));
146                         end
147                         dialback_requests[attr.from.."/"..(attr.id or "")] = nil;
148                 end
149                 return true;
150         end
151 end);
152
153 module:hook("stanza/jabber:server:dialback:result", function(event)
154         local origin, stanza = event.origin, event.stanza;
155
156         if origin.type == "s2sout_unauthed" or origin.type == "s2sout" then
157                 -- Remote server is telling us whether we passed dialback
158
159                 local attr = stanza.attr;
160                 if not hosts[attr.to] then
161                         origin:close("host-unknown");
162                         return true;
163                 elseif hosts[attr.to].s2sout[attr.from] ~= origin then
164                         -- This isn't right
165                         origin:close("invalid-id");
166                         return true;
167                 end
168                 if stanza.attr.type == "valid" then
169                         module:fire_event("s2s-authenticated", { session = origin, host = attr.from });
170                 else
171                         origin:close("not-authorized", "dialback authentication failed");
172                 end
173                 return true;
174         end
175 end);
176
177 module:hook_stanza("urn:ietf:params:xml:ns:xmpp-sasl", "failure", function (origin, stanza)
178         if origin.external_auth == "failed" then
179                 module:log("debug", "SASL EXTERNAL failed, falling back to dialback");
180                 initiate_dialback(origin);
181                 return true;
182         end
183 end, 100);
184
185 module:hook_stanza(xmlns_stream, "features", function (origin, stanza)
186         if not origin.external_auth or origin.external_auth == "failed" then
187                 module:log("debug", "Initiating dialback...");
188                 initiate_dialback(origin);
189                 return true;
190         end
191 end, 100);
192
193 module:hook("s2sout-authenticate-legacy", function (event)
194         module:log("debug", "Initiating dialback...");
195         initiate_dialback(event.origin);
196         return true;
197 end, 100);
198
199 -- Offer dialback to incoming hosts
200 module:hook("s2s-stream-features", function (data)
201         data.features:tag("dialback", { xmlns='urn:xmpp:features:dialback' }):up();
202 end);