2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 if module:get_host_type() ~= "component" then
10 error("Don't load mod_component manually, it should be for a component, please see http://prosody.im/doc/components", 0);
13 local hosts = _G.hosts;
15 local t_concat = table.concat;
17 local config = require "core.configmanager";
18 local sha1 = require "util.hashes".sha1;
19 local st = require "util.stanza";
21 local log = module._log;
23 local main_session, send;
25 local function on_destroy(session, err)
26 if main_session == session then
29 session.on_destroy = nil;
33 local function handle_stanza(event)
34 local stanza = event.stanza;
36 stanza.attr.xmlns = nil;
39 log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag());
40 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
41 event.origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
47 module:hook("iq/bare", handle_stanza);
48 module:hook("message/bare", handle_stanza);
49 module:hook("presence/bare", handle_stanza);
50 module:hook("iq/full", handle_stanza);
51 module:hook("message/full", handle_stanza);
52 module:hook("presence/full", handle_stanza);
53 module:hook("iq/host", handle_stanza);
54 module:hook("message/host", handle_stanza);
55 module:hook("presence/host", handle_stanza);
57 --- Handle authentication attempts by components
58 function handle_component_auth(event)
59 local session, stanza = event.origin, event.stanza;
61 if session.type ~= "component" then return; end
62 if main_session == session then return; end
64 log("info", "Handling component auth");
65 if (not session.host) or #stanza.tags > 0 then
66 (session.log or log)("warn", "Component handshake invalid");
67 session:close("not-authorized");
71 local secret = config.get(session.host, "core", "component_secret");
73 (session.log or log)("warn", "Component attempted to identify as %s, but component_secret is not set", session.host);
74 session:close("not-authorized");
78 local supplied_token = t_concat(stanza);
79 local calculated_token = sha1(session.streamid..secret, true);
80 if supplied_token:lower() ~= calculated_token:lower() then
81 log("info", "Component for %s authentication failed", session.host);
82 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" };
87 log("info", "Component authenticated: %s", session.host);
89 session.component_validate_from = module:get_option_boolean("validate_from_addresses") ~= false;
91 -- If component not already created for this host, create one now
92 if not main_session then
94 main_session = session;
95 session.on_destroy = on_destroy;
96 log("info", "Component successfully registered");
97 session.send(st.stanza("handshake"));
99 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)");
100 session:close{ condition = "conflict", text = "Component already connected" };
106 module:hook("stanza/jabber:component:accept:handshake", handle_component_auth);