dwrite/tests: Some tests for TranslateColorGlyphRun().
[wine/multimedia.git] / dlls / kernel32 / computername.c
blob0c81ac033ce1b9dbca7f245d2f90e5a50ccae342
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #define WIN32_NO_STATUS
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winerror.h"
42 #include "winnls.h"
43 #include "winternl.h"
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)
68 * From hostname(1):
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;
77 char *extrabuf;
78 int ebufsize = 1024;
79 struct hostent hostentry;
80 int locerr = ENOBUFS, res = ENOMEM;
82 extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
84 while( extrabuf )
86 res = gethostbyname_r ( name, &hostentry, extrabuf, ebufsize, &host, &locerr );
87 if( res != ERANGE ) break;
88 ebufsize *= 2;
89 extrabuf = HeapReAlloc( GetProcessHeap(), 0, extrabuf, ebufsize ) ;
92 if ( res )
93 WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
94 else if ( !host )
96 WARN ("gethostbyname_r returned NULL host, locerr = %d\n", locerr);
97 res = 1;
99 else
101 int len = strlen ( host->h_name );
102 if ( len < *size )
104 strcpy ( name, host->h_name );
105 *size = len;
107 else
109 memcpy ( name, host->h_name, *size );
110 name[*size] = 0;
111 SetLastError ( ERROR_MORE_DATA );
112 res = 1;
116 HeapFree( GetProcessHeap(), 0, extrabuf );
117 return !res;
119 #else
120 # define dns_gethostbyname(name,size) 0
121 #endif
123 /***********************************************************************
124 * dns_fqdn (INTERNAL)
126 static BOOL dns_fqdn ( char *name, int *size )
128 if ( gethostname ( name, *size + 1 ) )
130 switch( errno )
132 case ENAMETOOLONG:
133 SetLastError ( ERROR_MORE_DATA );
134 break;
135 default:
136 SetLastError ( ERROR_INVALID_PARAMETER );
137 break;
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 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 /***********************************************************************
195 * get_use_dns_option
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};
203 char tmp[80];
204 HANDLE root, hkey;
205 DWORD dummy;
206 OBJECT_ATTRIBUTES attr;
207 UNICODE_STRING nameW;
208 BOOL ret = TRUE;
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] );
224 NtClose( hkey );
226 NtClose( root );
227 return ret;
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;
244 TRACE("(void)\n");
245 _init_attr ( &attr, &nameW );
247 RtlInitUnicodeString( &nameW, ComputerW );
248 if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
249 goto out;
251 attr.RootDirectory = hkey;
252 RtlInitUnicodeString( &nameW, ComputerNameW );
253 if ( (st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
254 goto out;
256 st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
258 if ( st != STATUS_SUCCESS || get_use_dns_option() )
260 char hbuf[256];
261 int hlen = sizeof (hbuf);
262 char *dot;
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, '.' );
271 if ( dot ) *dot = 0;
272 hlen = strlen ( hbuf );
273 len = MultiByteToWideChar( CP_UNIXCP, 0, hbuf, hlen + 1, computer_name, MAX_COMPUTERNAME_LENGTH + 1 )
274 * sizeof( WCHAR );
275 if ( NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len ) != STATUS_SUCCESS )
276 WARN ( "failed to set ComputerName\n" );
278 else
280 len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
281 TRACE( "found in registry\n" );
284 NtClose( hsubkey );
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 ) )
289 != STATUS_SUCCESS )
290 goto out;
292 RtlInitUnicodeString( &nameW, ComputerNameW );
293 st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
295 out:
296 NtClose( hsubkey );
297 NtClose( hkey );
299 if ( st == STATUS_SUCCESS )
300 TRACE( "success\n" );
301 else
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 );
330 goto out;
333 attr.RootDirectory = hkey;
334 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
335 if ( ( st = NtOpenKey( &hsubkey, KEY_READ, &attr ) ) != STATUS_SUCCESS )
337 err = RtlNtStatusToDosError ( st );
338 goto out;
341 RtlInitUnicodeString( &nameW, ComputerNameW );
342 if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
343 != STATUS_SUCCESS )
345 err = RtlNtStatusToDosError ( st );
346 goto out;
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 )
354 *size = len + 1;
355 err = ERROR_BUFFER_OVERFLOW;
357 else
359 memcpy ( name, theName, len * sizeof (WCHAR) );
360 name[len] = 0;
361 *size = len;
364 out:
365 NtClose ( hsubkey );
366 NtClose ( hkey );
368 if ( err == ERROR_SUCCESS )
369 return TRUE;
370 else
372 SetLastError ( err );
373 WARN ( "Status %u 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 + 1;
385 unsigned int len;
386 BOOL ret;
388 if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
390 len = WideCharToMultiByte ( CP_ACP, 0, nameW, -1, NULL, 0, NULL, 0 );
391 /* for compatibility with Win9x */
392 __TRY
394 if ( *size < len )
396 *size = len;
397 SetLastError( ERROR_BUFFER_OVERFLOW );
398 ret = FALSE;
400 else
402 WideCharToMultiByte ( CP_ACP, 0, nameW, -1, name, len, NULL, 0 );
403 *size = len - 1;
404 ret = TRUE;
407 __EXCEPT_PAGE_FAULT
409 SetLastError( ERROR_INVALID_PARAMETER );
410 ret = FALSE;
412 __ENDTRY
414 return ret;
417 /***********************************************************************
418 * GetComputerNameExA (KERNEL32.@)
420 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
422 char buf[256];
423 int len = sizeof(buf) - 1, ret;
424 TRACE("%d, %p, %p\n", type, name, size);
425 switch( type )
427 case ComputerNameNetBIOS:
428 case ComputerNamePhysicalNetBIOS:
429 ret = GetComputerNameA (name, size);
430 if (!ret && GetLastError() == ERROR_BUFFER_OVERFLOW)
431 SetLastError( ERROR_MORE_DATA );
432 return ret;
434 case ComputerNameDnsHostname:
435 case ComputerNamePhysicalDnsHostname:
436 ret = dns_hostname (buf, &len);
437 break;
438 case ComputerNameDnsDomain:
439 case ComputerNamePhysicalDnsDomain:
440 ret = dns_domainname (buf, &len);
441 break;
442 case ComputerNameDnsFullyQualified:
443 case ComputerNamePhysicalDnsFullyQualified:
444 ret = dns_fqdn (buf, &len);
445 break;
446 default:
447 SetLastError (ERROR_INVALID_PARAMETER);
448 return FALSE;
451 if ( ret )
453 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
454 if ( *size < len + 1 )
456 *size = len + 1;
457 SetLastError( ERROR_MORE_DATA );
458 ret = FALSE;
460 else
462 memcpy( name, buf, len );
463 name[len] = 0;
464 *size = len;
465 ret = TRUE;
469 return ret;
473 /***********************************************************************
474 * GetComputerNameExW (KERNEL32.@)
476 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
478 char buf[256];
479 int len = sizeof(buf) - 1, ret;
481 TRACE("%d, %p, %p\n", type, name, size);
482 switch( type )
484 case ComputerNameNetBIOS:
485 case ComputerNamePhysicalNetBIOS:
486 ret = GetComputerNameW (name, size);
487 if (!ret && GetLastError() == ERROR_BUFFER_OVERFLOW)
488 SetLastError( ERROR_MORE_DATA );
489 return ret;
490 case ComputerNameDnsHostname:
491 case ComputerNamePhysicalDnsHostname:
492 ret = dns_hostname (buf, &len);
493 break;
494 case ComputerNameDnsDomain:
495 case ComputerNamePhysicalDnsDomain:
496 ret = dns_domainname (buf, &len);
497 break;
498 case ComputerNameDnsFullyQualified:
499 case ComputerNamePhysicalDnsFullyQualified:
500 ret = dns_fqdn (buf, &len);
501 break;
502 default:
503 SetLastError (ERROR_INVALID_PARAMETER);
504 return FALSE;
507 if ( ret )
509 unsigned int lenW;
511 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
513 lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
514 if ( *size < lenW + 1 )
516 *size = lenW + 1;
517 SetLastError( ERROR_MORE_DATA );
518 ret = FALSE;
520 else
522 MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
523 name[lenW] = 0;
524 *size = lenW;
525 ret = TRUE;
529 return ret;
532 /******************************************************************************
533 * netbios_char (INTERNAL)
535 static WCHAR netbios_char ( WCHAR wc )
537 static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
538 static const WCHAR deflt = '_';
539 unsigned int i;
541 if ( isalnumW ( wc ) ) return wc;
542 for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
543 if ( wc == special[i] ) return wc;
544 return deflt;
547 /******************************************************************************
548 * SetComputerNameW [KERNEL32.@]
550 * Set a new NetBIOS name for the local computer.
552 * PARAMS
553 * lpComputerName [I] Address of new computer name
555 * RETURNS
556 * Success: TRUE
557 * Failure: FALSE
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 );
565 int i;
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 );
574 return FALSE;
577 TRACE( "%s\n", debugstr_w (lpComputerName) );
579 /* Check parameter */
580 if ( plen > MAX_COMPUTERNAME_LENGTH )
581 goto out;
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 ) )
588 goto out;
591 _init_attr ( &attr, &nameW );
593 RtlInitUnicodeString (&nameW, ComputerW);
594 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
595 goto out;
596 attr.RootDirectory = hkey;
597 RtlInitUnicodeString( &nameW, ComputerNameW );
598 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
599 goto out;
600 if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
601 != STATUS_SUCCESS )
602 goto out;
604 out:
605 NtClose( hsubkey );
606 NtClose( hkey );
608 if ( st == STATUS_SUCCESS )
610 TRACE( "ComputerName changed\n" );
611 return TRUE;
614 else
616 SetLastError ( RtlNtStatusToDosError ( st ) );
617 WARN ( "status %u\n", st );
618 return FALSE;
622 /******************************************************************************
623 * SetComputerNameA [KERNEL32.@]
625 * See SetComputerNameW.
627 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
629 BOOL ret;
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 );
636 return ret;
639 /******************************************************************************
640 * SetComputerNameExW [KERNEL32.@]
643 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
645 TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
646 switch( type )
648 case ComputerNameNetBIOS:
649 case ComputerNamePhysicalNetBIOS:
650 return SetComputerNameW( lpComputerName );
651 default:
652 SetLastError( ERROR_ACCESS_DENIED );
653 return FALSE;
657 /******************************************************************************
658 * SetComputerNameExA [KERNEL32.@]
661 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
663 TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
664 switch( type )
666 case ComputerNameNetBIOS:
667 case ComputerNamePhysicalNetBIOS:
668 return SetComputerNameA( lpComputerName );
669 default:
670 SetLastError( ERROR_ACCESS_DENIED );
671 return FALSE;
675 /***********************************************************************
676 * DnsHostnameToComputerNameA (KERNEL32.@)
678 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR hostname,
679 LPSTR computername, LPDWORD size)
681 DWORD len;
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;
691 if (*size < len + 1)
693 *size = len;
694 return FALSE;
696 if (!computername) return FALSE;
698 memcpy( computername, hostname, len );
699 computername[len] = 0;
700 return TRUE;
703 /***********************************************************************
704 * DnsHostnameToComputerNameW (KERNEL32.@)
706 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR hostname,
707 LPWSTR computername, LPDWORD size)
709 DWORD len;
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;
719 if (*size < len + 1)
721 *size = len;
722 return FALSE;
724 if (!computername) return FALSE;
726 memcpy( computername, hostname, len * sizeof(WCHAR) );
727 computername[len] = 0;
728 return TRUE;