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