2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 lxp = require "lxp";
18 local logger = require "util.logger";
19 local config = require "core.configmanager";
20 local connlisteners = require "net.connlisteners";
21 local cm_register_component = require "core.componentmanager".register_component;
22 local cm_deregister_component = require "core.componentmanager".deregister_component;
23 local uuid_gen = require "util.uuid".generate;
24 local sha1 = require "util.hashes".sha1;
25 local st = require "util.stanza";
26 local init_xmlhandlers = require "core.xmlhandlers";
30 local log = module._log;
32 local component_listener = { default_port = 5347; default_mode = "*a"; default_interface = config.get("*", "core", "component_interface") or "127.0.0.1" };
34 local xmlns_component = 'jabber:component:accept';
36 --- Handle authentication attempts by components
37 function handle_component_auth(session, stanza)
38 log("info", "Handling component auth");
39 if (not session.host) or #stanza.tags > 0 then
40 (session.log or log)("warn", "Component handshake invalid");
41 session:close("not-authorized");
45 local secret = config.get(session.user, "core", "component_secret");
47 (session.log or log)("warn", "Component attempted to identify as %s, but component_password is not set", session.user);
48 session:close("not-authorized");
52 local supplied_token = t_concat(stanza);
53 local calculated_token = sha1(session.streamid..secret, true);
54 if supplied_token:lower() ~= calculated_token:lower() then
55 log("info", "Component for %s authentication failed", session.host);
56 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" };
62 log("info", "Component authenticated: %s", session.host);
64 -- If component not already created for this host, create one now
65 if not hosts[session.host].connected then
66 local send = session.send;
67 session.component_session = cm_register_component(session.host, function (_, data) return send(data); end);
68 hosts[session.host].connected = true;
69 log("info", "Component successfully registered");
71 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)");
74 -- Signal successful authentication
75 session.send(st.stanza("handshake"));
78 module:add_handler("component", "handshake", xmlns_component, handle_component_auth);