componentmanager: Support the 'disallow_s2s' option for components too (thanks darkho...
[prosody.git] / core / componentmanager.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 local prosody = _G.prosody;
10 local log = require "util.logger".init("componentmanager");
11 local certmanager = require "core.certmanager";
12 local configmanager = require "core.configmanager";
13 local modulemanager = require "core.modulemanager";
14 local jid_split = require "util.jid".split;
15 local fire_event = prosody.events.fire_event;
16 local events_new = require "util.events".new;
17 local st = require "util.stanza";
18 local prosody, hosts = prosody, prosody.hosts;
19 local ssl = ssl;
20 local uuid_gen = require "util.uuid".generate;
21
22 local pairs, setmetatable, type, tostring = pairs, setmetatable, type, tostring;
23
24 local components = {};
25
26 local disco_items = require "util.multitable".new();
27 local NULL = {};
28
29 module "componentmanager"
30
31 local function default_component_handler(origin, stanza)
32         log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag());
33         if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
34                 origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
35         end
36 end
37
38 function load_enabled_components(config)
39         local defined_hosts = config or configmanager.getconfig();
40                 
41         for host, host_config in pairs(defined_hosts) do
42                 if host ~= "*" and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
43                         hosts[host] = create_component(host);
44                         hosts[host].connected = false;
45                         disallow_s2s = configmanager.get(host, "core", "disallow_s2s");
46                         components[host] = default_component_handler;
47                         local ok, err = modulemanager.load(host, host_config.core.component_module);
48                         if not ok then
49                                 log("error", "Error loading %s component %s: %s", tostring(host_config.core.component_module), tostring(host), tostring(err));
50                         else
51                                 fire_event("component-activated", host, host_config);
52                                 log("debug", "Activated %s component: %s", host_config.core.component_module, host);
53                         end
54                 end
55         end
56 end
57
58 if prosody and prosody.events then
59         prosody.events.add_handler("server-starting", load_enabled_components);
60 end
61
62 function handle_stanza(origin, stanza)
63         local node, host = jid_split(stanza.attr.to);
64         local component = nil;
65         if host then
66                 if node then component = components[node.."@"..host]; end -- hack to allow hooking node@server
67                 if not component then component = components[host]; end
68         end
69         if component then
70                 log("debug", "%s stanza being handled by component: %s", stanza.name, host);
71                 component(origin, stanza, hosts[host]);
72         else
73                 log("error", "Component manager recieved a stanza for a non-existing component: "..tostring(stanza));
74                 default_component_handler(origin, stanza);
75         end
76 end
77
78 function create_component(host, component, events)
79         -- TODO check for host well-formedness
80         local ssl_ctx, ssl_ctx_in;
81         if host and ssl then
82                 -- We need to find SSL context to use...
83                 -- Discussion in prosody@ concluded that
84                 -- 1 level back is usually enough by default
85                 local base_host = host:gsub("^[^%.]+%.", "");
86                 if hosts[base_host] then
87                         ssl_ctx = hosts[base_host].ssl_ctx;
88                         ssl_ctx_in = hosts[base_host].ssl_ctx_in;
89                 else
90                         -- We have no cert, and no parent host to borrow a cert from
91                         -- Use global/default cert if there is one
92                         ssl_ctx = certmanager.create_context(host, "client");
93                         ssl_ctx_in = certmanager.create_context(host, "server");
94                 end
95         end
96         return { type = "component", host = host, connected = true, s2sout = {}, 
97                         ssl_ctx = ssl_ctx, ssl_ctx_in = ssl_ctx_in, events = events or events_new(),
98                         dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen() };
99 end
100
101 function register_component(host, component, session)
102         if not hosts[host] or (hosts[host].type == 'component' and not hosts[host].connected) then
103                 local old_events = hosts[host] and hosts[host].events;
104
105                 components[host] = component;
106                 hosts[host] = session or create_component(host, component, old_events);
107
108                 -- Add events object if not already one
109                 if not hosts[host].events then
110                         hosts[host].events = old_events or events_new();
111                 end
112
113                 if not hosts[host].dialback_secret then
114                         hosts[host].dialback_secret = configmanager.get(host, "core", "dialback_secret") or uuid_gen();
115                 end
116
117                 -- add to disco_items
118                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
119                         disco_items:set(host:sub(host:find(".", 1, true)+1), host, true);
120                 end
121                 modulemanager.load(host, "dialback");
122                 modulemanager.load(host, "tls");
123                 log("debug", "component added: "..host);
124                 return session or hosts[host];
125         else
126                 log("error", "Attempt to set component for existing host: "..host);
127         end
128 end
129
130 function deregister_component(host)
131         if components[host] then
132                 modulemanager.unload(host, "tls");
133                 modulemanager.unload(host, "dialback");
134                 hosts[host].connected = nil;
135                 local host_config = configmanager.getconfig()[host];
136                 if host_config and ((host_config.core.enabled == nil or host_config.core.enabled) and type(host_config.core.component_module) == "string") then
137                         -- Set default handler
138                         components[host] = default_component_handler;
139                 else
140                         -- Component not in config, or disabled, remove
141                         hosts[host] = nil; -- FIXME do proper unload of all modules and other cleanup before removing
142                         components[host] = nil;
143                 end
144                 -- remove from disco_items
145                 if not(host:find("@", 1, true) or host:find("/", 1, true)) and host:find(".", 1, true) then
146                         disco_items:remove(host:sub(host:find(".", 1, true)+1), host);
147                 end
148                 log("debug", "component removed: "..host);
149                 return true;
150         else
151                 log("error", "Attempt to remove component for non-existing host: "..host);
152         end
153 end
154
155 function set_component_handler(host, handler)
156         components[host] = handler;
157 end
158
159 function get_children(host)
160         return disco_items:get(host) or NULL;
161 end
162
163 return _M;