Use Interlocked* functions in AddRef and Release.
[wine.git] / dlls / kernel / computername.c
blob945d11e83559a8cd9847b427a727f56687f5ba09
1 /*
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <string.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <stdlib.h>
32 #include <errno.h>
33 #ifdef HAVE_NETDB_H
34 #include <netdb.h>
35 #endif
37 #include "ntstatus.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winerror.h"
41 #include "winnls.h"
42 #include "winreg.h"
43 #include "winternl.h"
44 #include "wine/unicode.h"
45 #include "wine/exception.h"
46 #include "excpt.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(computername);
51 /* Registry key and value names */
52 static const WCHAR ComputerW[] = {'M','a','c','h','i','n','e','\\',
53 'S','y','s','t','e','m','\\',
54 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
55 'C','o','n','t','r','o','l','\\',
56 'C','o','m','p','u','t','e','r','N','a','m','e',0};
57 static const WCHAR ActiveComputerNameW[] = {'A','c','t','i','v','e','C','o','m','p','u','t','e','r','N','a','m','e',0};
58 static const WCHAR ComputerNameW[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
60 static const char default_ComputerName[] = "WINE";
62 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
64 /* filter for page-fault exceptions */
65 static WINE_EXCEPTION_FILTER(page_fault)
67 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
68 return EXCEPTION_EXECUTE_HANDLER;
69 return EXCEPTION_CONTINUE_SEARCH;
72 /***********************************************************************
73 * dns_gethostbyname (INTERNAL)
75 * From hostname(1):
76 * "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
78 * Wine can use this technique only if the thread-safe gethostbyname_r is available.
80 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
81 static BOOL dns_gethostbyname ( char *name, int *size )
83 struct hostent* host = NULL;
84 char *extrabuf;
85 int ebufsize = 1024;
86 struct hostent hostentry;
87 int locerr = ENOBUFS, res = ENOMEM;
89 extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
91 while( extrabuf )
93 res = gethostbyname_r ( name, &hostentry, extrabuf, ebufsize, &host, &locerr );
94 if( res != ERANGE ) break;
95 ebufsize *= 2;
96 extrabuf = HeapReAlloc( GetProcessHeap(), 0, extrabuf, ebufsize ) ;
99 if ( res )
100 WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
101 else
103 int len = strlen ( host->h_name );
104 if ( len < *size )
106 strcpy ( name, host->h_name );
107 *size = len;
109 else
111 memcpy ( name, host->h_name, *size );
112 name[*size] = 0;
113 SetLastError ( ERROR_MORE_DATA );
114 res = 1;
118 HeapFree( GetProcessHeap(), 0, extrabuf );
119 return !res;
121 #else
122 # define dns_gethostbyname(name,size) 0
123 #endif
125 /***********************************************************************
126 * dns_fqdn (INTERNAL)
128 static BOOL dns_fqdn ( char *name, int *size )
130 if ( gethostname ( name, *size + 1 ) )
132 switch( errno )
134 case ENAMETOOLONG:
135 SetLastError ( ERROR_MORE_DATA );
136 default:
137 SetLastError ( ERROR_INVALID_PARAMETER );
139 return FALSE;
142 if ( !dns_gethostbyname ( name, size ) )
143 *size = strlen ( name );
145 return TRUE;
148 /***********************************************************************
149 * dns_hostname (INTERNAL)
151 static BOOL dns_hostname ( char *name, int *size )
153 char *c;
154 if ( ! dns_fqdn ( name, size ) ) return FALSE;
155 c = strchr ( name, '.' );
156 if (c)
158 *c = 0;
159 *size = (c - name);
161 return TRUE;
164 /***********************************************************************
165 * dns_domainname (INTERNAL)
167 static BOOL dns_domainname ( char *name, int *size )
169 char *c;
170 if ( ! dns_fqdn ( name, size ) ) return FALSE;
171 c = strchr ( name, '.' );
172 if (c)
174 c += 1;
175 *size -= (c - name);
176 memmove ( name, c, *size + 1 );
178 return TRUE;
181 /***********************************************************************
182 * _init_attr (INTERNAL)
184 inline static 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 /***********************************************************************
195 * get_use_dns_option
197 static BOOL get_use_dns_option(void)
199 static const WCHAR NetworkW[] = {'M','a','c','h','i','n','e','\\',
200 'S','o','f','t','w','a','r','e','\\',
201 'W','i','n','e','\\','W','i','n','e','\\',
202 'C','o','n','f','i','g','\\','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};
205 char tmp[80];
206 HKEY hkey;
207 DWORD dummy;
208 OBJECT_ATTRIBUTES attr;
209 UNICODE_STRING nameW;
210 BOOL ret = TRUE;
212 _init_attr( &attr, &nameW );
213 RtlInitUnicodeString( &nameW, NetworkW );
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] );
223 NtClose( hkey );
225 return ret;
229 /***********************************************************************
230 * COMPUTERNAME_Init (INTERNAL)
232 void COMPUTERNAME_Init (void)
234 HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
235 OBJECT_ATTRIBUTES attr;
236 UNICODE_STRING nameW;
237 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
238 DWORD len = sizeof( buf );
239 LPWSTR computer_name = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
240 NTSTATUS st = STATUS_INTERNAL_ERROR;
242 TRACE("(void)\n");
243 _init_attr ( &attr, &nameW );
245 RtlInitUnicodeString( &nameW, ComputerW );
246 if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
247 goto out;
249 attr.RootDirectory = hkey;
250 RtlInitUnicodeString( &nameW, ComputerNameW );
251 if ( (st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
252 goto out;
254 st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
256 if ( st == STATUS_OBJECT_NAME_NOT_FOUND || ( st == STATUS_SUCCESS && get_use_dns_option()))
258 char hbuf[256];
259 int hlen = sizeof (hbuf);
260 char *dot;
261 TRACE( "retrieving Unix host name\n" );
262 if ( gethostname ( hbuf, hlen ) )
264 strcpy ( hbuf, default_ComputerName );
265 WARN( "gethostname() error: %d, using host name %s\n", errno, hbuf );
267 hbuf[MAX_COMPUTERNAME_LENGTH] = 0;
268 dot = strchr ( hbuf, '.' );
269 if ( dot ) *dot = 0;
270 hlen = strlen ( hbuf );
271 len = MultiByteToWideChar( CP_ACP, 0, hbuf, hlen + 1, computer_name, MAX_COMPUTERNAME_LENGTH + 1 )
272 * sizeof( WCHAR );
273 if ( NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len ) != STATUS_SUCCESS )
274 WARN ( "failed to set ComputerName\n" );
276 else if ( st == STATUS_SUCCESS)
278 len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
279 TRACE( "found in registry\n" );
281 else goto out;
283 NtClose( hsubkey );
284 TRACE(" ComputerName: %s (%lu)\n", debugstr_w ( computer_name ), len / sizeof(WCHAR));
286 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
287 if ( ( st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, NULL ) )
288 != STATUS_SUCCESS )
289 goto out;
291 RtlInitUnicodeString( &nameW, ComputerNameW );
292 st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
294 out:
295 NtClose( hsubkey );
296 NtClose( hkey );
298 if ( st == STATUS_SUCCESS )
299 TRACE( "success\n" );
300 else
302 WARN( "status trying to set ComputerName: %lx\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 HKEY 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 )
326 goto out;
328 attr.RootDirectory = hkey;
329 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
330 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
331 goto out;
333 RtlInitUnicodeString( &nameW, ComputerNameW );
334 if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
335 != STATUS_SUCCESS )
336 goto out;
338 len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
339 TRACE ("ComputerName is %s (length %lu)\n", debugstr_w ( theName ), len);
341 __TRY
343 if ( *size < len )
345 memcpy ( name, theName, *size * sizeof (WCHAR) );
346 name[*size] = 0;
347 *size = len;
348 st = STATUS_MORE_ENTRIES;
350 else
352 memcpy ( name, theName, len * sizeof (WCHAR) );
353 name[len] = 0;
354 *size = len;
355 st = STATUS_SUCCESS;
358 __EXCEPT(page_fault)
360 st = STATUS_INVALID_PARAMETER;
362 __ENDTRY
364 out:
365 NtClose ( hsubkey );
366 NtClose ( hkey );
368 if ( st == STATUS_SUCCESS )
369 return TRUE;
370 else
372 SetLastError ( RtlNtStatusToDosError ( st ) );
373 WARN ( "Status %lu reading computer name from registry\n", st );
374 return FALSE;
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;
385 unsigned int len;
386 BOOL ret;
388 if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
390 len = WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, NULL, 0, NULL, 0 );
391 __TRY
393 if ( *size < len )
395 WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, *size, NULL, 0 );
396 name[*size] = 0;
397 *size = len;
398 SetLastError( ERROR_MORE_DATA );
399 ret = FALSE;
401 else
403 WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, len, NULL, 0 );
404 name[len] = 0;
405 *size = len;
406 ret = TRUE;
409 __EXCEPT(page_fault)
411 SetLastError( ERROR_INVALID_PARAMETER );
412 ret = FALSE;
414 __ENDTRY
416 return ret;
419 /***********************************************************************
420 * GetComputerNameExA (KERNEL32.@)
422 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
424 char buf[256];
425 int len = sizeof (buf), ret;
426 TRACE("%d, %p, %p\n", type, name, size);
427 switch( type )
429 case ComputerNameNetBIOS:
430 case ComputerNamePhysicalNetBIOS:
431 return GetComputerNameA (name, size);
432 case ComputerNameDnsHostname:
433 case ComputerNamePhysicalDnsHostname:
434 ret = dns_hostname (buf, &len);
435 break;
436 case ComputerNameDnsDomain:
437 case ComputerNamePhysicalDnsDomain:
438 ret = dns_domainname (buf, &len);
439 break;
440 case ComputerNameDnsFullyQualified:
441 case ComputerNamePhysicalDnsFullyQualified:
442 ret = dns_fqdn (buf, &len);
443 break;
444 default:
445 SetLastError (ERROR_INVALID_PARAMETER);
446 return FALSE;
449 if ( ret )
451 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
452 __TRY
454 if ( *size < len )
456 memcpy( name, buf, *size );
457 name[*size] = 0;
458 *size = len;
459 SetLastError( ERROR_MORE_DATA );
460 ret = FALSE;
462 else
464 memcpy( name, buf, len );
465 name[len] = 0;
466 *size = len;
467 ret = TRUE;
470 __EXCEPT(page_fault)
472 SetLastError( ERROR_INVALID_PARAMETER );
473 return FALSE;
475 __ENDTRY
478 return ret;
482 /***********************************************************************
483 * GetComputerNameExW (KERNEL32.@)
485 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
487 char buf[256];
488 int len = sizeof (buf), ret;
490 TRACE("%d, %p, %p\n", type, name, size);
491 switch( type )
493 case ComputerNameNetBIOS:
494 case ComputerNamePhysicalNetBIOS:
495 return GetComputerNameW (name, size);
496 case ComputerNameDnsHostname:
497 case ComputerNamePhysicalDnsHostname:
498 ret = dns_hostname (buf, &len);
499 break;
500 case ComputerNameDnsDomain:
501 case ComputerNamePhysicalDnsDomain:
502 ret = dns_domainname (buf, &len);
503 break;
504 case ComputerNameDnsFullyQualified:
505 case ComputerNamePhysicalDnsFullyQualified:
506 ret = dns_fqdn (buf, &len);
507 break;
508 default:
509 SetLastError (ERROR_INVALID_PARAMETER);
510 return FALSE;
513 if ( ret )
515 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
516 __TRY
518 unsigned int lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
519 if ( *size < lenW )
521 MultiByteToWideChar( CP_ACP, 0, buf, len, name, *size );
522 name[*size] = 0;
523 *size = lenW;
524 SetLastError( ERROR_MORE_DATA );
525 ret = FALSE;
527 else
529 MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
530 name[lenW] = 0;
531 *size = lenW;
532 ret = TRUE;
535 __EXCEPT(page_fault)
537 SetLastError( ERROR_INVALID_PARAMETER );
538 return FALSE;
540 __ENDTRY
543 return ret;
546 /******************************************************************************
547 * netbios_char (INTERNAL)
549 static WCHAR netbios_char ( WCHAR wc )
551 static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
552 static const WCHAR deflt = '_';
553 unsigned int i;
555 if ( isalnumW ( wc ) ) return wc;
556 for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
557 if ( wc == special[i] ) return wc;
558 return deflt;
561 /******************************************************************************
562 * SetComputerNameW [KERNEL32.@]
564 * PARAMS
565 * lpComputerName [I] Address of new computer name
567 * RETURNS STD
569 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
571 UNICODE_STRING nameW;
572 OBJECT_ATTRIBUTES attr;
573 HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
574 int plen = strlenW ( lpComputerName );
575 int i;
576 NTSTATUS st = STATUS_INTERNAL_ERROR;
578 if (get_use_dns_option())
580 /* This check isn't necessary, but may help debugging problems. */
581 WARN( "Disabled by Wine Configuration.\n" );
582 WARN( "Set \"UseDnsComputerName\" = \"N\" in category [Network] to enable.\n" );
583 SetLastError ( ERROR_ACCESS_DENIED );
584 return FALSE;
587 TRACE( "%s\n", debugstr_w (lpComputerName) );
589 /* Check parameter */
590 if ( plen > MAX_COMPUTERNAME_LENGTH )
591 goto out;
593 /* This is NT behaviour. Win 95/98 would coerce characters. */
594 for ( i = 0; i < plen; i++ )
596 WCHAR wc = lpComputerName[i];
597 if ( wc != netbios_char( wc ) )
598 goto out;
601 _init_attr ( &attr, &nameW );
603 RtlInitUnicodeString (&nameW, ComputerW);
604 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
605 goto out;
606 attr.RootDirectory = hkey;
607 RtlInitUnicodeString( &nameW, ComputerNameW );
608 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
609 goto out;
610 if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
611 != STATUS_SUCCESS )
612 goto out;
614 out:
615 NtClose( hsubkey );
616 NtClose( hkey );
618 if ( st == STATUS_SUCCESS )
620 TRACE( "ComputerName changed\n" );
621 return TRUE;
624 else
626 SetLastError ( RtlNtStatusToDosError ( st ) );
627 WARN ( "status %lu\n", st );
628 return FALSE;
632 /******************************************************************************
633 * SetComputerNameA [KERNEL32.@]
635 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
637 BOOL ret;
638 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
639 LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
641 MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
642 ret = SetComputerNameW( nameW );
643 HeapFree( GetProcessHeap(), 0, nameW );
644 return ret;
647 /******************************************************************************
648 * SetComputerNameExW [KERNEL32.@]
651 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
653 TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
654 switch( type )
656 case ComputerNameNetBIOS:
657 case ComputerNamePhysicalNetBIOS:
658 return SetComputerNameW( lpComputerName );
659 default:
660 SetLastError( ERROR_ACCESS_DENIED );
661 return FALSE;
665 /******************************************************************************
666 * SetComputerNameExA [KERNEL32.@]
669 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
671 TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
672 switch( type )
674 case ComputerNameNetBIOS:
675 case ComputerNamePhysicalNetBIOS:
676 return SetComputerNameA( lpComputerName );
677 default:
678 SetLastError( ERROR_ACCESS_DENIED );
679 return FALSE;
683 /***********************************************************************
684 * DnsHostnameToComputerNameA (KERNEL32.@)
686 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR Hostname, LPSTR ComputerName,
687 LPDWORD nSize)
689 FIXME("(%s, %s, %08lx): stub\n", debugstr_a(Hostname),
690 debugstr_a(ComputerName), *nSize);
691 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
692 return FALSE;
695 /***********************************************************************
696 * DnsHostnameToComputerNameW (KERNEL32.@)
698 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR Hostname, LPWSTR ComputerName,
699 LPDWORD nSize)
701 FIXME("(%s, %s, %08lx): stub\n", debugstr_w(Hostname),
702 debugstr_w(ComputerName), *nSize);
703 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
704 return FALSE;