215b01a0d0f390d84167394e8b4e018ab4b1dde9
[prosody.git] / core / stanza_router.lua
1
2 -- The code in this file should be self-explanatory, though the logic is horrible
3 -- for more info on that, see doc/stanza_routing.txt, which attempts to condense
4 -- the rules from the RFCs (mainly 3921)
5
6 require "core.servermanager"
7
8 local log = require "util.logger".init("stanzarouter")
9
10 local st = require "util.stanza";
11 local send = require "core.sessionmanager".send_to_session;
12 local send_s2s = require "core.s2smanager".send_to_host;
13 local user_exists = require "core.usermanager".user_exists;
14
15 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
16 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
17 local format = string.format;
18 local tostring = tostring;
19
20 local jid_split = require "util.jid".split;
21 local print = print;
22
23 function core_process_stanza(origin, stanza)
24         log("debug", "Received: "..tostring(stanza))
25         -- TODO verify validity of stanza (as well as JID validity)
26         if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
27                 if stanza.attr.type == "set" or stanza.attr.type == "get" then
28                         error("Invalid IQ");
29                 elseif #stanza.tags > 1 or not(stanza.attr.type == "error" or stanza.attr.type == "result") then
30                         error("Invalid IQ");
31                 end
32         end
33
34         if origin.type == "c2s" and not origin.full_jid
35                 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
36                                 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
37                 error("Client MUST bind resource after auth");
38         end
39
40         local to = stanza.attr.to;
41         -- TODO also, stazas should be returned to their original state before the function ends
42         if origin.type == "c2s" then
43                 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s)
44         end
45         
46         if not to then
47                         core_handle_stanza(origin, stanza);
48         elseif hosts[to] and hosts[to].type == "local" then
49                 core_handle_stanza(origin, stanza);
50         elseif stanza.name == "iq" and not select(3, jid_split(to)) then
51                 core_handle_stanza(origin, stanza);
52         elseif origin.type == "c2s" then
53                 core_route_stanza(origin, stanza);
54         end
55 end
56
57 -- This function handles stanzas which are not routed any further,
58 -- that is, they are handled by this server
59 function core_handle_stanza(origin, stanza)
60         -- Handlers
61         if origin.type == "c2s" or origin.type == "c2s_unauthed" then
62                 local session = origin;
63                 
64                 if stanza.name == "presence" and origin.roster then
65                         if stanza.attr.type == nil or stanza.attr.type == "available" or stanza.attr.type == "unavailable" then
66                                 for jid in pairs(origin.roster) do -- broadcast to all interested contacts
67                                         local subscription = origin.roster[jid].subscription;
68                                         if subscription == "both" or subscription == "from" then
69                                                 stanza.attr.to = jid;
70                                                 core_route_stanza(origin, stanza);
71                                         end
72                                 end
73                                 --[[local node, host = jid_split(stanza.attr.from);
74                                 for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources
75                                         if res.full_jid then
76                                                 res = user.sessions[k];
77                                                 break;
78                                         end
79                                 end]]
80                                 if not origin.presence then -- presence probes on initial presence
81                                         local probe = st.presence({from = origin.full_jid, type = "probe"});
82                                         for jid in pairs(origin.roster) do
83                                                 local subscription = origin.roster[jid].subscription;
84                                                 if subscription == "both" or subscription == "to" then
85                                                         probe.attr.to = jid;
86                                                         core_route_stanza(origin, probe);
87                                                 end
88                                         end
89                                 end
90                                 origin.presence = stanza;
91                                 stanza.attr.to = nil; -- reset it
92                         else
93                                 -- TODO error, bad type
94                         end
95                 else
96                         log("debug", "Routing stanza to local");
97                         handle_stanza(session, stanza);
98                 end
99         elseif origin.type == "s2sin_unauthed" then
100                 if stanza.name == "verify" and stanza.attr.xmlns == "jabber:server:dialback" then
101                         log("debug", "verifying dialback key...");
102                         local attr = stanza.attr;
103                         print(tostring(attr.to), tostring(attr.from))
104                         print(tostring(origin.to_host), tostring(origin.from_host))
105                         -- FIXME: Grr, ejabberd breaks this one too?? it is black and white in XEP-220 example 34
106                         --if attr.from ~= origin.to_host then error("invalid-from"); end
107                         local type = "invalid";
108                         if s2s_verify_dialback(attr.id, attr.from, attr.to, stanza[1]) then
109                                 type = "valid"
110                         end
111                         origin.send(format("<db:verify from='%s' to='%s' id='%s' type='%s'>%s</db:verify>", attr.to, attr.from, attr.id, type, stanza[1]));
112                 end
113         elseif origin.type == "s2sout_unauthed" then
114                 if stanza.name == "result" and stanza.attr.xmlns == "jabber:server:dialback" then
115                         if stanza.attr.type == "valid" then
116                                 s2s_make_authenticated(origin);
117                         else
118                                 -- FIXME
119                                 error("dialback failed!");
120                         end
121                 end
122         else
123                 log("warn", "Unhandled origin: %s", origin.type);
124         end
125 end
126
127 -- TODO: Does this function belong here?
128 function is_authorized_to_see_presence(origin, username, host)
129         local roster = datamanager.load(username, host, "roster") or {};
130         local item = roster[origin.username.."@"..origin.host];
131         return item and (item.subscription == "both" or item.subscription == "from");
132 end
133
134 function core_route_stanza(origin, stanza)
135         -- Hooks
136         --- ...later
137         
138         -- Deliver
139         local to = stanza.attr.to;
140         local node, host, resource = jid_split(to);
141
142         if stanza.name == "presence" and stanza.attr.type == "probe" then resource = nil; end
143
144         local host_session = hosts[host]
145         if host_session and host_session.type == "local" then
146                 -- Local host
147                 local user = host_session.sessions[node];
148                 if user then
149                         local res = user.sessions[resource];
150                         if not res then
151                                 -- if we get here, resource was not specified or was unavailable
152                                 if stanza.name == "presence" then
153                                         if stanza.attr.type == "probe" then
154                                                 if is_authorized_to_see_presence(origin, node, host) then
155                                                         for k in pairs(user.sessions) do -- return presence for all resources
156                                                                 if user.sessions[k].presence then
157                                                                         local pres = user.sessions[k].presence;
158                                                                         pres.attr.to = origin.full_jid;
159                                                                         pres.attr.from = user.sessions[k].full_jid;
160                                                                         send(origin, pres);
161                                                                         pres.attr.to = nil;
162                                                                         pres.attr.from = nil;
163                                                                 end
164                                                         end
165                                                 else
166                                                         send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
167                                                 end
168                                         else
169                                                 for k in pairs(user.sessions) do -- presence broadcast to all user resources
170                                                         if user.sessions[k].full_jid then
171                                                                 stanza.attr.to = user.sessions[k].full_jid;
172                                                                 send(user.sessions[k], stanza);
173                                                         end
174                                                 end
175                                         end
176                                 elseif stanza.name == "message" then -- select a resource to recieve message
177                                         for k in pairs(user.sessions) do
178                                                 if user.sessions[k].full_jid then
179                                                         res = user.sessions[k];
180                                                         break;
181                                                 end
182                                         end
183                                         -- TODO find resource with greatest priority
184                                         send(res, stanza);
185                                 else
186                                         -- TODO send IQ error
187                                 end
188                         else
189                                 -- User + resource is online...
190                                 stanza.attr.to = res.full_jid;
191                                 send(res, stanza); -- Yay \o/
192                         end
193                 else
194                         -- user not online
195                         if user_exists(node, host) then
196                                 if stanza.name == "presence" then
197                                         if stanza.attr.type == "probe" and is_authorized_to_see_presence(origin, node, host) then -- FIXME what to do for not c2s?
198                                                 -- TODO send last recieved unavailable presence
199                                         else
200                                                 -- TODO send unavailable presence
201                                         end
202                                 elseif stanza.name == "message" then
203                                         -- TODO send message error, or store offline messages
204                                 elseif stanza.name == "iq" then
205                                         -- TODO send IQ error
206                                 end
207                         else -- user does not exist
208                                 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
209                                 if stanza.name == "presence" then
210                                         if stanza.attr.type == "probe" then
211                                                 send(origin, st.presence({from = user.."@"..host, to = origin.username.."@"..origin.host, type = "unsubscribed"}));
212                                         end
213                                         -- else ignore
214                                 else
215                                         send(origin, st.error_reply(stanza, "cancel", "service-unavailable"));
216                                 end
217                         end
218                 end
219         else
220                 -- Remote host
221                 log("debug", "sending s2s stanza: %s", tostring(stanza));
222                 stanza.attr.xmlns = "jabber:server";
223                 send_s2s(origin.host, host, stanza);
224         end
225         stanza.attr.to = to; -- reset
226 end
227
228 function handle_stanza_toremote(stanza)
229         log("error", "Stanza bound for remote host, but s2s is not implemented");
230 end