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 -- Tell Lua where to find our libraries
23 package.path = CFG_SOURCEDIR.."/?.lua;"..package.path;
24 package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath;
27 -- Substitute ~ with path to home directory in data path
29 if os.getenv("HOME") then
30 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
34 -- Global 'prosody' object
37 events = require "util.events".new();
39 lock_globals = function () end;
40 unlock_globals = function () end;
42 local prosody = prosody;
44 config = require "core.configmanager"
50 if arg[1] == "--config" and arg[2] then
51 table.insert(filenames, arg[2]);
52 table.remove(arg, 1); table.remove(arg, 1);
54 table.insert(filenames, CFG_CONFIGDIR.."/"..arg[2]);
57 for _, format in ipairs(config.parsers()) do
58 table.insert(filenames, (CFG_CONFIGDIR or ".").."/prosody.cfg."..format);
61 for _,_filename in ipairs(filenames) do
63 local file = io.open(filename);
66 CFG_CONFIGDIR = filename:match("^(.*)[\\/][^\\/]*$");
70 local ok, level, err = config.load(filename);
73 print("**************************");
74 if level == "parser" then
75 print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
76 local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
77 print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
79 elseif level == "file" then
80 print("Prosody was unable to find the configuration file.");
81 print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
82 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
83 print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
85 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
87 print("**************************");
92 local original_logging_config = config.get("*", "core", "log");
93 config.set("*", "core", "log", { { levels = { min="info" }, to = "console" } });
95 require "core.loggingmanager"
97 if not require "util.dependencies".check_dependencies() then
101 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
102 require "util.datamanager".set_data_path(data_path);
104 -- Switch away from root and into the prosody user --
105 local switched_user, current_uid;
107 local want_pposix_version = "0.3.5";
108 local ok, pposix = pcall(require, "util.pposix");
110 if ok and pposix then
111 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
112 current_uid = pposix.getuid();
113 if current_uid == 0 then
115 local desired_user = config.get("*", "core", "prosody_user") or "prosody";
116 local desired_group = config.get("*", "core", "prosody_group") or desired_user;
117 local ok, err = pposix.setgid(desired_group);
119 ok, err = pposix.initgroups(desired_user);
122 ok, err = pposix.setuid(desired_user);
125 switched_user = true;
128 if not switched_user then
130 print("Warning: Couldn't switch to Prosody user/group '"..tostring(desired_user).."'/'"..tostring(desired_group).."': "..tostring(err));
134 -- Set our umask to protect data files
135 pposix.umask(config.get("*", "core", "umask") or "027");
137 print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
138 print("For more help send the below error to us through http://prosody.im/discuss");
139 print(tostring(pposix))
142 local function test_writeable(filename)
143 local f, err = io.open(filename, "a");
151 local unwriteable_files = {};
152 if type(original_logging_config) == "string" and original_logging_config:sub(1,1) ~= "*" then
153 local ok, err = test_writeable(original_logging_config);
155 table.insert(unwriteable_files, err);
157 elseif type(original_logging_config) == "table" then
158 for _, rule in ipairs(original_logging_config) do
159 if rule.filename then
160 local ok, err = test_writeable(rule.filename);
162 table.insert(unwriteable_files, err);
168 if #unwriteable_files > 0 then
169 print("One of more of the Prosody log files are not");
170 print("writeable, please correct the errors and try");
171 print("starting prosodyctl again.");
173 for _, err in ipairs(unwriteable_files) do
181 local error_messages = setmetatable({
182 ["invalid-username"] = "The given username is invalid in a Jabber ID";
183 ["invalid-hostname"] = "The given hostname is invalid";
184 ["no-password"] = "No password was supplied";
185 ["no-such-user"] = "The given user does not exist on the server";
186 ["unable-to-save-data"] = "Unable to store, perhaps you don't have permission?";
187 ["no-pidfile"] = "There is no 'pidfile' option in the configuration file, see http://prosody.im/doc/prosodyctl#pidfile for help";
188 ["no-posix"] = "The mod_posix module is not enabled in the Prosody config file, see http://prosody.im/doc/prosodyctl for more info";
189 ["no-such-method"] = "This module has no commands";
190 ["not-running"] = "Prosody is not running";
191 }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
193 hosts = prosody.hosts;
195 local function make_host(hostname)
198 events = prosody.events,
199 users = require "core.usermanager".new_null_provider(hostname)
203 for hostname, config in pairs(config.getconfig()) do
204 hosts[hostname] = make_host(hostname);
207 require "core.modulemanager"
209 require "util.prosodyctl"
211 -----------------------
213 function show_message(msg, ...)
214 print(msg:format(...));
217 function show_warning(msg, ...)
218 print(msg:format(...));
221 function show_usage(usage, desc)
222 print("Usage: "..arg[0].." "..usage);
228 local function getchar(n)
229 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
231 if stty_ret == 0 then
232 ok, char = pcall(io.read, n or 1);
233 os.execute("stty sane");
235 ok, char = pcall(io.read, "*l");
237 char = char:sub(1, n or 1);
245 local function getpass()
246 local stty_ret = os.execute("stty -echo 2>/dev/null");
247 if stty_ret ~= 0 then
248 io.write("\027[08m"); -- ANSI 'hidden' text attribute
250 local ok, pass = pcall(io.read, "*l");
251 if stty_ret == 0 then
252 os.execute("stty sane");
254 io.write("\027[00m");
262 function show_yesno(prompt)
263 io.write(prompt, " ");
264 local choice = getchar():lower();
266 if not choice:match("%a") then
267 choice = prompt:match("%[.-(%U).-%]$");
268 if not choice then return nil; end
270 return (choice == "y");
273 local function read_password()
276 io.write("Enter new password: ");
277 password = getpass();
279 show_message("No password - cancelled");
282 io.write("Retype new password: ");
283 if getpass() ~= password then
284 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
294 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2;
295 -----------------------
297 local command = arg[1];
299 function commands.adduser(arg)
300 if not arg[1] or arg[1] == "--help" then
301 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
304 local user, host = arg[1]:match("([^@]+)@(.+)");
305 if not user and host then
306 show_message [[Failed to understand JID, please supply the JID you want to create]]
307 show_usage [[adduser user@host]]
312 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
316 if not hosts[host] then
317 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
318 show_warning("The user will not be able to log in until this is changed.");
319 hosts[host] = make_host(host);
322 if prosodyctl.user_exists{ user = user, host = host } then
323 show_message [[That user already exists]];
327 local password = read_password();
328 if not password then return 1; end
330 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
332 if ok then return 0; end
338 function commands.passwd(arg)
339 if not arg[1] or arg[1] == "--help" then
340 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
343 local user, host = arg[1]:match("([^@]+)@(.+)");
344 if not user and host then
345 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
346 show_usage [[passwd user@host]]
351 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
355 if not hosts[host] then
356 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
357 show_warning("The user will not be able to log in until this is changed.");
358 hosts[host] = make_host(host);
361 if not prosodyctl.user_exists { user = user, host = host } then
362 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
366 local password = read_password();
367 if not password then return 1; end
369 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
371 if ok then return 0; end
373 show_message(error_messages[msg])
377 function commands.deluser(arg)
378 if not arg[1] or arg[1] == "--help" then
379 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
382 local user, host = arg[1]:match("([^@]+)@(.+)");
383 if not user and host then
384 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
385 show_usage [[passwd user@host]]
390 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
394 if not hosts[host] then
395 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
396 show_warning("The user will not be able to log in until this is changed.");
397 hosts[host] = make_host(host);
400 if not prosodyctl.user_exists { user = user, host = host } then
401 show_message [[That user does not exist on this server]]
405 local ok, msg = prosodyctl.passwd { user = user, host = host };
407 if ok then return 0; end
409 show_message(error_messages[msg])
413 function commands.start(arg)
414 if arg[1] == "--help" then
415 show_usage([[start]], [[Start Prosody]]);
418 local ok, ret = prosodyctl.isrunning();
420 show_message(error_messages[ret]);
425 local ok, ret = prosodyctl.getpid();
427 show_message("Couldn't get running Prosody's PID");
428 show_message(error_messages[ret]);
431 show_message("Prosody is already running with PID %s", ret or "(unknown)");
435 local ok, ret = prosodyctl.start();
437 if config.get("*", "core", "daemonize") ~= false then
440 local ok, running = prosodyctl.isrunning();
441 if ok and running then
444 show_message("Still waiting...");
445 elseif i >= prosodyctl_timeout then
446 show_message("Prosody is still not running. Please give it some time or check your log files for errors.");
452 show_message("Started");
457 show_message("Failed to start Prosody");
458 show_message(error_messages[ret])
462 function commands.status(arg)
463 if arg[1] == "--help" then
464 show_usage([[status]], [[Reports the running status of Prosody]]);
468 local ok, ret = prosodyctl.isrunning();
470 show_message(error_messages[ret]);
475 local ok, ret = prosodyctl.getpid();
477 show_message("Couldn't get running Prosody's PID");
478 show_message(error_messages[ret]);
481 show_message("Prosody is running with PID %s", ret or "(unknown)");
484 show_message("Prosody is not running");
485 if not switched_user and current_uid ~= 0 then
487 print(" You will also see this if prosodyctl is not running under");
488 print(" the same user account as Prosody. Try running as root (e.g. ");
489 print(" with 'sudo' in front) to gain access to Prosody's real status.");
496 function commands.stop(arg)
497 if arg[1] == "--help" then
498 show_usage([[stop]], [[Stop a running Prosody server]]);
502 if not prosodyctl.isrunning() then
503 show_message("Prosody is not running");
507 local ok, ret = prosodyctl.stop();
511 local ok, running = prosodyctl.isrunning();
512 if ok and not running then
515 show_message("Still waiting...");
516 elseif i >= prosodyctl_timeout then
517 show_message("Prosody is still running. Please give it some time or check your log files for errors.");
523 show_message("Stopped");
527 show_message(error_messages[ret]);
531 function commands.restart(arg)
532 if arg[1] == "--help" then
533 show_usage([[restart]], [[Restart a running Prosody server]]);
538 return commands.start(arg);
541 -- ejabberdctl compatibility
543 function commands.register(arg)
544 local user, host, password = unpack(arg);
545 if (not (user and host)) or arg[1] == "--help" then
546 if user ~= "--help" then
548 show_message [[No username specified]]
550 show_message [[Please specify which host you want to register the user on]];
553 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
557 password = read_password();
559 show_message [[Unable to register user with no password]];
564 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
566 if ok then return 0; end
568 show_message(error_messages[msg])
572 function commands.unregister(arg)
573 local user, host = unpack(arg);
574 if (not (user and host)) or arg[1] == "--help" then
575 if user ~= "--help" then
577 show_message [[No username specified]]
579 show_message [[Please specify which host you want to unregister the user from]];
582 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
586 local ok, msg = prosodyctl.deluser { user = user, host = host };
588 if ok then return 0; end
590 show_message(error_messages[msg])
594 ---------------------
596 if command and command:match("^mod_") then -- Is a command in a module
597 local module_name = command:match("^mod_(.+)");
598 local ret, err = modulemanager.load("*", module_name);
600 show_message("Failed to load module '"..module_name.."': "..err);
604 table.remove(arg, 1);
606 local module = modulemanager.get_module("*", module_name);
608 show_message("Failed to load module '"..module_name.."': Unknown error");
612 if not modulemanager.module_has_method(module, "command") then
613 show_message("Fail: mod_"..module_name.." does not support any commands");
617 local ok, ret = modulemanager.call_module_method(module, "command", arg);
619 if type(ret) == "number" then
621 elseif type(ret) == "string" then
626 show_message("Failed to execute command: "..error_messages[ret]);
631 if not commands[command] then -- Show help for all commands
632 function show_usage(usage, desc)
637 print("prosodyctl - Manage a Prosody server");
639 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
641 print("Where COMMAND may be one of:\n");
643 local hidden_commands = require "util.set".new{ "register", "unregister", "addplugin" };
644 local commands_order = { "adduser", "passwd", "deluser", "start", "stop", "restart" };
648 for _, command_name in ipairs(commands_order) do
649 local command = commands[command_name];
653 done[command_name] = true;
657 for command_name, command in pairs(commands) do
658 if not done[command_name] and not hidden_commands:contains(command_name) then
661 done[command_name] = true;
669 os.exit(commands[command]({ select(2, unpack(arg)) }));