2002-11-15 02:01:47 +01:00
|
|
|
/*
|
|
|
|
* Win32 kernel functions
|
|
|
|
*
|
|
|
|
* Copyright 1995 Martin von Loewis and Cameron Heide
|
|
|
|
* Copyright 1999 Peter Ganten
|
|
|
|
* Copyright 2002 Martin Wilck
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-11-15 02:01:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2003-11-27 01:59:36 +01:00
|
|
|
#include "wine/port.h"
|
2002-11-15 02:01:47 +01:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2002-11-15 02:01:47 +01:00
|
|
|
#include <string.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2002-11-19 00:12:15 +01:00
|
|
|
#ifdef HAVE_NETDB_H
|
2002-11-15 02:01:47 +01:00
|
|
|
#include <netdb.h>
|
2002-11-19 00:12:15 +01:00
|
|
|
#endif
|
2002-11-15 02:01:47 +01:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "ntstatus.h"
|
2005-11-28 17:32:54 +01:00
|
|
|
#define WIN32_NO_STATUS
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2002-11-15 02:01:47 +01:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
2002-11-19 00:12:15 +01:00
|
|
|
#include "winnls.h"
|
2002-11-15 02:01:47 +01:00
|
|
|
#include "winternl.h"
|
|
|
|
#include "wine/unicode.h"
|
|
|
|
#include "wine/exception.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
2005-06-14 13:42:34 +02:00
|
|
|
#include "kernel_private.h"
|
|
|
|
|
2002-11-15 02:01:47 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(computername);
|
|
|
|
|
|
|
|
/* Registry key and value names */
|
2015-11-19 12:53:33 +01:00
|
|
|
static const WCHAR ComputerW[] = {'\\','R','e','g','i','s','t','r','y','\\',
|
|
|
|
'M','a','c','h','i','n','e','\\',
|
2002-11-15 02:01:47 +01:00
|
|
|
'S','y','s','t','e','m','\\',
|
|
|
|
'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
|
|
|
|
'C','o','n','t','r','o','l','\\',
|
|
|
|
'C','o','m','p','u','t','e','r','N','a','m','e',0};
|
|
|
|
static const WCHAR ActiveComputerNameW[] = {'A','c','t','i','v','e','C','o','m','p','u','t','e','r','N','a','m','e',0};
|
|
|
|
static const WCHAR ComputerNameW[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
|
|
|
|
|
2019-12-12 19:19:12 +01:00
|
|
|
static const WCHAR default_ComputerName[] = {'W','I','N','E',0};
|
2003-08-19 05:21:04 +02:00
|
|
|
|
2002-11-15 02:01:47 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* dns_gethostbyname (INTERNAL)
|
|
|
|
*
|
|
|
|
* From hostname(1):
|
|
|
|
* "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
|
|
|
|
*
|
|
|
|
* Wine can use this technique only if the thread-safe gethostbyname_r is available.
|
|
|
|
*/
|
2019-12-12 19:19:12 +01:00
|
|
|
static void dns_gethostbyname ( char *name, int size )
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2019-12-12 19:19:12 +01:00
|
|
|
#ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
|
2002-11-15 02:01:47 +01:00
|
|
|
struct hostent* host = NULL;
|
|
|
|
char *extrabuf;
|
|
|
|
int ebufsize = 1024;
|
|
|
|
struct hostent hostentry;
|
2019-12-12 19:19:12 +01:00
|
|
|
int locerr = ENOBUFS, res;
|
2002-11-15 02:01:47 +01:00
|
|
|
|
2019-12-12 19:19:12 +01:00
|
|
|
for (;;)
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2019-12-12 19:19:12 +01:00
|
|
|
if (!(extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ))) return;
|
2002-11-15 02:01:47 +01:00
|
|
|
res = gethostbyname_r ( name, &hostentry, extrabuf, ebufsize, &host, &locerr );
|
|
|
|
if( res != ERANGE ) break;
|
|
|
|
ebufsize *= 2;
|
2019-12-12 19:19:12 +01:00
|
|
|
HeapFree( GetProcessHeap(), 0, extrabuf );
|
2002-11-15 02:01:47 +01:00
|
|
|
}
|
2007-02-22 15:35:31 +01:00
|
|
|
|
2002-11-15 02:01:47 +01:00
|
|
|
if ( res )
|
|
|
|
WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
|
2007-02-22 15:35:31 +01:00
|
|
|
else if ( !host )
|
|
|
|
WARN ("gethostbyname_r returned NULL host, locerr = %d\n", locerr);
|
2002-11-15 02:01:47 +01:00
|
|
|
else
|
2019-12-12 19:19:12 +01:00
|
|
|
if (strlen( host->h_name ) < size) strcpy( name, host->h_name );
|
2002-11-15 02:01:47 +01:00
|
|
|
|
|
|
|
HeapFree( GetProcessHeap(), 0, extrabuf );
|
|
|
|
#endif
|
2019-12-12 19:19:12 +01:00
|
|
|
}
|
2002-11-15 02:01:47 +01:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* dns_fqdn (INTERNAL)
|
|
|
|
*/
|
2019-12-12 19:19:12 +01:00
|
|
|
static BOOL dns_fqdn ( char *name, int size )
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2019-12-12 19:19:12 +01:00
|
|
|
if (gethostname( name, size ))
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
|
|
|
switch( errno )
|
|
|
|
{
|
|
|
|
case ENAMETOOLONG:
|
|
|
|
SetLastError ( ERROR_MORE_DATA );
|
2011-03-18 23:21:35 +01:00
|
|
|
break;
|
2002-11-15 02:01:47 +01:00
|
|
|
default:
|
|
|
|
SetLastError ( ERROR_INVALID_PARAMETER );
|
2011-03-18 23:21:35 +01:00
|
|
|
break;
|
2002-11-15 02:01:47 +01:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2019-12-12 19:19:12 +01:00
|
|
|
dns_gethostbyname( name, size );
|
2002-11-15 02:01:47 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* COMPUTERNAME_Init (INTERNAL)
|
|
|
|
*/
|
|
|
|
void COMPUTERNAME_Init (void)
|
|
|
|
{
|
2005-06-17 15:58:33 +02:00
|
|
|
HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
|
2002-11-15 02:01:47 +01:00
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
UNICODE_STRING nameW;
|
|
|
|
char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
|
|
|
|
DWORD len = sizeof( buf );
|
2019-12-12 19:19:12 +01:00
|
|
|
const WCHAR *computer_name = (WCHAR *)(buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
|
2002-11-15 02:01:47 +01:00
|
|
|
NTSTATUS st = STATUS_INTERNAL_ERROR;
|
2019-12-12 19:19:12 +01:00
|
|
|
char hbuf[256];
|
|
|
|
WCHAR *dot, bufW[256];
|
|
|
|
|
|
|
|
if (dns_fqdn( hbuf, sizeof(hbuf) ))
|
|
|
|
{
|
|
|
|
MultiByteToWideChar( CP_UNIXCP, 0, hbuf, -1, bufW, ARRAY_SIZE(bufW) );
|
|
|
|
dot = strchrW( bufW, '.' );
|
|
|
|
if (dot) *dot++ = 0;
|
|
|
|
else dot = bufW + strlenW(bufW);
|
|
|
|
SetComputerNameExW( ComputerNamePhysicalDnsDomain, dot );
|
|
|
|
SetComputerNameExW( ComputerNamePhysicalDnsHostname, bufW );
|
|
|
|
}
|
2002-11-15 02:01:47 +01:00
|
|
|
|
|
|
|
TRACE("(void)\n");
|
2019-12-12 19:19:12 +01:00
|
|
|
InitializeObjectAttributes( &attr, &nameW, 0, 0, NULL );
|
2002-11-15 02:01:47 +01:00
|
|
|
RtlInitUnicodeString( &nameW, ComputerW );
|
|
|
|
if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
attr.RootDirectory = hkey;
|
|
|
|
RtlInitUnicodeString( &nameW, ComputerNameW );
|
|
|
|
if ( (st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
|
|
|
|
|
2019-12-12 19:19:12 +01:00
|
|
|
if ( st != STATUS_SUCCESS)
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2019-12-12 19:19:12 +01:00
|
|
|
computer_name = default_ComputerName;
|
|
|
|
len = sizeof(default_ComputerName);
|
2002-11-15 02:01:47 +01:00
|
|
|
}
|
2006-09-09 23:07:17 +02:00
|
|
|
else
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
|
|
|
len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
|
|
|
|
}
|
2006-09-09 23:07:17 +02:00
|
|
|
|
2002-11-15 02:01:47 +01:00
|
|
|
NtClose( hsubkey );
|
2006-10-20 12:16:45 +02:00
|
|
|
TRACE(" ComputerName: %s (%u)\n", debugstr_w (computer_name), len);
|
2002-11-15 02:01:47 +01:00
|
|
|
|
|
|
|
RtlInitUnicodeString( &nameW, ActiveComputerNameW );
|
|
|
|
if ( ( st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, NULL ) )
|
|
|
|
!= STATUS_SUCCESS )
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
RtlInitUnicodeString( &nameW, ComputerNameW );
|
|
|
|
st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
|
|
|
|
|
|
|
|
out:
|
|
|
|
NtClose( hsubkey );
|
|
|
|
NtClose( hkey );
|
|
|
|
|
|
|
|
if ( st == STATUS_SUCCESS )
|
|
|
|
TRACE( "success\n" );
|
|
|
|
else
|
|
|
|
{
|
2006-10-12 22:56:29 +02:00
|
|
|
WARN( "status trying to set ComputerName: %x\n", st );
|
2002-11-15 02:01:47 +01:00
|
|
|
SetLastError ( RtlNtStatusToDosError ( st ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GetComputerNameW (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
|
|
|
|
{
|
2019-12-12 19:19:12 +01:00
|
|
|
BOOL ret = GetComputerNameExW( ComputerNameNetBIOS, name, size );
|
|
|
|
if (!ret && GetLastError() == ERROR_MORE_DATA) SetLastError( ERROR_BUFFER_OVERFLOW );
|
|
|
|
return ret;
|
2002-11-15 02:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GetComputerNameA (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
|
|
|
|
{
|
|
|
|
WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
|
2007-03-03 04:03:53 +01:00
|
|
|
DWORD sizeW = MAX_COMPUTERNAME_LENGTH + 1;
|
2004-08-11 01:43:21 +02:00
|
|
|
unsigned int len;
|
2002-11-15 02:01:47 +01:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
|
|
|
|
|
2007-02-16 00:22:18 +01:00
|
|
|
len = WideCharToMultiByte ( CP_ACP, 0, nameW, -1, NULL, 0, NULL, 0 );
|
|
|
|
/* for compatibility with Win9x */
|
2002-11-15 02:01:47 +01:00
|
|
|
__TRY
|
|
|
|
{
|
2007-03-03 04:03:53 +01:00
|
|
|
if ( *size < len )
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2007-03-03 04:03:53 +01:00
|
|
|
*size = len;
|
2010-08-22 11:40:00 +02:00
|
|
|
SetLastError( ERROR_BUFFER_OVERFLOW );
|
2002-11-15 02:01:47 +01:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
2007-03-03 04:03:53 +01:00
|
|
|
else
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2007-02-16 00:22:18 +01:00
|
|
|
WideCharToMultiByte ( CP_ACP, 0, nameW, -1, name, len, NULL, 0 );
|
2007-03-03 04:03:53 +01:00
|
|
|
*size = len - 1;
|
2002-11-15 02:01:47 +01:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-11-27 22:38:06 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* DnsHostnameToComputerNameA (KERNEL32.@)
|
|
|
|
*/
|
2005-03-30 19:04:55 +02:00
|
|
|
BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR hostname,
|
|
|
|
LPSTR computername, LPDWORD size)
|
2002-11-27 22:38:06 +01:00
|
|
|
{
|
2019-12-12 19:19:12 +01:00
|
|
|
WCHAR *hostW, nameW[MAX_COMPUTERNAME_LENGTH + 1];
|
2005-03-30 19:04:55 +02:00
|
|
|
DWORD len;
|
2019-12-12 19:19:12 +01:00
|
|
|
BOOL ret;
|
2005-03-30 19:04:55 +02:00
|
|
|
|
|
|
|
if (!hostname || !size) return FALSE;
|
2019-12-12 19:19:12 +01:00
|
|
|
len = MultiByteToWideChar( CP_ACP, 0, hostname, -1, NULL, 0 );
|
|
|
|
if (!(hostW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return FALSE;
|
|
|
|
MultiByteToWideChar( CP_ACP, 0, hostname, -1, hostW, len );
|
|
|
|
len = ARRAY_SIZE(nameW);
|
|
|
|
if ((ret = DnsHostnameToComputerNameW( hostW, nameW, &len )))
|
2005-03-30 19:04:55 +02:00
|
|
|
{
|
2019-12-12 19:19:12 +01:00
|
|
|
if (!computername || !WideCharToMultiByte( CP_ACP, 0, nameW, -1, computername, *size, NULL, NULL ))
|
|
|
|
*size = WideCharToMultiByte( CP_ACP, 0, nameW, -1, NULL, 0, NULL, NULL );
|
|
|
|
else
|
|
|
|
*size = strlen(computername);
|
2005-03-30 19:04:55 +02:00
|
|
|
}
|
2019-12-12 19:19:12 +01:00
|
|
|
HeapFree( GetProcessHeap(), 0, hostW );
|
2005-03-30 19:04:55 +02:00
|
|
|
return TRUE;
|
2002-11-27 22:38:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* DnsHostnameToComputerNameW (KERNEL32.@)
|
|
|
|
*/
|
2005-03-30 19:04:55 +02:00
|
|
|
BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR hostname,
|
|
|
|
LPWSTR computername, LPDWORD size)
|
2002-11-27 22:38:06 +01:00
|
|
|
{
|
2019-12-12 19:19:12 +01:00
|
|
|
return DnsHostnameToComputerNameExW( hostname, computername, size );
|
2002-11-27 22:38:06 +01:00
|
|
|
}
|