diff --git a/CHANGELOG.md b/CHANGELOG.md index b20cf5c..b1a175e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## 0.2.1 + +### Added + +- `Error::code()` returns the Windows status code of an `Error::Windows`. +- `Error::is_credential_rejected()` identifies statuses where Windows rejected + the credential itself: invalid password, logon failure, account restriction, + expired password, disabled account, expired account, password must change, + or locked-out account. Callers should not retry these, because each retry + counts as another failed logon toward account lockout. + ## 0.2.0 (2026-07-15) A breaking rework into a safe, opinionated Windows client for existing SMB diff --git a/Cargo.lock b/Cargo.lock index a353d70..317e81a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" [[package]] name = "sambrs" -version = "0.2.0" +version = "0.2.1" dependencies = [ "tracing", "windows-sys", diff --git a/Cargo.toml b/Cargo.toml index df19727..e13378a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sambrs" -version = "0.2.0" +version = "0.2.1" authors = ["Samuel Van der Stappen "] license = "MIT" edition = "2024" diff --git a/src/error.rs b/src/error.rs index 866a17f..062917f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,8 @@ -use windows_sys::Win32::Foundation::{ERROR_EXTENDED_ERROR, NO_ERROR}; +use windows_sys::Win32::Foundation::{ + ERROR_ACCOUNT_DISABLED, ERROR_ACCOUNT_EXPIRED, ERROR_ACCOUNT_LOCKED_OUT, + ERROR_ACCOUNT_RESTRICTION, ERROR_EXTENDED_ERROR, ERROR_INVALID_PASSWORD, ERROR_LOGON_FAILURE, + ERROR_PASSWORD_EXPIRED, ERROR_PASSWORD_MUST_CHANGE, NO_ERROR, +}; use windows_sys::Win32::NetworkManagement::WNet; pub type Result = std::result::Result; @@ -59,6 +63,44 @@ impl std::fmt::Display for Error { } } +impl Error { + /// The Windows status code behind an [`Error::Windows`], or `None` for + /// every other variant. Provider-specific codes stay on + /// [`Error::ExtendedError`]. + #[must_use] + pub fn code(&self) -> Option { + match self { + Self::Windows(code) => Some(*code), + _ => None, + } + } + + /// Whether Windows rejected the credentials themselves: a wrong password, + /// or an account that is locked out, disabled, expired, restricted, or + /// whose password has expired or must change. + /// + /// Do not retry a rejected credential. Each attempt counts as another + /// failed logon and can lock the account out. Wait for the credential to + /// change instead. Transient failures (unreachable server, session + /// conflicts, and so on) return `false`. + #[must_use] + pub fn is_credential_rejected(&self) -> bool { + matches!( + self.code(), + Some( + ERROR_INVALID_PASSWORD + | ERROR_LOGON_FAILURE + | ERROR_ACCOUNT_RESTRICTION + | ERROR_PASSWORD_EXPIRED + | ERROR_ACCOUNT_DISABLED + | ERROR_ACCOUNT_EXPIRED + | ERROR_PASSWORD_MUST_CHANGE + | ERROR_ACCOUNT_LOCKED_OUT + ) + ) + } +} + impl std::error::Error for Error {} fn os_message(code: u32) -> String { @@ -107,3 +149,35 @@ pub(crate) fn wnet_extended_error() -> Error { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn statuses_keep_their_code_and_classify_credential_rejection() { + assert_eq!(check_wnet(NO_ERROR), Ok(())); + for (code, rejected) in [ + (86, true), // invalid password + (1326, true), // logon failure + (1327, true), // account restriction + (1330, true), // password expired + (1331, true), // account disabled + (1793, true), // account expired + (1907, true), // password must change + (1909, true), // account locked out + (53, false), // bad net path + (1219, false), // session credential conflict + (2250, false), // not connected + (1234, false), // unlisted + ] { + let error = check_wnet(code).unwrap_err(); + assert_eq!(error, Error::Windows(code)); + assert_eq!(error.code(), Some(code)); + assert_eq!(error.is_credential_rejected(), rejected, "code {code}"); + assert!(error.to_string().contains(&code.to_string())); + } + assert_eq!(Error::InteriorNul.code(), None); + assert!(!Error::InteriorNul.is_credential_rejected()); + } +}