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