3 -- Copyright (C) 2008-2009 Matthew Wild
4 -- Copyright (C) 2008-2009 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 --
15 CFG_CONFIGDIR=os.getenv("PROSODY_CFGDIR");
17 CFG_DATADIR=os.getenv("PROSODY_DATADIR");
19 -- -- -- -- -- -- -- ---- -- -- -- -- -- -- -- --
22 package.path = CFG_SOURCEDIR.."/?.lua;"..package.path
23 package.cpath = CFG_SOURCEDIR.."/?.so;"..package.cpath
27 if os.getenv("HOME") then
28 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
32 -- Global 'prosody' object
35 events = require "util.events".new(),
38 local prosody = prosody;
40 config = require "core.configmanager"
46 if arg[1] == "--config" and arg[2] then
47 table.insert(filenames, arg[2]);
48 table.remove(arg, 1); table.remove(arg, 1);
50 table.insert(filenames, CFG_CONFIGDIR.."/"..arg[2]);
53 for _, format in ipairs(config.parsers()) do
54 table.insert(filenames, (CFG_CONFIGDIR or ".").."/prosody.cfg."..format);
57 for _,_filename in ipairs(filenames) do
59 local file = io.open(filename);
62 CFG_CONFIGDIR = filename:match("^(.*)[\\/][^\\/]*$");
66 local ok, level, err = config.load(filename);
69 print("**************************");
70 if level == "parser" then
71 print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
72 local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
73 print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
75 elseif level == "file" then
76 print("Prosody was unable to find the configuration file.");
77 print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
78 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
79 print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
81 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
83 print("**************************");
88 local original_logging_config = config.get("*", "core", "log");
89 config.set("*", "core", "log", { { levels = { min="info" }, to = "console" } });
91 require "core.loggingmanager"
93 if not require "util.dependencies".check_dependencies() then
97 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
98 require "util.datamanager".set_data_path(data_path);
100 -- Switch away from root and into the prosody user --
101 local switched_user, current_uid;
103 local want_pposix_version = "0.3.5";
104 local ok, pposix = pcall(require, "util.pposix");
106 if ok and pposix then
107 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
108 current_uid = pposix.getuid();
109 if current_uid == 0 then
111 local desired_user = config.get("*", "core", "prosody_user") or "prosody";
112 local desired_group = config.get("*", "core", "prosody_group") or desired_user;
113 local ok, err = pposix.setgid(desired_group);
115 ok, err = pposix.initgroups(desired_user);
118 ok, err = pposix.setuid(desired_user);
121 switched_user = true;
124 if not switched_user then
126 print("Warning: Couldn't switch to Prosody user/group '"..tostring(desired_user).."'/'"..tostring(desired_group).."': "..tostring(err));
130 -- Set our umask to protect data files
131 pposix.umask(config.get("*", "core", "umask") or "027");
133 print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
134 print("For more help send the below error to us through http://prosody.im/discuss");
135 print(tostring(pposix))
138 local function test_writeable(filename)
139 local f, err = io.open(filename, "a");
147 local unwriteable_files = {};
148 if type(original_logging_config) == "string" and original_logging_config:sub(1,1) ~= "*" then
149 local ok, err = test_writeable(original_logging_config);
151 table.insert(unwriteable_files, err);
153 elseif type(original_logging_config) == "table" then
154 for _, rule in ipairs(original_logging_config) do
155 if rule.filename then
156 local ok, err = test_writeable(rule.filename);
158 table.insert(unwriteable_files, err);
164 if #unwriteable_files > 0 then
165 print("One of more of the Prosody log files are not");
166 print("writeable, please correct the errors and try");
167 print("starting prosodyctl again.");
169 for _, err in ipairs(unwriteable_files) do
177 local error_messages = setmetatable({
178 ["invalid-username"] = "The given username is invalid in a Jabber ID";
179 ["invalid-hostname"] = "The given hostname is invalid";
180 ["no-password"] = "No password was supplied";
181 ["no-such-user"] = "The given user does not exist on the server";
182 ["unable-to-save-data"] = "Unable to store, perhaps you don't have permission?";
183 ["no-pidfile"] = "There is no 'pidfile' option in the configuration file, see http://prosody.im/doc/prosodyctl#pidfile for help";
184 ["no-posix"] = "The mod_posix module is not enabled in the Prosody config file, see http://prosody.im/doc/prosodyctl for more info";
185 ["no-such-method"] = "This module has no commands";
186 ["not-running"] = "Prosody is not running";
187 }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
189 hosts = prosody.hosts;
191 local function make_host(hostname)
194 events = prosody.events,
195 users = require "core.usermanager".new_null_provider(hostname)
199 for hostname, config in pairs(config.getconfig()) do
200 hosts[hostname] = make_host(hostname);
203 require "core.modulemanager"
205 require "util.prosodyctl"
207 -----------------------
209 function show_message(msg, ...)
210 print(msg:format(...));
213 function show_warning(msg, ...)
214 print(msg:format(...));
217 function show_usage(usage, desc)
218 print("Usage: "..arg[0].." "..usage);
224 local function getchar(n)
225 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
227 if stty_ret == 0 then
228 ok, char = pcall(io.read, n or 1);
229 os.execute("stty sane");
231 ok, char = pcall(io.read, "*l");
233 char = char:sub(1, n or 1);
241 local function getpass()
242 local stty_ret = os.execute("stty -echo 2>/dev/null");
243 if stty_ret ~= 0 then
244 io.write("\027[08m"); -- ANSI 'hidden' text attribute
246 local ok, pass = pcall(io.read, "*l");
247 if stty_ret == 0 then
248 os.execute("stty sane");
250 io.write("\027[00m");
258 function show_yesno(prompt)
259 io.write(prompt, " ");
260 local choice = getchar():lower();
262 if not choice:match("%a") then
263 choice = prompt:match("%[.-(%U).-%]$");
264 if not choice then return nil; end
266 return (choice == "y");
269 local function read_password()
272 io.write("Enter new password: ");
273 password = getpass();
275 show_message("No password - cancelled");
278 io.write("Retype new password: ");
279 if getpass() ~= password then
280 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
290 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2;
291 -----------------------
293 local command = arg[1];
295 function commands.adduser(arg)
296 if not arg[1] or arg[1] == "--help" then
297 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
300 local user, host = arg[1]:match("([^@]+)@(.+)");
301 if not user and host then
302 show_message [[Failed to understand JID, please supply the JID you want to create]]
303 show_usage [[adduser user@host]]
308 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
312 if not hosts[host] then
313 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
314 show_warning("The user will not be able to log in until this is changed.");
315 hosts[host] = make_host(host);
318 if prosodyctl.user_exists{ user = user, host = host } then
319 show_message [[That user already exists]];
323 local password = read_password();
324 if not password then return 1; end
326 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
328 if ok then return 0; end
330 show_message(error_messages[msg])
334 function commands.passwd(arg)
335 if not arg[1] or arg[1] == "--help" then
336 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
339 local user, host = arg[1]:match("([^@]+)@(.+)");
340 if not user and host then
341 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
342 show_usage [[passwd user@host]]
347 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
351 if not hosts[host] then
352 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
353 show_warning("The user will not be able to log in until this is changed.");
354 hosts[host] = make_host(host);
357 if not prosodyctl.user_exists { user = user, host = host } then
358 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
362 local password = read_password();
363 if not password then return 1; end
365 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
367 if ok then return 0; end
369 show_message(error_messages[msg])
373 function commands.deluser(arg)
374 if not arg[1] or arg[1] == "--help" then
375 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
378 local user, host = arg[1]:match("([^@]+)@(.+)");
379 if not user and host then
380 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
381 show_usage [[passwd user@host]]
386 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
390 if not hosts[host] then
391 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
392 show_warning("The user will not be able to log in until this is changed.");
393 hosts[host] = make_host(host);
396 if not prosodyctl.user_exists { user = user, host = host } then
397 show_message [[That user does not exist on this server]]
401 local ok, msg = prosodyctl.passwd { user = user, host = host };
403 if ok then return 0; end
405 show_message(error_messages[msg])
409 function commands.start(arg)
410 if arg[1] == "--help" then
411 show_usage([[start]], [[Start Prosody]]);
414 local ok, ret = prosodyctl.isrunning();
416 show_message(error_messages[ret]);
421 local ok, ret = prosodyctl.getpid();
423 show_message("Couldn't get running Prosody's PID");
424 show_message(error_messages[ret]);
427 show_message("Prosody is already running with PID %s", ret or "(unknown)");
431 local ok, ret = prosodyctl.start();
433 if config.get("*", "core", "daemonize") ~= false then
436 local ok, running = prosodyctl.isrunning();
437 if ok and running then
440 show_message("Still waiting...");
441 elseif i >= prosodyctl_timeout then
442 show_message("Prosody is still not running. Please give it some time or check your log files for errors.");
448 show_message("Started");
453 show_message("Failed to start Prosody");
454 show_message(error_messages[ret])
458 function commands.status(arg)
459 if arg[1] == "--help" then
460 show_usage([[status]], [[Reports the running status of Prosody]]);
464 local ok, ret = prosodyctl.isrunning();
466 show_message(error_messages[ret]);
471 local ok, ret = prosodyctl.getpid();
473 show_message("Couldn't get running Prosody's PID");
474 show_message(error_messages[ret]);
477 show_message("Prosody is running with PID %s", ret or "(unknown)");
480 show_message("Prosody is not running");
481 if not switched_user and current_uid ~= 0 then
483 print(" You will also see this if prosodyctl is not running under");
484 print(" the same user account as Prosody. Try running as root (e.g. ");
485 print(" with 'sudo' in front) to gain access to Prosody's real status.");
492 function commands.stop(arg)
493 if arg[1] == "--help" then
494 show_usage([[stop]], [[Stop a running Prosody server]]);
498 if not prosodyctl.isrunning() then
499 show_message("Prosody is not running");
503 local ok, ret = prosodyctl.stop();
507 local ok, running = prosodyctl.isrunning();
508 if ok and not running then
511 show_message("Still waiting...");
512 elseif i >= prosodyctl_timeout then
513 show_message("Prosody is still running. Please give it some time or check your log files for errors.");
519 show_message("Stopped");
523 show_message(error_messages[ret]);
527 function commands.restart(arg)
528 if arg[1] == "--help" then
529 show_usage([[restart]], [[Restart a running Prosody server]]);
534 return commands.start(arg);
537 -- ejabberdctl compatibility
539 function commands.register(arg)
540 local user, host, password = unpack(arg);
541 if (not (user and host)) or arg[1] == "--help" then
542 if user ~= "--help" then
544 show_message [[No username specified]]
546 show_message [[Please specify which host you want to register the user on]];
549 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
553 password = read_password();
555 show_message [[Unable to register user with no password]];
560 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
562 if ok then return 0; end
564 show_message(error_messages[msg])
568 function commands.unregister(arg)
569 local user, host = unpack(arg);
570 if (not (user and host)) or arg[1] == "--help" then
571 if user ~= "--help" then
573 show_message [[No username specified]]
575 show_message [[Please specify which host you want to unregister the user from]];
578 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
582 local ok, msg = prosodyctl.deluser { user = user, host = host };
584 if ok then return 0; end
586 show_message(error_messages[msg])
590 ---------------------
592 if command and command:match("^mod_") then -- Is a command in a module
593 local module_name = command:match("^mod_(.+)");
594 local ret, err = modulemanager.load("*", module_name);
596 show_message("Failed to load module '"..module_name.."': "..err);
600 table.remove(arg, 1);
602 local module = modulemanager.get_module("*", module_name);
604 show_message("Failed to load module '"..module_name.."': Unknown error");
608 if not modulemanager.module_has_method(module, "command") then
609 show_message("Fail: mod_"..module_name.." does not support any commands");
613 local ok, ret = modulemanager.call_module_method(module, "command", arg);
615 if type(ret) == "number" then
617 elseif type(ret) == "string" then
622 show_message("Failed to execute command: "..error_messages[ret]);
627 if not commands[command] then -- Show help for all commands
628 function show_usage(usage, desc)
633 print("prosodyctl - Manage a Prosody server");
635 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
637 print("Where COMMAND may be one of:\n");
639 local hidden_commands = require "util.set".new{ "register", "unregister", "addplugin" };
640 local commands_order = { "adduser", "passwd", "deluser", "start", "stop", "restart" };
644 for _, command_name in ipairs(commands_order) do
645 local command = commands[command_name];
649 done[command_name] = true;
653 for command_name, command in pairs(commands) do
654 if not done[command_name] and not hidden_commands:contains(command_name) then
657 done[command_name] = true;
665 os.exit(commands[command]({ select(2, unpack(arg)) }));