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.

231 lines
6.2 KiB

  1. /*****************************************************************************\
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. \*****************************************************************************/
  4. #include <assert.h>
  5. #include <windows.h>
  6. #include <tchar.h>
  7. #include <pdh.h>
  8. #include <pdhp.h>
  9. #include <pdhmsg.h>
  10. #include "plogman.h"
  11. PDH_FUNCTION
  12. PdhPlaWbemConnect( LPWSTR strComputer, IWbemClassObject** pWbemClass, IWbemServices** pWbemServices )
  13. {
  14. HRESULT hr;
  15. IWbemLocator *pLocator = NULL;
  16. LPCWSTR szRootOld = L"root\\wmi";
  17. LPCWSTR szRootNew = L"root\\perfmon";
  18. LPCWSTR szMask = L"\\\\%s\\%s";
  19. BSTR bszClass = SysAllocString(L"SysmonLog");
  20. BSTR bszNamespaceOld = NULL;
  21. BSTR bszNamespaceNew = NULL;
  22. LPWSTR buffer = NULL;
  23. hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
  24. if( S_FALSE == hr ){
  25. // This thread already called CoInitialize
  26. hr = ERROR_SUCCESS;
  27. }
  28. if( NULL != strComputer ){
  29. DWORD dwSize = wcslen(szRootNew) + wcslen(strComputer) + wcslen( szMask );
  30. buffer = (LPWSTR)G_ALLOC( dwSize * sizeof(WCHAR) );
  31. if( buffer == NULL ){
  32. hr = ERROR_OUTOFMEMORY;
  33. goto cleanup;
  34. }
  35. swprintf( buffer, szMask, strComputer, szRootOld );
  36. bszNamespaceOld = SysAllocString( buffer );
  37. swprintf( buffer, szMask, strComputer, szRootNew );
  38. bszNamespaceNew = SysAllocString( buffer );
  39. }else{
  40. bszNamespaceOld = SysAllocString( szRootOld );
  41. bszNamespaceNew = SysAllocString( szRootNew );
  42. }
  43. *pWbemServices = NULL;
  44. *pWbemClass = NULL;
  45. hr = CoCreateInstance(
  46. CLSID_WbemLocator,
  47. 0,
  48. CLSCTX_INPROC_SERVER,
  49. IID_IWbemLocator,
  50. (LPVOID*)&pLocator
  51. );
  52. CHECK_STATUS( hr );
  53. hr = pLocator->ConnectServer(
  54. bszNamespaceNew,
  55. NULL,
  56. NULL,
  57. NULL,
  58. 0,
  59. NULL,
  60. NULL,
  61. pWbemServices
  62. );
  63. if( FAILED(hr) ){
  64. hr = pLocator->ConnectServer(
  65. bszNamespaceOld,
  66. NULL,
  67. NULL,
  68. NULL,
  69. 0,
  70. NULL,
  71. NULL,
  72. pWbemServices
  73. );
  74. }
  75. CHECK_STATUS( hr );
  76. hr = CoSetProxyBlanket(
  77. *pWbemServices,
  78. RPC_C_AUTHN_WINNT,
  79. RPC_C_AUTHZ_NONE,
  80. NULL,
  81. RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
  82. RPC_C_IMP_LEVEL_IMPERSONATE,
  83. NULL,
  84. EOAC_NONE
  85. );
  86. CHECK_STATUS( hr );
  87. hr = (*pWbemServices)->GetObject( bszClass, 0, NULL, pWbemClass, NULL);
  88. CHECK_STATUS( hr );
  89. cleanup:
  90. if( pLocator != NULL ){
  91. pLocator->Release();
  92. }
  93. if( ERROR_SUCCESS != hr ){
  94. if( *pWbemClass != NULL ){
  95. (*pWbemClass)->Release();
  96. *pWbemClass = NULL;
  97. }
  98. if( *pWbemServices != NULL ){
  99. (*pWbemServices)->Release();
  100. *pWbemServices = NULL;
  101. }
  102. }
  103. G_FREE( buffer );
  104. SysFreeString( bszNamespaceOld );
  105. SysFreeString( bszNamespaceNew );
  106. SysFreeString( bszClass );
  107. return hr;
  108. }
  109. PDH_FUNCTION
  110. PdhPlaWbemSetRunAs(
  111. LPWSTR strName,
  112. LPWSTR strComputer,
  113. LPWSTR strUser,
  114. LPWSTR strPassword
  115. )
  116. {
  117. HRESULT hr = ERROR_SUCCESS;
  118. PDH_STATUS pdhStatus = ERROR_SUCCESS;
  119. IWbemClassObject* pWbemClass = NULL;
  120. IWbemServices* pWbemServices = NULL;
  121. __try{
  122. BSTR bszMethodName = SysAllocString( L"SetRunAs" );
  123. BSTR bszUser = SysAllocString(L"User");
  124. BSTR bszPassword = SysAllocString(L"Password");
  125. BSTR bszReturn = SysAllocString(L"ReturnValue");
  126. LPCWSTR szInstanceMask = L"SysmonLog.Name=\"%s\"";
  127. IWbemClassObject* pOutInst = NULL;
  128. IWbemClassObject* pInClass = NULL;
  129. IWbemClassObject* pInInst = NULL;
  130. VARIANT var;
  131. CIMTYPE vtType;
  132. LONG nFlavor;
  133. LPWSTR buffer = NULL;
  134. DWORD dwSize = wcslen( szInstanceMask ) + wcslen( strName );
  135. buffer = (LPWSTR)G_ALLOC( dwSize * sizeof(WCHAR) );
  136. if( NULL == buffer ){
  137. pdhStatus = PDH_MEMORY_ALLOCATION_FAILURE;
  138. goto cleanup;
  139. }
  140. swprintf( buffer, szInstanceMask, strName );
  141. BSTR bszInstancePath = SysAllocString( buffer );
  142. hr = PdhPlaWbemConnect( strComputer, &pWbemClass, &pWbemServices );
  143. CHECK_STATUS( hr );
  144. hr = pWbemClass->GetMethod( bszMethodName, 0, &pInClass, NULL);
  145. CHECK_STATUS( hr );
  146. hr = pInClass->SpawnInstance(0, &pInInst);
  147. CHECK_STATUS( hr );
  148. var.vt = VT_BSTR;
  149. var.bstrVal= SysAllocString( strUser );
  150. hr = pInInst->Put( bszUser, 0, &var, 0);
  151. VariantClear(&var);
  152. CHECK_STATUS( hr );
  153. var.vt = VT_BSTR;
  154. var.bstrVal= SysAllocString( strPassword );
  155. hr = pInInst->Put(bszPassword, 0, &var, 0);
  156. VariantClear(&var);
  157. CHECK_STATUS( hr );
  158. hr = pWbemServices->ExecMethod( bszInstancePath, bszMethodName, 0, NULL, pInInst, &pOutInst, NULL);
  159. CHECK_STATUS( hr );
  160. if( pOutInst != NULL ){
  161. hr = pOutInst->Get( bszReturn, 0, &var, &vtType, &nFlavor );
  162. CHECK_STATUS( hr );
  163. if( var.vt == VT_I4 ){
  164. pdhStatus = var.lVal;
  165. }
  166. VariantClear(&var);
  167. }
  168. cleanup:
  169. if( pWbemClass != NULL ){
  170. pWbemClass->Release();
  171. }
  172. if( pWbemServices != NULL ){
  173. pWbemServices->Release();
  174. }
  175. if( pInInst != NULL ){
  176. pInInst->Release();
  177. }
  178. if( pOutInst != NULL ){
  179. pOutInst->Release();
  180. }
  181. SysFreeString( bszInstancePath );
  182. SysFreeString( bszMethodName );
  183. SysFreeString( bszUser );
  184. SysFreeString( bszPassword );
  185. SysFreeString( bszReturn );
  186. G_FREE( buffer );
  187. } __except (EXCEPTION_EXECUTE_HANDLER) {
  188. hr = GetLastError();
  189. }
  190. if( ERROR_SUCCESS != pdhStatus ){
  191. return pdhStatus;
  192. }
  193. return PlaiErrorToPdhStatus( hr );
  194. }