Removed some unnecessary inclusions of thread.h
[wine] / dlls / kernel / computername.c
1 /*
2  * Win32 kernel functions
3  *
4  * Copyright 1995 Martin von Loewis and Cameron Heide
5  * Copyright 1999 Peter Ganten
6  * Copyright 2002 Martin Wilck
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
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
36
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"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(computername);
50
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};
59
60 static const char default_ComputerName[] = "WINE";
61
62 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
63
64 /* filter for page-fault exceptions */
65 static WINE_EXCEPTION_FILTER(page_fault)
66 {
67     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
68         return EXCEPTION_EXECUTE_HANDLER;
69     return EXCEPTION_CONTINUE_SEARCH;
70 }
71
72 /*********************************************************************** 
73  *                    dns_gethostbyname (INTERNAL)
74  *
75  *  From hostname(1):
76  *  "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
77  *
78  *  Wine can use this technique only if the thread-safe gethostbyname_r is available.
79  */
80 #ifdef  HAVE_LINUX_GETHOSTBYNAME_R_6
81 static BOOL dns_gethostbyname ( char *name, int *size )
82 {
83     struct hostent* host = NULL;
84     char *extrabuf;
85     int ebufsize = 1024;
86     struct hostent hostentry;
87     int locerr = ENOBUFS, res = ENOMEM;
88
89     extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
90
91     while( extrabuf ) 
92     {
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 ) ;
97     }
98     
99     if ( res )
100         WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
101     else
102     {
103         int len = strlen ( host->h_name );
104         if ( len < *size )
105         {
106             strcpy ( name, host->h_name );
107             *size = len;
108         }
109         else
110         {
111             memcpy ( name, host->h_name, *size );
112             name[*size] = 0;
113             SetLastError ( ERROR_MORE_DATA );
114             res = 1;
115         }
116     }
117
118     HeapFree( GetProcessHeap(), 0, extrabuf );
119     return !res;
120 }
121 #else
122 #  define dns_gethostbyname(name,size) 0
123 #endif
124
125 /*********************************************************************** 
126  *                     dns_fqdn (INTERNAL)
127  */
128 static BOOL dns_fqdn ( char *name, int *size )
129 {
130     if ( gethostname ( name, *size + 1 ) ) 
131     {
132         switch( errno )
133         {
134         case ENAMETOOLONG:
135             SetLastError ( ERROR_MORE_DATA );
136         default:
137             SetLastError ( ERROR_INVALID_PARAMETER );
138         }
139         return FALSE;
140     }
141
142     if ( !dns_gethostbyname ( name, size ) )
143         *size = strlen ( name );
144
145     return TRUE;
146 }
147
148 /*********************************************************************** 
149  *                     dns_hostname (INTERNAL)
150  */
151 static BOOL dns_hostname ( char *name, int *size )
152 {
153     char *c;
154     if ( ! dns_fqdn ( name, size ) ) return FALSE;
155     c = strchr ( name, '.' );
156     if (c)
157     {
158         *c = 0;
159         *size = (c - name);
160     }
161     return TRUE;
162 }
163
164 /*********************************************************************** 
165  *                     dns_domainname (INTERNAL)
166  */
167 static BOOL dns_domainname ( char *name, int *size )
168 {
169     char *c;
170     if ( ! dns_fqdn ( name, size ) ) return FALSE;
171     c = strchr ( name, '.' );
172     if (c)
173     {
174         c += 1;
175         *size -= (c - name);
176         memmove ( name, c, *size + 1 );
177     }
178     return TRUE;
179 }
180
181 /*********************************************************************** 
182  *                      _init_attr    (INTERNAL)
183  */
184 inline static void _init_attr ( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *name )
185 {
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;
192 }
193
194 /***********************************************************************
195  *           get_use_dns_option
196  */
197 static BOOL get_use_dns_option(void)
198 {
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};
204
205     char tmp[80];
206     HKEY hkey;
207     DWORD dummy;
208     OBJECT_ATTRIBUTES attr;
209     UNICODE_STRING nameW;
210     BOOL ret = TRUE;
211
212     _init_attr( &attr, &nameW );
213     RtlInitUnicodeString( &nameW, NetworkW );
214
215     if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
216     {
217         RtlInitUnicodeString( &nameW, UseDNSW );
218         if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
219         {
220             WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
221             ret = IS_OPTION_TRUE( str[0] );
222         }
223         NtClose( hkey );
224     }
225     return ret;
226 }
227
228
229 /*********************************************************************** 
230  *                      COMPUTERNAME_Init    (INTERNAL)
231  */
232 void COMPUTERNAME_Init (void)
233 {
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;
241
242     TRACE("(void)\n");
243     _init_attr ( &attr, &nameW );
244     
245     RtlInitUnicodeString( &nameW, ComputerW );
246     if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
247         goto out;
248     
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;
253     
254     st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
255
256     if ( st != STATUS_SUCCESS || get_use_dns_option() )
257     {
258         char hbuf[256];
259         int hlen = sizeof (hbuf);
260         char *dot;
261         TRACE( "retrieving Unix host name\n" );
262         if ( gethostname ( hbuf, hlen ) )
263         {
264             strcpy ( hbuf, default_ComputerName );
265             WARN( "gethostname() error: %d, using host name %s\n", errno, hbuf );
266         }
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" );
275     }
276     else if ( st == STATUS_SUCCESS)
277     {
278         len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
279         TRACE( "found in registry\n" );
280     }
281     else goto out;
282     
283     NtClose( hsubkey );
284     TRACE(" ComputerName: %s (%lu)\n", debugstr_w ( computer_name ), len / sizeof(WCHAR));
285
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;
290     
291     RtlInitUnicodeString( &nameW, ComputerNameW );
292     st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
293
294 out:
295     NtClose( hsubkey );
296     NtClose( hkey );
297
298     if ( st == STATUS_SUCCESS )
299         TRACE( "success\n" );
300     else
301     {
302         WARN( "status trying to set ComputerName: %lx\n", st );
303         SetLastError ( RtlNtStatusToDosError ( st ) );
304     }
305 }
306
307
308 /***********************************************************************
309  *              GetComputerNameW         (KERNEL32.@)
310  */
311 BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
312 {
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;
320     
321     TRACE ("%p %p\n", name, size);
322
323     _init_attr ( &attr, &nameW );
324     RtlInitUnicodeString( &nameW, ComputerW );
325     if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
326         goto out;
327          
328     attr.RootDirectory = hkey;
329     RtlInitUnicodeString( &nameW, ActiveComputerNameW );
330     if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
331         goto out;
332     
333     RtlInitUnicodeString( &nameW, ComputerNameW );
334     if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
335          != STATUS_SUCCESS )
336         goto out;
337
338     len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
339     TRACE ("ComputerName is %s (length %lu)\n", debugstr_w ( theName ), len);
340
341     __TRY
342     {
343         if ( *size < len )
344         {
345             memcpy ( name, theName, *size * sizeof (WCHAR) );
346             name[*size] = 0;
347             *size = len;
348             st = STATUS_MORE_ENTRIES;
349         }
350         else
351         {
352             memcpy ( name, theName, len * sizeof (WCHAR) );
353             name[len] = 0;
354             *size = len;
355             st = STATUS_SUCCESS;
356         }
357     }
358     __EXCEPT(page_fault)
359     {
360         st = STATUS_INVALID_PARAMETER;
361     }
362     __ENDTRY
363
364 out:
365     NtClose ( hsubkey );
366     NtClose ( hkey );
367
368     if ( st == STATUS_SUCCESS )
369         return TRUE;
370     else
371     {
372         SetLastError ( RtlNtStatusToDosError ( st ) );
373         WARN ( "Status %lu reading computer name from registry\n", st );
374         return FALSE;
375     }
376 }
377
378 /***********************************************************************
379  *              GetComputerNameA         (KERNEL32.@)
380  */
381 BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
382 {
383     WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
384     DWORD sizeW = MAX_COMPUTERNAME_LENGTH;
385     unsigned int len;
386     BOOL ret;
387
388     if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
389
390     len = WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, NULL, 0, NULL, 0 );
391     __TRY
392     {
393         if ( *size < len )
394         {
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;
400         }
401         else 
402         {
403             WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, len, NULL, 0 );
404             name[len] = 0;
405             *size = len;
406             ret = TRUE;
407         }
408     }
409     __EXCEPT(page_fault)
410     {
411         SetLastError( ERROR_INVALID_PARAMETER );
412         ret = FALSE;
413     }
414     __ENDTRY
415
416     return ret;
417 }
418
419 /***********************************************************************
420  *              GetComputerNameExA         (KERNEL32.@)
421  */
422 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
423 {
424     char buf[256];
425     int len = sizeof (buf), ret;
426     TRACE("%d, %p, %p\n", type, name, size);
427     switch( type )
428     {
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;
447     }
448
449     if ( ret )
450     {
451         TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
452         __TRY
453         {
454             if ( *size < len )
455             {
456                 memcpy( name, buf, *size );
457                 name[*size] = 0;
458                 *size = len;
459                 SetLastError( ERROR_MORE_DATA );
460                 ret = FALSE;
461             }
462             else
463             {
464                 memcpy( name, buf, len );
465                 name[len] = 0;
466                 *size = len;
467                 ret = TRUE;
468             }
469         }
470         __EXCEPT(page_fault)
471         {
472             SetLastError( ERROR_INVALID_PARAMETER );
473             return FALSE;
474         }
475         __ENDTRY
476     }
477
478     return ret;
479 }
480
481
482 /***********************************************************************
483  *              GetComputerNameExW         (KERNEL32.@)
484  */
485 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
486 {
487     char buf[256];
488     int len = sizeof (buf), ret;
489
490     TRACE("%d, %p, %p\n", type, name, size);
491     switch( type )
492     {
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;
511     }
512
513     if ( ret )
514     {
515         TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
516         __TRY
517         {
518             unsigned int lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
519             if ( *size < lenW )
520             {
521                 MultiByteToWideChar( CP_ACP, 0, buf, len, name, *size );
522                 name[*size] = 0;
523                 *size = lenW;
524                 SetLastError( ERROR_MORE_DATA );
525                 ret = FALSE;
526             }
527             else
528             {
529                 MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
530                 name[lenW] = 0;
531                 *size = lenW;
532                 ret = TRUE;
533             }
534         }
535         __EXCEPT(page_fault)
536         {
537             SetLastError( ERROR_INVALID_PARAMETER );
538             return FALSE;
539         }
540         __ENDTRY
541     }
542
543     return ret;
544 }
545
546 /******************************************************************************
547  * netbios_char (INTERNAL)
548  */
549 static WCHAR netbios_char ( WCHAR wc )
550 {
551     static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
552     static const WCHAR deflt = '_';
553     unsigned int i;
554     
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;
559 }
560
561 /******************************************************************************
562  * SetComputerNameW [KERNEL32.@]
563  *
564  * PARAMS
565  *    lpComputerName [I] Address of new computer name
566  *
567  * RETURNS STD
568  */
569 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
570 {
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;
577
578     if (get_use_dns_option())
579     {
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;
585     }
586
587     TRACE( "%s\n", debugstr_w (lpComputerName) );
588
589     /* Check parameter */
590     if ( plen > MAX_COMPUTERNAME_LENGTH ) 
591         goto out;
592
593     /* This is NT behaviour. Win 95/98 would coerce characters. */
594     for ( i = 0; i < plen; i++ )
595     {
596         WCHAR wc = lpComputerName[i];
597         if ( wc != netbios_char( wc ) )
598             goto out;
599     }
600     
601     _init_attr ( &attr, &nameW );
602     
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;
613
614 out:
615     NtClose( hsubkey );
616     NtClose( hkey );
617     
618     if ( st == STATUS_SUCCESS )
619     {
620         TRACE( "ComputerName changed\n" );
621         return TRUE;
622     }
623
624     else
625     {
626         SetLastError ( RtlNtStatusToDosError ( st ) );
627         WARN ( "status %lu\n", st );
628         return FALSE;
629     }
630 }
631
632 /******************************************************************************
633  * SetComputerNameA [KERNEL32.@]
634  */
635 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
636 {
637     BOOL ret;
638     DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
639     LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
640
641     MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
642     ret = SetComputerNameW( nameW );
643     HeapFree( GetProcessHeap(), 0, nameW );
644     return ret;
645 }
646
647 /******************************************************************************
648  * SetComputerNameExW [KERNEL32.@]
649  *
650  */
651 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
652 {
653     TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
654     switch( type )
655     {
656     case ComputerNameNetBIOS:
657     case ComputerNamePhysicalNetBIOS:
658         return SetComputerNameW( lpComputerName );
659     default:
660         SetLastError( ERROR_ACCESS_DENIED );
661         return FALSE;
662     }
663 }
664
665 /******************************************************************************
666  * SetComputerNameExA [KERNEL32.@]
667  *
668  */
669 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
670 {
671     TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
672     switch( type )
673     {
674     case ComputerNameNetBIOS:
675     case ComputerNamePhysicalNetBIOS:
676         return SetComputerNameA( lpComputerName );
677     default:
678         SetLastError( ERROR_ACCESS_DENIED );
679         return FALSE;
680     }
681 }
682
683 /***********************************************************************
684  *              DnsHostnameToComputerNameA         (KERNEL32.@)
685  */
686 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR hostname,
687     LPSTR computername, LPDWORD size)
688 {
689     DWORD len;
690
691     FIXME("(%s, %p, %p): stub\n", debugstr_a(hostname), computername, size);
692
693     if (!hostname || !size) return FALSE;
694     len = lstrlenA(hostname);
695
696     if (len > MAX_COMPUTERNAME_LENGTH)
697         len = MAX_COMPUTERNAME_LENGTH;
698
699     if (*size < len)
700     {
701         *size = len;
702         return FALSE;
703     }
704     if (!computername) return FALSE;
705
706     memcpy( computername, hostname, len );
707     computername[len + 1] = 0;
708     return TRUE;
709 }
710
711 /***********************************************************************
712  *              DnsHostnameToComputerNameW         (KERNEL32.@)
713  */
714 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR hostname,
715     LPWSTR computername, LPDWORD size)
716 {
717     DWORD len;
718
719     FIXME("(%s, %p, %p): stub\n", debugstr_w(hostname), computername, size);
720
721     if (!hostname || !size) return FALSE;
722     len = lstrlenW(hostname);
723
724     if (len > MAX_COMPUTERNAME_LENGTH)
725         len = MAX_COMPUTERNAME_LENGTH;
726
727     if (*size < len)
728     {
729         *size = len;
730         return FALSE;
731     }
732     if (!computername) return FALSE;
733
734     memcpy( computername, hostname, len * sizeof(WCHAR) );
735     computername[len + 1] = 0;
736     return TRUE;
737 }