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 usermanager = require "core.usermanager";
14 local signal = require "util.signal";
15 local lfs = require "lfs";
17 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
19 local io, os = io, os;
20 local tostring, tonumber = tostring, tonumber;
22 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
24 local prosody = prosody;
28 function adduser(params)
29 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
31 return false, "invalid-username";
33 return false, "invalid-hostname";
36 local provider = prosody.hosts[host].users;
37 if not(provider) or provider.name == "null" then
38 usermanager.initialize_host(host);
41 local ok = usermanager.create_user(user, password, host);
43 return false, "unable-to-save-data";
48 function user_exists(params)
49 local provider = prosody.hosts[params.host].users;
50 if not(provider) or provider.name == "null" then
51 usermanager.initialize_host(params.host);
54 return usermanager.user_exists(params.user, params.host);
57 function passwd(params)
58 if not _M.user_exists(params) then
59 return false, "no-such-user";
62 return _M.adduser(params);
65 function deluser(params)
66 if not _M.user_exists(params) then
67 return false, "no-such-user";
69 params.password = nil;
71 return _M.adduser(params);
75 local pidfile = config.get("*", "core", "pidfile");
77 return false, "no-pidfile";
80 local file, err = io.open(pidfile, "r+");
82 return false, "pidfile-read-failed", err;
85 local locked, err = lfs.lock(file, "w");
88 return false, "pidfile-not-locked";
91 local pid = tonumber(file:read("*a"));
95 return false, "invalid-pid";
102 local ok, pid, err = _M.getpid();
104 if pid == "pidfile-read-failed" or pid == "pidfile-not-locked" then
105 -- Report as not running, since we can't open the pidfile
106 -- (it probably doesn't exist)
111 return true, signal.kill(pid, 0) == 0;
115 local ok, ret = _M.isrunning();
120 return false, "already-running";
122 if not CFG_SOURCEDIR then
123 os.execute("./prosody");
125 os.execute(CFG_SOURCEDIR.."/../../bin/prosody");
131 local ok, ret = _M.isrunning();
136 return false, "not-running";
139 local ok, pid = _M.getpid()
140 if not ok then return false, pid; end
142 signal.kill(pid, signal.SIGTERM);