Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sambrs"
version = "0.2.0"
version = "0.2.1"
authors = ["Samuel Van der Stappen <shogun_einst.0i@icloud.com>"]
license = "MIT"
edition = "2024"
Expand Down
76 changes: 75 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -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<T> = std::result::Result<T, Error>;
Expand Down Expand Up @@ -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<u32> {
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 {
Expand Down Expand Up @@ -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());
}
}
Loading