mod_httpserver: Rename to mod_http_files
[prosody.git] / plugins / mod_admin_telnet.lua
index da40f57e0a675b871108e19dc7007da15c8003e8..cb6302c795ce25327ab0f22288d0b8e965943041 100644 (file)
@@ -6,27 +6,25 @@
 -- COPYING file in the source package for more information.
 --
 
-module.host = "*";
+module:set_global();
 
 local _G = _G;
 
 local prosody = _G.prosody;
 local hosts = prosody.hosts;
-local connlisteners_register = require "net.connlisteners".register;
 
-local console_listener = { default_port = 5582; default_mode = "*l"; default_interface = "127.0.0.1" };
+local console_listener = { default_port = 5582; default_mode = "*l"; interface = "127.0.0.1" };
 
-require "util.iterators";
+local iterators = require "util.iterators";
+local keys, values = iterators.keys, iterators.values;
 local jid_bare = require "util.jid".bare;
 local set, array = require "util.set", require "util.array";
 local cert_verify_identity = require "util.x509".verify_identity;
 
-local commands = {};
-local def_env = {};
+local commands = module:shared("commands")
+local def_env = module:shared("env");
 local default_env_mt = { __index = def_env };
 
-prosody.console = { commands = commands, env = def_env };
-
 local function redirect_output(_G, session)
        local env = setmetatable({ print = session.print }, { __index = function (t, k) return rawget(_G, k); end });
        env.dofile = function(name)
@@ -149,8 +147,6 @@ function console_listener.ondisconnect(conn, err)
        end
 end
 
-connlisteners_register('console', console_listener);
-
 -- Console commands --
 -- These are simple commands, not valid standalone in Lua
 
@@ -281,8 +277,12 @@ local function get_hosts_set(hosts, module)
                return set.new { hosts };
        elseif hosts == nil then
                local mm = require "modulemanager";
-               return set.new(array.collect(keys(prosody.hosts)))
+               local hosts_set = set.new(array.collect(keys(prosody.hosts)))
                        / function (host) return prosody.hosts[host].type == "local" or module and mm.is_loaded(host, module); end;
+               if module and mm.get_module("*", module) then
+                       hosts_set:add("*");
+               end
+               return hosts_set;
        end
 end
 
@@ -292,16 +292,22 @@ function def_env.module:load(name, hosts, config)
        hosts = get_hosts_set(hosts);
        
        -- Load the module for each host
-       local ok, err, count = true, nil, 0;
+       local ok, err, count, mod = true, nil, 0, nil;
        for host in hosts do
                if (not mm.is_loaded(host, name)) then
-                       ok, err = mm.load(host, name, config);
-                       if not ok then
+                       mod, err = mm.load(host, name, config);
+                       if not mod then
                                ok = false;
+                               if err == "global-module-already-loaded" then
+                                       if count > 0 then
+                                               ok, err, count = true, nil, 1;
+                                       end
+                                       break;
+                               end
                                self.session.print(err or "Unknown error loading module");
                        else
                                count = count + 1;
-                               self.session.print("Loaded for "..host);
+                               self.session.print("Loaded for "..mod.module.host);
                        end
                end
        end
@@ -334,11 +340,15 @@ end
 function def_env.module:reload(name, hosts)
        local mm = require "modulemanager";
 
-       hosts = get_hosts_set(hosts, name);
-       
+       hosts = array.collect(get_hosts_set(hosts, name)):sort(function (a, b)
+               if a == "*" then return true
+               elseif b == "*" then return false
+               else return a < b; end
+       end);
+
        -- Reload the module for each host
        local ok, err, count = true, nil, 0;
-       for host in hosts do
+       for _, host in ipairs(hosts) do
                if mm.is_loaded(host, name) then
                        ok, err = mm.reload(host, name);
                        if not ok then
@@ -359,6 +369,7 @@ end
 function def_env.module:list(hosts)
        if hosts == nil then
                hosts = array.collect(keys(prosody.hosts));
