Merge 0.9->0.10
[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 tostring, tonumber = tostring, tonumber;
26
27 local CFG_SOURCEDIR = _G.CFG_SOURCEDIR;
28
29 local _G = _G;
30 local prosody = prosody;
31
32 module "prosodyctl"
33
34 -- UI helpers
35 function show_message(msg, ...)
36         print(msg:format(...));
37 end
38
39 function show_warning(msg, ...)
40         print(msg:format(...));
41 end
42
43 function show_usage(usage, desc)
44         print("Usage: ".._G.arg[0].." "..usage);
45         if desc then
46                 print(" "..desc);
47         end
48 end
49
50 function getchar(n)
51         local stty_ret = os.execute("stty raw -echo 2>/dev/null");
52         local ok, char;
53         if stty_ret == 0 then
54                 ok, char = pcall(io.read, n or 1);
55                 os.execute("stty sane");
56         else
57                 ok, char = pcall(io.read, "*l");
58                 if ok then
59                         char = char:sub(1, n or 1);
60                 end
61         end
62         if ok then
63                 return char;
64         end
65 end
66
67 function getline()
68         local ok, line = pcall(io.read, "*l");
69         if ok then
70                 return line;
71         end
72 end
73
74 function getpass()
75         local stty_ret = os.execute("stty -echo 2>/dev/null");
76         if stty_ret ~= 0 then
77                 io.write("\027[08m"); -- ANSI 'hidden' text attribute
78         end
79         local ok, pass = pcall(io.read, "*l");
80         if stty_ret == 0 then
81                 os.execute("stty sane");
82         else
83                 io.write("\027[00m");
84         end
85         io.write("\n");
86         if ok then
87                 return pass;
88         end
89 end
90
91 function show_yesno(prompt)
92         io.write(prompt, " ");
93         local choice = getchar():lower();
94         io.write("\n");
95         if not choice:match("%a") then
96                 choice = prompt:match("%[.-(%U).-%]$");
97                 if not choice then return nil; end
98         end
99         return (choice == "y");
100 end
101
102 function read_password()
103         local password;
104         while true do
105                 io.write("Enter new password: ");
106                 password = getpass();
107                 if not password then
108                         show_message("No password - cancelled");
109                         return;
110                 end
111                 io.write("Retype new password: ");
112                 if getpass() ~= password then
113                         if not show_yesno [=[Passwords did not match, try again? [Y/n]]=] then
114                                 return;
115                         end
116                 else
117                         break;
118                 end
119         end
120         return password;
121 end
122
123 function show_prompt(prompt)
124         io.write(prompt, " ");
125         local line = getline();
126         line = line and line:gsub("\n$","");
127         return (line and #line > 0) and line or nil;
128 end
129
130 -- Server control
131 function adduser(params)
132         local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
133         if not user then
134                 return false, "invalid-username";
135         elseif not host then
136                 return false, "invalid-hostname";
137         end
138
139         local host_session = prosody.hosts[host];
140         if not host_session then
141                 return false, "no-such-host";
142         end
143
144         storagemanager.initialize_host(host);
145         local provider = host_session.users;
146         if not(provider) or provider.name == "null" then
147                 usermanager.initialize_host(host);
148         end
149
150         local ok, errmsg = usermanager.create_user(user, password, host);
151         if not ok then
152                 return false, errmsg;
153         end
154         return true;
155 end
156
157 function user_exists(params)
158         local user, host, password = nodeprep(params.user), nameprep(params.host), params.password;
159
160         storagemanager.initialize_host(host);
161         local provider = prosody.hosts[host].users;
162         if not(provider) or provider.name == "null" then
163                 usermanager.initialize_host(host);
164         end
165
166         return usermanager.user_exists(user, host);
167 end
168
169 function passwd(params)
170         if not _M.user_exists(params) then
171                 return false, "no-such-user";
172         end
173
174         return _M.adduser(params);
175 end
176
177 function deluser(params)
178         if not _M.user_exists(params) then
179                 return false, "no-such-user";
180         end
181         local user, host = nodeprep(params.user), nameprep(params.host);
182
183         return usermanager.delete_user(user, host);
184 end
185
186 function getpid()
187         local pidfile = config.get("*", "pidfile");
188         if not pidfile then
189                 return false, "no-pidfile";
190         end
191
192         local modules_enabled = set.new(config.get("*", "modules_enabled"));
193         if not modules_enabled:contains("posix") then
194                 return false, "no-posix";
195         end
196
197         local file, err = io.open(pidfile, "r+");
198         if not file then
199                 return false, "pidfile-read-failed", err;
200         end
201
202         local locked, err = lfs.lock(file, "w");
203         if locked then
204                 file:close();
205                 return false, "pidfile-not-locked";
206         end
207
208         local pid = tonumber(file:read("*a"));
209         file:close();
210
211         if not pid then
212                 return false, "invalid-pid";
213         end
214
215         return true, pid;
216 end
217
218 function isrunning()
219         local ok, pid, err = _M.getpid();
220         if not ok then
221                 if pid == "pidfile-read-failed" or pid == "pidfile-not-locked" then
222                         -- Report as not running, since we can't open the pidfile
223                         -- (it probably doesn't exist)
224                         return true, false;
225                 end
226                 return ok, pid;
227         end
228         return true, signal.kill(pid, 0) == 0;
229 end
230
231 function start()
232         local ok, ret = _M.isrunning();
233         if not ok then
234                 return ok, ret;
235         end
236         if ret then
237                 return false, "already-running";
238         end
239         if not CFG_SOURCEDIR then
240                 os.execute("./prosody");
241         else
242                 os.execute(CFG_SOURCEDIR.."/../../bin/prosody");
243         end
244         return true;
245 end
246
247 function stop()
248         local ok, ret = _M.isrunning();
249         if not ok then
250                 return ok, ret;
251         end
252         if not ret then
253                 return false, "not-running";
254         end
255
256         local ok, pid = _M.getpid()
257         if not ok then return false, pid; end
258
259         signal.kill(pid, signal.SIGTERM);
260         return true;
261 end
262
263 function reload()
264         local ok, ret = _M.isrunning();
265         if not ok then
266                 return ok, ret;
267         end
268         if not ret then
269                 return false, "not-running";
270         end
271
272         local ok, pid = _M.getpid()
273         if not ok then return false, pid; end
274
275         signal.kill(pid, signal.SIGHUP);
276         return true;
277 end
278
279 return _M;