2022-09-27 15:19:30 +03:00
|
|
|
#include <Windows.h>
|
|
|
|
#include <wincred.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
2022-09-27 18:15:58 +03:00
|
|
|
#include <freerdp/settings.h>
|
2022-09-27 15:19:30 +03:00
|
|
|
|
2022-09-28 09:20:24 +03:00
|
|
|
static PCWSTR ValidateString(const BYTE* pb, ULONG cb)
|
2022-09-27 18:15:58 +03:00
|
|
|
{
|
|
|
|
if (!pb || !cb)
|
|
|
|
return 0;
|
2022-09-27 15:19:30 +03:00
|
|
|
|
2022-09-27 18:15:58 +03:00
|
|
|
if (cb & (sizeof(WCHAR) - 1))
|
|
|
|
return 0;
|
2022-09-27 15:19:30 +03:00
|
|
|
|
2022-09-27 18:15:58 +03:00
|
|
|
if (*(WCHAR*)(pb + cb - sizeof(WCHAR)))
|
|
|
|
return 0;
|
2022-09-27 15:19:30 +03:00
|
|
|
|
2022-09-27 18:15:58 +03:00
|
|
|
return (PCWSTR)pb;
|
2022-09-27 15:19:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:20:24 +03:00
|
|
|
static void AddDefaultSettings_I(rdpSettings* settings, size_t idHostname, size_t idUsername,
|
2022-09-27 15:19:30 +03:00
|
|
|
size_t idPassword)
|
|
|
|
{
|
|
|
|
PCSTR ServerHostname = freerdp_settings_get_string(settings, idHostname);
|
|
|
|
|
|
|
|
if (!ServerHostname)
|
|
|
|
return;
|
|
|
|
|
2022-09-27 18:15:58 +03:00
|
|
|
BOOL bExistUserName = freerdp_settings_get_string(settings, idUsername) != 0;
|
|
|
|
BOOL bExistPassword = freerdp_settings_get_string(settings, idPassword) != 0;
|
2022-09-27 15:19:30 +03:00
|
|
|
|
|
|
|
if (bExistUserName && bExistPassword)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PCREDENTIALA Credential;
|
|
|
|
PCWSTR lpWideCharStr;
|
|
|
|
|
|
|
|
PSTR Password;
|
|
|
|
PSTR UserName;
|
|
|
|
|
|
|
|
PSTR TargetName = 0;
|
2022-09-27 18:15:58 +03:00
|
|
|
int len = _vsnprintf(TargetName, 0, "TERMSRV/%s", (va_list)&ServerHostname);
|
|
|
|
len++;
|
|
|
|
TargetName = (PSTR)malloc(len);
|
|
|
|
_vsnprintf(TargetName, len, "TERMSRV/%s", (va_list)&ServerHostname);
|
|
|
|
|
|
|
|
if (!TargetName)
|
|
|
|
return;
|
|
|
|
|
|
|
|
TargetName[len - 1] = 0;
|
2022-09-27 15:19:30 +03:00
|
|
|
|
2022-09-27 18:15:58 +03:00
|
|
|
if (!CredReadA(TargetName, CRED_TYPE_GENERIC, 0, &Credential))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!bExistPassword)
|
2022-09-27 15:19:30 +03:00
|
|
|
{
|
2022-09-27 18:15:58 +03:00
|
|
|
ULONG cch = Credential->CredentialBlobSize;
|
|
|
|
|
|
|
|
if (lpWideCharStr = ValidateString(Credential->CredentialBlob, cch))
|
2022-09-27 15:19:30 +03:00
|
|
|
{
|
2022-09-27 18:15:58 +03:00
|
|
|
Password = 0, len = 0, cch /= sizeof(WCHAR);
|
|
|
|
|
|
|
|
len = WideCharToMultiByte(CP_UTF8, 0, lpWideCharStr, cch, Password, len, 0, 0);
|
|
|
|
Password = (PSTR)malloc(len);
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, lpWideCharStr, cch, Password, len, 0, 0);
|
|
|
|
|
|
|
|
if (Password)
|
2022-09-27 15:19:30 +03:00
|
|
|
{
|
2022-09-27 18:15:58 +03:00
|
|
|
freerdp_settings_set_string(settings, idPassword, Password);
|
2022-09-27 15:19:30 +03:00
|
|
|
}
|
|
|
|
}
|
2022-09-27 18:15:58 +03:00
|
|
|
}
|
2022-09-27 15:19:30 +03:00
|
|
|
|
2022-09-27 18:15:58 +03:00
|
|
|
if (!bExistUserName)
|
|
|
|
{
|
|
|
|
if (UserName = Credential->UserName)
|
|
|
|
{
|
|
|
|
freerdp_settings_set_string(settings, idUsername, UserName);
|
|
|
|
}
|
2022-09-27 15:19:30 +03:00
|
|
|
}
|
2022-09-27 18:15:58 +03:00
|
|
|
|
|
|
|
CredFree(Credential);
|
2022-09-27 15:19:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:20:24 +03:00
|
|
|
void WINAPI AddDefaultSettings(_Inout_ rdpSettings* settings)
|
2022-09-27 15:19:30 +03:00
|
|
|
{
|
|
|
|
AddDefaultSettings_I(settings, FreeRDP_ServerHostname, FreeRDP_Username, FreeRDP_Password);
|
2022-09-27 18:15:58 +03:00
|
|
|
AddDefaultSettings_I(settings, FreeRDP_GatewayHostname, FreeRDP_GatewayUsername, FreeRDP_GatewayPassword);
|
2022-09-27 15:19:30 +03:00
|
|
|
}
|