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.

209 lines
4.6 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 AuthzpFree
  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 (sizeof(TOKEN_USER)+SECURITY_MAX_SID_SIZE)
  46. BYTE TokenInfoBuf[MAX_TOKEN_USER_INFO_SIZE];
  47. TOKEN_USER* pTokenUserInfo = (TOKEN_USER*) TokenInfoBuf;
  48. DWORD dwSize;
  49. if ( ARGUMENT_PRESENT(ppUserSid) )
  50. {
  51. *ppUserSid = NULL;
  52. if ( GetTokenInformation( hToken, TokenUser, pTokenUserInfo,
  53. MAX_TOKEN_USER_INFO_SIZE, &dwSize ))
  54. {
  55. dwSize = GetLengthSid( pTokenUserInfo->User.Sid );
  56. *ppUserSid = AuthzpAlloc( dwSize );
  57. if (*ppUserSid == NULL)
  58. {
  59. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  60. goto Finish;
  61. }
  62. CopyMemory( *ppUserSid, pTokenUserInfo->User.Sid, dwSize );
  63. }
  64. else
  65. {
  66. //
  67. // GetTokenInformation already sets last error.
  68. //
  69. goto Finish;
  70. }
  71. }
  72. if ( GetTokenInformation( hToken, TokenStatistics,
  73. (PVOID) &TokenStats,
  74. sizeof(TOKEN_STATISTICS), &dwSize ) )
  75. {
  76. *pAuthenticationId = TokenStats.AuthenticationId;
  77. fResult = TRUE;
  78. goto Finish;
  79. }
  80. //
  81. // error case
  82. //
  83. if ( ppUserSid && *ppUserSid )
  84. {
  85. AuthzpFree( *ppUserSid );
  86. *ppUserSid = NULL;
  87. }
  88. Finish:
  89. return fResult;
  90. }
  91. BOOL
  92. AuthzpGetThreadTokenInfo(
  93. OUT PSID* ppUserSid, OPTIONAL
  94. OUT PLUID pAuthenticationId
  95. )
  96. /*++
  97. Routine Description:
  98. Get user-sid and the user-logon-id from the thread token.
  99. Arguments:
  100. ppUserSid - pointer to user sid
  101. if non NULL, allocate and copy the user sid
  102. from the token. callers must free it using AuthzpFree
  103. pAuthenticationId - pointer to logon id
  104. Return Value:
  105. TRUE on success
  106. FALSE otherwise
  107. call GetLastError() to retrieve the errorcode,
  108. Notes:
  109. Caller must have TOKEN_QUERY access right.
  110. --*/
  111. {
  112. BOOL fResult = FALSE;
  113. HANDLE hToken=NULL;
  114. if ( OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken ) )
  115. {
  116. fResult = AuthzpGetTokenInfo( hToken, ppUserSid, pAuthenticationId );
  117. CloseHandle( hToken );
  118. }
  119. return fResult;
  120. }
  121. BOOL
  122. AuthzpGetProcessTokenInfo(
  123. OUT PSID* ppUserSid, OPTIONAL
  124. OUT PLUID pAuthenticationId
  125. )
  126. /*++
  127. Routine Description:
  128. Get user-sid and the user-logon-id from the process token.
  129. Arguments:
  130. ppUserSid - pointer to user sid
  131. if non NULL, allocate and copy the user sid
  132. from the token. callers must free it using AuthzpFree
  133. pAuthenticationId - pointer to logon id
  134. Return Value:
  135. TRUE on success
  136. FALSE otherwise
  137. call GetLastError() to retrieve the errorcode,
  138. Notes:
  139. Caller must have TOKEN_QUERY access right.
  140. --*/
  141. {
  142. BOOL fResult = FALSE;
  143. HANDLE hToken=NULL;
  144. if ( OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ) )
  145. {
  146. fResult = AuthzpGetTokenInfo( hToken, ppUserSid, pAuthenticationId );
  147. CloseHandle( hToken );
  148. }
  149. return fResult;
  150. }