comparison mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 171:d15c7d86db11

mod_adhoc_cmd_admin: Check whether action is to be performed on the host the command was sent to.
author Florian Zeitz <florob@babelmonkeys.de>
date Sat, 12 Jun 2010 23:58:10 +0200
parents b3a68e71b8a1
children 648c24de9040
comparison
equal deleted inserted replaced
170:0d438a7ac4fc 171:d15c7d86db11
13 13
14 local usermanager_user_exists = require "core.usermanager".user_exists; 14 local usermanager_user_exists = require "core.usermanager".user_exists;
15 local usermanager_create_user = require "core.usermanager".create_user; 15 local usermanager_create_user = require "core.usermanager".create_user;
16 local usermanager_get_password = require "core.usermanager".get_password; 16 local usermanager_get_password = require "core.usermanager".get_password;
17 local usermanager_set_password = require "core.usermanager".set_password or 17 local usermanager_set_password = require "core.usermanager".set_password or
18 function (username, host, password) return usermanager_create_user(username, password, host) end; 18 function (username, password, host) return usermanager_create_user(username, password, host) end;
19 local is_admin = require "core.usermanager".is_admin; 19 local is_admin = require "core.usermanager".is_admin;
20 20
21 local rm_load_roster = require "core.rostermanager".load_roster; 21 local rm_load_roster = require "core.rostermanager".load_roster;
22 22
23 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid"; 23 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
147 if data.action == "cancel" then 147 if data.action == "cancel" then
148 return { status = "canceled" }; 148 return { status = "canceled" };
149 end 149 end
150 local fields = add_user_layout:data(data.form); 150 local fields = add_user_layout:data(data.form);
151 local username, host, resource = jid.split(fields.accountjid); 151 local username, host, resource = jid.split(fields.accountjid);
152 if (fields["password"] == fields["password-verify"]) and username and host and host == data.to then 152 if data.to ~= host then
153 return { status = "completed", error = { message = "Trying to add a user on " .. host .. " but command was sent to " .. data.to}};
154 end
155 if (fields["password"] == fields["password-verify"]) and username and host then
153 if usermanager_user_exists(username, host) then 156 if usermanager_user_exists(username, host) then
154 return { status = "completed", error = { message = "Account already exists" } }; 157 return { status = "completed", error = { message = "Account already exists" } };
155 else 158 else
156 if usermanager_create_user(username, fields.password, host) then 159 if usermanager_create_user(username, fields.password, host) then
157 module:log("info", "Created new account " .. username.."@"..host); 160 module:log("info", "Created new account " .. username.."@"..host);
175 if data.action == "cancel" then 178 if data.action == "cancel" then
176 return { status = "canceled" }; 179 return { status = "canceled" };
177 end 180 end
178 local fields = change_user_password_layout:data(data.form); 181 local fields = change_user_password_layout:data(data.form);
179 local username, host, resource = jid.split(fields.accountjid); 182 local username, host, resource = jid.split(fields.accountjid);
180 if usermanager_user_exists(username, host) and usermanager_set_password(username, host, fields.password) then 183 if data.to ~= host then
184 return { status = "completed", error = { message = "Trying to change the password of a user on " .. host .. " but command was sent to " .. data.to}};
185 end
186 if usermanager_user_exists(username, host) and usermanager_set_password(username, fields.password, host) then
181 return { status = "completed", info = "Password successfully changed" }; 187 return { status = "completed", info = "Password successfully changed" };
182 else 188 else
183 return { status = "completed", error = { message = "User does not exist" } }; 189 return { status = "completed", error = { message = "User does not exist" } };
184 end 190 end
185 else 191 else
208 local fields = delete_user_layout:data(data.form); 214 local fields = delete_user_layout:data(data.form);
209 local failed = {}; 215 local failed = {};
210 local succeeded = {}; 216 local succeeded = {};
211 for _, aJID in ipairs(fields.accountjids) do 217 for _, aJID in ipairs(fields.accountjids) do
212 local username, host, resource = jid.split(aJID); 218 local username, host, resource = jid.split(aJID);
213 if usermanager_user_exists(username, host) and disconnect_user(aJID) and usermanager_create_user(username, nil, host) then 219 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) and usermanager_create_user(username, nil, host) then
214 module:log("debug", "User " .. aJID .. " has been deleted"); 220 module:log("debug", "User " .. aJID .. " has been deleted");
215 succeeded[#succeeded+1] = aJID; 221 succeeded[#succeeded+1] = aJID;
216 else 222 else
217 module:log("debug", "Tried to delete non-existant user "..aJID); 223 module:log("debug", "Tried to delete non-existant user "..aJID);
218 failed[#failed+1] = aJID; 224 failed[#failed+1] = aJID;
232 if data.action == "cancel" then 238 if data.action == "cancel" then
233 return { status = "canceled" }; 239 return { status = "canceled" };
234 end 240 end
235 241
236 local fields = end_user_session_layout:data(data.form); 242 local fields = end_user_session_layout:data(data.form);
237 243 local failed = {};
244 local succeeded = {};
238 for _, aJID in ipairs(fields.accountjids) do 245 for _, aJID in ipairs(fields.accountjids) do
239 disconnect_user(aJID); 246 local username, host, resource = jid.split(aJID);
240 end 247 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) then
241 return { status = "completed", info = "User(s) have been disconnected" }; 248 succeeded[#succeeded+1] = aJID;
249 else
250 failed[#failed+1] = aJID;
251 end
252 end
253 return {status = "completed", info = (#succeeded ~= 0 and
254 "The following accounts were successfully disconnected:\n"..t_concat(succeeded, "\n").."\n" or "")..
255 (#failed ~= 0 and
256 "The following accounts could not be disconnected:\n"..t_concat(failed, "\n") or "") };
242 else 257 else
243 return { status = "executing", form = end_user_session_layout }, "executing"; 258 return { status = "executing", form = end_user_session_layout }, "executing";
244 end 259 end
245 end 260 end
246 261
251 end 266 end
252 local fields = get_user_password_layout:data(data.form); 267 local fields = get_user_password_layout:data(data.form);
253 local user, host, resource = jid.split(fields.accountjid); 268 local user, host, resource = jid.split(fields.accountjid);
254 local accountjid = ""; 269 local accountjid = "";
255 local password = ""; 270 local password = "";
256 if usermanager_user_exists(user, host) then 271 if host ~= data.to then
272 return { status = "completed", error = { message = "Tried to get password for a user on " .. host .. " but command was sent to " .. data.to } };
273 elseif usermanager_user_exists(user, host) then
257 accountjid = fields.accountjid; 274 accountjid = fields.accountjid;
258 password = usermanager_get_password(user, host); 275 password = usermanager_get_password(user, host);
259 else 276 else
260 return { status = "completed", error = { message = "User does not exist" } }; 277 return { status = "completed", error = { message = "User does not exist" } };
261 end 278 end
272 end 289 end
273 290
274 local fields = add_user_layout:data(data.form); 291 local fields = add_user_layout:data(data.form);
275 292
276 local user, host, resource = jid.split(fields.accountjid); 293 local user, host, resource = jid.split(fields.accountjid);
277 if not usermanager_user_exists(user, host) then 294 if host ~= data.to then
295 return { status = "completed", error = { message = "Tried to get roster for a user on " .. host .. " but command was sent to " .. data.to } };
296 elseif not usermanager_user_exists(user, host) then
278 return { status = "completed", error = { message = "User does not exist" } }; 297 return { status = "completed", error = { message = "User does not exist" } };
279 end 298 end
280 local roster = rm_load_roster(user, host); 299 local roster = rm_load_roster(user, host);
281 300
282 local query = st.stanza("query", { xmlns = "jabber:iq:roster" }); 301 local query = st.stanza("query", { xmlns = "jabber:iq:roster" });
313 end 332 end
314 333
315 local fields = get_user_stats_layout:data(data.form); 334 local fields = get_user_stats_layout:data(data.form);
316 335
317 local user, host, resource = jid.split(fields.accountjid); 336 local user, host, resource = jid.split(fields.accountjid);
318 if not usermanager_user_exists(user, host) then 337 if host ~= data.to then
338 return { status = "completed", error = { message = "Tried to get stats for a user on " .. host .. " but command was sent to " .. data.to } };
339 elseif not usermanager_user_exists(user, host) then
319 return { status = "completed", error = { message = "User does not exist" } }; 340 return { status = "completed", error = { message = "User does not exist" } };
320 end 341 end
321 local roster = rm_load_roster(user, host); 342 local roster = rm_load_roster(user, host);
322 local rostersize = 0; 343 local rostersize = 0;
323 local IPs = ""; 344 local IPs = "";