aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/moduleapi.lua20
-rw-r--r--core/modulemanager.lua23
-rw-r--r--core/portmanager.lua69
-rw-r--r--plugins/mod_auth_internal_hashed.lua2
-rw-r--r--plugins/mod_auth_internal_plain.lua2
-rw-r--r--plugins/mod_c2s.lua9
-rw-r--r--plugins/mod_net_multiplex.lua70
-rw-r--r--plugins/s2s/mod_s2s.lua6
-rw-r--r--plugins/s2s/s2sout.lib.lua5
9 files changed, 175 insertions, 31 deletions
diff --git a/core/moduleapi.lua b/core/moduleapi.lua
index 7a4d1fa6..a577c07a 100644
--- a/core/moduleapi.lua
+++ b/core/moduleapi.lua
@@ -22,6 +22,7 @@ local tonumber, tostring = tonumber, tostring;
local prosody = prosody;
local hosts = prosody.hosts;
+local core_post_stanza = prosody.core_post_stanza;
-- Registry of shared module data
local shared_data = setmetatable({}, { __mode = "v" });
@@ -50,6 +51,7 @@ function api:set_global()
local _log = logger.init("mod_"..self.name);
self.log = function (self, ...) return _log(...); end;
self._log = _log;
+ self.global = true;
end
function api:add_feature(xmlns)
@@ -291,4 +293,22 @@ function api:handle_items(type, added_cb, removed_cb, existing)
end
end
+function api:provides(name, item)
+ if not item then item = self.environment; end
+ if not item.name then
+ local item_name = module.name;
+ -- Strip a provider prefix to find the item name
+ -- (e.g. "auth_foo" -> "foo" for an auth provider)
+ if item_name:find(name.."_", 1, true) == 1 then
+ item_name = item_name:sub(#name+2);
+ end
+ item.name = item_name;
+ end
+ self:add_item(name, item);
+end
+
+function api:send(stanza)
+ return core_post_stanza(hosts[self.host], stanza);
+end
+
return api;
diff --git a/core/modulemanager.lua b/core/modulemanager.lua
index 0ca37105..f9f3a8b8 100644
--- a/core/modulemanager.lua
+++ b/core/modulemanager.lua
@@ -101,11 +101,12 @@ local function do_unload_module(host, name)
end
if mod.module.items then -- remove items
+ local events = (host == "*" and prosody.events) or hosts[host].events;
for key,t in pairs(mod.module.items) do
for i = #t,1,-1 do
local value = t[i];
t[i] = nil;
- hosts[host].events.fire_event("item-removed/"..key, {source = mod.module, item = value});
+ events.fire_event("item-removed/"..key, {source = mod.module, item = value});
end
end
end
@@ -122,6 +123,7 @@ local function do_load_module(host, module_name)
if not modulemap[host] then
modulemap[host] = {};
+ hosts[host].modules = modulemap[host];
end
if modulemap[host][module_name] then
@@ -147,8 +149,6 @@ local function do_load_module(host, module_name)
api_instance.environment = pluginenv;
setfenv(mod, pluginenv);
- hosts[host].modules = modulemap[host];
- modulemap[host][module_name] = pluginenv;
local ok, err = pcall(mod);
if ok then
@@ -160,15 +160,18 @@ local function do_load_module(host, module_name)
end
end
- -- Use modified host, if the module set one
- if api_instance.host == "*" and host ~= "*" then
- modulemap[host][module_name] = nil;
- modulemap["*"][module_name] = pluginenv;
- api_instance:set_global();
+ modulemap[pluginenv.module.host][module_name] = pluginenv;
+ if pluginenv.module.host == "*" then
+ if not pluginenv.module.global then -- COMPAT w/pre-0.9
+ log("warn", "mod_%s: Setting module.host = '*' deprecated, call module:set_global() instead", module_name);
+ api_instance:set_global();
+ end
+ else
+ hosts[host].modules[module_name] = pluginenv;
end
- else
+ end
+ if not ok then
log("error", "Error initializing module '%s' on '%s': %s", module_name, host, err or "nil");
- do_unload_module(api_instance.host, module_name); -- Ignore error, module may be partially-loaded
end
return ok and pluginenv, err;
end
diff --git a/core/portmanager.lua b/core/portmanager.lua
index 103c5d44..914a8e2f 100644
--- a/core/portmanager.lua
+++ b/core/portmanager.lua
@@ -13,8 +13,8 @@ end
--- Private state
--- service_name -> service_info
-local services = {};
+-- service_name -> { service_info, ... }
+local services = setmetatable({}, { __index = function (t, k) rawset(t, k, {}); return rawget(t, k); end });
-- service_name, interface (string), port (number)
local active_services = multitable.new();
@@ -52,28 +52,42 @@ end
module("portmanager", package.seeall);
+prosody.events.add_handler("item-added/net-provider", function (event)
+ local item = event.item;
+ register_service(item.name, item);
+end);
+prosody.events.add_handler("item-removed/net-provider", function (event)
+ local item = event.item;
+ unregister_service(item.name, item);
+end);
+
--- Public API
function activate_service(service_name)
- local service_info = services[service_name];
+ local service_info = services[service_name][1];
if not service_info then
return nil, "Unknown service: "..service_name;
end
+
+ local listener = service_info.listener;
+
+ local config_prefix = (service_info.config_prefix or service_name).."_";
+ if config_prefix == "_" then
+ config_prefix = "";
+ end
- local bind_interfaces = set.new(config.get("*", service_name.."_interfaces")
- or config.get("*", service_name.."_interface") -- COMPAT w/pre-0.9
+ local bind_interfaces = set.new(config.get("*", config_prefix.."interfaces")
+ or config.get("*", config_prefix.."interface") -- COMPAT w/pre-0.9
or (service_info.private and default_local_interfaces)
or config.get("*", "interfaces")
or config.get("*", "interface") -- COMPAT w/pre-0.9
- or service_info.default_interface -- COMPAT w/pre0.9
+ or listener.default_interface -- COMPAT w/pre0.9
or default_interfaces);
- local bind_ports = set.new(config.get("*", service_name.."_ports")
- or (service_info.multiplex and config.get("*", "ports"))
+ local bind_ports = set.new(config.get("*", config_prefix.."ports")
or service_info.default_ports
- or {service_info.default_port});
+ or {listener.default_port}); -- COMPAT w/pre-0.9
- local listener = service_info.listener;
local mode = listener.default_mode or "*a";
local ssl;
if service_info.encryption == "ssl" then
@@ -85,8 +99,8 @@ function activate_service(service_name)
for interface in bind_interfaces do
for port in bind_ports do
- if not service_info.multiplex and #active_services:search(nil, interface, port) > 0 then
- log("error", "Multiple services configured to listen on the same port: %s, %s", table.concat(active_services:search(nil, interface, port), ", "), service_name);
+ if #active_services:search(nil, interface, port) > 0 then
+ log("error", "Multiple services configured to listen on the same port ([%s]:%d): %s, %s", interface, port, active_services:search(nil, interface, port)[1][1].service.name or "<unnamed>", service_name or "<unnamed>");
else
local handler, err = server.addserver(interface, port, listener, mode, ssl);
if not handler then
@@ -102,6 +116,7 @@ function activate_service(service_name)
end
end
log("info", "Activated service '%s'", service_name);
+ return true;
end
function deactivate(service_name)
@@ -118,16 +133,36 @@ function deactivate(service_name)
end
function register_service(service_name, service_info)
- services[service_name] = service_info;
+ table.insert(services[service_name], service_info);
- if not active_services[service_name] then
- activate_service(service_name);
+ if not active_services:get(service_name) then
+ log("debug", "No active service for %s, activating...", service_name);
+ local ok, err = activate_service(service_name);
+ if not ok then
+ log("error", "Failed to activate service '%s': %s", service_name, err or "unknown error");
+ end
end
fire_event("service-added", { name = service_name, service = service_info });
return true;
end
+function unregister_service(service_name, service_info)
+ local service_info_list = services[service_name];
+ for i, service in ipairs(service_info_list) do
+ if service == service_info then
+ table.remove(service_info_list, i);
+ end
+ end
+ if active_services[service_name] == service_info then
+ deactivate(service_name);
+ if #service_info_list > 0 then -- Other services registered with this name
+ activate(service_name); -- Re-activate with the next available one
+ end
+ end
+ fire_event("service-removed", { name = service_name, service = service_info });
+end
+
function get_service(service_name)
return services[service_name];
end
@@ -136,4 +171,8 @@ function get_active_services(...)
return active_services;
end
+function get_registered_services()
+ return services;
+end
+
return _M;
diff --git a/plugins/mod_auth_internal_hashed.lua b/plugins/mod_auth_internal_hashed.lua
index ee810426..399044ad 100644
--- a/plugins/mod_auth_internal_hashed.lua
+++ b/plugins/mod_auth_internal_hashed.lua
@@ -54,7 +54,7 @@ local iteration_count = 4096;
function new_hashpass_provider(host)
local provider = { name = "internal_hashed" };
- log("debug", "initializing hashpass authentication provider for host '%s'", host);
+ log("debug", "initializing internal_hashed authentication provider for host '%s'", host);
function provider.test_password(username, password)
local credentials = datamanager.load(username, host, "accounts") or {};
diff --git a/plugins/mod_auth_internal_plain.lua b/plugins/mod_auth_internal_plain.lua
index 784553ea..93b50351 100644
--- a/plugins/mod_auth_internal_plain.lua
+++ b/plugins/mod_auth_internal_plain.lua
@@ -23,7 +23,7 @@ local prosody = _G.prosody;
function new_default_provider(host)
local provider = { name = "internal_plain" };
- log("debug", "initializing default authentication provider for host '%s'", host);
+ log("debug", "initializing internal_plain authentication provider for host '%s'", host);
function provider.test_password(username, password)
log("debug", "test password '%s' for user %s at host %s", password, username, module.host);
diff --git a/plugins/mod_c2s.lua b/plugins/mod_c2s.lua
index 682c4e3b..67156f55 100644
--- a/plugins/mod_c2s.lua
+++ b/plugins/mod_c2s.lua
@@ -220,15 +220,20 @@ function listener.associate_session(conn, session)
sessions[conn] = session;
end
-portmanager.register_service("c2s", {
+module:add_item("net-provider", {
+ name = "c2s";
listener = listener;
default_port = 5222;
encryption = "starttls";
});
-portmanager.register_service("legacy_ssl", {
+module:add_item("net-provider", {
+ name = "legacy_ssl";
listener = listener;
encryption = "ssl";
+ multiplex = {
+ pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:client%1.*>";
+ };
});
diff --git a/plugins/mod_net_multiplex.lua b/plugins/mod_net_multiplex.lua
new file mode 100644
index 00000000..44e1c1ee
--- /dev/null
+++ b/plugins/mod_net_multiplex.lua
@@ -0,0 +1,70 @@
+module:set_global();
+
+local max_buffer_len = module:get_option_number("multiplex_buffer_size", 1024);
+
+local portmanager = require "core.portmanager";
+
+local available_services = {};
+
+local function add_service(service)
+ local multiplex_pattern = service.multiplex and service.multiplex.pattern;
+ if multiplex_pattern then
+ module:log("debug", "Adding multiplex service %q with pattern %q", service.name, multiplex_pattern);
+ available_services[service] = multiplex_pattern;
+ else
+ module:log("debug", "Service %q is not multiplex-capable", service.name);
+ end
+end
+module:hook("service-added", function (event) add_service(event.service); end);
+module:hook("service-removed", function (event) available_services[event.service] = nil; end);
+
+for service_name, services in pairs(portmanager.get_registered_services()) do
+ for i, service in ipairs(services) do
+ add_service(service);
+ end
+end
+
+local buffers = {};
+
+local listener = { default_mode = "*a" };
+
+function listener.onconnect()
+end
+
+function listener.onincoming(conn, data)
+ if not data then return; end
+ local buf = buffers[conn];
+ buffers[conn] = nil;
+ buf = buf and buf..data or data;
+ for service, multiplex_pattern in pairs(available_services) do
+ if buf:match(multiplex_pattern) then
+ module:log("debug", "Routing incoming connection to %s", service.name);
+ local listener = service.listener;
+ conn:setlistener(listener);
+ local onconnect = listener.onconnect;
+ if onconnect then onconnect(conn) end
+ return listener.onincoming(conn, buf);
+ end
+ end
+ if #buf > max_buffer_len then -- Give up
+ conn:close();
+ else
+ buffers[conn] = buf;
+ end
+end
+
+function listener.ondisconnect(conn, err)
+ buffers[conn] = nil; -- warn if no buffer?
+end
+
+module:add_item("net-provider", {
+ name = "multiplex";
+ config_prefix = "";
+ listener = listener;
+});
+
+module:provides("net", {
+ name = "multiplex_ssl";
+ config_prefix = "ssl";
+ listener = listener;
+});
diff --git a/plugins/s2s/mod_s2s.lua b/plugins/s2s/mod_s2s.lua
index 1fa8ae81..ad7f4862 100644
--- a/plugins/s2s/mod_s2s.lua
+++ b/plugins/s2s/mod_s2s.lua
@@ -464,9 +464,13 @@ end
s2sout.set_listener(listener);
-require "core.portmanager".register_service("s2s", {
+module:add_item("net-provider", {
+ name = "s2s";
listener = listener;
default_port = 5269;
encryption = "starttls";
+ multiplex = {
+ pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:server%1.*>";
+ };
});
diff --git a/plugins/s2s/s2sout.lib.lua b/plugins/s2s/s2sout.lib.lua
index f9f9d362..6ee1ca83 100644
--- a/plugins/s2s/s2sout.lib.lua
+++ b/plugins/s2s/s2sout.lib.lua
@@ -316,7 +316,10 @@ module:hook_global("service-added", function (event)
if event.name ~= "s2s" then return end
local s2s_sources = portmanager.get_active_services():get("s2s");
-
+ if not s2s_sources then
+ module:log("warn", "s2s not listening on any ports, outgoing connections may fail");
+ return;
+ end
for source, _ in pairs(s2s_sources) do
if source == "*" or source == "0.0.0.0" then
if not socket.local_addresses then