mirror of
https://github.com/morgan9e/FreeRDP
synced 2026-04-15 00:44:19 +09:00
[winpr,sysinfo] fix GetComputerNameA
* Trunctate to at most MAX_COMPUTERNAME_LENGTH characters * Fix usage of function
This commit is contained in:
@@ -1163,7 +1163,14 @@ static UINT rdpdr_process_connect(rdpdrPlugin* rdpdr)
|
||||
const char* name = freerdp_settings_get_string(settings, FreeRDP_ClientHostname);
|
||||
if (!name)
|
||||
name = freerdp_settings_get_string(settings, FreeRDP_ComputerName);
|
||||
strncpy(rdpdr->computerName, name, sizeof(rdpdr->computerName) - 1);
|
||||
if (!name)
|
||||
{
|
||||
DWORD size = sizeof(rdpdr->computerName) - 1;
|
||||
if (!GetComputerNameExA(ComputerNameNetBIOS, rdpdr->computerName, &size))
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
}
|
||||
else
|
||||
strncpy(rdpdr->computerName, name, strnlen(name, sizeof(rdpdr->computerName)));
|
||||
|
||||
for (UINT32 index = 0; index < freerdp_settings_get_uint32(settings, FreeRDP_DeviceCount);
|
||||
index++)
|
||||
@@ -1290,14 +1297,12 @@ static UINT rdpdr_send_client_name_request(rdpdrPlugin* rdpdr)
|
||||
WINPR_ASSERT(rdpdr->state == RDPDR_CHANNEL_STATE_ANNOUNCE_REPLY);
|
||||
rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_NAME_REQUEST);
|
||||
|
||||
if (!rdpdr->computerName[0])
|
||||
{
|
||||
DWORD size = sizeof(rdpdr->computerName) - 1;
|
||||
GetComputerNameA(rdpdr->computerName, &size);
|
||||
}
|
||||
const size_t len = strnlen(rdpdr->computerName, sizeof(rdpdr->computerName));
|
||||
if (len == 0)
|
||||
return ERROR_INTERNAL_ERROR;
|
||||
|
||||
WINPR_ASSERT(rdpdr->computerName);
|
||||
computerNameW = ConvertUtf8ToWCharAlloc(rdpdr->computerName, &computerNameLenW);
|
||||
computerNameW = ConvertUtf8NToWCharAlloc(rdpdr->computerName, len, &computerNameLenW);
|
||||
computerNameLenW *= sizeof(WCHAR);
|
||||
|
||||
if (computerNameLenW > 0)
|
||||
|
||||
@@ -318,17 +318,14 @@ static void settings_load_hkey_local_machine(rdpSettings* settings)
|
||||
settings_client_load_hkey_local_machine(settings);
|
||||
}
|
||||
|
||||
static BOOL settings_get_computer_name(rdpSettings* settings)
|
||||
static BOOL settings_init_computer_name(rdpSettings* settings)
|
||||
{
|
||||
CHAR computerName[256] = { 0 };
|
||||
DWORD nSize = sizeof(computerName);
|
||||
CHAR computerName[MAX_COMPUTERNAME_LENGTH + 1] = { 0 };
|
||||
DWORD nSize = MAX_COMPUTERNAME_LENGTH;
|
||||
|
||||
if (!GetComputerNameExA(ComputerNameNetBIOS, computerName, &nSize))
|
||||
return FALSE;
|
||||
|
||||
if (nSize > MAX_COMPUTERNAME_LENGTH)
|
||||
computerName[MAX_COMPUTERNAME_LENGTH] = '\0';
|
||||
|
||||
return freerdp_settings_set_string(settings, FreeRDP_ComputerName, computerName);
|
||||
}
|
||||
|
||||
@@ -986,7 +983,7 @@ rdpSettings* freerdp_settings_new(DWORD flags)
|
||||
if (!freerdp_settings_set_bool(settings, FreeRDP_SupportMultitransport, TRUE))
|
||||
goto out_fail;
|
||||
|
||||
if (!settings_get_computer_name(settings))
|
||||
if (!settings_init_computer_name(settings))
|
||||
goto out_fail;
|
||||
|
||||
if (!freerdp_settings_set_pointer_len(settings, FreeRDP_RdpServerCertificate, NULL, 1))
|
||||
@@ -1001,14 +998,6 @@ rdpSettings* freerdp_settings_new(DWORD flags)
|
||||
goto out_fail;
|
||||
}
|
||||
|
||||
{
|
||||
char ClientHostname[33] = { 0 };
|
||||
DWORD size = sizeof(ClientHostname) - 2;
|
||||
GetComputerNameA(ClientHostname, &size);
|
||||
if (!freerdp_settings_set_string(settings, FreeRDP_ClientHostname, ClientHostname))
|
||||
goto out_fail;
|
||||
}
|
||||
|
||||
/* [MS-RDPBCGR] 2.2.7.1.5 Pointer Capability Set (TS_POINTER_CAPABILITYSET)
|
||||
*
|
||||
* if we are in server mode send a reasonable large cache size,
|
||||
|
||||
@@ -105,45 +105,49 @@ static BOOL check_context_(NTLM_CONTEXT* context, const char* file, const char*
|
||||
return rc;
|
||||
}
|
||||
|
||||
static char* get_name(COMPUTER_NAME_FORMAT type)
|
||||
{
|
||||
DWORD nSize = 0;
|
||||
|
||||
if (GetComputerNameExA(type, NULL, &nSize))
|
||||
return NULL;
|
||||
|
||||
if (GetLastError() != ERROR_MORE_DATA)
|
||||
return NULL;
|
||||
|
||||
char* computerName = calloc(1, nSize);
|
||||
|
||||
if (!computerName)
|
||||
return NULL;
|
||||
|
||||
if (!GetComputerNameExA(type, computerName, &nSize))
|
||||
{
|
||||
free(computerName);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return computerName;
|
||||
}
|
||||
|
||||
static int ntlm_SetContextWorkstation(NTLM_CONTEXT* context, char* Workstation)
|
||||
{
|
||||
char* ws = Workstation;
|
||||
DWORD nSize = 0;
|
||||
CHAR* computerName = NULL;
|
||||
|
||||
WINPR_ASSERT(context);
|
||||
|
||||
if (!Workstation)
|
||||
{
|
||||
if (GetComputerNameExA(ComputerNameNetBIOS, NULL, &nSize) ||
|
||||
GetLastError() != ERROR_MORE_DATA)
|
||||
return -1;
|
||||
|
||||
computerName = calloc(nSize, sizeof(CHAR));
|
||||
|
||||
computerName = get_name(ComputerNameNetBIOS);
|
||||
if (!computerName)
|
||||
return -1;
|
||||
|
||||
if (!GetComputerNameExA(ComputerNameNetBIOS, computerName, &nSize))
|
||||
{
|
||||
free(computerName);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (nSize > MAX_COMPUTERNAME_LENGTH)
|
||||
computerName[MAX_COMPUTERNAME_LENGTH] = '\0';
|
||||
|
||||
ws = computerName;
|
||||
|
||||
if (!ws)
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t len = 0;
|
||||
context->Workstation.Buffer = ConvertUtf8ToWCharAlloc(ws, &len);
|
||||
|
||||
if (!Workstation)
|
||||
free(ws);
|
||||
free(computerName);
|
||||
|
||||
if (!context->Workstation.Buffer || (len > UINT16_MAX / sizeof(WCHAR)))
|
||||
return -1;
|
||||
|
||||
@@ -298,54 +298,55 @@ static BOOL ntlm_av_pair_add_copy(NTLM_AV_PAIR* pAvPairList, size_t cbAvPairList
|
||||
ntlm_av_pair_get_value_pointer(pAvPair), (UINT16)avLen);
|
||||
}
|
||||
|
||||
static char* get_name(COMPUTER_NAME_FORMAT type)
|
||||
{
|
||||
DWORD nSize = 0;
|
||||
|
||||
if (GetComputerNameExA(type, NULL, &nSize))
|
||||
return NULL;
|
||||
|
||||
if (GetLastError() != ERROR_MORE_DATA)
|
||||
return NULL;
|
||||
|
||||
char* computerName = calloc(1, nSize);
|
||||
|
||||
if (!computerName)
|
||||
return NULL;
|
||||
|
||||
if (!GetComputerNameExA(type, computerName, &nSize))
|
||||
{
|
||||
free(computerName);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return computerName;
|
||||
}
|
||||
|
||||
static int ntlm_get_target_computer_name(PUNICODE_STRING pName, COMPUTER_NAME_FORMAT type)
|
||||
{
|
||||
char* name = NULL;
|
||||
int status = -1;
|
||||
DWORD nSize = 0;
|
||||
CHAR* computerName = NULL;
|
||||
|
||||
WINPR_ASSERT(pName);
|
||||
|
||||
if (GetComputerNameExA(ComputerNameNetBIOS, NULL, &nSize) || GetLastError() != ERROR_MORE_DATA)
|
||||
return -1;
|
||||
|
||||
computerName = calloc(nSize, sizeof(CHAR));
|
||||
|
||||
if (!computerName)
|
||||
return -1;
|
||||
|
||||
if (!GetComputerNameExA(ComputerNameNetBIOS, computerName, &nSize))
|
||||
{
|
||||
free(computerName);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (nSize > MAX_COMPUTERNAME_LENGTH)
|
||||
computerName[MAX_COMPUTERNAME_LENGTH] = '\0';
|
||||
|
||||
name = computerName;
|
||||
|
||||
char* name = get_name(ComputerNameNetBIOS);
|
||||
if (!name)
|
||||
return -1;
|
||||
|
||||
if (type == ComputerNameNetBIOS)
|
||||
CharUpperA(name);
|
||||
CharUpperA(name);
|
||||
|
||||
size_t len = 0;
|
||||
pName->Buffer = ConvertUtf8ToWCharAlloc(name, &len);
|
||||
free(name);
|
||||
|
||||
if (!pName->Buffer || (len == 0) || (len > UINT16_MAX / sizeof(WCHAR)))
|
||||
{
|
||||
free(pName->Buffer);
|
||||
pName->Buffer = NULL;
|
||||
free(name);
|
||||
return status;
|
||||
}
|
||||
|
||||
pName->Length = (USHORT)((len) * sizeof(WCHAR));
|
||||
pName->MaximumLength = pName->Length;
|
||||
free(name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -434,22 +434,18 @@ BOOL GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize)
|
||||
|
||||
BOOL GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize)
|
||||
{
|
||||
char* dot = NULL;
|
||||
size_t length = 0;
|
||||
char hostname[256] = { 0 };
|
||||
|
||||
if (!lpnSize)
|
||||
{
|
||||
SetLastError(ERROR_BAD_ARGUMENTS);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (gethostname(hostname, sizeof(hostname)) == -1)
|
||||
char hostname[256 + 1] = { 0 };
|
||||
if (gethostname(hostname, ARRAYSIZE(hostname) - 1) == -1)
|
||||
return FALSE;
|
||||
|
||||
length = strnlen(hostname, sizeof(hostname));
|
||||
dot = strchr(hostname, '.');
|
||||
|
||||
size_t length = strnlen(hostname, MAX_COMPUTERNAME_LENGTH);
|
||||
const char* dot = strchr(hostname, '.');
|
||||
if (dot)
|
||||
length = (dot - hostname);
|
||||
|
||||
@@ -460,7 +456,7 @@ BOOL GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
CopyMemory(lpBuffer, hostname, length);
|
||||
strncpy(lpBuffer, hostname, length);
|
||||
lpBuffer[length] = '\0';
|
||||
*lpnSize = (DWORD)length;
|
||||
return TRUE;
|
||||
|
||||
@@ -222,37 +222,22 @@ static char* x509_name_parse(char* name, char* txt, size_t* length)
|
||||
return entry;
|
||||
}
|
||||
|
||||
static char* x509_get_default_name(void)
|
||||
static char* get_name(COMPUTER_NAME_FORMAT type)
|
||||
{
|
||||
CHAR* computerName = NULL;
|
||||
DWORD nSize = 0;
|
||||
|
||||
if (GetComputerNameExA(ComputerNamePhysicalDnsFullyQualified, NULL, &nSize) ||
|
||||
GetLastError() != ERROR_MORE_DATA)
|
||||
goto fallback;
|
||||
|
||||
computerName = (CHAR*)calloc(1, nSize);
|
||||
|
||||
if (!computerName)
|
||||
goto fallback;
|
||||
|
||||
if (!GetComputerNameExA(ComputerNamePhysicalDnsFullyQualified, computerName, &nSize))
|
||||
goto fallback;
|
||||
|
||||
return computerName;
|
||||
fallback:
|
||||
free(computerName);
|
||||
|
||||
if (GetComputerNameExA(ComputerNamePhysicalNetBIOS, NULL, &nSize) ||
|
||||
GetLastError() != ERROR_MORE_DATA)
|
||||
if (GetComputerNameExA(type, NULL, &nSize))
|
||||
return NULL;
|
||||
|
||||
computerName = (CHAR*)calloc(1, nSize);
|
||||
if (GetLastError() != ERROR_MORE_DATA)
|
||||
return NULL;
|
||||
|
||||
char* computerName = calloc(1, nSize);
|
||||
|
||||
if (!computerName)
|
||||
return NULL;
|
||||
|
||||
if (!GetComputerNameExA(ComputerNamePhysicalNetBIOS, computerName, &nSize))
|
||||
if (!GetComputerNameExA(type, computerName, &nSize))
|
||||
{
|
||||
free(computerName);
|
||||
return NULL;
|
||||
@@ -261,6 +246,14 @@ fallback:
|
||||
return computerName;
|
||||
}
|
||||
|
||||
static char* x509_get_default_name(void)
|
||||
{
|
||||
char* computerName = get_name(ComputerNamePhysicalDnsFullyQualified);
|
||||
if (!computerName)
|
||||
computerName = get_name(ComputerNamePhysicalNetBIOS);
|
||||
return computerName;
|
||||
}
|
||||
|
||||
static int command_line_pre_filter(void* pvctx, int index, int argc, LPSTR* argv)
|
||||
{
|
||||
MAKECERT_CONTEXT* context = pvctx;
|
||||
|
||||
Reference in New Issue
Block a user