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.

481 lines
11 KiB

  1. #include <wbemprov.h>
  2. #include "ntrkcomm.h"
  3. extern long g_cObj;
  4. CWbemServices::CWbemServices(
  5. IWbemServices* pNamespace)
  6. :m_pWbemServices(NULL)
  7. {
  8. m_pWbemServices = pNamespace;
  9. if(m_pWbemServices != NULL)
  10. m_pWbemServices->AddRef();
  11. }
  12. CWbemServices::~CWbemServices()
  13. {
  14. if(m_pWbemServices != NULL)
  15. m_pWbemServices->Release();
  16. }
  17. HRESULT
  18. CWbemServices::CreateClassEnum(
  19. /* [in] */ const BSTR Superclass,
  20. /* [in] */ long lFlags,
  21. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  22. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum
  23. )
  24. {
  25. SCODE sc = m_pWbemServices->CreateClassEnum(
  26. Superclass,
  27. lFlags,
  28. pCtx,
  29. ppEnum);
  30. CoImpersonateClient();
  31. return sc;
  32. }
  33. HRESULT
  34. CWbemServices::CreateInstanceEnum(
  35. /* [in] */ const BSTR Class,
  36. /* [in] */ long lFlags,
  37. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  38. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum)
  39. {
  40. HRESULT hr = m_pWbemServices->CreateInstanceEnum(
  41. Class,
  42. lFlags,
  43. pCtx,
  44. ppEnum);
  45. CoImpersonateClient();
  46. return hr;
  47. }
  48. HRESULT
  49. CWbemServices::DeleteClass(
  50. /* [in] */ const BSTR Class,
  51. /* [in] */ long lFlags,
  52. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  53. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult)
  54. {
  55. HRESULT hr = m_pWbemServices->DeleteClass(
  56. Class,
  57. lFlags,
  58. pCtx,
  59. ppCallResult);
  60. CoImpersonateClient();
  61. return hr;
  62. }
  63. HRESULT
  64. CWbemServices::DeleteInstance(
  65. /* [in] */ const BSTR ObjectPath,
  66. /* [in] */ long lFlags,
  67. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  68. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult)
  69. {
  70. HRESULT hr = m_pWbemServices->DeleteInstance(
  71. ObjectPath,
  72. lFlags,
  73. pCtx,
  74. ppCallResult);
  75. CoImpersonateClient();
  76. return hr;
  77. }
  78. HRESULT
  79. CWbemServices::ExecMethod(
  80. const BSTR strObjectPath,
  81. const BSTR MethodName,
  82. long lFlags,
  83. IWbemContext* pCtx,
  84. IWbemClassObject* pInParams,
  85. IWbemClassObject** ppOurParams,
  86. IWbemCallResult** ppCallResult)
  87. {
  88. HRESULT hr = m_pWbemServices->ExecMethod(
  89. strObjectPath,
  90. MethodName,
  91. lFlags,
  92. pCtx,
  93. pInParams,
  94. ppOurParams,
  95. ppCallResult) ;
  96. CoImpersonateClient();
  97. return hr;
  98. }
  99. HRESULT
  100. CWbemServices::ExecNotificationQuery(
  101. /* [in] */ const BSTR QueryLanguage,
  102. /* [in] */ const BSTR Query,
  103. /* [in] */ long lFlags,
  104. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  105. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum)
  106. {
  107. HRESULT hr = m_pWbemServices->ExecNotificationQuery(
  108. QueryLanguage,
  109. Query,
  110. lFlags,
  111. pCtx,
  112. ppEnum);
  113. CoImpersonateClient();
  114. return hr;
  115. }
  116. HRESULT
  117. CWbemServices::ExecQuery(
  118. /* [in] */ const BSTR QueryLanguage,
  119. /* [in] */ const BSTR Query,
  120. /* [in] */ long lFlags,
  121. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  122. /* [out] */ IEnumWbemClassObject __RPC_FAR *__RPC_FAR *ppEnum)
  123. {
  124. HRESULT hr = m_pWbemServices->ExecQuery(
  125. QueryLanguage,
  126. Query,
  127. lFlags,
  128. pCtx,
  129. ppEnum);
  130. CoImpersonateClient();
  131. return hr;
  132. }
  133. HRESULT
  134. CWbemServices::GetObject(
  135. /* [in] */ const BSTR ObjectPath,
  136. /* [in] */ long lFlags,
  137. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  138. /* [unique][in][out] */ IWbemClassObject __RPC_FAR *__RPC_FAR *ppObject,
  139. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult)
  140. {
  141. HRESULT hr = m_pWbemServices->GetObject(
  142. ObjectPath,
  143. lFlags,
  144. pCtx,
  145. ppObject,
  146. ppCallResult);
  147. CoImpersonateClient();
  148. return hr;
  149. }
  150. HRESULT
  151. CWbemServices::PutClass(
  152. /* [in] */ IWbemClassObject __RPC_FAR *pObject,
  153. /* [in] */ long lFlags,
  154. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  155. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult)
  156. {
  157. HRESULT hr = m_pWbemServices->PutClass(
  158. pObject,
  159. lFlags,
  160. pCtx,
  161. ppCallResult);
  162. CoImpersonateClient();
  163. return hr;
  164. }
  165. HRESULT
  166. CWbemServices::PutInstance(
  167. /* [in] */ IWbemClassObject __RPC_FAR *pInst,
  168. /* [in] */ long lFlags,
  169. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  170. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppCallResult)
  171. {
  172. HRESULT hr = m_pWbemServices->PutInstance(
  173. pInst,
  174. lFlags,
  175. pCtx,
  176. ppCallResult);
  177. CoImpersonateClient();
  178. return hr;
  179. }
  180. /*CImpersonatedProvider
  181. * Purpose: provide a general solution for impersonate client for
  182. * Wbem providers.
  183. * USAGE:
  184. * Inherit from this class, and implement abstact virtual functions.
  185. * child class should implement function prefixed with "Do".
  186. * ******************************************/
  187. CImpersonatedProvider::CImpersonatedProvider(
  188. const BSTR ObjectPath,
  189. const BSTR User,
  190. const BSTR Password,
  191. IWbemContext * pCtx)
  192. :m_cRef(0), m_pNamespace(NULL)
  193. {
  194. }
  195. CImpersonatedProvider::~CImpersonatedProvider()
  196. {
  197. if(m_pNamespace)
  198. delete m_pNamespace;
  199. }
  200. STDMETHODIMP_(ULONG)
  201. CImpersonatedProvider::AddRef(void)
  202. {
  203. InterlockedIncrement(&g_cObj);
  204. return InterlockedIncrement((long *)&m_cRef);
  205. }
  206. STDMETHODIMP_(ULONG)
  207. CImpersonatedProvider::Release(void)
  208. {
  209. InterlockedDecrement(&g_cObj);
  210. long lNewCount = InterlockedDecrement((long *)&m_cRef);
  211. if (0L == lNewCount)
  212. delete this;
  213. return lNewCount>0 ? lNewCount : 0;
  214. }
  215. STDMETHODIMP
  216. CImpersonatedProvider::QueryInterface(
  217. REFIID riid,
  218. PPVOID ppv)
  219. {
  220. *ppv=NULL;
  221. // Since we have dual inheritance, it is necessary to cast the return type
  222. if(riid== IID_IWbemServices)
  223. *ppv = (IWbemServices*)this;
  224. if(IID_IUnknown==riid || riid== IID_IWbemProviderInit)
  225. *ppv = (IWbemProviderInit*)this;
  226. if (NULL != *ppv)
  227. {
  228. AddRef();
  229. return S_OK;
  230. }
  231. else
  232. return E_NOINTERFACE;
  233. }
  234. STDMETHODIMP
  235. CImpersonatedProvider::Initialize(
  236. LPWSTR pszUser, LONG lFlags,
  237. LPWSTR pszNamespace, LPWSTR pszLocale,
  238. IWbemServices *pNamespace,
  239. IWbemContext *pCtx,
  240. IWbemProviderInitSink *pInitSink)
  241. {
  242. HRESULT hr = WBEM_S_NO_ERROR;
  243. LONG lStatus = WBEM_S_INITIALIZED;
  244. m_pNamespace = new CWbemServices(pNamespace);
  245. if(m_pNamespace == NULL)
  246. {
  247. hr = WBEM_E_OUT_OF_MEMORY;
  248. lStatus = WBEM_E_FAILED;
  249. }
  250. //Let CIMOM know you are initialized
  251. //==================================
  252. pInitSink->SetStatus(lStatus,0);
  253. return hr;
  254. }
  255. HRESULT
  256. CImpersonatedProvider::CreateInstanceEnumAsync(
  257. /* [in] */ const BSTR Class,
  258. /* [in] */ long lFlags,
  259. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  260. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler)
  261. {
  262. CoImpersonateClient();
  263. return DoCreateInstanceEnumAsync(
  264. Class,
  265. lFlags,
  266. pCtx,
  267. pResponseHandler);
  268. }
  269. HRESULT
  270. CImpersonatedProvider::DeleteInstanceAsync(
  271. /* [in] */ const BSTR ObjectPath,
  272. /* [in] */ long lFlags,
  273. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  274. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler)
  275. {
  276. CoImpersonateClient();
  277. return DoDeleteInstanceAsync(
  278. ObjectPath,
  279. lFlags,
  280. pCtx,
  281. pResponseHandler);
  282. }
  283. HRESULT
  284. CImpersonatedProvider::ExecMethodAsync(
  285. const BSTR strObjectPath,
  286. const BSTR MethodName,
  287. long lFlags,
  288. IWbemContext* pCtx,
  289. IWbemClassObject* pInParams,
  290. IWbemObjectSink* pResponseHandler)
  291. {
  292. CoImpersonateClient();
  293. return DoExecMethodAsync(
  294. strObjectPath,
  295. MethodName,
  296. lFlags,
  297. pCtx,
  298. pInParams,
  299. pResponseHandler);
  300. }
  301. HRESULT
  302. CImpersonatedProvider::ExecQueryAsync(
  303. /* [in] */ const BSTR QueryLanguage,
  304. /* [in] */ const BSTR Query,
  305. /* [in] */ long lFlags,
  306. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  307. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler)
  308. {
  309. CoImpersonateClient();
  310. return DoExecQueryAsync(
  311. QueryLanguage,
  312. Query,
  313. lFlags,
  314. pCtx,
  315. pResponseHandler);
  316. }
  317. HRESULT
  318. CImpersonatedProvider::GetObjectAsync(
  319. /* [in] */ const BSTR ObjectPath,
  320. /* [in] */ long lFlags,
  321. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  322. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler)
  323. {
  324. CoImpersonateClient();
  325. return DoGetObjectAsync(
  326. ObjectPath,
  327. lFlags,
  328. pCtx,
  329. pResponseHandler);
  330. }
  331. HRESULT
  332. CImpersonatedProvider::PutInstanceAsync(
  333. /* [in] */ IWbemClassObject __RPC_FAR *pInst,
  334. /* [in] */ long lFlags,
  335. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  336. /* [in] */ IWbemObjectSink __RPC_FAR *pResponseHandler)
  337. {
  338. CoImpersonateClient();
  339. return DoPutInstanceAsync(
  340. pInst,
  341. lFlags,
  342. pCtx,
  343. pResponseHandler);
  344. }
  345. // CWbemInstanceMgr
  346. CWbemInstanceMgr::CWbemInstanceMgr(
  347. IWbemObjectSink* pHandler,
  348. DWORD dwSize)
  349. :m_pSink(NULL), m_ppInst(NULL), m_dwIndex(0)
  350. {
  351. m_pSink = pHandler;
  352. if(m_pSink != NULL)
  353. m_pSink->AddRef();
  354. m_dwThreshHold = dwSize;
  355. m_ppInst = new IWbemClassObject*[dwSize];
  356. for(DWORD i = 0; i < dwSize; i++)
  357. m_ppInst[i] = NULL;
  358. }
  359. CWbemInstanceMgr::~CWbemInstanceMgr()
  360. {
  361. if(m_ppInst != NULL)
  362. {
  363. if(m_dwIndex >0)
  364. {
  365. m_pSink->Indicate(
  366. m_dwIndex,
  367. m_ppInst);
  368. }
  369. for(DWORD i =0; i<m_dwIndex; i++)
  370. {
  371. if(m_ppInst[i] != NULL)
  372. (m_ppInst[i])->Release();
  373. }
  374. delete [] m_ppInst;
  375. }
  376. if(m_pSink != NULL)
  377. m_pSink->Release();
  378. }
  379. void
  380. CWbemInstanceMgr::Indicate(IWbemClassObject* pInst)
  381. {
  382. if(pInst == NULL)
  383. throw WBEM_E_INVALID_PARAMETER;
  384. m_ppInst[m_dwIndex++] = pInst;
  385. pInst->AddRef();
  386. if(m_dwIndex == m_dwThreshHold)
  387. {
  388. SCODE sc = m_pSink->Indicate(
  389. m_dwIndex,
  390. m_ppInst);
  391. if(sc != S_OK)
  392. throw sc;
  393. // reset state
  394. for(DWORD i=0; i< m_dwThreshHold; i++)
  395. {
  396. if(m_ppInst[i] != NULL)
  397. (m_ppInst[i])->Release();
  398. m_ppInst[i] = NULL;
  399. }
  400. m_dwIndex = 0;
  401. }
  402. return;
  403. }
  404. void
  405. CWbemInstanceMgr::SetStatus(
  406. LONG lFlags,
  407. HRESULT hr,
  408. const BSTR strParam,
  409. IWbemClassObject* pObjParam)
  410. {
  411. m_pSink->SetStatus(
  412. lFlags,
  413. hr,
  414. strParam,
  415. pObjParam);
  416. }