@@ -14,6 +14,7 @@ use windows::Win32::Security::Authentication::Identity::{GetUserNameExA, NameUse
1414use windows:: Win32 :: Security :: Cryptography :: {
1515 CRYPTPROTECTMEMORY_BLOCK_SIZE , CRYPTPROTECTMEMORY_CROSS_PROCESS , CryptProtectMemory ,
1616} ;
17+ use windows:: Win32 :: System :: WindowsProgramming :: GetUserNameA ;
1718use windows_strings:: PSTR ;
1819
1920use 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