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