Merge 0.6->0.7
[prosody.git] / plugins / mod_component.lua
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
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 config = require "core.configmanager";
18 local cm_register_component = require "core.componentmanager".register_component;
19 local cm_deregister_component = require "core.componentmanager".deregister_component;
20 local sha1 = require "util.hashes".sha1;
21 local st = require "util.stanza";
22
23 local log = module._log;
24
25 --- Handle authentication attempts by components
26 function handle_component_auth(session, stanza)
27         log("info", "Handling component auth");
28         if (not session.host) or #stanza.tags > 0 then
29                 (session.log or log)("warn", "Component handshake invalid");
30                 session:close("not-authorized");
31                 return;
32         end
33         
34         local secret = config.get(session.user, "core", "component_secret");
35         if not secret then
36                 (session.log or log)("warn", "Component attempted to identify as %s, but component_secret is not set", session.user);
37                 session:close("not-authorized");
38                 return;
39         end
40         
41         local supplied_token = t_concat(stanza);
42         local calculated_token = sha1(session.streamid..secret, true);
43         if supplied_token:lower() ~= calculated_token:lower() then
44                 log("info", "Component for %s authentication failed", session.host);
45                 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" };
46                 return;
47         end
48         
49         
50         -- Authenticated now
51         log("info", "Component authenticated: %s", session.host);
52         
53         -- If component not already created for this host, create one now
54         if not hosts[session.host].connected then
55                 local send = session.send;
56                 session.component_session = cm_register_component(session.host, function (_, data) 
57                                 if data.attr and data.attr.xmlns == "jabber:client" then
58                                         data.attr.xmlns = nil;
59                                 end
60                                 return send(data);
61                         end);
62                 hosts[session.host].connected = true;
63                 log("info", "Component successfully registered");
64         else
65                 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)");
66         end
67         
68         -- Signal successful authentication
69         session.send(st.stanza("handshake"));
70 end
71
72 module:add_handler("component", "handshake", "jabber:component:accept", handle_component_auth);