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 */
|
|
|
|
static const WCHAR ComputerW[] = {'M','a','c','h','i','n','e','\\',
|
|
|
|
'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};
|
|
|
|
|
|
|
|
static const char default_ComputerName[] = "WINE";
|
|
|
|
|
2003-08-19 05:21:04 +02:00
|
|
|
#define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
|
|
|
|
static BOOL dns_gethostbyname ( char *name, int *size )
|
|
|
|
{
|
|
|
|
struct hostent* host = NULL;
|
|
|
|
char *extrabuf;
|
|
|
|
int ebufsize = 1024;
|
|
|
|
struct hostent hostentry;
|
|
|
|
int locerr = ENOBUFS, res = ENOMEM;
|
|
|
|
|
|
|
|
extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
|
|
|
|
|
|
|
|
while( extrabuf )
|
|
|
|
{
|
|
|
|
res = gethostbyname_r ( name, &hostentry, extrabuf, ebufsize, &host, &locerr );
|
|
|
|
if( res != ERANGE ) break;
|
|
|
|
ebufsize *= 2;
|
|
|
|
extrabuf = HeapReAlloc( GetProcessHeap(), 0, extrabuf, ebufsize ) ;
|
|
|
|
}
|
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);
|
|
|
|
res = 1;
|
|
|
|
}
|
2002-11-15 02:01:47 +01:00
|
|
|
else
|
|
|
|
{
|
2004-08-11 01:43:21 +02:00
|
|
|
int len = strlen ( host->h_name );
|
2002-11-15 02:01:47 +01:00
|
|
|
if ( len < *size )
|
|
|
|
{
|
|
|
|
strcpy ( name, host->h_name );
|
|
|
|
*size = len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy ( name, host->h_name, *size );
|
|
|
|
name[*size] = 0;
|
|
|
|
SetLastError ( ERROR_MORE_DATA );
|
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapFree( GetProcessHeap(), 0, extrabuf );
|
|
|
|
return !res;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define dns_gethostbyname(name,size) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* dns_fqdn (INTERNAL)
|
|
|
|
*/
|
|
|
|
static BOOL dns_fqdn ( char *name, int *size )
|
|
|
|
{
|
|
|
|
if ( gethostname ( name, *size + 1 ) )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !dns_gethostbyname ( name, size ) )
|
|
|
|
*size = strlen ( name );
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* dns_hostname (INTERNAL)
|
|
|
|
*/
|
|
|
|
static BOOL dns_hostname ( char *name, int *size )
|
|
|
|
{
|
|
|
|
char *c;
|
|
|
|
if ( ! dns_fqdn ( name, size ) ) return FALSE;
|
|
|
|
c = strchr ( name, '.' );
|
|
|
|
if (c)
|
|
|
|
{
|
|
|
|
*c = 0;
|
|
|
|
*size = (c - name);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* dns_domainname (INTERNAL)
|
|
|
|
*/
|
|
|
|
static BOOL dns_domainname ( char *name, int *size )
|
|
|
|
{
|
|
|
|
char *c;
|
|
|
|
if ( ! dns_fqdn ( name, size ) ) return FALSE;
|
|
|
|
c = strchr ( name, '.' );
|
|
|
|
if (c)
|
|
|
|
{
|
|
|
|
c += 1;
|
|
|
|
*size -= (c - name);
|
|
|
|
memmove ( name, c, *size + 1 );
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* _init_attr (INTERNAL)
|
|
|
|
*/
|
2007-03-17 11:22:45 +01:00
|
|
|
static inline void _init_attr ( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *name )
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
|
|
|
attr->Length = sizeof (OBJECT_ATTRIBUTES);
|
|
|
|
attr->RootDirectory = 0;
|
|
|
|
attr->ObjectName = name;
|
|
|
|
attr->Attributes = 0;
|
|
|
|
attr->SecurityDescriptor = NULL;
|
|
|
|
attr->SecurityQualityOfService = NULL;
|
|
|
|
}
|
|
|
|
|
2003-08-19 05:21:04 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* get_use_dns_option
|
|
|
|
*/
|
|
|
|
static BOOL get_use_dns_option(void)
|
|
|
|
{
|
2005-06-14 19:22:41 +02:00
|
|
|
static const WCHAR NetworkW[] = {'S','o','f','t','w','a','r','e','\\',
|
|
|
|
'W','i','n','e','\\','N','e','t','w','o','r','k',0};
|
2003-08-19 05:21:04 +02:00
|
|
|
static const WCHAR UseDNSW[] = {'U','s','e','D','n','s','C','o','m','p','u','t','e','r','N','a','m','e',0};
|
|
|
|
|
|
|
|
char tmp[80];
|
2005-06-17 15:58:33 +02:00
|
|
|
HANDLE root, hkey;
|
2003-08-19 05:21:04 +02:00
|
|
|
DWORD dummy;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
UNICODE_STRING nameW;
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
_init_attr( &attr, &nameW );
|
2009-11-30 13:57:16 +01:00
|
|
|
RtlOpenCurrentUser( KEY_READ, &root );
|
2005-06-14 19:22:41 +02:00
|
|
|
attr.RootDirectory = root;
|
2003-08-19 05:21:04 +02:00
|
|
|
RtlInitUnicodeString( &nameW, NetworkW );
|
|
|
|
|
2005-06-14 19:22:41 +02:00
|
|
|
/* @@ Wine registry key: HKCU\Software\Wine\Network */
|
2009-11-30 13:57:16 +01:00
|
|
|
if (!NtOpenKey( &hkey, KEY_READ, &attr ))
|
2003-08-19 05:21:04 +02:00
|
|
|
{
|
|
|
|
RtlInitUnicodeString( &nameW, UseDNSW );
|
|
|
|
if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
|
|
|
|
{
|
|
|
|
WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
|
|
|
|
ret = IS_OPTION_TRUE( str[0] );
|
|
|
|
}
|
|
|
|
NtClose( hkey );
|
|
|
|
}
|
2005-06-14 19:22:41 +02:00
|
|
|
NtClose( root );
|
2003-08-19 05:21:04 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-15 02:01:47 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* 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 );
|
|
|
|
LPWSTR computer_name = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
|
|
|
|
NTSTATUS st = STATUS_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
TRACE("(void)\n");
|
|
|
|
_init_attr ( &attr, &nameW );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
2004-12-06 12:38:25 +01:00
|
|
|
if ( st != STATUS_SUCCESS || get_use_dns_option() )
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
|
|
|
char hbuf[256];
|
|
|
|
int hlen = sizeof (hbuf);
|
|
|
|
char *dot;
|
|
|
|
TRACE( "retrieving Unix host name\n" );
|
|
|
|
if ( gethostname ( hbuf, hlen ) )
|
|
|
|
{
|
|
|
|
strcpy ( hbuf, default_ComputerName );
|
|
|
|
WARN( "gethostname() error: %d, using host name %s\n", errno, hbuf );
|
|
|
|
}
|
|
|
|
hbuf[MAX_COMPUTERNAME_LENGTH] = 0;
|
|
|
|
dot = strchr ( hbuf, '.' );
|
|
|
|
if ( dot ) *dot = 0;
|
|
|
|
hlen = strlen ( hbuf );
|
2008-11-28 14:27:04 +01:00
|
|
|
len = MultiByteToWideChar( CP_UNIXCP, 0, hbuf, hlen + 1, computer_name, MAX_COMPUTERNAME_LENGTH + 1 )
|
2002-11-15 02:01:47 +01:00
|
|
|
* sizeof( WCHAR );
|
|
|
|
if ( NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len ) != STATUS_SUCCESS )
|
2004-01-27 01:01:43 +01:00
|
|
|
WARN ( "failed to set ComputerName\n" );
|
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 ));
|
|
|
|
TRACE( "found in registry\n" );
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
UNICODE_STRING nameW;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
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
|
|
|
char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
|
|
|
|
DWORD len = sizeof( buf );
|
|
|
|
LPWSTR theName = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
|
|
|
|
NTSTATUS st = STATUS_INVALID_PARAMETER;
|
2010-08-22 11:40:00 +02:00
|
|
|
DWORD err = ERROR_SUCCESS;
|
2002-11-15 02:01:47 +01:00
|
|
|
|
|
|
|
TRACE ("%p %p\n", name, size);
|
|
|
|
|
|
|
|
_init_attr ( &attr, &nameW );
|
|
|
|
RtlInitUnicodeString( &nameW, ComputerW );
|
2009-11-30 13:57:16 +01:00
|
|
|
if ( ( st = NtOpenKey( &hkey, KEY_READ, &attr ) ) != STATUS_SUCCESS )
|
2010-08-22 11:40:00 +02:00
|
|
|
{
|
|
|
|
err = RtlNtStatusToDosError ( st );
|
2002-11-15 02:01:47 +01:00
|
|
|
goto out;
|
2010-08-22 11:40:00 +02:00
|
|
|
}
|
2002-11-15 02:01:47 +01:00
|
|
|
|
|
|
|
attr.RootDirectory = hkey;
|
|
|
|
RtlInitUnicodeString( &nameW, ActiveComputerNameW );
|
2009-11-30 13:57:16 +01:00
|
|
|
if ( ( st = NtOpenKey( &hsubkey, KEY_READ, &attr ) ) != STATUS_SUCCESS )
|
2010-08-22 11:40:00 +02:00
|
|
|
{
|
|
|
|
err = RtlNtStatusToDosError ( st );
|
2002-11-15 02:01:47 +01:00
|
|
|
goto out;
|
2010-08-22 11:40:00 +02:00
|
|
|
}
|
2002-11-15 02:01:47 +01:00
|
|
|
|
|
|
|
RtlInitUnicodeString( &nameW, ComputerNameW );
|
|
|
|
if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
|
|
|
|
!= STATUS_SUCCESS )
|
2010-08-22 11:40:00 +02:00
|
|
|
{
|
|
|
|
err = RtlNtStatusToDosError ( st );
|
2002-11-15 02:01:47 +01:00
|
|
|
goto out;
|
2010-08-22 11:40:00 +02:00
|
|
|
}
|
2002-11-15 02:01:47 +01:00
|
|
|
|
|
|
|
len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
|
2006-10-12 22:56:29 +02:00
|
|
|
TRACE ("ComputerName is %s (length %u)\n", debugstr_w ( theName ), len);
|
2002-11-15 02:01:47 +01:00
|
|
|
|
2007-02-16 00:22:18 +01:00
|
|
|
if ( *size < len + 1 )
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2007-02-16 00:22:18 +01:00
|
|
|
*size = len + 1;
|
2010-08-22 11:40:00 +02:00
|
|
|
err = ERROR_BUFFER_OVERFLOW;
|
2002-11-15 02:01:47 +01:00
|
|
|
}
|
2007-02-16 00:22:18 +01:00
|
|
|
else
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2007-02-16 00:22:18 +01:00
|
|
|
memcpy ( name, theName, len * sizeof (WCHAR) );
|
|
|
|
name[len] = 0;
|
|
|
|
*size = len;
|
2002-11-15 02:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
NtClose ( hsubkey );
|
|
|
|
NtClose ( hkey );
|
|
|
|
|
2010-08-22 11:40:00 +02:00
|
|
|
if ( err == ERROR_SUCCESS )
|
2002-11-15 02:01:47 +01:00
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
{
|
2010-08-22 11:40:00 +02:00
|
|
|
SetLastError ( err );
|
2006-10-12 22:56:29 +02:00
|
|
|
WARN ( "Status %u reading computer name from registry\n", st );
|
2002-11-15 02:01:47 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GetComputerNameExA (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
|
|
|
|
{
|
|
|
|
char buf[256];
|
2007-02-21 18:13:25 +01:00
|
|
|
int len = sizeof(buf) - 1, ret;
|
2002-11-15 02:01:47 +01:00
|
|
|
TRACE("%d, %p, %p\n", type, name, size);
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case ComputerNameNetBIOS:
|
|
|
|
case ComputerNamePhysicalNetBIOS:
|
2010-08-22 11:40:00 +02:00
|
|
|
ret = GetComputerNameA (name, size);
|
|
|
|
if (!ret && GetLastError() == ERROR_BUFFER_OVERFLOW)
|
|
|
|
SetLastError( ERROR_MORE_DATA );
|
|
|
|
return ret;
|
|
|
|
|
2002-11-15 02:01:47 +01:00
|
|
|
case ComputerNameDnsHostname:
|
|
|
|
case ComputerNamePhysicalDnsHostname:
|
|
|
|
ret = dns_hostname (buf, &len);
|
|
|
|
break;
|
|
|
|
case ComputerNameDnsDomain:
|
|
|
|
case ComputerNamePhysicalDnsDomain:
|
|
|
|
ret = dns_domainname (buf, &len);
|
|
|
|
break;
|
|
|
|
case ComputerNameDnsFullyQualified:
|
|
|
|
case ComputerNamePhysicalDnsFullyQualified:
|
|
|
|
ret = dns_fqdn (buf, &len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SetLastError (ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ret )
|
|
|
|
{
|
|
|
|
TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
|
2007-02-16 00:22:18 +01:00
|
|
|
if ( *size < len + 1 )
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2007-02-16 00:22:18 +01:00
|
|
|
*size = len + 1;
|
|
|
|
SetLastError( ERROR_MORE_DATA );
|
|
|
|
ret = FALSE;
|
2002-11-15 02:01:47 +01:00
|
|
|
}
|
2007-02-16 00:22:18 +01:00
|
|
|
else
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2007-02-16 00:22:18 +01:00
|
|
|
memcpy( name, buf, len );
|
|
|
|
name[len] = 0;
|
|
|
|
*size = len;
|
|
|
|
ret = TRUE;
|
2002-11-15 02:01:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* GetComputerNameExW (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
|
|
|
|
{
|
|
|
|
char buf[256];
|
2007-02-21 18:13:25 +01:00
|
|
|
int len = sizeof(buf) - 1, ret;
|
2002-11-15 02:01:47 +01:00
|
|
|
|
|
|
|
TRACE("%d, %p, %p\n", type, name, size);
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case ComputerNameNetBIOS:
|
|
|
|
case ComputerNamePhysicalNetBIOS:
|
2010-08-22 11:40:00 +02:00
|
|
|
ret = GetComputerNameW (name, size);
|
|
|
|
if (!ret && GetLastError() == ERROR_BUFFER_OVERFLOW)
|
|
|
|
SetLastError( ERROR_MORE_DATA );
|
|
|
|
return ret;
|
2002-11-15 02:01:47 +01:00
|
|
|
case ComputerNameDnsHostname:
|
|
|
|
case ComputerNamePhysicalDnsHostname:
|
|
|
|
ret = dns_hostname (buf, &len);
|
|
|
|
break;
|
|
|
|
case ComputerNameDnsDomain:
|
|
|
|
case ComputerNamePhysicalDnsDomain:
|
|
|
|
ret = dns_domainname (buf, &len);
|
|
|
|
break;
|
|
|
|
case ComputerNameDnsFullyQualified:
|
|
|
|
case ComputerNamePhysicalDnsFullyQualified:
|
|
|
|
ret = dns_fqdn (buf, &len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SetLastError (ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ret )
|
|
|
|
{
|
2007-02-16 00:22:18 +01:00
|
|
|
unsigned int lenW;
|
|
|
|
|
2002-11-15 02:01:47 +01:00
|
|
|
TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
|
2007-02-16 00:22:18 +01:00
|
|
|
|
|
|
|
lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
|
|
|
|
if ( *size < lenW + 1 )
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2007-02-16 00:22:18 +01:00
|
|
|
*size = lenW + 1;
|
|
|
|
SetLastError( ERROR_MORE_DATA );
|
|
|
|
ret = FALSE;
|
2002-11-15 02:01:47 +01:00
|
|
|
}
|
2007-02-16 00:22:18 +01:00
|
|
|
else
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
2007-02-16 00:22:18 +01:00
|
|
|
MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
|
|
|
|
name[lenW] = 0;
|
|
|
|
*size = lenW;
|
|
|
|
ret = TRUE;
|
2002-11-15 02:01:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* netbios_char (INTERNAL)
|
|
|
|
*/
|
|
|
|
static WCHAR netbios_char ( WCHAR wc )
|
|
|
|
{
|
|
|
|
static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
|
|
|
|
static const WCHAR deflt = '_';
|
2004-08-11 01:43:21 +02:00
|
|
|
unsigned int i;
|
2002-11-15 02:01:47 +01:00
|
|
|
|
|
|
|
if ( isalnumW ( wc ) ) return wc;
|
|
|
|
for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
|
|
|
|
if ( wc == special[i] ) return wc;
|
|
|
|
return deflt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* SetComputerNameW [KERNEL32.@]
|
|
|
|
*
|
2005-11-08 12:01:03 +01:00
|
|
|
* Set a new NetBIOS name for the local computer.
|
|
|
|
*
|
2002-11-15 02:01:47 +01:00
|
|
|
* PARAMS
|
|
|
|
* lpComputerName [I] Address of new computer name
|
|
|
|
*
|
2005-11-08 12:01:03 +01:00
|
|
|
* RETURNS
|
|
|
|
* Success: TRUE
|
|
|
|
* Failure: FALSE
|
2002-11-15 02:01:47 +01:00
|
|
|
*/
|
|
|
|
BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
|
|
|
|
{
|
|
|
|
UNICODE_STRING nameW;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
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
|
|
|
int plen = strlenW ( lpComputerName );
|
|
|
|
int i;
|
|
|
|
NTSTATUS st = STATUS_INTERNAL_ERROR;
|
|
|
|
|
2003-08-19 05:21:04 +02:00
|
|
|
if (get_use_dns_option())
|
2002-11-15 02:01:47 +01:00
|
|
|
{
|
|
|
|
/* This check isn't necessary, but may help debugging problems. */
|
|
|
|
WARN( "Disabled by Wine Configuration.\n" );
|
2010-06-24 20:15:06 +02:00
|
|
|
WARN( "Set \"UseDnsComputerName\" = \"N\" in HKCU\\Software\\Wine\\Network to enable.\n" );
|
2002-11-15 02:01:47 +01:00
|
|
|
SetLastError ( ERROR_ACCESS_DENIED );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE( "%s\n", debugstr_w (lpComputerName) );
|
|
|
|
|
|
|
|
/* Check parameter */
|
|
|
|
if ( plen > MAX_COMPUTERNAME_LENGTH )
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* This is NT behaviour. Win 95/98 would coerce characters. */
|
|
|
|
for ( i = 0; i < plen; i++ )
|
|
|
|
{
|
|
|
|
WCHAR wc = lpComputerName[i];
|
|
|
|
if ( wc != netbios_char( wc ) )
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
_init_attr ( &attr, &nameW );
|
|
|
|
|
|
|
|
RtlInitUnicodeString (&nameW, ComputerW);
|
|
|
|
if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
|
|
|
|
goto out;
|
|
|
|
attr.RootDirectory = hkey;
|
|
|
|
RtlInitUnicodeString( &nameW, ComputerNameW );
|
|
|
|
if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
|
|
|
|
goto out;
|
|
|
|
if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
|
|
|
|
!= STATUS_SUCCESS )
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
out:
|
|
|
|
NtClose( hsubkey );
|
|
|
|
NtClose( hkey );
|
|
|
|
|
|
|
|
if ( st == STATUS_SUCCESS )
|
|
|
|
{
|
|
|
|
TRACE( "ComputerName changed\n" );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError ( RtlNtStatusToDosError ( st ) );
|
2006-10-12 22:56:29 +02:00
|
|
|
WARN ( "status %u\n", st );
|
2002-11-15 02:01:47 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* SetComputerNameA [KERNEL32.@]
|
2005-11-08 12:01:03 +01:00
|
|
|
*
|
|
|
|
* See SetComputerNameW.
|
2002-11-15 02:01:47 +01:00
|
|
|
*/
|
|
|
|
BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
|
|
|
|
LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
|
|
|
|
|
|
|
|
MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
|
|
|
|
ret = SetComputerNameW( nameW );
|
|
|
|
HeapFree( GetProcessHeap(), 0, nameW );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* SetComputerNameExW [KERNEL32.@]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
|
|
|
|
{
|
|
|
|
TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case ComputerNameNetBIOS:
|
|
|
|
case ComputerNamePhysicalNetBIOS:
|
|
|
|
return SetComputerNameW( lpComputerName );
|
|
|
|
default:
|
|
|
|
SetLastError( ERROR_ACCESS_DENIED );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* SetComputerNameExA [KERNEL32.@]
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
|
|
|
|
{
|
|
|
|
TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
|
|
|
|
switch( type )
|
|
|
|
{
|
|
|
|
case ComputerNameNetBIOS:
|
|
|
|
case ComputerNamePhysicalNetBIOS:
|
|
|
|
return SetComputerNameA( lpComputerName );
|
|
|
|
default:
|
|
|
|
SetLastError( ERROR_ACCESS_DENIED );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2005-03-30 19:04:55 +02:00
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
FIXME("(%s, %p, %p): stub\n", debugstr_a(hostname), computername, size);
|
|
|
|
|
|
|
|
if (!hostname || !size) return FALSE;
|
|
|
|
len = lstrlenA(hostname);
|
|
|
|
|
|
|
|
if (len > MAX_COMPUTERNAME_LENGTH)
|
|
|
|
len = MAX_COMPUTERNAME_LENGTH;
|
|
|
|
|
2014-01-29 23:41:28 +01:00
|
|
|
if (*size < len + 1)
|
2005-03-30 19:04:55 +02:00
|
|
|
{
|
|
|
|
*size = len;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!computername) return FALSE;
|
|
|
|
|
|
|
|
memcpy( computername, hostname, len );
|
2014-01-29 23:41:28 +01:00
|
|
|
computername[len] = 0;
|
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
|
|
|
{
|
2005-03-30 19:04:55 +02:00
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
FIXME("(%s, %p, %p): stub\n", debugstr_w(hostname), computername, size);
|
|
|
|
|
|
|
|
if (!hostname || !size) return FALSE;
|
|
|
|
len = lstrlenW(hostname);
|
|
|
|
|
|
|
|
if (len > MAX_COMPUTERNAME_LENGTH)
|
|
|
|
len = MAX_COMPUTERNAME_LENGTH;
|
|
|
|
|
2014-01-29 23:41:28 +01:00
|
|
|
if (*size < len + 1)
|
2005-03-30 19:04:55 +02:00
|
|
|
{
|
|
|
|
*size = len;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!computername) return FALSE;
|
|
|
|
|
|
|
|
memcpy( computername, hostname, len * sizeof(WCHAR) );
|
2014-01-29 23:41:28 +01:00
|
|
|
computername[len] = 0;
|
2005-03-30 19:04:55 +02:00
|
|
|
return TRUE;
|
2002-11-27 22:38:06 +01:00
|
|
|
}
|