ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

compname.c
Go to the documentation of this file.
00001 /*
00002  *  ReactOS kernel
00003  *  Copyright (C) 2003 ReactOS Team
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  */
00019 /*
00020  * COPYRIGHT:       See COPYING in the top level directory
00021  * PROJECT:         ReactOS system libraries
00022  * PURPOSE:         Computer name functions
00023  * FILE:            lib/kernel32/misc/computername.c
00024  * PROGRAMER:       Eric Kohl
00025  */
00026 
00027 /* INCLUDES ******************************************************************/
00028 
00029 #include <k32.h>
00030 
00031 #define NDEBUG
00032 #include <debug.h>
00033 
00034 
00035 /* FUNCTIONS *****************************************************************/
00036 
00037 static
00038 BOOL
00039 GetComputerNameFromRegistry(LPWSTR RegistryKey,
00040                             LPWSTR ValueNameStr,
00041                             LPWSTR lpBuffer,
00042                             LPDWORD nSize)
00043 {
00044     PKEY_VALUE_PARTIAL_INFORMATION KeyInfo;
00045     OBJECT_ATTRIBUTES ObjectAttributes;
00046     UNICODE_STRING KeyName;
00047     UNICODE_STRING ValueName;
00048     HANDLE KeyHandle;
00049     ULONG KeyInfoSize;
00050     ULONG ReturnSize;
00051     NTSTATUS Status;
00052 
00053     RtlInitUnicodeString(&KeyName, RegistryKey);
00054     InitializeObjectAttributes(&ObjectAttributes,
00055                                &KeyName,
00056                                OBJ_CASE_INSENSITIVE,
00057                                NULL,
00058                                NULL);
00059 
00060     Status = NtOpenKey(&KeyHandle,
00061                        KEY_READ,
00062                        &ObjectAttributes);
00063     if (!NT_SUCCESS(Status))
00064     {
00065         BaseSetLastNTError (Status);
00066         return FALSE;
00067     }
00068 
00069     KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + *nSize * sizeof(WCHAR);
00070     KeyInfo = RtlAllocateHeap(RtlGetProcessHeap(), 0, KeyInfoSize);
00071     if (KeyInfo == NULL)
00072     {
00073         NtClose(KeyHandle);
00074         SetLastError(ERROR_OUTOFMEMORY);
00075         return FALSE;
00076     }
00077 
00078     RtlInitUnicodeString(&ValueName, ValueNameStr);
00079 
00080     Status = NtQueryValueKey(KeyHandle,
00081                              &ValueName,
00082                              KeyValuePartialInformation,
00083                              KeyInfo,
00084                              KeyInfoSize,
00085                              &ReturnSize);
00086 
00087     NtClose(KeyHandle);
00088 
00089     if (!NT_SUCCESS(Status))
00090     {
00091         *nSize = ReturnSize;
00092         goto failed;
00093     }
00094 
00095     if (KeyInfo->Type != REG_SZ)
00096     {
00097         Status = STATUS_UNSUCCESSFUL;
00098         goto failed;
00099     }
00100 
00101     if (!lpBuffer || *nSize < (KeyInfo->DataLength / sizeof(WCHAR)))
00102     {
00103         *nSize = ReturnSize;
00104         Status = STATUS_BUFFER_OVERFLOW;
00105         goto failed;
00106     }
00107 
00108     *nSize = KeyInfo->DataLength / sizeof(WCHAR) - 1;
00109     RtlCopyMemory(lpBuffer, KeyInfo->Data, KeyInfo->DataLength);
00110     lpBuffer[*nSize] = 0;
00111 
00112     RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo);
00113 
00114     return TRUE;
00115 
00116 failed:
00117     RtlFreeHeap(RtlGetProcessHeap(), 0, KeyInfo);
00118     BaseSetLastNTError(Status);
00119     return FALSE;
00120 }
00121 
00122 /*
00123  * @implemented
00124  */
00125 BOOL
00126 WINAPI
00127 GetComputerNameExW(COMPUTER_NAME_FORMAT NameType,
00128                    LPWSTR lpBuffer,
00129                    LPDWORD nSize)
00130 {
00131     UNICODE_STRING ResultString;
00132     UNICODE_STRING DomainPart;
00133     RTL_QUERY_REGISTRY_TABLE QueryTable[2];
00134     NTSTATUS Status;
00135     BOOL ret = TRUE;
00136     DWORD HostSize;
00137 
00138     switch (NameType)
00139     {
00140         case ComputerNameNetBIOS:
00141             return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
00142                                                L"\\Control\\ComputerName\\ComputerName",
00143                                                L"ComputerName",
00144                                                lpBuffer,
00145                                                nSize);
00146 
00147         case ComputerNameDnsDomain:
00148             return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
00149                                                L"\\Services\\Tcpip\\Parameters",
00150                                                L"Domain",
00151                                                lpBuffer,
00152                                                nSize);
00153 
00154         case ComputerNameDnsFullyQualified:
00155             ResultString.Length = 0;
00156             ResultString.MaximumLength = (USHORT)*nSize * sizeof(WCHAR);
00157             ResultString.Buffer = lpBuffer;
00158 
00159             RtlZeroMemory(QueryTable, sizeof(QueryTable));
00160             RtlInitUnicodeString(&DomainPart, NULL);
00161 
00162             QueryTable[0].Name = L"HostName";
00163             QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
00164             QueryTable[0].EntryContext = &DomainPart;
00165 
00166             Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
00167                                             L"\\Registry\\Machine\\System"
00168                                             L"\\CurrentControlSet\\Services\\Tcpip"
00169                                             L"\\Parameters",
00170                                             QueryTable,
00171                                             NULL,
00172                                             NULL);
00173 
00174             if (NT_SUCCESS(Status))
00175             {
00176                 Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart);
00177                 HostSize = DomainPart.Length;
00178 
00179                 if (!NT_SUCCESS(Status))
00180                 {
00181                     ret = FALSE;
00182                 }
00183 
00184                 RtlAppendUnicodeToString(&ResultString, L".");
00185                 RtlFreeUnicodeString(&DomainPart);
00186 
00187                 RtlInitUnicodeString(&DomainPart, NULL);
00188                 QueryTable[0].Name = L"Domain";
00189                 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
00190                 QueryTable[0].EntryContext = &DomainPart;
00191 
00192                 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
00193                                                 L"\\Registry\\Machine\\System"
00194                                                 L"\\CurrentControlSet\\Services\\Tcpip"
00195                                                 L"\\Parameters",
00196                                                 QueryTable,
00197                                                 NULL,
00198                                                 NULL);
00199 
00200                 if (NT_SUCCESS(Status))
00201                 {
00202                     Status = RtlAppendUnicodeStringToString(&ResultString, &DomainPart);
00203                     if ((!NT_SUCCESS(Status)) || (!ret))
00204                     {
00205                         *nSize = HostSize + DomainPart.Length;
00206                         SetLastError(ERROR_MORE_DATA);
00207                         RtlFreeUnicodeString(&DomainPart);
00208                         return FALSE;
00209                     }
00210                     RtlFreeUnicodeString(&DomainPart);
00211                     *nSize = ResultString.Length / sizeof(WCHAR) - 1;
00212                     return TRUE;
00213                 }
00214             }
00215             return FALSE;
00216 
00217         case ComputerNameDnsHostname:
00218             return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
00219                                                L"\\Services\\Tcpip\\Parameters",
00220                                                L"Hostname",
00221                                                lpBuffer,
00222                                                nSize);
00223 
00224         case ComputerNamePhysicalDnsDomain:
00225             return GetComputerNameFromRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
00226                                                L"\\Services\\Tcpip\\Parameters",
00227                                                L"Domain",
00228                                                lpBuffer,
00229                                                nSize);
00230 
00231         /* XXX Redo these */
00232         case ComputerNamePhysicalDnsFullyQualified:
00233             return GetComputerNameExW(ComputerNameDnsFullyQualified,
00234                                       lpBuffer,
00235                                       nSize);
00236 
00237         case ComputerNamePhysicalDnsHostname:
00238             return GetComputerNameExW(ComputerNameDnsHostname,
00239                                       lpBuffer,
00240                                       nSize);
00241 
00242         case ComputerNamePhysicalNetBIOS:
00243             return GetComputerNameExW(ComputerNameNetBIOS,
00244                                       lpBuffer,
00245                                       nSize);
00246 
00247         case ComputerNameMax:
00248             return FALSE;
00249     }
00250 
00251     return FALSE;
00252 }
00253 
00254 /*
00255  * @implemented
00256  */
00257 BOOL
00258 WINAPI
00259 GetComputerNameExA(COMPUTER_NAME_FORMAT NameType,
00260                    LPSTR lpBuffer,
00261                    LPDWORD nSize)
00262 {
00263     UNICODE_STRING UnicodeString;
00264     ANSI_STRING AnsiString;
00265     BOOL Result;
00266     PWCHAR TempBuffer = RtlAllocateHeap( RtlGetProcessHeap(), 0, *nSize * sizeof(WCHAR) );
00267 
00268     if (!TempBuffer)
00269     {
00270         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
00271         return FALSE;
00272     }
00273 
00274     AnsiString.MaximumLength = (USHORT)*nSize;
00275     AnsiString.Length = 0;
00276     AnsiString.Buffer = lpBuffer;
00277 
00278     Result = GetComputerNameExW(NameType, TempBuffer, nSize);
00279 
00280     if (Result)
00281     {
00282         UnicodeString.MaximumLength = (USHORT)*nSize * sizeof(WCHAR) + sizeof(WCHAR);
00283         UnicodeString.Length = (USHORT)*nSize * sizeof(WCHAR) + sizeof(WCHAR);
00284         UnicodeString.Buffer = TempBuffer;
00285 
00286         RtlUnicodeStringToAnsiString(&AnsiString,
00287                                      &UnicodeString,
00288                                      FALSE);
00289     }
00290 
00291     RtlFreeHeap(RtlGetProcessHeap(), 0, TempBuffer);
00292 
00293     return Result;
00294 }
00295 
00296 /*
00297  * @implemented
00298  */
00299 BOOL
00300 WINAPI
00301 GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize)
00302 {
00303     BOOL ret;
00304     ret = GetComputerNameExA(ComputerNameNetBIOS, lpBuffer, lpnSize);    
00305     if(!ret && GetLastError() == ERROR_MORE_DATA)
00306       SetLastError(ERROR_BUFFER_OVERFLOW);
00307       return ret;
00308 }
00309 
00310 
00311 /*
00312  * @implemented
00313  */
00314 BOOL
00315 WINAPI
00316 GetComputerNameW(LPWSTR lpBuffer, LPDWORD lpnSize)
00317 {
00318     BOOL ret;
00319     ret=GetComputerNameExW(ComputerNameNetBIOS, lpBuffer, lpnSize);
00320     if(!ret && GetLastError() == ERROR_MORE_DATA)
00321       SetLastError(ERROR_BUFFER_OVERFLOW);
00322     return ret;
00323 }
00324 
00325 
00326 /*
00327  * @implemented
00328  */
00329 static
00330 BOOL
00331 IsValidComputerName(COMPUTER_NAME_FORMAT NameType,
00332                     LPCWSTR lpComputerName)
00333 {
00334     PWCHAR p;
00335     ULONG Length;
00336 
00337     /* FIXME: do verification according to NameType */
00338 
00339     Length = 0;
00340     p = (PWCHAR)lpComputerName;
00341 
00342     while (*p != 0)
00343     {
00344         if (!(iswctype(*p, _ALPHA | _DIGIT) || *p == L'!' || *p == L'@' || *p == L'#' ||
00345             *p == L'$' || *p == L'%' || *p == L'^' || *p == L'&' || *p == L'\'' ||
00346             *p == L')' || *p == L'(' || *p == L'.' || *p == L'-' || *p == L'_' ||
00347             *p == L'{' || *p == L'}' || *p == L'~'))
00348             return FALSE;
00349 
00350         Length++;
00351         p++;
00352     }
00353 
00354     if (Length == 0 || Length > MAX_COMPUTERNAME_LENGTH)
00355         return FALSE;
00356 
00357     return TRUE;
00358 }
00359 
00360 
00361 static
00362 BOOL
00363 SetComputerNameToRegistry(LPCWSTR RegistryKey,
00364                           LPCWSTR ValueNameStr,
00365                           LPCWSTR lpBuffer)
00366 {
00367     OBJECT_ATTRIBUTES ObjectAttributes;
00368     UNICODE_STRING KeyName;
00369     UNICODE_STRING ValueName;
00370     HANDLE KeyHandle;
00371     NTSTATUS Status;
00372 
00373     RtlInitUnicodeString(&KeyName, RegistryKey);
00374     InitializeObjectAttributes(&ObjectAttributes,
00375                                &KeyName,
00376                                OBJ_CASE_INSENSITIVE,
00377                                NULL,
00378                                NULL);
00379 
00380     Status = NtOpenKey(&KeyHandle,
00381                        KEY_WRITE,
00382                        &ObjectAttributes);
00383     if (!NT_SUCCESS(Status))
00384     {
00385         BaseSetLastNTError(Status);
00386         return FALSE;
00387     }
00388 
00389     RtlInitUnicodeString(&ValueName, ValueNameStr);
00390 
00391     Status = NtSetValueKey(KeyHandle,
00392                            &ValueName,
00393                            0,
00394                            REG_SZ,
00395                            (PVOID)lpBuffer,
00396                            (wcslen (lpBuffer) + 1) * sizeof(WCHAR));
00397     if (!NT_SUCCESS(Status))
00398     {
00399         NtClose(KeyHandle);
00400         BaseSetLastNTError(Status);
00401         return FALSE;
00402     }
00403 
00404     NtFlushKey(KeyHandle);
00405     NtClose(KeyHandle);
00406 
00407     return TRUE;
00408 }
00409 
00410 
00411 /*
00412  * @implemented
00413  */
00414 BOOL
00415 WINAPI
00416 SetComputerNameA(LPCSTR lpComputerName)
00417 {
00418     return SetComputerNameExA(ComputerNamePhysicalNetBIOS, lpComputerName);
00419 }
00420 
00421 
00422 /*
00423  * @implemented
00424  */
00425 BOOL
00426 WINAPI
00427 SetComputerNameW(LPCWSTR lpComputerName)
00428 {
00429     return SetComputerNameExW(ComputerNamePhysicalNetBIOS, lpComputerName);
00430 }
00431 
00432 
00433 /*
00434  * @implemented
00435  */
00436 BOOL
00437 WINAPI
00438 SetComputerNameExA(COMPUTER_NAME_FORMAT NameType,
00439                    LPCSTR lpBuffer)
00440 {
00441     UNICODE_STRING Buffer;
00442     BOOL bResult;
00443 
00444     RtlCreateUnicodeStringFromAsciiz(&Buffer, (LPSTR)lpBuffer);
00445 
00446     bResult = SetComputerNameExW(NameType, Buffer.Buffer);
00447 
00448     RtlFreeUnicodeString(&Buffer);
00449 
00450     return bResult;
00451 }
00452 
00453 
00454 /*
00455  * @implemented
00456  */
00457 BOOL
00458 WINAPI
00459 SetComputerNameExW(COMPUTER_NAME_FORMAT NameType,
00460                    LPCWSTR lpBuffer)
00461 {
00462     if (!IsValidComputerName(NameType, lpBuffer))
00463     {
00464         SetLastError(ERROR_INVALID_PARAMETER);
00465         return FALSE;
00466     }
00467 
00468     switch( NameType )
00469     {
00470         case ComputerNamePhysicalDnsDomain:
00471             return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
00472                                              L"\\Services\\Tcpip\\Parameters",
00473                                              L"Domain",
00474                                              lpBuffer);
00475 
00476         case ComputerNamePhysicalDnsHostname:
00477             return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
00478                                              L"\\Services\\Tcpip\\Parameters",
00479                                              L"Hostname",
00480                                              lpBuffer);
00481 
00482         case ComputerNamePhysicalNetBIOS:
00483             return SetComputerNameToRegistry(L"\\Registry\\Machine\\System\\CurrentControlSet"
00484                                              L"\\Control\\ComputerName\\ComputerName",
00485                                              L"ComputerName",
00486                                              lpBuffer);
00487 
00488         default:
00489             SetLastError (ERROR_INVALID_PARAMETER);
00490             return FALSE;
00491     }
00492 }
00493 
00494 
00495 /*
00496  * @implemented
00497  */
00498 BOOL
00499 WINAPI
00500 DnsHostnameToComputerNameA(LPCSTR Hostname,
00501                            LPSTR ComputerName,
00502                            LPDWORD nSize)
00503 {
00504     DWORD len;
00505 
00506     DPRINT("(%s, %p, %p)\n", Hostname, ComputerName, nSize);
00507 
00508     if (!Hostname || !nSize)
00509         return FALSE;
00510 
00511     len = lstrlenA(Hostname);
00512 
00513     if (len > MAX_COMPUTERNAME_LENGTH)
00514         len = MAX_COMPUTERNAME_LENGTH;
00515 
00516     if (*nSize < len)
00517     {
00518         *nSize = len;
00519         return FALSE;
00520     }
00521 
00522     if (!ComputerName) return FALSE;
00523 
00524     memcpy(ComputerName, Hostname, len);
00525     ComputerName[len + 1] = 0;
00526     return TRUE;
00527 }
00528 
00529 
00530 /*
00531  * @implemented
00532  */
00533 BOOL
00534 WINAPI
00535 DnsHostnameToComputerNameW(LPCWSTR hostname,
00536                            LPWSTR computername,
00537                            LPDWORD size)
00538 {
00539     DWORD len;
00540 
00541     DPRINT("(%s, %p, %p): stub\n", hostname, computername, size);
00542 
00543     if (!hostname || !size) return FALSE;
00544     len = lstrlenW(hostname);
00545 
00546     if (len > MAX_COMPUTERNAME_LENGTH)
00547         len = MAX_COMPUTERNAME_LENGTH;
00548 
00549     if (*size < len)
00550     {
00551         *size = len;
00552         return FALSE;
00553     }
00554     if (!computername) return FALSE;
00555 
00556     memcpy(computername, hostname, len * sizeof(WCHAR));
00557     computername[len + 1] = 0;
00558     return TRUE;
00559 }
00560 
00561 DWORD
00562 WINAPI
00563 AddLocalAlternateComputerNameA(LPSTR lpName, PNTSTATUS Status)
00564 {
00565     STUB;
00566     return 0;
00567 }
00568 
00569 DWORD
00570 WINAPI
00571 AddLocalAlternateComputerNameW(LPWSTR lpName, PNTSTATUS Status)
00572 {
00573     STUB;
00574     return 0;
00575 }
00576 
00577 DWORD
00578 WINAPI
00579 EnumerateLocalComputerNamesA(PVOID pUnknown, DWORD Size, LPSTR lpBuffer, LPDWORD lpnSize)
00580 {
00581     STUB;
00582     return ERROR_CALL_NOT_IMPLEMENTED;
00583 }
00584 
00585 DWORD
00586 WINAPI
00587 EnumerateLocalComputerNamesW(PVOID pUnknown, DWORD Size, LPWSTR lpBuffer, LPDWORD lpnSize)
00588 {
00589     STUB;
00590     return ERROR_CALL_NOT_IMPLEMENTED;
00591 }
00592 
00593 DWORD
00594 WINAPI
00595 RemoveLocalAlternateComputerNameA(LPSTR lpName, DWORD Unknown)
00596 {
00597     STUB;
00598     return ERROR_CALL_NOT_IMPLEMENTED;
00599 }
00600 
00601 DWORD
00602 WINAPI
00603 RemoveLocalAlternateComputerNameW(LPWSTR lpName, DWORD Unknown)
00604 {
00605     STUB;
00606     return ERROR_CALL_NOT_IMPLEMENTED;
00607 }
00608 
00609 /*
00610  * @unimplemented
00611  */
00612 BOOL
00613 WINAPI
00614 SetLocalPrimaryComputerNameA(IN DWORD Unknown1,
00615                              IN DWORD Unknown2)
00616 {
00617     STUB;
00618     return FALSE;
00619 }
00620 
00621 /*
00622  * @unimplemented
00623  */
00624 BOOL
00625 WINAPI
00626 SetLocalPrimaryComputerNameW(IN DWORD Unknown1,
00627                              IN DWORD Unknown2)
00628 {
00629     STUB;
00630     return FALSE;
00631 }
00632 
00633 
00634 /* EOF */

Generated on Sat May 26 2012 04:22:56 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.