mod_ping: Convert from Windows line endings
[prosody.git] / plugins / mod_component.lua
1 -- Prosody IM v0.4
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 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);
11 end
12
13 local hosts = _G.hosts;
14
15 local t_concat = table.concat;
16
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";
27
28 local sessions = {};
29
30 local log = module._log;
31
32 local component_listener = { default_port = 5347; default_mode = "*a"; default_interface = config.get("*", "core", "component_interface") or "127.0.0.1" };
33
34 local xmlns_component = 'jabber:component:accept';
35
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");
42                 return;
43         end
44         
45         local secret = config.get(session.user, "core", "component_secret");
46         if not secret then
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");
49                 return;
50         end
51         
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" };
57                 return;
58         end
59         
60         
61         -- Authenticated now
62         log("info", "Component authenticated: %s", session.host);
63         
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) 
68                                 if data.attr and data.attr.xmlns == "jabber:client" then
69                                         data.attr.xmlns = nil;
70                                 end
71                                 return send(data);
72                         end);
73                 hosts[session.host].connected = true;
74                 log("info", "Component successfully registered");
75         else
76                 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)");
77         end
78         
79         -- Signal successful authentication
80         session.send(st.stanza("handshake"));
81 end
82
83 module:add_handler("component", "handshake", xmlns_component, handle_component_auth);