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