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