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.

566 lines
18 KiB

  1. // Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  2. // JobObjSecLimitInfoProv.cpp
  3. //#define _WIN32_WINNT 0x0500
  4. #include "precomp.h"
  5. //#include <windows.h>
  6. #include "cominit.h"
  7. //#include <objbase.h>
  8. //#include <comdef.h>
  9. #include "CUnknown.h"
  10. #include <wbemprov.h>
  11. #include "FRQueryEx.h"
  12. #include "globals.h"
  13. #include "Factory.h"
  14. #include "helpers.h"
  15. #include <map>
  16. #include <vector>
  17. #include "SmartHandle.h"
  18. #include <crtdbg.h>
  19. #include "CVARIANT.h"
  20. #include "CObjProps.h"
  21. #include "CJobObjSecLimitInfoProps.h"
  22. #include "JobObjSecLimitInfoProv.h"
  23. /*****************************************************************************/
  24. // QueryInterface override to allow for this component's interface(s)
  25. /*****************************************************************************/
  26. STDMETHODIMP CJobObjSecLimitInfoProv::QueryInterface(const IID& iid, void** ppv)
  27. {
  28. HRESULT hr = S_OK;
  29. if(iid == IID_IWbemServices)
  30. {
  31. *ppv = static_cast<IWbemServices*>(this);
  32. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  33. }
  34. else if(iid == IID_IWbemProviderInit)
  35. {
  36. *ppv = static_cast<IWbemProviderInit*>(this);
  37. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  38. }
  39. else
  40. {
  41. hr = CUnknown::QueryInterface(iid, ppv);
  42. }
  43. return hr;
  44. }
  45. /*****************************************************************************/
  46. // Creation function used by CFactory
  47. /*****************************************************************************/
  48. HRESULT CJobObjSecLimitInfoProv::CreateInstance(CUnknown** ppNewComponent)
  49. {
  50. HRESULT hr = S_OK;
  51. CUnknown* pUnk = NULL;
  52. pUnk = new CJobObjSecLimitInfoProv;
  53. if(pUnk != NULL)
  54. {
  55. *ppNewComponent = pUnk;
  56. }
  57. else
  58. {
  59. hr = E_OUTOFMEMORY;
  60. }
  61. return hr ;
  62. }
  63. /*****************************************************************************/
  64. // IWbemProviderInit implementation
  65. /*****************************************************************************/
  66. STDMETHODIMP CJobObjSecLimitInfoProv::Initialize(
  67. LPWSTR pszUser,
  68. LONG lFlags,
  69. LPWSTR pszNamespace,
  70. LPWSTR pszLocale,
  71. IWbemServices *pNamespace,
  72. IWbemContext *pCtx,
  73. IWbemProviderInitSink *pInitSink)
  74. {
  75. m_pNamespace = pNamespace;
  76. m_chstrNamespace = pszNamespace;
  77. //Let CIMOM know you are initialized
  78. //==================================
  79. return pInitSink->SetStatus(
  80. WBEM_S_INITIALIZED,
  81. 0);
  82. }
  83. /*****************************************************************************/
  84. // IWbemServices implementation
  85. /*****************************************************************************/
  86. STDMETHODIMP CJobObjSecLimitInfoProv::GetObjectAsync(
  87. const BSTR ObjectPath,
  88. long lFlags,
  89. IWbemContext __RPC_FAR *pCtx,
  90. IWbemObjectSink __RPC_FAR *pResponseHandler)
  91. {
  92. HRESULT hr = WBEM_E_NOT_FOUND;
  93. IWbemClassObjectPtr pStatusObject;
  94. try
  95. {
  96. HRESULT hrImp = CheckImpersonationLevel();
  97. if(SUCCEEDED(hrImp))
  98. {
  99. // We need the name of the instance they requested...
  100. WCHAR wstrObjInstKeyVal[MAX_PATH];
  101. hr = GetObjInstKeyVal(
  102. ObjectPath,
  103. IDS_Win32_NamedJobObjectSecLimitSetting,
  104. g_rgJobObjSecLimitInfoPropNames[JOSECLMTPROP_ID],
  105. wstrObjInstKeyVal,
  106. sizeof(wstrObjInstKeyVal) - sizeof(WCHAR));
  107. if(SUCCEEDED(hr))
  108. {
  109. // wstrObjInstKeyVal now contains the name of the object. See if
  110. // it exists...
  111. CHString chstrUndecoratedJOName;
  112. UndecorateJOName(
  113. wstrObjInstKeyVal,
  114. chstrUndecoratedJOName);
  115. SmartHandle hJob;
  116. hJob = ::OpenJobObjectW(
  117. MAXIMUM_ALLOWED,
  118. FALSE,
  119. chstrUndecoratedJOName);
  120. if(hJob)
  121. {
  122. // We seem to have found one matching the specified name,
  123. // so create a return instance...
  124. IWbemClassObjectPtr pIWCO = NULL;
  125. CJobObjSecLimitInfoProps cjoslip(hJob, m_chstrNamespace);
  126. hr = CreateInst(
  127. m_pNamespace,
  128. &pIWCO,
  129. _bstr_t(IDS_Win32_NamedJobObjectSecLimitSetting),
  130. pCtx);
  131. if(SUCCEEDED(hr))
  132. {
  133. cjoslip.SetReqProps(PROP_ALL_REQUIRED);
  134. }
  135. if(SUCCEEDED(hr))
  136. {
  137. // set the key properties...
  138. hr = cjoslip.SetKeysFromPath(
  139. ObjectPath,
  140. pCtx);
  141. }
  142. if(SUCCEEDED(hr))
  143. {
  144. // set the non-key requested properties...
  145. hr = cjoslip.SetNonKeyReqProps();
  146. }
  147. if(SUCCEEDED(hr))
  148. {
  149. // Load requested non-key properties
  150. // to the instance...
  151. hr = cjoslip.LoadPropertyValues(
  152. pIWCO,
  153. pCtx,
  154. m_pNamespace);
  155. // Commit the instance...
  156. if(SUCCEEDED(hr))
  157. {
  158. IWbemClassObject *pTmp = (IWbemClassObject*) pIWCO;
  159. hr = pResponseHandler->Indicate(
  160. 1,
  161. &pTmp);
  162. }
  163. }
  164. }
  165. else
  166. {
  167. hr = WBEM_E_NOT_FOUND;
  168. SetStatusObject(
  169. pCtx,
  170. m_pNamespace,
  171. ::GetLastError(),
  172. NULL,
  173. L"::OpenJobObject",
  174. JOB_OBJECT_NAMESPACE,
  175. &pStatusObject);
  176. }
  177. }
  178. }
  179. else
  180. {
  181. hr = hrImp;
  182. }
  183. }
  184. catch(CVARIANTError& cve)
  185. {
  186. hr = cve.GetWBEMError();
  187. }
  188. catch(...)
  189. {
  190. hr = WBEM_E_PROVIDER_FAILURE;
  191. }
  192. // Set Status
  193. return pResponseHandler->SetStatus(0, hr, NULL, pStatusObject);
  194. }
  195. STDMETHODIMP CJobObjSecLimitInfoProv::ExecQueryAsync(
  196. const BSTR QueryLanguage,
  197. const BSTR Query,
  198. long lFlags,
  199. IWbemContext __RPC_FAR *pCtx,
  200. IWbemObjectSink __RPC_FAR *pResponseHandler)
  201. {
  202. HRESULT hr = WBEM_S_NO_ERROR;
  203. IWbemClassObjectPtr pStatusObject;
  204. try
  205. {
  206. HRESULT hrImp = CheckImpersonationLevel();
  207. if(SUCCEEDED(hrImp))
  208. {
  209. // We will optimize for those cases in which
  210. // a particular set of named job objects
  211. // (e.g., 1 or more). Enumerate also
  212. // optimizes for the properties that were
  213. // requested.
  214. CFrameworkQuery cfwq;
  215. hr = cfwq.Init(
  216. QueryLanguage,
  217. Query,
  218. lFlags,
  219. m_chstrNamespace);
  220. std::vector<_bstr_t> rgNamedJOs;
  221. if(SUCCEEDED(hr))
  222. {
  223. hr = cfwq.GetValuesForProp(
  224. _bstr_t(g_rgJobObjSecLimitInfoPropNames[JOSECLMTPROP_ID]),
  225. rgNamedJOs);
  226. }
  227. // If none were specifically requested, they
  228. // want them all...
  229. if(rgNamedJOs.size() == 0)
  230. {
  231. hr = GetJobObjectList(rgNamedJOs);
  232. }
  233. else
  234. {
  235. // Object paths were specified. Before
  236. // passing them along, we need to un-
  237. // decorate them.
  238. UndecorateNamesInNamedJONameList(rgNamedJOs);
  239. }
  240. // Find out what propeties were requested...
  241. CJobObjSecLimitInfoProps cjoslip(m_chstrNamespace);
  242. cjoslip.GetWhichPropsReq(cfwq);
  243. if(SUCCEEDED(hr))
  244. {
  245. hr = Enumerate(
  246. pCtx,
  247. pResponseHandler,
  248. rgNamedJOs,
  249. cjoslip,
  250. &pStatusObject);
  251. }
  252. else
  253. {
  254. SetStatusObject(
  255. pCtx,
  256. m_pNamespace,
  257. -1L,
  258. NULL,
  259. L"Helpers.cpp::GetJobObjectList",
  260. JOB_OBJECT_NAMESPACE,
  261. &pStatusObject);
  262. }
  263. }
  264. else
  265. {
  266. hr = hrImp;
  267. }
  268. }
  269. catch(CVARIANTError& cve)
  270. {
  271. hr = cve.GetWBEMError();
  272. }
  273. catch(...)
  274. {
  275. hr = WBEM_E_PROVIDER_FAILURE;
  276. }
  277. // Set Status
  278. return pResponseHandler->SetStatus(0, hr, NULL, pStatusObject);
  279. }
  280. STDMETHODIMP CJobObjSecLimitInfoProv::CreateInstanceEnumAsync(
  281. const BSTR Class,
  282. long lFlags,
  283. IWbemContext __RPC_FAR *pCtx,
  284. IWbemObjectSink __RPC_FAR *pResponseHandler)
  285. {
  286. HRESULT hr = WBEM_S_NO_ERROR;
  287. IWbemClassObjectPtr pStatusObject;
  288. try
  289. {
  290. HRESULT hrImp = CheckImpersonationLevel();
  291. if(SUCCEEDED(hrImp))
  292. {
  293. if(_wcsicmp(
  294. Class,
  295. IDS_Win32_NamedJobObjectSecLimitSetting) != NULL)
  296. {
  297. hr = WBEM_E_INVALID_CLASS;
  298. }
  299. // For every job object, return all accounting
  300. // info properties...
  301. if(SUCCEEDED(hr))
  302. {
  303. // Get a list of named jobs...
  304. std::vector<_bstr_t> rgNamedJOs;
  305. hr = GetJobObjectList(rgNamedJOs);
  306. if(SUCCEEDED(hr))
  307. {
  308. CJobObjSecLimitInfoProps cjoslip(m_chstrNamespace);
  309. cjoslip.SetReqProps(PROP_ALL_REQUIRED);
  310. hr = Enumerate(
  311. pCtx,
  312. pResponseHandler,
  313. rgNamedJOs,
  314. cjoslip,
  315. &pStatusObject);
  316. }
  317. else
  318. {
  319. SetStatusObject(
  320. pCtx,
  321. m_pNamespace,
  322. -1L,
  323. NULL,
  324. L"Helpers.cpp::GetJobObjectList",
  325. JOB_OBJECT_NAMESPACE,
  326. &pStatusObject);
  327. }
  328. }
  329. }
  330. else
  331. {
  332. hr = hrImp;
  333. }
  334. }
  335. catch(CVARIANTError& cve)
  336. {
  337. hr = cve.GetWBEMError();
  338. }
  339. catch(...)
  340. {
  341. hr = WBEM_E_PROVIDER_FAILURE;
  342. }
  343. // Set Status
  344. return pResponseHandler->SetStatus(0, hr, NULL, pStatusObject);
  345. }
  346. /*****************************************************************************/
  347. // Private member function implementations
  348. /*****************************************************************************/
  349. HRESULT CJobObjSecLimitInfoProv::Enumerate(
  350. IWbemContext __RPC_FAR *pCtx,
  351. IWbemObjectSink __RPC_FAR *pResponseHandler,
  352. std::vector<_bstr_t>& rgNamedJOs,
  353. CJobObjSecLimitInfoProps& cjoslip,
  354. IWbemClassObject** ppStatusObject)
  355. {
  356. HRESULT hr = S_OK;
  357. hr = CheckImpersonationLevel();
  358. try // CVARIANT can throw and I want the error...
  359. {
  360. if(SUCCEEDED(hr))
  361. {
  362. long lNumJobs = rgNamedJOs.size();
  363. if(lNumJobs > 0)
  364. {
  365. SmartHandle hJob;
  366. for(long m = 0L; m < lNumJobs && SUCCEEDED(hr); m++)
  367. {
  368. cjoslip.ClearProps();
  369. // We have the name of a JO; need to open it up
  370. // and get its properties...
  371. hJob = ::OpenJobObjectW(
  372. MAXIMUM_ALLOWED,
  373. FALSE,
  374. rgNamedJOs[m]);
  375. // (NOTE: hJob smarthandle class automatically
  376. // closes its handle on destruction and on
  377. // reassignment.)
  378. if(hJob)
  379. {
  380. // Set the handle...
  381. cjoslip.SetHandle(hJob);
  382. // Set the key properties directly...
  383. CHString chstrDecoratedJOName;
  384. DecorateJOName(
  385. rgNamedJOs[m],
  386. chstrDecoratedJOName);
  387. std::vector<CVARIANT> vecvKeys;
  388. CVARIANT vID(chstrDecoratedJOName);
  389. vecvKeys.push_back(vID);
  390. hr = cjoslip.SetKeysDirect(vecvKeys);
  391. if(FAILED(hr))
  392. {
  393. SetStatusObject(
  394. pCtx,
  395. m_pNamespace,
  396. ::GetLastError(),
  397. NULL,
  398. L"CJobObjSecLimitInfoProps::SetKeysDirect",
  399. JOB_OBJECT_NAMESPACE,
  400. ppStatusObject);
  401. }
  402. if(SUCCEEDED(hr))
  403. {
  404. // set the non-key requested
  405. // properties...
  406. hr = cjoslip.SetNonKeyReqProps();
  407. if(FAILED(hr))
  408. {
  409. SetStatusObject(
  410. pCtx,
  411. m_pNamespace,
  412. ::GetLastError(),
  413. NULL,
  414. L"CJobObjSecLimitInfoProps::SetNonKeyReqProps",
  415. JOB_OBJECT_NAMESPACE,
  416. ppStatusObject);
  417. }
  418. }
  419. // Create a new outgoing instance...
  420. IWbemClassObjectPtr pIWCO = NULL;
  421. if(SUCCEEDED(hr))
  422. {
  423. hr = CreateInst(
  424. m_pNamespace,
  425. &pIWCO,
  426. _bstr_t(IDS_Win32_NamedJobObjectSecLimitSetting),
  427. pCtx);
  428. if(FAILED(hr))
  429. {
  430. SetStatusObject(
  431. pCtx,
  432. m_pNamespace,
  433. ::GetLastError(),
  434. NULL,
  435. L"CJobObjSecLimitInfoProv::CreateInst",
  436. JOB_OBJECT_NAMESPACE,
  437. ppStatusObject);
  438. }
  439. }
  440. // Load the properties of the
  441. // new outgoing instance...
  442. if(SUCCEEDED(hr))
  443. {
  444. hr = cjoslip.LoadPropertyValues(
  445. pIWCO,
  446. pCtx,
  447. m_pNamespace);
  448. if(FAILED(hr))
  449. {
  450. SetStatusObject(
  451. pCtx,
  452. m_pNamespace,
  453. ::GetLastError(),
  454. NULL,
  455. L"CJobObjSecLimitInfoProps::LoadPropertyValues",
  456. JOB_OBJECT_NAMESPACE,
  457. ppStatusObject);
  458. }
  459. }
  460. // And send it out...
  461. if(SUCCEEDED(hr))
  462. {
  463. IWbemClassObject *pTmp = (IWbemClassObject*) pIWCO;
  464. hr = pResponseHandler->Indicate(
  465. 1,
  466. &pTmp);
  467. }
  468. }
  469. else
  470. {
  471. _ASSERT(0);
  472. hr = WBEM_E_NOT_FOUND;
  473. SetStatusObject(
  474. pCtx,
  475. m_pNamespace,
  476. ::GetLastError(),
  477. NULL,
  478. L"::OpenJobObject",
  479. JOB_OBJECT_NAMESPACE,
  480. ppStatusObject);
  481. }
  482. }
  483. }
  484. }
  485. }
  486. catch(CVARIANTError& cve)
  487. {
  488. hr = cve.GetWBEMError();
  489. }
  490. return hr;
  491. }