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.

224 lines
5.5 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. enumusers.cxx
  5. Abstract:
  6. enumusers
  7. Author:
  8. Larry Zhu (LZhu) December 1, 2001 Created
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #include "precomp.hxx"
  14. #pragma hdrstop
  15. #include "enumusers.hxx"
  16. NTSTATUS
  17. GetUserInfo(
  18. IN LUID* pLogonId,
  19. IN HANDLE LsaHandle,
  20. IN ULONG AuthPackageId,
  21. OUT VOID** ppUserInfoResponse,
  22. OUT DWORD* pUserInfoResponseLength
  23. )
  24. /*++
  25. Routine Description:
  26. This function asks the MS V1.0 Authentication Package for information on
  27. a specific user.
  28. Arguments:
  29. pLogonId - Supplies the logon id of the user we want information about.
  30. ppUserInfoResponse - Returns a pointer to a structure of information about
  31. the user. This memory is allocated by the authentication package
  32. and must be freed with LsaFreeReturnBuffer when done with it.
  33. pUserInfoResponseLength - Returns the length of the returned information
  34. in number of bytes.
  35. Return Value:
  36. NET_API_STATUS - NERR_Success or reason for failure.
  37. --*/
  38. {
  39. TNtStatus ntstatus;
  40. NTSTATUS AuthPackageStatus = STATUS_UNSUCCESSFUL;
  41. MSV1_0_GETUSERINFO_REQUEST UserInfoRequest;
  42. DebugPrintf(SSPI_LOG, "Calling MsV1_0GetUserInfo\n");
  43. //
  44. // Ask authentication package for user information.
  45. //
  46. UserInfoRequest.MessageType = MsV1_0GetUserInfo;
  47. RtlCopyLuid(&UserInfoRequest.LogonId, pLogonId);
  48. ntstatus DBGCHK = LsaCallAuthenticationPackage(
  49. LsaHandle,
  50. AuthPackageId,
  51. &UserInfoRequest,
  52. sizeof(MSV1_0_GETUSERINFO_REQUEST),
  53. ppUserInfoResponse,
  54. pUserInfoResponseLength,
  55. &AuthPackageStatus
  56. );
  57. if (ntstatus == STATUS_SUCCESS)
  58. {
  59. ntstatus DBGCHK = AuthPackageStatus;
  60. }
  61. return ntstatus;
  62. }
  63. NTSTATUS
  64. EnumUsers(
  65. IN HANDLE LsaHandle,
  66. IN ULONG AuthPackageId,
  67. OUT VOID** ppEnumUsersResponse
  68. )
  69. /*++
  70. Routine Description:
  71. This function asks the MS V1.0 Authentication Package to list all users
  72. who are physically logged on to the local computer.
  73. Arguments:
  74. ppEnumUsersResponse - Returns a pointer to a list of user logon ids. This
  75. memory is allocated by the authentication package and must be freed
  76. with LsaFreeReturnBuffer when done with it.
  77. Return Value:
  78. NET_API_STATUS - NERR_Success or reason for failure.
  79. --*/
  80. {
  81. TNtStatus Status;
  82. NTSTATUS AuthPackageStatus;
  83. MSV1_0_ENUMUSERS_REQUEST EnumUsersRequest;
  84. ULONG EnumUsersResponseLength;
  85. DebugPrintf(SSPI_LOG, "Calling MsV1_0EnumerateUsers\n");
  86. //
  87. // Ask authentication package to enumerate users who are physically
  88. // logged to the local machine.
  89. //
  90. EnumUsersRequest.MessageType = MsV1_0EnumerateUsers;
  91. Status DBGCHK = LsaCallAuthenticationPackage(
  92. LsaHandle,
  93. AuthPackageId,
  94. &EnumUsersRequest,
  95. sizeof(MSV1_0_ENUMUSERS_REQUEST),
  96. ppEnumUsersResponse,
  97. &EnumUsersResponseLength,
  98. &AuthPackageStatus
  99. );
  100. if (NT_SUCCESS(Status))
  101. {
  102. Status DBGCHK = AuthPackageStatus;
  103. }
  104. return Status;
  105. }
  106. VOID __cdecl
  107. main(
  108. IN INT argc,
  109. IN PSTR argv[]
  110. )
  111. {
  112. TNtStatus Status = STATUS_UNSUCCESSFUL;
  113. HANDLE LogonHandle = NULL;
  114. ULONG PackageId = -1;
  115. MSV1_0_ENUMUSERS_RESPONSE* pEnumUsersResponse = NULL;
  116. DWORD UserInfoResponseLength = 0;
  117. DebugPrintf(SSPI_LOG, "Testing MsV1_0EnumerateUsers and MsV1_0GetUserInfo\n");
  118. AUTO_LOG_OPEN(TEXT("enumusers.exe"));
  119. Status DBGCHK = GetLsaHandleAndPackageId(
  120. NTLMSP_NAME_A,
  121. &LogonHandle,
  122. &PackageId
  123. );
  124. if (NT_SUCCESS(Status))
  125. {
  126. Status DBGCHK = EnumUsers(LogonHandle, PackageId, (VOID**) &pEnumUsersResponse);
  127. }
  128. for (ULONG i = 0; NT_SUCCESS(Status) && i < pEnumUsersResponse->NumberOfLoggedOnUsers; i++)
  129. {
  130. MSV1_0_GETUSERINFO_RESPONSE* pUserInfoResponse = NULL;
  131. DebugPrintf(SSPI_LOG, "*************** %#x **********\n", i);
  132. Status DBGCHK = GetUserInfo(
  133. &pEnumUsersResponse->LogonIds[i],
  134. LogonHandle,
  135. PackageId,
  136. (VOID**) &pUserInfoResponse,
  137. &UserInfoResponseLength
  138. );
  139. if (NT_SUCCESS(Status))
  140. { UNICODE_STRING UserSidString = {0};
  141. Status DBGCHK = RtlConvertSidToUnicodeString( &UserSidString, pUserInfoResponse->UserSid, TRUE );
  142. if (NT_SUCCESS(Status) )
  143. {
  144. DebugPrintf(SSPI_LOG, "Sid: %wZ\n", &UserSidString);
  145. DebugPrintf(SSPI_LOG, "UserName: %wZ\n", &pUserInfoResponse->UserName);
  146. DebugPrintf(SSPI_LOG, "LogonDomainName: %wZ\n", &pUserInfoResponse->LogonDomainName);
  147. DebugPrintf(SSPI_LOG, "LogonServer: %wZ\n", &pUserInfoResponse->LogonServer);
  148. DebugPrintf(SSPI_LOG, "LogonType: %#x : %s\n", pUserInfoResponse->LogonType, LogonType2Str(pUserInfoResponse->LogonType));
  149. }
  150. RtlFreeUnicodeString(&UserSidString);
  151. }
  152. if (pUserInfoResponse)
  153. {
  154. LsaFreeReturnBuffer(pUserInfoResponse);
  155. }
  156. }
  157. if (LogonHandle != NULL)
  158. {
  159. LsaDeregisterLogonProcess(LogonHandle);
  160. }
  161. if (pEnumUsersResponse)
  162. {
  163. LsaFreeReturnBuffer(pEnumUsersResponse);
  164. }
  165. AUTO_LOG_CLOSE();
  166. }