# HG changeset patch # User Kim Alvefur # Date 1501240529 -7200 # Node ID 4354f556c5db9ce66011d5e268ce5e1e21bc8dac # Parent d43012448c1ff7d0dc65eab0c406cdf2859c66bd core.usermanager, various modules: Disconnect other resources on password change (thanks waqas) (fixes #512) diff -r d43012448c1f -r 4354f556c5db core/usermanager.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) diff -r d43012448c1f -r 4354f556c5db plugins/mod_admin_adhoc.lua --- 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" } }; diff -r d43012448c1f -r 4354f556c5db plugins/mod_admin_telnet.lua --- 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 diff -r d43012448c1f -r 4354f556c5db plugins/mod_auth_internal_hashed.lua --- 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 diff -r d43012448c1f -r 4354f556c5db plugins/mod_c2s.lua --- 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); diff -r d43012448c1f -r 4354f556c5db plugins/mod_register.lua --- 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?