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.

192 lines
5.3 KiB

  1. /*
  2. **++
  3. **
  4. ** Copyright (c) 2000-2002 Microsoft Corporation
  5. **
  6. **
  7. ** Module Name:
  8. **
  9. ** secutil.cpp
  10. **
  11. ** Abstract:
  12. **
  13. ** Utility functions for the VSSEC test.
  14. **
  15. ** Author:
  16. **
  17. ** Adi Oltean [aoltean] 02/12/2002
  18. **
  19. **
  20. ** Revision History:
  21. **
  22. **--
  23. */
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // Includes
  26. #include "sec.h"
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // Command line parsing
  29. CVssSecurityTest::CVssSecurityTest()
  30. {
  31. CVssFunctionTracer ft(VSSDBG_VSSTEST, L"CVssSecurityTest::CVssSecurityTest()");
  32. // Initialize data members
  33. m_bCoInitializeSucceeded = false;
  34. // Print display header
  35. ft.Msg(L"\nVSS Security Test application, version 1.0\n");
  36. }
  37. CVssSecurityTest::~CVssSecurityTest()
  38. {
  39. // Unloading the COM library
  40. if (m_bCoInitializeSucceeded)
  41. CoUninitialize();
  42. }
  43. void CVssSecurityTest::Initialize()
  44. {
  45. CVssFunctionTracer ft(VSSDBG_VSSTEST, L"CVssSecurityTest::Initialize()");
  46. ft.Msg (L"\n----------------- Command line parsing ---------------\n");
  47. // Extract the executable name from the command line
  48. LPWSTR wszCmdLine = GetCommandLine();
  49. for(;iswspace(*wszCmdLine); wszCmdLine++);
  50. if (*wszCmdLine == L'"') {
  51. if (!wcschr(wszCmdLine+1, L'"'))
  52. ft.Throw( VSSDBG_VSSTEST, E_UNEXPECTED, L"Invalid command line: %s\n", GetCommandLine() );
  53. wszCmdLine = wcschr(wszCmdLine+1, L'"') + 1;
  54. } else
  55. for(;*wszCmdLine && !iswspace(*wszCmdLine); wszCmdLine++);
  56. ft.Msg( L"Command line: '%s '\n", wszCmdLine );
  57. // Parse command line
  58. if (!ParseCmdLine(wszCmdLine))
  59. throw(E_INVALIDARG);
  60. PrintArguments();
  61. BS_ASSERT(!IsOptionPresent(L"-D"));
  62. ft.Msg (L"\n----------------- Initializing ---------------------\n");
  63. // Initialize COM library
  64. CHECK_COM(CoInitializeEx (NULL, COINIT_MULTITHREADED),;);
  65. m_bCoInitializeSucceeded = true;
  66. ft.Msg (L"COM library initialized.\n");
  67. // Initialize COM security
  68. CHECK_COM
  69. (
  70. CoInitializeSecurity
  71. (
  72. NULL, // IN PSECURITY_DESCRIPTOR pSecDesc,
  73. -1, // IN LONG cAuthSvc,
  74. NULL, // IN SOLE_AUTHENTICATION_SERVICE *asAuthSvc,
  75. NULL, // IN void *pReserved1,
  76. RPC_C_AUTHN_LEVEL_CONNECT, // IN DWORD dwAuthnLevel,
  77. RPC_C_IMP_LEVEL_IDENTIFY, // IN DWORD dwImpLevel,
  78. NULL, // IN void *pAuthList,
  79. EOAC_NONE, // IN DWORD dwCapabilities,
  80. NULL // IN void *pReserved3
  81. )
  82. ,;);
  83. ft.Msg (L"COM security initialized.\n");
  84. // Turns off SEH exception handing for COM servers (BUG# 530092)
  85. // ft.ComDisableSEH(VSSDBG_VSSTEST);
  86. ft.Msg (L"COM SEH disabled.\n");
  87. }
  88. ///////////////////////////////////////////////////////////////////////////////
  89. // Utility functions
  90. // Convert a failure type into a string
  91. LPCWSTR CVssSecurityTest::GetStringFromFailureType( IN HRESULT hrStatus )
  92. {
  93. static WCHAR wszBuffer[MAX_TEXT_BUFFER];
  94. switch (hrStatus)
  95. {
  96. VSS_ERROR_CASE(wszBuffer, MAX_TEXT_BUFFER, E_OUTOFMEMORY)
  97. case NOERROR:
  98. break;
  99. default:
  100. ::FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM,
  101. NULL, hrStatus, 0, wszBuffer, MAX_TEXT_BUFFER - 1, NULL);
  102. break;
  103. }
  104. return (wszBuffer);
  105. }
  106. BOOL CVssSecurityTest::IsAdmin()
  107. {
  108. CVssFunctionTracer ft(VSSDBG_VSSTEST, L"CVssSecurityTest::IsAdmin()");
  109. HANDLE hThreadToken = NULL;
  110. CHECK_WIN32( OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &hThreadToken), ;);
  111. DWORD cbToken = 0;
  112. GetTokenInformation( hThreadToken, TokenUser, NULL, 0, &cbToken );
  113. TOKEN_USER *pUserToken = (TOKEN_USER *)_alloca( cbToken );
  114. CHECK_WIN32( GetTokenInformation( hThreadToken, TokenUser, pUserToken, cbToken, &cbToken ),
  115. CloseHandle(hThreadToken);
  116. );
  117. WCHAR wszName[MAX_TEXT_BUFFER];
  118. DWORD dwNameSize = MAX_TEXT_BUFFER;
  119. WCHAR wszDomainName[MAX_TEXT_BUFFER];
  120. DWORD dwDomainNameSize = MAX_TEXT_BUFFER;
  121. SID_NAME_USE snUse;
  122. CHECK_WIN32( LookupAccountSid( NULL, pUserToken->User.Sid,
  123. wszName, &dwNameSize,
  124. wszDomainName, &dwDomainNameSize,
  125. &snUse),
  126. CloseHandle(hThreadToken);
  127. );
  128. ft.Msg( L"* (ThreadToken) Name: %s, Domain Name: %s, SidUse: %d\n", wszName, wszDomainName, snUse );
  129. SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
  130. PSID psid = 0;
  131. CHECK_WIN32(
  132. AllocateAndInitializeSid(
  133. &siaNtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
  134. DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psid ),
  135. CloseHandle(hThreadToken);
  136. );
  137. BOOL bIsAdmin = FALSE;
  138. CHECK_WIN32( CheckTokenMembership( 0, psid, &bIsAdmin ),
  139. FreeSid( psid );
  140. CloseHandle(hThreadToken);
  141. );
  142. FreeSid( psid );
  143. CloseHandle(hThreadToken);
  144. return bIsAdmin;
  145. }