Stringprep!
[prosody.git] / core / stanza_router.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21
22 local log = require "util.logger".init("stanzarouter")
23
24 local st = require "util.stanza";
25 local send_s2s = require "core.s2smanager".send_to_host;
26 local user_exists = require "core.usermanager".user_exists;
27
28 local rostermanager = require "core.rostermanager";
29 local sessionmanager = require "core.sessionmanager";
30 local offlinemanager = require "core.offlinemanager";
31
32 local s2s_verify_dialback = require "core.s2smanager".verify_dialback;
33 local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
34
35 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
36 local component_handle_stanza = require "core.componentmanager".handle_stanza;
37
38 local handle_outbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
39 local handle_inbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_inbound_presence_subscriptions_and_probes;
40 local handle_normal_presence = require "core.presencemanager".handle_normal_presence;
41
42 local format = string.format;
43 local tostring = tostring;
44 local t_concat = table.concat;
45 local t_insert = table.insert;
46 local tonumber = tonumber;
47 local s_find = string.find;
48
49 local jid_split = require "util.jid".split;
50 local jid_prepped_split = require "util.jid".prepped_split;
51 local print = print;
52 local function checked_error_reply(origin, stanza)
53         if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
54                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
55         end
56 end
57
58 function core_process_stanza(origin, stanza)
59         (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:pretty_print()) --top_tag())
60
61         if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling.
62         -- TODO verify validity of stanza (as well as JID validity)
63         if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then
64                 if stanza.attr.type == "set" or stanza.attr.type == "get" then
65                         error("Invalid IQ");
66                 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then
67                         error("Invalid IQ");
68                 end
69         end
70
71         if origin.type == "c2s" and not origin.full_jid
72                 and not(stanza.name == "iq" and stanza.tags[1].name == "bind"
73                                 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
74                 error("Client MUST bind resource after auth");
75         end
76
77         -- TODO also, stanzas should be returned to their original state before the function ends
78         if origin.type == "c2s" then
79                 stanza.attr.from = origin.full_jid;
80         end
81         local to, xmlns = stanza.attr.to, stanza.attr.xmlns;
82         local from = stanza.attr.from;
83         local node, host, resource;
84         local from_node, from_host, from_resource;
85         local to_bare, from_bare;
86         if to then
87                 node, host, resource = jid_prepped_split(to);
88                 if not host then
89                         error("Invalid to JID");
90                 end
91                 to_bare = node and (node.."@"..host) or host; -- bare JID
92                 if resource then to = to_bare.."/"..resource; else to = to_bare; end
93                 stanza.attr.to = to;
94         end
95         if from then
96                 from_node, from_host, from_resource = jid_prepped_split(from);
97                 if not from_host then
98                         error("Invalid from JID");
99                 end
100                 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
101                 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
102                 stanza.attr.from = from;
103         end
104
105         --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
106                 log("warn", "stanza recieved for a non-local server");
107                 return; -- FIXME what should we do here?
108         end]] -- FIXME
109
110         -- FIXME do stanzas not of jabber:client get handled by components?
111         if (origin.type == "s2sin" or origin.type == "c2s") and (not xmlns or xmlns == "jabber:server" or xmlns == "jabber:client") then                        
112                 if origin.type == "s2sin" and not origin.dummy then
113                         local host_status = origin.hosts[from_host];
114                         if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
115                                 log("warn", "Received a stanza claiming to be from %s, over a conn authed for %s!", from_host, origin.from_host);
116                                 return; -- FIXME what should we do here? does this work with subdomains?
117                         end
118                 end
119                 if not to then
120                         core_handle_stanza(origin, stanza);
121                 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server
122                         core_handle_stanza(origin, stanza);
123                 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then
124                         modules_handle_stanza(host or origin.host or origin.to_host, origin, stanza);
125                 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource
126                         component_handle_stanza(origin, stanza);
127                 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server
128                         component_handle_stanza(origin, stanza);
129                 elseif hosts[host] and hosts[host].type == "component" then -- directed at a component
130                         component_handle_stanza(origin, stanza);
131                 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
132                         handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
133                 elseif origin.type ~= "c2s" and stanza.name == "iq" and not resource then -- directed at bare JID
134                         core_handle_stanza(origin, stanza);
135                 else
136                         if origin.type == "c2s" and stanza.name == "presence" and to ~= nil and not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then
137                                 origin.directed = origin.directed or {};
138                                 t_insert(origin.directed, to); -- FIXME does it make more sense to add to_bare rather than to?
139                         end
140                         core_route_stanza(origin, stanza);
141                 end
142         else
143                 core_handle_stanza(origin, stanza);
144         end
145 end
146
147 -- This function handles stanzas which are not routed any further,
148 -- that is, they are handled by this server
149 function core_handle_stanza(origin, stanza)
150         -- Handlers
151         if modules_handle_stanza(select(2, jid_split(stanza.attr.to)) or origin.host, origin, stanza) then return; end
152         if origin.type == "c2s" or origin.type == "s2sin" then
153                 if origin.type == "c2s" then
154                         if stanza.name == "presence" and origin.roster then
155                                 if stanza.attr.type == nil or stanza.attr.type == "unavailable" then
156                                         handle_normal_presence(origin, stanza, core_route_stanza);
157                                 else
158                                         log("warn", "Unhandled c2s presence: %s", tostring(stanza));
159                                         checked_error_reply(origin, stanza);
160                                 end
161                         else
162                                 log("warn", "Unhandled c2s stanza: %s", tostring(stanza));
163                                 checked_error_reply(origin, stanza);
164                         end
165                 else -- s2s stanzas
166                         log("warn", "Unhandled s2s stanza: %s", tostring(stanza));
167                         checked_error_reply(origin, stanza);
168                 end
169         else
170                 log("warn", "Unhandled %s stanza: %s", origin.type, tostring(stanza));
171                 checked_error_reply(origin, stanza);
172         end
173 end
174
175 function core_route_stanza(origin, stanza)
176         -- Hooks
177         --- ...later
178
179         -- Deliver
180         local to = stanza.attr.to;
181         local node, host, resource = jid_split(to);
182         local to_bare = node and (node.."@"..host) or host; -- bare JID
183         local from = stanza.attr.from;
184         local from_node, from_host, from_resource = jid_split(from);
185         local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
186
187         -- Auto-detect origin if not specified
188         origin = origin or hosts[from_host];
189         if not origin then return false; end
190         
191         if stanza.name == "presence" and (stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable") then resource = nil; end
192
193         local host_session = hosts[host]
194         if host_session and host_session.type == "local" then
195                 -- Local host
196                 local user = host_session.sessions[node];
197                 if user then
198                         local res = user.sessions[resource];
199                         if not res then
200                                 -- if we get here, resource was not specified or was unavailable
201                                 if stanza.name == "presence" then
202                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
203                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
204                                         else -- sender is available or unavailable
205                                                 for _, session in pairs(user.sessions) do -- presence broadcast to all user resources.
206                                                         if session.full_jid then -- FIXME should this be just for available resources? Do we need to check subscription?
207                                                                 stanza.attr.to = session.full_jid; -- reset at the end of function
208                                                                 session.send(stanza);
209                                                         end
210                                                 end
211                                         end
212                                 elseif stanza.name == "message" then -- select a resource to recieve message
213                                         local priority = 0;
214                                         local recipients = {};
215                                         for _, session in pairs(user.sessions) do -- find resource with greatest priority
216                                                 local p = session.priority;
217                                                 if p > priority then
218                                                         priority = p;
219                                                         recipients = {session};
220                                                 elseif p == priority then
221                                                         t_insert(recipients, session);
222                                                 end
223                                         end
224                                         local count = 0;
225                                         for _, session in pairs(recipients) do
226                                                 session.send(stanza);
227                                                 count = count + 1;
228                                         end
229                                         if count == 0 then
230                                                 offlinemanager.store(node, host, stanza);
231                                                 -- TODO deal with storage errors
232                                         end
233                                 else
234                                         -- TODO send IQ error
235                                 end
236                         else
237                                 -- User + resource is online...
238                                 stanza.attr.to = res.full_jid; -- reset at the end of function
239                                 res.send(stanza); -- Yay \o/
240                         end
241                 else
242                         -- user not online
243                         if user_exists(node, host) then
244                                 if stanza.name == "presence" then
245                                         if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
246                                                 handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);
247                                         else
248                                                 -- TODO send unavailable presence or unsubscribed
249                                         end
250                                 elseif stanza.name == "message" then -- FIXME if full jid, then send out to resources with highest priority
251                                         if stanza.attr.type == "chat" or stanza.attr.type == "normal" or not stanza.attr.type then
252                                                 offlinemanager.store(node, host, stanza);
253                                                 -- FIXME don't store messages with only chat state notifications
254                                         end
255                                         -- TODO allow configuration of offline storage
256                                         -- TODO send error if not storing offline
257                                 elseif stanza.name == "iq" then
258                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
259                                 end
260                         else -- user does not exist
261                                 -- TODO we would get here for nodeless JIDs too. Do something fun maybe? Echo service? Let plugins use xmpp:server/resource addresses?
262                                 if stanza.name == "presence" then
263                                         local t = stanza.attr.type;
264                                         if t == "subscribe" or t == "probe" then
265                                                 origin.send(st.presence({from = to_bare, to = from_bare, type = "unsubscribed"}));
266                                         end
267                                         -- else ignore
268                                 else
269                                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
270                                 end
271                         end
272                 end
273         elseif origin.type == "c2s" then
274                 -- Remote host
275                 local xmlns = stanza.attr.xmlns;
276                 --stanza.attr.xmlns = "jabber:server";
277                 stanza.attr.xmlns = nil;
278                 log("debug", "sending s2s stanza: %s", tostring(stanza));
279                 send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
280                 stanza.attr.xmlns = xmlns; -- reset
281         elseif origin.type == "component" or origin.type == "local" then
282                 -- Route via s2s for components and modules
283                 log("debug", "Routing outgoing stanza for %s to %s", origin.host, host);
284                 send_s2s(origin.host, host, stanza);
285         else
286                 log("warn", "received stanza from unhandled connection type: %s", origin.type);
287         end
288         stanza.attr.to = to; -- reset
289 end