Added magic comments to all Wine-specific registry accesses to make
[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     /* @@ Wine registry key: HKLM\Software\Wine\Wine\Config\Network */
216     if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
217     {
218         RtlInitUnicodeString( &nameW, UseDNSW );
219         if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
220         {
221             WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
222             ret = IS_OPTION_TRUE( str[0] );
223         }
224         NtClose( hkey );
225     }
226     return ret;
227 }
228
229
230 /*********************************************************************** 
231  *                      COMPUTERNAME_Init    (INTERNAL)
232  */
233 void COMPUTERNAME_Init (void)
234 {
235     HKEY 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;
242
243     TRACE("(void)\n");
244     _init_attr ( &attr, &nameW );
245     
246     RtlInitUnicodeString( &nameW, ComputerW );
247     if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
248         goto out;
249     
250     attr.RootDirectory = hkey;
251     RtlInitUnicodeString( &nameW, ComputerNameW );
252     if ( (st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
253         goto out;
254     
255     st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
256
257     if ( st != STATUS_SUCCESS || get_use_dns_option() )
258     {
259         char hbuf[256];
260         int hlen = sizeof (hbuf);
261         char *dot;
262         TRACE( "retrieving Unix host name\n" );
263         if ( gethostname ( hbuf, hlen ) )
264         {
265             strcpy ( hbuf, default_ComputerName );
266             WARN( "gethostname() error: %d, using host name %s\n", errno, hbuf );
267         }
268         hbuf[MAX_COMPUTERNAME_LENGTH] = 0;
269         dot = strchr ( hbuf, '.' );
270         if ( dot ) *dot = 0;
271         hlen = strlen ( hbuf );
272         len = MultiByteToWideChar( CP_ACP, 0, hbuf, hlen + 1, computer_name, MAX_COMPUTERNAME_LENGTH + 1 )
273             * sizeof( WCHAR );
274         if ( NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len ) != STATUS_SUCCESS )
275             WARN ( "failed to set ComputerName\n" );
276     }
277     else if ( st == STATUS_SUCCESS)
278     {
279         len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
280         TRACE( "found in registry\n" );
281     }
282     else goto out;
283     
284     NtClose( hsubkey );
285     TRACE(" ComputerName: %s (%lu)\n", debugstr_w ( computer_name ), len / sizeof(WCHAR));
286
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;
291     
292     RtlInitUnicodeString( &nameW, ComputerNameW );
293     st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
294
295 out:
296     NtClose( hsubkey );
297     NtClose( hkey );
298
299     if ( st == STATUS_SUCCESS )
300         TRACE( "success\n" );
301     else
302     {
303         WARN( "status trying to set ComputerName: %lx\n", st );
304         SetLastError ( RtlNtStatusToDosError ( st ) );
305     }
306 }
307
308
309 /***********************************************************************
310  *              GetComputerNameW         (KERNEL32.@)
311  */
312 BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
313 {
314     UNICODE_STRING nameW;
315     OBJECT_ATTRIBUTES attr;
316     HKEY 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     
322     TRACE ("%p %p\n", name, size);
323
324     _init_attr ( &attr, &nameW );
325     RtlInitUnicodeString( &nameW, ComputerW );
326     if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
327         goto out;
328          
329     attr.RootDirectory = hkey;
330     RtlInitUnicodeString( &nameW, ActiveComputerNameW );
331     if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
332         goto out;
333     
334     RtlInitUnicodeString( &nameW, ComputerNameW );
335     if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
336          != STATUS_SUCCESS )
337         goto out;
338
339     len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
340     TRACE ("ComputerName is %s (length %lu)\n", debugstr_w ( theName ), len);
341
342     __TRY
343     {
344         if ( *size < len )
345         {
346             memcpy ( name, theName, *size * sizeof (WCHAR) );
347             name[*size] = 0;
348             *size = len;
349             st = STATUS_MORE_ENTRIES;
350         }
351         else
352         {
353             memcpy ( name, theName, len * sizeof (WCHAR) );
354             name[len] = 0;
355             *size = len;
356             st = STATUS_SUCCESS;
357         }
358     }
359     __EXCEPT(page_fault)
360     {
361         st = STATUS_INVALID_PARAMETER;
362     }
363     __ENDTRY
364
365 out:
366     NtClose ( hsubkey );
367     NtClose ( hkey );
368
369     if ( st == STATUS_SUCCESS )
370         return TRUE;
371     else
372     {
373         SetLastError ( RtlNtStatusToDosError ( st ) );
374         WARN ( "Status %lu reading computer name from registry\n", st );
375         return FALSE;
376     }
377 }
378
379 /***********************************************************************
380  *              GetComputerNameA         (KERNEL32.@)
381  */
382 BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
383 {
384     WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
385     DWORD sizeW = MAX_COMPUTERNAME_LENGTH;
386     unsigned int len;
387     BOOL ret;
388
389     if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
390
391     len = WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, NULL, 0, NULL, 0 );
392     __TRY
393     {
394         if ( *size < len )
395         {
396             WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, *size, NULL, 0 );
397             name[*size] = 0;
398             *size = len;
399             SetLastError( ERROR_MORE_DATA );
400             ret = FALSE;
401         }
402         else 
403         {
404             WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, len, NULL, 0 );
405             name[len] = 0;
406             *size = len;
407             ret = TRUE;
408         }
409     }
410     __EXCEPT(page_fault)
411     {
412         SetLastError( ERROR_INVALID_PARAMETER );
413         ret = FALSE;
414     }
415     __ENDTRY
416
417     return ret;
418 }
419
420 /***********************************************************************
421  *              GetComputerNameExA         (KERNEL32.@)
422  */
423 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
424 {
425     char buf[256];
426     int len = sizeof (buf), ret;
427     TRACE("%d, %p, %p\n", type, name, size);
428     switch( type )
429     {
430     case ComputerNameNetBIOS:
431     case ComputerNamePhysicalNetBIOS:
432         return GetComputerNameA (name, size);
433     case ComputerNameDnsHostname:
434     case ComputerNamePhysicalDnsHostname:
435         ret = dns_hostname (buf, &len);
436         break;
437     case ComputerNameDnsDomain:
438     case ComputerNamePhysicalDnsDomain:
439         ret = dns_domainname (buf, &len);
440         break;
441     case ComputerNameDnsFullyQualified:
442     case ComputerNamePhysicalDnsFullyQualified:
443         ret = dns_fqdn (buf, &len);
444         break;
445     default:
446         SetLastError (ERROR_INVALID_PARAMETER);
447         return FALSE;
448     }
449
450     if ( ret )
451     {
452         TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
453         __TRY
454         {
455             if ( *size < len )
456             {
457                 memcpy( name, buf, *size );
458                 name[*size] = 0;
459                 *size = len;
460                 SetLastError( ERROR_MORE_DATA );
461                 ret = FALSE;
462             }
463             else
464             {
465                 memcpy( name, buf, len );
466                 name[len] = 0;
467                 *size = len;
468                 ret = TRUE;
469             }
470         }
471         __EXCEPT(page_fault)
472         {
473             SetLastError( ERROR_INVALID_PARAMETER );
474             return FALSE;
475         }
476         __ENDTRY
477     }
478
479     return ret;
480 }
481
482
483 /***********************************************************************
484  *              GetComputerNameExW         (KERNEL32.@)
485  */
486 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
487 {
488     char buf[256];
489     int len = sizeof (buf), ret;
490
491     TRACE("%d, %p, %p\n", type, name, size);
492     switch( type )
493     {
494     case ComputerNameNetBIOS:
495     case ComputerNamePhysicalNetBIOS:
496         return GetComputerNameW (name, size);
497     case ComputerNameDnsHostname:
498     case ComputerNamePhysicalDnsHostname:
499         ret = dns_hostname (buf, &len);
500         break;
501     case ComputerNameDnsDomain:
502     case ComputerNamePhysicalDnsDomain:
503         ret = dns_domainname (buf, &len);
504         break;
505     case ComputerNameDnsFullyQualified:
506     case ComputerNamePhysicalDnsFullyQualified:
507         ret = dns_fqdn (buf, &len);
508         break;
509     default:
510         SetLastError (ERROR_INVALID_PARAMETER);
511         return FALSE;
512     }
513
514     if ( ret )
515     {
516         TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
517         __TRY
518         {
519             unsigned int lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
520             if ( *size < lenW )
521             {
522                 MultiByteToWideChar( CP_ACP, 0, buf, len, name, *size );
523                 name[*size] = 0;
524                 *size = lenW;
525                 SetLastError( ERROR_MORE_DATA );
526                 ret = FALSE;
527             }
528             else
529             {
530                 MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
531                 name[lenW] = 0;
532                 *size = lenW;
533                 ret = TRUE;
534             }
535         }
536         __EXCEPT(page_fault)
537         {
538             SetLastError( ERROR_INVALID_PARAMETER );
539             return FALSE;
540         }
541         __ENDTRY
542     }
543
544     return ret;
545 }
546
547 /******************************************************************************
548  * netbios_char (INTERNAL)
549  */
550 static WCHAR netbios_char ( WCHAR wc )
551 {
552     static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
553     static const WCHAR deflt = '_';
554     unsigned int i;
555     
556     if ( isalnumW ( wc ) ) return wc;
557     for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
558         if ( wc == special[i] ) return wc;
559     return deflt;
560 }
561
562 /******************************************************************************
563  * SetComputerNameW [KERNEL32.@]
564  *
565  * PARAMS
566  *    lpComputerName [I] Address of new computer name
567  *
568  * RETURNS STD
569  */
570 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
571 {
572     UNICODE_STRING nameW;
573     OBJECT_ATTRIBUTES attr;
574     HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
575     int plen = strlenW ( lpComputerName );
576     int i;
577     NTSTATUS st = STATUS_INTERNAL_ERROR;
578
579     if (get_use_dns_option())
580     {
581         /* This check isn't necessary, but may help debugging problems. */
582         WARN( "Disabled by Wine Configuration.\n" );
583         WARN( "Set \"UseDnsComputerName\" = \"N\" in category [Network] to enable.\n" );
584         SetLastError ( ERROR_ACCESS_DENIED );
585         return FALSE;
586     }
587
588     TRACE( "%s\n", debugstr_w (lpComputerName) );
589
590     /* Check parameter */
591     if ( plen > MAX_COMPUTERNAME_LENGTH ) 
592         goto out;
593
594     /* This is NT behaviour. Win 95/98 would coerce characters. */
595     for ( i = 0; i < plen; i++ )
596     {
597         WCHAR wc = lpComputerName[i];
598         if ( wc != netbios_char( wc ) )
599             goto out;
600     }
601     
602     _init_attr ( &attr, &nameW );
603     
604     RtlInitUnicodeString (&nameW, ComputerW);
605     if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
606         goto out;
607     attr.RootDirectory = hkey;
608     RtlInitUnicodeString( &nameW, ComputerNameW );
609     if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
610         goto out;
611     if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
612          != STATUS_SUCCESS )
613         goto out;
614
615 out:
616     NtClose( hsubkey );
617     NtClose( hkey );
618     
619     if ( st == STATUS_SUCCESS )
620     {
621         TRACE( "ComputerName changed\n" );
622         return TRUE;
623     }
624
625     else
626     {
627         SetLastError ( RtlNtStatusToDosError ( st ) );
628         WARN ( "status %lu\n", st );
629         return FALSE;
630     }
631 }
632
633 /******************************************************************************
634  * SetComputerNameA [KERNEL32.@]
635  */
636 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
637 {
638     BOOL ret;
639     DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
640     LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
641
642     MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
643     ret = SetComputerNameW( nameW );
644     HeapFree( GetProcessHeap(), 0, nameW );
645     return ret;
646 }
647
648 /******************************************************************************
649  * SetComputerNameExW [KERNEL32.@]
650  *
651  */
652 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
653 {
654     TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
655     switch( type )
656     {
657     case ComputerNameNetBIOS:
658     case ComputerNamePhysicalNetBIOS:
659         return SetComputerNameW( lpComputerName );
660     default:
661         SetLastError( ERROR_ACCESS_DENIED );
662         return FALSE;
663     }
664 }
665
666 /******************************************************************************
667  * SetComputerNameExA [KERNEL32.@]
668  *
669  */
670 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
671 {
672     TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
673     switch( type )
674     {
675     case ComputerNameNetBIOS:
676     case ComputerNamePhysicalNetBIOS:
677         return SetComputerNameA( lpComputerName );
678     default:
679         SetLastError( ERROR_ACCESS_DENIED );
680         return FALSE;
681     }
682 }
683
684 /***********************************************************************
685  *              DnsHostnameToComputerNameA         (KERNEL32.@)
686  */
687 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR hostname,
688     LPSTR computername, LPDWORD size)
689 {
690     DWORD len;
691
692     FIXME("(%s, %p, %p): stub\n", debugstr_a(hostname), computername, size);
693
694     if (!hostname || !size) return FALSE;
695     len = lstrlenA(hostname);
696
697     if (len > MAX_COMPUTERNAME_LENGTH)
698         len = MAX_COMPUTERNAME_LENGTH;
699
700     if (*size < len)
701     {
702         *size = len;
703         return FALSE;
704     }
705     if (!computername) return FALSE;
706
707     memcpy( computername, hostname, len );
708     computername[len + 1] = 0;
709     return TRUE;
710 }
711
712 /***********************************************************************
713  *              DnsHostnameToComputerNameW         (KERNEL32.@)
714  */
715 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR hostname,
716     LPWSTR computername, LPDWORD size)
717 {
718     DWORD len;
719
720     FIXME("(%s, %p, %p): stub\n", debugstr_w(hostname), computername, size);
721
722     if (!hostname || !size) return FALSE;
723     len = lstrlenW(hostname);
724
725     if (len > MAX_COMPUTERNAME_LENGTH)
726         len = MAX_COMPUTERNAME_LENGTH;
727
728     if (*size < len)
729     {
730         *size = len;
731         return FALSE;
732     }
733     if (!computername) return FALSE;
734
735     memcpy( computername, hostname, len * sizeof(WCHAR) );
736     computername[len + 1] = 0;
737     return TRUE;
738 }