[KERNEL32][CONSRV]
[reactos.git] / reactos / dll / win32 / kernel32 / client / compname.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2003 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS system libraries
22 * PURPOSE: Computer name functions
23 * FILE: lib/kernel32/misc/computername.c
24 * PROGRAMER: Eric Kohl
25 */
26
27 /* INCLUDES ******************************************************************/
28
29 #include <k32.h>
30
31 #define NDEBUG
32 #include <debug.h>
33
34
35 /* FUNCTIONS *****************************************************************/
36
37 static
38 BOOL
39 GetComputerNameFromRegistry(LPWSTR RegistryKey,
40 LPWSTR ValueNameStr,
41 LPWSTR lpBuffer,
42 LPDWORD nSize)
43 {
44 PKEY_VALUE_PARTIAL_INFORMATION KeyInfo;
45 OBJECT_ATTRIBUTES ObjectAttributes;
46 UNICODE_STRING KeyName;
47 UNICODE_STRING ValueName;
48 HANDLE KeyHandle;
49 ULONG KeyInfoSize;
50 ULONG ReturnSize;
51 NTSTATUS Status;
52
53 RtlInitUnicodeString(&KeyName, RegistryKey);
54 InitializeObjectAttributes(&ObjectAttributes,
55 &KeyName,
56 OBJ_CASE_INSENSITIVE,
57 NULL,
58 NULL);
59
60 Status = NtOpenKey(&KeyHandle,
61 KEY_READ,
62 &ObjectAttributes);
63 if (!NT_SUCCESS(Status))
64 {
65 BaseSetLastNTError (Status);
66 return FALSE;
67 }
68
69 KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + *nSize * sizeof(WCHAR);
70 KeyInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, KeyInfoSize);
71 if (KeyInfo == NULL)
72 {
73 NtClose(KeyHandle);
74 SetLastError(ERROR_OUTOFMEMORY);
75 return FALSE;
76 }
77
78 RtlInitUnicodeString(&ValueName, ValueNameStr);
79
80 Status = NtQueryValueKey(KeyHandle,
81 &ValueName,
82 KeyValuePartialInformation,
83 KeyInfo,
84 KeyInfoSize,
85 &ReturnSize);
86
87 NtClose(KeyHandle);
88
89 if (!NT_SUCCESS(Status))
90 {
91 *nSize = ReturnSize;
92 goto failed;
93 }
94
95 if (KeyInfo->Type != REG_SZ)
96 {
97 Status = STATUS_UNSUCCESSFUL;
98 goto failed;
99 }
100
101 if (!lpBuffer || *nSize < (KeyInfo->DataLength / sizeof(WCHAR)))
102 {
103 *nSize = ReturnSize;
104 Status = STATUS_BUFFER_OVERFLOW;
105 goto failed;
106 }
107
108 *nSize = KeyInfo->DataLength / sizeof(WCHAR) - 1;
109 RtlCopyMemory(lpBuffer, KeyInfo->Data, KeyInfo->DataLength);
110 lpBuffer[*nSize] = 0;
111
112 RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo);
113
114 return TRUE;
115
116 failed:
117 RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo);
118 BaseSetLastNTError(Status);
119 return FALSE;
120 }
121
122 /*
123 * @implemented
124 */
125 BOOL
126 WINAPI
127 GetComputerNameExW(COMPUTER_NAME_FORMAT NameType,
128 LPWSTR lpBuffer,
129 LPDWORD nSize)
130 {
131 UNICODE_STRING ResultString;
132 UNICODE_STRING DomainPart;
133 RTL_QUERY_REGISTRY_TABLE QueryTable[2];
134 NTSTATUS Status;
135 BOOL ret = TRUE;
136 DWORD HostSize;
137
138 switch (NameType)
139 {
140 case ComputerNameNetBIOS:
141 return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
142 L"\\Control\\ComputerName\\ComputerName",
143 L"ComputerName",
144 lpBuffer,
145 nSize);
146
147 case ComputerNameDnsDomain:
148 return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
149 L"\\Services\\Tcpip\\Parameters",
150 L"Domain",
151 lpBuffer,
152 nSize);
153
154 case ComputerNameDnsFullyQualified:
155 ResultString.Length = 0;
156 ResultString.MaximumLength = (USHORT)*nSize * sizeof(WCHAR);
157 ResultString.Buffer = lpBuffer;
158
159 RtlZeroMemory(QueryTable, sizeof(QueryTable));
160 RtlInitUnicodeString(&DomainPart, NULL);
161
162 QueryTable[0].Name = L"HostName";
163 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
164 QueryTable[0].EntryContext = &DomainPart;
165
166 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
167 L"\\Registry\\Machine\\System"
168 L"\\CurrentControlSet\\Services\\Tcpip"
169 L"\\Parameters",
170 QueryTable,
171 NULL,
172 NULL);
173
174 if (NT_SUCCESS(Status))
175 {
176 Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart);
177 HostSize = DomainPart.Length;
178
179 if (!NT_SUCCESS(Status))
180 {
181 ret = FALSE;
182 }
183
184 RtlAppendUnicodeToString(&ResultString, L".");
185 RtlFreeUnicodeString(&DomainPart);
186
187 RtlInitUnicodeString(&DomainPart, NULL);
188 QueryTable[0].Name = L"Domain";
189 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
190 QueryTable[0].EntryContext = &DomainPart;
191
192 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
193 L"\\Registry\\Machine\\System"
194 L"\\CurrentControlSet\\Services\\Tcpip"
195 L"\\Parameters",
196 QueryTable,
197 NULL,
198 NULL);
199
200 if (NT_SUCCESS(Status))
201 {
202 Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart);
203 if ((!NT_SUCCESS(Status)) || (!ret))
204 {
205 *nSize = HostSize + DomainPart.Length;
206 SetLastError(ERROR_MORE_DATA);
207 RtlFreeUnicodeString(&DomainPart);
208 return FALSE;
209 }
210 RtlFreeUnicodeString(&DomainPart);
211 *nSize = ResultString.Length / sizeof(WCHAR) - 1;
212 return TRUE;
213 }
214 }
215 return FALSE;
216
217 case ComputerNameDnsHostname:
218 return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
219 L"\\Services\\Tcpip\\Parameters",
220 L"Hostname",
221 lpBuffer,
222 nSize);
223
224 case ComputerNamePhysicalDnsDomain:
225 return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
226 L"\\Services\\Tcpip\\Parameters",
227 L"Domain",
228 lpBuffer,
229 nSize);
230
231 /* XXX Redo these */
232 case ComputerNamePhysicalDnsFullyQualified:
233 return GetComputerNameExW(ComputerNameDnsFullyQualified,
234 lpBuffer,
235 nSize);
236
237 case ComputerNamePhysicalDnsHostname:
238 return GetComputerNameExW(ComputerNameDnsHostname,
239 lpBuffer,
240 nSize);
241
242 case ComputerNamePhysicalNetBIOS:
243 return GetComputerNameExW(ComputerNameNetBIOS,
244 lpBuffer,
245 nSize);
246
247 case ComputerNameMax:
248 return FALSE;
249 }
250
251 return FALSE;
252 }
253
254 /*
255 * @implemented
256 */
257 BOOL
258 WINAPI
259 GetComputerNameExA(COMPUTER_NAME_FORMAT NameType,
260 LPSTR lpBuffer,
261 LPDWORD nSize)
262 {
263 UNICODE_STRING UnicodeString;
264 ANSI_STRING AnsiString;
265 BOOL Result;
266 PWCHAR TempBuffer = RtlAllocateHeap( RtlGetProcessHeap(), 0, *nSize * sizeof(WCHAR) );
267
268 if (!TempBuffer)
269 {
270 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
271 return FALSE;
272 }
273
274 AnsiString.MaximumLength = (USHORT)*nSize;
275 AnsiString.Length = 0;
276 AnsiString.Buffer = lpBuffer;
277
278 Result = GetComputerNameExW(NameType, TempBuffer, nSize);
279
280 if (Result)
281 {
282 UnicodeString.MaximumLength = (USHORT)*nSize * sizeof(WCHAR) + sizeof(WCHAR);
283 UnicodeString.Length = (USHORT)*nSize * sizeof(WCHAR) + sizeof(WCHAR);
284 UnicodeString.Buffer = TempBuffer;
285
286 RtlUnicodeStringToAnsiString(&AnsiString,
287 &UnicodeString,
288 FALSE);
289 }
290
291 RtlFreeHeap(RtlGetProcessHeap(), 0, TempBuffer);
292
293 return Result;
294 }
295
296 /*
297 * @implemented
298 */
299 BOOL
300 WINAPI
301 GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize)
302 {
303 BOOL ret;
304 ret = GetComputerNameExA(ComputerNameNetBIOS, lpBuffer, lpnSize);
305 if(!ret && GetLastError() == ERROR_MORE_DATA)
306 SetLastError(ERROR_BUFFER_OVERFLOW);
307 return ret;
308 }
309
310
311 /*
312 * @implemented
313 */
314 BOOL
315 WINAPI
316 GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize)
317 {
318 BOOL ret;
319 ret=GetComputerNameExW(ComputerNameNetBIOS, lpBuffer, lpnSize);
320 if(!ret && GetLastError() == ERROR_MORE_DATA)
321 SetLastError(ERROR_BUFFER_OVERFLOW);
322 return ret;
323 }
324
325
326 /*
327 * @implemented
328 */
329 static
330 BOOL
331 IsValidComputerName(COMPUTER_NAME_FORMAT NameType,
332 LPCWSTR lpComputerName)
333 {
334 PWCHAR p;
335 ULONG Length;
336
337 /* FIXME: do verification according to NameType */
338
339 Length = 0;
340 p = (PWCHAR)lpComputerName;
341
342 while (*p != 0)
343 {
344 if (!(iswctype(*p, _ALPHA | _DIGIT) || *p == L'!' || *p == L'@' || *p == L'#' ||
345 *p == L'$' || *p == L'%' || *p == L'^' || *p == L'&' || *p == L'\'' ||
346 *p == L')' || *p == L'(' || *p == L'.' || *p == L'-' || *p == L'_' ||
347 *p == L'{' || *p == L'}' || *p == L'~'))
348 return FALSE;
349
350 Length++;
351 p++;
352 }
353
354 if (Length == 0 || Length > MAX_COMPUTERNAME_LENGTH)
355 return FALSE;
356
357 return TRUE;
358 }
359
360
361 static
362 BOOL
363 SetComputerNameToRegistry(LPCWSTR RegistryKey,
364 LPCWSTR ValueNameStr,
365 LPCWSTR lpBuffer)
366 {
367 OBJECT_ATTRIBUTES ObjectAttributes;
368 UNICODE_STRING KeyName;
369 UNICODE_STRING ValueName;
370 HANDLE KeyHandle;
371 NTSTATUS Status;
372
373 RtlInitUnicodeString(&KeyName, RegistryKey);
374 InitializeObjectAttributes(&ObjectAttributes,
375 &KeyName,
376 OBJ_CASE_INSENSITIVE,
377 NULL,
378 NULL);
379
380 Status = NtOpenKey(&KeyHandle,
381 KEY_WRITE,
382 &ObjectAttributes);
383 if (!NT_SUCCESS(Status))
384 {
385 BaseSetLastNTError(Status);
386 return FALSE;
387 }
388
389 RtlInitUnicodeString(&ValueName, ValueNameStr);
390
391 Status = NtSetValueKey(KeyHandle,
392 &ValueName,
393 0,
394 REG_SZ,
395 (PVOID)lpBuffer,
396 (wcslen (lpBuffer) + 1) * sizeof(WCHAR));
397 if (!NT_SUCCESS(Status))
398 {
399 NtClose(KeyHandle);
400 BaseSetLastNTError(Status);
401 return FALSE;
402 }
403
404 NtFlushKey(KeyHandle);
405 NtClose(KeyHandle);
406
407 return TRUE;
408 }
409
410
411 /*
412 * @implemented
413 */
414 BOOL
415 WINAPI
416 SetComputerNameA(LPCSTR lpComputerName)
417 {
418 return SetComputerNameExA(ComputerNamePhysicalNetBIOS, lpComputerName);
419 }
420
421
422 /*
423 * @implemented
424 */
425 BOOL
426 WINAPI
427 SetComputerNameW(LPCWSTR lpComputerName)
428 {
429 return SetComputerNameExW(ComputerNamePhysicalNetBIOS, lpComputerName);
430 }
431
432
433 /*
434 * @implemented
435 */
436 BOOL
437 WINAPI
438 SetComputerNameExA(COMPUTER_NAME_FORMAT NameType,
439 LPCSTR lpBuffer)
440 {
441 UNICODE_STRING Buffer;
442 BOOL bResult;
443
444 RtlCreateUnicodeStringFromAsciiz(&Buffer, (LPSTR)lpBuffer);
445
446 bResult = SetComputerNameExW(NameType, Buffer.Buffer);
447
448 RtlFreeUnicodeString(&Buffer);
449
450 return bResult;
451 }
452
453
454 /*
455 * @implemented
456 */
457 BOOL
458 WINAPI
459 SetComputerNameExW(COMPUTER_NAME_FORMAT NameType,
460 LPCWSTR lpBuffer)
461 {
462 if (!IsValidComputerName(NameType, lpBuffer))
463 {
464 SetLastError(ERROR_INVALID_PARAMETER);
465 return FALSE;
466 }
467
468 switch( NameType )
469 {
470 case ComputerNamePhysicalDnsDomain:
471 return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
472 L"\\Services\\Tcpip\\Parameters",
473 L"Domain",
474 lpBuffer);
475
476 case ComputerNamePhysicalDnsHostname:
477 return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
478 L"\\Services\\Tcpip\\Parameters",
479 L"Hostname",
480 lpBuffer);
481
482 case ComputerNamePhysicalNetBIOS:
483 return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
484 L"\\Control\\ComputerName\\ComputerName",
485 L"ComputerName",
486 lpBuffer);
487
488 default:
489 SetLastError (ERROR_INVALID_PARAMETER);
490 return FALSE;
491 }
492 }
493
494
495 /*
496 * @implemented
497 */
498 BOOL
499 WINAPI
500 DnsHostnameToComputerNameA(LPCSTR Hostname,
501 LPSTR ComputerName,
502 LPDWORD nSize)
503 {
504 DWORD len;
505
506 DPRINT("(%s, %p, %p)\n", Hostname, ComputerName, nSize);
507
508 if (!Hostname || !nSize)
509 return FALSE;
510
511 len = lstrlenA(Hostname);
512
513 if (len > MAX_COMPUTERNAME_LENGTH)
514 len = MAX_COMPUTERNAME_LENGTH;
515
516 if (*nSize < len)
517 {
518 *nSize = len;
519 return FALSE;
520 }
521
522 if (!ComputerName) return FALSE;
523
524 memcpy(ComputerName, Hostname, len);
525 ComputerName[len + 1] = 0;
526 return TRUE;
527 }
528
529
530 /*
531 * @implemented
532 */
533 BOOL
534 WINAPI
535 DnsHostnameToComputerNameW(LPCWSTR hostname,
536 LPWSTR computername,
537 LPDWORD size)
538 {
539 DWORD len;
540
541 DPRINT("(%s, %p, %p): stub\n", hostname, computername, size);
542
543 if (!hostname || !size) return FALSE;
544 len = lstrlenW(hostname);
545
546 if (len > MAX_COMPUTERNAME_LENGTH)
547 len = MAX_COMPUTERNAME_LENGTH;
548
549 if (*size < len)
550 {
551 *size = len;
552 return FALSE;
553 }
554 if (!computername) return FALSE;
555
556 memcpy(computername, hostname, len * sizeof(WCHAR));
557 computername[len + 1] = 0;
558 return TRUE;
559 }
560
561 DWORD
562 WINAPI
563 AddLocalAlternateComputerNameA(LPSTR lpName, PNTSTATUS Status)
564 {
565 STUB;
566 return 0;
567 }
568
569 DWORD
570 WINAPI
571 AddLocalAlternateComputerNameW(LPWSTR lpName, PNTSTATUS Status)
572 {
573 STUB;
574 return 0;
575 }
576
577 DWORD
578 WINAPI
579 EnumerateLocalComputerNamesA(PVOID pUnknown, DWORD Size, LPSTR lpBuffer, LPDWORD lpnSize)
580 {
581 STUB;
582 return ERROR_CALL_NOT_IMPLEMENTED;
583 }
584
585 DWORD
586 WINAPI
587 EnumerateLocalComputerNamesW(PVOID pUnknown, DWORD Size, LPWSTR lpBuffer, LPDWORD lpnSize)
588 {
589 STUB;
590 return ERROR_CALL_NOT_IMPLEMENTED;
591 }
592
593 DWORD
594 WINAPI
595 RemoveLocalAlternateComputerNameA(LPSTR lpName, DWORD Unknown)
596 {
597 STUB;
598 return ERROR_CALL_NOT_IMPLEMENTED;
599 }
600
601 DWORD
602 WINAPI
603 RemoveLocalAlternateComputerNameW(LPWSTR lpName, DWORD Unknown)
604 {
605 STUB;
606 return ERROR_CALL_NOT_IMPLEMENTED;
607 }
608
609 /*
610 * @unimplemented
611 */
612 BOOL
613 WINAPI
614 SetLocalPrimaryComputerNameA(IN DWORD Unknown1,
615 IN DWORD Unknown2)
616 {
617 STUB;
618 return FALSE;
619 }
620
621 /*
622 * @unimplemented
623 */
624 BOOL
625 WINAPI
626 SetLocalPrimaryComputerNameW(IN DWORD Unknown1,
627 IN DWORD Unknown2)
628 {
629 STUB;
630 return FALSE;
631 }
632
633
634 /* EOF */