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 -- Required to be able to find packages installed with luarocks
33 pcall(require, "luarocks.require")
36 config = require "core.configmanager"
39 -- TODO: Check for other formats when we add support for them
40 -- Use lfs? Make a new conf/ dir?
41 local ok, level, err = config.load((CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
44 print("**************************");
45 if level == "parser" then
46 print("A problem occured while reading the config file "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
47 local err_line, err_message = tostring(err):match("%[string .-%]:(%d*): (.*)");
48 print("Error"..(err_line and (" on line "..err_line) or "")..": "..(err_message or tostring(err)));
50 elseif level == "file" then
51 print("Prosody was unable to find the configuration file.");
52 print("We looked for: "..(CFG_CONFIGDIR or ".").."/prosody.cfg.lua");
53 print("A sample config file is included in the Prosody download called prosody.cfg.lua.dist");
54 print("Copy or rename it to prosody.cfg.lua and edit as necessary.");
56 print("More help on configuring Prosody can be found at http://prosody.im/doc/configure");
58 print("**************************");
64 -- Switch away from root and into the prosody user --
65 local switched_user, current_uid;
66 local ok, pposix = pcall(require, "util.pposix");
68 current_uid = pposix.getuid();
69 if current_uid == 0 then
71 local desired_user = config.get("*", "core", "prosody_user") or "prosody";
72 local ok, err = pposix.setuid(desired_user);
78 print("Warning: Couldn't switch to Prosody user '"..tostring(desired_user).."': "..tostring(err));
82 print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
83 print("For more help send the below error to us through http://prosody.im/discuss");
84 print(tostring(pposix))
87 local error_messages = setmetatable({
88 ["invalid-username"] = "The given username is invalid in a Jabber ID";
89 ["invalid-hostname"] = "The given hostname is invalid";
90 ["no-password"] = "No password was supplied";
91 ["no-such-user"] = "The given user does not exist on the server";
92 }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
96 require "core.hostmanager"
97 require "core.eventmanager".fire_event("server-starting");
99 require "util.prosodyctl"
100 -----------------------
102 function show_message(msg, ...)
103 print(msg:format(...));
106 function show_warning(msg, ...)
107 print(msg:format(...));
110 function show_usage(usage, desc)
111 print("Usage: "..arg[0].." "..usage);
117 local function getchar(n)
118 os.execute("stty raw -echo");
119 local char = io.read(n or 1);
120 os.execute("stty sane");
124 local function getpass()
125 os.execute("stty -echo");
126 local pass = io.read("*l");
127 os.execute("stty sane");
132 function show_yesno(prompt)
133 io.write(prompt, " ");
134 local choice = getchar():lower();
136 if not choice:match("%a") then
137 choice = prompt:match("%[.-(%U).-%]$");
138 if not choice then return nil; end
140 return (choice == "y");
143 local function read_password()
146 io.write("Enter new password: ");
147 password = getpass();
148 io.write("Retype new password: ");
149 if getpass() ~= password then
150 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
159 -----------------------
161 local command = arg[1];
163 function commands.adduser(arg)
164 if not arg[1] or arg[1] == "--help" then
165 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
168 local user, host = arg[1]:match("([^@]+)@(.+)");
169 if not user and host then
170 show_message [[Failed to understand JID, please supply the JID you want to create]]
171 show_usage [[adduser user@host]]
176 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
180 if prosodyctl.user_exists{ user = user, host = host } then
181 show_message [[That user already exists]];
185 if not hosts[host] then
186 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
187 show_warning("The user will not be able to log in until this is changed.");
190 local password = read_password();
191 if not password then return 1; end
193 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
195 if ok then return 0; end
197 show_message(error_messages[msg])
201 function commands.passwd(arg)
202 if not arg[1] or arg[1] == "--help" then
203 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
206 local user, host = arg[1]:match("([^@]+)@(.+)");
207 if not user and host then
208 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
209 show_usage [[passwd user@host]]
214 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
218 if not prosodyctl.user_exists { user = user, host = host } then
219 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
223 local password = read_password();
224 if not password then return 1; end
226 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
228 if ok then return 0; end
230 show_message(error_messages[msg])
234 function commands.deluser(arg)
235 if not arg[1] or arg[1] == "--help" then
236 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
239 local user, host = arg[1]:match("([^@]+)@(.+)");
240 if not user and host then
241 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
242 show_usage [[passwd user@host]]
247 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
251 if not prosodyctl.user_exists { user = user, host = host } then
252 show_message [[That user does not exist on this server]]
256 local ok, msg = prosodyctl.passwd { user = user, host = host };
258 if ok then return 0; end
260 show_message(error_messages[msg])
264 function commands.start(arg)
265 if arg[1] == "--help" then
266 show_usage([[start]], [[Start Prosody]]);
269 local ok, ret = prosodyctl.isrunning();
271 show_message(error_messages[ret]);
276 local ok, ret = prosodyctl.getpid();
278 show_message("Couldn't get running Prosody's PID");
279 show_message(error_messages[ret]);
282 show_message("Prosody is already running with PID %s", ret or "(unknown)");
286 local ok, ret = prosodyctl.start();
287 if ok then return 0; end
289 show_message("Failed to start Prosody");
290 show_message(error_messages[ret])
294 function commands.status(arg)
295 if arg[1] == "--help" then
296 show_usage([[status]], [[Reports the running status of Prosody]]);
300 local ok, ret = prosodyctl.isrunning();
302 show_message(error_messages[ret]);
307 local ok, ret = prosodyctl.getpid();
309 show_message("Couldn't get running Prosody's PID");
310 show_message(error_messages[ret]);
313 show_message("Prosody is running with PID %s", ret or "(unknown)");
316 show_message("Prosody is not running");
317 if not switched_user and current_uid ~= 0 then
318 print("\nNote: You will also see this if prosodyctl is not running under the same");
319 print(" user account as Prosody. Try running as root (e.g. with 'sudo' in front) to");
320 print(" gain access to Prosody's real status.");
327 function commands.stop(arg)
328 if arg[1] == "--help" then
329 show_usage([[stop]], [[Stop a running Prosody server]]);
333 if not prosodyctl.isrunning() then
334 show_message("Prosody is not running");
338 local ok, ret = prosodyctl.stop();
339 if ok then return 0; end
341 show_message(error_messages[ret]);
345 -- ejabberdctl compatibility
347 function commands.register(arg)
348 local user, host, password = unpack(arg);
349 if (not (user and host)) or arg[1] == "--help" then
350 if user ~= "--help" then
352 show_message [[No username specified]]
354 show_message [[Please specify which host you want to register the user on]];
357 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
361 password = read_password();
363 show_message [[Unable to register user with no password]];
368 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
370 if ok then return 0; end
372 show_message(error_messages[msg])
376 function commands.unregister(arg)
377 local user, host = unpack(arg);
378 if (not (user and host)) or arg[1] == "--help" then
379 if user ~= "--help" then
381 show_message [[No username specified]]
383 show_message [[Please specify which host you want to unregister the user from]];
386 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
390 local ok, msg = prosodyctl.deluser { user = user, host = host };
392 if ok then return 0; end
394 show_message(error_messages[msg])
399 ---------------------
401 if not commands[command] then -- Show help for all commands
402 function show_usage(usage, desc)
407 print("prosodyctl - Manage a Prosody server");
409 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
411 print("Where COMMAND may be one of:\n");
413 local hidden_commands = require "util.set".new{ "register", "unregister" };
414 local commands_order = { "adduser", "passwd", "deluser" };
418 for _, command_name in ipairs(commands_order) do
419 local command = commands[command_name];
423 done[command_name] = true;
427 for command_name, command in pairs(commands) do
428 if not done[command_name] and not hidden_commands:contains(command_name) then
431 done[command_name] = true;
439 os.exit(commands[command]({ select(2, unpack(arg)) }));