kernel32: Remove old bare-console code.
[wine] / dlls / kernel32 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #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"
47
48 #include "kernel_private.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(computername);
51
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};
60
61 static const char default_ComputerName[] = "WINE";
62
63 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
64
65 /*********************************************************************** 
66  *                    dns_gethostbyname (INTERNAL)
67  *
68  *  From hostname(1):
69  *  "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
70  *
71  *  Wine can use this technique only if the thread-safe gethostbyname_r is available.
72  */
73 #ifdef  HAVE_LINUX_GETHOSTBYNAME_R_6
74 static BOOL dns_gethostbyname ( char *name, int *size )
75 {
76     struct hostent* host = NULL;
77     char *extrabuf;
78     int ebufsize = 1024;
79     struct hostent hostentry;
80     int locerr = ENOBUFS, res = ENOMEM;
81
82     extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
83
84     while( extrabuf ) 
85     {
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 ) ;
90     }
91
92     if ( res )
93         WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
94     else if ( !host )
95     {
96         WARN ("gethostbyname_r returned NULL host, locerr = %d\n", locerr);
97         res = 1;
98     }
99     else
100     {
101         int len = strlen ( host->h_name );
102         if ( len < *size )
103         {
104             strcpy ( name, host->h_name );
105             *size = len;
106         }
107         else
108         {
109             memcpy ( name, host->h_name, *size );
110             name[*size] = 0;
111             SetLastError ( ERROR_MORE_DATA );
112             res = 1;
113         }
114     }
115
116     HeapFree( GetProcessHeap(), 0, extrabuf );
117     return !res;
118 }
119 #else
120 #  define dns_gethostbyname(name,size) 0
121 #endif
122
123 /*********************************************************************** 
124  *                     dns_fqdn (INTERNAL)
125  */
126 static BOOL dns_fqdn ( char *name, int *size )
127 {
128     if ( gethostname ( name, *size + 1 ) ) 
129     {
130         switch( errno )
131         {
132         case ENAMETOOLONG:
133             SetLastError ( ERROR_MORE_DATA );
134         default:
135             SetLastError ( ERROR_INVALID_PARAMETER );
136         }
137         return FALSE;
138     }
139
140     if ( !dns_gethostbyname ( name, size ) )
141         *size = strlen ( name );
142
143     return TRUE;
144 }
145
146 /*********************************************************************** 
147  *                     dns_hostname (INTERNAL)
148  */
149 static BOOL dns_hostname ( char *name, int *size )
150 {
151     char *c;
152     if ( ! dns_fqdn ( name, size ) ) return FALSE;
153     c = strchr ( name, '.' );
154     if (c)
155     {
156         *c = 0;
157         *size = (c - name);
158     }
159     return TRUE;
160 }
161
162 /*********************************************************************** 
163  *                     dns_domainname (INTERNAL)
164  */
165 static BOOL dns_domainname ( char *name, int *size )
166 {
167     char *c;
168     if ( ! dns_fqdn ( name, size ) ) return FALSE;
169     c = strchr ( name, '.' );
170     if (c)
171     {
172         c += 1;
173         *size -= (c - name);
174         memmove ( name, c, *size + 1 );
175     }
176     return TRUE;
177 }
178
179 /*********************************************************************** 
180  *                      _init_attr    (INTERNAL)
181  */
182 static inline void _init_attr ( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *name )
183 {
184     attr->Length = sizeof (OBJECT_ATTRIBUTES);
185     attr->RootDirectory = 0;
186     attr->ObjectName = name;
187     attr->Attributes = 0;
188     attr->SecurityDescriptor = NULL;
189     attr->SecurityQualityOfService = NULL;
190 }
191
192 /***********************************************************************
193  *           get_use_dns_option
194  */
195 static BOOL get_use_dns_option(void)
196 {
197     static const WCHAR NetworkW[] = {'S','o','f','t','w','a','r','e','\\',
198                                      'W','i','n','e','\\','N','e','t','w','o','r','k',0};
199     static const WCHAR UseDNSW[] = {'U','s','e','D','n','s','C','o','m','p','u','t','e','r','N','a','m','e',0};
200
201     char tmp[80];
202     HANDLE root, hkey;
203     DWORD dummy;
204     OBJECT_ATTRIBUTES attr;
205     UNICODE_STRING nameW;
206     BOOL ret = TRUE;
207
208     _init_attr( &attr, &nameW );
209     RtlOpenCurrentUser( KEY_READ, &root );
210     attr.RootDirectory = root;
211     RtlInitUnicodeString( &nameW, NetworkW );
212
213     /* @@ Wine registry key: HKCU\Software\Wine\Network */
214     if (!NtOpenKey( &hkey, KEY_READ, &attr ))
215     {
216         RtlInitUnicodeString( &nameW, UseDNSW );
217         if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
218         {
219             WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
220             ret = IS_OPTION_TRUE( str[0] );
221         }
222         NtClose( hkey );
223     }
224     NtClose( root );
225     return ret;
226 }
227
228
229 /*********************************************************************** 
230  *                      COMPUTERNAME_Init    (INTERNAL)
231  */
232 void COMPUTERNAME_Init (void)
233 {
234     HANDLE 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_UNIXCP, 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
277     {
278         len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
279         TRACE( "found in registry\n" );
280     }
281
282     NtClose( hsubkey );
283     TRACE(" ComputerName: %s (%u)\n", debugstr_w (computer_name), len);
284
285     RtlInitUnicodeString( &nameW, ActiveComputerNameW );
286     if ( ( st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, NULL ) )
287          != STATUS_SUCCESS )
288         goto out;
289     
290     RtlInitUnicodeString( &nameW, ComputerNameW );
291     st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
292
293 out:
294     NtClose( hsubkey );
295     NtClose( hkey );
296
297     if ( st == STATUS_SUCCESS )
298         TRACE( "success\n" );
299     else
300     {
301         WARN( "status trying to set ComputerName: %x\n", st );
302         SetLastError ( RtlNtStatusToDosError ( st ) );
303     }
304 }
305
306
307 /***********************************************************************
308  *              GetComputerNameW         (KERNEL32.@)
309  */
310 BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
311 {
312     UNICODE_STRING nameW;
313     OBJECT_ATTRIBUTES attr;
314     HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
315     char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
316     DWORD len = sizeof( buf );
317     LPWSTR theName = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
318     NTSTATUS st = STATUS_INVALID_PARAMETER;
319     DWORD err = ERROR_SUCCESS;
320     
321     TRACE ("%p %p\n", name, size);
322
323     _init_attr ( &attr, &nameW );
324     RtlInitUnicodeString( &nameW, ComputerW );
325     if ( ( st = NtOpenKey( &hkey, KEY_READ, &attr ) ) != STATUS_SUCCESS )
326     {
327         err = RtlNtStatusToDosError ( st );
328         goto out;
329     }
330          
331     attr.RootDirectory = hkey;
332     RtlInitUnicodeString( &nameW, ActiveComputerNameW );
333     if ( ( st = NtOpenKey( &hsubkey, KEY_READ, &attr ) ) != STATUS_SUCCESS )
334     {
335         err = RtlNtStatusToDosError ( st );
336         goto out;
337     }
338     
339     RtlInitUnicodeString( &nameW, ComputerNameW );
340     if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
341          != STATUS_SUCCESS )
342     {
343         err = RtlNtStatusToDosError ( st );
344         goto out;
345     }
346
347     len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
348     TRACE ("ComputerName is %s (length %u)\n", debugstr_w ( theName ), len);
349
350     if ( *size < len + 1 )
351     {
352         *size = len + 1;
353         err = ERROR_BUFFER_OVERFLOW;
354     }
355     else
356     {
357         memcpy ( name, theName, len * sizeof (WCHAR) );
358         name[len] = 0;
359         *size = len;
360     }
361
362 out:
363     NtClose ( hsubkey );
364     NtClose ( hkey );
365
366     if ( err == ERROR_SUCCESS )
367         return TRUE;
368     else
369     {
370         SetLastError ( err );
371         WARN ( "Status %u reading computer name from registry\n", st );
372         return FALSE;
373     }
374 }
375
376 /***********************************************************************
377  *              GetComputerNameA         (KERNEL32.@)
378  */
379 BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
380 {
381     WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
382     DWORD sizeW = MAX_COMPUTERNAME_LENGTH + 1;
383     unsigned int len;
384     BOOL ret;
385
386     if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
387
388     len = WideCharToMultiByte ( CP_ACP, 0, nameW, -1, NULL, 0, NULL, 0 );
389     /* for compatibility with Win9x */
390     __TRY
391     {
392         if ( *size < len )
393         {
394             *size = len;
395             SetLastError( ERROR_BUFFER_OVERFLOW );
396             ret = FALSE;
397         }
398         else
399         {
400             WideCharToMultiByte ( CP_ACP, 0, nameW, -1, name, len, NULL, 0 );
401             *size = len - 1;
402             ret = TRUE;
403         }
404     }
405     __EXCEPT_PAGE_FAULT
406     {
407         SetLastError( ERROR_INVALID_PARAMETER );
408         ret = FALSE;
409     }
410     __ENDTRY
411
412     return ret;
413 }
414
415 /***********************************************************************
416  *              GetComputerNameExA         (KERNEL32.@)
417  */
418 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
419 {
420     char buf[256];
421     int len = sizeof(buf) - 1, ret;
422     TRACE("%d, %p, %p\n", type, name, size);
423     switch( type )
424     {
425     case ComputerNameNetBIOS:
426     case ComputerNamePhysicalNetBIOS:
427         ret = GetComputerNameA (name, size);
428         if (!ret && GetLastError() == ERROR_BUFFER_OVERFLOW)
429             SetLastError( ERROR_MORE_DATA );
430         return ret;
431
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         if ( *size < len + 1 )
453         {
454             *size = len + 1;
455             SetLastError( ERROR_MORE_DATA );
456             ret = FALSE;
457         }
458         else
459         {
460             memcpy( name, buf, len );
461             name[len] = 0;
462             *size = len;
463             ret = TRUE;
464         }
465     }
466
467     return ret;
468 }
469
470
471 /***********************************************************************
472  *              GetComputerNameExW         (KERNEL32.@)
473  */
474 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
475 {
476     char buf[256];
477     int len = sizeof(buf) - 1, ret;
478
479     TRACE("%d, %p, %p\n", type, name, size);
480     switch( type )
481     {
482     case ComputerNameNetBIOS:
483     case ComputerNamePhysicalNetBIOS:
484         ret = GetComputerNameW (name, size);
485         if (!ret && GetLastError() == ERROR_BUFFER_OVERFLOW)
486             SetLastError( ERROR_MORE_DATA );
487         return ret;
488     case ComputerNameDnsHostname:
489     case ComputerNamePhysicalDnsHostname:
490         ret = dns_hostname (buf, &len);
491         break;
492     case ComputerNameDnsDomain:
493     case ComputerNamePhysicalDnsDomain:
494         ret = dns_domainname (buf, &len);
495         break;
496     case ComputerNameDnsFullyQualified:
497     case ComputerNamePhysicalDnsFullyQualified:
498         ret = dns_fqdn (buf, &len);
499         break;
500     default:
501         SetLastError (ERROR_INVALID_PARAMETER);
502         return FALSE;
503     }
504
505     if ( ret )
506     {
507         unsigned int lenW;
508
509         TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
510
511         lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
512         if ( *size < lenW + 1 )
513         {
514             *size = lenW + 1;
515             SetLastError( ERROR_MORE_DATA );
516             ret = FALSE;
517         }
518         else
519         {
520             MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
521             name[lenW] = 0;
522             *size = lenW;
523             ret = TRUE;
524         }
525     }
526
527     return ret;
528 }
529
530 /******************************************************************************
531  * netbios_char (INTERNAL)
532  */
533 static WCHAR netbios_char ( WCHAR wc )
534 {
535     static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
536     static const WCHAR deflt = '_';
537     unsigned int i;
538     
539     if ( isalnumW ( wc ) ) return wc;
540     for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
541         if ( wc == special[i] ) return wc;
542     return deflt;
543 }
544
545 /******************************************************************************
546  * SetComputerNameW [KERNEL32.@]
547  *
548  * Set a new NetBIOS name for the local computer.
549  *
550  * PARAMS
551  *    lpComputerName [I] Address of new computer name
552  *
553  * RETURNS
554  *    Success: TRUE
555  *    Failure: FALSE
556  */
557 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
558 {
559     UNICODE_STRING nameW;
560     OBJECT_ATTRIBUTES attr;
561     HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
562     int plen = strlenW ( lpComputerName );
563     int i;
564     NTSTATUS st = STATUS_INTERNAL_ERROR;
565
566     if (get_use_dns_option())
567     {
568         /* This check isn't necessary, but may help debugging problems. */
569         WARN( "Disabled by Wine Configuration.\n" );
570         WARN( "Set \"UseDnsComputerName\" = \"N\" in HKCU\\Software\\Wine\\Network to enable.\n" );
571         SetLastError ( ERROR_ACCESS_DENIED );
572         return FALSE;
573     }
574
575     TRACE( "%s\n", debugstr_w (lpComputerName) );
576
577     /* Check parameter */
578     if ( plen > MAX_COMPUTERNAME_LENGTH ) 
579         goto out;
580
581     /* This is NT behaviour. Win 95/98 would coerce characters. */
582     for ( i = 0; i < plen; i++ )
583     {
584         WCHAR wc = lpComputerName[i];
585         if ( wc != netbios_char( wc ) )
586             goto out;
587     }
588     
589     _init_attr ( &attr, &nameW );
590     
591     RtlInitUnicodeString (&nameW, ComputerW);
592     if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
593         goto out;
594     attr.RootDirectory = hkey;
595     RtlInitUnicodeString( &nameW, ComputerNameW );
596     if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
597         goto out;
598     if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
599          != STATUS_SUCCESS )
600         goto out;
601
602 out:
603     NtClose( hsubkey );
604     NtClose( hkey );
605     
606     if ( st == STATUS_SUCCESS )
607     {
608         TRACE( "ComputerName changed\n" );
609         return TRUE;
610     }
611
612     else
613     {
614         SetLastError ( RtlNtStatusToDosError ( st ) );
615         WARN ( "status %u\n", st );
616         return FALSE;
617     }
618 }
619
620 /******************************************************************************
621  * SetComputerNameA [KERNEL32.@]
622  *
623  * See SetComputerNameW.
624  */
625 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
626 {
627     BOOL ret;
628     DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
629     LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
630
631     MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
632     ret = SetComputerNameW( nameW );
633     HeapFree( GetProcessHeap(), 0, nameW );
634     return ret;
635 }
636
637 /******************************************************************************
638  * SetComputerNameExW [KERNEL32.@]
639  *
640  */
641 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
642 {
643     TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
644     switch( type )
645     {
646     case ComputerNameNetBIOS:
647     case ComputerNamePhysicalNetBIOS:
648         return SetComputerNameW( lpComputerName );
649     default:
650         SetLastError( ERROR_ACCESS_DENIED );
651         return FALSE;
652     }
653 }
654
655 /******************************************************************************
656  * SetComputerNameExA [KERNEL32.@]
657  *
658  */
659 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
660 {
661     TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
662     switch( type )
663     {
664     case ComputerNameNetBIOS:
665     case ComputerNamePhysicalNetBIOS:
666         return SetComputerNameA( lpComputerName );
667     default:
668         SetLastError( ERROR_ACCESS_DENIED );
669         return FALSE;
670     }
671 }
672
673 /***********************************************************************
674  *              DnsHostnameToComputerNameA         (KERNEL32.@)
675  */
676 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR hostname,
677     LPSTR computername, LPDWORD size)
678 {
679     DWORD len;
680
681     FIXME("(%s, %p, %p): stub\n", debugstr_a(hostname), computername, size);
682
683     if (!hostname || !size) return FALSE;
684     len = lstrlenA(hostname);
685
686     if (len > MAX_COMPUTERNAME_LENGTH)
687         len = MAX_COMPUTERNAME_LENGTH;
688
689     if (*size < len)
690     {
691         *size = len;
692         return FALSE;
693     }
694     if (!computername) return FALSE;
695
696     memcpy( computername, hostname, len );
697     computername[len + 1] = 0;
698     return TRUE;
699 }
700
701 /***********************************************************************
702  *              DnsHostnameToComputerNameW         (KERNEL32.@)
703  */
704 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR hostname,
705     LPWSTR computername, LPDWORD size)
706 {
707     DWORD len;
708
709     FIXME("(%s, %p, %p): stub\n", debugstr_w(hostname), computername, size);
710
711     if (!hostname || !size) return FALSE;
712     len = lstrlenW(hostname);
713
714     if (len > MAX_COMPUTERNAME_LENGTH)
715         len = MAX_COMPUTERNAME_LENGTH;
716
717     if (*size < len)
718     {
719         *size = len;
720         return FALSE;
721     }
722     if (!computername) return FALSE;
723
724     memcpy( computername, hostname, len * sizeof(WCHAR) );
725     computername[len + 1] = 0;
726     return TRUE;
727 }