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 local data_path = config.get("*", "core", "data_path") or CFG_DATADIR or "data";
65 require "util.datamanager".set_data_path(data_path);
67 -- Switch away from root and into the prosody user --
68 local switched_user, current_uid;
69 local ok, pposix = pcall(require, "util.pposix");
71 current_uid = pposix.getuid();
72 if current_uid == 0 then
74 local desired_user = config.get("*", "core", "prosody_user") or "prosody";
75 local ok, err = pposix.setuid(desired_user);
81 print("Warning: Couldn't switch to Prosody user '"..tostring(desired_user).."': "..tostring(err));
85 print("Error: Unable to load pposix module. Check that Prosody is installed correctly.")
86 print("For more help send the below error to us through http://prosody.im/discuss");
87 print(tostring(pposix))
90 local error_messages = setmetatable({
91 ["invalid-username"] = "The given username is invalid in a Jabber ID";
92 ["invalid-hostname"] = "The given hostname is invalid";
93 ["no-password"] = "No password was supplied";
94 ["no-such-user"] = "The given user does not exist on the server";
95 }, { __index = function (t,k) return "Error: "..(tostring(k):gsub("%-", " "):gsub("^.", string.upper)); end });
99 require "core.hostmanager"
100 require "core.eventmanager".fire_event("server-starting");
102 require "util.prosodyctl"
103 -----------------------
105 function show_message(msg, ...)
106 print(msg:format(...));
109 function show_warning(msg, ...)
110 print(msg:format(...));
113 function show_usage(usage, desc)
114 print("Usage: "..arg[0].." "..usage);
120 local function getchar(n)
121 os.execute("stty raw -echo");
122 local char = io.read(n or 1);
123 os.execute("stty sane");
127 local function getpass()
128 os.execute("stty -echo");
129 local pass = io.read("*l");
130 os.execute("stty sane");
135 function show_yesno(prompt)
136 io.write(prompt, " ");
137 local choice = getchar():lower();
139 if not choice:match("%a") then
140 choice = prompt:match("%[.-(%U).-%]$");
141 if not choice then return nil; end
143 return (choice == "y");
146 local function read_password()
149 io.write("Enter new password: ");
150 password = getpass();
151 io.write("Retype new password: ");
152 if getpass() ~= password then
153 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
162 -----------------------
164 local command = arg[1];
166 function commands.adduser(arg)
167 if not arg[1] or arg[1] == "--help" then
168 show_usage([[adduser JID]], [[Create the specified user account in Prosody]]);
171 local user, host = arg[1]:match("([^@]+)@(.+)");
172 if not user and host then
173 show_message [[Failed to understand JID, please supply the JID you want to create]]
174 show_usage [[adduser user@host]]
179 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
183 if prosodyctl.user_exists{ user = user, host = host } then
184 show_message [[That user already exists]];
188 if not hosts[host] then
189 show_warning("The host '%s' is not listed in the configuration file (or is not enabled).", host)
190 show_warning("The user will not be able to log in until this is changed.");
193 local password = read_password();
194 if not password then return 1; end
196 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
198 if ok then return 0; end
200 show_message(error_messages[msg])
204 function commands.passwd(arg)
205 if not arg[1] or arg[1] == "--help" then
206 show_usage([[passwd JID]], [[Set the password for the specified user account in Prosody]]);
209 local user, host = arg[1]:match("([^@]+)@(.+)");
210 if not user and host then
211 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
212 show_usage [[passwd user@host]]
217 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
221 if not prosodyctl.user_exists { user = user, host = host } then
222 show_message [[That user does not exist, use prosodyctl adduser to create a new user]]
226 local password = read_password();
227 if not password then return 1; end
229 local ok, msg = prosodyctl.passwd { user = user, host = host, password = password };
231 if ok then return 0; end
233 show_message(error_messages[msg])
237 function commands.deluser(arg)
238 if not arg[1] or arg[1] == "--help" then
239 show_usage([[deluser JID]], [[Permanently remove the specified user account from Prosody]]);
242 local user, host = arg[1]:match("([^@]+)@(.+)");
243 if not user and host then
244 show_message [[Failed to understand JID, please supply the JID you want to set the password for]]
245 show_usage [[passwd user@host]]
250 show_message [[Please specify a JID, including a host. e.g. alice@example.com]];
254 if not prosodyctl.user_exists { user = user, host = host } then
255 show_message [[That user does not exist on this server]]
259 local ok, msg = prosodyctl.passwd { user = user, host = host };
261 if ok then return 0; end
263 show_message(error_messages[msg])
267 function commands.start(arg)
268 if arg[1] == "--help" then
269 show_usage([[start]], [[Start Prosody]]);
272 local ok, ret = prosodyctl.isrunning();
274 show_message(error_messages[ret]);
279 local ok, ret = prosodyctl.getpid();
281 show_message("Couldn't get running Prosody's PID");
282 show_message(error_messages[ret]);
285 show_message("Prosody is already running with PID %s", ret or "(unknown)");
289 local ok, ret = prosodyctl.start();
290 if ok then return 0; end
292 show_message("Failed to start Prosody");
293 show_message(error_messages[ret])
297 function commands.status(arg)
298 if arg[1] == "--help" then
299 show_usage([[status]], [[Reports the running status of Prosody]]);
303 local ok, ret = prosodyctl.isrunning();
305 show_message(error_messages[ret]);
310 local ok, ret = prosodyctl.getpid();
312 show_message("Couldn't get running Prosody's PID");
313 show_message(error_messages[ret]);
316 show_message("Prosody is running with PID %s", ret or "(unknown)");
319 show_message("Prosody is not running");
320 if not switched_user and current_uid ~= 0 then
321 print("\nNote: You will also see this if prosodyctl is not running under the same");
322 print(" user account as Prosody. Try running as root (e.g. with 'sudo' in front) to");
323 print(" gain access to Prosody's real status.");
330 function commands.stop(arg)
331 if arg[1] == "--help" then
332 show_usage([[stop]], [[Stop a running Prosody server]]);
336 if not prosodyctl.isrunning() then
337 show_message("Prosody is not running");
341 local ok, ret = prosodyctl.stop();
342 if ok then return 0; end
344 show_message(error_messages[ret]);
348 -- ejabberdctl compatibility
350 function commands.register(arg)
351 local user, host, password = unpack(arg);
352 if (not (user and host)) or arg[1] == "--help" then
353 if user ~= "--help" then
355 show_message [[No username specified]]
357 show_message [[Please specify which host you want to register the user on]];
360 show_usage("register USER HOST [PASSWORD]", "Register a user on the server, with the given password");
364 password = read_password();
366 show_message [[Unable to register user with no password]];
371 local ok, msg = prosodyctl.adduser { user = user, host = host, password = password };
373 if ok then return 0; end
375 show_message(error_messages[msg])
379 function commands.unregister(arg)
380 local user, host = unpack(arg);
381 if (not (user and host)) or arg[1] == "--help" then
382 if user ~= "--help" then
384 show_message [[No username specified]]
386 show_message [[Please specify which host you want to unregister the user from]];
389 show_usage("unregister USER HOST [PASSWORD]", "Permanently remove a user account from the server");
393 local ok, msg = prosodyctl.deluser { user = user, host = host };
395 if ok then return 0; end
397 show_message(error_messages[msg])
402 ---------------------
404 if not commands[command] then -- Show help for all commands
405 function show_usage(usage, desc)
410 print("prosodyctl - Manage a Prosody server");
412 print("Usage: "..arg[0].." COMMAND [OPTIONS]");
414 print("Where COMMAND may be one of:\n");
416 local hidden_commands = require "util.set".new{ "register", "unregister" };
417 local commands_order = { "adduser", "passwd", "deluser" };
421 for _, command_name in ipairs(commands_order) do
422 local command = commands[command_name];
426 done[command_name] = true;
430 for command_name, command in pairs(commands) do
431 if not done[command_name] and not hidden_commands:contains(command_name) then
434 done[command_name] = true;
442 os.exit(commands[command]({ select(2, unpack(arg)) }));