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
24 #include "wine/port.h"
38 #define WIN32_NO_STATUS
44 #include "wine/unicode.h"
45 #include "wine/exception.h"
47 #include "wine/debug.h"
49 #include "kernel_private.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(computername);
53 /* Registry key and value names */
54 static const WCHAR ComputerW[] = {'M','a','c','h','i','n','e','\\',
55 'S','y','s','t','e','m','\\',
56 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
57 'C','o','n','t','r','o','l','\\',
58 'C','o','m','p','u','t','e','r','N','a','m','e',0};
59 static const WCHAR ActiveComputerNameW[] = {'A','c','t','i','v','e','C','o','m','p','u','t','e','r','N','a','m','e',0};
60 static const WCHAR ComputerNameW[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
62 static const char default_ComputerName[] = "WINE";
64 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
66 /* filter for page-fault exceptions */
67 static WINE_EXCEPTION_FILTER(page_fault)
69 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
70 return EXCEPTION_EXECUTE_HANDLER;
71 return EXCEPTION_CONTINUE_SEARCH;
74 /***********************************************************************
75 * dns_gethostbyname (INTERNAL)
78 * "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
80 * Wine can use this technique only if the thread-safe gethostbyname_r is available.
82 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
83 static BOOL dns_gethostbyname ( char *name, int *size )
85 struct hostent* host = NULL;
88 struct hostent hostentry;
89 int locerr = ENOBUFS, res = ENOMEM;
91 extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
95 res = gethostbyname_r ( name, &hostentry, extrabuf, ebufsize, &host, &locerr );
96 if( res != ERANGE ) break;
98 extrabuf = HeapReAlloc( GetProcessHeap(), 0, extrabuf, ebufsize ) ;
102 WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
105 int len = strlen ( host->h_name );
108 strcpy ( name, host->h_name );
113 memcpy ( name, host->h_name, *size );
115 SetLastError ( ERROR_MORE_DATA );
120 HeapFree( GetProcessHeap(), 0, extrabuf );
124 # define dns_gethostbyname(name,size) 0
127 /***********************************************************************
128 * dns_fqdn (INTERNAL)
130 static BOOL dns_fqdn ( char *name, int *size )
132 if ( gethostname ( name, *size + 1 ) )
137 SetLastError ( ERROR_MORE_DATA );
139 SetLastError ( ERROR_INVALID_PARAMETER );
144 if ( !dns_gethostbyname ( name, size ) )
145 *size = strlen ( name );
150 /***********************************************************************
151 * dns_hostname (INTERNAL)
153 static BOOL dns_hostname ( char *name, int *size )
156 if ( ! dns_fqdn ( name, size ) ) return FALSE;
157 c = strchr ( name, '.' );
166 /***********************************************************************
167 * dns_domainname (INTERNAL)
169 static BOOL dns_domainname ( char *name, int *size )
172 if ( ! dns_fqdn ( name, size ) ) return FALSE;
173 c = strchr ( name, '.' );
178 memmove ( name, c, *size + 1 );
183 /***********************************************************************
184 * _init_attr (INTERNAL)
186 inline static void _init_attr ( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *name )
188 attr->Length = sizeof (OBJECT_ATTRIBUTES);
189 attr->RootDirectory = 0;
190 attr->ObjectName = name;
191 attr->Attributes = 0;
192 attr->SecurityDescriptor = NULL;
193 attr->SecurityQualityOfService = NULL;
196 /***********************************************************************
199 static BOOL get_use_dns_option(void)
201 static const WCHAR NetworkW[] = {'S','o','f','t','w','a','r','e','\\',
202 'W','i','n','e','\\','N','e','t','w','o','r','k',0};
203 static const WCHAR UseDNSW[] = {'U','s','e','D','n','s','C','o','m','p','u','t','e','r','N','a','m','e',0};
208 OBJECT_ATTRIBUTES attr;
209 UNICODE_STRING nameW;
212 _init_attr( &attr, &nameW );
213 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
214 attr.RootDirectory = root;
215 RtlInitUnicodeString( &nameW, NetworkW );
217 /* @@ Wine registry key: HKCU\Software\Wine\Network */
218 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
220 RtlInitUnicodeString( &nameW, UseDNSW );
221 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
223 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
224 ret = IS_OPTION_TRUE( str[0] );
233 /***********************************************************************
234 * COMPUTERNAME_Init (INTERNAL)
236 void COMPUTERNAME_Init (void)
238 HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
239 OBJECT_ATTRIBUTES attr;
240 UNICODE_STRING nameW;
241 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
242 DWORD len = sizeof( buf );
243 LPWSTR computer_name = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
244 NTSTATUS st = STATUS_INTERNAL_ERROR;
247 _init_attr ( &attr, &nameW );
249 RtlInitUnicodeString( &nameW, ComputerW );
250 if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
253 attr.RootDirectory = hkey;
254 RtlInitUnicodeString( &nameW, ComputerNameW );
255 if ( (st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
258 st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
260 if ( st != STATUS_SUCCESS || get_use_dns_option() )
263 int hlen = sizeof (hbuf);
265 TRACE( "retrieving Unix host name\n" );
266 if ( gethostname ( hbuf, hlen ) )
268 strcpy ( hbuf, default_ComputerName );
269 WARN( "gethostname() error: %d, using host name %s\n", errno, hbuf );
271 hbuf[MAX_COMPUTERNAME_LENGTH] = 0;
272 dot = strchr ( hbuf, '.' );
274 hlen = strlen ( hbuf );
275 len = MultiByteToWideChar( CP_ACP, 0, hbuf, hlen + 1, computer_name, MAX_COMPUTERNAME_LENGTH + 1 )
277 if ( NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len ) != STATUS_SUCCESS )
278 WARN ( "failed to set ComputerName\n" );
280 else if ( st == STATUS_SUCCESS)
282 len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
283 TRACE( "found in registry\n" );
288 TRACE(" ComputerName: %s (%lu)\n", debugstr_w ( computer_name ), len / sizeof(WCHAR));
290 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
291 if ( ( st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, NULL ) )
295 RtlInitUnicodeString( &nameW, ComputerNameW );
296 st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
302 if ( st == STATUS_SUCCESS )
303 TRACE( "success\n" );
306 WARN( "status trying to set ComputerName: %lx\n", st );
307 SetLastError ( RtlNtStatusToDosError ( st ) );
312 /***********************************************************************
313 * GetComputerNameW (KERNEL32.@)
315 BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
317 UNICODE_STRING nameW;
318 OBJECT_ATTRIBUTES attr;
319 HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
320 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
321 DWORD len = sizeof( buf );
322 LPWSTR theName = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
323 NTSTATUS st = STATUS_INVALID_PARAMETER;
325 TRACE ("%p %p\n", name, size);
327 _init_attr ( &attr, &nameW );
328 RtlInitUnicodeString( &nameW, ComputerW );
329 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
332 attr.RootDirectory = hkey;
333 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
334 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
337 RtlInitUnicodeString( &nameW, ComputerNameW );
338 if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
342 len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
343 TRACE ("ComputerName is %s (length %lu)\n", debugstr_w ( theName ), len);
349 memcpy ( name, theName, *size * sizeof (WCHAR) );
352 st = STATUS_MORE_ENTRIES;
356 memcpy ( name, theName, len * sizeof (WCHAR) );
364 st = STATUS_INVALID_PARAMETER;
372 if ( st == STATUS_SUCCESS )
376 SetLastError ( RtlNtStatusToDosError ( st ) );
377 WARN ( "Status %lu reading computer name from registry\n", st );
382 /***********************************************************************
383 * GetComputerNameA (KERNEL32.@)
385 BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
387 WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
388 DWORD sizeW = MAX_COMPUTERNAME_LENGTH;
392 if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
394 len = WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, NULL, 0, NULL, 0 );
399 WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, *size, NULL, 0 );
402 SetLastError( ERROR_MORE_DATA );
407 WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, len, NULL, 0 );
415 SetLastError( ERROR_INVALID_PARAMETER );
423 /***********************************************************************
424 * GetComputerNameExA (KERNEL32.@)
426 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
429 int len = sizeof (buf), ret;
430 TRACE("%d, %p, %p\n", type, name, size);
433 case ComputerNameNetBIOS:
434 case ComputerNamePhysicalNetBIOS:
435 return GetComputerNameA (name, size);
436 case ComputerNameDnsHostname:
437 case ComputerNamePhysicalDnsHostname:
438 ret = dns_hostname (buf, &len);
440 case ComputerNameDnsDomain:
441 case ComputerNamePhysicalDnsDomain:
442 ret = dns_domainname (buf, &len);
444 case ComputerNameDnsFullyQualified:
445 case ComputerNamePhysicalDnsFullyQualified:
446 ret = dns_fqdn (buf, &len);
449 SetLastError (ERROR_INVALID_PARAMETER);
455 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
460 memcpy( name, buf, *size );
463 SetLastError( ERROR_MORE_DATA );
468 memcpy( name, buf, len );
476 SetLastError( ERROR_INVALID_PARAMETER );
486 /***********************************************************************
487 * GetComputerNameExW (KERNEL32.@)
489 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
492 int len = sizeof (buf), ret;
494 TRACE("%d, %p, %p\n", type, name, size);
497 case ComputerNameNetBIOS:
498 case ComputerNamePhysicalNetBIOS:
499 return GetComputerNameW (name, size);
500 case ComputerNameDnsHostname:
501 case ComputerNamePhysicalDnsHostname:
502 ret = dns_hostname (buf, &len);
504 case ComputerNameDnsDomain:
505 case ComputerNamePhysicalDnsDomain:
506 ret = dns_domainname (buf, &len);
508 case ComputerNameDnsFullyQualified:
509 case ComputerNamePhysicalDnsFullyQualified:
510 ret = dns_fqdn (buf, &len);
513 SetLastError (ERROR_INVALID_PARAMETER);
519 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
522 unsigned int lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
525 MultiByteToWideChar( CP_ACP, 0, buf, len, name, *size );
528 SetLastError( ERROR_MORE_DATA );
533 MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
541 SetLastError( ERROR_INVALID_PARAMETER );
550 /******************************************************************************
551 * netbios_char (INTERNAL)
553 static WCHAR netbios_char ( WCHAR wc )
555 static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
556 static const WCHAR deflt = '_';
559 if ( isalnumW ( wc ) ) return wc;
560 for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
561 if ( wc == special[i] ) return wc;
565 /******************************************************************************
566 * SetComputerNameW [KERNEL32.@]
568 * Set a new NetBIOS name for the local computer.
571 * lpComputerName [I] Address of new computer name
577 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
579 UNICODE_STRING nameW;
580 OBJECT_ATTRIBUTES attr;
581 HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
582 int plen = strlenW ( lpComputerName );
584 NTSTATUS st = STATUS_INTERNAL_ERROR;
586 if (get_use_dns_option())
588 /* This check isn't necessary, but may help debugging problems. */
589 WARN( "Disabled by Wine Configuration.\n" );
590 WARN( "Set \"UseDnsComputerName\" = \"N\" in category [Network] to enable.\n" );
591 SetLastError ( ERROR_ACCESS_DENIED );
595 TRACE( "%s\n", debugstr_w (lpComputerName) );
597 /* Check parameter */
598 if ( plen > MAX_COMPUTERNAME_LENGTH )
601 /* This is NT behaviour. Win 95/98 would coerce characters. */
602 for ( i = 0; i < plen; i++ )
604 WCHAR wc = lpComputerName[i];
605 if ( wc != netbios_char( wc ) )
609 _init_attr ( &attr, &nameW );
611 RtlInitUnicodeString (&nameW, ComputerW);
612 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
614 attr.RootDirectory = hkey;
615 RtlInitUnicodeString( &nameW, ComputerNameW );
616 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
618 if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
626 if ( st == STATUS_SUCCESS )
628 TRACE( "ComputerName changed\n" );
634 SetLastError ( RtlNtStatusToDosError ( st ) );
635 WARN ( "status %lu\n", st );
640 /******************************************************************************
641 * SetComputerNameA [KERNEL32.@]
643 * See SetComputerNameW.
645 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
648 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
649 LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
651 MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
652 ret = SetComputerNameW( nameW );
653 HeapFree( GetProcessHeap(), 0, nameW );
657 /******************************************************************************
658 * SetComputerNameExW [KERNEL32.@]
661 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
663 TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
666 case ComputerNameNetBIOS:
667 case ComputerNamePhysicalNetBIOS:
668 return SetComputerNameW( lpComputerName );
670 SetLastError( ERROR_ACCESS_DENIED );
675 /******************************************************************************
676 * SetComputerNameExA [KERNEL32.@]
679 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
681 TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
684 case ComputerNameNetBIOS:
685 case ComputerNamePhysicalNetBIOS:
686 return SetComputerNameA( lpComputerName );
688 SetLastError( ERROR_ACCESS_DENIED );
693 /***********************************************************************
694 * DnsHostnameToComputerNameA (KERNEL32.@)
696 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR hostname,
697 LPSTR computername, LPDWORD size)
701 FIXME("(%s, %p, %p): stub\n", debugstr_a(hostname), computername, size);
703 if (!hostname || !size) return FALSE;
704 len = lstrlenA(hostname);
706 if (len > MAX_COMPUTERNAME_LENGTH)
707 len = MAX_COMPUTERNAME_LENGTH;
714 if (!computername) return FALSE;
716 memcpy( computername, hostname, len );
717 computername[len + 1] = 0;
721 /***********************************************************************
722 * DnsHostnameToComputerNameW (KERNEL32.@)
724 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR hostname,
725 LPWSTR computername, LPDWORD size)
729 FIXME("(%s, %p, %p): stub\n", debugstr_w(hostname), computername, size);
731 if (!hostname || !size) return FALSE;
732 len = lstrlenW(hostname);
734 if (len > MAX_COMPUTERNAME_LENGTH)
735 len = MAX_COMPUTERNAME_LENGTH;
742 if (!computername) return FALSE;
744 memcpy( computername, hostname, len * sizeof(WCHAR) );
745 computername[len + 1] = 0;