Skip to content

Commit b8034c0

Browse files
committed
fall back to GetUserNameExA instead like putty does
1 parent 9963571 commit b8034c0

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

pageant/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ namedpipes = [
4545
"dep:windows-strings",
4646
"windows/Win32_Security_Authentication_Identity",
4747
"windows/Win32_Security_Cryptography",
48+
"windows/Win32_System_WindowsProgramming",
4849
]

pageant/src/namedpipes.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use windows::Win32::Security::Authentication::Identity::{GetUserNameExA, NameUse
1414
use windows::Win32::Security::Cryptography::{
1515
CRYPTPROTECTMEMORY_BLOCK_SIZE, CRYPTPROTECTMEMORY_CROSS_PROCESS, CryptProtectMemory,
1616
};
17+
use windows::Win32::System::WindowsProgramming::GetUserNameA;
1718
use windows_strings::PSTR;
1819

1920
use crate::Error;
@@ -65,14 +66,17 @@ impl PageantStream {
6566
Some(PSTR(name_buf.as_mut_ptr())),
6667
&mut name_length,
6768
) {
68-
// GetUserNameExA can fail on non-domain-joined machines where UPN
69-
// (NameUserPrincipal) is not configured. Fall back to the USERNAME
70-
// environment variable, matching the behavior of the original PuTTY Pageant.
71-
if let Ok(name) = std::env::var("USERNAME") {
72-
debug!("GetUserNameExA failed, using USERNAME env var fallback: {name}");
73-
return Ok(name);
74-
}
75-
return Err(Error::from_win32());
69+
// GetUserNameExA fails on non-domain-joined machines, where no UPN
70+
// (NameUserPrincipal) is configured. Fall back to GetUserNameA
71+
// (the SAM account name), like the original PuTTY Pageant.
72+
debug!("GetUserNameExA failed, falling back to GetUserNameA");
73+
74+
let mut name_length = 0;
75+
// don't check result on this, always returns ERROR_INSUFFICIENT_BUFFER
76+
let _ = GetUserNameA(None, &mut name_length);
77+
78+
name_buf = vec![0u8; name_length as usize];
79+
GetUserNameA(Some(PSTR(name_buf.as_mut_ptr())), &mut name_length)?;
7680
}
7781

7882
//remove terminating null

0 commit comments

Comments
 (0)