/core/stanza_router.lua

http://github.com/bjc/prosody · Lua · 222 lines · 193 code · 16 blank · 13 comment · 133 complexity · 5ac0600061777f1a1abb641e8acc0129 MD5 · raw file

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