2 local config = require "core.configmanager";
3 local encodings = require "util.encodings";
4 local stringprep = encodings.stringprep;
5 local usermanager = require "core.usermanager";
6 local signal = require "util.signal";
8 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
10 local io, os = io, os;
11 local tostring, tonumber = tostring, tonumber;
14 function adduser(params)
15 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
17 return false, "invalid-username";
19 return false, "invalid-hostname";
22 local ok = usermanager.create_user(user, password, host);
24 return false, "unable-to-save-data";
29 function user_exists(params)
30 return usermanager.user_exists(params.user, params.host);
33 function passwd(params)
34 if not _M.user_exists(params) then
35 return false, "no-such-user";
38 return _M.adduser(params);
41 function deluser(params)
42 if not _M.user_exists(params) then
43 return false, "no-such-user";
45 params.password = nil;
47 return _M.adduser(params);
51 local pidfile = config.get("*", "core", "pidfile");
53 return false, "no-pidfile";
56 local file, err = io.open(pidfile);
58 return false, "pidfile-read-failed", ret;
61 local pid = tonumber(file:read("*a"));
65 return false, "invalid-pid";
72 local ok, pid, err = _M.getpid();
74 if pid == "pidfile-read-failed" then
75 -- Report as not running, since we can't open the pidfile
76 -- (it probably doesn't exist)
81 return true, signal.kill(pid, 0) == 0;
85 local ok, ret = _M.isrunning();
90 return false, "already-running";
92 if not CFG_SOURCEDIR then
93 os.execute("./prosody");
94 elseif CFG_SOURCEDIR:match("^/usr/local") then
95 os.execute("/usr/local/bin/prosody");
97 os.execute("prosody");
103 local ok, ret = _M.isrunning();
108 return false, "not-running";
111 local ok, pid = _M.getpid()
112 if not ok then return false, pid; end
114 signal.kill(pid, signal.SIGTERM);