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