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.

120 lines
4.1 KiB

  1. #include "precomp.h"
  2. #include "ClassFac.h"
  3. #include <arrTempl.h>
  4. DWORD WMIScriptClassFactory::m_scriptsStarted = 0;
  5. DWORD WMIScriptClassFactory::m_scriptsAllowed = 300;
  6. bool WMIScriptClassFactory::m_bIsScriptsAllowedInitialized = false;
  7. bool WMIScriptClassFactory::m_bWeDeadNow = false;
  8. DWORD WMIScriptClassFactory::m_timerID = 0;
  9. HRESULT WMIScriptClassFactory::CreateInstance(IUnknown* pOuter, REFIID riid, void** ppv)
  10. {
  11. if (!m_bIsScriptsAllowedInitialized)
  12. FindScriptsAllowed();
  13. HRESULT hr = CClassFactory<CScriptConsumer>::CreateInstance(pOuter, riid, ppv);
  14. return hr;
  15. }
  16. // our time has come. Curl up & die.
  17. void CALLBACK WMIScriptClassFactory::TimeoutProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
  18. {
  19. CoSuspendClassObjects();
  20. KillTimer(NULL, m_timerID);
  21. m_timerID = 0;
  22. m_bWeDeadNow = true;
  23. }
  24. bool WMIScriptClassFactory::LimitReached(void)
  25. {
  26. return m_bWeDeadNow;
  27. }
  28. // determine number of scripts we're allowed to run
  29. // from the class registration object
  30. void WMIScriptClassFactory::FindScriptsAllowed(void)
  31. {
  32. m_bIsScriptsAllowedInitialized = true;
  33. HRESULT hr;
  34. IWbemLocator* pLocator;
  35. if (SUCCEEDED(hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
  36. IID_IWbemLocator, (void**)&pLocator)))
  37. {
  38. CReleaseMe releaseLocator(pLocator);
  39. BSTR bstrNamespace;
  40. bstrNamespace = SysAllocString(L"root\\CIMv2");
  41. if (bstrNamespace)
  42. {
  43. IWbemServices* pNamespace;
  44. CSysFreeMe freeBstr(bstrNamespace);
  45. hr = pLocator->ConnectServer(bstrNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pNamespace);
  46. if (SUCCEEDED(hr))
  47. {
  48. CReleaseMe relNamespace(pNamespace);
  49. BSTR bstrClassName;
  50. bstrClassName = SysAllocString(L"ScriptingStandardConsumerSetting=@");
  51. if (bstrClassName)
  52. {
  53. IWbemClassObject* pRegistration = NULL;
  54. CSysFreeMe freeTheClassNames(bstrClassName);
  55. hr = pNamespace->GetObject(bstrClassName, 0, NULL, &pRegistration, NULL);
  56. if (SUCCEEDED(hr))
  57. {
  58. CReleaseMe relRegistration(pRegistration);
  59. VARIANT v;
  60. VariantInit(&v);
  61. if (SUCCEEDED(pRegistration->Get(L"MaximumScripts", 0, &v, NULL, NULL))
  62. && ((v.vt == VT_I4) || (v.vt == VT_UI4))
  63. && (v.ulVal > 0))
  64. {
  65. m_scriptsAllowed = (DWORD)v.ulVal;
  66. VariantClear(&v);
  67. }
  68. if (SUCCEEDED(hr = pRegistration->Get(L"Timeout", 0, &v, NULL, NULL))
  69. && (v.vt == VT_I4))
  70. {
  71. // maximum to prevent overflow, doc'd in MOF
  72. if ((((DWORD)v.lVal) <= 71000) && ((DWORD)v.lVal > 0))
  73. {
  74. UINT nMilliseconds = (DWORD)v.lVal * 1000 * 60;
  75. m_timerID = SetTimer(NULL, 0, nMilliseconds, TimeoutProc);
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. // after the specified number of scripts have been run
  85. // we suspend the class object
  86. // note that access to m_scriptsStarted is not serialized.
  87. // this should not cause a problem, m_scripts allowed does not change
  88. // after instanciation, and if we blow it, it just means we allow
  89. // an extra script to be run, or call CoSuspend an extra time.
  90. void WMIScriptClassFactory::IncrementScriptsRun(void)
  91. {
  92. InterlockedIncrement((long*)&m_scriptsStarted);
  93. if (m_scriptsStarted >= m_scriptsAllowed)
  94. {
  95. CoSuspendClassObjects();
  96. m_bWeDeadNow = true;
  97. }
  98. }