util.prosodyctl: Handle os.execute in Lua 5.2 returning true when command terminates...
[prosody.git] / util / prosodyctl.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
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";
18 local pcall = pcall;
19 local type = type;
20
21 local nodeprep, nameprep = stringprep.nodeprep, stringprep.nameprep;
22
23 local io, os = io, os;
24 local print = print;
25 local tonumber = tonumber;
26
27 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
28
29 local _G = _G;
30 local prosody = prosody;
31
32 -- UI helpers
33 local function show_message(msg, ...)
34         print(msg:format(...));
35 end
36
37 local function show_usage(usage, desc)
38         print("Usage: ".._G.arg[0].." "..usage);
39         if desc then
40                 print(" "..desc);
41         end
42 end
43
44 local function getchar(n)
45         local stty_ret = os.execute("stty raw -echo 2>/dev/null");
46         local ok, char;
47         if stty_ret == true or stty_ret == 0 then
48                 ok, char = pcall(io.read, n or 1);
49                 os.execute("stty sane");
50         else
51                 ok, char = pcall(io.read, "*l");
52                 if ok then
53                         char = char:sub(1, n or 1);
54                 end
55         end
56         if ok then
57                 return char;
58         end
59 end
60
61 local function getline()
62         local ok, line = pcall(io.read, "*l");
63         if ok then
64                 return line;
65         end
66 end
67
68 local function getpass()
69         local stty_ret = os.execute("stty -echo 2>/dev/null");
70         if stty_ret ~= 0 then
71                 io.write("\027[08m"); -- ANSI 'hidden' text attribute
72         end
73         local ok, pass = pcall(io.read, "*l");
74         if stty_ret == 0 then
75                 os.execute("stty sane");
76         else
77                 io.write("\027[00m");
78         end
79         io.write("\n");
80         if ok then
81                 return pass;
82         end
83 end
84
85 local function show_yesno(prompt)
86         io.write(prompt, " ");
87         local choice = getchar():lower();
88         io.write("\n");
89         if not choice:match("%a") then
90                 choice = prompt:match("%[.-(%U).-%]$");
91                 if not choice then return nil; end
92         end
93         return (choice == "y");
94 end
95
96 local function read_password()
97         local password;
98         while true do
99                 io.write("Enter new password: ");
100                 password = getpass();
101                 if not password then
102                         show_message("No password - cancelled");
103                         return;
104                 end
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
108                                 return;
109                         end
110                 else
111                         break;
112                 end
113         end
114         return password;
115 end
116
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;
122 end
123
124 -- Server control
125 local function adduser(params)
126         local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
127         if not user then
128                 return false, "invalid-username";
129         elseif not host then
130                 return false, "invalid-hostname";
131         end
132
133         local host_session = prosody.hosts[host];
134         if not host_session then
135                 return false, "no-such-host";
136         end
137
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);
142         end
143
144         local ok, errmsg = usermanager.create_user(user, password, host);
145         if not ok then
146                 return false, errmsg or "creating-user-failed";
147         end
148         return true;
149 end
150
151 local function user_exists(params)
152         local user, host = nodeprep(params.user), nameprep(params.host);
153
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);
158         end
159
160         return usermanager.user_exists(user, host);
161 end
162
163 local function passwd(params)
164         if not user_exists(params) then
165                 return false, "no-such-user";
166         end
167
168         return adduser(params);
169 end
170
171 local function deluser(params)
172         if not user_exists(params) then
173                 return false, "no-such-user";
174         end
175         local user, host = nodeprep(params.user), nameprep(params.host);
176
177         return usermanager.delete_user(user, host);
178 end
179
180 local function getpid()
181         local pidfile = config.get("*", "pidfile");
182         if not pidfile then
183                 return false, "no-pidfile";
184         end
185
186         if type(pidfile) ~= "string" then
187                 return false, "invalid-pidfile";
188         end
189
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";
193         end
194
195         local file, err = io.open(pidfile, "r+");
196         if not file then
197                 return false, "pidfile-read-failed", err;
198         end
199
200         local locked, err = lfs.lock(file, "w");
201         if locked then
202                 file:close();
203                 return false, "pidfile-not-locked";
204         end
205
206         local pid = tonumber(file:read("*a"));
207         file:close();
208
209         if not pid then
210                 return false, "invalid-pid";
211         end
212
213         return true, pid;
214 end
215
216 local function isrunning()
217         local ok, pid, err = getpid();
218         if not ok then
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)
222                         return true, false;
223                 end
224                 return ok, pid;
225         end
226         return true, signal.kill(pid, 0) == 0;
227 end
228
229 local function start()
230         local ok, ret = isrunning();
231         if not ok then
232                 return ok, ret;
233         end
234         if ret then
235                 return false, "already-running";
236         end
237         if not CFG_SOURCEDIR then
238                 os.execute("./prosody");
239         else
240                 os.execute(CFG_SOURCEDIR.."/../../bin/prosody");
241         end
242         return true;
243 end
244
245 local function stop()
246         local ok, ret = isrunning();
247         if not ok then
248                 return ok, ret;
249         end
250         if not ret then
251                 return false, "not-running";
252         end
253
254         local ok, pid = getpid()
255         if not ok then return false, pid; end
256
257         signal.kill(pid, signal.SIGTERM);
258         return true;
259 end
260
261 local function reload()
262         local ok, ret = isrunning();
263         if not ok then
264                 return ok, ret;
265         end
266         if not ret then
267                 return false, "not-running";
268         end
269
270         local ok, pid = getpid()
271         if not ok then return false, pid; end
272
273         signal.kill(pid, signal.SIGHUP);
274         return true;
275 end
276
277 return {
278         show_message = show_message;
279         show_warning = show_message;
280         show_usage = show_usage;
281         getchar = getchar;
282         getline = getline;
283         getpass = getpass;
284         show_yesno = show_yesno;
285         read_password = read_password;
286         show_prompt = show_prompt;
287         adduser = adduser;
288         user_exists = user_exists;
289         passwd = passwd;
290         deluser = deluser;
291         getpid = getpid;
292         isrunning = isrunning;
293         start = start;
294         stop = stop;
295         reload = reload;
296 };