From 512ec8f51880d4e9e1cc5564bd27991a2c2915d6 Mon Sep 17 00:00:00 2001 From: Jeffrey Altman Date: Tue, 12 Feb 2008 13:16:20 +0000 Subject: [PATCH] windows-afscreds-username-20080212 LICENSE MIT Use GetUserNameEx to obtain the username instead of searching the registry. Try UserPrincipalName first and fallback to SamCompatible. --- src/WINNT/client_creds/NTMakefile | 3 +- src/WINNT/client_creds/window.cpp | 51 +++++-------------------------- 2 files changed, 9 insertions(+), 45 deletions(-) diff --git a/src/WINNT/client_creds/NTMakefile b/src/WINNT/client_creds/NTMakefile index caef2be7b..3331a14b9 100644 --- a/src/WINNT/client_creds/NTMakefile +++ b/src/WINNT/client_creds/NTMakefile @@ -55,7 +55,8 @@ VCLIBS =\ ole32.lib \ mpr.lib \ userenv.lib \ - netapi32.lib + netapi32.lib \ + secur32.lib EXELIBS = \ $(DESTDIR)\lib\afs\afspioctl.lib \ diff --git a/src/WINNT/client_creds/window.cpp b/src/WINNT/client_creds/window.cpp index 84a07e5c3..29027d94a 100644 --- a/src/WINNT/client_creds/window.cpp +++ b/src/WINNT/client_creds/window.cpp @@ -19,6 +19,9 @@ extern "C" { #include "afscreds.h" +#define SECURITY_WIN32 1 +#include + /* * DEFINITIONS ________________________________________________________________ * @@ -258,7 +261,7 @@ void Main_OnInitDialog (HWND hDlg) TCHAR szVersion[256]; DWORD dwPatch = 0; - TCHAR szUser[256]; + TCHAR szUser[1024]; GetString (szVersion, IDS_UNKNOWN); GetString (szUser, IDS_UNKNOWN); @@ -276,49 +279,9 @@ void Main_OnInitDialog (HWND hDlg) RegCloseKey (hk); } - /* We should probably be using GetUserNameEx() for this */ - BOOL fFoundUserName = FALSE; - if (RegOpenKey (HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"), &hk) == 0) - { - DWORD dwSize = sizeof(szUser); - DWORD dwType = REG_SZ; - if (RegQueryValueEx (hk, TEXT("Logon User Name"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0) - fFoundUserName = TRUE; - RegCloseKey (hk); - } - if (!fFoundUserName ) - { - if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Explorer"), &hk) == 0) - { - DWORD dwSize = sizeof(szUser); - DWORD dwType = REG_SZ; - if (RegQueryValueEx (hk, TEXT("Logon User Name"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0) - fFoundUserName = TRUE; - RegCloseKey (hk); - } - } - if (!fFoundUserName ) - { - if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"), &hk) == 0) - { - DWORD dwSize = sizeof(szUser); - DWORD dwType = REG_SZ; - if (RegQueryValueEx (hk, TEXT("DefaultUserName"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0) - fFoundUserName = TRUE; - RegCloseKey (hk); - } - } - if (!fFoundUserName) - { - if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Network\\Logon"), &hk) == 0) - { - DWORD dwSize = sizeof(szUser); - DWORD dwType = REG_SZ; - if (RegQueryValueEx (hk, TEXT("UserName"), NULL, &dwType, (PBYTE)szUser, &dwSize) == 0) - fFoundUserName = TRUE; - RegCloseKey (hk); - } - } + DWORD dwUserLen = sizeof(szUser)/sizeof(TCHAR); + if (!GetUserNameEx(NameUserPrincipal, szUser, &dwUserLen)) + GetUserNameEx(NameSamCompatible, szUser, &dwUserLen); TCHAR szSource[ cchRESOURCE ]; TCHAR szTarget[ cchRESOURCE ]; -- 2.39.5