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";
19 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
21 local io, os = io, os;
22 local tostring, tonumber = tostring, tonumber;
24 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
26 local prosody = prosody;
30 function adduser(params)
31 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
33 return false, "invalid-username";
35 return false, "invalid-hostname";
38 local provider = prosody.hosts[host].users;
39 if not(provider) or provider.name == "null" then
40 usermanager.initialize_host(host);
42 storagemanager.initialize_host(host);
44 local ok = usermanager.create_user(user, password, host);
46 return false, "unable-to-save-data";
51 function user_exists(params)
52 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
53 local provider = prosody.hosts[host].users;
54 if not(provider) or provider.name == "null" then
55 usermanager.initialize_host(host);
57 storagemanager.initialize_host(host);
59 return usermanager.user_exists(user, host);
62 function passwd(params)
63 if not _M.user_exists(params) then
64 return false, "no-such-user";
67 return _M.adduser(params);
70 function deluser(params)
71 if not _M.user_exists(params) then
72 return false, "no-such-user";
74 params.password = nil;
76 return _M.adduser(params);
80 local pidfile = config.get("*", "core", "pidfile");
82 return false, "no-pidfile";
85 local modules_enabled = set.new(config.get("*", "core", "modules_enabled"));
86 if not modules_enabled:contains("posix") then
87 return false, "no-posix";
90 local file, err = io.open(pidfile, "r+");
92 return false, "pidfile-read-failed", err;
95 local locked, err = lfs.lock(file, "w");
98 return false, "pidfile-not-locked";
101 local pid = tonumber(file:read("*a"));
105 return false, "invalid-pid";
112 local ok, pid, err = _M.getpid();
114 if pid == "pidfile-read-failed" or pid == "pidfile-not-locked" then
115 -- Report as not running, since we can't open the pidfile
116 -- (it probably doesn't exist)
121 return true, signal.kill(pid, 0) == 0;
125 local ok, ret = _M.isrunning();
130 return false, "already-running";
132 if not CFG_SOURCEDIR then
133 os.execute("./prosody");
135 os.execute(CFG_SOURCEDIR.."/../../bin/prosody");
141 local ok, ret = _M.isrunning();
146 return false, "not-running";
149 local ok, pid = _M.getpid()
150 if not ok then return false, pid; end
152 signal.kill(pid, signal.SIGTERM);