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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
38 #define WIN32_NO_STATUS
44 #include "wine/unicode.h"
45 #include "wine/exception.h"
46 #include "wine/debug.h"
48 #include "kernel_private.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(computername);
52 /* Registry key and value names */
53 static const WCHAR ComputerW[] = {'M','a','c','h','i','n','e','\\',
54 'S','y','s','t','e','m','\\',
55 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
56 'C','o','n','t','r','o','l','\\',
57 'C','o','m','p','u','t','e','r','N','a','m','e',0};
58 static const WCHAR ActiveComputerNameW[] = {'A','c','t','i','v','e','C','o','m','p','u','t','e','r','N','a','m','e',0};
59 static const WCHAR ComputerNameW[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
61 static const char default_ComputerName[] = "WINE";
63 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
65 /***********************************************************************
66 * dns_gethostbyname (INTERNAL)
69 * "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
71 * Wine can use this technique only if the thread-safe gethostbyname_r is available.
73 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
74 static BOOL dns_gethostbyname ( char *name, int *size )
76 struct hostent* host = NULL;
79 struct hostent hostentry;
80 int locerr = ENOBUFS, res = ENOMEM;
82 extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
86 res = gethostbyname_r ( name, &hostentry, extrabuf, ebufsize, &host, &locerr );
87 if( res != ERANGE ) break;
89 extrabuf = HeapReAlloc( GetProcessHeap(), 0, extrabuf, ebufsize ) ;
93 WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
96 WARN ("gethostbyname_r returned NULL host, locerr = %d\n", locerr);
101 int len = strlen ( host->h_name );
104 strcpy ( name, host->h_name );
109 memcpy ( name, host->h_name, *size );
111 SetLastError ( ERROR_MORE_DATA );
116 HeapFree( GetProcessHeap(), 0, extrabuf );
120 # define dns_gethostbyname(name,size) 0
123 /***********************************************************************
124 * dns_fqdn (INTERNAL)
126 static BOOL dns_fqdn ( char *name, int *size )
128 if ( gethostname ( name, *size + 1 ) )
133 SetLastError ( ERROR_MORE_DATA );
136 SetLastError ( ERROR_INVALID_PARAMETER );
142 if ( !dns_gethostbyname ( name, size ) )
143 *size = strlen ( name );
148 /***********************************************************************
149 * dns_hostname (INTERNAL)
151 static BOOL dns_hostname ( char *name, int *size )
154 if ( ! dns_fqdn ( name, size ) ) return FALSE;
155 c = strchr ( name, '.' );
164 /***********************************************************************
165 * dns_domainname (INTERNAL)
167 static BOOL dns_domainname ( char *name, int *size )
170 if ( ! dns_fqdn ( name, size ) ) return FALSE;
171 c = strchr ( name, '.' );
176 memmove ( name, c, *size + 1 );
181 /***********************************************************************
182 * _init_attr (INTERNAL)
184 static inline void _init_attr ( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *name )
186 attr->Length = sizeof (OBJECT_ATTRIBUTES);
187 attr->RootDirectory = 0;
188 attr->ObjectName = name;
189 attr->Attributes = 0;
190 attr->SecurityDescriptor = NULL;
191 attr->SecurityQualityOfService = NULL;
194 /***********************************************************************
197 static BOOL get_use_dns_option(void)
199 static const WCHAR NetworkW[] = {'S','o','f','t','w','a','r','e','\\',
200 'W','i','n','e','\\','N','e','t','w','o','r','k',0};
201 static const WCHAR UseDNSW[] = {'U','s','e','D','n','s','C','o','m','p','u','t','e','r','N','a','m','e',0};
206 OBJECT_ATTRIBUTES attr;
207 UNICODE_STRING nameW;
210 _init_attr( &attr, &nameW );
211 RtlOpenCurrentUser( KEY_READ, &root );
212 attr.RootDirectory = root;
213 RtlInitUnicodeString( &nameW, NetworkW );
215 /* @@ Wine registry key: HKCU\Software\Wine\Network */
216 if (!NtOpenKey( &hkey, KEY_READ, &attr ))
218 RtlInitUnicodeString( &nameW, UseDNSW );
219 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
221 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
222 ret = IS_OPTION_TRUE( str[0] );
231 /***********************************************************************
232 * COMPUTERNAME_Init (INTERNAL)
234 void COMPUTERNAME_Init (void)
236 HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
237 OBJECT_ATTRIBUTES attr;
238 UNICODE_STRING nameW;
239 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
240 DWORD len = sizeof( buf );
241 LPWSTR computer_name = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
242 NTSTATUS st = STATUS_INTERNAL_ERROR;
245 _init_attr ( &attr, &nameW );
247 RtlInitUnicodeString( &nameW, ComputerW );
248 if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
251 attr.RootDirectory = hkey;
252 RtlInitUnicodeString( &nameW, ComputerNameW );
253 if ( (st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
256 st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
258 if ( st != STATUS_SUCCESS || get_use_dns_option() )
261 int hlen = sizeof (hbuf);
263 TRACE( "retrieving Unix host name\n" );
264 if ( gethostname ( hbuf, hlen ) )
266 strcpy ( hbuf, default_ComputerName );
267 WARN( "gethostname() error: %d, using host name %s\n", errno, hbuf );
269 hbuf[MAX_COMPUTERNAME_LENGTH] = 0;
270 dot = strchr ( hbuf, '.' );
272 hlen = strlen ( hbuf );
273 len = MultiByteToWideChar( CP_UNIXCP, 0, hbuf, hlen + 1, computer_name, MAX_COMPUTERNAME_LENGTH + 1 )
275 if ( NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len ) != STATUS_SUCCESS )
276 WARN ( "failed to set ComputerName\n" );
280 len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
281 TRACE( "found in registry\n" );
285 TRACE(" ComputerName: %s (%u)\n", debugstr_w (computer_name), len);
287 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
288 if ( ( st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, NULL ) )
292 RtlInitUnicodeString( &nameW, ComputerNameW );
293 st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
299 if ( st == STATUS_SUCCESS )
300 TRACE( "success\n" );
303 WARN( "status trying to set ComputerName: %x\n", st );
304 SetLastError ( RtlNtStatusToDosError ( st ) );
309 /***********************************************************************
310 * GetComputerNameW (KERNEL32.@)
312 BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
314 UNICODE_STRING nameW;
315 OBJECT_ATTRIBUTES attr;
316 HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
317 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
318 DWORD len = sizeof( buf );
319 LPWSTR theName = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
320 NTSTATUS st = STATUS_INVALID_PARAMETER;
321 DWORD err = ERROR_SUCCESS;
323 TRACE ("%p %p\n", name, size);
325 _init_attr ( &attr, &nameW );
326 RtlInitUnicodeString( &nameW, ComputerW );
327 if ( ( st = NtOpenKey( &hkey, KEY_READ, &attr ) ) != STATUS_SUCCESS )
329 err = RtlNtStatusToDosError ( st );
333 attr.RootDirectory = hkey;
334 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
335 if ( ( st = NtOpenKey( &hsubkey, KEY_READ, &attr ) ) != STATUS_SUCCESS )
337 err = RtlNtStatusToDosError ( st );
341 RtlInitUnicodeString( &nameW, ComputerNameW );
342 if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
345 err = RtlNtStatusToDosError ( st );
349 len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
350 TRACE ("ComputerName is %s (length %u)\n", debugstr_w ( theName ), len);
352 if ( *size < len + 1 )
355 err = ERROR_BUFFER_OVERFLOW;
359 memcpy ( name, theName, len * sizeof (WCHAR) );
368 if ( err == ERROR_SUCCESS )
372 SetLastError ( err );
373 WARN ( "Status %u reading computer name from registry\n", st );
378 /***********************************************************************
379 * GetComputerNameA (KERNEL32.@)
381 BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
383 WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
384 DWORD sizeW = MAX_COMPUTERNAME_LENGTH + 1;
388 if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
390 len = WideCharToMultiByte ( CP_ACP, 0, nameW, -1, NULL, 0, NULL, 0 );
391 /* for compatibility with Win9x */
397 SetLastError( ERROR_BUFFER_OVERFLOW );
402 WideCharToMultiByte ( CP_ACP, 0, nameW, -1, name, len, NULL, 0 );
409 SetLastError( ERROR_INVALID_PARAMETER );
417 /***********************************************************************
418 * GetComputerNameExA (KERNEL32.@)
420 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
423 int len = sizeof(buf) - 1, ret;
424 TRACE("%d, %p, %p\n", type, name, size);
427 case ComputerNameNetBIOS:
428 case ComputerNamePhysicalNetBIOS:
429 ret = GetComputerNameA (name, size);
430 if (!ret && GetLastError() == ERROR_BUFFER_OVERFLOW)
431 SetLastError( ERROR_MORE_DATA );
434 case ComputerNameDnsHostname:
435 case ComputerNamePhysicalDnsHostname:
436 ret = dns_hostname (buf, &len);
438 case ComputerNameDnsDomain:
439 case ComputerNamePhysicalDnsDomain:
440 ret = dns_domainname (buf, &len);
442 case ComputerNameDnsFullyQualified:
443 case ComputerNamePhysicalDnsFullyQualified:
444 ret = dns_fqdn (buf, &len);
447 SetLastError (ERROR_INVALID_PARAMETER);
453 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
454 if ( *size < len + 1 )
457 SetLastError( ERROR_MORE_DATA );
462 memcpy( name, buf, len );
473 /***********************************************************************
474 * GetComputerNameExW (KERNEL32.@)
476 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
479 int len = sizeof(buf) - 1, ret;
481 TRACE("%d, %p, %p\n", type, name, size);
484 case ComputerNameNetBIOS:
485 case ComputerNamePhysicalNetBIOS:
486 ret = GetComputerNameW (name, size);
487 if (!ret && GetLastError() == ERROR_BUFFER_OVERFLOW)
488 SetLastError( ERROR_MORE_DATA );
490 case ComputerNameDnsHostname:
491 case ComputerNamePhysicalDnsHostname:
492 ret = dns_hostname (buf, &len);
494 case ComputerNameDnsDomain:
495 case ComputerNamePhysicalDnsDomain:
496 ret = dns_domainname (buf, &len);
498 case ComputerNameDnsFullyQualified:
499 case ComputerNamePhysicalDnsFullyQualified:
500 ret = dns_fqdn (buf, &len);
503 SetLastError (ERROR_INVALID_PARAMETER);
511 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
513 lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
514 if ( *size < lenW + 1 )
517 SetLastError( ERROR_MORE_DATA );
522 MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
532 /******************************************************************************
533 * netbios_char (INTERNAL)
535 static WCHAR netbios_char ( WCHAR wc )
537 static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
538 static const WCHAR deflt = '_';
541 if ( isalnumW ( wc ) ) return wc;
542 for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
543 if ( wc == special[i] ) return wc;
547 /******************************************************************************
548 * SetComputerNameW [KERNEL32.@]
550 * Set a new NetBIOS name for the local computer.
553 * lpComputerName [I] Address of new computer name
559 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
561 UNICODE_STRING nameW;
562 OBJECT_ATTRIBUTES attr;
563 HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
564 int plen = strlenW ( lpComputerName );
566 NTSTATUS st = STATUS_INTERNAL_ERROR;
568 if (get_use_dns_option())
570 /* This check isn't necessary, but may help debugging problems. */
571 WARN( "Disabled by Wine Configuration.\n" );
572 WARN( "Set \"UseDnsComputerName\" = \"N\" in HKCU\\Software\\Wine\\Network to enable.\n" );
573 SetLastError ( ERROR_ACCESS_DENIED );
577 TRACE( "%s\n", debugstr_w (lpComputerName) );
579 /* Check parameter */
580 if ( plen > MAX_COMPUTERNAME_LENGTH )
583 /* This is NT behaviour. Win 95/98 would coerce characters. */
584 for ( i = 0; i < plen; i++ )
586 WCHAR wc = lpComputerName[i];
587 if ( wc != netbios_char( wc ) )
591 _init_attr ( &attr, &nameW );
593 RtlInitUnicodeString (&nameW, ComputerW);
594 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
596 attr.RootDirectory = hkey;
597 RtlInitUnicodeString( &nameW, ComputerNameW );
598 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
600 if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
608 if ( st == STATUS_SUCCESS )
610 TRACE( "ComputerName changed\n" );
616 SetLastError ( RtlNtStatusToDosError ( st ) );
617 WARN ( "status %u\n", st );
622 /******************************************************************************
623 * SetComputerNameA [KERNEL32.@]
625 * See SetComputerNameW.
627 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
630 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
631 LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
633 MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
634 ret = SetComputerNameW( nameW );
635 HeapFree( GetProcessHeap(), 0, nameW );
639 /******************************************************************************
640 * SetComputerNameExW [KERNEL32.@]
643 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
645 TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
648 case ComputerNameNetBIOS:
649 case ComputerNamePhysicalNetBIOS:
650 return SetComputerNameW( lpComputerName );
652 SetLastError( ERROR_ACCESS_DENIED );
657 /******************************************************************************
658 * SetComputerNameExA [KERNEL32.@]
661 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
663 TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
666 case ComputerNameNetBIOS:
667 case ComputerNamePhysicalNetBIOS:
668 return SetComputerNameA( lpComputerName );
670 SetLastError( ERROR_ACCESS_DENIED );
675 /***********************************************************************
676 * DnsHostnameToComputerNameA (KERNEL32.@)
678 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR hostname,
679 LPSTR computername, LPDWORD size)
683 FIXME("(%s, %p, %p): stub\n", debugstr_a(hostname), computername, size);
685 if (!hostname || !size) return FALSE;
686 len = lstrlenA(hostname);
688 if (len > MAX_COMPUTERNAME_LENGTH)
689 len = MAX_COMPUTERNAME_LENGTH;
696 if (!computername) return FALSE;
698 memcpy( computername, hostname, len );
699 computername[len + 1] = 0;
703 /***********************************************************************
704 * DnsHostnameToComputerNameW (KERNEL32.@)
706 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR hostname,
707 LPWSTR computername, LPDWORD size)
711 FIXME("(%s, %p, %p): stub\n", debugstr_w(hostname), computername, size);
713 if (!hostname || !size) return FALSE;
714 len = lstrlenW(hostname);
716 if (len > MAX_COMPUTERNAME_LENGTH)
717 len = MAX_COMPUTERNAME_LENGTH;
724 if (!computername) return FALSE;
726 memcpy( computername, hostname, len * sizeof(WCHAR) );
727 computername[len + 1] = 0;