Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.6 KiB

  1. /*************************************************************************
  2. *
  3. * domname.c
  4. *
  5. * Get domain name given PDC's server name.
  6. *
  7. * This was a ripoff of NetpGetDomainNameEx.
  8. *
  9. * Copyright (c) 1998 Microsoft Corporation
  10. *
  11. *
  12. *************************************************************************/
  13. /*
  14. * Includes
  15. */
  16. #include <nt.h> // NT definitions (temporary)
  17. #include <ntrtl.h> // NT Rtl structure definitions (temporary)
  18. #include <ntlsa.h>
  19. #include <windef.h> // Win32 type definitions
  20. #include <lmcons.h> // LAN Manager common definitions
  21. #include <lmerr.h> // LAN Manager error code
  22. #include <lmapibuf.h> // NetApiBufferAllocate()
  23. #include <winerror.h> // ERROR_ equates, NO_ERROR.
  24. #undef DBGPRINT
  25. #define DBGPRINT(_x_) DbgPrint _x_
  26. NTSTATUS
  27. GetDomainName (
  28. IN PWCHAR ServerNamePtr, // name of server to get domain of
  29. OUT PWCHAR *DomainNamePtr // alloc and set ptr (free with NetApiBufferFree)
  30. )
  31. /*++
  32. Routine Description:
  33. Returns the name of the domain or workgroup this machine belongs to.
  34. Arguments:
  35. DomainNamePtr - The name of the domain or workgroup
  36. IsWorkgroupName - Returns TRUE if the name is a workgroup name.
  37. Returns FALSE if the name is a domain name.
  38. Return Value:
  39. NERR_Success - Success.
  40. NERR_CfgCompNotFound - There was an error determining the domain name
  41. --*/
  42. {
  43. NTSTATUS status;
  44. LSA_HANDLE PolicyHandle;
  45. PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo;
  46. OBJECT_ATTRIBUTES ObjAttributes;
  47. UNICODE_STRING UniServerName;
  48. //
  49. // Check for caller's errors.
  50. //
  51. if ( DomainNamePtr == NULL ) {
  52. return STATUS_INVALID_PARAMETER;
  53. }
  54. //
  55. // Open a handle to the local security policy. Initialize the
  56. // objects attributes structure first.
  57. //
  58. InitializeObjectAttributes(
  59. &ObjAttributes,
  60. NULL,
  61. 0L,
  62. NULL,
  63. NULL
  64. );
  65. RtlInitUnicodeString( &UniServerName, ServerNamePtr );
  66. status = LsaOpenPolicy(
  67. &UniServerName,
  68. &ObjAttributes,
  69. POLICY_VIEW_LOCAL_INFORMATION,
  70. &PolicyHandle
  71. );
  72. #ifdef DEBUG
  73. DbgPrint( "GetDomainName: LsaOpenPolicy returned NTSTATUS = 0x%x\n", status );
  74. #endif // DEBUG
  75. if (! NT_SUCCESS(status)) {
  76. return( status );
  77. }
  78. //
  79. // Get the name of the primary domain from LSA
  80. //
  81. status = LsaQueryInformationPolicy(
  82. PolicyHandle,
  83. PolicyAccountDomainInformation,
  84. (PVOID *)&DomainInfo
  85. );
  86. #ifdef DEBUG
  87. DbgPrint( "GetDomainName: LsaQueryInformationPolicy returned NTSTATUS = 0x%x\n", status );
  88. #endif // DEBUG
  89. if (! NT_SUCCESS(status)) {
  90. (void) LsaClose(PolicyHandle);
  91. return( status );
  92. }
  93. (void) LsaClose(PolicyHandle);
  94. if (NetApiBufferAllocate(
  95. DomainInfo->DomainName.Length + sizeof(WCHAR),
  96. DomainNamePtr
  97. ) != NERR_Success) {
  98. (void) LsaFreeMemory((PVOID) DomainInfo);
  99. return( STATUS_INSUFFICIENT_RESOURCES );
  100. }
  101. RtlZeroMemory(
  102. *DomainNamePtr,
  103. DomainInfo->DomainName.Length + sizeof(WCHAR)
  104. );
  105. memcpy(
  106. *DomainNamePtr,
  107. DomainInfo->DomainName.Buffer,
  108. DomainInfo->DomainName.Length
  109. );
  110. (void) LsaFreeMemory((PVOID) DomainInfo);
  111. return( STATUS_SUCCESS );
  112. }