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.

232 lines
5.3 KiB

  1. /*++
  2. Copyright (c) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. backuphistory.c
  5. Abstract:
  6. This module contains the routines related to maintaining the backup
  7. history for SR.
  8. Author:
  9. Molly Brown (MollyBro) 04-Sept-2001
  10. Revision History:
  11. MollyBro
  12. Based on legacy filter version of SR.
  13. --*/
  14. #include "tokentst.h"
  15. #define FILE_NAME_1 "c:\\test\\a.dll"
  16. #define FILE_NAME_2 "c:\\test\\b.dll"
  17. VOID
  18. _cdecl
  19. main(
  20. int argc,
  21. char *argv[]
  22. )
  23. {
  24. HANDLE file = INVALID_HANDLE_VALUE;
  25. HANDLE monitorThread = INVALID_HANDLE_VALUE;
  26. MONITOR_THREAD_CONTEXT context;
  27. DWORD monitorThreadId;
  28. DWORD currentThreadId;
  29. PCHAR currentFileName, newFileName, tempFileName;
  30. //
  31. // Get parameters
  32. //
  33. if (argc > 1) {
  34. printf("This programs tries to steal the system token while SR is working.\n");
  35. printf("usage: %s\n", argv[0]);
  36. return;
  37. }
  38. //
  39. // Get the current thread and create the monitor thread that will be polling for the token.
  40. //
  41. currentThreadId = GetCurrentThreadId();
  42. context.MainThread = INVALID_HANDLE_VALUE;
  43. context.MainThread = OpenThread( THREAD_ALL_ACCESS,
  44. FALSE,
  45. currentThreadId );
  46. if (context.MainThread == INVALID_HANDLE_VALUE) {
  47. printf("Error opening main thread: %d\n", GetLastError());
  48. }
  49. monitorThread = CreateThread( NULL,
  50. 0,
  51. MonitorThreadProc,
  52. &context,
  53. 0,
  54. &monitorThreadId );
  55. currentFileName = FILE_NAME_1;
  56. newFileName = FILE_NAME_2;
  57. while (TRUE) {
  58. if (!ModifyFile( currentFileName, newFileName )) {
  59. goto main_exit;
  60. }
  61. tempFileName = currentFileName;
  62. currentFileName = newFileName;
  63. newFileName = tempFileName;
  64. }
  65. main_exit:
  66. CloseHandle( monitorThread );
  67. }
  68. DWORD
  69. WINAPI
  70. MonitorThreadProc(
  71. PMONITOR_THREAD_CONTEXT Context
  72. )
  73. {
  74. NTSTATUS status;
  75. HANDLE currentTokenHandle, newTokenHandle;
  76. currentTokenHandle = newTokenHandle = NULL;
  77. status = NtOpenThreadToken( Context->MainThread,
  78. TOKEN_QUERY,
  79. TRUE,
  80. &currentTokenHandle );
  81. if (!NT_SUCCESS( status ) &&
  82. status != STATUS_NO_TOKEN) {
  83. printf( "Error initializing currentTokenUser: 0x%x.\n", status );
  84. return status;
  85. }
  86. while (TRUE) {
  87. //
  88. // Get the current token information
  89. //
  90. newTokenHandle = NULL;
  91. status = NtOpenThreadToken( Context->MainThread,
  92. TOKEN_QUERY,
  93. TRUE,
  94. &newTokenHandle );
  95. if (!NT_SUCCESS( status ) &&
  96. status != STATUS_NO_TOKEN) {
  97. printf( "Error initializing newTokenUser: 0x%x.\n", status );
  98. return status;
  99. }
  100. if ((newTokenHandle == NULL && currentTokenHandle == NULL) ||
  101. (newTokenHandle != NULL && currentTokenHandle != NULL)) {
  102. // printf( "Tokens match\n" );
  103. } else {
  104. printf( "Tokens changed\n" );
  105. }
  106. //
  107. // Close the currentTokenHandle and remember the newTokenHandle
  108. // for the next compare.
  109. //
  110. NtClose( currentTokenHandle );
  111. currentTokenHandle = newTokenHandle;
  112. }
  113. }
  114. BOOL
  115. ModifyFile (
  116. PCHAR FileName1,
  117. PCHAR FileName2
  118. )
  119. {
  120. HANDLE file;
  121. BOOL returnValue;
  122. file = CreateFile( FileName1,
  123. GENERIC_ALL,
  124. FILE_SHARE_READ | FILE_SHARE_WRITE,
  125. NULL,
  126. CREATE_ALWAYS,
  127. 0,
  128. NULL );
  129. if (file == INVALID_HANDLE_VALUE) {
  130. printf( "Error opening file %s %d\n", FileName1, GetLastError() );
  131. return FALSE;
  132. }
  133. CloseHandle( file );
  134. returnValue = MoveFile( FileName1, FileName2 );
  135. if (!returnValue) {
  136. printf( "Error renaming file from %s to %s: %d\n", FileName1, FileName2, GetLastError() );
  137. }
  138. return returnValue;
  139. }
  140. NTSTATUS
  141. GetCurrentTokenInformation (
  142. HANDLE ThreadHandle,
  143. PTOKEN_USER TokenUserInfoBuffer,
  144. ULONG TokenUserInfoBufferLength
  145. )
  146. {
  147. NTSTATUS status;
  148. HANDLE tokenHandle;
  149. ULONG returnedLength;
  150. status = NtOpenThreadToken( ThreadHandle,
  151. TOKEN_QUERY,
  152. TRUE,
  153. &tokenHandle );
  154. if (!NT_SUCCESS( status )) {
  155. return status;
  156. }
  157. status = NtQueryInformationToken( tokenHandle,
  158. TokenUser,
  159. TokenUserInfoBuffer,
  160. TokenUserInfoBufferLength,
  161. &returnedLength );
  162. NtClose( tokenHandle );
  163. return status;
  164. }