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";
20 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
22 local io, os = io, os;
24 local tostring, tonumber = tostring, tonumber;
26 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
29 local prosody = prosody;
34 function show_message(msg, ...)
35 print(msg:format(...));
38 function show_warning(msg, ...)
39 print(msg:format(...));
42 function show_usage(usage, desc)
43 print("Usage: ".._G.arg[0].." "..usage);
50 local stty_ret = os.execute("stty raw -echo 2>/dev/null");
53 ok, char = pcall(io.read, n or 1);
54 os.execute("stty sane");
56 ok, char = pcall(io.read, "*l");
58 char = char:sub(1, n or 1);
67 local stty_ret = os.execute("stty -echo 2>/dev/null");
69 io.write("\027[08m"); -- ANSI 'hidden' text attribute
71 local ok, pass = pcall(io.read, "*l");
73 os.execute("stty sane");
83 function show_yesno(prompt)
84 io.write(prompt, " ");
85 local choice = getchar():lower();
87 if not choice:match("%a") then
88 choice = prompt:match("%[.-(%U).-%]$");
89 if not choice then return nil; end
91 return (choice == "y");
94 function read_password()
97 io.write("Enter new password: ");
100 show_message("No password - cancelled");
103 io.write("Retype new password: ");
104 if getpass() ~= password then
105 if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
116 function adduser(params)
117 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
119 return false, "invalid-username";
121 return false, "invalid-hostname";
124 local provider = prosody.hosts[host].users;
125 if not(provider) or provider.name == "null" then
126 usermanager.initialize_host(host);
128 storagemanager.initialize_host(host);
130 local ok, errmsg = usermanager.create_user(user, password, host);
132 return false, errmsg;
137 function user_exists(params)
138 local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
139 local provider = prosody.hosts[host].users;
140 if not(provider) or provider.name == "null" then
141 usermanager.initialize_host(host);
143 storagemanager.initialize_host(host);
145 return usermanager.user_exists(user, host);
148 function passwd(params)
149 if not _M.user_exists(params) then
150 return false, "no-such-user";
153 return _M.adduser(params);
156 function deluser(params)
157 if not _M.user_exists(params) then
158 return false, "no-such-user";
160 params.password = nil;
162 return _M.adduser(params);
166 local pidfile = config.get("*", "core", "pidfile");
168 return false, "no-pidfile";
171 local modules_enabled = set.new(config.get("*", "core", "modules_enabled"));
172 if not modules_enabled:contains("posix") then
173 return false, "no-posix";
176 local file, err = io.open(pidfile, "r+");
178 return false, "pidfile-read-failed", err;
181 local locked, err = lfs.lock(file, "w");
184 return false, "pidfile-not-locked";
187 local pid = tonumber(file:read("*a"));
191 return false, "invalid-pid";
198 local ok, pid, err = _M.getpid();
200 if pid == "pidfile-read-failed" or pid == "pidfile-not-locked" then
201 -- Report as not running, since we can't open the pidfile
202 -- (it probably doesn't exist)
207 return true, signal.kill(pid, 0) == 0;
211 local ok, ret = _M.isrunning();
216 return false, "already-running";
218 if not CFG_SOURCEDIR then
219 os.execute("./prosody");
221 os.execute(CFG_SOURCEDIR.."/../../bin/prosody");
227 local ok, ret = _M.isrunning();
232 return false, "not-running";
235 local ok, pid = _M.getpid()
236 if not ok then return false, pid; end
238 signal.kill(pid, signal.SIGTERM);