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