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
43 #include "wine/unicode.h"
44 #include "wine/exception.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(computername);
50 /* Registry key and value names */
51 static const WCHAR ComputerW[] = {'M','a','c','h','i','n','e','\\',
52 'S','y','s','t','e','m','\\',
53 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
54 'C','o','n','t','r','o','l','\\',
55 'C','o','m','p','u','t','e','r','N','a','m','e',0};
56 static const WCHAR ActiveComputerNameW[] = {'A','c','t','i','v','e','C','o','m','p','u','t','e','r','N','a','m','e',0};
57 static const WCHAR ComputerNameW[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
59 static const char default_ComputerName[] = "WINE";
61 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
63 /* filter for page-fault exceptions */
64 static WINE_EXCEPTION_FILTER(page_fault)
66 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
67 return EXCEPTION_EXECUTE_HANDLER;
68 return EXCEPTION_CONTINUE_SEARCH;
71 /***********************************************************************
72 * dns_gethostbyname (INTERNAL)
75 * "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
77 * Wine can use this technique only if the thread-safe gethostbyname_r is available.
79 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
80 static BOOL dns_gethostbyname ( char *name, int *size )
82 struct hostent* host = NULL;
85 struct hostent hostentry;
86 int locerr = ENOBUFS, res = ENOMEM;
88 extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
92 res = gethostbyname_r ( name, &hostentry, extrabuf, ebufsize, &host, &locerr );
93 if( res != ERANGE ) break;
95 extrabuf = HeapReAlloc( GetProcessHeap(), 0, extrabuf, ebufsize ) ;
99 WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
102 size_t len = strlen ( host->h_name );
105 strcpy ( name, host->h_name );
110 memcpy ( name, host->h_name, *size );
112 SetLastError ( ERROR_MORE_DATA );
117 HeapFree( GetProcessHeap(), 0, extrabuf );
121 # define dns_gethostbyname(name,size) 0
124 /***********************************************************************
125 * dns_fqdn (INTERNAL)
127 static BOOL dns_fqdn ( char *name, int *size )
129 if ( gethostname ( name, *size + 1 ) )
134 SetLastError ( ERROR_MORE_DATA );
136 SetLastError ( ERROR_INVALID_PARAMETER );
141 if ( !dns_gethostbyname ( name, size ) )
142 *size = strlen ( name );
147 /***********************************************************************
148 * dns_hostname (INTERNAL)
150 static BOOL dns_hostname ( char *name, int *size )
153 if ( ! dns_fqdn ( name, size ) ) return FALSE;
154 c = strchr ( name, '.' );
163 /***********************************************************************
164 * dns_domainname (INTERNAL)
166 static BOOL dns_domainname ( char *name, int *size )
169 if ( ! dns_fqdn ( name, size ) ) return FALSE;
170 c = strchr ( name, '.' );
175 memmove ( name, c, *size + 1 );
180 /***********************************************************************
181 * _init_attr (INTERNAL)
183 inline static void _init_attr ( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *name )
185 attr->Length = sizeof (OBJECT_ATTRIBUTES);
186 attr->RootDirectory = 0;
187 attr->ObjectName = name;
188 attr->Attributes = 0;
189 attr->SecurityDescriptor = NULL;
190 attr->SecurityQualityOfService = NULL;
193 /***********************************************************************
196 static BOOL get_use_dns_option(void)
198 static const WCHAR NetworkW[] = {'M','a','c','h','i','n','e','\\',
199 'S','o','f','t','w','a','r','e','\\',
200 'W','i','n','e','\\','W','i','n','e','\\',
201 'C','o','n','f','i','g','\\','N','e','t','w','o','r','k',0};
202 static const WCHAR UseDNSW[] = {'U','s','e','D','n','s','C','o','m','p','u','t','e','r','N','a','m','e',0};
207 OBJECT_ATTRIBUTES attr;
208 UNICODE_STRING nameW;
211 _init_attr( &attr, &nameW );
212 RtlInitUnicodeString( &nameW, NetworkW );
214 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
216 RtlInitUnicodeString( &nameW, UseDNSW );
217 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
219 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
220 ret = IS_OPTION_TRUE( str[0] );
228 /***********************************************************************
229 * COMPUTERNAME_Init (INTERNAL)
231 void COMPUTERNAME_Init (void)
233 HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
234 OBJECT_ATTRIBUTES attr;
235 UNICODE_STRING nameW;
236 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
237 DWORD len = sizeof( buf );
238 LPWSTR computer_name = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
239 NTSTATUS st = STATUS_INTERNAL_ERROR;
242 _init_attr ( &attr, &nameW );
244 RtlInitUnicodeString( &nameW, ComputerW );
245 if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
248 attr.RootDirectory = hkey;
249 RtlInitUnicodeString( &nameW, ComputerNameW );
250 if ( (st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
253 st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
255 if ( st == STATUS_OBJECT_NAME_NOT_FOUND || ( st == STATUS_SUCCESS && get_use_dns_option()))
258 int hlen = sizeof (hbuf);
260 TRACE( "retrieving Unix host name\n" );
261 if ( gethostname ( hbuf, hlen ) )
263 strcpy ( hbuf, default_ComputerName );
264 WARN( "gethostname() error: %d, using host name %s\n", errno, hbuf );
266 hbuf[MAX_COMPUTERNAME_LENGTH] = 0;
267 dot = strchr ( hbuf, '.' );
269 hlen = strlen ( hbuf );
270 len = MultiByteToWideChar( CP_ACP, 0, hbuf, hlen + 1, computer_name, MAX_COMPUTERNAME_LENGTH + 1 )
272 if ( NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len ) != STATUS_SUCCESS )
273 WARN ( "failed to set ComputerName" );
275 else if ( st == STATUS_SUCCESS)
277 len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
278 TRACE( "found in registry\n" );
283 TRACE(" ComputerName: %s (%lu)\n", debugstr_w ( computer_name ), len / sizeof(WCHAR));
285 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
286 if ( ( st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, NULL ) )
290 RtlInitUnicodeString( &nameW, ComputerNameW );
291 st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
297 if ( st == STATUS_SUCCESS )
298 TRACE( "success\n" );
301 WARN( "status trying to set ComputerName: %lx\n", st );
302 SetLastError ( RtlNtStatusToDosError ( st ) );
307 /***********************************************************************
308 * GetComputerNameW (KERNEL32.@)
310 BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
312 UNICODE_STRING nameW;
313 OBJECT_ATTRIBUTES attr;
314 HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
315 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
316 DWORD len = sizeof( buf );
317 LPWSTR theName = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
318 NTSTATUS st = STATUS_INVALID_PARAMETER;
320 TRACE ("%p %p\n", name, size);
322 _init_attr ( &attr, &nameW );
323 RtlInitUnicodeString( &nameW, ComputerW );
324 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
327 attr.RootDirectory = hkey;
328 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
329 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
332 RtlInitUnicodeString( &nameW, ComputerNameW );
333 if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
337 len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
338 TRACE ("ComputerName is %s (length %lu)\n", debugstr_w ( theName ), len);
344 memcpy ( name, theName, *size * sizeof (WCHAR) );
347 st = STATUS_MORE_ENTRIES;
351 memcpy ( name, theName, len * sizeof (WCHAR) );
359 st = STATUS_INVALID_PARAMETER;
367 if ( st == STATUS_SUCCESS )
371 SetLastError ( RtlNtStatusToDosError ( st ) );
372 WARN ( "Status %lu reading computer name from registry\n", st );
377 /***********************************************************************
378 * GetComputerNameA (KERNEL32.@)
380 BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
382 WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
383 DWORD sizeW = MAX_COMPUTERNAME_LENGTH;
387 if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
389 len = WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, NULL, 0, NULL, 0 );
394 WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, *size, NULL, 0 );
397 SetLastError( ERROR_MORE_DATA );
402 WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, len, NULL, 0 );
410 SetLastError( ERROR_INVALID_PARAMETER );
418 /***********************************************************************
419 * GetComputerNameExA (KERNEL32.@)
421 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
424 int len = sizeof (buf), ret;
425 TRACE("%d, %p, %p\n", type, name, size);
428 case ComputerNameNetBIOS:
429 case ComputerNamePhysicalNetBIOS:
430 return GetComputerNameA (name, size);
431 case ComputerNameDnsHostname:
432 case ComputerNamePhysicalDnsHostname:
433 ret = dns_hostname (buf, &len);
435 case ComputerNameDnsDomain:
436 case ComputerNamePhysicalDnsDomain:
437 ret = dns_domainname (buf, &len);
439 case ComputerNameDnsFullyQualified:
440 case ComputerNamePhysicalDnsFullyQualified:
441 ret = dns_fqdn (buf, &len);
444 SetLastError (ERROR_INVALID_PARAMETER);
450 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
455 memcpy( name, buf, *size );
458 SetLastError( ERROR_MORE_DATA );
463 memcpy( name, buf, len );
471 SetLastError( ERROR_INVALID_PARAMETER );
481 /***********************************************************************
482 * GetComputerNameExW (KERNEL32.@)
484 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
487 int len = sizeof (buf), ret;
489 TRACE("%d, %p, %p\n", type, name, size);
492 case ComputerNameNetBIOS:
493 case ComputerNamePhysicalNetBIOS:
494 return GetComputerNameW (name, size);
495 case ComputerNameDnsHostname:
496 case ComputerNamePhysicalDnsHostname:
497 ret = dns_hostname (buf, &len);
499 case ComputerNameDnsDomain:
500 case ComputerNamePhysicalDnsDomain:
501 ret = dns_domainname (buf, &len);
503 case ComputerNameDnsFullyQualified:
504 case ComputerNamePhysicalDnsFullyQualified:
505 ret = dns_fqdn (buf, &len);
508 SetLastError (ERROR_INVALID_PARAMETER);
514 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
517 int lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
520 MultiByteToWideChar( CP_ACP, 0, buf, len, name, *size );
523 SetLastError( ERROR_MORE_DATA );
528 MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
536 SetLastError( ERROR_INVALID_PARAMETER );
545 /******************************************************************************
546 * netbios_char (INTERNAL)
548 static WCHAR netbios_char ( WCHAR wc )
550 static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
551 static const WCHAR deflt = '_';
554 if ( isalnumW ( wc ) ) return wc;
555 for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
556 if ( wc == special[i] ) return wc;
560 /******************************************************************************
561 * SetComputerNameW [KERNEL32.@]
564 * lpComputerName [I] Address of new computer name
568 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
570 UNICODE_STRING nameW;
571 OBJECT_ATTRIBUTES attr;
572 HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
573 int plen = strlenW ( lpComputerName );
575 NTSTATUS st = STATUS_INTERNAL_ERROR;
577 if (get_use_dns_option())
579 /* This check isn't necessary, but may help debugging problems. */
580 WARN( "Disabled by Wine Configuration.\n" );
581 WARN( "Set \"UseDnsComputerName\" = \"N\" in category [Network] to enable.\n" );
582 SetLastError ( ERROR_ACCESS_DENIED );
586 TRACE( "%s\n", debugstr_w (lpComputerName) );
588 /* Check parameter */
589 if ( plen > MAX_COMPUTERNAME_LENGTH )
592 /* This is NT behaviour. Win 95/98 would coerce characters. */
593 for ( i = 0; i < plen; i++ )
595 WCHAR wc = lpComputerName[i];
596 if ( wc != netbios_char( wc ) )
600 _init_attr ( &attr, &nameW );
602 RtlInitUnicodeString (&nameW, ComputerW);
603 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
605 attr.RootDirectory = hkey;
606 RtlInitUnicodeString( &nameW, ComputerNameW );
607 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
609 if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
617 if ( st == STATUS_SUCCESS )
619 TRACE( "ComputerName changed\n" );
625 SetLastError ( RtlNtStatusToDosError ( st ) );
626 WARN ( "status %lu\n", st );
631 /******************************************************************************
632 * SetComputerNameA [KERNEL32.@]
634 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
637 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
638 LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
640 MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
641 ret = SetComputerNameW( nameW );
642 HeapFree( GetProcessHeap(), 0, nameW );
646 /******************************************************************************
647 * SetComputerNameExW [KERNEL32.@]
650 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
652 TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
655 case ComputerNameNetBIOS:
656 case ComputerNamePhysicalNetBIOS:
657 return SetComputerNameW( lpComputerName );
659 SetLastError( ERROR_ACCESS_DENIED );
664 /******************************************************************************
665 * SetComputerNameExA [KERNEL32.@]
668 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
670 TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
673 case ComputerNameNetBIOS:
674 case ComputerNamePhysicalNetBIOS:
675 return SetComputerNameA( lpComputerName );
677 SetLastError( ERROR_ACCESS_DENIED );
682 /***********************************************************************
683 * DnsHostnameToComputerNameA (KERNEL32.@)
685 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR Hostname, LPSTR ComputerName,
688 FIXME("(%s, %s, %08lx): stub\n", debugstr_a(Hostname),
689 debugstr_a(ComputerName), *nSize);
690 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
694 /***********************************************************************
695 * DnsHostnameToComputerNameW (KERNEL32.@)
697 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR Hostname, LPWSTR ComputerName,
700 FIXME("(%s, %s, %08lx): stub\n", debugstr_w(Hostname),
701 debugstr_w(ComputerName), *nSize);
702 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);