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.

187 lines
4.3 KiB

  1. #include "stdafx.hxx"
  2. #include "vss.h"
  3. #include "vswriter.h"
  4. #include <debug.h>
  5. #include <cwriter.h>
  6. CComModule _Module;
  7. HANDLE g_hEvent = NULL;
  8. BOOL WINAPI CtrlCHandler(DWORD type)
  9. {
  10. wprintf(L"Control handler called.\n");
  11. if (g_hEvent)
  12. SetEvent(g_hEvent);
  13. return TRUE;
  14. }
  15. BOOL AssertPrivilege( LPCWSTR privName )
  16. {
  17. HANDLE tokenHandle;
  18. BOOL stat = FALSE;
  19. if ( OpenProcessToken (GetCurrentProcess(),
  20. TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
  21. &tokenHandle))
  22. {
  23. LUID value;
  24. if ( LookupPrivilegeValue( NULL, privName, &value ) )
  25. {
  26. TOKEN_PRIVILEGES newState;
  27. DWORD error;
  28. newState.PrivilegeCount = 1;
  29. newState.Privileges[0].Luid = value;
  30. newState.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED_BY_DEFAULT|SE_PRIVILEGE_ENABLED;
  31. /*
  32. * We will always call GetLastError below, so clear
  33. * any prior error values on this thread.
  34. */
  35. SetLastError( ERROR_SUCCESS );
  36. stat = AdjustTokenPrivileges (tokenHandle,
  37. FALSE,
  38. &newState,
  39. (DWORD)0,
  40. NULL,
  41. NULL );
  42. /*
  43. * Supposedly, AdjustTokenPriveleges always returns TRUE
  44. * (even when it fails). So, call GetLastError to be
  45. * extra sure everything's cool.
  46. */
  47. if ( (error = GetLastError()) != ERROR_SUCCESS )
  48. {
  49. stat = FALSE;
  50. }
  51. if ( !stat )
  52. {
  53. wprintf( L"AdjustTokenPrivileges for %s failed with %d",
  54. privName,
  55. error );
  56. }
  57. }
  58. DWORD cbTokens;
  59. GetTokenInformation (tokenHandle,
  60. TokenPrivileges,
  61. NULL,
  62. 0,
  63. &cbTokens);
  64. TOKEN_PRIVILEGES *pTokens = (TOKEN_PRIVILEGES *) new BYTE[cbTokens];
  65. GetTokenInformation (tokenHandle,
  66. TokenPrivileges,
  67. pTokens,
  68. cbTokens,
  69. &cbTokens);
  70. delete pTokens;
  71. CloseHandle( tokenHandle );
  72. }
  73. return stat;
  74. }
  75. extern "C" __cdecl wmain(int argc, WCHAR **argv)
  76. {
  77. CTestVssWriter *pInstance;
  78. bool bCreated = false;
  79. bool bSubscribed = false;
  80. HRESULT hr = S_OK;
  81. SetConsoleCtrlHandler(CtrlCHandler, TRUE);
  82. BS_ASSERT(FALSE);
  83. try
  84. {
  85. CHECK_SUCCESS(CoInitializeEx(NULL, COINIT_MULTITHREADED));
  86. if ( !AssertPrivilege( SE_BACKUP_NAME ) )
  87. {
  88. wprintf( L"AssertPrivilege returned error, rc:%d\n", GetLastError() );
  89. return 2;
  90. }
  91. CHECK_SUCCESS
  92. (
  93. CoInitializeSecurity
  94. (
  95. NULL, // IN PSECURITY_DESCRIPTOR pSecDesc,
  96. -1, // IN LONG cAuthSvc,
  97. NULL, // IN SOLE_AUTHENTICATION_SERVICE *asAuthSvc,
  98. NULL, // IN void *pReserved1,
  99. RPC_C_AUTHN_LEVEL_CONNECT, // IN DWORD dwAuthnLevel,
  100. RPC_C_IMP_LEVEL_IMPERSONATE, // IN DWORD dwImpLevel,
  101. NULL, // IN void *pAuthList,
  102. EOAC_NONE, // IN DWORD dwCapabilities,
  103. NULL // IN void *pReserved3
  104. )
  105. );
  106. pInstance = new CTestVssWriter;
  107. if (pInstance == NULL)
  108. {
  109. wprintf(L"allocation failure\n");
  110. DebugBreak();
  111. }
  112. bCreated = true;
  113. pInstance->Initialize();
  114. pInstance->Subscribe();
  115. bSubscribed = true;
  116. HANDLE hEventW = CreateEvent(NULL, TRUE, FALSE, L"TESTWRITEREVENT");
  117. if (hEventW == NULL)
  118. {
  119. wprintf(L"CreateEvent failed with error %d.\n", GetLastError());
  120. DebugBreak();
  121. }
  122. g_hEvent = hEventW;
  123. wprintf(L"Ready to receive events.\n");
  124. HANDLE hEventB = CreateEvent(NULL, TRUE, FALSE, L"TESTBACKUPEVENT");
  125. if (hEventB == NULL)
  126. {
  127. wprintf(L"CreateEvent failed with error %d.\n", GetLastError());
  128. DebugBreak();
  129. }
  130. SetEvent(hEventB);
  131. WaitForSingleObject(hEventW, INFINITE);
  132. }
  133. catch(...)
  134. {
  135. _ASSERTE(FALSE && "Unexpected exception");
  136. hr = E_UNEXPECTED;
  137. }
  138. SetConsoleCtrlHandler(CtrlCHandler, FALSE);
  139. if (bSubscribed)
  140. CHECK_SUCCESS(pInstance->Unsubscribe());
  141. if (bCreated)
  142. delete pInstance;
  143. if (FAILED(hr))
  144. wprintf(L"Failed with %08x.\n", hr);
  145. return(0);
  146. }