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.

485 lines
10 KiB

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