Merge 0.7->0.8
[prosody.git] / core / stanza_router.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 log = require "util.logger".init("stanzarouter")
10
11 local hosts = _G.prosody.hosts;
12 local tostring = tostring;
13 local st = require "util.stanza";
14 local send_s2s = require "core.s2smanager".send_to_host;
15 local jid_split = require "util.jid".split;
16 local jid_prepped_split = require "util.jid".prepped_split;
17
18 local full_sessions = _G.prosody.full_sessions;
19 local bare_sessions = _G.prosody.bare_sessions;
20
21 local function handle_unhandled_stanza(host, origin, stanza)
22         local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns or "jabber:client", origin.type;
23         if name == "iq" and xmlns == "jabber:client" then
24                 if stanza.attr.type == "get" or stanza.attr.type == "set" then
25                         xmlns = stanza.tags[1].attr.xmlns or "jabber:client";
26                         log("debug", "Stanza of type %s from %s has xmlns: %s", name, origin_type, xmlns);
27                 else
28                         log("debug", "Discarding %s from %s of type: %s", name, origin_type, stanza.attr.type);
29                         return true;
30                 end
31         end
32         if stanza.attr.xmlns == nil then
33                 log("debug", "Unhandled %s stanza: %s; xmlns=%s", origin.type, stanza.name, xmlns); -- we didn't handle it
34                 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
35                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
36                 end
37         elseif not((name == "features" or name == "error") and xmlns == "http://etherx.jabber.org/streams") then -- FIXME remove check once we handle S2S features
38                 log("warn", "Unhandled %s stream element: %s; xmlns=%s: %s", origin.type, stanza.name, xmlns, tostring(stanza)); -- we didn't handle it
39                 origin:close("unsupported-stanza-type");
40         end
41 end
42
43 local iq_types = { set=true, get=true, result=true, error=true };
44 function core_process_stanza(origin, stanza)
45         (origin.log or log)("debug", "Received[%s]: %s", origin.type, stanza:top_tag())
46
47         -- TODO verify validity of stanza (as well as JID validity)
48         if stanza.attr.type == "error" and #stanza.tags == 0 then return; end -- TODO invalid stanza, log
49         if stanza.name == "iq" then
50                 if not stanza.attr.id then stanza.attr.id = ""; end -- COMPAT Jabiru doesn't send the id attribute on roster requests
51                 if not iq_types[stanza.attr.type] or ((stanza.attr.type == "set" or stanza.attr.type == "get") and (#stanza.tags ~= 1)) then
52                         origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid IQ type or incorrect number of children"));
53                         return;
54                 end
55         end
56
57         if origin.type == "c2s" and not stanza.attr.xmlns then
58                 if not origin.full_jid
59                         and not(stanza.name == "iq" and stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "bind"
60                                         and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then
61                         -- authenticated client isn't bound and current stanza is not a bind request
62                         if stanza.attr.type ~= "result" and stanza.attr.type ~= "error" then
63                                 origin.send(st.error_reply(stanza, "auth", "not-authorized")); -- FIXME maybe allow stanzas to account or server
64                         end
65                         return;
66                 end
67
68                 -- TODO also, stanzas should be returned to their original state before the function ends
69                 stanza.attr.from = origin.full_jid;
70         end
71         local to, xmlns = stanza.attr.to, stanza.attr.xmlns;
72         local from = stanza.attr.from;
73         local node, host, resource;
74         local from_node, from_host, from_resource;
75         local to_bare, from_bare;
76         if to then
77                 if full_sessions[to] or bare_sessions[to] or hosts[to] then
78                         node, host = jid_split(to); -- TODO only the host is needed, optimize
79                 else
80                         node, host, resource = jid_prepped_split(to);
81                         if not host then
82                                 log("warn", "Received stanza with invalid destination JID: %s", to);
83                                 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
84                                         origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The destination address is invalid: "..to));
85                                 end
86                                 return;
87                         end
88                         to_bare = node and (node.."@"..host) or host; -- bare JID
89                         if resource then to = to_bare.."/"..resource; else to = to_bare; end
90                         stanza.attr.to = to;
91                 end
92         end
93         if from and not origin.full_jid then
94                 -- We only stamp the 'from' on c2s stanzas, so we still need to check validity
95                 from_node, from_host, from_resource = jid_prepped_split(from);
96                 if not from_host then
97                         log("warn", "Received stanza with invalid source JID: %s", from);
98                         if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
99                                 origin.send(st.error_reply(stanza, "modify", "jid-malformed", "The source address is invalid: "..from));
100                         end
101                         return;
102                 end
103                 from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID
104                 if from_resource then from = from_bare.."/"..from_resource; else from = from_bare; end
105                 stanza.attr.from = from;
106         end
107
108         --[[if to and not(hosts[to]) and not(hosts[to_bare]) and (hosts[host] and hosts[host].type ~= "local") then -- not for us?
109                 log("warn", "stanza recieved for a non-local server");
110                 return; -- FIXME what should we do here?
111         end]] -- FIXME
112
113         if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and xmlns == nil then
114                 if origin.type == "s2sin" and not origin.dummy then
115                         local host_status = origin.hosts[from_host];
116                         if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
117                                 log("warn", "Received a stanza claiming to be from %s, over a stream authed for %s!", from_host, origin.from_host);
118                                 return; -- FIXME what should we do here? does this work with subdomains?
119                         end
120                 end
121                 core_post_stanza(origin, stanza, origin.full_jid);
122         else
123                 local h = hosts[stanza.attr.to or origin.host or origin.to_host];
124                 if h then
125                         local event;
126                         if xmlns == nil then
127                                 if stanza.name == "iq" and (stanza.attr.type == "set" or stanza.attr.type == "get") then
128                                         event = "stanza/iq/"..stanza.tags[1].attr.xmlns..":"..stanza.tags[1].name;
129                                 else
130                                         event = "stanza/"..stanza.name;
131                                 end
132                         else
133                                 event = "stanza/"..xmlns..":"..stanza.name;
134                         end
135                         if h.events.fire_event(event, {origin = origin, stanza = stanza}) then return; end
136                 end
137                 if host and not hosts[host] then host = nil; end -- COMPAT: workaround for a Pidgin bug which sets 'to' to the SRV result
138                 handle_unhandled_stanza(host or origin.host or origin.to_host, origin, stanza);
139         end
140 end
141
142 function core_post_stanza(origin, stanza, preevents)
143         local to = stanza.attr.to;
144         local node, host, resource = jid_split(to);
145         local to_bare = node and (node.."@"..host) or host; -- bare JID
146
147         local to_type, to_self;
148         if node then
149                 if resource then
150                         to_type = '/full';
151                 else
152                         to_type = '/bare';
153                         if node == origin.username and host == origin.host then
154                                 stanza.attr.to = nil;
155                                 to_self = true;
156                         end
157                 end
158         else
159                 if host then
160                         to_type = '/host';
161                 else
162                         to_type = '/bare';
163                         to_self = true;
164                 end
165         end
166
167         local event_data = {origin=origin, stanza=stanza};
168         if preevents then -- c2s connection
169                 if hosts[origin.host].events.fire_event('pre-'..stanza.name..to_type, event_data) then return; end -- do preprocessing
170         end
171         local h = hosts[to_bare] or hosts[host or origin.host];
172         if h then
173                 if h.events.fire_event(stanza.name..to_type, event_data) then return; end -- do processing
174                 if to_self and h.events.fire_event(stanza.name..'/self', event_data) then return; end -- do processing
175                 handle_unhandled_stanza(h.host, origin, stanza);
176         else
177                 core_route_stanza(origin, stanza);
178         end
179 end
180
181 function core_route_stanza(origin, stanza)
182         local node, host, resource = jid_split(stanza.attr.to);
183         local from_node, from_host, from_resource = jid_split(stanza.attr.from);
184
185         -- Auto-detect origin if not specified
186         origin = origin or hosts[from_host];
187         if not origin then return false; end
188         
189         if hosts[host] then
190                 -- old stanza routing code removed
191                 core_post_stanza(origin, stanza);
192         elseif origin.type == "c2s" then
193                 -- Remote host
194                 if not hosts[from_host] then
195                         log("error", "No hosts[from_host] (please report): %s", tostring(stanza));
196                 end
197                 if (not hosts[from_host]) or (not hosts[from_host].disallow_s2s) then
198                         local xmlns = stanza.attr.xmlns;
199                         --stanza.attr.xmlns = "jabber:server";
200                         stanza.attr.xmlns = nil;
201                         log("debug", "sending s2s stanza: %s", tostring(stanza.top_tag and stanza:top_tag()) or stanza);
202                         send_s2s(origin.host, host, stanza); -- TODO handle remote routing errors
203                         stanza.attr.xmlns = xmlns; -- reset
204                 else
205                         core_route_stanza(hosts[from_host], st.error_reply(stanza, "cancel", "not-allowed", "Communication with remote servers is not allowed"));
206                 end
207         elseif origin.type == "component" or origin.type == "local" then
208                 -- Route via s2s for components and modules
209                 log("debug", "Routing outgoing stanza for %s to %s", from_host, host);
210                 send_s2s(from_host, host, stanza);
211         else
212                 log("warn", "received %s stanza from unhandled connection type: %s", tostring(stanza.name), tostring(origin.type));
213         end
214 end