From fd7b0c31603ec38f008cc56dda7acd999937c5dc Mon Sep 17 00:00:00 2001 From: aschumann-virtualcable Date: Thu, 1 Oct 2026 13:05:09 +0200 Subject: [PATCH] fix(windows): reset user password with NetUserSetInfo instead of NetUserChangePassword change_user_password called NetUserChangePassword with a NULL domain. Per the API docs a NULL domain means the logon domain of the calling user, which is not the local account database when the actor runs as SYSTEM on a workgroup machine. The call failed (2221 / 1351), so the Rename action ordered by the Windows Random Password OS manager never changed the password and the broker kept one that did not match the machine. Use NetUserSetInfo level 1003 on the local machine. It needs neither the old password nor a domain lookup, and it is idempotent, which matters because the Rename action is replayed on the second boot. Linux and macOS already do an administrative reset through chpasswd. Checked by hand as SYSTEM on a workgroup clone of the template: - Windows 11 (26100) and Windows 10 (19045): NetUserChangePassword with NULL domain returns 2221; with the computer name returns 0; NetUserSetInfo 1003 returns 0. - cargo test managed: 16 passed. The Windows function itself is not unit tested (see windows/system/tests.rs), end-to-end validation is on a fresh clone in demo50. --- crates/shared/src/windows/system/mod.rs | 33 ++++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/crates/shared/src/windows/system/mod.rs b/crates/shared/src/windows/system/mod.rs index 22873bd..5b22742 100644 --- a/crates/shared/src/windows/system/mod.rs +++ b/crates/shared/src/windows/system/mod.rs @@ -43,7 +43,7 @@ use windows::{ NETSETUP_DOMAIN_JOIN_IF_JOINED, NETSETUP_JOIN_DOMAIN, NETSETUP_JOIN_WITH_NEW_NAME, NetApiBufferFree, NetGetJoinInformation, NetJoinDomain, NetLocalGroupAddMembers, NetLocalGroupGetMembers, NetSetupDomainName, NetSetupUnknownStatus, - NetUserChangePassword, + NetUserSetInfo, USER_INFO_1003, }, }, Networking::WinSock::AF_INET, @@ -404,33 +404,36 @@ impl System for WindowsOperations { fn change_user_password( &self, user: &str, - old_password: &str, + _old_password: &str, new_password: &str, ) -> Result<()> { unsafe { let user_w = U16CString::from_str(user).context("invalid user UTF-16")?; - let old_w = - U16CString::from_str(old_password).context("invalid old password UTF-16")?; - let new_w = + let mut new_w = U16CString::from_str(new_password).context("invalid new password UTF-16")?; - let res = NetUserChangePassword( - PCWSTR::null(), // NULL for local machine + // NetUserChangePassword with a NULL domain resolves the caller's logon + // domain, which is not the local SAM when running as SYSTEM on a + // workgroup machine (error 2221/1351). Resetting through NetUserSetInfo + // on the local machine needs neither the old password nor that lookup. + let info = USER_INFO_1003 { + usri1003_password: PWSTR(new_w.as_mut_ptr()), + }; + + let res = NetUserSetInfo( + PCWSTR::null(), PCWSTR(user_w.as_ptr()), - PCWSTR(old_w.as_ptr()), - PCWSTR(new_w.as_ptr()), + 1003, + &info as *const USER_INFO_1003 as *const u8, + None, ); if res == 0 { Ok(()) } else { let detail = Self::format_net_error(res); - log::error!( - "NetUserChangePassword for user '{}' failed: {}", - user, - detail - ); - Err(anyhow::anyhow!("NetUserChangePassword failed: {}", detail)) + log::error!("NetUserSetInfo for user '{}' failed: {}", user, detail); + Err(anyhow::anyhow!("NetUserSetInfo failed: {}", detail)) } } }