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"
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 /***********************************************************************
67 * dns_gethostbyname (INTERNAL)
70 * "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
72 * Wine can use this technique only if the thread-safe gethostbyname_r is available.
74 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
75 static BOOL dns_gethostbyname ( char *name, int *size )
77 struct hostent* host = NULL;
80 struct hostent hostentry;
81 int locerr = ENOBUFS, res = ENOMEM;
83 extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
87 res = gethostbyname_r ( name, &hostentry, extrabuf, ebufsize, &host, &locerr );
88 if( res != ERANGE ) break;
90 extrabuf = HeapReAlloc( GetProcessHeap(), 0, extrabuf, ebufsize ) ;
94 WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
97 WARN ("gethostbyname_r returned NULL host, locerr = %d\n", locerr);
102 int 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 static inline 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[] = {'S','o','f','t','w','a','r','e','\\',
199 'W','i','n','e','\\','N','e','t','w','o','r','k',0};
200 static const WCHAR UseDNSW[] = {'U','s','e','D','n','s','C','o','m','p','u','t','e','r','N','a','m','e',0};
205 OBJECT_ATTRIBUTES attr;
206 UNICODE_STRING nameW;
209 _init_attr( &attr, &nameW );
210 RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
211 attr.RootDirectory = root;
212 RtlInitUnicodeString( &nameW, NetworkW );
214 /* @@ Wine registry key: HKCU\Software\Wine\Network */
215 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
217 RtlInitUnicodeString( &nameW, UseDNSW );
218 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
220 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
221 ret = IS_OPTION_TRUE( str[0] );
230 /***********************************************************************
231 * COMPUTERNAME_Init (INTERNAL)
233 void COMPUTERNAME_Init (void)
235 HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
236 OBJECT_ATTRIBUTES attr;
237 UNICODE_STRING nameW;
238 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
239 DWORD len = sizeof( buf );
240 LPWSTR computer_name = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
241 NTSTATUS st = STATUS_INTERNAL_ERROR;
244 _init_attr ( &attr, &nameW );
246 RtlInitUnicodeString( &nameW, ComputerW );
247 if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
250 attr.RootDirectory = hkey;
251 RtlInitUnicodeString( &nameW, ComputerNameW );
252 if ( (st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
255 st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
257 if ( st != STATUS_SUCCESS || get_use_dns_option() )
260 int hlen = sizeof (hbuf);
262 TRACE( "retrieving Unix host name\n" );
263 if ( gethostname ( hbuf, hlen ) )
265 strcpy ( hbuf, default_ComputerName );
266 WARN( "gethostname() error: %d, using host name %s\n", errno, hbuf );
268 hbuf[MAX_COMPUTERNAME_LENGTH] = 0;
269 dot = strchr ( hbuf, '.' );
271 hlen = strlen ( hbuf );
272 len = MultiByteToWideChar( CP_ACP, 0, hbuf, hlen + 1, computer_name, MAX_COMPUTERNAME_LENGTH + 1 )
274 if ( NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len ) != STATUS_SUCCESS )
275 WARN ( "failed to set ComputerName\n" );
279 len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
280 TRACE( "found in registry\n" );
284 TRACE(" ComputerName: %s (%u)\n", debugstr_w (computer_name), len);
286 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
287 if ( ( st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, NULL ) )
291 RtlInitUnicodeString( &nameW, ComputerNameW );
292 st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
298 if ( st == STATUS_SUCCESS )
299 TRACE( "success\n" );
302 WARN( "status trying to set ComputerName: %x\n", st );
303 SetLastError ( RtlNtStatusToDosError ( st ) );
308 /***********************************************************************
309 * GetComputerNameW (KERNEL32.@)
311 BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
313 UNICODE_STRING nameW;
314 OBJECT_ATTRIBUTES attr;
315 HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
316 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
317 DWORD len = sizeof( buf );
318 LPWSTR theName = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
319 NTSTATUS st = STATUS_INVALID_PARAMETER;
321 TRACE ("%p %p\n", name, size);
323 _init_attr ( &attr, &nameW );
324 RtlInitUnicodeString( &nameW, ComputerW );
325 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
328 attr.RootDirectory = hkey;
329 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
330 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
333 RtlInitUnicodeString( &nameW, ComputerNameW );
334 if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
338 len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
339 TRACE ("ComputerName is %s (length %u)\n", debugstr_w ( theName ), len);
341 if ( *size < len + 1 )
344 st = STATUS_MORE_ENTRIES;
348 memcpy ( name, theName, len * sizeof (WCHAR) );
358 if ( st == STATUS_SUCCESS )
362 SetLastError ( RtlNtStatusToDosError ( st ) );
363 WARN ( "Status %u reading computer name from registry\n", st );
368 /***********************************************************************
369 * GetComputerNameA (KERNEL32.@)
371 BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
373 WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
374 DWORD sizeW = MAX_COMPUTERNAME_LENGTH + 1;
378 if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
380 len = WideCharToMultiByte ( CP_ACP, 0, nameW, -1, NULL, 0, NULL, 0 );
381 /* for compatibility with Win9x */
387 SetLastError( ERROR_MORE_DATA );
392 WideCharToMultiByte ( CP_ACP, 0, nameW, -1, name, len, NULL, 0 );
399 SetLastError( ERROR_INVALID_PARAMETER );
407 /***********************************************************************
408 * GetComputerNameExA (KERNEL32.@)
410 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
413 int len = sizeof(buf) - 1, ret;
414 TRACE("%d, %p, %p\n", type, name, size);
417 case ComputerNameNetBIOS:
418 case ComputerNamePhysicalNetBIOS:
419 return GetComputerNameA (name, size);
420 case ComputerNameDnsHostname:
421 case ComputerNamePhysicalDnsHostname:
422 ret = dns_hostname (buf, &len);
424 case ComputerNameDnsDomain:
425 case ComputerNamePhysicalDnsDomain:
426 ret = dns_domainname (buf, &len);
428 case ComputerNameDnsFullyQualified:
429 case ComputerNamePhysicalDnsFullyQualified:
430 ret = dns_fqdn (buf, &len);
433 SetLastError (ERROR_INVALID_PARAMETER);
439 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
440 if ( *size < len + 1 )
443 SetLastError( ERROR_MORE_DATA );
448 memcpy( name, buf, len );
459 /***********************************************************************
460 * GetComputerNameExW (KERNEL32.@)
462 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
465 int len = sizeof(buf) - 1, ret;
467 TRACE("%d, %p, %p\n", type, name, size);
470 case ComputerNameNetBIOS:
471 case ComputerNamePhysicalNetBIOS:
472 return GetComputerNameW (name, size);
473 case ComputerNameDnsHostname:
474 case ComputerNamePhysicalDnsHostname:
475 ret = dns_hostname (buf, &len);
477 case ComputerNameDnsDomain:
478 case ComputerNamePhysicalDnsDomain:
479 ret = dns_domainname (buf, &len);
481 case ComputerNameDnsFullyQualified:
482 case ComputerNamePhysicalDnsFullyQualified:
483 ret = dns_fqdn (buf, &len);
486 SetLastError (ERROR_INVALID_PARAMETER);
494 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
496 lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
497 if ( *size < lenW + 1 )
500 SetLastError( ERROR_MORE_DATA );
505 MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
515 /******************************************************************************
516 * netbios_char (INTERNAL)
518 static WCHAR netbios_char ( WCHAR wc )
520 static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
521 static const WCHAR deflt = '_';
524 if ( isalnumW ( wc ) ) return wc;
525 for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
526 if ( wc == special[i] ) return wc;
530 /******************************************************************************
531 * SetComputerNameW [KERNEL32.@]
533 * Set a new NetBIOS name for the local computer.
536 * lpComputerName [I] Address of new computer name
542 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
544 UNICODE_STRING nameW;
545 OBJECT_ATTRIBUTES attr;
546 HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
547 int plen = strlenW ( lpComputerName );
549 NTSTATUS st = STATUS_INTERNAL_ERROR;
551 if (get_use_dns_option())
553 /* This check isn't necessary, but may help debugging problems. */
554 WARN( "Disabled by Wine Configuration.\n" );
555 WARN( "Set \"UseDnsComputerName\" = \"N\" in category [Network] to enable.\n" );
556 SetLastError ( ERROR_ACCESS_DENIED );
560 TRACE( "%s\n", debugstr_w (lpComputerName) );
562 /* Check parameter */
563 if ( plen > MAX_COMPUTERNAME_LENGTH )
566 /* This is NT behaviour. Win 95/98 would coerce characters. */
567 for ( i = 0; i < plen; i++ )
569 WCHAR wc = lpComputerName[i];
570 if ( wc != netbios_char( wc ) )
574 _init_attr ( &attr, &nameW );
576 RtlInitUnicodeString (&nameW, ComputerW);
577 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
579 attr.RootDirectory = hkey;
580 RtlInitUnicodeString( &nameW, ComputerNameW );
581 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
583 if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
591 if ( st == STATUS_SUCCESS )
593 TRACE( "ComputerName changed\n" );
599 SetLastError ( RtlNtStatusToDosError ( st ) );
600 WARN ( "status %u\n", st );
605 /******************************************************************************
606 * SetComputerNameA [KERNEL32.@]
608 * See SetComputerNameW.
610 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
613 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
614 LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
616 MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
617 ret = SetComputerNameW( nameW );
618 HeapFree( GetProcessHeap(), 0, nameW );
622 /******************************************************************************
623 * SetComputerNameExW [KERNEL32.@]
626 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
628 TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
631 case ComputerNameNetBIOS:
632 case ComputerNamePhysicalNetBIOS:
633 return SetComputerNameW( lpComputerName );
635 SetLastError( ERROR_ACCESS_DENIED );
640 /******************************************************************************
641 * SetComputerNameExA [KERNEL32.@]
644 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
646 TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
649 case ComputerNameNetBIOS:
650 case ComputerNamePhysicalNetBIOS:
651 return SetComputerNameA( lpComputerName );
653 SetLastError( ERROR_ACCESS_DENIED );
658 /***********************************************************************
659 * DnsHostnameToComputerNameA (KERNEL32.@)
661 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR hostname,
662 LPSTR computername, LPDWORD size)
666 FIXME("(%s, %p, %p): stub\n", debugstr_a(hostname), computername, size);
668 if (!hostname || !size) return FALSE;
669 len = lstrlenA(hostname);
671 if (len > MAX_COMPUTERNAME_LENGTH)
672 len = MAX_COMPUTERNAME_LENGTH;
679 if (!computername) return FALSE;
681 memcpy( computername, hostname, len );
682 computername[len + 1] = 0;
686 /***********************************************************************
687 * DnsHostnameToComputerNameW (KERNEL32.@)
689 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR hostname,
690 LPWSTR computername, LPDWORD size)
694 FIXME("(%s, %p, %p): stub\n", debugstr_w(hostname), computername, size);
696 if (!hostname || !size) return FALSE;
697 len = lstrlenW(hostname);
699 if (len > MAX_COMPUTERNAME_LENGTH)
700 len = MAX_COMPUTERNAME_LENGTH;
707 if (!computername) return FALSE;
709 memcpy( computername, hostname, len * sizeof(WCHAR) );
710 computername[len + 1] = 0;