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"
43 -- TODO: Check for other formats when we add support for them
44 -- Use lfs? Make a new conf/ dir?
45 local ok, level, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
48 print("**************************");
49 if level == "parser" then
50 print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
51 local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
52 print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
54 elseif level == "file" then
55 print("Prosody was unable to find the configuration file.");
56 print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
57 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
58 print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
60 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
62 print("**************************");
68 require "core.loggingmanager"
70 if not require "util.dependencies".check_dependencies() then
74 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
75 require "util.datamanager".set_data_path(data_path);
77 -- Switch away from root and into the prosody user --
78 local switched_user, current_uid;
80 local want_pposix_version = "0.3.3";
81 local ok, pposix = pcall(require, "util.pposix");
84 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
85 current_uid = pposix.getuid();
86 if current_uid == 0 then
88 local desired_user = config.get("*", "core", "prosody_user") or "prosody";
89 local desired_group = config.get("*", "core", "prosody_group") or desired_user;
90 local ok, err = pposix.setgid(desired_group);
92 ok, err = pposix.setuid(desired_user);
98 if not switched_user then
100 print("Warning: Couldn't switch to Prosody user/group '"..tostring(desired_user).."'/'"..tostring(desired_group).."': "..tostring(err));
104 -- Set our umask to protect data files
105 pposix.umask(config.get("*", "core", "umask") or "027");
107 print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
108 print("For more help send the below error to us through http://prosody.im/discuss");
109 print(tostring(pposix))
112 local error_messages = setmetatable({
113 ["invalid-username"] = "The given username is invalid in a Jabber ID";
114 ["invalid-hostname"] = "The given hostname is invalid";
115 ["no-password"] = "No password was supplied";
116 ["no-such-user"] = "The given user does not exist on the server";
117 ["unable-to-save-data"] = "Unable to store, perhaps you don't have permission?";
118 ["no-pidfile"] = "There is no 'pidfile' option in the configuration file, see http://prosody.im/doc/prosodyctl#pidfile for help";
119 ["no-such-method"] = "This module has no commands";
120 ["not-running"] = "Prosody is not running";
121 }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
123 hosts = prosody.hosts;
125 local function make_host(hostname)
126 return { events = prosody.events, users = require "core.usermanager".new_null_provider(hostname) };
129 for hostname, config in pairs(config.getconfig()) do
130 hosts[hostname] = make_host(hostname);
133 require "core.modulemanager"
135 require "util.prosodyctl"
137 -----------------------
139 function show_message(msg, ...)
140 print(msg:format(...));
143 function show_warning(msg, ...)
144 print(msg:format(...));
147 function show_usage(usage, desc)
148 print("Usage: "..arg[0].." "..usage);
154 local function getchar(n)
155 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
157 if stty_ret == 0 then
158 ok, char = pcall(io.read, n or 1);
159 os.execute("stty sane");
161 ok, char = pcall(io.read, "*l");
163 char = char:sub(1, n or 1);
171 local function getpass()
172 local stty_ret = os.execute("stty -echo 2>/dev/null");
173 if stty_ret ~= 0 then
174 io.write("\027[08m"); -- ANSI 'hidden' text attribute
176 local ok, pass = pcall(io.read, "*l");
177 if stty_ret == 0 then
178 os.execute("stty sane");
180 io.write("\027[00m");
188 function show_yesno(prompt)
189 io.write(prompt, " ");
190 local choice = getchar():lower();
192 if not choice:match("%a") then
193 choice = prompt:match("%[.-(%U).-%]$");
194 if not choice then return nil; end
196 return (choice == "y");
199 local function read_password()
202 io.write("Enter new password: ");
203 password = getpass();
205 show_message("No password - cancelled");
208 io.write("Retype new password: ");
209 if getpass() ~= password then
210 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
220 local prosodyctl_timeout = (config.get("*", "core", "prosodyctl_timeout") or 5) * 2;
221 -----------------------
223 local command = arg[1];
225 function commands.adduser(arg)
226 if not arg[1] or arg[1] == "--help" then
227 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
230 local user, host = arg[1]:match("([^@]+)@(.+)");
231 if not user and host then
232 show_message [[Failed to understand JID, please supply the JID you want to create]]
233 show_usage [[adduser user@host]]
238 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
242 if not hosts[host] then
243 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
244 show_warning("The user will not be able to log in until this is changed.");
245 hosts[host] = make_host(host);
246 elseif config.get(host, "core", "authentication")
247 and config.get(host, "core", "authentication") ~= "default" then
248 show_warning("The host '%s' is configured to use the '%s' authentication provider", host,
249 config.get(host, "core", "authentication"));
250 show_warning("prosodyctl currently only supports the default provider, sorry :(");
254 if prosodyctl.user_exists{ user = user, host = host } then
255 show_message [[That user already exists]];
259 local password = read_password();
260 if not password then return 1; end
262 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
264 if ok then return 0; end
266 show_message(error_messages[msg])
270 function commands.passwd(arg)
271 if not arg[1] or arg[1] == "--help" then
272 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
275 local user, host = arg[1]:match("([^@]+)@(.+)");
276 if not user and host then
277 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
278 show_usage [[passwd user@host]]
283 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
287 if not hosts[host] then
288 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
289 show_warning("The user will not be able to log in until this is changed.");
290 hosts[host] = make_host(host);
291 elseif config.get(host, "core", "authentication")
292 and config.get(host, "core", "authentication") ~= "default" then
293 show_warning("The host '%s' is configured to use the '%s' authentication provider", host,
294 config.get(host, "core", "authentication"));
295 show_warning("prosodyctl currently only supports the default provider, sorry :(");
299 if not prosodyctl.user_exists { user = user, host = host } then
300 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
304 local password = read_password();
305 if not password then return 1; end
307 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
309 if ok then return 0; end
311 show_message(error_messages[msg])
315 function commands.deluser(arg)
316 if not arg[1] or arg[1] == "--help" then
317 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
320 local user, host = arg[1]:match("([^@]+)@(.+)");
321 if not user and host then
322 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
323 show_usage [[passwd user@host]]
328 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
332 if not hosts[host] then
333 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
334 show_warning("The user will not be able to log in until this is changed.");
335 hosts[host] = make_host(host);
336 elseif config.get(host, "core", "authentication")
337 and config.get(host, "core", "authentication") ~= "default" then
338 show_warning("The host '%s' is configured to use the '%s' authentication provider", host,
339 config.get(host, "core", "authentication"));
340 show_warning("prosodyctl currently only supports the default provider, sorry :(");
344 if not prosodyctl.user_exists { user = user, host = host } then
345 show_message [[That user does not exist on this server]]
349 local ok, msg = prosodyctl.passwd { user = user, host = host };
351 if ok then return 0; end
353 show_message(error_messages[msg])
357 function commands.start(arg)
358 if arg[1] == "--help" then
359 show_usage([[start]], [[Start Prosody]]);
362 local ok, ret = prosodyctl.isrunning();
364 show_message(error_messages[ret]);
369 local ok, ret = prosodyctl.getpid();
371 show_message("Couldn't get running Prosody's PID");
372 show_message(error_messages[ret]);
375 show_message("Prosody is already running with PID %s", ret or "(unknown)");
379 local ok, ret = prosodyctl.start();
381 if config.get("*", "core", "daemonize") ~= false then
384 local ok, running = prosodyctl.isrunning();
385 if ok and running then
388 show_message("Still waiting...");
389 elseif i >= prosodyctl_timeout then
390 show_message("Prosody is still not running. Please give it some time or check your log files for errors.");
396 show_message("Started");
401 show_message("Failed to start Prosody");
402 show_message(error_messages[ret])
406 function commands.status(arg)
407 if arg[1] == "--help" then
408 show_usage([[status]], [[Reports the running status of Prosody]]);
412 local ok, ret = prosodyctl.isrunning();
414 show_message(error_messages[ret]);
419 local ok, ret = prosodyctl.getpid();
421 show_message("Couldn't get running Prosody's PID");
422 show_message(error_messages[ret]);
425 show_message("Prosody is running with PID %s", ret or "(unknown)");
428 show_message("Prosody is not running");
429 if not switched_user and current_uid ~= 0 then
431 print(" You will also see this if prosodyctl is not running under");
432 print(" the same user account as Prosody. Try running as root (e.g. ");
433 print(" with 'sudo' in front) to gain access to Prosody's real status.");
440 function commands.stop(arg)
441 if arg[1] == "--help" then
442 show_usage([[stop]], [[Stop a running Prosody server]]);
446 if not prosodyctl.isrunning() then
447 show_message("Prosody is not running");
451 local ok, ret = prosodyctl.stop();
455 local ok, running = prosodyctl.isrunning();
456 if ok and not running then
459 show_message("Still waiting...");
460 elseif i >= prosodyctl_timeout then
461 show_message("Prosody is still running. Please give it some time or check your log files for errors.");
467 show_message("Stopped");
471 show_message(error_messages[ret]);
475 function commands.restart(arg)
476 if arg[1] == "--help" then
477 show_usage([[restart]], [[Restart a running Prosody server]]);
481 local ret = commands.stop(arg);
483 ret = commands.start(arg);
488 -- ejabberdctl compatibility
490 function commands.register(arg)
491 local user, host, password = unpack(arg);
492 if (not (user and host)) or arg[1] == "--help" then
493 if user ~= "--help" then
495 show_message [[No username specified]]
497 show_message [[Please specify which host you want to register the user on]];
500 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
504 password = read_password();
506 show_message [[Unable to register user with no password]];
511 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
513 if ok then return 0; end
515 show_message(error_messages[msg])
519 function commands.unregister(arg)
520 local user, host = unpack(arg);
521 if (not (user and host)) or arg[1] == "--help" then
522 if user ~= "--help" then
524 show_message [[No username specified]]
526 show_message [[Please specify which host you want to unregister the user from]];
529 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
533 local ok, msg = prosodyctl.deluser { user = user, host = host };
535 if ok then return 0; end
537 show_message(error_messages[msg])
541 local http_errors = {
542 [404] = "Plugin not found, did you type the address correctly?"
545 function commands.addplugin(arg)
546 if not arg[1] or arg[1] == "--help" then
547 show_usage("addplugin URL", "Download and install a plugin from a URL");
551 if url:match("^http://") then
552 local http = require "socket.http";
553 show_message("Fetching...");
554 local code, err = http.request(url);
555 if not code or not tostring(err):match("^[23]") then
556 show_message("Failed: "..(http_errors[err] or ("HTTP error "..err)));
559 if url:match("%.lua$") then
560 local ok, err = datamanager.store(url:match("/mod_([^/]+)$"), "*", "plugins", {code});
562 show_message("Failed to save to data store: "..err);
566 show_message("Saved. Don't forget to load the module using the config file or admin console!");
568 show_message("Sorry, I don't understand how to fetch plugins from there.");
572 ---------------------
574 if command and command:match("^mod_") then -- Is a command in a module
575 local module_name = command:match("^mod_(.+)");
576 local ret, err = modulemanager.load("*", module_name);
578 show_message("Failed to load module '"..module_name.."': "..err);
582 table.remove(arg, 1);
584 local module = modulemanager.get_module("*", module_name);
586 show_message("Failed to load module '"..module_name.."': Unknown error");
590 if not modulemanager.module_has_method(module, "command") then
591 show_message("Fail: mod_"..module_name.." does not support any commands");
595 local ok, ret = modulemanager.call_module_method(module, "command", arg);
597 if type(ret) == "number" then
599 elseif type(ret) == "string" then
604 show_message("Failed to execute command: "..error_messages[ret]);
609 if not commands[command] then -- Show help for all commands
610 function show_usage(usage, desc)
615 print("prosodyctl - Manage a Prosody server");
617 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
619 print("Where COMMAND may be one of:\n");
621 local hidden_commands = require "util.set".new{ "register", "unregister", "addplugin" };
622 local commands_order = { "adduser", "passwd", "deluser", "start", "stop", "restart" };
626 for _, command_name in ipairs(commands_order) do
627 local command = commands[command_name];
631 done[command_name] = true;
635 for command_name, command in pairs(commands) do
636 if not done[command_name] and not hidden_commands:contains(command_name) then
639 done[command_name] = true;
647 os.exit(commands[command]({ select(2, unpack(arg)) }));