prosodyctl
author João Duarte <jvsDuarte08@gmail.com>
Wed, 17 Jul 2019 09:47:37 -0700
changeset 10150 903db0fcb716
parent 10149 0fcd251a27e5
child 10151 1a35b3a2b11b
permissions -rwxr-xr-x
prosodyctl: Removed trailing whitespaces
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env lua
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1501
diff changeset
     2
-- Prosody IM
3742
a18acd47904b prosodyctl: Added and updated some comments and some semicolons, to match main prosody executable.
Waqas Hussain <waqas20@gmail.com>
parents: 3741
diff changeset
     3
-- Copyright (C) 2008-2010 Matthew Wild
a18acd47904b prosodyctl: Added and updated some comments and some semicolons, to match main prosody executable.
Waqas Hussain <waqas20@gmail.com>
parents: 3741
diff changeset
     4
-- Copyright (C) 2008-2010 Waqas Hussain
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
     5
--
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
-- This project is MIT/X11 licensed. Please see the
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
-- COPYING file in the source package for more information.
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
--
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
-- prosodyctl - command-line controller for Prosody XMPP server
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
-- Will be modified by configure script if run --
7300
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7270
diff changeset
    13
CFG_SOURCEDIR=CFG_SOURCEDIR or os.getenv("PROSODY_SRCDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7270
diff changeset
    14
CFG_CONFIGDIR=CFG_CONFIGDIR or os.getenv("PROSODY_CFGDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7270
diff changeset
    15
CFG_PLUGINDIR=CFG_PLUGINDIR or os.getenv("PROSODY_PLUGINDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7270
diff changeset
    16
CFG_DATADIR=CFG_DATADIR or os.getenv("PROSODY_DATADIR");
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
3742
a18acd47904b prosodyctl: Added and updated some comments and some semicolons, to match main prosody executable.
Waqas Hussain <waqas20@gmail.com>
parents: 3741
diff changeset
    18
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
3999
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    20
local function is_relative(path)
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    21
	local path_sep = package.config:sub(1,1);
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    22
        return ((path_sep == "/" and path:sub(1,1) ~= "/")
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    23
	or (path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\")))
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    24
end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    25
3742
a18acd47904b prosodyctl: Added and updated some comments and some semicolons, to match main prosody executable.
Waqas Hussain <waqas20@gmail.com>
parents: 3741
diff changeset
    26
-- Tell Lua where to find our libraries
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
if CFG_SOURCEDIR then
3999
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    28
	local function filter_relative_paths(path)
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    29
		if is_relative(path) then return ""; end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    30
	end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    31
	local function sanitise_paths(paths)
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    32
		return (paths:gsub("[^;]+;?", filter_relative_paths):gsub(";;+", ";"));
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    33
	end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    34
	package.path = sanitise_paths(CFG_SOURCEDIR.."/?.lua;"..package.path);
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
    35
	package.cpath = sanitise_paths(CFG_SOURCEDIR.."/?.so;"..package.cpath);
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
3742
a18acd47904b prosodyctl: Added and updated some comments and some semicolons, to match main prosody executable.
Waqas Hussain <waqas20@gmail.com>
parents: 3741
diff changeset
    38
-- Substitute ~ with path to home directory in data path
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
if CFG_DATADIR then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	if os.getenv("HOME") then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
		CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
	end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8564
diff changeset
    45
-----------
1580
5be6dc582df3 prosodyctl: Also switch group when we switch user
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
    46
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
    47
local startup = require "util.startup";
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
    48
startup.prosodyctl();
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
    49
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8564
diff changeset
    50
-----------
3339
7893055e54d1 prosodyctl: Warn and exit if any log files are not writeable, fixes #94
Matthew Wild <mwild1@gmail.com>
parents: 3338
diff changeset
    51
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
    52
local error_messages = setmetatable({
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
		["invalid-username"] = "The given username is invalid in a Jabber ID";
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
		["invalid-hostname"] = "The given hostname is invalid";
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
		["no-password"] = "No password was supplied";
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
		["no-such-user"] = "The given user does not exist on the server";
4827
fefbfd76d2d3 prosodyctl: Show an error if the user doesn't supply a hostname to the certificate commands
Kim Alvefur <zash@zash.se>
parents: 4826
diff changeset
    57
		["no-such-host"] = "The given hostname does not exist in the config";
1124
055cfdc96afa prosodyctl: Add message for unable-to-save-data error
Matthew Wild <mwild1@gmail.com>
parents: 1122
diff changeset
    58
		["unable-to-save-data"] = "Unable to store, perhaps you don't have permission?";
7362
a5a080c12c96 Update every link to the documentation to use HTTPS
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7316
diff changeset
    59
		["no-pidfile"] = "There is no 'pidfile' option in the configuration file, see https://prosody.im/doc/prosodyctl#pidfile for help";
a5a080c12c96 Update every link to the documentation to use HTTPS
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7316
diff changeset
    60
		["invalid-pidfile"] = "The 'pidfile' option in the configuration file is not a string, see https://prosody.im/doc/prosodyctl#pidfile for help";
a5a080c12c96 Update every link to the documentation to use HTTPS
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7316
diff changeset
    61
		["no-posix"] = "The mod_posix module is not enabled in the Prosody config file, see https://prosody.im/doc/prosodyctl for more info";
1390
ef672c9fe7c9 prosodyctl: Allow commands to be implemented in modules
Matthew Wild <mwild1@gmail.com>
parents: 1205
diff changeset
    62
		["no-such-method"] = "This module has no commands";
1460
5882ed6219ff prosodyctl: Add message for not-running error
Matthew Wild <mwild1@gmail.com>
parents: 1459
diff changeset
    63
		["not-running"] = "Prosody is not running";
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
    64
		}, { __index = function (_,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
    66
local configmanager = require "core.configmanager";
5023
dcc8e789df36 mod_admin_telnet, prosody, prosodyctl, ejabberd2prosody: Don't depend on modules setting globals
Florian Zeitz <florob@babelmonkeys.de>
parents: 4881
diff changeset
    67
local modulemanager = require "core.modulemanager"
dcc8e789df36 mod_admin_telnet, prosody, prosodyctl, ejabberd2prosody: Don't depend on modules setting globals
Florian Zeitz <florob@babelmonkeys.de>
parents: 4881
diff changeset
    68
local prosodyctl = require "util.prosodyctl"
6787
4da860edc27c prosodyctl: Import LuaSocket to a local, don't assume that a global will be set
Kim Alvefur <zash@zash.se>
parents: 6754
diff changeset
    69
local socket = require "socket"
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8564
diff changeset
    70
local dependencies = require "util.dependencies";
8201
db82ce3decee prosody, prosodyctl: Set up TLS settings for HTTPS requests in net.http (part of fix for #659)
Kim Alvefur <zash@zash.se>
parents: 8193
diff changeset
    71
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
-----------------------
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
4142
caa78589598f prosodyctl, util.prosodyctl: Move UI functions to util.prosodyctl so they can be used outside of prosodyctl itself
Matthew Wild <mwild1@gmail.com>
parents: 4095
diff changeset
    74
local show_message, show_warning = prosodyctl.show_message, prosodyctl.show_warning;
caa78589598f prosodyctl, util.prosodyctl: Move UI functions to util.prosodyctl so they can be used outside of prosodyctl itself
Matthew Wild <mwild1@gmail.com>
parents: 4095
diff changeset
    75
local show_usage = prosodyctl.show_usage;
caa78589598f prosodyctl, util.prosodyctl: Move UI functions to util.prosodyctl so they can be used outside of prosodyctl itself
Matthew Wild <mwild1@gmail.com>
parents: 4095
diff changeset
    76
local show_yesno = prosodyctl.show_yesno;
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
    77
local show_prompt = prosodyctl.show_prompt;
4142
caa78589598f prosodyctl, util.prosodyctl: Move UI functions to util.prosodyctl so they can be used outside of prosodyctl itself
Matthew Wild <mwild1@gmail.com>
parents: 4095
diff changeset
    78
local read_password = prosodyctl.read_password;
1459
545208bc0e84 prosodyctl: Use prosodyctl_timeout option if it exists in the config
Matthew Wild <mwild1@gmail.com>
parents: 1458
diff changeset
    79
6324
c9730926002b prosodyctl: Improve JID splitting and normalization for adduser/passwd/deluser
Matthew Wild <mwild1@gmail.com>
parents: 6035
diff changeset
    80
local jid_split = require "util.jid".prepped_split;
c9730926002b prosodyctl: Improve JID splitting and normalization for adduser/passwd/deluser
Matthew Wild <mwild1@gmail.com>
parents: 6035
diff changeset
    81
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
    82
local prosodyctl_timeout = (configmanager.get("*", "prosodyctl_timeout") or 5) * 2;
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
-----------------------
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
local commands = {};
9784
c7727c13260f prosodyctl: Pass the original argv table to subcommands (with first argument removed)
Kim Alvefur <zash@zash.se>
parents: 9713
diff changeset
    85
local command = table.remove(arg, 1);
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
10141
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    87
-- This function receives no arguments. It clones all the plugins from prosody's plugin repository
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    88
function commands.get_modules(arg)
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    89
  if arg[1] == "--help" then
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    90
		show_usage([[get_modules]], [[Downloads all available modules]]);
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
    91
    return 1;
10141
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    92
  end
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    93
  if os.execute '[ -e "./downloaded_modules" ]' then
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    94
    print("The modules have already been imported")
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    95
    print("Do you want to re-import?(Y/N)")
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    96
    local answer = io.read()
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    97
    if answer == "Y" then
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    98
      print("Deleting previous imports")
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
    99
      os.execute("rm -rf downloaded_modules")
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   100
      print("Downloading plugins")
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   101
      os.execute("hg clone https://hg.prosody.im/prosody-modules/ downloaded_modules")
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   102
      print("Done!")
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   103
      return 0;
10141
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   104
    else
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   105
      print("We keep what we have then!")
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   106
      return 0;
10141
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   107
    end
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   108
  else
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   109
    print("Getting all the available modules")
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   110
    os.execute("hg clone https://hg.prosody.im/prosody-modules/ downloaded_modules")
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   111
    print("Done!")
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   112
    return 0;
10141
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   113
  end
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   114
end
04aea97afee9 prosodyctl: Added the 'get_modules' commands
João Duarte <jvsDuarte08@gmail.com>
parents: 10139
diff changeset
   115
10142
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   116
-- Function to write rockspecs from a module at working_directory/downloaded_modules
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   117
-- Receives the module's name as an argument
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   118
-- The rockspec is saved inside its module's folder
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   119
function commands.write_rockspec(arg)
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   120
  if arg[1] == "--help" then
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   121
    show_usage([[write_rockspec]], [[Picks up a module and writes an initial rockspec]]);
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   122
    return 1;
10142
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   123
  end
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   124
  print("Writing rockspec for "..arg[1])
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   125
  os.execute("luarocks write_rockspec "..arg[1].." ./downloaded_modules/"..arg[1])
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   126
  print("Rockspec created! Moving it into the ./downloaded_modules/"..arg[1].." folder")
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   127
  os.execute("mv "..arg[1].."-scm-1.rockspec ./downloaded_modules/"..arg[1])
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   128
  print("Done!")
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   129
  return 0;
10142
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   130
end
e1ca0d1b5467 prosodyctl: Added the 'write_rockspec' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10141
diff changeset
   131
10143
7aa3d8155c96 prosodyctl: Added the 'make' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10142
diff changeset
   132
-- Command to install a rockspec with local sources
7aa3d8155c96 prosodyctl: Added the 'make' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10142
diff changeset
   133
-- The module is installed at the plugins folder
7aa3d8155c96 prosodyctl: Added the 'make' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10142
diff changeset
   134
function commands.make(arg)
7aa3d8155c96 prosodyctl: Added the 'make' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10142
diff changeset
   135
  if arg[1] == "--help" then
7aa3d8155c96 prosodyctl: Added the 'make' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10142
diff changeset
   136
    show_usage([[make]], [[Installs a module with sources available locally]]);
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   137
    return 1;
10143
7aa3d8155c96 prosodyctl: Added the 'make' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10142
diff changeset
   138
  end
7aa3d8155c96 prosodyctl: Added the 'make' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10142
diff changeset
   139
  os.execute("cd downloaded_modules/"..arg[1].." && luarocks --tree='../../plugins' make "..arg[1].."-scm-1.rockspec")
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   140
  return 0;
10143
7aa3d8155c96 prosodyctl: Added the 'make' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10142
diff changeset
   141
end
7aa3d8155c96 prosodyctl: Added the 'make' function
João Duarte <jvsDuarte08@gmail.com>
parents: 10142
diff changeset
   142
10144
cf0ae6eb74e6 prosodyctl: Added the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10143
diff changeset
   143
-- Command to remove a rockspec
cf0ae6eb74e6 prosodyctl: Added the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10143
diff changeset
   144
-- Receives as an argument the name of the plugin to be removed from the plugins folder
cf0ae6eb74e6 prosodyctl: Added the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10143
diff changeset
   145
function commands.remove(arg)
cf0ae6eb74e6 prosodyctl: Added the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10143
diff changeset
   146
  if arg[1] == "--help" then
cf0ae6eb74e6 prosodyctl: Added the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10143
diff changeset
   147
    show_usage([[make]], [[Removes a module installed in the wroking directory's plugins folder]]);
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   148
    return 1;
10144
cf0ae6eb74e6 prosodyctl: Added the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10143
diff changeset
   149
  end
10148
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   150
  local flag = "--tree="
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   151
  -- I'm considering the flag is the first, but there can be many flags
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   152
  if arg[1] and arg[1]:sub(1, #flag) == flag then
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   153
    local dir = arg[1]:match("=(.+)$")
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   154
    print("Removing module "..arg[2].." at "..dir..", from luarocks repo")
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   155
    -- These extra double brackets allow us to correctly process names with spaces
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   156
    os.execute("luarocks remove --tree='"..dir.."' "..arg[2])
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   157
    return 0;
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   158
  else
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   159
    print("Removing "..arg[1].." from ./plugins")
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   160
    os.execute("luarocks --tree='"..prosody.paths.plugins.."' remove "..arg[1])
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   161
    print("Done!")
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   162
    return 0;
10148
48a62e2e2a63 prosodyctl: Improved the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10147
diff changeset
   163
  end
10144
cf0ae6eb74e6 prosodyctl: Added the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10143
diff changeset
   164
end
cf0ae6eb74e6 prosodyctl: Added the 'remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10143
diff changeset
   165
10145
cf70deac27f6 prosodyctl: Added the 'install' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10144
diff changeset
   166
function commands.install(arg)
cf70deac27f6 prosodyctl: Added the 'install' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10144
diff changeset
   167
  if arg[1] == "--help" then
cf70deac27f6 prosodyctl: Added the 'install' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10144
diff changeset
   168
    show_usage([[make]], [[Installs a rockspec/rock from a specified server]]);
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   169
    return 1;
10145
cf70deac27f6 prosodyctl: Added the 'install' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10144
diff changeset
   170
  end
10147
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   171
  -- Need to think about the case with many flags
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   172
  local flag = "--tree="
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   173
  -- I'm considering the flag is the first, but there can be many flags
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   174
  if arg[1] and arg[1]:sub(1, #flag) == flag then
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   175
    local dir = arg[1]:match("=(.+)$")
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   176
    print("Installing module "..arg[2].." at "..dir..", from luarocks repo")
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   177
    -- These extra double brackets allow us to correctly process names with spaces
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   178
    os.execute("luarocks install --tree='"..dir.."' "..arg[2])
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   179
    return 0;
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   180
  else
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   181
    print("Installing module "..arg[1].." locally, from luarocks repo")
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   182
    os.execute("luarocks --tree='"..prosody.paths.plugins.."' install "..arg[1])
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   183
    return 0;
10147
a099bff1842d prosodyctl: The 'install' command can now recognize the flag '--tree'
João Duarte <jvsDuarte08@gmail.com>
parents: 10146
diff changeset
   184
  end
10145
cf70deac27f6 prosodyctl: Added the 'install' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10144
diff changeset
   185
end
cf70deac27f6 prosodyctl: Added the 'install' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10144
diff changeset
   186
10130
ad640c2e072e prosodyctl: Implemented the 'list' command, which is a bridge to 'luarocks list'
João Duarte <jvsDuarte08@gmail.com>
parents: 10129
diff changeset
   187
function commands.list(arg)
10146
1e192e8d4e9d prosodyctl: Improved the 'list' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10145
diff changeset
   188
  if arg[1] == "--help" then
10139
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   189
		show_usage([[list]], [[Shows installed rocks]]);
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   190
		return 1;
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   191
	end
10130
ad640c2e072e prosodyctl: Implemented the 'list' command, which is a bridge to 'luarocks list'
João Duarte <jvsDuarte08@gmail.com>
parents: 10129
diff changeset
   192
  -- Need to think about the case with many flags
10132
026815f8d832 prosodyctl: Implemented a command bridge to the 'luarocks-admin add' command, called 'admin_add'
João Duarte <jvsDuarte08@gmail.com>
parents: 10130
diff changeset
   193
  local flag = "--tree="
10130
ad640c2e072e prosodyctl: Implemented the 'list' command, which is a bridge to 'luarocks list'
João Duarte <jvsDuarte08@gmail.com>
parents: 10129
diff changeset
   194
  -- I'm considering the flag is the first, but there can be many flags
ad640c2e072e prosodyctl: Implemented the 'list' command, which is a bridge to 'luarocks list'
João Duarte <jvsDuarte08@gmail.com>
parents: 10129
diff changeset
   195
  if arg[1] and arg[1]:sub(1, #flag) == flag then
ad640c2e072e prosodyctl: Implemented the 'list' command, which is a bridge to 'luarocks list'
João Duarte <jvsDuarte08@gmail.com>
parents: 10129
diff changeset
   196
    local dir = arg[1]:match("=(.+)$")
ad640c2e072e prosodyctl: Implemented the 'list' command, which is a bridge to 'luarocks list'
João Duarte <jvsDuarte08@gmail.com>
parents: 10129
diff changeset
   197
    -- These extra double brackets allow us to correctly process names with spaces
10146
1e192e8d4e9d prosodyctl: Improved the 'list' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10145
diff changeset
   198
    os.execute("luarocks list --tree='"..dir.."'")
10138
e5132c4cfb81 prosodyctl: Complemented my functions with return calls, when possible
João Duarte <jvsDuarte08@gmail.com>
parents: 10137
diff changeset
   199
    return 0;
10130
ad640c2e072e prosodyctl: Implemented the 'list' command, which is a bridge to 'luarocks list'
João Duarte <jvsDuarte08@gmail.com>
parents: 10129
diff changeset
   200
  else
10146
1e192e8d4e9d prosodyctl: Improved the 'list' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10145
diff changeset
   201
    os.execute("luarocks list --tree="..prosody.paths.plugins)
10138
e5132c4cfb81 prosodyctl: Complemented my functions with return calls, when possible
João Duarte <jvsDuarte08@gmail.com>
parents: 10137
diff changeset
   202
    return 0;
10130
ad640c2e072e prosodyctl: Implemented the 'list' command, which is a bridge to 'luarocks list'
João Duarte <jvsDuarte08@gmail.com>
parents: 10129
diff changeset
   203
  end
ad640c2e072e prosodyctl: Implemented the 'list' command, which is a bridge to 'luarocks list'
João Duarte <jvsDuarte08@gmail.com>
parents: 10129
diff changeset
   204
end
ad640c2e072e prosodyctl: Implemented the 'list' command, which is a bridge to 'luarocks list'
João Duarte <jvsDuarte08@gmail.com>
parents: 10129
diff changeset
   205
10134
667f37603308 prosodyctl: Added the 'admin_add' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10133
diff changeset
   206
function commands.admin_add(arg)
10137
73bae75c6d77 prosodyctl: Corrected the calls to the recently moved function 'admin_operation'
João Duarte <jvsDuarte08@gmail.com>
parents: 10136
diff changeset
   207
  prosodyctl.admin_operation("add ", arg)
10138
e5132c4cfb81 prosodyctl: Complemented my functions with return calls, when possible
João Duarte <jvsDuarte08@gmail.com>
parents: 10137
diff changeset
   208
  return 0;
10134
667f37603308 prosodyctl: Added the 'admin_add' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10133
diff changeset
   209
end
667f37603308 prosodyctl: Added the 'admin_add' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10133
diff changeset
   210
10135
0780bcf51ba7 prosodyctl: Added the 'admin_remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10134
diff changeset
   211
function commands.admin_remove(arg)
10137
73bae75c6d77 prosodyctl: Corrected the calls to the recently moved function 'admin_operation'
João Duarte <jvsDuarte08@gmail.com>
parents: 10136
diff changeset
   212
  prosodyctl.admin_operation("remove ", arg)
10138
e5132c4cfb81 prosodyctl: Complemented my functions with return calls, when possible
João Duarte <jvsDuarte08@gmail.com>
parents: 10137
diff changeset
   213
  return 0;
10135
0780bcf51ba7 prosodyctl: Added the 'admin_remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10134
diff changeset
   214
end
0780bcf51ba7 prosodyctl: Added the 'admin_remove' command
João Duarte <jvsDuarte08@gmail.com>
parents: 10134
diff changeset
   215
10139
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   216
function commands.enabled_plugins(arg)
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   217
  if arg[1] == "--help" then
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   218
    show_usage([[enabled_plugins]], [[Shows plugins currently enabled on prosody]]);
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   219
    return 1;
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   220
	end
10129
58fe5cff5ca2 prosodyctl: Renamed the command function 'test' to 'enabled_plugins', which now only shows the plugins, in a list
João Duarte <jvsDuarte08@gmail.com>
parents: 10128
diff changeset
   221
	for module in modulemanager.get_modules_for_host() do
10150
903db0fcb716 prosodyctl: Removed trailing whitespaces
João Duarte <jvsDuarte08@gmail.com>
parents: 10149
diff changeset
   222
		show_warning("%s", module)
10129
58fe5cff5ca2 prosodyctl: Renamed the command function 'test' to 'enabled_plugins', which now only shows the plugins, in a list
João Duarte <jvsDuarte08@gmail.com>
parents: 10128
diff changeset
   223
	end
10126
95814c597836 prosodyctl: Created a custom function, 'test', that prints back a welcoming message
João Duarte <jvsDuarte08@gmail.com>
parents: 9996
diff changeset
   224
end
95814c597836 prosodyctl: Created a custom function, 'test', that prints back a welcoming message
João Duarte <jvsDuarte08@gmail.com>
parents: 9996
diff changeset
   225
10139
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   226
function commands.local_plugins(arg)
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   227
  if arg[1] == "--help" then
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   228
    show_usage([[local_plugins]], [[Shows plugins currently available for prosody, locally]]);
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   229
    return 1;
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
   230
	end
10128
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   231
	local directory = "./plugins"
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   232
    local i, t, popen = 0, {}, io.popen
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   233
    local pfile = popen('ls -a "'..directory..'"')
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   234
    for filename in pfile:lines() do
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   235
		if filename == "." or filename == ".." then
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   236
			i = i + 1
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   237
		else
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   238
			i = i + 1
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   239
			t[i] = filename
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   240
			show_warning("%s", t[i])
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   241
        end
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   242
    end
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   243
    pfile:close()
10149
0fcd251a27e5 prosodyctl: Added missing semicolons to some return calls
João Duarte <jvsDuarte08@gmail.com>
parents: 10148
diff changeset
   244
    return 0;
10128
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   245
end
5a3611218709 prosodyctl: Added the 'local_plugins' command function, which prints back a list of locally available plugins
João Duarte <jvsDuarte08@gmail.com>
parents: 10127
diff changeset
   246
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   247
function commands.adduser(arg)
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   248
	if not arg[1] or arg[1] == "--help" then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   249
		show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   250
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   251
	end
6324
c9730926002b prosodyctl: Improve JID splitting and normalization for adduser/passwd/deluser
Matthew Wild <mwild1@gmail.com>
parents: 6035
diff changeset
   252
	local user, host = jid_split(arg[1]);
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   253
	if not user and host then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   254
		show_message [[Failed to understand JID, please supply the JID you want to create]]
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   255
		show_usage [[adduser user@host]]
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   256
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   257
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   258
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   259
	if not host then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   260
		show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   261
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   262
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   263
8721
c23cdeac5b61 prosodyctl: Use prosody.hosts instead of _G.hosts
Kim Alvefur <zash@zash.se>
parents: 8704
diff changeset
   264
	if not prosody.hosts[host] then
3038
6b68355d615a prosodyctl: Support for the new usermanager, refactor host creation and fail if a host is not using the default authentication provider, for now. (thanks Nicola)
Matthew Wild <mwild1@gmail.com>
parents: 3015
diff changeset
   265
		show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
6b68355d615a prosodyctl: Support for the new usermanager, refactor host creation and fail if a host is not using the default authentication provider, for now. (thanks Nicola)
Matthew Wild <mwild1@gmail.com>
parents: 3015
diff changeset
   266
		show_warning("The user will not be able to log in until this is changed.");
8721
c23cdeac5b61 prosodyctl: Use prosody.hosts instead of _G.hosts
Kim Alvefur <zash@zash.se>
parents: 8704
diff changeset
   267
		prosody.hosts[host] = startup.make_host(host); --luacheck: ignore 122
3038
6b68355d615a prosodyctl: Support for the new usermanager, refactor host creation and fail if a host is not using the default authentication provider, for now. (thanks Nicola)
Matthew Wild <mwild1@gmail.com>
parents: 3015
diff changeset
   268
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   269
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   270
	if prosodyctl.user_exists{ user = user, host = host } then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   271
		show_message [[That user already exists]];
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   272
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   273
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   274
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   275
	local password = read_password();
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   276
	if not password then return 1; end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   277
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   278
	local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   279
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   280
	if ok then return 0; end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   281
3777
5ecbcef42ffb mod_admin_adhoc: Support for reloading multiple modules
Florian Zeitz <florob@babelmonkeys.de>
parents: 3773
diff changeset
   282
	show_message(msg)
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   283
	return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   284
end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   285
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   286
function commands.passwd(arg)
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   287
	if not arg[1] or arg[1] == "--help" then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   288
		show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   289
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   290
	end
5610
f73d5fb4ea13 prosodyctl: Use jid.split() to parse parameter to adduser/deluser/passwd
Matthew Wild <mwild1@gmail.com>
parents: 5592
diff changeset
   291
	local user, host = jid_split(arg[1]);
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   292
	if not user and host then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   293
		show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   294
		show_usage [[passwd user@host]]
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   295
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   296
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   297
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   298
	if not host then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   299
		show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   300
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   301
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   302
8721
c23cdeac5b61 prosodyctl: Use prosody.hosts instead of _G.hosts
Kim Alvefur <zash@zash.se>
parents: 8704
diff changeset
   303
	if not prosody.hosts[host] then
3038
6b68355d615a prosodyctl: Support for the new usermanager, refactor host creation and fail if a host is not using the default authentication provider, for now. (thanks Nicola)
Matthew Wild <mwild1@gmail.com>
parents: 3015
diff changeset
   304
		show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
6b68355d615a prosodyctl: Support for the new usermanager, refactor host creation and fail if a host is not using the default authentication provider, for now. (thanks Nicola)
Matthew Wild <mwild1@gmail.com>
parents: 3015
diff changeset
   305
		show_warning("The user will not be able to log in until this is changed.");
8721
c23cdeac5b61 prosodyctl: Use prosody.hosts instead of _G.hosts
Kim Alvefur <zash@zash.se>
parents: 8704
diff changeset
   306
		prosody.hosts[host] = startup.make_host(host); --luacheck: ignore 122
3038
6b68355d615a prosodyctl: Support for the new usermanager, refactor host creation and fail if a host is not using the default authentication provider, for now. (thanks Nicola)
Matthew Wild <mwild1@gmail.com>
parents: 3015
diff changeset
   307
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   308
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   309
	if not prosodyctl.user_exists { user = user, host = host } then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   310
		show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   311
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   312
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   313
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   314
	local password = read_password();
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   315
	if not password then return 1; end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   316
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   317
	local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   318
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   319
	if ok then return 0; end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   320
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   321
	show_message(error_messages[msg])
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   322
	return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   323
end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   324
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   325
function commands.deluser(arg)
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   326
	if not arg[1] or arg[1] == "--help" then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   327
		show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   328
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   329
	end
6324
c9730926002b prosodyctl: Improve JID splitting and normalization for adduser/passwd/deluser
Matthew Wild <mwild1@gmail.com>
parents: 6035
diff changeset
   330
	local user, host = jid_split(arg[1]);
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   331
	if not user and host then
7679
7311dc843718 prosodyctl: Fix copy/paste error in help text for deluser command
Matthew Wild <mwild1@gmail.com>
parents: 6501
diff changeset
   332
		show_message [[Failed to understand JID, please supply the JID to the user account you want to delete]]
7311dc843718 prosodyctl: Fix copy/paste error in help text for deluser command
Matthew Wild <mwild1@gmail.com>
parents: 6501
diff changeset
   333
		show_usage [[deluser user@host]]
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   334
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   335
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   336
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   337
	if not host then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   338
		show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   339
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   340
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   341
8721
c23cdeac5b61 prosodyctl: Use prosody.hosts instead of _G.hosts
Kim Alvefur <zash@zash.se>
parents: 8704
diff changeset
   342
	if not prosody.hosts[host] then
3038
6b68355d615a prosodyctl: Support for the new usermanager, refactor host creation and fail if a host is not using the default authentication provider, for now. (thanks Nicola)
Matthew Wild <mwild1@gmail.com>
parents: 3015
diff changeset
   343
		show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
8721
c23cdeac5b61 prosodyctl: Use prosody.hosts instead of _G.hosts
Kim Alvefur <zash@zash.se>
parents: 8704
diff changeset
   344
		prosody.hosts[host] = startup.make_host(host); --luacheck: ignore 122
3038
6b68355d615a prosodyctl: Support for the new usermanager, refactor host creation and fail if a host is not using the default authentication provider, for now. (thanks Nicola)
Matthew Wild <mwild1@gmail.com>
parents: 3015
diff changeset
   345
	end
6b68355d615a prosodyctl: Support for the new usermanager, refactor host creation and fail if a host is not using the default authentication provider, for now. (thanks Nicola)
Matthew Wild <mwild1@gmail.com>
parents: 3015
diff changeset
   346
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   347
	if not prosodyctl.user_exists { user = user, host = host } then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   348
		show_message [[That user does not exist on this server]]
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   349
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   350
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   351
5101
a94c43cad081 prosodyctl: Use util.prosodyctl.deluser
Kim Alvefur <zash@zash.se>
parents: 5024
diff changeset
   352
	local ok, msg = prosodyctl.deluser { user = user, host = host };
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   353
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   354
	if ok then return 0; end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   355
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   356
	show_message(error_messages[msg])
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   357
	return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   358
end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   359
1089
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   360
function commands.start(arg)
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   361
	if arg[1] == "--help" then
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   362
		show_usage([[start]], [[Start Prosody]]);
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   363
		return 1;
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   364
	end
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   365
	local ok, ret = prosodyctl.isrunning();
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   366
	if not ok then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   367
		show_message(error_messages[ret]);
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   368
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   369
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   370
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   371
	if ret then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   372
		--luacheck: ignore 421/ret
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   373
		local ok, ret = prosodyctl.getpid();
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   374
		if not ok then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   375
			show_message("Couldn't get running Prosody's PID");
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   376
			show_message(error_messages[ret]);
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   377
			return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   378
		end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   379
		show_message("Prosody is already running with PID %s", ret or "(unknown)");
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   380
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   381
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   382
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   383
	--luacheck: ignore 411/ret
9786
d844e197eedf prosodyctl: Use the same runtime for starting prosody
Kim Alvefur <zash@zash.se>
parents: 9784
diff changeset
   384
	local ok, ret = prosodyctl.start(prosody.paths.source, arg[-1]);
1458
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   385
	if ok then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   386
		local daemonize = configmanager.get("*", "daemonize");
6062
6cc6b4d407df prosodyctl, util.prosodyctl: Update to reflect that mod_posix gets loaded by default on posix platforms
Kim Alvefur <zash@zash.se>
parents: 6038
diff changeset
   387
		if daemonize == nil then
6cc6b4d407df prosodyctl, util.prosodyctl: Update to reflect that mod_posix gets loaded by default on posix platforms
Kim Alvefur <zash@zash.se>
parents: 6038
diff changeset
   388
			daemonize = prosody.installed;
6cc6b4d407df prosodyctl, util.prosodyctl: Update to reflect that mod_posix gets loaded by default on posix platforms
Kim Alvefur <zash@zash.se>
parents: 6038
diff changeset
   389
		end
6cc6b4d407df prosodyctl, util.prosodyctl: Update to reflect that mod_posix gets loaded by default on posix platforms
Kim Alvefur <zash@zash.se>
parents: 6038
diff changeset
   390
		if daemonize then
2511
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   391
			local i=1;
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   392
			while true do
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   393
				local ok, running = prosodyctl.isrunning();
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   394
				if ok and running then
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   395
					break;
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   396
				elseif i == 5 then
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   397
					show_message("Still waiting...");
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   398
				elseif i >= prosodyctl_timeout then
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   399
					show_message("Prosody is still not running. Please give it some time or check your log files for errors.");
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   400
					return 2;
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   401
				end
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   402
				socket.sleep(0.5);
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   403
				i = i + 1;
1458
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   404
			end
2511
a81c710b1708 prosodyctl: Don't display message about failing to start Prosody is daemonizing is disabled (if daemonizing is disabled then Prosody is stopped by the time control returns to prosodyctl, which then can't see Prosody running)
Matthew Wild <mwild1@gmail.com>
parents: 2510
diff changeset
   405
			show_message("Started");
1458
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   406
		end
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   407
		return 0;
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   408
	end
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   409
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   410
	show_message("Failed to start Prosody");
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   411
	show_message(error_messages[ret])
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   412
	return 1;
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   413
end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   414
1089
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   415
function commands.status(arg)
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   416
	if arg[1] == "--help" then
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   417
		show_usage([[status]], [[Reports the running status of Prosody]]);
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   418
		return 1;
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   419
	end
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   420
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   421
	local ok, ret = prosodyctl.isrunning();
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   422
	if not ok then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   423
		show_message(error_messages[ret]);
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   424
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   425
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   426
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   427
	if ret then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   428
		--luacheck: ignore 421/ret
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   429
		local ok, ret = prosodyctl.getpid();
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   430
		if not ok then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   431
			show_message("Couldn't get running Prosody's PID");
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   432
			show_message(error_messages[ret]);
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   433
			return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   434
		end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   435
		show_message("Prosody is running with PID %s", ret or "(unknown)");
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   436
		return 0;
1089
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   437
	else
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   438
		show_message("Prosody is not running");
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   439
		if not prosody.switched_user and prosody.current_uid ~= 0 then
1122
07b2b5942957 prosodyctl: Reformat note to fit in small-width terminals
Matthew Wild <mwild1@gmail.com>
parents: 1120
diff changeset
   440
			print("\nNote:")
07b2b5942957 prosodyctl: Reformat note to fit in small-width terminals
Matthew Wild <mwild1@gmail.com>
parents: 1120
diff changeset
   441
			print(" You will also see this if prosodyctl is not running under");
07b2b5942957 prosodyctl: Reformat note to fit in small-width terminals
Matthew Wild <mwild1@gmail.com>
parents: 1120
diff changeset
   442
			print(" the same user account as Prosody. Try running as root (e.g. ");
07b2b5942957 prosodyctl: Reformat note to fit in small-width terminals
Matthew Wild <mwild1@gmail.com>
parents: 1120
diff changeset
   443
			print(" with 'sudo' in front) to gain access to Prosody's real status.");
1115
8a7bc1a5eae6 prosodyctl: status: Show warning if we can't find a running Prosody, and we didn't switch user
Matthew Wild <mwild1@gmail.com>
parents: 1114
diff changeset
   444
		end
1089
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   445
		return 2
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   446
	end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   447
end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   448
1089
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   449
function commands.stop(arg)
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   450
	if arg[1] == "--help" then
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   451
		show_usage([[stop]], [[Stop a running Prosody server]]);
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   452
		return 1;
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   453
	end
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   454
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   455
	if not prosodyctl.isrunning() then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   456
		show_message("Prosody is not running");
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   457
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   458
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   459
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   460
	local ok, ret = prosodyctl.stop();
1458
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   461
	if ok then
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   462
		local i=1;
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   463
		while true do
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   464
			local ok, running = prosodyctl.isrunning();
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   465
			if ok and not running then
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   466
				break;
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   467
			elseif i == 5 then
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   468
				show_message("Still waiting...");
1459
545208bc0e84 prosodyctl: Use prosodyctl_timeout option if it exists in the config
Matthew Wild <mwild1@gmail.com>
parents: 1458
diff changeset
   469
			elseif i >= prosodyctl_timeout then
1458
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   470
				show_message("Prosody is still running. Please give it some time or check your log files for errors.");
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   471
				return 2;
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   472
			end
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   473
			socket.sleep(0.5);
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   474
			i = i + 1;
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   475
		end
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   476
		show_message("Stopped");
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   477
		return 0;
fce75b4efda9 prosodyctl: Improve usability with status messages, and waiting up to 5s for the server to start/stop
Matthew Wild <mwild1@gmail.com>
parents: 1390
diff changeset
   478
	end
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   479
1089
a817cbaa0f46 prosodyctl: Multiple fixes for start/status/stop
Matthew Wild <mwild1@gmail.com>
parents: 1087
diff changeset
   480
	show_message(error_messages[ret]);
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   481
	return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   482
end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   483
2696
cb5acafbec62 prosodyctl: Add restart command for KSid and johnny :)
Matthew Wild <mwild1@gmail.com>
parents: 2587
diff changeset
   484
function commands.restart(arg)
cb5acafbec62 prosodyctl: Add restart command for KSid and johnny :)
Matthew Wild <mwild1@gmail.com>
parents: 2587
diff changeset
   485
	if arg[1] == "--help" then
2705
8a5af6f14c07 prosodyctl: Fix 'restart' command to not report itself as the 'stop' command (thanks albert!)
Matthew Wild <mwild1@gmail.com>
parents: 2696
diff changeset
   486
		show_usage([[restart]], [[Restart a running Prosody server]]);
2696
cb5acafbec62 prosodyctl: Add restart command for KSid and johnny :)
Matthew Wild <mwild1@gmail.com>
parents: 2587
diff changeset
   487
		return 1;
cb5acafbec62 prosodyctl: Add restart command for KSid and johnny :)
Matthew Wild <mwild1@gmail.com>
parents: 2587
diff changeset
   488
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   489
3724
c12ebbd4ab61 prosodyctl: Make the 'restart' command start Prosody even if it wasn't already running
Matthew Wild <mwild1@gmail.com>
parents: 3630
diff changeset
   490
	commands.stop(arg);
c12ebbd4ab61 prosodyctl: Make the 'restart' command start Prosody even if it wasn't already running
Matthew Wild <mwild1@gmail.com>
parents: 3630
diff changeset
   491
	return commands.start(arg);
2696
cb5acafbec62 prosodyctl: Add restart command for KSid and johnny :)
Matthew Wild <mwild1@gmail.com>
parents: 2587
diff changeset
   492
end
cb5acafbec62 prosodyctl: Add restart command for KSid and johnny :)
Matthew Wild <mwild1@gmail.com>
parents: 2587
diff changeset
   493
4324
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   494
function commands.about(arg)
4331
9c45858e3208 prosodyctl: Fix 'about' command to not show up where it shouldn't...
Matthew Wild <mwild1@gmail.com>
parents: 4324
diff changeset
   495
	if arg[1] == "--help" then
9c45858e3208 prosodyctl: Fix 'about' command to not show up where it shouldn't...
Matthew Wild <mwild1@gmail.com>
parents: 4324
diff changeset
   496
		show_usage([[about]], [[Show information about this Prosody installation]]);
9c45858e3208 prosodyctl: Fix 'about' command to not show up where it shouldn't...
Matthew Wild <mwild1@gmail.com>
parents: 4324
diff changeset
   497
		return 1;
9c45858e3208 prosodyctl: Fix 'about' command to not show up where it shouldn't...
Matthew Wild <mwild1@gmail.com>
parents: 4324
diff changeset
   498
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   499
6587
c3a56f8847ac prosodyctl: Show relative paths in about
Kim Alvefur <zash@zash.se>
parents: 6504
diff changeset
   500
	local pwd = ".";
5023
dcc8e789df36 mod_admin_telnet, prosody, prosodyctl, ejabberd2prosody: Don't depend on modules setting globals
Florian Zeitz <florob@babelmonkeys.de>
parents: 4881
diff changeset
   501
	local array = require "util.array";
4815
04e6115e060b prosodyctl: Fix import of util.iterators
Kim Alvefur <zash@zash.se>
parents: 4487
diff changeset
   502
	local keys = require "util.iterators".keys;
6590
54306208f30b prosodyctl: Expand plugin paths and attempt to identify prosody-modules checkouts
Kim Alvefur <zash@zash.se>
parents: 6589
diff changeset
   503
	local hg = require"util.mercurial";
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   504
	local relpath = configmanager.resolve_relative_path;
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   505
4324
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   506
	print("Prosody "..(prosody.version or "(unknown version)"));
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   507
	print("");
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   508
	print("# Prosody directories");
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8564
diff changeset
   509
	print("Data directory:     "..relpath(pwd, prosody.paths.data));
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8564
diff changeset
   510
	print("Config directory:   "..relpath(pwd, prosody.paths.config or "."));
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8564
diff changeset
   511
	print("Source directory:   "..relpath(pwd, prosody.paths.source or "."));
6590
54306208f30b prosodyctl: Expand plugin paths and attempt to identify prosody-modules checkouts
Kim Alvefur <zash@zash.se>
parents: 6589
diff changeset
   512
	print("Plugin directories:")
54306208f30b prosodyctl: Expand plugin paths and attempt to identify prosody-modules checkouts
Kim Alvefur <zash@zash.se>
parents: 6589
diff changeset
   513
	print("  "..(prosody.paths.plugins:gsub("([^;]+);?", function(path)
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   514
			path = configmanager.resolve_relative_path(pwd, path);
6590
54306208f30b prosodyctl: Expand plugin paths and attempt to identify prosody-modules checkouts
Kim Alvefur <zash@zash.se>
parents: 6589
diff changeset
   515
			local hgid, hgrepo = hg.check_id(path);
54306208f30b prosodyctl: Expand plugin paths and attempt to identify prosody-modules checkouts
Kim Alvefur <zash@zash.se>
parents: 6589
diff changeset
   516
			if not hgid and hgrepo then
54306208f30b prosodyctl: Expand plugin paths and attempt to identify prosody-modules checkouts
Kim Alvefur <zash@zash.se>
parents: 6589
diff changeset
   517
				return path.." - "..hgrepo .."!\n  ";
54306208f30b prosodyctl: Expand plugin paths and attempt to identify prosody-modules checkouts
Kim Alvefur <zash@zash.se>
parents: 6589
diff changeset
   518
			end
6614
65dd3770bcb0 prosodyctl: Document magic commit ID
Paul Aurich <paul@darkrain42.org>
parents: 6590
diff changeset
   519
			-- 010452cfaf53 is the first commit in the prosody-modules repository
6590
54306208f30b prosodyctl: Expand plugin paths and attempt to identify prosody-modules checkouts
Kim Alvefur <zash@zash.se>
parents: 6589
diff changeset
   520
			hgrepo = hgrepo == "010452cfaf53" and "prosody-modules";
54306208f30b prosodyctl: Expand plugin paths and attempt to identify prosody-modules checkouts
Kim Alvefur <zash@zash.se>
parents: 6589
diff changeset
   521
			return path..(hgid and " - "..(hgrepo or "HG").." rev: "..hgid or "")
54306208f30b prosodyctl: Expand plugin paths and attempt to identify prosody-modules checkouts
Kim Alvefur <zash@zash.se>
parents: 6589
diff changeset
   522
				.."\n  ";
54306208f30b prosodyctl: Expand plugin paths and attempt to identify prosody-modules checkouts
Kim Alvefur <zash@zash.se>
parents: 6589
diff changeset
   523
		end)));
4324
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   524
	print("");
9830
bdc2a024933b prosodyctl: about: Report the current operating system according to uname
Kim Alvefur <zash@zash.se>
parents: 9802
diff changeset
   525
	local have_pposix, pposix = pcall(require, "util.pposix");
bdc2a024933b prosodyctl: about: Report the current operating system according to uname
Kim Alvefur <zash@zash.se>
parents: 9802
diff changeset
   526
	if have_pposix and pposix.uname then
bdc2a024933b prosodyctl: about: Report the current operating system according to uname
Kim Alvefur <zash@zash.se>
parents: 9802
diff changeset
   527
		print("# Operating system");
bdc2a024933b prosodyctl: about: Report the current operating system according to uname
Kim Alvefur <zash@zash.se>
parents: 9802
diff changeset
   528
		local uname, err = pposix.uname();
bdc2a024933b prosodyctl: about: Report the current operating system according to uname
Kim Alvefur <zash@zash.se>
parents: 9802
diff changeset
   529
		print(uname and uname.sysname .. " " .. uname.release or "Unknown POSIX", err or "");
bdc2a024933b prosodyctl: about: Report the current operating system according to uname
Kim Alvefur <zash@zash.se>
parents: 9802
diff changeset
   530
		print("");
bdc2a024933b prosodyctl: about: Report the current operating system according to uname
Kim Alvefur <zash@zash.se>
parents: 9802
diff changeset
   531
	end
4324
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   532
	print("# Lua environment");
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   533
	print("Lua version:             ", _G._VERSION);
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   534
	print("");
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   535
	print("Lua module search paths:");
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   536
	for path in package.path:gmatch("[^;]+") do
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   537
		print("  "..path);
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   538
	end
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   539
	print("");
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   540
	print("Lua C module search paths:");
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   541
	for path in package.cpath:gmatch("[^;]+") do
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   542
		print("  "..path);
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   543
	end
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   544
	print("");
8704
b7a22baaf55f prosodyctl: Increase robustness in luarocks version detection (fixes #1003)
Kim Alvefur <zash@zash.se>
parents: 8674
diff changeset
   545
	local luarocks_status = "Not installed"
b7a22baaf55f prosodyctl: Increase robustness in luarocks version detection (fixes #1003)
Kim Alvefur <zash@zash.se>
parents: 8674
diff changeset
   546
	if pcall(require, "luarocks.loader") then
b7a22baaf55f prosodyctl: Increase robustness in luarocks version detection (fixes #1003)
Kim Alvefur <zash@zash.se>
parents: 8674
diff changeset
   547
		luarocks_status = "Installed (2.x+)";
b7a22baaf55f prosodyctl: Increase robustness in luarocks version detection (fixes #1003)
Kim Alvefur <zash@zash.se>
parents: 8674
diff changeset
   548
		if package.loaded["luarocks.cfg"] then
b7a22baaf55f prosodyctl: Increase robustness in luarocks version detection (fixes #1003)
Kim Alvefur <zash@zash.se>
parents: 8674
diff changeset
   549
			luarocks_status = "Installed ("..(package.loaded["luarocks.cfg"].program_version or "2.x+")..")";
b7a22baaf55f prosodyctl: Increase robustness in luarocks version detection (fixes #1003)
Kim Alvefur <zash@zash.se>
parents: 8674
diff changeset
   550
		end
b7a22baaf55f prosodyctl: Increase robustness in luarocks version detection (fixes #1003)
Kim Alvefur <zash@zash.se>
parents: 8674
diff changeset
   551
	elseif pcall(require, "luarocks.require") then
b7a22baaf55f prosodyctl: Increase robustness in luarocks version detection (fixes #1003)
Kim Alvefur <zash@zash.se>
parents: 8674
diff changeset
   552
		luarocks_status = "Installed (1.x)";
b7a22baaf55f prosodyctl: Increase robustness in luarocks version detection (fixes #1003)
Kim Alvefur <zash@zash.se>
parents: 8674
diff changeset
   553
	end
4333
040193dead77 prosodyctl: Add info about the presence of LuaRocks to 'about' command
Matthew Wild <mwild1@gmail.com>
parents: 4331
diff changeset
   554
	print("LuaRocks:        ", luarocks_status);
040193dead77 prosodyctl: Add info about the presence of LuaRocks to 'about' command
Matthew Wild <mwild1@gmail.com>
parents: 4331
diff changeset
   555
	print("");
9860
4be2af104bf0 prosodyctl about: Report network backend in use
Matthew Wild <mwild1@gmail.com>
parents: 9713
diff changeset
   556
	print("# Network");
4be2af104bf0 prosodyctl about: Report network backend in use
Matthew Wild <mwild1@gmail.com>
parents: 9713
diff changeset
   557
	print("");
4be2af104bf0 prosodyctl about: Report network backend in use
Matthew Wild <mwild1@gmail.com>
parents: 9713
diff changeset
   558
	print("Backend: "..require "net.server".get_backend());
4be2af104bf0 prosodyctl about: Report network backend in use
Matthew Wild <mwild1@gmail.com>
parents: 9713
diff changeset
   559
	print("");
4324
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   560
	print("# Lua module versions");
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   561
	local module_versions, longest_name = {}, 8;
6714
76683a3f3d75 prosodyctl: Soft-require LuaSec and LuaEvent so they show up in the module version listing
Kim Alvefur <zash@zash.se>
parents: 6640
diff changeset
   562
	local luaevent =dependencies.softreq"luaevent";
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   563
	dependencies.softreq"ssl";
9995
8cd180dc18a8 prosodyctl: Include version of LuaDBI in 'about'
Kim Alvefur <zash@zash.se>
parents: 9860
diff changeset
   564
	dependencies.softreq"DBI";
4324
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   565
	for name, module in pairs(package.loaded) do
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   566
		if type(module) == "table" and rawget(module, "_VERSION")
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   567
		and name ~= "_G" and not name:match("%.") then
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   568
			if #name > longest_name then
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   569
				longest_name = #name;
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   570
			end
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   571
			module_versions[name] = module._VERSION;
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   572
		end
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   573
	end
7252
c49f69bb68f7 prosodyctl: Include libevent version in "about" output if luaevent is available
Kim Alvefur <zash@zash.se>
parents: 7216
diff changeset
   574
	if luaevent then
c49f69bb68f7 prosodyctl: Include libevent version in "about" output if luaevent is available
Kim Alvefur <zash@zash.se>
parents: 7216
diff changeset
   575
		module_versions["libevent"] = luaevent.core.libevent_version();
c49f69bb68f7 prosodyctl: Include libevent version in "about" output if luaevent is available
Kim Alvefur <zash@zash.se>
parents: 7216
diff changeset
   576
	end
4324
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   577
	local sorted_keys = array.collect(keys(module_versions)):sort();
7253
6ffc9247417a prosodyctl: Use already sorted array of module names [luacheck]
Kim Alvefur <zash@zash.se>
parents: 7252
diff changeset
   578
	for _, name in ipairs(sorted_keys) do
4324
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   579
		print(name..":"..string.rep(" ", longest_name-#name), module_versions[name]);
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   580
	end
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   581
	print("");
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   582
end
5e7cba840409 prosodyctl: Add 'about' command to list Prosody version, directories, and various other stuff
Matthew Wild <mwild1@gmail.com>
parents: 4167
diff changeset
   583
4335
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   584
function commands.reload(arg)
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   585
	if arg[1] == "--help" then
4476
53ce21286b8c prosodyctl: Adjust description of 'reload' command (thanks crocket)
Matthew Wild <mwild1@gmail.com>
parents: 4336
diff changeset
   586
		show_usage([[reload]], [[Reload Prosody's configuration and re-open log files]]);
4335
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   587
		return 1;
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   588
	end
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   589
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   590
	if not prosodyctl.isrunning() then
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   591
		show_message("Prosody is not running");
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   592
		return 1;
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   593
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   594
4335
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   595
	local ok, ret = prosodyctl.reload();
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   596
	if ok then
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   597
4336
abcbcb15205c prosodyctl: Update message on reload success
Matthew Wild <mwild1@gmail.com>
parents: 4335
diff changeset
   598
		show_message("Prosody log files re-opened and config file reloaded. You may need to reload modules for some changes to take effect.");
4335
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   599
		return 0;
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   600
	end
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   601
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   602
	show_message(error_messages[ret]);
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   603
	return 1;
3a2a01432b5c Add "reload" command to prosodyctl
Vladimir Protasov <eoranged@ya.ru>
parents: 4334
diff changeset
   604
end
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   605
-- ejabberdctl compatibility
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   606
7923
1db51061342b prosodyctl: Handle move of 'unpack' in Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 7682
diff changeset
   607
local unpack = table.unpack or unpack; -- luacheck: ignore 113
1db51061342b prosodyctl: Handle move of 'unpack' in Lua 5.2
Kim Alvefur <zash@zash.se>
parents: 7682
diff changeset
   608
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   609
function commands.register(arg)
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   610
	local user, host, password = unpack(arg);
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   611
	if (not (user and host)) or arg[1] == "--help" then
1102
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   612
		if user ~= "--help" then
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   613
			if not user then
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   614
				show_message [[No username specified]]
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   615
			elseif not host then
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   616
				show_message [[Please specify which host you want to register the user on]];
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   617
			end
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   618
		end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   619
		show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   620
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   621
	end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   622
	if not password then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   623
		password = read_password();
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   624
		if not password then
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   625
			show_message [[Unable to register user with no password]];
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   626
			return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   627
		end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   628
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   629
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   630
	local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   631
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   632
	if ok then return 0; end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   633
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   634
	show_message(error_messages[msg])
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   635
	return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   636
end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   637
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   638
function commands.unregister(arg)
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   639
	local user, host = unpack(arg);
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   640
	if (not (user and host)) or arg[1] == "--help" then
1102
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   641
		if user ~= "--help" then
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   642
			if not user then
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   643
				show_message [[No username specified]]
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   644
			elseif not host then
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   645
				show_message [[Please specify which host you want to unregister the user from]];
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   646
			end
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   647
		end
1102
c81df501fd38 prosodyctl: Hide ejabberd compatibility commands from command listing
Matthew Wild <mwild1@gmail.com>
parents: 1089
diff changeset
   648
		show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   649
		return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   650
	end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   651
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   652
	local ok, msg = prosodyctl.deluser { user = user, host = host };
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   653
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   654
	if ok then return 0; end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
   655
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   656
	show_message(error_messages[msg])
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   657
	return 1;
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   658
end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   659
5292
46fbb5f1ef0a prosodyctl: Load LFS and util.openssl when actually needed (fixes unhelpful warnings if no LuaSec installed)
Kim Alvefur <zash@zash.se>
parents: 5152
diff changeset
   660
local openssl;
46fbb5f1ef0a prosodyctl: Load LFS and util.openssl when actually needed (fixes unhelpful warnings if no LuaSec installed)
Kim Alvefur <zash@zash.se>
parents: 5152
diff changeset
   661
local lfs;
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   662
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   663
local cert_commands = {};
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   664
6840
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   665
-- If a file already exists, ask if the user wants to use it or replace it
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   666
-- Backups the old file if replaced
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   667
local function use_existing(filename)
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   668
	local attrs = lfs.attributes(filename);
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   669
	if attrs then
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   670
		if show_yesno(filename .. " exists, do you want to replace it? [y/n]") then
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   671
			local backup = filename..".bkp~"..os.date("%FT%T", attrs.change);
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   672
			os.rename(filename, backup);
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   673
			show_message(filename.." backed up to "..backup);
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   674
		else
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   675
			-- Use the existing file
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   676
			return true;
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   677
		end
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   678
	end
4826
1c4852da78c8 prosodyctl: Replace hack with lfs for checking if a file exists
Kim Alvefur <zash@zash.se>
parents: 4824
diff changeset
   679
end
1c4852da78c8 prosodyctl: Replace hack with lfs for checking if a file exists
Kim Alvefur <zash@zash.se>
parents: 4824
diff changeset
   680
8638
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8564
diff changeset
   681
local have_pposix, pposix = pcall(require, "util.pposix");
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8564
diff changeset
   682
local cert_basedir = prosody.paths.data == "." and "./certs" or prosody.paths.data;
8103
1773559b03a8 prosodyctl cert: If running as root, write certificate files to config directory (fixes #530)
Kim Alvefur <zash@zash.se>
parents: 8102
diff changeset
   683
if have_pposix and pposix.getuid() == 0 then
1773559b03a8 prosodyctl cert: If running as root, write certificate files to config directory (fixes #530)
Kim Alvefur <zash@zash.se>
parents: 8102
diff changeset
   684
	-- FIXME should be enough to check if this directory is writable
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   685
	local cert_dir = configmanager.get("*", "certificates") or "certs";
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   686
	cert_basedir = configmanager.resolve_relative_path(prosody.paths.config, cert_dir);
8103
1773559b03a8 prosodyctl cert: If running as root, write certificate files to config directory (fixes #530)
Kim Alvefur <zash@zash.se>
parents: 8102
diff changeset
   687
end
1773559b03a8 prosodyctl cert: If running as root, write certificate files to config directory (fixes #530)
Kim Alvefur <zash@zash.se>
parents: 8102
diff changeset
   688
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   689
function cert_commands.config(arg)
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   690
	if #arg >= 1 and arg[1] ~= "--help" then
8103
1773559b03a8 prosodyctl cert: If running as root, write certificate files to config directory (fixes #530)
Kim Alvefur <zash@zash.se>
parents: 8102
diff changeset
   691
		local conf_filename = cert_basedir .. "/" .. arg[1] .. ".cnf";
6840
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   692
		if use_existing(conf_filename) then
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   693
			return nil, conf_filename;
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   694
		end
7196
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   695
		local distinguished_name;
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   696
		if arg[#arg]:find("^/") then
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   697
			distinguished_name = table.remove(arg);
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   698
		end
4824
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   699
		local conf = openssl.config.new();
8721
c23cdeac5b61 prosodyctl: Use prosody.hosts instead of _G.hosts
Kim Alvefur <zash@zash.se>
parents: 8704
diff changeset
   700
		conf:from_prosody(prosody.hosts, configmanager, arg);
7196
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   701
		if distinguished_name then
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   702
			local dn = {};
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   703
			for k, v in distinguished_name:gmatch("/([^=/]+)=([^/]+)") do
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   704
				table.insert(dn, k);
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   705
				dn[k] = v;
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   706
			end
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   707
			conf.distinguished_name = dn;
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   708
		else
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   709
			show_message("Please provide details to include in the certificate config file.");
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   710
			show_message("Leave the field empty to use the default value or '.' to exclude the field.")
7486
8c5320a4bfaf prosodyctl: remove unused one-letter loop variable [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7458
diff changeset
   711
			for _, k in ipairs(openssl._DN_order) do
7196
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   712
				local v = conf.distinguished_name[k];
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   713
				if v then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   714
					local nv = nil;
7196
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   715
					if k == "commonName" then
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   716
						v = arg[1]
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   717
					elseif k == "emailAddress" then
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   718
						v = "xmpp@" .. arg[1];
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   719
					elseif k == "countryName" then
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   720
						local tld = arg[1]:match"%.([a-z]+)$";
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   721
						if tld and #tld == 2 and tld ~= "uk" then
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   722
							v = tld:upper();
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   723
						end
5547
f306daf2bf6d prosodyctl: Guess the country from the TLD for the cert config
Kim Alvefur <zash@zash.se>
parents: 5546
diff changeset
   724
					end
7196
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   725
					nv = show_prompt(("%s (%s):"):format(k, nv or v));
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   726
					nv = (not nv or nv == "") and v or nv;
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   727
					if nv:find"[\192-\252][\128-\191]+" then
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   728
						conf.req.string_mask = "utf8only"
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   729
					end
1c0104a56321 prosodyctl: Allow a DN path to be given to 'cert generate' command (fixes #349)
Kim Alvefur <zash@zash.se>
parents: 7193
diff changeset
   730
					conf.distinguished_name[k] = nv ~= "." and nv or nil;
5546
edc97af48d19 prosodyctl: Ask about the distinguished name in a in a consistent order
Kim Alvefur <zash@zash.se>
parents: 5545
diff changeset
   731
				end
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   732
			end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   733
		end
6035
1b5ca55bf895 prosodyctl: Show real error if certificate config file can't be opened
Kim Alvefur <zash@zash.se>
parents: 5547
diff changeset
   734
		local conf_file, err = io.open(conf_filename, "w");
1b5ca55bf895 prosodyctl: Show real error if certificate config file can't be opened
Kim Alvefur <zash@zash.se>
parents: 5547
diff changeset
   735
		if not conf_file then
1b5ca55bf895 prosodyctl: Show real error if certificate config file can't be opened
Kim Alvefur <zash@zash.se>
parents: 5547
diff changeset
   736
			show_warning("Could not open OpenSSL config file for writing");
1b5ca55bf895 prosodyctl: Show real error if certificate config file can't be opened
Kim Alvefur <zash@zash.se>
parents: 5547
diff changeset
   737
			show_warning(err);
1b5ca55bf895 prosodyctl: Show real error if certificate config file can't be opened
Kim Alvefur <zash@zash.se>
parents: 5547
diff changeset
   738
			os.exit(1);
1b5ca55bf895 prosodyctl: Show real error if certificate config file can't be opened
Kim Alvefur <zash@zash.se>
parents: 5547
diff changeset
   739
		end
4824
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   740
		conf_file:write(conf:serialize());
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   741
		conf_file:close();
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   742
		print("");
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   743
		show_message("Config written to " .. conf_filename);
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   744
		return nil, conf_filename;
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   745
	else
4872
b2059452fb55 prosodyctl: Improve help messages for cert commands
Kim Alvefur <zash@zash.se>
parents: 4827
diff changeset
   746
		show_usage("cert config HOSTNAME [HOSTNAME+]", "Builds a certificate config file covering the supplied hostname(s)")
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   747
	end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   748
end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   749
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   750
function cert_commands.key(arg)
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   751
	if #arg >= 1 and arg[1] ~= "--help" then
8103
1773559b03a8 prosodyctl cert: If running as root, write certificate files to config directory (fixes #530)
Kim Alvefur <zash@zash.se>
parents: 8102
diff changeset
   752
		local key_filename = cert_basedir .. "/" .. arg[1] .. ".key";
6840
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   753
		if use_existing(key_filename) then
4826
1c4852da78c8 prosodyctl: Replace hack with lfs for checking if a file exists
Kim Alvefur <zash@zash.se>
parents: 4824
diff changeset
   754
			return nil, key_filename;
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   755
		end
5151
dfe6a70efaa2 prosodyctl: Set stricter umask while generating key (thanks darkrain)
Kim Alvefur <zash@zash.se>
parents: 5150
diff changeset
   756
		os.remove(key_filename); -- This file, if it exists is unlikely to have write permissions
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   757
		local key_size = tonumber(arg[2] or show_prompt("Choose key size (2048):") or 2048);
5151
dfe6a70efaa2 prosodyctl: Set stricter umask while generating key (thanks darkrain)
Kim Alvefur <zash@zash.se>
parents: 5150
diff changeset
   758
		local old_umask = pposix.umask("0377");
4824
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   759
		if openssl.genrsa{out=key_filename, key_size} then
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   760
			os.execute(("chmod 400 '%s'"):format(key_filename));
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   761
			show_message("Key written to ".. key_filename);
5151
dfe6a70efaa2 prosodyctl: Set stricter umask while generating key (thanks darkrain)
Kim Alvefur <zash@zash.se>
parents: 5150
diff changeset
   762
			pposix.umask(old_umask);
4824
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   763
			return nil, key_filename;
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   764
		end
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   765
		show_message("There was a problem, see OpenSSL output");
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   766
	else
4872
b2059452fb55 prosodyctl: Improve help messages for cert commands
Kim Alvefur <zash@zash.se>
parents: 4827
diff changeset
   767
		show_usage("cert key HOSTNAME <bits>", "Generates a RSA key named HOSTNAME.key\n "
4935
bc62abceef07 prosodyctl: Fix typo
Kim Alvefur <zash@zash.se>
parents: 4881
diff changeset
   768
		.."Prompts for a key size if none given")
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   769
	end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   770
end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   771
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   772
function cert_commands.request(arg)
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   773
	if #arg >= 1 and arg[1] ~= "--help" then
8103
1773559b03a8 prosodyctl cert: If running as root, write certificate files to config directory (fixes #530)
Kim Alvefur <zash@zash.se>
parents: 8102
diff changeset
   774
		local req_filename = cert_basedir .. "/" .. arg[1] .. ".req";
6840
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   775
		if use_existing(req_filename) then
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   776
			return nil, req_filename;
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   777
		end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   778
		local _, key_filename = cert_commands.key({arg[1]});
4872
b2059452fb55 prosodyctl: Improve help messages for cert commands
Kim Alvefur <zash@zash.se>
parents: 4827
diff changeset
   779
		local _, conf_filename = cert_commands.config(arg);
6406
61801dacc90b prosodyctl: Use sha256 for certificate requests and self-signed certificates
Kim Alvefur <zash@zash.se>
parents: 6367
diff changeset
   780
		if openssl.req{new=true, key=key_filename, utf8=true, sha256=true, config=conf_filename, out=req_filename} then
4824
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   781
			show_message("Certificate request written to ".. req_filename);
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   782
		else
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   783
			show_message("There was a problem, see OpenSSL output");
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   784
		end
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   785
	else
4872
b2059452fb55 prosodyctl: Improve help messages for cert commands
Kim Alvefur <zash@zash.se>
parents: 4827
diff changeset
   786
		show_usage("cert request HOSTNAME [HOSTNAME+]", "Generates a certificate request for the supplied hostname(s)")
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   787
	end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   788
end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   789
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   790
function cert_commands.generate(arg)
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   791
	if #arg >= 1 and arg[1] ~= "--help" then
8103
1773559b03a8 prosodyctl cert: If running as root, write certificate files to config directory (fixes #530)
Kim Alvefur <zash@zash.se>
parents: 8102
diff changeset
   792
		local cert_filename = cert_basedir .. "/" .. arg[1] .. ".crt";
6840
13b44929ae49 prosodyctl: Move files out of the way when generating new cert or key
Kim Alvefur <zash@zash.se>
parents: 6787
diff changeset
   793
		if use_existing(cert_filename) then
5152
fee5f8d4ec74 prosodyctl: Fix copypaste error
Kim Alvefur <zash@zash.se>
parents: 5151
diff changeset
   794
			return nil, cert_filename;
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   795
		end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   796
		local _, key_filename = cert_commands.key({arg[1]});
4872
b2059452fb55 prosodyctl: Improve help messages for cert commands
Kim Alvefur <zash@zash.se>
parents: 4827
diff changeset
   797
		local _, conf_filename = cert_commands.config(arg);
4824
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   798
		if key_filename and conf_filename and cert_filename
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   799
			and openssl.req{new=true, x509=true, nodes=true, key=key_filename,
6406
61801dacc90b prosodyctl: Use sha256 for certificate requests and self-signed certificates
Kim Alvefur <zash@zash.se>
parents: 6367
diff changeset
   800
				days=365, sha256=true, utf8=true, config=conf_filename, out=cert_filename} then
4824
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   801
			show_message("Certificate written to ".. cert_filename);
7068
4fc3c008976f prosodyctl: Show an example ssl config after generating a certificate
Kim Alvefur <zash@zash.se>
parents: 6963
diff changeset
   802
			print();
4824
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   803
		else
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   804
			show_message("There was a problem, see OpenSSL output");
73e261ed00a9 prosodyctl: Use util.openssl in certificate helpers. Improve feedback
Kim Alvefur <zash@zash.se>
parents: 4815
diff changeset
   805
		end
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   806
	else
4872
b2059452fb55 prosodyctl: Improve help messages for cert commands
Kim Alvefur <zash@zash.se>
parents: 4827
diff changeset
   807
		show_usage("cert generate HOSTNAME [HOSTNAME+]", "Generates a self-signed certificate for the current hostname(s)")
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   808
	end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   809
end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   810
8114
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   811
local function sh_esc(s)
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   812
	return "'" .. s:gsub("'", "'\\''") .. "'";
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   813
end
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   814
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   815
local function copy(from, to, umask, owner, group)
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   816
	local old_umask = umask and pposix.umask(umask);
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   817
	local attrs = lfs.attributes(to);
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   818
	if attrs then -- Move old file out of the way
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   819
		local backup = to..".bkp~"..os.date("%FT%T", attrs.change);
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   820
		os.rename(to, backup);
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   821
	end
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   822
	-- FIXME friendlier error handling, maybe move above backup back?
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   823
	local input = assert(io.open(from));
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   824
	local output = assert(io.open(to, "w"));
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   825
	local data = input:read(2^11);
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   826
	while data and output:write(data) do
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   827
		data = input:read(2^11);
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   828
	end
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   829
	assert(input:close());
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   830
	assert(output:close());
9547
cbd3b9d4c60b prosodyctl: Change ownership of certs to same as the cert base dir when running from a source checkout
Kim Alvefur <zash@zash.se>
parents: 9546
diff changeset
   831
	if not prosody.installed then
cbd3b9d4c60b prosodyctl: Change ownership of certs to same as the cert base dir when running from a source checkout
Kim Alvefur <zash@zash.se>
parents: 9546
diff changeset
   832
		-- FIXME this is possibly specific to GNU chown
cbd3b9d4c60b prosodyctl: Change ownership of certs to same as the cert base dir when running from a source checkout
Kim Alvefur <zash@zash.se>
parents: 9546
diff changeset
   833
		os.execute(("chown -c --reference=%s %s"):format(sh_esc(cert_basedir), sh_esc(to)));
cbd3b9d4c60b prosodyctl: Change ownership of certs to same as the cert base dir when running from a source checkout
Kim Alvefur <zash@zash.se>
parents: 9546
diff changeset
   834
	elseif owner and group then
8563
489998717387 prosodyctl: Use correct separator in chown call (fixes #1093)
Kim Alvefur <zash@zash.se>
parents: 8422
diff changeset
   835
		local ok = os.execute(("chown %s:%s %s"):format(sh_esc(owner), sh_esc(group), sh_esc(to)));
8114
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   836
		assert(ok == true or ok == 0, "Failed to change ownership of "..to);
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   837
	end
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   838
	if old_umask then pposix.umask(old_umask); end
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   839
	return true;
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   840
end
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   841
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   842
function cert_commands.import(arg)
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   843
	local hostnames = {};
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   844
	-- Move hostname arguments out of arg, the rest should be a list of paths
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   845
	while arg[1] and prosody.hosts[ arg[1] ] do
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   846
		table.insert(hostnames, table.remove(arg, 1));
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   847
	end
8250
2c386ee5cd5c prosodyctl: Use all enabled hosts if no hostnames passed to cert import
Kim Alvefur <zash@zash.se>
parents: 8205
diff changeset
   848
	if hostnames[1] == nil then
8252
cc664a3917e2 prosodyctl: cert import: Use env variable set by certbot if invoked as post-renew hook
Kim Alvefur <zash@zash.se>
parents: 8251
diff changeset
   849
		local domains = os.getenv"RENEWED_DOMAINS"; -- Set if invoked via certbot
cc664a3917e2 prosodyctl: cert import: Use env variable set by certbot if invoked as post-renew hook
Kim Alvefur <zash@zash.se>
parents: 8251
diff changeset
   850
		if domains then
cc664a3917e2 prosodyctl: cert import: Use env variable set by certbot if invoked as post-renew hook
Kim Alvefur <zash@zash.se>
parents: 8251
diff changeset
   851
			for host in domains:gmatch("%S+") do
8250
2c386ee5cd5c prosodyctl: Use all enabled hosts if no hostnames passed to cert import
Kim Alvefur <zash@zash.se>
parents: 8205
diff changeset
   852
				table.insert(hostnames, host);
2c386ee5cd5c prosodyctl: Use all enabled hosts if no hostnames passed to cert import
Kim Alvefur <zash@zash.se>
parents: 8205
diff changeset
   853
			end
8252
cc664a3917e2 prosodyctl: cert import: Use env variable set by certbot if invoked as post-renew hook
Kim Alvefur <zash@zash.se>
parents: 8251
diff changeset
   854
		else
cc664a3917e2 prosodyctl: cert import: Use env variable set by certbot if invoked as post-renew hook
Kim Alvefur <zash@zash.se>
parents: 8251
diff changeset
   855
			for host in pairs(prosody.hosts) do
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   856
				if host ~= "*" and configmanager.get(host, "enabled") ~= false then
8252
cc664a3917e2 prosodyctl: cert import: Use env variable set by certbot if invoked as post-renew hook
Kim Alvefur <zash@zash.se>
parents: 8251
diff changeset
   857
					table.insert(hostnames, host);
cc664a3917e2 prosodyctl: cert import: Use env variable set by certbot if invoked as post-renew hook
Kim Alvefur <zash@zash.se>
parents: 8251
diff changeset
   858
				end
cc664a3917e2 prosodyctl: cert import: Use env variable set by certbot if invoked as post-renew hook
Kim Alvefur <zash@zash.se>
parents: 8251
diff changeset
   859
			end
8250
2c386ee5cd5c prosodyctl: Use all enabled hosts if no hostnames passed to cert import
Kim Alvefur <zash@zash.se>
parents: 8205
diff changeset
   860
		end
2c386ee5cd5c prosodyctl: Use all enabled hosts if no hostnames passed to cert import
Kim Alvefur <zash@zash.se>
parents: 8205
diff changeset
   861
	end
8114
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   862
	if not arg[1] or arg[1] == "--help" then -- Probably forgot the path
8250
2c386ee5cd5c prosodyctl: Use all enabled hosts if no hostnames passed to cert import
Kim Alvefur <zash@zash.se>
parents: 8205
diff changeset
   863
		show_usage("cert import [HOSTNAME+] /path/to/certs [/other/paths/]+",
8114
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   864
			"Copies certificates to "..cert_basedir);
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   865
		return 1;
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   866
	end
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   867
	local owner, group;
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   868
	if pposix.getuid() == 0 then -- We need root to change ownership
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   869
		owner = configmanager.get("*", "prosody_user") or "prosody";
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   870
		group = configmanager.get("*", "prosody_group") or owner;
8114
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   871
	end
8277
3798955049e3 prosodyctl: cert import: Reuse function from certmanager for locating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 8268
diff changeset
   872
	local cm = require "core.certmanager";
8120
66d8f6b3c3ef prosodyctl: Delay reporting of successful certificate imports until all done
Kim Alvefur <zash@zash.se>
parents: 8114
diff changeset
   873
	local imported = {};
8114
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   874
	for _, host in ipairs(hostnames) do
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   875
		for _, dir in ipairs(arg) do
8277
3798955049e3 prosodyctl: cert import: Reuse function from certmanager for locating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 8268
diff changeset
   876
			local paths = cm.find_cert(dir, host);
3798955049e3 prosodyctl: cert import: Reuse function from certmanager for locating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 8268
diff changeset
   877
			if paths then
3798955049e3 prosodyctl: cert import: Reuse function from certmanager for locating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 8268
diff changeset
   878
				copy(paths.certificate, cert_basedir .. "/" .. host .. ".crt", nil, owner, group);
3798955049e3 prosodyctl: cert import: Reuse function from certmanager for locating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 8268
diff changeset
   879
				copy(paths.key, cert_basedir .. "/" .. host .. ".key", "0377", owner, group);
8120
66d8f6b3c3ef prosodyctl: Delay reporting of successful certificate imports until all done
Kim Alvefur <zash@zash.se>
parents: 8114
diff changeset
   880
				table.insert(imported, host);
8114
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   881
			else
8122
94a1fdaf12d1 prosodyctl: Make note about reporting where certificates are searched for
Kim Alvefur <zash@zash.se>
parents: 8121
diff changeset
   882
				-- TODO Say where we looked
8114
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   883
				show_warning("No certificate for host "..host.." found :(");
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   884
			end
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   885
			-- TODO Additional checks
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   886
			-- Certificate names matches the hostname
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   887
			-- Private key matches public key in certificate
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   888
		end
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   889
	end
8120
66d8f6b3c3ef prosodyctl: Delay reporting of successful certificate imports until all done
Kim Alvefur <zash@zash.se>
parents: 8114
diff changeset
   890
	if imported[1] then
66d8f6b3c3ef prosodyctl: Delay reporting of successful certificate imports until all done
Kim Alvefur <zash@zash.se>
parents: 8114
diff changeset
   891
		show_message("Imported certificate and key for hosts "..table.concat(imported, ", "));
8123
3c5f0cb4381a prosodyctl: Attempt to reload prosody after importing certificates
Kim Alvefur <zash@zash.se>
parents: 8122
diff changeset
   892
		local ok, err = prosodyctl.reload();
3c5f0cb4381a prosodyctl: Attempt to reload prosody after importing certificates
Kim Alvefur <zash@zash.se>
parents: 8122
diff changeset
   893
		if not ok and err ~= "not-running" then
3c5f0cb4381a prosodyctl: Attempt to reload prosody after importing certificates
Kim Alvefur <zash@zash.se>
parents: 8122
diff changeset
   894
			show_message(error_messages[err]);
3c5f0cb4381a prosodyctl: Attempt to reload prosody after importing certificates
Kim Alvefur <zash@zash.se>
parents: 8122
diff changeset
   895
		end
8121
f8c52010bd37 prosodyctl: Return non-zero exit code from cert import if no certificates imported
Kim Alvefur <zash@zash.se>
parents: 8120
diff changeset
   896
	else
f8c52010bd37 prosodyctl: Return non-zero exit code from cert import if no certificates imported
Kim Alvefur <zash@zash.se>
parents: 8120
diff changeset
   897
		show_warning("No certificates imported :(");
f8c52010bd37 prosodyctl: Return non-zero exit code from cert import if no certificates imported
Kim Alvefur <zash@zash.se>
parents: 8120
diff changeset
   898
		return 1;
8120
66d8f6b3c3ef prosodyctl: Delay reporting of successful certificate imports until all done
Kim Alvefur <zash@zash.se>
parents: 8114
diff changeset
   899
	end
8114
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   900
end
3cbb311f8468 prosodyctl: cert import: Command to copy certificates into prosodys certificate directory (fixes #892)
Kim Alvefur <zash@zash.se>
parents: 8113
diff changeset
   901
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   902
function commands.cert(arg)
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   903
	if #arg >= 1 and arg[1] ~= "--help" then
5292
46fbb5f1ef0a prosodyctl: Load LFS and util.openssl when actually needed (fixes unhelpful warnings if no LuaSec installed)
Kim Alvefur <zash@zash.se>
parents: 5152
diff changeset
   904
		openssl = require "util.openssl";
46fbb5f1ef0a prosodyctl: Load LFS and util.openssl when actually needed (fixes unhelpful warnings if no LuaSec installed)
Kim Alvefur <zash@zash.se>
parents: 5152
diff changeset
   905
		lfs = require "lfs";
8110
83d776b344ad prosodyctl: Verify permissions on directory that certificates are written to
Kim Alvefur <zash@zash.se>
parents: 8109
diff changeset
   906
		local cert_dir_attrs = lfs.attributes(cert_basedir);
8111
939ccedb509d prosodyctl: Verify that directory certs are written to exists
Kim Alvefur <zash@zash.se>
parents: 8110
diff changeset
   907
		if not cert_dir_attrs then
939ccedb509d prosodyctl: Verify that directory certs are written to exists
Kim Alvefur <zash@zash.se>
parents: 8110
diff changeset
   908
			show_warning("The directory "..cert_basedir.." does not exist");
939ccedb509d prosodyctl: Verify that directory certs are written to exists
Kim Alvefur <zash@zash.se>
parents: 8110
diff changeset
   909
			return 1; -- TODO Should we create it?
939ccedb509d prosodyctl: Verify that directory certs are written to exists
Kim Alvefur <zash@zash.se>
parents: 8110
diff changeset
   910
		end
9546
92bfb12684b0 prosodyctl: Allow cert dir to not be owned by root (fixes #1075)
Kim Alvefur <zash@zash.se>
parents: 8950
diff changeset
   911
		local uid = pposix.getuid();
92bfb12684b0 prosodyctl: Allow cert dir to not be owned by root (fixes #1075)
Kim Alvefur <zash@zash.se>
parents: 8950
diff changeset
   912
		if uid ~= 0 and uid ~= cert_dir_attrs.uid then
8110
83d776b344ad prosodyctl: Verify permissions on directory that certificates are written to
Kim Alvefur <zash@zash.se>
parents: 8109
diff changeset
   913
			show_warning("The directory "..cert_basedir.." is not owned by the current user, won't be able to write files to it");
83d776b344ad prosodyctl: Verify permissions on directory that certificates are written to
Kim Alvefur <zash@zash.se>
parents: 8109
diff changeset
   914
			return 1;
8422
57610304e30d prosodyctl: Fix traceback with lfs < 1.6.2 and show warning
Matthew Wild <mwild1@gmail.com>
parents: 8280
diff changeset
   915
		elseif not cert_dir_attrs.permissions then -- COMPAT with LuaFilesystem < 1.6.2 (hey CentOS!)
57610304e30d prosodyctl: Fix traceback with lfs < 1.6.2 and show warning
Matthew Wild <mwild1@gmail.com>
parents: 8280
diff changeset
   916
			show_message("Unable to check permissions on "..cert_basedir.." (LuaFilesystem 1.6.2+ required)");
57610304e30d prosodyctl: Fix traceback with lfs < 1.6.2 and show warning
Matthew Wild <mwild1@gmail.com>
parents: 8280
diff changeset
   917
			show_message("Please confirm that Prosody (and only Prosody) can write to this directory)");
8110
83d776b344ad prosodyctl: Verify permissions on directory that certificates are written to
Kim Alvefur <zash@zash.se>
parents: 8109
diff changeset
   918
		elseif cert_dir_attrs.permissions:match("^%.w..%-..%-.$") then
83d776b344ad prosodyctl: Verify permissions on directory that certificates are written to
Kim Alvefur <zash@zash.se>
parents: 8109
diff changeset
   919
			show_warning("The directory "..cert_basedir.." not only writable by its owner");
83d776b344ad prosodyctl: Verify permissions on directory that certificates are written to
Kim Alvefur <zash@zash.se>
parents: 8109
diff changeset
   920
			return 1;
83d776b344ad prosodyctl: Verify permissions on directory that certificates are written to
Kim Alvefur <zash@zash.se>
parents: 8109
diff changeset
   921
		end
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   922
		local subcmd = table.remove(arg, 1);
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   923
		if type(cert_commands[subcmd]) == "function" then
8251
259e0010a6d4 prosodyctl: Only demand a hostname argument to cert commands other than import
Kim Alvefur <zash@zash.se>
parents: 8250
diff changeset
   924
			if subcmd ~= "import" then -- hostnames are optional for import
259e0010a6d4 prosodyctl: Only demand a hostname argument to cert commands other than import
Kim Alvefur <zash@zash.se>
parents: 8250
diff changeset
   925
				if not arg[1] then
259e0010a6d4 prosodyctl: Only demand a hostname argument to cert commands other than import
Kim Alvefur <zash@zash.se>
parents: 8250
diff changeset
   926
					show_message"You need to supply at least one hostname"
259e0010a6d4 prosodyctl: Only demand a hostname argument to cert commands other than import
Kim Alvefur <zash@zash.se>
parents: 8250
diff changeset
   927
					arg = { "--help" };
259e0010a6d4 prosodyctl: Only demand a hostname argument to cert commands other than import
Kim Alvefur <zash@zash.se>
parents: 8250
diff changeset
   928
				end
8721
c23cdeac5b61 prosodyctl: Use prosody.hosts instead of _G.hosts
Kim Alvefur <zash@zash.se>
parents: 8704
diff changeset
   929
				if arg[1] ~= "--help" and not prosody.hosts[arg[1]] then
8251
259e0010a6d4 prosodyctl: Only demand a hostname argument to cert commands other than import
Kim Alvefur <zash@zash.se>
parents: 8250
diff changeset
   930
					show_message(error_messages["no-such-host"]);
259e0010a6d4 prosodyctl: Only demand a hostname argument to cert commands other than import
Kim Alvefur <zash@zash.se>
parents: 8250
diff changeset
   931
					return 1;
259e0010a6d4 prosodyctl: Only demand a hostname argument to cert commands other than import
Kim Alvefur <zash@zash.se>
parents: 8250
diff changeset
   932
				end
4827
fefbfd76d2d3 prosodyctl: Show an error if the user doesn't supply a hostname to the certificate commands
Kim Alvefur <zash@zash.se>
parents: 4826
diff changeset
   933
			end
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   934
			return cert_commands[subcmd](arg);
8180
f52cdad171b0 prosodyctl: Make `cert check` do the same as `check certs`
Kim Alvefur <zash@zash.se>
parents: 8147
diff changeset
   935
		elseif subcmd == "check" then
f52cdad171b0 prosodyctl: Make `cert check` do the same as `check certs`
Kim Alvefur <zash@zash.se>
parents: 8147
diff changeset
   936
			return commands.check({"certs"});
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   937
		end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   938
	end
8125
e56a90168890 prosodyctl: Add cert import to short help
Kim Alvefur <zash@zash.se>
parents: 8123
diff changeset
   939
	show_usage("cert config|request|generate|key|import", "Helpers for generating X.509 certificates and keys.")
8147
8e9a36d0c7d1 prosodyctl: Show description of each certificate subcommand
Kim Alvefur <zash@zash.se>
parents: 8125
diff changeset
   940
	for _, cmd in pairs(cert_commands) do
8e9a36d0c7d1 prosodyctl: Show description of each certificate subcommand
Kim Alvefur <zash@zash.se>
parents: 8125
diff changeset
   941
		print()
8e9a36d0c7d1 prosodyctl: Show description of each certificate subcommand
Kim Alvefur <zash@zash.se>
parents: 8125
diff changeset
   942
		cmd{ "--help" }
8e9a36d0c7d1 prosodyctl: Show description of each certificate subcommand
Kim Alvefur <zash@zash.se>
parents: 8125
diff changeset
   943
	end
4487
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   944
end
5f466a50e78b prosodyctl: Add commands for generating certificates and keys
Kim Alvefur <zash@zash.se>
parents: 4476
diff changeset
   945
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   946
function commands.check(arg)
5655
6d7f7548b2c9 prosodyctl: Add 'prosodyctl check --help'
Kim Alvefur <zash@zash.se>
parents: 5620
diff changeset
   947
	if arg[1] == "--help" then
6d7f7548b2c9 prosodyctl: Add 'prosodyctl check --help'
Kim Alvefur <zash@zash.se>
parents: 5620
diff changeset
   948
		show_usage([[check]], [[Perform basic checks on your Prosody installation]]);
6d7f7548b2c9 prosodyctl: Add 'prosodyctl check --help'
Kim Alvefur <zash@zash.se>
parents: 5620
diff changeset
   949
		return 1;
6d7f7548b2c9 prosodyctl: Add 'prosodyctl check --help'
Kim Alvefur <zash@zash.se>
parents: 5620
diff changeset
   950
	end
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   951
	local what = table.remove(arg, 1);
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   952
	local set = require "util.set";
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   953
	local it = require "util.iterators";
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   954
	local ok = true;
6158
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
   955
	local function disabled_hosts(host, conf) return host ~= "*" and conf.enabled ~= false; end
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   956
	local function enabled_hosts() return it.filter(disabled_hosts, pairs(configmanager.getconfig())); end
8950
f12cc1d7aa65 prosodyctl: Warn if attempting to run an unknown check (fixes #1161)
Kim Alvefur <zash@zash.se>
parents: 8884
diff changeset
   957
	if not (what == nil or what == "disabled" or what == "config" or what == "dns" or what == "certs") then
f12cc1d7aa65 prosodyctl: Warn if attempting to run an unknown check (fixes #1161)
Kim Alvefur <zash@zash.se>
parents: 8884
diff changeset
   958
		show_warning("Don't know how to check '%s'. Try one of 'config', 'dns', 'certs' or 'disabled'.", what);
f12cc1d7aa65 prosodyctl: Warn if attempting to run an unknown check (fixes #1161)
Kim Alvefur <zash@zash.se>
parents: 8884
diff changeset
   959
		return 1;
f12cc1d7aa65 prosodyctl: Warn if attempting to run an unknown check (fixes #1161)
Kim Alvefur <zash@zash.se>
parents: 8884
diff changeset
   960
	end
6159
4ee14b7ef2cc prosodyctl: Add check that points out any disabled hosts
Kim Alvefur <zash@zash.se>
parents: 6158
diff changeset
   961
	if not what or what == "disabled" then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   962
		local disabled_hosts_set = set.new();
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   963
		for host, host_options in it.filter("*", pairs(configmanager.getconfig())) do
6159
4ee14b7ef2cc prosodyctl: Add check that points out any disabled hosts
Kim Alvefur <zash@zash.se>
parents: 6158
diff changeset
   964
			if host_options.enabled == false then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   965
				disabled_hosts_set:add(host);
6159
4ee14b7ef2cc prosodyctl: Add check that points out any disabled hosts
Kim Alvefur <zash@zash.se>
parents: 6158
diff changeset
   966
			end
4ee14b7ef2cc prosodyctl: Add check that points out any disabled hosts
Kim Alvefur <zash@zash.se>
parents: 6158
diff changeset
   967
		end
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   968
		if not disabled_hosts_set:empty() then
6159
4ee14b7ef2cc prosodyctl: Add check that points out any disabled hosts
Kim Alvefur <zash@zash.se>
parents: 6158
diff changeset
   969
			local msg = "Checks will be skipped for these disabled hosts: %s";
4ee14b7ef2cc prosodyctl: Add check that points out any disabled hosts
Kim Alvefur <zash@zash.se>
parents: 6158
diff changeset
   970
			if what then msg = "These hosts are disabled: %s"; end
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   971
			show_warning(msg, tostring(disabled_hosts_set));
6159
4ee14b7ef2cc prosodyctl: Add check that points out any disabled hosts
Kim Alvefur <zash@zash.se>
parents: 6158
diff changeset
   972
			if what then return 0; end
4ee14b7ef2cc prosodyctl: Add check that points out any disabled hosts
Kim Alvefur <zash@zash.se>
parents: 6158
diff changeset
   973
			print""
4ee14b7ef2cc prosodyctl: Add check that points out any disabled hosts
Kim Alvefur <zash@zash.se>
parents: 6158
diff changeset
   974
		end
4ee14b7ef2cc prosodyctl: Add check that points out any disabled hosts
Kim Alvefur <zash@zash.se>
parents: 6158
diff changeset
   975
	end
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   976
	if not what or what == "config" then
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   977
		print("Checking config...");
6160
cf0f7caa885e prosodyctl: Check for deprecated config options
Kim Alvefur <zash@zash.se>
parents: 6159
diff changeset
   978
		local deprecated = set.new({
6489
64146196b142 prosodyctl: Add 'require_encryption' to list of deprecated options pointed out by the check command
Kim Alvefur <zash@zash.se>
parents: 6406
diff changeset
   979
			"bosh_ports", "disallow_s2s", "no_daemonize", "anonymous_login", "require_encryption",
9799
02735bc82126 mod_websocket: Drop CORS code in favor of that in mod_http
Kim Alvefur <zash@zash.se>
parents: 9798
diff changeset
   980
			"vcard_compatibility", "cross_domain_bosh", "cross_domain_websocket"
6160
cf0f7caa885e prosodyctl: Check for deprecated config options
Kim Alvefur <zash@zash.se>
parents: 6159
diff changeset
   981
		});
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   982
		local known_global_options = set.new({
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   983
			"pidfile", "log", "plugin_paths", "prosody_user", "prosody_group", "daemonize",
6754
18f18bceb662 prosodyctl: check: Add some more known global config options
Kim Alvefur <zash@zash.se>
parents: 6714
diff changeset
   984
			"umask", "prosodyctl_timeout", "use_ipv6", "use_libevent", "network_settings",
18f18bceb662 prosodyctl: check: Add some more known global config options
Kim Alvefur <zash@zash.se>
parents: 6714
diff changeset
   985
			"network_backend", "http_default_host",
9713
f31ed70c993a prosodyctl check: Add statisticsmanager settings to known global options
Kim Alvefur <zash@zash.se>
parents: 9548
diff changeset
   986
			"statistics_interval", "statistics", "statistics_config",
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   987
		});
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
   988
		local config = configmanager.getconfig();
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   989
		-- Check that we have any global options (caused by putting a host at the top)
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   990
		if it.count(it.filter("log", pairs(config["*"]))) == 0 then
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   991
			ok = false;
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   992
			print("");
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   993
			print("    No global options defined. Perhaps you have put a host definition at the top")
7362
a5a080c12c96 Update every link to the documentation to use HTTPS
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7316
diff changeset
   994
			print("    of the config file? They should be at the bottom, see https://prosody.im/doc/configure#overview");
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
   995
		end
6162
fbc3b195dab8 prosodyctl: Check that there is at least one enabled VirtualHost (or Component) defined
Kim Alvefur <zash@zash.se>
parents: 6161
diff changeset
   996
		if it.count(enabled_hosts()) == 0 then
fbc3b195dab8 prosodyctl: Check that there is at least one enabled VirtualHost (or Component) defined
Kim Alvefur <zash@zash.se>
parents: 6161
diff changeset
   997
			ok = false;
fbc3b195dab8 prosodyctl: Check that there is at least one enabled VirtualHost (or Component) defined
Kim Alvefur <zash@zash.se>
parents: 6161
diff changeset
   998
			print("");
fbc3b195dab8 prosodyctl: Check that there is at least one enabled VirtualHost (or Component) defined
Kim Alvefur <zash@zash.se>
parents: 6161
diff changeset
   999
			if it.count(it.filter("*", pairs(config))) == 0 then
fbc3b195dab8 prosodyctl: Check that there is at least one enabled VirtualHost (or Component) defined
Kim Alvefur <zash@zash.se>
parents: 6161
diff changeset
  1000
				print("    No hosts are defined, please add at least one VirtualHost section")
fbc3b195dab8 prosodyctl: Check that there is at least one enabled VirtualHost (or Component) defined
Kim Alvefur <zash@zash.se>
parents: 6161
diff changeset
  1001
			elseif config["*"]["enabled"] == false then
fbc3b195dab8 prosodyctl: Check that there is at least one enabled VirtualHost (or Component) defined
Kim Alvefur <zash@zash.se>
parents: 6161
diff changeset
  1002
				print("    No hosts are enabled. Remove enabled = false from the global section or put enabled = true under at least one VirtualHost section")
fbc3b195dab8 prosodyctl: Check that there is at least one enabled VirtualHost (or Component) defined
Kim Alvefur <zash@zash.se>
parents: 6161
diff changeset
  1003
			else
fbc3b195dab8 prosodyctl: Check that there is at least one enabled VirtualHost (or Component) defined
Kim Alvefur <zash@zash.se>
parents: 6161
diff changeset
  1004
				print("    All hosts are disabled. Remove enabled = false from at least one VirtualHost section")
fbc3b195dab8 prosodyctl: Check that there is at least one enabled VirtualHost (or Component) defined
Kim Alvefur <zash@zash.se>
parents: 6161
diff changeset
  1005
			end
fbc3b195dab8 prosodyctl: Check that there is at least one enabled VirtualHost (or Component) defined
Kim Alvefur <zash@zash.se>
parents: 6161
diff changeset
  1006
		end
7085
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1007
		if not config["*"].modules_enabled then
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1008
			print("    No global modules_enabled is set?");
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1009
			local suggested_global_modules;
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1010
			for host, options in enabled_hosts() do --luacheck: ignore 213/host
7085
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1011
				if not options.component_module and options.modules_enabled then
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1012
					suggested_global_modules = set.intersection(suggested_global_modules or set.new(options.modules_enabled), set.new(options.modules_enabled));
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1013
				end
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1014
			end
8268
17ebd8ac8545 prosodyctl check: Fix traceback when no modules_enabled are defined (e.g., a completely empty config)
Waqas Hussain <waqas20@gmail.com>
parents: 8252
diff changeset
  1015
			if suggested_global_modules and not suggested_global_modules:empty() then
7085
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1016
				print("    Consider moving these modules into modules_enabled in the global section:")
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1017
				print("    "..tostring(suggested_global_modules / function (x) return ("%q"):format(x) end));
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1018
			end
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1019
			print();
8b590fc77d91 prosodyctl: check config: Suggest moving modules enabled on all hosts to a global_modules enabled if that is unset
Kim Alvefur <zash@zash.se>
parents: 7083
diff changeset
  1020
		end
8790
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1021
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1022
		do -- Check for modules enabled both normally and as components
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1023
			local modules = set.new(config["*"]["modules_enabled"]);
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1024
			for host, options in enabled_hosts() do
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1025
				local component_module = options.component_module;
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1026
				if component_module and modules:contains(component_module) then
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1027
					print(("    mod_%s is enabled both in modules_enabled and as Component %q %q"):format(component_module, host, component_module));
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1028
					print("    This means the service is enabled on all VirtualHosts as well as the Component.");
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1029
					print("    Are you sure this what you want? It may cause unexpected behaviour.");
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1030
				end
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1031
			end
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1032
		end
5dbebb7627ef prosodyctl: Config check for modules that are also components (fixes #1138)
Kim Alvefur <zash@zash.se>
parents: 8721
diff changeset
  1033
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1034
		-- Check for global options under hosts
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1035
		local global_options = set.new(it.to_array(it.keys(config["*"])));
6160
cf0f7caa885e prosodyctl: Check for deprecated config options
Kim Alvefur <zash@zash.se>
parents: 6159
diff changeset
  1036
		local deprecated_global_options = set.intersection(global_options, deprecated);
cf0f7caa885e prosodyctl: Check for deprecated config options
Kim Alvefur <zash@zash.se>
parents: 6159
diff changeset
  1037
		if not deprecated_global_options:empty() then
cf0f7caa885e prosodyctl: Check for deprecated config options
Kim Alvefur <zash@zash.se>
parents: 6159
diff changeset
  1038
			print("");
cf0f7caa885e prosodyctl: Check for deprecated config options
Kim Alvefur <zash@zash.se>
parents: 6159
diff changeset
  1039
			print("    You have some deprecated options in the global section:");
cf0f7caa885e prosodyctl: Check for deprecated config options
Kim Alvefur <zash@zash.se>
parents: 6159
diff changeset
  1040
			print("    "..tostring(deprecated_global_options))
cf0f7caa885e prosodyctl: Check for deprecated config options
Kim Alvefur <zash@zash.se>
parents: 6159
diff changeset
  1041
			ok = false;
cf0f7caa885e prosodyctl: Check for deprecated config options
Kim Alvefur <zash@zash.se>
parents: 6159
diff changeset
  1042
		end
9234
3d12b4f41b23 prosodyctl: Also look for options that belong in global section under disabled hosts (fixes #1207)
Kim Alvefur <zash@zash.se>
parents: 8951
diff changeset
  1043
		for host, options in it.filter(function (h) return h ~= "*" end, pairs(configmanager.getconfig())) do
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1044
			local host_options = set.new(it.to_array(it.keys(options)));
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1045
			local misplaced_options = set.intersection(host_options, known_global_options);
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1046
			for name in pairs(options) do
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1047
				if name:match("^interfaces?")
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1048
				or name:match("_ports?$") or name:match("_interfaces?$")
6917
5df76208e050 prosodyctl: check: Don't complain about c2s_ssl or s2s_ssl being in VirtualHost sections, that's supported
Kim Alvefur <zash@zash.se>
parents: 6848
diff changeset
  1049
				or (name:match("_ssl$") and not name:match("^[cs]2s_ssl$")) then
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1050
					misplaced_options:add(name);
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1051
				end
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1052
			end
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1053
			if not misplaced_options:empty() then
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1054
				ok = false;
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1055
				print("");
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1056
				local n = it.count(misplaced_options);
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1057
				print("    You have "..n.." option"..(n>1 and "s " or " ").."set under "..host.." that should be");
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1058
				print("    in the global section of the config file, above any VirtualHost or Component definitions,")
7362
a5a080c12c96 Update every link to the documentation to use HTTPS
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7316
diff changeset
  1059
				print("    see https://prosody.im/doc/configure#overview for more information.")
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1060
				print("");
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1061
				print("    You need to move the following option"..(n>1 and "s" or "")..": "..table.concat(it.to_array(misplaced_options), ", "));
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1062
			end
9234
3d12b4f41b23 prosodyctl: Also look for options that belong in global section under disabled hosts (fixes #1207)
Kim Alvefur <zash@zash.se>
parents: 8951
diff changeset
  1063
		end
3d12b4f41b23 prosodyctl: Also look for options that belong in global section under disabled hosts (fixes #1207)
Kim Alvefur <zash@zash.se>
parents: 8951
diff changeset
  1064
		for host, options in enabled_hosts() do
3d12b4f41b23 prosodyctl: Also look for options that belong in global section under disabled hosts (fixes #1207)
Kim Alvefur <zash@zash.se>
parents: 8951
diff changeset
  1065
			local host_options = set.new(it.to_array(it.keys(options)));
5616
a79c6717ee2b prosodyctl: check config: Show a suggestion to change hosts that begin with jabber/xmpp/chat/im subdomains, and link to DNS documentation
Matthew Wild <mwild1@gmail.com>
parents: 5610
diff changeset
  1066
			local subdomain = host:match("^[^.]+");
5619
6a87b75aedd5 prosodyctl: check config: Fix check for whether host is a component
Matthew Wild <mwild1@gmail.com>
parents: 5617
diff changeset
  1067
			if not(host_options:contains("component_module")) and (subdomain == "jabber" or subdomain == "xmpp"
5616
a79c6717ee2b prosodyctl: check config: Show a suggestion to change hosts that begin with jabber/xmpp/chat/im subdomains, and link to DNS documentation
Matthew Wild <mwild1@gmail.com>
parents: 5610
diff changeset
  1068
			   or subdomain == "chat" or subdomain == "im") then
8112
2f214c4db170 prosodyctl: Normalize inconsistent whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8111
diff changeset
  1069
				print("");
2f214c4db170 prosodyctl: Normalize inconsistent whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8111
diff changeset
  1070
				print("    Suggestion: If "..host.. " is a new host with no real users yet, consider renaming it now to");
2f214c4db170 prosodyctl: Normalize inconsistent whitespace [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8111
diff changeset
  1071
				print("     "..host:gsub("^[^.]+%.", "")..". You can use SRV records to redirect XMPP clients and servers to "..host..".");
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1072
				print("     For more information see: https://prosody.im/doc/dns");
5616
a79c6717ee2b prosodyctl: check config: Show a suggestion to change hosts that begin with jabber/xmpp/chat/im subdomains, and link to DNS documentation
Matthew Wild <mwild1@gmail.com>
parents: 5610
diff changeset
  1073
			end
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1074
		end
6962
aa8647a5105d prosodyctl check: Point out items in the modules_enabled list that incorrectly include the 'mod_' prefix
Kim Alvefur <zash@zash.se>
parents: 6961
diff changeset
  1075
		local all_modules = set.new(config["*"].modules_enabled);
6960
e442016918a9 prosodyctl check: Include global options when checking "all options"
Kim Alvefur <zash@zash.se>
parents: 6917
diff changeset
  1076
		local all_options = set.new(it.to_array(it.keys(config["*"])));
6847
8946052e12d2 prosodyctl check: Warn if encryption is required but LuaSec is unavailable
Kim Alvefur <zash@zash.se>
parents: 6840
diff changeset
  1077
		for host in enabled_hosts() do
8946052e12d2 prosodyctl check: Warn if encryption is required but LuaSec is unavailable
Kim Alvefur <zash@zash.se>
parents: 6840
diff changeset
  1078
			all_options:include(set.new(it.to_array(it.keys(config[host]))));
6962
aa8647a5105d prosodyctl check: Point out items in the modules_enabled list that incorrectly include the 'mod_' prefix
Kim Alvefur <zash@zash.se>
parents: 6961
diff changeset
  1079
			all_modules:include(set.new(config[host].modules_enabled));
aa8647a5105d prosodyctl check: Point out items in the modules_enabled list that incorrectly include the 'mod_' prefix
Kim Alvefur <zash@zash.se>
parents: 6961
diff changeset
  1080
		end
aa8647a5105d prosodyctl check: Point out items in the modules_enabled list that incorrectly include the 'mod_' prefix
Kim Alvefur <zash@zash.se>
parents: 6961
diff changeset
  1081
		for mod in all_modules do
aa8647a5105d prosodyctl check: Point out items in the modules_enabled list that incorrectly include the 'mod_' prefix
Kim Alvefur <zash@zash.se>
parents: 6961
diff changeset
  1082
			if mod:match("^mod_") then
aa8647a5105d prosodyctl check: Point out items in the modules_enabled list that incorrectly include the 'mod_' prefix
Kim Alvefur <zash@zash.se>
parents: 6961
diff changeset
  1083
				print("");
aa8647a5105d prosodyctl check: Point out items in the modules_enabled list that incorrectly include the 'mod_' prefix
Kim Alvefur <zash@zash.se>
parents: 6961
diff changeset
  1084
				print("    Modules in modules_enabled should not have the 'mod_' prefix included.");
aa8647a5105d prosodyctl check: Point out items in the modules_enabled list that incorrectly include the 'mod_' prefix
Kim Alvefur <zash@zash.se>
parents: 6961
diff changeset
  1085
				print("    Change '"..mod.."' to '"..mod:match("^mod_(.*)").."'.");
6963
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1086
			elseif mod:match("^auth_") then
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1087
				print("");
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1088
				print("    Authentication modules should not be added to modules_enabled,");
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1089
				print("    but be specified in the 'authentication' option.");
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1090
				print("    Remove '"..mod.."' from modules_enabled and instead add");
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1091
				print("        authentication = '"..mod:match("^auth_(.*)").."'");
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1092
				print("    For more information see https://prosody.im/doc/authentication");
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1093
			elseif mod:match("^storage_") then
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1094
				print("");
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1095
				print("    storage modules should not be added to modules_enabled,");
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1096
				print("    but be specified in the 'storage' option.");
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1097
				print("    Remove '"..mod.."' from modules_enabled and instead add");
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1098
				print("        storage = '"..mod:match("^storage_(.*)").."'");
50e2277ea05f prosodyctl check: Point out that authentication and storage modules should not be added to modules_enabled (fixes #570)
Kim Alvefur <zash@zash.se>
parents: 6962
diff changeset
  1099
				print("    For more information see https://prosody.im/doc/storage");
6962
aa8647a5105d prosodyctl check: Point out items in the modules_enabled list that incorrectly include the 'mod_' prefix
Kim Alvefur <zash@zash.se>
parents: 6961
diff changeset
  1100
			end
6847
8946052e12d2 prosodyctl check: Warn if encryption is required but LuaSec is unavailable
Kim Alvefur <zash@zash.se>
parents: 6840
diff changeset
  1101
		end
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1102
		for host, host_config in pairs(config) do --luacheck: ignore 213/host
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1103
			if type(rawget(host_config, "storage")) == "string" and rawget(host_config, "default_storage") then
8070
91550b6f93d2 prosodyctl: Point out how default_storage is redundant if storage is a string
Kim Alvefur <zash@zash.se>
parents: 8015
diff changeset
  1104
				print("");
91550b6f93d2 prosodyctl: Point out how default_storage is redundant if storage is a string
Kim Alvefur <zash@zash.se>
parents: 8015
diff changeset
  1105
				print("    The 'default_storage' option is not needed if 'storage' is set to a string.");
91550b6f93d2 prosodyctl: Point out how default_storage is redundant if storage is a string
Kim Alvefur <zash@zash.se>
parents: 8015
diff changeset
  1106
				break;
91550b6f93d2 prosodyctl: Point out how default_storage is redundant if storage is a string
Kim Alvefur <zash@zash.se>
parents: 8015
diff changeset
  1107
			end
91550b6f93d2 prosodyctl: Point out how default_storage is redundant if storage is a string
Kim Alvefur <zash@zash.se>
parents: 8015
diff changeset
  1108
		end
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1109
		local require_encryption = set.intersection(all_options, set.new({
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1110
			"require_encryption", "c2s_require_encryption", "s2s_require_encryption"
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1111
		})):empty();
6961
ebac87d80ef4 prosodyctl check: Really store imported LuaSec (must be a leftover from debugging)
Kim Alvefur <zash@zash.se>
parents: 6960
diff changeset
  1112
		local ssl = dependencies.softreq"ssl";
6847
8946052e12d2 prosodyctl check: Warn if encryption is required but LuaSec is unavailable
Kim Alvefur <zash@zash.se>
parents: 6840
diff changeset
  1113
		if not ssl then
7458
82d8c11ab0cb prosodyctl: Warn if encryption is required but mod_tls is not enabled (see #617)
Kim Alvefur <zash@zash.se>
parents: 7448
diff changeset
  1114
			if not require_encryption then
6847
8946052e12d2 prosodyctl check: Warn if encryption is required but LuaSec is unavailable
Kim Alvefur <zash@zash.se>
parents: 6840
diff changeset
  1115
				print("");
8946052e12d2 prosodyctl check: Warn if encryption is required but LuaSec is unavailable
Kim Alvefur <zash@zash.se>
parents: 6840
diff changeset
  1116
				print("    You require encryption but LuaSec is not available.");
8946052e12d2 prosodyctl check: Warn if encryption is required but LuaSec is unavailable
Kim Alvefur <zash@zash.se>
parents: 6840
diff changeset
  1117
				print("    Connections will fail.");
8946052e12d2 prosodyctl check: Warn if encryption is required but LuaSec is unavailable
Kim Alvefur <zash@zash.se>
parents: 6840
diff changeset
  1118
				ok = false;
8946052e12d2 prosodyctl check: Warn if encryption is required but LuaSec is unavailable
Kim Alvefur <zash@zash.se>
parents: 6840
diff changeset
  1119
			end
6848
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1120
		elseif not ssl.loadcertificate then
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1121
			if all_options:contains("s2s_secure_auth") then
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1122
				print("");
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1123
				print("    You have set s2s_secure_auth but your version of LuaSec does ");
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1124
				print("    not support certificate validation, so all s2s connections will");
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1125
				print("    fail.");
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1126
				ok = false;
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1127
			elseif all_options:contains("s2s_secure_domains") then
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1128
				local secure_domains = set.new();
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1129
				for host in enabled_hosts() do
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1130
					if config[host].s2s_secure_auth == true then
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1131
						secure_domains:add("*");
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1132
					else
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1133
						secure_domains:include(set.new(config[host].s2s_secure_domains));
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1134
					end
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1135
				end
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1136
				if not secure_domains:empty() then
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1137
					print("");
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1138
					print("    You have set s2s_secure_domains but your version of LuaSec does ");
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1139
					print("    not support certificate validation, so s2s connections to/from ");
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1140
					print("    these domains will fail.");
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1141
					ok = false;
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1142
				end
bb7854355df1 prosodyctl check: Warn if certificate checking is enforced but LuaSec is too old
Kim Alvefur <zash@zash.se>
parents: 6847
diff changeset
  1143
			end
7458
82d8c11ab0cb prosodyctl: Warn if encryption is required but mod_tls is not enabled (see #617)
Kim Alvefur <zash@zash.se>
parents: 7448
diff changeset
  1144
		elseif require_encryption and not all_modules:contains("tls") then
82d8c11ab0cb prosodyctl: Warn if encryption is required but mod_tls is not enabled (see #617)
Kim Alvefur <zash@zash.se>
parents: 7448
diff changeset
  1145
			print("");
82d8c11ab0cb prosodyctl: Warn if encryption is required but mod_tls is not enabled (see #617)
Kim Alvefur <zash@zash.se>
parents: 7448
diff changeset
  1146
			print("    You require encryption but mod_tls is not enabled.");
82d8c11ab0cb prosodyctl: Warn if encryption is required but mod_tls is not enabled (see #617)
Kim Alvefur <zash@zash.se>
parents: 7448
diff changeset
  1147
			print("    Connections will fail.");
82d8c11ab0cb prosodyctl: Warn if encryption is required but mod_tls is not enabled (see #617)
Kim Alvefur <zash@zash.se>
parents: 7448
diff changeset
  1148
			ok = false;
6847
8946052e12d2 prosodyctl check: Warn if encryption is required but LuaSec is unavailable
Kim Alvefur <zash@zash.se>
parents: 6840
diff changeset
  1149
		end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1150
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1151
		print("Done.\n");
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1152
	end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1153
	if not what or what == "dns" then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1154
		local dns = require "net.dns";
5809
be997c6a69be prosodyctl: check: Support for unicode (IDN) domains (thanks once again albert)
Matthew Wild <mwild1@gmail.com>
parents: 5723
diff changeset
  1155
		local idna = require "util.encodings".idna;
5592
5705e21ba24b prosodyctl: check dns: Use socket.local_addresses() if available
Matthew Wild <mwild1@gmail.com>
parents: 5591
diff changeset
  1156
		local ip = require "util.ip";
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1157
		local c2s_ports = set.new(configmanager.get("*", "c2s_ports") or {5222});
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1158
		local s2s_ports = set.new(configmanager.get("*", "s2s_ports") or {5269});
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1159
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1160
		local c2s_srv_required, s2s_srv_required;
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1161
		if not c2s_ports:contains(5222) then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1162
			c2s_srv_required = true;
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1163
		end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1164
		if not s2s_ports:contains(5269) then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1165
			s2s_srv_required = true;
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1166
		end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1167
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1168
		local problem_hosts = set.new();
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1169
5592
5705e21ba24b prosodyctl: check dns: Use socket.local_addresses() if available
Matthew Wild <mwild1@gmail.com>
parents: 5591
diff changeset
  1170
		local external_addresses, internal_addresses = set.new(), set.new();
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1171
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1172
		local fqdn = socket.dns.tohostname(socket.dns.gethostname());
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1173
		if fqdn then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1174
			do
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1175
				local res = dns.lookup(idna.to_ascii(fqdn), "A");
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1176
				if res then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1177
					for _, record in ipairs(res) do
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1178
						external_addresses:add(record.a);
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1179
					end
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1180
				end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1181
			end
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1182
			do
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1183
				local res = dns.lookup(idna.to_ascii(fqdn), "AAAA");
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1184
				if res then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1185
					for _, record in ipairs(res) do
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1186
						external_addresses:add(record.aaaa);
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1187
					end
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1188
				end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1189
			end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1190
		end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1191
5723
24b6eb65480c prosodyctl: Import local_addresses from the new util.net intead of luasocket
Kim Alvefur <zash@zash.se>
parents: 5657
diff changeset
  1192
		local local_addresses = require"util.net".local_addresses() or {};
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1193
5592
5705e21ba24b prosodyctl: check dns: Use socket.local_addresses() if available
Matthew Wild <mwild1@gmail.com>
parents: 5591
diff changeset
  1194
		for addr in it.values(local_addresses) do
5705e21ba24b prosodyctl: check dns: Use socket.local_addresses() if available
Matthew Wild <mwild1@gmail.com>
parents: 5591
diff changeset
  1195
			if not ip.new_ip(addr).private then
5705e21ba24b prosodyctl: check dns: Use socket.local_addresses() if available
Matthew Wild <mwild1@gmail.com>
parents: 5591
diff changeset
  1196
				external_addresses:add(addr);
5705e21ba24b prosodyctl: check dns: Use socket.local_addresses() if available
Matthew Wild <mwild1@gmail.com>
parents: 5591
diff changeset
  1197
			else
5705e21ba24b prosodyctl: check dns: Use socket.local_addresses() if available
Matthew Wild <mwild1@gmail.com>
parents: 5591
diff changeset
  1198
				internal_addresses:add(addr);
5705e21ba24b prosodyctl: check dns: Use socket.local_addresses() if available
Matthew Wild <mwild1@gmail.com>
parents: 5591
diff changeset
  1199
			end
5705e21ba24b prosodyctl: check dns: Use socket.local_addresses() if available
Matthew Wild <mwild1@gmail.com>
parents: 5591
diff changeset
  1200
		end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1201
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1202
		if external_addresses:empty() then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1203
			print("");
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1204
			print("   Failed to determine the external addresses of this server. Checks may be inaccurate.");
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1205
			c2s_srv_required, s2s_srv_required = true, true;
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1206
		end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1207
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1208
		local v6_supported = not not socket.tcp6;
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1209
7314
e9526dd3e8fc prosodyctl: Only use host part of JIDs eg components that have node parts (thanks suzyo)
Kim Alvefur <zash@zash.se>
parents: 7300
diff changeset
  1210
		for jid, host_options in enabled_hosts() do
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1211
			local all_targets_ok, some_targets_ok = true, false;
7314
e9526dd3e8fc prosodyctl: Only use host part of JIDs eg components that have node parts (thanks suzyo)
Kim Alvefur <zash@zash.se>
parents: 7300
diff changeset
  1212
			local node, host = jid_split(jid);
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1213
8926
ddd98e262519 prosodyctl: Only check for s2s if mod_s2s is enabled (fixes #1031)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8920
diff changeset
  1214
			local modules, component_module = modulemanager.get_modules_for_host(host);
ddd98e262519 prosodyctl: Only check for s2s if mod_s2s is enabled (fixes #1031)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8920
diff changeset
  1215
			if component_module then
ddd98e262519 prosodyctl: Only check for s2s if mod_s2s is enabled (fixes #1031)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8920
diff changeset
  1216
				modules:add(component_module);
ddd98e262519 prosodyctl: Only check for s2s if mod_s2s is enabled (fixes #1031)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8920
diff changeset
  1217
			end
ddd98e262519 prosodyctl: Only check for s2s if mod_s2s is enabled (fixes #1031)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8920
diff changeset
  1218
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1219
			local is_component = not not host_options.component_module;
7314
e9526dd3e8fc prosodyctl: Only use host part of JIDs eg components that have node parts (thanks suzyo)
Kim Alvefur <zash@zash.se>
parents: 7300
diff changeset
  1220
			print("Checking DNS for "..(is_component and "component" or "host").." "..jid.."...");
e9526dd3e8fc prosodyctl: Only use host part of JIDs eg components that have node parts (thanks suzyo)
Kim Alvefur <zash@zash.se>
parents: 7300
diff changeset
  1221
			if node then
e9526dd3e8fc prosodyctl: Only use host part of JIDs eg components that have node parts (thanks suzyo)
Kim Alvefur <zash@zash.se>
parents: 7300
diff changeset
  1222
				print("Only the domain part ("..host..") is used in DNS.")
e9526dd3e8fc prosodyctl: Only use host part of JIDs eg components that have node parts (thanks suzyo)
Kim Alvefur <zash@zash.se>
parents: 7300
diff changeset
  1223
			end
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1224
			local target_hosts = set.new();
8927
8fe98e365ab8 prosodyctl: Restrict c2s checks for when c2s is enabled
Kim Alvefur <zash@zash.se>
parents: 8926
diff changeset
  1225
			if modules:contains("c2s") then
5809
be997c6a69be prosodyctl: check: Support for unicode (IDN) domains (thanks once again albert)
Matthew Wild <mwild1@gmail.com>
parents: 5723
diff changeset
  1226
				local res = dns.lookup("_xmpp-client._tcp."..idna.to_ascii(host)..".", "SRV");
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1227
				if res then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1228
					for _, record in ipairs(res) do
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1229
						target_hosts:add(record.srv.target);
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1230
						if not c2s_ports:contains(record.srv.port) then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1231
							print("    SRV target "..record.srv.target.." contains unknown client port: "..record.srv.port);
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1232
						end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1233
					end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1234
				else
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1235
					if c2s_srv_required then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1236
						print("    No _xmpp-client SRV record found for "..host..", but it looks like you need one.");
7255
fa0169cc8511 prosodyctl: Fix typo'd variable name [luacheck]
Matthew Wild <mwild1@gmail.com>
parents: 7253
diff changeset
  1237
						all_targets_ok = false;
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1238
					else
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1239
						target_hosts:add(host);
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1240
					end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1241
				end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1242
			end
8926
ddd98e262519 prosodyctl: Only check for s2s if mod_s2s is enabled (fixes #1031)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8920
diff changeset
  1243
			if modules:contains("s2s") then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1244
				local res = dns.lookup("_xmpp-server._tcp."..idna.to_ascii(host)..".", "SRV");
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1245
				if res then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1246
					for _, record in ipairs(res) do
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1247
						target_hosts:add(record.srv.target);
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1248
						if not s2s_ports:contains(record.srv.port) then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1249
							print("    SRV target "..record.srv.target.." contains unknown server port: "..record.srv.port);
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1250
						end
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1251
					end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1252
				else
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1253
					if s2s_srv_required then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1254
						print("    No _xmpp-server SRV record found for "..host..", but it looks like you need one.");
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1255
						all_targets_ok = false;
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1256
					else
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1257
						target_hosts:add(host);
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1258
					end
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1259
				end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1260
			end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1261
			if target_hosts:empty() then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1262
				target_hosts:add(host);
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1263
			end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1264
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1265
			if target_hosts:contains("localhost") then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1266
				print("    Target 'localhost' cannot be accessed from other servers");
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1267
				target_hosts:remove("localhost");
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1268
			end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1269
5620
8349ae2a44ce prosodyctl: check dns: Add check that proxy65 addresses resolve correctly
Matthew Wild <mwild1@gmail.com>
parents: 5619
diff changeset
  1270
			if modules:contains("proxy65") then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1271
				local proxy65_target = configmanager.get(host, "proxy65_address") or host;
5809
be997c6a69be prosodyctl: check: Support for unicode (IDN) domains (thanks once again albert)
Matthew Wild <mwild1@gmail.com>
parents: 5723
diff changeset
  1272
				local A, AAAA = dns.lookup(idna.to_ascii(proxy65_target), "A"), dns.lookup(idna.to_ascii(proxy65_target), "AAAA");
5620
8349ae2a44ce prosodyctl: check dns: Add check that proxy65 addresses resolve correctly
Matthew Wild <mwild1@gmail.com>
parents: 5619
diff changeset
  1273
				local prob = {};
8349ae2a44ce prosodyctl: check dns: Add check that proxy65 addresses resolve correctly
Matthew Wild <mwild1@gmail.com>
parents: 5619
diff changeset
  1274
				if not A then
8349ae2a44ce prosodyctl: check dns: Add check that proxy65 addresses resolve correctly
Matthew Wild <mwild1@gmail.com>
parents: 5619
diff changeset
  1275
					table.insert(prob, "A");
8349ae2a44ce prosodyctl: check dns: Add check that proxy65 addresses resolve correctly
Matthew Wild <mwild1@gmail.com>
parents: 5619
diff changeset
  1276
				end
8349ae2a44ce prosodyctl: check dns: Add check that proxy65 addresses resolve correctly
Matthew Wild <mwild1@gmail.com>
parents: 5619
diff changeset
  1277
				if v6_supported and not AAAA then
8349ae2a44ce prosodyctl: check dns: Add check that proxy65 addresses resolve correctly
Matthew Wild <mwild1@gmail.com>
parents: 5619
diff changeset
  1278
					table.insert(prob, "AAAA");
8349ae2a44ce prosodyctl: check dns: Add check that proxy65 addresses resolve correctly
Matthew Wild <mwild1@gmail.com>
parents: 5619
diff changeset
  1279
				end
8349ae2a44ce prosodyctl: check dns: Add check that proxy65 addresses resolve correctly
Matthew Wild <mwild1@gmail.com>
parents: 5619
diff changeset
  1280
				if #prob > 0 then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1281
					print("    File transfer proxy "..proxy65_target.." has no "..table.concat(prob, "/")
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1282
					.." record. Create one or set 'proxy65_address' to the correct host/IP.");
5620
8349ae2a44ce prosodyctl: check dns: Add check that proxy65 addresses resolve correctly
Matthew Wild <mwild1@gmail.com>
parents: 5619
diff changeset
  1283
				end
8349ae2a44ce prosodyctl: check dns: Add check that proxy65 addresses resolve correctly
Matthew Wild <mwild1@gmail.com>
parents: 5619
diff changeset
  1284
			end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1285
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1286
			for target_host in target_hosts do
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1287
				local host_ok_v4, host_ok_v6;
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1288
				do
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1289
					local res = dns.lookup(idna.to_ascii(target_host), "A");
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1290
					if res then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1291
						for _, record in ipairs(res) do
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1292
							if external_addresses:contains(record.a) then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1293
								some_targets_ok = true;
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1294
								host_ok_v4 = true;
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1295
							elseif internal_addresses:contains(record.a) then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1296
								host_ok_v4 = true;
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1297
								some_targets_ok = true;
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1298
								print("    "..target_host.." A record points to internal address, external connections might fail");
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1299
							else
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1300
								print("    "..target_host.." A record points to unknown address "..record.a);
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1301
								all_targets_ok = false;
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1302
							end
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1303
						end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1304
					end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1305
				end
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1306
				do
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1307
					local res = dns.lookup(idna.to_ascii(target_host), "AAAA");
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1308
					if res then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1309
						for _, record in ipairs(res) do
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1310
							if external_addresses:contains(record.aaaa) then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1311
								some_targets_ok = true;
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1312
								host_ok_v6 = true;
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1313
							elseif internal_addresses:contains(record.aaaa) then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1314
								host_ok_v6 = true;
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1315
								some_targets_ok = true;
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1316
								print("    "..target_host.." AAAA record points to internal address, external connections might fail");
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1317
							else
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1318
								print("    "..target_host.." AAAA record points to unknown address "..record.aaaa);
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1319
								all_targets_ok = false;
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1320
							end
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1321
						end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1322
					end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1323
				end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1324
5590
597450c73ce6 prosodyctl: check dns: More concise output (merged separate v4/v6 warnings)
Matthew Wild <mwild1@gmail.com>
parents: 5589
diff changeset
  1325
				local bad_protos = {}
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1326
				if not host_ok_v4 then
5590
597450c73ce6 prosodyctl: check dns: More concise output (merged separate v4/v6 warnings)
Matthew Wild <mwild1@gmail.com>
parents: 5589
diff changeset
  1327
					table.insert(bad_protos, "IPv4");
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1328
				end
5590
597450c73ce6 prosodyctl: check dns: More concise output (merged separate v4/v6 warnings)
Matthew Wild <mwild1@gmail.com>
parents: 5589
diff changeset
  1329
				if not host_ok_v6 then
597450c73ce6 prosodyctl: check dns: More concise output (merged separate v4/v6 warnings)
Matthew Wild <mwild1@gmail.com>
parents: 5589
diff changeset
  1330
					table.insert(bad_protos, "IPv6");
597450c73ce6 prosodyctl: check dns: More concise output (merged separate v4/v6 warnings)
Matthew Wild <mwild1@gmail.com>
parents: 5589
diff changeset
  1331
				end
597450c73ce6 prosodyctl: check dns: More concise output (merged separate v4/v6 warnings)
Matthew Wild <mwild1@gmail.com>
parents: 5589
diff changeset
  1332
				if #bad_protos > 0 then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1333
					print("    Host "..target_host.." does not seem to resolve to this server ("..table.concat(bad_protos, "/")..")");
5590
597450c73ce6 prosodyctl: check dns: More concise output (merged separate v4/v6 warnings)
Matthew Wild <mwild1@gmail.com>
parents: 5589
diff changeset
  1334
				end
597450c73ce6 prosodyctl: check dns: More concise output (merged separate v4/v6 warnings)
Matthew Wild <mwild1@gmail.com>
parents: 5589
diff changeset
  1335
				if host_ok_v6 and not v6_supported then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1336
					print("    Host "..target_host.." has AAAA records, but your version of LuaSocket does not support IPv6.");
7362
a5a080c12c96 Update every link to the documentation to use HTTPS
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7316
diff changeset
  1337
					print("      Please see https://prosody.im/doc/ipv6 for more information.");
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1338
				end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1339
			end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1340
			if not all_targets_ok then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1341
				print("    "..(some_targets_ok and "Only some" or "No").." targets for "..host.." appear to resolve to this server.");
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1342
				if is_component then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1343
					print("    DNS records are necessary if you want users on other servers to access this component.");
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1344
				end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1345
				problem_hosts:add(host);
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1346
			end
5591
f0bf2a1790d9 prosodyctl: check dns: Whitespace fix in output
Matthew Wild <mwild1@gmail.com>
parents: 5590
diff changeset
  1347
			print("");
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1348
		end
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1349
		if not problem_hosts:empty() then
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1350
			print("");
7362
a5a080c12c96 Update every link to the documentation to use HTTPS
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 7316
diff changeset
  1351
			print("For more information about DNS configuration please see https://prosody.im/doc/dns");
5585
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1352
			print("");
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1353
			ok = false;
3e097acf82de prosodyctl: Add 'prosodyctl check dns' to make an attempt at verifying the server's DNS records
Matthew Wild <mwild1@gmail.com>
parents: 5584
diff changeset
  1354
		end
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1355
	end
5657
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1356
	if not what or what == "certs" then
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1357
		local cert_ok;
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1358
		print"Checking certificates..."
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1359
		local x509_verify_identity = require"util.x509".verify_identity;
7211
f666d50cc32b prosodyctl: check certs: Use certmanager to get the final ssl config in order to support the new certificate(s) config option
Kim Alvefur <zash@zash.se>
parents: 7196
diff changeset
  1360
		local create_context = require "core.certmanager".create_context;
5657
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1361
		local ssl = dependencies.softreq"ssl";
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1362
		-- local datetime_parse = require"util.datetime".parse_x509;
6615
6cc48b51d699 prosodyctl: Use ssl.loadcertificate instead of ssl.x509.load, as the ssl.x509 export dissapears in 97b1974 or 356e03a
Kim Alvefur <zash@zash.se>
parents: 6614
diff changeset
  1363
		local load_cert = ssl and ssl.loadcertificate;
5657
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1364
		-- or ssl.cert_from_pem
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1365
		if not ssl then
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1366
			print("LuaSec not available, can't perform certificate checks")
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1367
			if what == "certs" then cert_ok = false end
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1368
		elseif not load_cert then
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1369
			print("This version of LuaSec (" .. ssl._VERSION .. ") does not support certificate checking");
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1370
			cert_ok = false
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1371
		else
8193
331caee0c774 prosodyctl: Skip certificate checks for hosts of the form node@host (fixes #779)
Kim Alvefur <zash@zash.se>
parents: 8180
diff changeset
  1372
			local function skip_bare_jid_hosts(host)
331caee0c774 prosodyctl: Skip certificate checks for hosts of the form node@host (fixes #779)
Kim Alvefur <zash@zash.se>
parents: 8180
diff changeset
  1373
				if jid_split(host) then
331caee0c774 prosodyctl: Skip certificate checks for hosts of the form node@host (fixes #779)
Kim Alvefur <zash@zash.se>
parents: 8180
diff changeset
  1374
					-- See issue #779
331caee0c774 prosodyctl: Skip certificate checks for hosts of the form node@host (fixes #779)
Kim Alvefur <zash@zash.se>
parents: 8180
diff changeset
  1375
					return false;
331caee0c774 prosodyctl: Skip certificate checks for hosts of the form node@host (fixes #779)
Kim Alvefur <zash@zash.se>
parents: 8180
diff changeset
  1376
				end
331caee0c774 prosodyctl: Skip certificate checks for hosts of the form node@host (fixes #779)
Kim Alvefur <zash@zash.se>
parents: 8180
diff changeset
  1377
				return true;
331caee0c774 prosodyctl: Skip certificate checks for hosts of the form node@host (fixes #779)
Kim Alvefur <zash@zash.se>
parents: 8180
diff changeset
  1378
			end
331caee0c774 prosodyctl: Skip certificate checks for hosts of the form node@host (fixes #779)
Kim Alvefur <zash@zash.se>
parents: 8180
diff changeset
  1379
			for host in it.filter(skip_bare_jid_hosts, enabled_hosts()) do
6158
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1380
				print("Checking certificate for "..host);
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1381
				-- First, let's find out what certificate this host uses.
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1382
				local host_ssl_config = configmanager.rawget(host, "ssl")
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1383
					or configmanager.rawget(host:match("%.(.*)"), "ssl");
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1384
				local global_ssl_config = configmanager.rawget("*", "ssl");
7211
f666d50cc32b prosodyctl: check certs: Use certmanager to get the final ssl config in order to support the new certificate(s) config option
Kim Alvefur <zash@zash.se>
parents: 7196
diff changeset
  1385
				local ok, err, ssl_config = create_context(host, "server", host_ssl_config, global_ssl_config);
f666d50cc32b prosodyctl: check certs: Use certmanager to get the final ssl config in order to support the new certificate(s) config option
Kim Alvefur <zash@zash.se>
parents: 7196
diff changeset
  1386
				if not ok then
f666d50cc32b prosodyctl: check certs: Use certmanager to get the final ssl config in order to support the new certificate(s) config option
Kim Alvefur <zash@zash.se>
parents: 7196
diff changeset
  1387
					print("  Error: "..err);
6158
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1388
					cert_ok = false
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1389
				elseif not ssl_config.certificate then
7212
92e5036bc55c prosodyctl: check certs: Update messages to account for 'ssl' option maybe not existing
Kim Alvefur <zash@zash.se>
parents: 7211
diff changeset
  1390
					print("  No 'certificate' found for "..host)
6158
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1391
					cert_ok = false
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1392
				elseif not ssl_config.key then
7215
167dbd29fbeb prosodyctl: Fix typo (thanks av6)
Kim Alvefur <zash@zash.se>
parents: 7213
diff changeset
  1393
					print("  No 'key' found for "..host)
6158
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1394
					cert_ok = false
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1395
				else
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1396
					local key, err = io.open(ssl_config.key); -- Permissions check only
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1397
					if not key then
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1398
						print("    Could not open "..ssl_config.key..": "..err);
5657
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1399
						cert_ok = false
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1400
					else
6158
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1401
						key:close();
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1402
					end
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1403
					local cert_fh, err = io.open(ssl_config.certificate); -- Load the file.
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1404
					if not cert_fh then
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1405
						print("    Could not open "..ssl_config.certificate..": "..err);
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1406
						cert_ok = false
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1407
					else
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1408
						print("  Certificate: "..ssl_config.certificate)
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1409
						local cert = load_cert(cert_fh:read"*a"); cert_fh:close();
6158
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1410
						if not cert:validat(os.time()) then
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1411
							print("    Certificate has expired.")
5657
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1412
							cert_ok = false
7213
48149ecbb649 prosodyctl: check certs: Warn about certificate expiry in the near future
Kim Alvefur <zash@zash.se>
parents: 7212
diff changeset
  1413
						elseif not cert:validat(os.time() + 86400) then
48149ecbb649 prosodyctl: check certs: Warn about certificate expiry in the near future
Kim Alvefur <zash@zash.se>
parents: 7212
diff changeset
  1414
							print("    Certificate expires within one day.")
48149ecbb649 prosodyctl: check certs: Warn about certificate expiry in the near future
Kim Alvefur <zash@zash.se>
parents: 7212
diff changeset
  1415
							cert_ok = false
48149ecbb649 prosodyctl: check certs: Warn about certificate expiry in the near future
Kim Alvefur <zash@zash.se>
parents: 7212
diff changeset
  1416
						elseif not cert:validat(os.time() + 86400*7) then
48149ecbb649 prosodyctl: check certs: Warn about certificate expiry in the near future
Kim Alvefur <zash@zash.se>
parents: 7212
diff changeset
  1417
							print("    Certificate expires within one week.")
7216
193e4c65d218 prosodyctl: One month is 31 days, no 13 (thanks av6)
Kim Alvefur <zash@zash.se>
parents: 7215
diff changeset
  1418
						elseif not cert:validat(os.time() + 86400*31) then
7213
48149ecbb649 prosodyctl: check certs: Warn about certificate expiry in the near future
Kim Alvefur <zash@zash.se>
parents: 7212
diff changeset
  1419
							print("    Certificate expires within one month.")
6158
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1420
						end
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1421
						if configmanager.get(host, "component_module") == nil
5657
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1422
							and not x509_verify_identity(host, "_xmpp-client", cert) then
7448
6d2038147d3d prosodyctl: Fix typo (thanks av6)
Kim Alvefur <zash@zash.se>
parents: 7314
diff changeset
  1423
							print("    Not valid for client connections to "..host..".")
6158
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1424
							cert_ok = false
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1425
						end
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1426
						if (not (configmanager.get(host, "anonymous_login")
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1427
							or configmanager.get(host, "authentication") == "anonymous"))
6640
ecc039288edc prosodyctl: check certs: Correctly check that certificates are valid for s2s
Kim Alvefur <zash@zash.se>
parents: 6615
diff changeset
  1428
							and not x509_verify_identity(host, "_xmpp-server", cert) then
7448
6d2038147d3d prosodyctl: Fix typo (thanks av6)
Kim Alvefur <zash@zash.se>
parents: 7314
diff changeset
  1429
							print("    Not valid for server-to-server connections to "..host..".")
6158
08e9c9d0beb3 prosodyctl: Only perform checks on enabled hosts
Kim Alvefur <zash@zash.se>
parents: 6062
diff changeset
  1430
							cert_ok = false
5657
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1431
						end
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1432
					end
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1433
				end
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1434
			end
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1435
		end
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1436
		if cert_ok == false then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1437
			print("")
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1438
			print("For more information about certificates please see https://prosody.im/doc/certificates");
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1439
			ok = false
5657
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1440
		end
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1441
		print("")
7957f14038e8 prosodyctl: Add 'prosodyctl check certs' for validating TLS/SSL certificates
Kim Alvefur <zash@zash.se>
parents: 5655
diff changeset
  1442
	end
5584
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1443
	if not ok then
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1444
		print("Problems found, see above.");
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1445
	else
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1446
		print("All checks passed, congratulations!");
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1447
	end
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1448
	return ok and 0 or 2;
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1449
end
1d841117117c prosodyctl: Add 'check' command, which currently checks the config file for some common mistakes
Matthew Wild <mwild1@gmail.com>
parents: 5554
diff changeset
  1450
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1451
---------------------
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1452
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1453
local async = require "util.async";
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1454
local server = require "net.server";
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1455
local watchers = {
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1456
	error = function (_, err)
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1457
		error(err);
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1458
	end;
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1459
	waiting = function ()
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1460
		server.loop();
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1461
	end;
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1462
};
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1463
local command_runner = async.runner(function ()
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1464
	if command and command:match("^mod_") then -- Is a command in a module
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1465
		local module_name = command:match("^mod_(.+)");
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1466
		do
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1467
			local ret, err = modulemanager.load("*", module_name);
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1468
			if not ret then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1469
				show_message("Failed to load module '"..module_name.."': "..err);
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1470
				os.exit(1);
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1471
			end
1390
ef672c9fe7c9 prosodyctl: Allow commands to be implemented in modules
Matthew Wild <mwild1@gmail.com>
parents: 1205
diff changeset
  1472
		end
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1473
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1474
		local module = modulemanager.get_module("*", module_name);
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1475
		if not module then
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1476
			show_message("Failed to load module '"..module_name.."': Unknown error");
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1477
			os.exit(1);
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1478
		end
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1479
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1480
		if not modulemanager.module_has_method(module, "command") then
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1481
			show_message("Fail: mod_"..module_name.." does not support any commands");
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1482
			os.exit(1);
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1483
		end
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1484
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1485
		local ok, ret = modulemanager.call_module_method(module, "command", arg);
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1486
		if ok then
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1487
			if type(ret) == "number" then
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1488
				os.exit(ret);
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1489
			elseif type(ret) == "string" then
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1490
				show_message(ret);
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1491
			end
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1492
			os.exit(0); -- :)
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1493
		else
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1494
			show_message("Failed to execute command: "..error_messages[ret]);
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1495
			os.exit(1); -- :(
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1496
		end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1497
	end
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1498
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1499
	if not commands[command] then -- Show help for all commands
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1500
		function show_usage(usage, desc)
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1501
			print(" "..usage);
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1502
			print("    "..desc);
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1503
		end
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1504
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1505
		print("prosodyctl - Manage a Prosody server");
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1506
		print("");
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1507
		print("Usage: "..arg[0].." COMMAND [OPTIONS]");
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1508
		print("");
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1509
		print("Where COMMAND may be one of:\n");
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1510
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1511
		local hidden_commands = require "util.set".new{ "register", "unregister", "addplugin" };
10139
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
  1512
		local commands_order = { "adduser", "passwd", "deluser", "start", "stop", "restart", "reload", "about", "local_plugins", "enabled_plugins",
3ae2809030dd prosodyctl: added help support to all my functions
João Duarte <jvsDuarte08@gmail.com>
parents: 10138
diff changeset
  1513
      "admin_add", "admin_remove", "list", };
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1514
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1515
		local done = {};
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1516
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1517
		for _, command_name in ipairs(commands_order) do
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1518
			local command_func = commands[command_name];
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1519
			if command_func then
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1520
				command_func{ "--help" };
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1521
				print""
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1522
				done[command_name] = true;
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1523
			end
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1524
		end
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1525
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1526
		for command_name, command_func in pairs(commands) do
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1527
			if not done[command_name] and not hidden_commands:contains(command_name) then
8674
a4899174a894 prosodyctl: Large number of changes to satisfy [luacheck], includes bug fixes
Matthew Wild <mwild1@gmail.com>
parents: 8671
diff changeset
  1528
				command_func{ "--help" };
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1529
				print""
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1530
				done[command_name] = true;
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1531
			end
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1532
		end
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1533
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1534
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1535
		os.exit(0);
1087
5e9475bec571 prosodyctl, util.prosodyctl: New prosodyctl utility for managing Prosody servers
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
  1536
	end
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1537
9784
c7727c13260f prosodyctl: Pass the original argv table to subcommands (with first argument removed)
Kim Alvefur <zash@zash.se>
parents: 9713
diff changeset
  1538
	os.exit(commands[command](arg));
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1539
end, watchers);
7925
2fd20f372cb1 prosodyctl: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7923
diff changeset
  1540
8655
03bb534593cb prosodyctl: Run commands inside async context
Matthew Wild <mwild1@gmail.com>
parents: 8638
diff changeset
  1541
command_runner:run(true);