mirror of
https://github.com/reactos/reactos.git
synced 2025-06-11 04:47:22 +00:00
[KERNEL32] Improve SetComputerNameExW (#3171)
Reduce failures of kernel32_apitest SetComputerNameExW testcase and improve SetComputerNameExW.
This commit is contained in:
parent
8c87489a43
commit
378b26ce27
1 changed files with 71 additions and 25 deletions
|
@ -21,16 +21,19 @@
|
||||||
* PROJECT: ReactOS system libraries
|
* PROJECT: ReactOS system libraries
|
||||||
* PURPOSE: Computer name functions
|
* PURPOSE: Computer name functions
|
||||||
* FILE: dll/win32/kernel32/client/compname.c
|
* FILE: dll/win32/kernel32/client/compname.c
|
||||||
* PROGRAMER: Eric Kohl
|
* PROGRAMERS: Eric Kohl
|
||||||
|
* Katayama Hirofumi MZ
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* INCLUDES ******************************************************************/
|
/* INCLUDES ******************************************************************/
|
||||||
|
|
||||||
#include <k32.h>
|
#include <k32.h>
|
||||||
|
#include <windns.h>
|
||||||
|
|
||||||
#define NDEBUG
|
#define NDEBUG
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
|
|
||||||
|
typedef NTSTATUS (WINAPI *FN_DnsValidateName_W)(LPCWSTR, DNS_NAME_FORMAT);
|
||||||
|
|
||||||
/* FUNCTIONS *****************************************************************/
|
/* FUNCTIONS *****************************************************************/
|
||||||
|
|
||||||
|
@ -451,6 +454,31 @@ GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
BOOL
|
||||||
|
BaseVerifyDnsName(LPCWSTR lpDnsName)
|
||||||
|
{
|
||||||
|
HINSTANCE hDNSAPI;
|
||||||
|
FN_DnsValidateName_W fnValidate;
|
||||||
|
NTSTATUS Status;
|
||||||
|
BOOL ret = FALSE;
|
||||||
|
|
||||||
|
hDNSAPI = LoadLibraryW(L"dnsapi.dll");
|
||||||
|
if (hDNSAPI == NULL)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
fnValidate = (FN_DnsValidateName_W)GetProcAddress(hDNSAPI, "DnsValidateName_W");
|
||||||
|
if (fnValidate)
|
||||||
|
{
|
||||||
|
Status = (*fnValidate)(lpDnsName, DnsNameHostnameLabel);
|
||||||
|
if (Status == STATUS_SUCCESS || Status == DNS_ERROR_NON_RFC_NAME)
|
||||||
|
ret = TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
FreeLibrary(hDNSAPI);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @implemented
|
* @implemented
|
||||||
|
@ -460,40 +488,57 @@ BOOL
|
||||||
IsValidComputerName(COMPUTER_NAME_FORMAT NameType,
|
IsValidComputerName(COMPUTER_NAME_FORMAT NameType,
|
||||||
LPCWSTR lpComputerName)
|
LPCWSTR lpComputerName)
|
||||||
{
|
{
|
||||||
PWCHAR p;
|
size_t Length;
|
||||||
ULONG Length;
|
static const WCHAR s_szInvalidChars[] =
|
||||||
|
L"\"/\\[]:|<>+=;,?"
|
||||||
|
L"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
|
||||||
|
L"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
|
||||||
|
|
||||||
/* FIXME: do verification according to NameType */
|
if (lpComputerName == NULL)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
Length = 0;
|
#define MAX_COMPUTER_NAME_EX 64
|
||||||
p = (PWCHAR)lpComputerName;
|
/* Get string length */
|
||||||
|
if (!NT_SUCCESS(RtlStringCchLengthW(lpComputerName, MAX_COMPUTER_NAME_EX + 1, &Length)))
|
||||||
|
return FALSE;
|
||||||
|
#undef MAX_COMPUTER_NAME_EX
|
||||||
|
|
||||||
while (*p != 0)
|
/* An empty name is invalid, except a DNS name */
|
||||||
|
if (Length == 0 && NameType != ComputerNamePhysicalDnsDomain)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
/* Leading or trailing spaces are invalid */
|
||||||
|
if (Length > 0 &&
|
||||||
|
(lpComputerName[0] == L' ' || lpComputerName[Length - 1] == L' '))
|
||||||
{
|
{
|
||||||
if (!(iswctype(*p, _ALPHA | _DIGIT) || *p == L'!' || *p == L'@' || *p == L'#' ||
|
return FALSE;
|
||||||
*p == L'$' || *p == L'%' || *p == L'^' || *p == L'&' || *p == L'\'' ||
|
|
||||||
*p == L')' || *p == L'(' || *p == L'.' || *p == L'-' || *p == L'_' ||
|
|
||||||
*p == L'{' || *p == L'}' || *p == L'~'))
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
Length++;
|
|
||||||
p++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (NameType == ComputerNamePhysicalDnsDomain)
|
/* Check whether the name contains any invalid character */
|
||||||
return TRUE;
|
if (wcscspn(lpComputerName, s_szInvalidChars) < Length)
|
||||||
|
|
||||||
if (Length == 0)
|
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
if (NameType == ComputerNamePhysicalNetBIOS &&
|
switch (NameType)
|
||||||
Length > MAX_COMPUTERNAME_LENGTH)
|
{
|
||||||
return FALSE;
|
case ComputerNamePhysicalNetBIOS:
|
||||||
|
if (Length > MAX_COMPUTERNAME_LENGTH)
|
||||||
|
return FALSE;
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
return TRUE;
|
case ComputerNamePhysicalDnsDomain:
|
||||||
|
/* An empty DNS name is valid */
|
||||||
|
if (Length != 0)
|
||||||
|
return BaseVerifyDnsName(lpComputerName);
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
|
case ComputerNamePhysicalDnsHostname:
|
||||||
|
return BaseVerifyDnsName(lpComputerName);
|
||||||
|
|
||||||
|
default:
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static
|
static
|
||||||
BOOL
|
BOOL
|
||||||
SetComputerNameToRegistry(LPCWSTR RegistryKey,
|
SetComputerNameToRegistry(LPCWSTR RegistryKey,
|
||||||
|
@ -547,6 +592,7 @@ SetComputerNameToRegistry(LPCWSTR RegistryKey,
|
||||||
NtFlushKey(KeyHandle);
|
NtFlushKey(KeyHandle);
|
||||||
NtClose(KeyHandle);
|
NtClose(KeyHandle);
|
||||||
|
|
||||||
|
SetLastError(ERROR_SUCCESS);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -637,7 +683,7 @@ SetComputerNameExW(COMPUTER_NAME_FORMAT NameType,
|
||||||
lpBuffer);
|
lpBuffer);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
SetLastError (ERROR_INVALID_PARAMETER);
|
SetLastError(ERROR_INVALID_PARAMETER);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue