Source code of Windows XP (NT5)
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.

211 lines
4.7 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. tgetsid.c
  5. Abstract:
  6. This file is a temporary test for querying the machine's SID.
  7. Author:
  8. Jim Kelly (JimK) 8-10-1994
  9. Environment:
  10. User Mode - Win32
  11. to build: nmake UMTYPE=console UMTEST=tgetsid
  12. Revision History:
  13. --*/
  14. ///////////////////////////////////////////////////////////////////////////////
  15. // //
  16. // Includes //
  17. // //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #include <nt.h>
  20. #include <ntrtl.h>
  21. #include <ntlsa.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. BOOLEAN
  25. GetMachineSid(
  26. OUT PSID *Sid,
  27. OUT PULONG SidLength
  28. );
  29. VOID __cdecl
  30. main( VOID )
  31. {
  32. BOOLEAN
  33. Result;
  34. PSID
  35. Sid;
  36. PISID
  37. ISid;
  38. ULONG
  39. i,
  40. SubCount,
  41. Tmp,
  42. SidLength;
  43. Result = GetMachineSid( &Sid, &SidLength );
  44. if (Result) {
  45. ISid = (PISID)Sid; // pointer to opaque structure
  46. printf("S-%u-", (USHORT)ISid->Revision );
  47. if ( (ISid->IdentifierAuthority.Value[0] != 0) ||
  48. (ISid->IdentifierAuthority.Value[1] != 0) ){
  49. printf("0x%02hx%02hx%02hx%02hx%02hx%02hx",
  50. (USHORT)ISid->IdentifierAuthority.Value[0],
  51. (USHORT)ISid->IdentifierAuthority.Value[1],
  52. (USHORT)ISid->IdentifierAuthority.Value[2],
  53. (USHORT)ISid->IdentifierAuthority.Value[3],
  54. (USHORT)ISid->IdentifierAuthority.Value[4],
  55. (USHORT)ISid->IdentifierAuthority.Value[5] );
  56. } else {
  57. Tmp = (ULONG)ISid->IdentifierAuthority.Value[5] +
  58. (ULONG)(ISid->IdentifierAuthority.Value[4] << 8) +
  59. (ULONG)(ISid->IdentifierAuthority.Value[3] << 16) +
  60. (ULONG)(ISid->IdentifierAuthority.Value[2] << 24);
  61. printf("%lu", Tmp);
  62. }
  63. SubCount = (ULONG)(*RtlSubAuthorityCountSid( Sid ));
  64. for (i = 0; i<SubCount; i++) {
  65. printf("-0x%lx", (*RtlSubAuthoritySid( Sid, i)));
  66. }
  67. }
  68. printf("\n\n Th Tha That's all folks\n");
  69. }
  70. BOOLEAN
  71. GetMachineSid(
  72. OUT PSID *Sid,
  73. OUT PULONG SidLength
  74. )
  75. /*++
  76. Routine Description:
  77. This routine retrieves the sid of this machine's account
  78. domain and returns it in memory allocated with RtlAllocateHeap().
  79. If this machine is a server in a domain, then this SID will
  80. be domain's SID.
  81. Arguments:
  82. Sid - receives a pointer to the returned SID.
  83. SidLength - Receives the length (in bytes) of the returned SID.
  84. Return Value:
  85. TRUE - The SID was allocated and returned.
  86. FALSE - Some error prevented the SID from being returned.
  87. --*/
  88. {
  89. NTSTATUS
  90. Status;
  91. OBJECT_ATTRIBUTES
  92. ObjectAttributes;
  93. LSA_HANDLE
  94. PolicyHandle;
  95. POLICY_ACCOUNT_DOMAIN_INFO
  96. *DomainInfo = NULL;
  97. PSID
  98. ReturnSid;
  99. BOOLEAN
  100. ReturnStatus = FALSE;
  101. InitializeObjectAttributes( &ObjectAttributes,
  102. NULL, // No name
  103. 0, // No attributes
  104. 0, // No root handle
  105. NULL // No SecurityDescriptor
  106. );
  107. Status = LsaOpenPolicy( NULL, // Local System
  108. &ObjectAttributes,
  109. POLICY_VIEW_LOCAL_INFORMATION,
  110. &PolicyHandle
  111. );
  112. if (NT_SUCCESS(Status)) {
  113. Status = LsaQueryInformationPolicy(
  114. PolicyHandle,
  115. PolicyAccountDomainInformation,
  116. &DomainInfo
  117. );
  118. if (NT_SUCCESS(Status)) {
  119. ASSERT(DomainInfo != NULL);
  120. //
  121. // Allocate the return buffer
  122. //
  123. (*SidLength) = RtlLengthSid( DomainInfo->DomainSid );
  124. ReturnSid = RtlAllocateHeap( RtlProcessHeap(), 0, (*SidLength) );
  125. if (ReturnSid != NULL) {
  126. //
  127. // Copy the sid
  128. //
  129. RtlMoveMemory( ReturnSid, DomainInfo->DomainSid, (*SidLength) );
  130. (*Sid) = ReturnSid;
  131. ReturnStatus = TRUE;
  132. }
  133. LsaFreeMemory( DomainInfo );
  134. }
  135. Status = LsaClose( PolicyHandle );
  136. ASSERT(NT_SUCCESS(Status));
  137. }
  138. return(ReturnStatus);
  139. }