Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(ssh): support Pageant for Windows local accounts

t8y2497f0702541f
diff --git a/vendor/pageant/Cargo.toml b/vendor/pageant/Cargo.toml
index b5cc3e42b0..cdc3162cbd 100644
--- a/vendor/pageant/Cargo.toml
+++ b/vendor/pageant/Cargo.toml
@@ -91,6 +91,7 @@ features = [
"Win32_System_Memory",
"Win32_System_Threading",
"Win32_System_DataExchange",
+ "Win32_System_WindowsProgramming",
]
package = "windows"
@@ -104,5 +105,6 @@ features = [
"Win32_System_Memory",
"Win32_System_Threading",
"Win32_System_DataExchange",
+ "Win32_System_WindowsProgramming",
]
package = "windows"
diff --git a/vendor/pageant/DBX-PATCH.md b/vendor/pageant/DBX-PATCH.md
index 7b5caa59cf..d3b013a5f2 100644
--- a/vendor/pageant/DBX-PATCH.md
+++ b/vendor/pageant/DBX-PATCH.md
@@ -4,3 +4,5 @@ This is `pageant` 0.2.1 with WinRT `HSTRING` removed from Pageant window and
mapping names. It uses null-terminated UTF-16 strings instead. The Win7 target
uses Windows bindings 0.61.3 to avoid COMBASE and WinRT imports unavailable on
Windows 7, while other Windows targets retain the upstream 0.62 dependency.
+Named-pipe username lookup also falls back to the local account name when a
+user principal is unavailable or empty.
diff --git a/vendor/pageant/src/lib.rs b/vendor/pageant/src/lib.rs
index 7c1bd84503..bab8b66333 100644
--- a/vendor/pageant/src/lib.rs
+++ b/vendor/pageant/src/lib.rs
@@ -19,6 +19,9 @@ pub(crate) use windows_modern as windows;
mod error;
pub use error::*;
+#[cfg(any(windows, test))]
+mod username;
+
#[cfg(all(windows, feature = "wmmessage"))]
pub mod wmmessage;
diff --git a/vendor/pageant/src/namedpipes.rs b/vendor/pageant/src/namedpipes.rs
index 81eec49f42..a3c96426dd 100644
--- a/vendor/pageant/src/namedpipes.rs
+++ b/vendor/pageant/src/namedpipes.rs
@@ -14,9 +14,11 @@ use crate::windows::Win32::Security::Authentication::Identity::{GetUserNameExA,
use crate::windows::Win32::Security::Cryptography::{
CRYPTPROTECTMEMORY_BLOCK_SIZE, CRYPTPROTECTMEMORY_CROSS_PROCESS, CryptProtectMemory,
};
+use crate::windows::Win32::System::WindowsProgramming::GetUserNameA;
use crate::windows::core::PSTR;
use crate::Error;
+use crate::username::{UsernameLookupError, format_pipe_name, resolve_username};
/// Pageant transport stream. Implements [AsyncRead] and [AsyncWrite].
pub struct PageantStream {
@@ -48,40 +50,52 @@ impl PageantStream {
fn determine_pipe_name() -> Result<String, Error> {
let username = Self::get_username()?;
let suffix = Self::capi_obfuscate_string("Pageant")?;
- Ok(format!("\\\\.\\pipe\\pageant.{username}.{suffix}"))
+ Ok(format_pipe_name(&username, &suffix))
}
fn get_username() -> Result<String, Error> {
+ match resolve_username(Self::get_principal_username(), Self::get_local_username) {
+ Ok(username) => Ok(username),
+ Err(UsernameLookupError::Api(error)) => Err(error),
+ Err(UsernameLookupError::Invalid) => Err(Error::InvalidUsername),
+ }
+ }
+
+ fn get_principal_username() -> Result<Vec<u8>, Error> {
unsafe {
let mut name_length = 0;
-
- // don't check result on this, always returns ERROR_MORE_DATA
GetUserNameExA(NameUserPrincipal, None, &mut name_length);
+ if name_length == 0 {
+ return Ok(Vec::new());
+ }
let mut name_buf = vec![0u8; name_length as usize];
-
- if !GetUserNameExA(
+ if GetUserNameExA(
NameUserPrincipal,
Some(PSTR(name_buf.as_mut_ptr())),
&mut name_length,
) {
- // Pageant falls back to GetUserNameA here,
- // but as far as I can tell, all Versions of Windows supported by Rust today
- // should be able to answer the UserNameEx request - the comments in Pageant source
- // point to Windows XP and earlier compatibility...
- return Err(Error::from_win32());
+ Ok(name_buf)
+ } else {
+ Err(Error::from_win32())
}
+ }
+ }
- //remove terminating null
- if let Some(0) = name_buf.pop() {
- let mut name = String::from_utf8(name_buf).map_err(|_| Error::InvalidUsername)?;
- if let Some(at_index) = name.find('@') {
- name.drain(at_index..);
- }
- Ok(name)
- } else {
- Err(Error::InvalidUsername)
+ fn get_local_username() -> Result<Vec<u8>, Error> {
+ unsafe {
+ let mut name_length = 0;
+ let size_result = GetUserNameA(None, &mut name_length);
+ if name_length == 0 {
+ return match size_result {
+ Ok(()) => Ok(Vec::new()),
+ Err(error) => Err(error.into()),
+ };
}
+
+ let mut name_buf = vec![0u8; name_length as usize];
+ GetUserNameA(Some(PSTR(name_buf.as_mut_ptr())), &mut name_length)?;
+ Ok(name_buf)
}
}
diff --git a/vendor/pageant/src/username.rs b/vendor/pageant/src/username.rs
new file mode 100644
index 0000000000..e957a92ce6
--- /dev/null
+++ b/vendor/pageant/src/username.rs
@@ -0,0 +1,116 @@
+#[derive(Debug, PartialEq)]
+pub(crate) enum UsernameLookupError<E> {
+ Api(E),
+ Invalid,
+}
+
+#[derive(Debug, PartialEq)]
+enum UsernameParseError {
+ Empty,
+ Invalid,
+}
+
+pub(crate) fn resolve_username<E>(
+ principal: Result<Vec<u8>, E>,
+ local: impl FnOnce() -> Result<Vec<u8>, E>,
+) -> Result<String, UsernameLookupError<E>> {
+ if let Ok(buffer) = principal {
+ match parse_username(buffer) {
+ Ok(username) => return Ok(username),
+ Err(UsernameParseError::Invalid) => return Err(UsernameLookupError::Invalid),
+ Err(UsernameParseError::Empty) => {}
+ }
+ }
+
+ parse_username(local().map_err(UsernameLookupError::Api)?).map_err(|_| UsernameLookupError::Invalid)
+}
+
+fn parse_username(buffer: Vec<u8>) -> Result<String, UsernameParseError> {
+ let Some((&0, bytes)) = buffer.split_last() else {
+ return if buffer.is_empty() { Err(UsernameParseError::Empty) } else { Err(UsernameParseError::Invalid) };
+ };
+ let username = std::str::from_utf8(bytes).map_err(|_| UsernameParseError::Invalid)?;
+ let username = username.rsplit_once('\\').map_or(username, |(_, username)| username);
+ let username = username.split_once('@').map_or(username, |(username, _)| username);
+ if username.is_empty() { Err(UsernameParseError::Empty) } else { Ok(username.to_owned()) }
+}
+
+pub(crate) fn format_pipe_name(username: &str, suffix: &str) -> String {
+ format!("\\\\.\\pipe\\pageant.{username}.{suffix}")
+}
+
+#[cfg(test)]
+mod tests {
+ use std::cell::Cell;
+
+ use super::*;
+
+ #[derive(Debug, PartialEq)]
+ enum ApiError {
+ Principal,
+ Local,
+ }
+
+ #[test]
+ fn normalizes_principal_and_sam_names() {
+ assert_eq!(parse_username(b"[email protected]\0".to_vec()), Ok("alice".to_owned()));
+ assert_eq!(parse_username(b"DOMAIN\\alice\0".to_vec()), Ok("alice".to_owned()));
+ }
+
+ #[test]
+ fn falls_back_when_principal_is_empty() {
+ for principal in [Vec::new(), vec![0]] {
+ let local_called = Cell::new(false);
+ let username = resolve_username::<ApiError>(Ok(principal), || {
+ local_called.set(true);
+ Ok(b"local_user\0".to_vec())
+ });
+
+ assert_eq!(username, Ok("local_user".to_owned()));
+ assert!(local_called.get());
+ }
+ }
+
+ #[test]
+ fn falls_back_when_principal_lookup_fails() {
+ assert_eq!(
+ resolve_username(Err(ApiError::Principal), || Ok(b"local_user\0".to_vec())),
+ Ok("local_user".to_owned())
+ );
+ }
+
+ #[test]
+ fn preserves_the_local_api_error_when_both_lookups_fail() {
+ assert_eq!(
+ resolve_username::<ApiError>(Err(ApiError::Principal), || Err(ApiError::Local)),
+ Err(UsernameLookupError::Api(ApiError::Local))
+ );
+ }
+
+ #[test]
+ fn rejects_invalid_utf8_without_falling_back() {
+ let local_called = Cell::new(false);
+ let result = resolve_username::<ApiError>(Ok(vec![0xff, 0]), || {
+ local_called.set(true);
+ Ok(b"local_user\0".to_vec())
+ });
+
+ assert_eq!(result, Err(UsernameLookupError::Invalid));
+ assert!(!local_called.get());
+ assert_eq!(
+ resolve_username::<ApiError>(Err(ApiError::Principal), || Ok(vec![0xff, 0])),
+ Err(UsernameLookupError::Invalid)
+ );
+ }
+
+ #[test]
+ fn rejects_missing_terminators() {
+ assert_eq!(parse_username(b"alice".to_vec()), Err(UsernameParseError::Invalid));
+ assert_eq!(parse_username(Vec::new()), Err(UsernameParseError::Empty));
+ }
+
+ #[test]
+ fn keeps_the_pageant_pipe_suffix_unchanged() {
+ assert_eq!(format_pipe_name("alice", "0123abcd"), "\\\\.\\pipe\\pageant.alice.0123abcd");
+ }
+}