2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 * Copyright 1999 Peter Ganten
6 * Copyright 2002 Martin Wilck
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 #include "wine/unicode.h"
40 #include "wine/exception.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(computername);
46 /* Registry key and value names */
47 static const WCHAR ComputerW[] = {'M','a','c','h','i','n','e','\\',
48 'S','y','s','t','e','m','\\',
49 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
50 'C','o','n','t','r','o','l','\\',
51 'C','o','m','p','u','t','e','r','N','a','m','e',0};
52 static const WCHAR ActiveComputerNameW[] = {'A','c','t','i','v','e','C','o','m','p','u','t','e','r','N','a','m','e',0};
53 static const WCHAR ComputerNameW[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
55 static const char default_ComputerName[] = "WINE";
57 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
59 /* filter for page-fault exceptions */
60 static WINE_EXCEPTION_FILTER(page_fault)
62 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
63 return EXCEPTION_EXECUTE_HANDLER;
64 return EXCEPTION_CONTINUE_SEARCH;
67 /***********************************************************************
68 * dns_gethostbyname (INTERNAL)
71 * "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
73 * Wine can use this technique only if the thread-safe gethostbyname_r is available.
75 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
76 static BOOL dns_gethostbyname ( char *name, int *size )
78 struct hostent* host = NULL;
81 struct hostent hostentry;
82 int locerr = ENOBUFS, res = ENOMEM;
84 extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
88 res = gethostbyname_r ( name, &hostentry, extrabuf, ebufsize, &host, &locerr );
89 if( res != ERANGE ) break;
91 extrabuf = HeapReAlloc( GetProcessHeap(), 0, extrabuf, ebufsize ) ;
95 WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
98 size_t len = strlen ( host->h_name );
101 strcpy ( name, host->h_name );
106 memcpy ( name, host->h_name, *size );
108 SetLastError ( ERROR_MORE_DATA );
113 HeapFree( GetProcessHeap(), 0, extrabuf );
117 # define dns_gethostbyname(name,size) 0
120 /***********************************************************************
121 * dns_fqdn (INTERNAL)
123 static BOOL dns_fqdn ( char *name, int *size )
125 if ( gethostname ( name, *size + 1 ) )
130 SetLastError ( ERROR_MORE_DATA );
132 SetLastError ( ERROR_INVALID_PARAMETER );
137 if ( !dns_gethostbyname ( name, size ) )
138 *size = strlen ( name );
143 /***********************************************************************
144 * dns_hostname (INTERNAL)
146 static BOOL dns_hostname ( char *name, int *size )
149 if ( ! dns_fqdn ( name, size ) ) return FALSE;
150 c = strchr ( name, '.' );
159 /***********************************************************************
160 * dns_domainname (INTERNAL)
162 static BOOL dns_domainname ( char *name, int *size )
165 if ( ! dns_fqdn ( name, size ) ) return FALSE;
166 c = strchr ( name, '.' );
171 memmove ( name, c, *size + 1 );
176 /***********************************************************************
177 * _init_attr (INTERNAL)
179 inline static void _init_attr ( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *name )
181 attr->Length = sizeof (OBJECT_ATTRIBUTES);
182 attr->RootDirectory = 0;
183 attr->ObjectName = name;
184 attr->Attributes = 0;
185 attr->SecurityDescriptor = NULL;
186 attr->SecurityQualityOfService = NULL;
189 /***********************************************************************
192 static BOOL get_use_dns_option(void)
194 static const WCHAR NetworkW[] = {'M','a','c','h','i','n','e','\\',
195 'S','o','f','t','w','a','r','e','\\',
196 'W','i','n','e','\\','W','i','n','e','\\',
197 'C','o','n','f','i','g','\\','N','e','t','w','o','r','k',0};
198 static const WCHAR UseDNSW[] = {'U','s','e','D','n','s','C','o','m','p','u','t','e','r','N','a','m','e',0};
203 OBJECT_ATTRIBUTES attr;
204 UNICODE_STRING nameW;
207 _init_attr( &attr, &nameW );
208 RtlInitUnicodeString( &nameW, NetworkW );
210 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
212 RtlInitUnicodeString( &nameW, UseDNSW );
213 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
215 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
216 ret = IS_OPTION_TRUE( str[0] );
224 /***********************************************************************
225 * COMPUTERNAME_Init (INTERNAL)
227 void COMPUTERNAME_Init (void)
229 HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
230 OBJECT_ATTRIBUTES attr;
231 UNICODE_STRING nameW;
232 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
233 DWORD len = sizeof( buf );
234 LPWSTR computer_name = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
235 NTSTATUS st = STATUS_INTERNAL_ERROR;
238 _init_attr ( &attr, &nameW );
240 RtlInitUnicodeString( &nameW, ComputerW );
241 if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
244 attr.RootDirectory = hkey;
245 RtlInitUnicodeString( &nameW, ComputerNameW );
246 if ( (st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
249 st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
251 if ( st == STATUS_OBJECT_NAME_NOT_FOUND || ( st == STATUS_SUCCESS && get_use_dns_option()))
254 int hlen = sizeof (hbuf);
256 TRACE( "retrieving Unix host name\n" );
257 if ( gethostname ( hbuf, hlen ) )
259 strcpy ( hbuf, default_ComputerName );
260 WARN( "gethostname() error: %d, using host name %s\n", errno, hbuf );
262 hbuf[MAX_COMPUTERNAME_LENGTH] = 0;
263 dot = strchr ( hbuf, '.' );
265 hlen = strlen ( hbuf );
266 len = MultiByteToWideChar( CP_ACP, 0, hbuf, hlen + 1, computer_name, MAX_COMPUTERNAME_LENGTH + 1 )
268 if ( NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len ) != STATUS_SUCCESS )
269 WARN ( "failed to set ComputerName" );
271 else if ( st == STATUS_SUCCESS)
273 len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
274 TRACE( "found in registry\n" );
279 TRACE(" ComputerName: %s (%lu)\n", debugstr_w ( computer_name ), len / sizeof(WCHAR));
281 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
282 if ( ( st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, NULL ) )
286 RtlInitUnicodeString( &nameW, ComputerNameW );
287 st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
293 if ( st == STATUS_SUCCESS )
294 TRACE( "success\n" );
297 WARN( "status trying to set ComputerName: %lx\n", st );
298 SetLastError ( RtlNtStatusToDosError ( st ) );
303 /***********************************************************************
304 * GetComputerNameW (KERNEL32.@)
306 BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
308 UNICODE_STRING nameW;
309 OBJECT_ATTRIBUTES attr;
310 HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
311 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
312 DWORD len = sizeof( buf );
313 LPWSTR theName = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
314 NTSTATUS st = STATUS_INVALID_PARAMETER;
316 TRACE ("%p %p\n", name, size);
318 _init_attr ( &attr, &nameW );
319 RtlInitUnicodeString( &nameW, ComputerW );
320 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
323 attr.RootDirectory = hkey;
324 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
325 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
328 RtlInitUnicodeString( &nameW, ComputerNameW );
329 if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
333 len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
334 TRACE ("ComputerName is %s (length %lu)\n", debugstr_w ( theName ), len);
340 memcpy ( name, theName, *size * sizeof (WCHAR) );
343 st = STATUS_MORE_ENTRIES;
347 memcpy ( name, theName, len * sizeof (WCHAR) );
355 st = STATUS_INVALID_PARAMETER;
363 if ( st == STATUS_SUCCESS )
367 SetLastError ( RtlNtStatusToDosError ( st ) );
368 WARN ( "Status %lu reading computer name from registry\n", st );
373 /***********************************************************************
374 * GetComputerNameA (KERNEL32.@)
376 BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
378 WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
379 DWORD sizeW = MAX_COMPUTERNAME_LENGTH;
383 if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
385 len = WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, NULL, 0, NULL, 0 );
390 WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, *size, NULL, 0 );
393 SetLastError( ERROR_MORE_DATA );
398 WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, len, NULL, 0 );
406 SetLastError( ERROR_INVALID_PARAMETER );
414 /***********************************************************************
415 * GetComputerNameExA (KERNEL32.@)
417 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
420 int len = sizeof (buf), ret;
421 TRACE("%d, %p, %p\n", type, name, size);
424 case ComputerNameNetBIOS:
425 case ComputerNamePhysicalNetBIOS:
426 return GetComputerNameA (name, size);
427 case ComputerNameDnsHostname:
428 case ComputerNamePhysicalDnsHostname:
429 ret = dns_hostname (buf, &len);
431 case ComputerNameDnsDomain:
432 case ComputerNamePhysicalDnsDomain:
433 ret = dns_domainname (buf, &len);
435 case ComputerNameDnsFullyQualified:
436 case ComputerNamePhysicalDnsFullyQualified:
437 ret = dns_fqdn (buf, &len);
440 SetLastError (ERROR_INVALID_PARAMETER);
446 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
451 memcpy( name, buf, *size );
454 SetLastError( ERROR_MORE_DATA );
459 memcpy( name, buf, len );
467 SetLastError( ERROR_INVALID_PARAMETER );
477 /***********************************************************************
478 * GetComputerNameExW (KERNEL32.@)
480 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
483 int len = sizeof (buf), ret;
485 TRACE("%d, %p, %p\n", type, name, size);
488 case ComputerNameNetBIOS:
489 case ComputerNamePhysicalNetBIOS:
490 return GetComputerNameW (name, size);
491 case ComputerNameDnsHostname:
492 case ComputerNamePhysicalDnsHostname:
493 ret = dns_hostname (buf, &len);
495 case ComputerNameDnsDomain:
496 case ComputerNamePhysicalDnsDomain:
497 ret = dns_domainname (buf, &len);
499 case ComputerNameDnsFullyQualified:
500 case ComputerNamePhysicalDnsFullyQualified:
501 ret = dns_fqdn (buf, &len);
504 SetLastError (ERROR_INVALID_PARAMETER);
510 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
513 int lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
516 MultiByteToWideChar( CP_ACP, 0, buf, len, name, *size );
519 SetLastError( ERROR_MORE_DATA );
524 MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
532 SetLastError( ERROR_INVALID_PARAMETER );
541 /******************************************************************************
542 * netbios_char (INTERNAL)
544 static WCHAR netbios_char ( WCHAR wc )
546 static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
547 static const WCHAR deflt = '_';
550 if ( isalnumW ( wc ) ) return wc;
551 for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
552 if ( wc == special[i] ) return wc;
556 /******************************************************************************
557 * SetComputerNameW [KERNEL32.@]
560 * lpComputerName [I] Address of new computer name
564 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
566 UNICODE_STRING nameW;
567 OBJECT_ATTRIBUTES attr;
568 HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
569 int plen = strlenW ( lpComputerName );
571 NTSTATUS st = STATUS_INTERNAL_ERROR;
573 if (get_use_dns_option())
575 /* This check isn't necessary, but may help debugging problems. */
576 WARN( "Disabled by Wine Configuration.\n" );
577 WARN( "Set \"UseDnsComputerName\" = \"N\" in category [Network] to enable.\n" );
578 SetLastError ( ERROR_ACCESS_DENIED );
582 TRACE( "%s\n", debugstr_w (lpComputerName) );
584 /* Check parameter */
585 if ( plen > MAX_COMPUTERNAME_LENGTH )
588 /* This is NT behaviour. Win 95/98 would coerce characters. */
589 for ( i = 0; i < plen; i++ )
591 WCHAR wc = lpComputerName[i];
592 if ( wc != netbios_char( wc ) )
596 _init_attr ( &attr, &nameW );
598 RtlInitUnicodeString (&nameW, ComputerW);
599 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
601 attr.RootDirectory = hkey;
602 RtlInitUnicodeString( &nameW, ComputerNameW );
603 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
605 if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
613 if ( st == STATUS_SUCCESS )
615 TRACE( "ComputerName changed\n" );
621 SetLastError ( RtlNtStatusToDosError ( st ) );
622 WARN ( "status %lu\n", st );
627 /******************************************************************************
628 * SetComputerNameA [KERNEL32.@]
630 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
633 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
634 LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
636 MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
637 ret = SetComputerNameW( nameW );
638 HeapFree( GetProcessHeap(), 0, nameW );
642 /******************************************************************************
643 * SetComputerNameExW [KERNEL32.@]
646 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
648 TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
651 case ComputerNameNetBIOS:
652 case ComputerNamePhysicalNetBIOS:
653 return SetComputerNameW( lpComputerName );
655 SetLastError( ERROR_ACCESS_DENIED );
660 /******************************************************************************
661 * SetComputerNameExA [KERNEL32.@]
664 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
666 TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
669 case ComputerNameNetBIOS:
670 case ComputerNamePhysicalNetBIOS:
671 return SetComputerNameA( lpComputerName );
673 SetLastError( ERROR_ACCESS_DENIED );
678 /***********************************************************************
679 * DnsHostnameToComputerNameA (KERNEL32.@)
681 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR Hostname, LPSTR ComputerName,
684 FIXME("(%s, %s, %08lx): stub\n", debugstr_a(Hostname),
685 debugstr_a(ComputerName), *nSize);
686 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
690 /***********************************************************************
691 * DnsHostnameToComputerNameW (KERNEL32.@)
693 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR Hostname, LPWSTR ComputerName,
696 FIXME("(%s, %s, %08lx): stub\n", debugstr_w(Hostname),
697 debugstr_w(ComputerName), *nSize);
698 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);