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