mod_storage_sql: Remove the subkey column from the Prosody table, and make the map...
[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 sha1 = require "util.hashes".sha1;
18 local st = require "util.stanza";
19
20 local log = module._log;
21
22 local main_session, send;
23
24 local function on_destroy(session, err)
25         if main_session == session then
26                 main_session = nil;
27                 send = nil;
28                 session.on_destroy = nil;
29         end
30 end
31
32 local function handle_stanza(event)
33         local stanza = event.stanza;
34         if send then
35                 stanza.attr.xmlns = nil;
36                 send(stanza);
37         else
38                 log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag());
39                 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
40                         event.origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
41                 end
42         end
43         return true;
44 end
45
46 module:hook("iq/bare", handle_stanza, -1);
47 module:hook("message/bare", handle_stanza, -1);
48 module:hook("presence/bare", handle_stanza, -1);
49 module:hook("iq/full", handle_stanza, -1);
50 module:hook("message/full", handle_stanza, -1);
51 module:hook("presence/full", handle_stanza, -1);
52 module:hook("iq/host", handle_stanza, -1);
53 module:hook("message/host", handle_stanza, -1);
54 module:hook("presence/host", handle_stanza, -1);
55
56 --- Handle authentication attempts by components
57 function handle_component_auth(event)
58         local session, stanza = event.origin, event.stanza;
59         
60         if session.type ~= "component" then return; end
61         if main_session == session then return; end
62
63         if (not session.host) or #stanza.tags > 0 then
64                 (session.log or log)("warn", "Invalid component handshake for host: %s", session.host);
65                 session:close("not-authorized");
66                 return true;
67         end
68         
69         local secret = module:get_option("component_secret");
70         if not secret then
71                 (session.log or log)("warn", "Component attempted to identify as %s, but component_secret is not set", session.host);
72                 session:close("not-authorized");
73                 return true;
74         end
75         
76         local supplied_token = t_concat(stanza);
77         local calculated_token = sha1(session.streamid..secret, true);
78         if supplied_token:lower() ~= calculated_token:lower() then
79                 log("info", "Component authentication failed for %s", session.host);
80                 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" };
81                 return true;
82         end
83         
84         -- If component not already created for this host, create one now
85         if not main_session then
86                 send = session.send;
87                 main_session = session;
88                 session.on_destroy = on_destroy;
89                 session.component_validate_from = module:get_option_boolean("validate_from_addresses") ~= false;
90                 log("info", "Component successfully authenticated: %s", session.host);
91                 session.send(st.stanza("handshake"));
92         else -- TODO: Implement stanza distribution
93                 log("error", "Multiple components bound to the same address, first one wins: %s", session.host);
94                 session:close{ condition = "conflict", text = "Component already connected" };
95         end
96         
97         return true;
98 end
99
100 module:hook("stanza/jabber:component:accept:handshake", handle_component_auth);