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.

204 lines
4.2 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 2000
  6. //
  7. // File: A D T U T I L . C
  8. //
  9. // Contents: Functions to construct audit event parameters
  10. //
  11. //
  12. // History:
  13. // 07-January-2000 kumarp created
  14. //
  15. //------------------------------------------------------------------------
  16. #include "pch.h"
  17. #pragma hdrstop
  18. #include "adtgen.h"
  19. #include "authzp.h"
  20. BOOL
  21. AuthzpGetTokenInfo(
  22. IN HANDLE hToken,
  23. OUT PSID* ppUserSid, OPTIONAL
  24. OUT PLUID pAuthenticationId
  25. )
  26. /*++
  27. Routine Description:
  28. Get user-sid and the user-logon-id from a token.
  29. Arguments:
  30. hToken - handle of token to query
  31. ppUserSid - pointer to user sid
  32. if non NULL, allocate and copy the user sid
  33. from the token. callers must free it using LocalFree
  34. pAuthenticationId - pointer to logon-id
  35. Return Value:
  36. TRUE on success
  37. FALSE otherwise
  38. call GetLastError() to retrieve the errorcode,
  39. Notes:
  40. Caller must have TOKEN_QUERY access right.
  41. --*/
  42. {
  43. BOOL fResult = FALSE;
  44. TOKEN_STATISTICS TokenStats;
  45. #define MAX_TOKEN_USER_INFO_SIZE 256
  46. BYTE TokenInfoBuf[MAX_TOKEN_USER_INFO_SIZE];
  47. TOKEN_USER* pTokenUserInfo = (TOKEN_USER*) TokenInfoBuf;
  48. DWORD dwSize;
  49. if ( ppUserSid )
  50. {
  51. *ppUserSid = NULL;
  52. }
  53. if ( GetTokenInformation( hToken, TokenUser, pTokenUserInfo,
  54. MAX_TOKEN_USER_INFO_SIZE, &dwSize ))
  55. {
  56. dwSize = GetLengthSid( pTokenUserInfo->User.Sid );
  57. if ( ppUserSid )
  58. {
  59. *ppUserSid = AuthzpAlloc( dwSize );
  60. if (*ppUserSid == NULL)
  61. {
  62. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  63. goto Finish;
  64. }
  65. CopyMemory( *ppUserSid, pTokenUserInfo->User.Sid, dwSize );
  66. }
  67. if ( GetTokenInformation( hToken, TokenStatistics,
  68. (PVOID) &TokenStats,
  69. sizeof(TOKEN_STATISTICS), &dwSize ) )
  70. {
  71. *pAuthenticationId = TokenStats.AuthenticationId;
  72. fResult = TRUE;
  73. goto Finish;
  74. }
  75. }
  76. //
  77. // error case
  78. //
  79. if ( ppUserSid && *ppUserSid )
  80. {
  81. LocalFree( *ppUserSid );
  82. *ppUserSid = NULL;
  83. }
  84. Finish:
  85. return fResult;
  86. }
  87. BOOL
  88. AuthzpGetThreadTokenInfo(
  89. OUT PSID* ppUserSid, OPTIONAL
  90. OUT PLUID pAuthenticationId
  91. )
  92. /*++
  93. Routine Description:
  94. Get user-sid and the user-logon-id from the thread token.
  95. Arguments:
  96. ppUserSid - pointer to user sid
  97. if non NULL, allocate and copy the user sid
  98. from the token. callers must free it using LocalFree
  99. pAuthenticationId - pointer to logon id
  100. Return Value:
  101. TRUE on success
  102. FALSE otherwise
  103. call GetLastError() to retrieve the errorcode,
  104. Notes:
  105. Caller must have TOKEN_QUERY access right.
  106. --*/
  107. {
  108. BOOL fResult = FALSE;
  109. HANDLE hToken=NULL;
  110. if ( OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken ) )
  111. {
  112. fResult = AuthzpGetTokenInfo( hToken, ppUserSid, pAuthenticationId );
  113. CloseHandle( hToken );
  114. }
  115. return fResult;
  116. }
  117. BOOL
  118. AuthzpGetProcessTokenInfo(
  119. OUT PSID* ppUserSid, OPTIONAL
  120. OUT PLUID pAuthenticationId
  121. )
  122. /*++
  123. Routine Description:
  124. Get user-sid and the user-logon-id from the process token.
  125. Arguments:
  126. ppUserSid - pointer to user sid
  127. if non NULL, allocate and copy the user sid
  128. from the token. callers must free it using LocalFree
  129. pAuthenticationId - pointer to logon id
  130. Return Value:
  131. TRUE on success
  132. FALSE otherwise
  133. call GetLastError() to retrieve the errorcode,
  134. Notes:
  135. Caller must have TOKEN_QUERY access right.
  136. --*/
  137. {
  138. BOOL fResult = FALSE;
  139. HANDLE hToken=NULL;
  140. if ( OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ) )
  141. {
  142. fResult = AuthzpGetTokenInfo( hToken, ppUserSid, pAuthenticationId );
  143. CloseHandle( hToken );
  144. }
  145. return fResult;
  146. }