3 -- Copyright (C) 2008-2010 Matthew Wild
4 -- Copyright (C) 2008-2010 Waqas Hussain
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 -- prosodyctl - command-line controller for Prosody XMPP server
12 -- Will be modified by configure script if run --
14 CFG_SOURCEDIR=os.getenv("PROSODY_SRCDIR");
15 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
16 CFG_PLUGINDIR=os.getenv("PROSODY_PLUGINDIR");
17 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
19 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
21 local function is_relative(path)
22 local path_sep = package.config:sub(1,1);
23 return ((path_sep == "/" and path:sub(1,1) ~= "/")
24 or (path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\")))
27 -- Tell Lua where to find our libraries
29 local function filter_relative_paths(path)
30 if is_relative(path) then return ""; end
32 local function sanitise_paths(paths)
33 return (paths:gsub("[^;]+;?", filter_relative_paths):gsub(";;+", ";"));
35 package.path = sanitise_paths(CFG_SOURCEDIR.."/?.lua;"..package.path);
36 package.cpath = sanitise_paths(CFG_SOURCEDIR.."/?.so;"..package.cpath);
39 -- Substitute ~ with path to home directory in data path
41 if os.getenv("HOME") then
42 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
46 -- Global 'prosody' object
49 events = require "util.events".new();
51 lock_globals = function () end;
52 unlock_globals = function () end;
56 local dependencies = require "util.dependencies";
57 if not dependencies.check_dependencies() then
61 config = require "core.configmanager"
67 if arg[1] == "--config" and arg[2] then
68 table.insert(filenames, arg[2]);
69 table.remove(arg, 1); table.remove(arg, 1);
71 table.insert(filenames, CFG_CONFIGDIR.."/"..arg[2]);
74 for _, format in ipairs(config.parsers()) do
75 table.insert(filenames, (CFG_CONFIGDIR or ".").."/prosody.cfg."..format);
78 for _,_filename in ipairs(filenames) do
80 local file = io.open(filename);
83 CFG_CONFIGDIR = filename:match("^(.*)[\\/][^\\/]*$");
87 local ok, level, err = config.load(filename);
90 print("**************************");
91 if level == "parser" then
92 print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
93 local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
94 print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
96 elseif level == "file" then
97 print("Prosody was unable to find the configuration file.");
98 print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
99 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
100 print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
102 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
104 print("**************************");
109 local original_logging_config = config.get("*", "core", "log");
110 config.set("*", "core", "log", { { levels = { min="info" }, to = "console" } });
112 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
113 local custom_plugin_paths = config.get("*", "core", "plugin_paths");
114 if custom_plugin_paths then
115 local path_sep = package.config:sub(3,3);
116 -- path1;path2;path3;defaultpath...
117 CFG_PLUGINDIR = table.concat(custom_plugin_paths, path_sep)..path_sep..(CFG_PLUGINDIR or "plugins");
119 prosody.paths = { source = CFG_SOURCEDIR, config = CFG_CONFIGDIR,
120 plugins = CFG_PLUGINDIR or "plugins", data = data_path };
122 require "core.loggingmanager"
124 dependencies.log_warnings();
126 -- Switch away from root and into the prosody user --
127 local switched_user, current_uid;
129 local want_pposix_version = "0.3.5";
130 local ok, pposix = pcall(require, "util.pposix");
132 if ok and pposix then
133 if pposix._VERSION ~= want_pposix_version then print(string.format("Unknown version (%s) of binary pposix module, expected %s", tostring(pposix._VERSION), want_pposix_version)); return; end
134 current_uid = pposix.getuid();
135 if current_uid == 0 then
137 local desired_user = config.get("*", "core", "prosody_user") or "prosody";
138 local desired_group = config.get("*", "core", "prosody_group") or desired_user;
139 local ok, err = pposix.setgid(desired_group);
141 ok, err = pposix.initgroups(desired_user);
144 ok, err = pposix.setuid(desired_user);
147 switched_user = true;
150 if not switched_user then
152 print("Warning: Couldn't switch to Prosody user/group '"..tostring(desired_user).."'/'"..tostring(desired_group).."': "..tostring(err));
156 -- Set our umask to protect data files
157 pposix.umask(config.get("*", "core", "umask") or "027");
159 print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
160 print("For more help send the below error to us through http://prosody.im/discuss");
161 print(tostring(pposix))
164 local function test_writeable(filename)
165 local f, err = io.open(filename, "a");
173 local unwriteable_files = {};
174 if type(original_logging_config) == "string" and original_logging_config:sub(1,1) ~= "*" then
175 local ok, err = test_writeable(original_logging_config);
177 table.insert(unwriteable_files, err);
179 elseif type(original_logging_config) == "table" then
180 for _, rule in ipairs(original_logging_config) do
181 if rule.filename then
182 local ok, err = test_writeable(rule.filename);
184 table.insert(unwriteable_files, err);
190 if #unwriteable_files > 0 then
191 print("One of more of the Prosody log files are not");
192 print("writeable, please correct the errors and try");
193 print("starting prosodyctl again.");
195 for _, err in ipairs(unwriteable_files) do
203 local error_messages = setmetatable({
204 ["invalid-username"] = "The given username is invalid in a Jabber ID";
205 ["invalid-hostname"] = "The given hostname is invalid";
206 ["no-password"] = "No password was supplied";
207 ["no-such-user"] = "The given user does not exist on the server";
208 ["unable-to-save-data"] = "Unable to store, perhaps you don't have permission?";
209 ["no-pidfile"] = "There is no 'pidfile' option in the configuration file, see http://prosody.im/doc/prosodyctl#pidfile for help";
210 ["no-posix"] = "The mod_posix module is not enabled in the Prosody config file, see http://prosody.im/doc/prosodyctl for more info";
211 ["no-such-method"] = "This module has no commands";
212 ["not-running"] = "Prosody is not running";
213 }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
215 hosts = prosody.hosts;
217 local function make_host(hostname)
220 events = prosody.events,
221 users = require "core.usermanager".new_null_provider(hostname)
225 for hostname, config in pairs(config.getconfig()) do
226 hosts[hostname] = make_host(hostname);
229 require "core.modulemanager"
231 require "util.prosodyctl"
233 -----------------------
235 local show_message, show_warning = prosodyctl.show_message, prosodyctl.show_warning;
236 local show_usage = prosodyctl.show_usage;
237 local getchar, getpass = prosodyctl.getchar, prosodyctl.getpass;
238 local show_yesno = prosodyctl.show_yesno;
239 local read_password = prosodyctl.read_password;
241 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2;
242 -----------------------
244 local command = arg[1];
246 function commands.adduser(arg)
247 if not arg[1] or arg[1] == "--help" then
248 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
251 local user, host = arg[1]:match("([^@]+)@(.+)");
252 if not user and host then
253 show_message [[Failed to understand JID, please supply the JID you want to create]]
254 show_usage [[adduser user@host]]
259 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
263 if not hosts[host] then
264 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
265 show_warning("The user will not be able to log in until this is changed.");
266 hosts[host] = make_host(host);
269 if prosodyctl.user_exists{ user = user, host = host } then
270 show_message [[That user already exists]];
274 local password = read_password();
275 if not password then return 1; end
277 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
279 if ok then return 0; end
285 function commands.passwd(arg)
286 if not arg[1] or arg[1] == "--help" then
287 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
290 local user, host = arg[1]:match("([^@]+)@(.+)");
291 if not user and host then
292 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
293 show_usage [[passwd user@host]]
298 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
302 if not hosts[host] then
303 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
304 show_warning("The user will not be able to log in until this is changed.");
305 hosts[host] = make_host(host);
308 if not prosodyctl.user_exists { user = user, host = host } then
309 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
313 local password = read_password();
314 if not password then return 1; end
316 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
318 if ok then return 0; end
320 show_message(error_messages[msg])
324 function commands.deluser(arg)
325 if not arg[1] or arg[1] == "--help" then
326 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
329 local user, host = arg[1]:match("([^@]+)@(.+)");
330 if not user and host then
331 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
332 show_usage [[passwd user@host]]
337 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
341 if not hosts[host] then
342 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
343 show_warning("The user will not be able to log in until this is changed.");
344 hosts[host] = make_host(host);
347 if not prosodyctl.user_exists { user = user, host = host } then
348 show_message [[That user does not exist on this server]]
352 local ok, msg = prosodyctl.passwd { user = user, host = host };
354 if ok then return 0; end
356 show_message(error_messages[msg])
360 function commands.start(arg)
361 if arg[1] == "--help" then
362 show_usage([[start]], [[Start Prosody]]);
365 local ok, ret = prosodyctl.isrunning();
367 show_message(error_messages[ret]);
372 local ok, ret = prosodyctl.getpid();
374 show_message("Couldn't get running Prosody's PID");
375 show_message(error_messages[ret]);
378 show_message("Prosody is already running with PID %s", ret or "(unknown)");
382 local ok, ret = prosodyctl.start();
384 if config.get("*", "core", "daemonize") ~= false then
387 local ok, running = prosodyctl.isrunning();
388 if ok and running then
391 show_message("Still waiting...");
392 elseif i >= prosodyctl_timeout then
393 show_message("Prosody is still not running. Please give it some time or check your log files for errors.");
399 show_message("Started");
404 show_message("Failed to start Prosody");
405 show_message(error_messages[ret])
409 function commands.status(arg)
410 if arg[1] == "--help" then
411 show_usage([[status]], [[Reports the running status of Prosody]]);
415 local ok, ret = prosodyctl.isrunning();
417 show_message(error_messages[ret]);
422 local ok, ret = prosodyctl.getpid();
424 show_message("Couldn't get running Prosody's PID");
425 show_message(error_messages[ret]);
428 show_message("Prosody is running with PID %s", ret or "(unknown)");
431 show_message("Prosody is not running");
432 if not switched_user and current_uid ~= 0 then
434 print(" You will also see this if prosodyctl is not running under");
435 print(" the same user account as Prosody. Try running as root (e.g. ");
436 print(" with 'sudo' in front) to gain access to Prosody's real status.");
443 function commands.stop(arg)
444 if arg[1] == "--help" then
445 show_usage([[stop]], [[Stop a running Prosody server]]);
449 if not prosodyctl.isrunning() then
450 show_message("Prosody is not running");
454 local ok, ret = prosodyctl.stop();
458 local ok, running = prosodyctl.isrunning();
459 if ok and not running then
462 show_message("Still waiting...");
463 elseif i >= prosodyctl_timeout then
464 show_message("Prosody is still running. Please give it some time or check your log files for errors.");
470 show_message("Stopped");
474 show_message(error_messages[ret]);
478 function commands.restart(arg)
479 if arg[1] == "--help" then
480 show_usage([[restart]], [[Restart a running Prosody server]]);
485 return commands.start(arg);
488 function commands.about(arg)
489 if arg[1] == "--help" then
490 show_usage([[about]], [[Show information about this Prosody installation]]);
494 require "util.array";
495 require "util.iterators";
497 print("Prosody "..(prosody.version or "(unknown version)"));
499 print("# Prosody directories");
500 print("Data directory: ", CFG_DATADIR or "./");
501 print("Plugin directory:", CFG_PLUGINDIR or "./");
502 print("Config directory:", CFG_CONFIGDIR or "./");
503 print("Source directory:", CFG_SOURCEDIR or "./");
505 print("# Lua environment");
506 print("Lua version: ", _G._VERSION);
508 print("Lua module search paths:");
509 for path in package.path:gmatch("[^;]+") do
513 print("Lua C module search paths:");
514 for path in package.cpath:gmatch("[^;]+") do
518 print("# Lua module versions");
519 local module_versions, longest_name = {}, 8;
520 for name, module in pairs(package.loaded) do
521 if type(module) == "table" and rawget(module, "_VERSION")
522 and name ~= "_G" and not name:match("%.") then
523 if #name > longest_name then
524 longest_name = #name;
526 module_versions[name] = module._VERSION;
529 local sorted_keys = array.collect(keys(module_versions)):sort();
530 for _, name in ipairs(array.collect(keys(module_versions)):sort()) do
531 print(name..":"..string.rep(" ", longest_name-#name), module_versions[name]);
536 -- ejabberdctl compatibility
538 function commands.register(arg)
539 local user, host, password = unpack(arg);
540 if (not (user and host)) or arg[1] == "--help" then
541 if user ~= "--help" then
543 show_message [[No username specified]]
545 show_message [[Please specify which host you want to register the user on]];
548 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
552 password = read_password();
554 show_message [[Unable to register user with no password]];
559 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
561 if ok then return 0; end
563 show_message(error_messages[msg])
567 function commands.unregister(arg)
568 local user, host = unpack(arg);
569 if (not (user and host)) or arg[1] == "--help" then
570 if user ~= "--help" then
572 show_message [[No username specified]]
574 show_message [[Please specify which host you want to unregister the user from]];
577 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
581 local ok, msg = prosodyctl.deluser { user = user, host = host };
583 if ok then return 0; end
585 show_message(error_messages[msg])
589 ---------------------
591 if command and command:match("^mod_") then -- Is a command in a module
592 local module_name = command:match("^mod_(.+)");
593 local ret, err = modulemanager.load("*", module_name);
595 show_message("Failed to load module '"..module_name.."': "..err);
599 table.remove(arg, 1);
601 local module = modulemanager.get_module("*", module_name);
603 show_message("Failed to load module '"..module_name.."': Unknown error");
607 if not modulemanager.module_has_method(module, "command") then
608 show_message("Fail: mod_"..module_name.." does not support any commands");
612 local ok, ret = modulemanager.call_module_method(module, "command", arg);
614 if type(ret) == "number" then
616 elseif type(ret) == "string" then
621 show_message("Failed to execute command: "..error_messages[ret]);
626 if not commands[command] then -- Show help for all commands
627 function show_usage(usage, desc)
632 print("prosodyctl - Manage a Prosody server");
634 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
636 print("Where COMMAND may be one of:\n");
638 local hidden_commands = require "util.set".new{ "register", "unregister", "addplugin" };
639 local commands_order = { "adduser", "passwd", "deluser", "start", "stop", "restart", "about" };
643 for _, command_name in ipairs(commands_order) do
644 local command = commands[command_name];
648 done[command_name] = true;
652 for command_name, command in pairs(commands) do
653 if not done[command_name] and not hidden_commands:contains(command_name) then
656 done[command_name] = true;
664 os.exit(commands[command]({ select(2, unpack(arg)) }));