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