core.usermanager, various modules: Disconnect other resources on password change (thanks waqas) (fixes #512)
authorKim Alvefur <zash@zash.se>
Fri, 28 Jul 2017 13:15:29 +0200
changeset 8195 4354f556c5db
parent 8194 d43012448c1f
child 8196 bb0118e46c45
core.usermanager, various modules: Disconnect other resources on password change (thanks waqas) (fixes #512)
core/usermanager.lua
plugins/mod_admin_adhoc.lua
plugins/mod_admin_telnet.lua
plugins/mod_auth_internal_hashed.lua
plugins/mod_c2s.lua
plugins/mod_register.lua
--- a/core/usermanager.lua	Sun Aug 06 13:27:47 2017 +0200
+++ b/core/usermanager.lua	Fri Jul 28 13:15:29 2017 +0200
@@ -76,8 +76,12 @@
 	return hosts[host].users.get_password(username);
 end
 
-local function set_password(username, password, host)
-	return hosts[host].users.set_password(username, password);
+local function set_password(username, password, host, resource)
+	local ok, err = hosts[host].users.set_password(username, password);
+	if ok then
+		prosody.events.fire_event("user-password-changed", { username = username, host = host, resource = resource });
+	end
+	return ok, err;
 end
 
 local function user_exists(username, host)
--- a/plugins/mod_admin_adhoc.lua	Sun Aug 06 13:27:47 2017 +0200
+++ b/plugins/mod_admin_adhoc.lua	Fri Jul 28 13:15:29 2017 +0200
@@ -97,7 +97,7 @@
 	if module_host ~= host then
 		return { status = "completed", error = { message = "Trying to change the password of a user on " .. host .. " but command was sent to " .. module_host}};
 	end
-	if usermanager_user_exists(username, host) and usermanager_set_password(username, fields.password, host) then
+	if usermanager_user_exists(username, host) and usermanager_set_password(username, fields.password, host, nil) then
 		return { status = "completed", info = "Password successfully changed" };
 	else
 		return { status = "completed", error = { message = "User does not exist" } };
--- a/plugins/mod_admin_telnet.lua	Sun Aug 06 13:27:47 2017 +0200
+++ b/plugins/mod_admin_telnet.lua	Fri Jul 28 13:15:29 2017 +0200
@@ -1030,7 +1030,7 @@
 	elseif not um.user_exists(username, host) then
 		return nil, "No such user";
 	end
-	local ok, err = um.set_password(username, password, host);
+	local ok, err = um.set_password(username, password, host, nil);
 	if ok then
 		return true, "User password changed";
 	else
--- a/plugins/mod_auth_internal_hashed.lua	Sun Aug 06 13:27:47 2017 +0200
+++ b/plugins/mod_auth_internal_hashed.lua	Fri Jul 28 13:15:29 2017 +0200
@@ -120,7 +120,9 @@
 			local credentials = accounts:get(username);
 			if not credentials then return; end
 			if credentials.password then
-				usermanager.set_password(username, credentials.password, host);
+				if provider.set_password(username, credentials.password) == nil then
+					return nil, "Auth failed. Could not set hashed password from plaintext.";
+				end
 				credentials = accounts:get(username);
 				if not credentials then return; end
 			end
--- a/plugins/mod_c2s.lua	Sun Aug 06 13:27:47 2017 +0200
+++ b/plugins/mod_c2s.lua	Fri Jul 28 13:15:29 2017 +0200
@@ -203,6 +203,18 @@
 	end
 end, 200);
 
+module:hook_global("user-password-changed", function(event)
+	local username, host, resource = event.username, event.host, event.resource;
+	local user = hosts[host].sessions[username];
+	if user and user.sessions then
+		for r, session in pairs(user.sessions) do
+			if r ~= resource then
+				session:close{ condition = "reset", text = "Password changed" };
+			end
+		end
+	end
+end, 200);
+
 --- Port listener
 function listener.onconnect(conn)
 	local session = sm_new_session(conn);
--- a/plugins/mod_register.lua	Sun Aug 06 13:27:47 2017 +0200
+++ b/plugins/mod_register.lua	Fri Jul 28 13:15:29 2017 +0200
@@ -130,7 +130,7 @@
 			local password = query:get_child_text("password");
 			if username and password then
 				if username == session.username then
-					if usermanager_set_password(username, password, session.host) then
+					if usermanager_set_password(username, password, session.host, session.resource) then
 						session.send(st.reply(stanza));
 					else
 						-- TODO unable to write file, file may be locked, etc, what's the correct error?