+               table.insert(hosts, 1, "*");
        end
        if type(hosts) == "string" then
                hosts = { hosts };
@@ -369,8 +380,8 @@ function def_env.module:list(hosts)
        
        local print = self.session.print;
        for _, host in ipairs(hosts) do
-               print(host..":");
-               local modules = array.collect(keys(prosody.hosts[host] and prosody.hosts[host].modules or {})):sort();
+               print((host == "*" and "Global" or host)..":");
+               local modules = array.collect(keys(modulemanager.get_modules(host) or {})):sort();
                if #modules == 0 then
                        if prosody.hosts[host] then
                                print("    No modules loaded");
@@ -573,6 +584,20 @@ local function print_subject(print, subject)
        end
 end
 
+-- As much as it pains me to use the 0-based depths that OpenSSL does,
+-- I think there's going to be more confusion among operators if we
+-- break from that.
+local function print_errors(print, errors)
+       for depth, t in ipairs(errors) do
+               print(
+                       ("    %d: %s"):format(
+                               depth-1,
+                               table.concat(t, "\n|        ")
+                       )
+               );
+       end
+end
+
 function def_env.s2s:showcert(domain)
        local ser = require "util.serialization".serialize;
        local print = self.session.print;
@@ -588,16 +613,17 @@ function def_env.s2s:showcert(domain)
        for session in domain_sessions do
                local conn = session.conn;
                conn = conn and conn:socket();
-               if not conn.getpeercertificate then
+               if not conn.getpeerchain then
                        if conn.dohandshake then
                                error("This version of LuaSec does not support certificate viewing");
                        end
                else
-                       local cert = conn:getpeercertificate();
+                       local certs = conn:getpeerchain();
+                       local cert = certs[1];
                        if cert then
                                local digest = cert:digest("sha1");
                                if not cert_set[digest] then
-                                       local chain_valid, chain_err = conn:getpeerchainvalid();
+                                       local chain_valid, chain_errors = conn:getpeerverification();
                                        cert_set[digest] = {
                                                {
                                                  from = session.from_host,
@@ -605,8 +631,8 @@ function def_env.s2s:showcert(domain)
                                                  direction = session.direction
                                                };
                                                chain_valid = chain_valid;
-                                               chain_err = chain_err;
-                                               cert = cert;
+                                               chain_errors = chain_errors;
+                                               certs = certs;
                                        };
                                else
                                        table.insert(cert_set[digest], {
@@ -635,7 +661,8 @@ function def_env.s2s:showcert(domain)
        end
        
        for cert_info in values(domain_certs) do
-               local cert = cert_info.cert;
+               local certs = cert_info.certs;
+               local cert = certs[1];
                print("---")
                print("Fingerprint (SHA1): "..pretty_fingerprint(cert:digest("sha1")));
                print("");
@@ -649,9 +676,15 @@ function def_env.s2s:showcert(domain)
                        end
                end
                print("");
-               local chain_valid, err = cert_info.chain_valid, cert_info.chain_err;
+               local chain_valid, errors = cert_info.chain_valid, cert_info.chain_errors;
                local valid_identity = cert_verify_identity(domain, "xmpp-server", cert);
-               print("Trusted certificate: "..(chain_valid and "Yes" or ("No ("..err..")")));
+               if chain_valid then
+                       print("Trusted certificate: Yes");
+               else
+                       print("Trusted certificate: No");
+                       print_errors(print, errors);
+               end
+               print("");
                print("Issuer: ");
                print_subject(print, cert:issuer());
                print("");
@@ -756,4 +789,8 @@ if option and option ~= "short" and option ~= "full" and option ~= "graphic" the
 end
 end
 
-prosody.net_activate_ports("console", "console", {5582}, "tcp");
+require "core.portmanager".register_service("console", {
+       listener = console_listener;
+       default_port = 5582;
+       private = true;
+});