2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local config = require "core.configmanager";
11 local encodings = require "util.encodings";
12 local stringprep = encodings.stringprep;
13 local storagemanager = require "core.storagemanager";
14 local usermanager = require "core.usermanager";
15 local signal = require "util.signal";
16 local set = require "util.set";
17 local lfs = require "lfs";
21 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
23 local io, os = io, os;
25 local tostring, tonumber = tostring, tonumber;
27 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
30 local prosody = prosody;
33 local function show_message(msg, ...)
34 print(msg:format(...));
37 local function show_usage(usage, desc)
38 print("Usage: ".._G.arg[0].." "..usage);
44 local function getchar(n)
45 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
48 ok, char = pcall(io.read, n or 1);
49 os.execute("stty sane");
51 ok, char = pcall(io.read, "*l");
53 char = char:sub(1, n or 1);
61 local function getline()
62 local ok, line = pcall(io.read, "*l");
68 local function getpass()
69 local stty_ret = os.execute("stty -echo 2>/dev/null");
71 io.write("\027[08m"); -- ANSI 'hidden' text attribute
73 local ok, pass = pcall(io.read, "*l");
75 os.execute("stty sane");
85 local function show_yesno(prompt)
86 io.write(prompt, " ");
87 local choice = getchar():lower();
89 if not choice:match("%a") then
90 choice = prompt:match("%[.-(%U).-%]$");
91 if not choice then return nil; end
93 return (choice == "y");
96 local function read_password()
99 io.write("Enter new password: ");
100 password = getpass();
102 show_message("No password - cancelled");
105 io.write("Retype new password: ");
106 if getpass() ~= password then
107 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
117 local function show_prompt(prompt)
118 io.write(prompt, " ");
119 local line = getline();
120 line = line and line:gsub("\n$","");
121 return (line and #line > 0) and line or nil;
125 local function adduser(params)
126 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
128 return false, "invalid-username";
130 return false, "invalid-hostname";
133 local host_session = prosody.hosts[host];
134 if not host_session then
135 return false, "no-such-host";
138 storagemanager.initialize_host(host);
139 local provider = host_session.users;
140 if not(provider) or provider.name == "null" then
141 usermanager.initialize_host(host);
144 local ok, errmsg = usermanager.create_user(user, password, host);
146 return false, errmsg or "creating-user-failed";
151 local function user_exists(params)
152 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
154 storagemanager.initialize_host(host);
155 local provider = prosody.hosts[host].users;
156 if not(provider) or provider.name == "null" then
157 usermanager.initialize_host(host);
160 return usermanager.user_exists(user, host);
163 local function passwd(params)
164 if not user_exists(params) then
165 return false, "no-such-user";
168 return adduser(params);
171 local function deluser(params)
172 if not user_exists(params) then
173 return false, "no-such-user";
175 local user, host = nodeprep(params.user), nameprep(params.host);
177 return usermanager.delete_user(user, host);
180 local function getpid()
181 local pidfile = config.get("*", "pidfile");
183 return false, "no-pidfile";
186 if type(pidfile) ~= "string" then
187 return false, "invalid-pidfile";
190 local modules_enabled = set.new(config.get("*", "modules_disabled"));
191 if prosody.platform ~= "posix" or modules_enabled:contains("posix") then
192 return false, "no-posix";
195 local file, err = io.open(pidfile, "r+");
197 return false, "pidfile-read-failed", err;
200 local locked, err = lfs.lock(file, "w");
203 return false, "pidfile-not-locked";
206 local pid = tonumber(file:read("*a"));
210 return false, "invalid-pid";
216 local function isrunning()
217 local ok, pid, err = getpid();
219 if pid == "pidfile-read-failed" or pid == "pidfile-not-locked" then
220 -- Report as not running, since we can't open the pidfile
221 -- (it probably doesn't exist)
226 return true, signal.kill(pid, 0) == 0;
229 local function start()
230 local ok, ret = isrunning();
235 return false, "already-running";
237 if not CFG_SOURCEDIR then
238 os.execute("./prosody");
240 os.execute(CFG_SOURCEDIR.."/../../bin/prosody");
245 local function stop()
246 local ok, ret = isrunning();
251 return false, "not-running";
254 local ok, pid = getpid()
255 if not ok then return false, pid; end
257 signal.kill(pid, signal.SIGTERM);
261 local function reload()
262 local ok, ret = isrunning();
267 return false, "not-running";
270 local ok, pid = getpid()
271 if not ok then return false, pid; end
273 signal.kill(pid, signal.SIGHUP);
278 show_message = show_message;
279 show_warning = show_message;
280 show_usage = show_usage;
284 show_yesno = show_yesno;
285 read_password = read_password;
286 show_prompt = show_prompt;
288 user_exists = user_exists;
292 isrunning = isrunning;