mod_tls: Let hosts without an 'ssl' option inherit it from their parent hosts.
[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 local main_session, send;
26
27 local function on_destroy(session, err)
28         if main_session == session then
29                 main_session = nil;
30                 send = nil;
31                 session.on_destroy = nil;
32                 hosts[session.host].connected = nil;
33         end
34 end
35
36 local function handle_stanza(event)
37         local stanza = event.stanza;
38         if send then
39                 stanza.attr.xmlns = nil;
40                 send(stanza);
41         else
42                 log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag());
43                 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
44                         event.origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
45                 end
46         end
47 end
48
49 module:hook("iq/bare", handle_stanza);
50 module:hook("message/bare", handle_stanza);
51 module:hook("presence/bare", handle_stanza);
52 module:hook("iq/full", handle_stanza);
53 module:hook("message/full", handle_stanza);
54 module:hook("presence/full", handle_stanza);
55 module:hook("iq/host", handle_stanza);
56 module:hook("message/host", handle_stanza);
57 module:hook("presence/host", handle_stanza);
58
59 cm_register_component(module.host, function() end);
60
61 --- Handle authentication attempts by components
62 function handle_component_auth(event)
63         local session, stanza = event.origin, event.stanza;
64         
65         if session.type ~= "component" then return; end
66         if main_session == session then return; end
67
68         log("info", "Handling component auth");
69         if (not session.host) or #stanza.tags > 0 then
70                 (session.log or log)("warn", "Component handshake invalid");
71                 session:close("not-authorized");
72                 return true;
73         end
74         
75         local secret = config.get(session.host, "core", "component_secret");
76         if not secret then
77                 (session.log or log)("warn", "Component attempted to identify as %s, but component_secret is not set", session.host);
78                 session:close("not-authorized");
79                 return true;
80         end
81         
82         local supplied_token = t_concat(stanza);
83         local calculated_token = sha1(session.streamid..secret, true);
84         if supplied_token:lower() ~= calculated_token:lower() then
85                 log("info", "Component for %s authentication failed", session.host);
86                 session:close{ condition = "not-authorized", text = "Given token does not match calculated token" };
87                 return true;
88         end
89         
90         
91         -- Authenticated now
92         log("info", "Component authenticated: %s", session.host);
93         
94         session.component_validate_from = module:get_option_boolean("validate_from_addresses") ~= false;
95         
96         -- If component not already created for this host, create one now
97         if not main_session then
98                 send = session.send;
99                 main_session = session;
100                 session.on_destroy = on_destroy;
101                 hosts[session.host].connected = true;
102                 log("info", "Component successfully registered");
103         else
104                 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)");
105                 session:close{ condition = "conflict", text = "Component already connected" };
106                 return true;
107         end
108         
109         -- Signal successful authentication
110         session.send(st.stanza("handshake"));
111         return true;
112 end
113
114 module:hook("stanza/jabber:component:accept:handshake", handle_component_auth);