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.

718 lines
21 KiB

  1. #include "precomp.h"
  2. #include <stdio.h>
  3. #include <wbemutil.h>
  4. #include <ArrTempl.h>
  5. #include <lmaccess.h>
  6. #include <wbemdisp.h>
  7. #include "ScriptKiller.h"
  8. #include <ErrorObj.h>
  9. #include "script.h"
  10. #include "ClassFac.h"
  11. #include <GroupsForUser.h>
  12. #include <GenUtils.h>
  13. #include <strsafe.h>
  14. #define SCRIPT_PROPNAME_SCRIPT L"ScriptText"
  15. #define SCRIPT_PROPNAME_FILENAME L"ScriptFilename"
  16. #define SCRIPT_PROPNAME_ENGINE L"ScriptingEngine"
  17. #define SCRIPT_PROPNAME_TIMEOUT L"KillTimeout"
  18. #define SCRIPT_EVENTNAME L"TargetEvent"
  19. // uncomment me to remove the WMI script object
  20. // #define NO_DISP_CLASS
  21. #ifdef HOWARDS_DEBUG_CODE
  22. #define NO_DISP_CLASS
  23. #endif // HOWARDS_DEBUG_CODE
  24. HRESULT STDMETHODCALLTYPE CScriptConsumer::XProvider::FindConsumer(
  25. IWbemClassObject* pLogicalConsumer,
  26. IWbemUnboundObjectSink** ppConsumer)
  27. {
  28. if (WMIScriptClassFactory::LimitReached())
  29. return RPC_E_DISCONNECTED;
  30. CScriptSink* pSink = new CScriptSink(m_pObject->m_pControl);
  31. if (!pSink)
  32. return WBEM_E_OUT_OF_MEMORY;
  33. HRESULT hres = pSink->Initialize(pLogicalConsumer);
  34. if(FAILED(hres))
  35. {
  36. delete pSink;
  37. *ppConsumer = NULL;
  38. return hres;
  39. }
  40. else return pSink->QueryInterface(IID_IWbemUnboundObjectSink,
  41. (void**)ppConsumer);
  42. }
  43. void* CScriptConsumer::GetInterface(REFIID riid)
  44. {
  45. if(riid == IID_IWbemEventConsumerProvider)
  46. return &m_XProvider;
  47. else return NULL;
  48. }
  49. class CScriptSite : public IActiveScriptSite, public IActiveScriptSiteWindow
  50. {
  51. protected:
  52. long m_lRef;
  53. IDispatch* m_pObject;
  54. CScriptSink* m_pSink;
  55. HRESULT m_hr;
  56. public:
  57. CScriptSite(CScriptSink* pSink, IDispatch* pObject);
  58. ~CScriptSite();
  59. HRESULT GetScriptHResult()
  60. { return m_hr; }
  61. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv);
  62. ULONG STDMETHODCALLTYPE AddRef();
  63. ULONG STDMETHODCALLTYPE Release();
  64. virtual HRESULT STDMETHODCALLTYPE GetLCID(
  65. /* [out] */ LCID __RPC_FAR *plcid);
  66. virtual HRESULT STDMETHODCALLTYPE GetItemInfo(
  67. /* [in] */ LPCOLESTR pstrName,
  68. /* [in] */ DWORD dwReturnMask,
  69. /* [out] */ IUnknown __RPC_FAR *__RPC_FAR *ppiunkItem,
  70. /* [out] */ ITypeInfo __RPC_FAR *__RPC_FAR *ppti);
  71. virtual HRESULT STDMETHODCALLTYPE GetDocVersionString(
  72. /* [out] */ BSTR __RPC_FAR *pbstrVersion);
  73. virtual HRESULT STDMETHODCALLTYPE OnScriptTerminate(
  74. /* [in] */ const VARIANT __RPC_FAR *pvarResult,
  75. /* [in] */ const EXCEPINFO __RPC_FAR *pexcepinfo);
  76. virtual HRESULT STDMETHODCALLTYPE OnStateChange(
  77. /* [in] */ SCRIPTSTATE ssScriptState);
  78. virtual HRESULT STDMETHODCALLTYPE OnScriptError(
  79. /* [in] */ IActiveScriptError __RPC_FAR *pscripterror);
  80. virtual HRESULT STDMETHODCALLTYPE OnEnterScript( void);
  81. virtual HRESULT STDMETHODCALLTYPE OnLeaveScript( void);
  82. virtual HRESULT STDMETHODCALLTYPE GetWindow(
  83. /* [out] */ HWND __RPC_FAR *phwnd);
  84. virtual HRESULT STDMETHODCALLTYPE EnableModeless(
  85. /* [in] */ BOOL fEnable);
  86. };
  87. CScriptSite::CScriptSite(CScriptSink* pSink, IDispatch* pObject) :
  88. m_lRef(0), m_hr(0)
  89. {
  90. m_pSink = pSink;
  91. m_pSink->AddRef();
  92. m_pObject = pObject;
  93. if(m_pObject)
  94. m_pObject->AddRef();
  95. }
  96. CScriptSite::~CScriptSite()
  97. {
  98. if (m_pObject)
  99. m_pObject->Release();
  100. if (m_pSink)
  101. m_pSink->Release();
  102. }
  103. HRESULT STDMETHODCALLTYPE CScriptSite::QueryInterface(REFIID riid, void** ppv)
  104. {
  105. if(riid == IID_IUnknown || riid == IID_IActiveScriptSite)
  106. *ppv = (IActiveScriptSite*)this;
  107. else if(riid == IID_IActiveScriptSiteWindow)
  108. *ppv = (IActiveScriptSiteWindow*)this;
  109. else
  110. return E_NOINTERFACE;
  111. ((IUnknown*)*ppv)->AddRef();
  112. return S_OK;
  113. }
  114. ULONG STDMETHODCALLTYPE CScriptSite::AddRef()
  115. {
  116. return InterlockedIncrement(&m_lRef);
  117. }
  118. ULONG STDMETHODCALLTYPE CScriptSite::Release()
  119. {
  120. long lRef = InterlockedDecrement(&m_lRef);
  121. if(lRef == 0)
  122. delete this;
  123. return lRef;
  124. }
  125. HRESULT STDMETHODCALLTYPE CScriptSite::GetLCID(
  126. /* [out] */ LCID __RPC_FAR *plcid)
  127. {
  128. return E_NOTIMPL;
  129. }
  130. HRESULT STDMETHODCALLTYPE CScriptSite::GetItemInfo(
  131. /* [in] */ LPCOLESTR pstrName,
  132. /* [in] */ DWORD dwReturnMask,
  133. /* [out] */ IUnknown __RPC_FAR *__RPC_FAR *ppiunkItem,
  134. /* [out] */ ITypeInfo __RPC_FAR *__RPC_FAR *ppti)
  135. {
  136. if(wbem_wcsicmp(pstrName, SCRIPT_EVENTNAME))
  137. return TYPE_E_ELEMENTNOTFOUND;
  138. if(ppti)
  139. *ppti = NULL;
  140. if(ppiunkItem)
  141. *ppiunkItem = NULL;
  142. if(dwReturnMask & SCRIPTINFO_IUNKNOWN)
  143. {
  144. if(ppiunkItem == NULL)
  145. return E_POINTER;
  146. m_pObject->QueryInterface(IID_IUnknown, (void**)ppiunkItem);
  147. }
  148. return S_OK;
  149. }
  150. HRESULT STDMETHODCALLTYPE CScriptSite::GetDocVersionString(
  151. /* [out] */ BSTR __RPC_FAR *pbstrVersion)
  152. { return E_NOTIMPL;}
  153. HRESULT STDMETHODCALLTYPE CScriptSite::OnScriptTerminate(
  154. /* [in] */ const VARIANT __RPC_FAR *pvarResult,
  155. /* [in] */ const EXCEPINFO __RPC_FAR *pexcepinfo)
  156. {
  157. return S_OK;
  158. }
  159. HRESULT STDMETHODCALLTYPE CScriptSite::OnStateChange(
  160. /* [in] */ SCRIPTSTATE ssScriptState)
  161. { return S_OK;}
  162. HRESULT STDMETHODCALLTYPE CScriptSite::OnScriptError(
  163. /* [in] */ IActiveScriptError __RPC_FAR *pscripterror)
  164. {
  165. HRESULT hres;
  166. EXCEPINFO ei;
  167. hres = pscripterror->GetExceptionInfo(&ei);
  168. if(SUCCEEDED(hres))
  169. {
  170. if (ei.bstrSource)
  171. {
  172. m_pSink->m_wsErrorMessage = ei.bstrSource;
  173. m_pSink->m_wsErrorMessage += L": ";
  174. }
  175. if (ei.bstrDescription)
  176. m_pSink->m_wsErrorMessage += ei.bstrDescription;
  177. else
  178. m_pSink->m_wsErrorMessage += L"unknown";
  179. if ((ei.wCode != 0) && (ei.wCode >= 1000))
  180. m_hr = WBEM_E_FAILED;
  181. else
  182. m_hr = ei.scode;
  183. }
  184. DWORD cookie;
  185. ULONG lineNo = 0;
  186. LONG charPos = 0;
  187. hres = pscripterror->GetSourcePosition(&cookie, &lineNo, &charPos);
  188. // we will construct an error message of the form:
  189. // filename.vbs (3,15)
  190. if(SUCCEEDED(hres))
  191. {
  192. if (m_pSink->m_wsScriptFileName.Length() > 0)
  193. m_pSink->m_wsErrorLine = m_pSink->m_wsScriptFileName;
  194. else
  195. m_pSink->m_wsErrorLine = SCRIPT_PROPNAME_SCRIPT;
  196. // if the sprintf fails for some reason, no problem we concat a null sting.
  197. WCHAR buf[256] = L"\0";
  198. // experimentation shows that the line/pos appear to be zero based: add one.
  199. // swprintf(buf, L" (%u,%d)", lineNo +1, charPos +1);
  200. StringCchPrintfW(buf, 256, L" (%u,%d)", lineNo +1, charPos +1);
  201. m_pSink->m_wsErrorLine += buf;
  202. }
  203. return S_OK;
  204. }
  205. HRESULT STDMETHODCALLTYPE CScriptSite::OnEnterScript( void)
  206. { return S_OK;}
  207. HRESULT STDMETHODCALLTYPE CScriptSite::OnLeaveScript( void)
  208. { return S_OK;}
  209. HRESULT STDMETHODCALLTYPE CScriptSite::GetWindow(
  210. /* [out] */ HWND __RPC_FAR *phwnd)
  211. {
  212. *phwnd = NULL;
  213. return S_OK;
  214. }
  215. HRESULT STDMETHODCALLTYPE CScriptSite::EnableModeless(
  216. /* [in] */ BOOL fEnable)
  217. {return S_OK;}
  218. CScriptSink::~CScriptSink()
  219. {
  220. if(m_pEngineFac)
  221. m_pEngineFac->Release();
  222. if (m_pErrorObj)
  223. m_pErrorObj->Release();
  224. }
  225. HRESULT CScriptSink::Initialize(IWbemClassObject* pLogicalConsumer)
  226. {
  227. VARIANT v;
  228. VariantInit(&v);
  229. // this is actually a pointer to a static object
  230. // if it fails, something is Very, Very Wrong.
  231. m_pErrorObj = ErrorObj::GetErrorObj();
  232. if (!m_pErrorObj)
  233. return WBEM_E_CRITICAL_ERROR;
  234. BSTR propName;
  235. propName = SysAllocString(L"CreatorSID");
  236. if (!propName)
  237. return WBEM_E_OUT_OF_MEMORY;
  238. CSysFreeMe freeName(propName);
  239. if (SUCCEEDED(pLogicalConsumer->Get(propName, 0, &v, NULL, NULL)))
  240. {
  241. HRESULT hDebug;
  242. long ubound;
  243. hDebug = SafeArrayGetUBound(V_ARRAY(&v), 1, &ubound);
  244. PVOID pVoid;
  245. hDebug = SafeArrayAccessData(V_ARRAY(&v), &pVoid);
  246. if(SUCCEEDED(hDebug))
  247. {
  248. m_pSidCreator = new BYTE[ubound +1];
  249. if (m_pSidCreator)
  250. memcpy(m_pSidCreator, pVoid, ubound + 1);
  251. else
  252. {
  253. SafeArrayUnaccessData(V_ARRAY(&v));
  254. return WBEM_E_OUT_OF_MEMORY;
  255. }
  256. SafeArrayUnaccessData(V_ARRAY(&v));
  257. }
  258. }
  259. else
  260. {
  261. return WBEM_E_OUT_OF_MEMORY;
  262. }
  263. // Get the information
  264. // ===================
  265. HRESULT hres;
  266. VariantInit(&v);
  267. hres = pLogicalConsumer->Get(SCRIPT_PROPNAME_ENGINE, 0, &v, NULL, NULL);
  268. if(FAILED(hres) || V_VT(&v) != VT_BSTR)
  269. {
  270. m_pErrorObj->ReportError(SCRIPT_PROPNAME_ENGINE, NULL, NULL, WBEM_E_INVALID_PARAMETER, true);
  271. return WBEM_E_INVALID_PARAMETER;
  272. }
  273. WString wsEngine = V_BSTR(&v);
  274. VariantClear(&v);
  275. hres = pLogicalConsumer->Get(SCRIPT_PROPNAME_TIMEOUT, 0, &v, NULL, NULL);
  276. if(V_VT(&v) == VT_I4)
  277. m_dwKillTimeout = V_I4(&v);
  278. else
  279. m_dwKillTimeout = 0;
  280. VariantClear(&v);
  281. hres = pLogicalConsumer->Get(SCRIPT_PROPNAME_SCRIPT, 0, &v, NULL, NULL);
  282. if (SUCCEEDED(hres))
  283. {
  284. if (V_VT(&v) == VT_BSTR)
  285. {
  286. m_wsScript = V_BSTR(&v);
  287. VariantClear(&v);
  288. }
  289. else
  290. // try the script file name approach
  291. {
  292. hres = pLogicalConsumer->Get(SCRIPT_PROPNAME_FILENAME, 0, &v, NULL, NULL);
  293. if (SUCCEEDED(hres) && (V_VT(&v) == VT_BSTR))
  294. {
  295. m_wsScriptFileName = V_BSTR(&v);
  296. VariantClear(&v);
  297. }
  298. else
  299. {
  300. m_pErrorObj->ReportError(L"Initialize", L"ScriptText, ScriptFilename", NULL, WBEM_E_ILLEGAL_NULL, true);
  301. return WBEM_E_INVALID_PARAMETER;
  302. }
  303. }
  304. }
  305. else
  306. return WBEM_E_INVALID_PARAMETER;
  307. // Get the CLSID
  308. // =============
  309. CLSID clsid;
  310. if (wsEngine.Length() == 0)
  311. hres = WBEM_E_INVALID_PARAMETER;
  312. else
  313. hres = CLSIDFromProgID((LPCWSTR)wsEngine, &clsid);
  314. if(FAILED(hres))
  315. {
  316. ERRORTRACE((LOG_ESS, "Scripting engine '%S' not found: %X\n",
  317. (LPCWSTR)wsEngine, hres));
  318. m_pErrorObj->ReportError(L"Initialize", (WCHAR *)wsEngine, NULL, hres, true);
  319. return hres;
  320. }
  321. hres = CoGetClassObject(clsid, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
  322. NULL, IID_IClassFactory, (void**)&m_pEngineFac);
  323. if(FAILED(hres))
  324. {
  325. ERRORTRACE((LOG_ESS, "Unable to create scripting engine %S: %X\n",
  326. (LPCWSTR)wsEngine, hres));
  327. m_pErrorObj->ReportError(L"Initialize", (WCHAR*)wsEngine, NULL, hres, true);
  328. return hres;
  329. }
  330. return S_OK;
  331. }
  332. // runs the script contained in the script text
  333. HRESULT CScriptSink::RunScriptText(IWbemClassObject *pObj)
  334. {
  335. HRESULT hres = S_OK;
  336. WMIScriptClassFactory::IncrementScriptsRun();
  337. IActiveScript* pScript;
  338. hres = m_pEngineFac->CreateInstance(NULL, IID_IActiveScript,
  339. (void**)&pScript);
  340. if(FAILED(hres))
  341. {
  342. ERRORTRACE((LOG_ESS, "Unable to create a script. Error code %X\n",
  343. hres));
  344. return hres;
  345. }
  346. IActiveScriptParse* pParse;
  347. hres = pScript->QueryInterface(IID_IActiveScriptParse, (void**)&pParse);
  348. if(FAILED(hres))
  349. {
  350. ERRORTRACE((LOG_ESS, "Scripting engine does not support "
  351. "parsing!\n"));
  352. pScript->Release();
  353. return hres;
  354. }
  355. IDispatch* pDObject;
  356. #ifdef NO_DISP_CLASS
  357. pDObject = NULL;
  358. #else
  359. IBindCtx *pbc = NULL;;
  360. IMoniker *pMk = NULL;
  361. ULONG chEaten = 0;
  362. if(FAILED(hres = CreateBindCtx(0, &pbc)))
  363. {
  364. ERRORTRACE((LOG_ESS, "Unable to Create IBindCtx: 0x%X\n", hres));
  365. pScript->Release();
  366. return hres;
  367. }
  368. if(FAILED(hres = pbc->RegisterObjectParam(L"WmiObject", pObj)))
  369. {
  370. ERRORTRACE((LOG_ESS, "Unable to Register IBindCtx: 0x%X\n", hres));
  371. pScript->Release();
  372. return hres;
  373. }
  374. if(FAILED(hres = MkParseDisplayName(pbc, L"winmgmts:", &chEaten, &pMk)))
  375. {
  376. ERRORTRACE((LOG_ESS, "Unable to MkParseDisplayName: 0x%X\n", hres));
  377. pScript->Release();
  378. return hres;
  379. }
  380. if(FAILED(hres = pMk->BindToObject(pbc, 0, IID_ISWbemObject, (void **)&pDObject)))
  381. {
  382. ERRORTRACE((LOG_ESS, "Unable to BindToObject: 0x%X\n", hres));
  383. pScript->Release();
  384. return hres;
  385. }
  386. pMk->Release();
  387. pbc->Release();
  388. #endif
  389. CScriptSite* pSite = new CScriptSite(this, pDObject);
  390. pSite->AddRef();
  391. #ifndef NO_DISP_CLASS
  392. if(pDObject) pDObject->Release();
  393. #endif
  394. hres = pScript->SetScriptSite(pSite);
  395. hres = pParse->InitNew();
  396. if(FAILED(hres))
  397. {
  398. ERRORTRACE((LOG_ESS, "Failed to initialize script(InitNew): %X\n",
  399. hres));
  400. pSite->Release();
  401. pScript->Release();
  402. pParse->Release();
  403. return hres;
  404. }
  405. #ifndef NO_DISP_CLASS
  406. hres = pScript->AddNamedItem(SCRIPT_EVENTNAME,
  407. SCRIPTITEM_ISVISIBLE | SCRIPTITEM_NOCODE);
  408. if(FAILED(hres))
  409. {
  410. ERRORTRACE((LOG_ESS, "Failed to add named item: %X\n", hres));
  411. pSite->Release();
  412. pScript->Release();
  413. pParse->Release();
  414. return hres;
  415. }
  416. #endif
  417. EXCEPINFO ei;
  418. hres = pParse->ParseScriptText(
  419. (LPCWSTR)m_wsScript,
  420. NULL, NULL, NULL,
  421. 0, 0, 0, NULL, &ei);
  422. if(FAILED(hres))
  423. {
  424. ERRORTRACE((LOG_ESS, "Failed to parse script. Error code %X\n"
  425. "Scripting engine says: %S\n", hres,
  426. (LPCWSTR)m_wsErrorMessage));
  427. m_pErrorObj->ReportError(L"ParseScriptText", m_wsErrorLine, m_wsErrorMessage, hres, true);
  428. pSite->Release();
  429. pScript->Release();
  430. pParse->Release();
  431. return hres;
  432. }
  433. pParse->Release();
  434. if (m_dwKillTimeout)
  435. {
  436. FILETIME now;
  437. GetSystemTimeAsFileTime(&now);
  438. WAYCOOL_FILETIME expires(now);
  439. expires.AddSeconds(m_dwKillTimeout);
  440. SCRIPTTHREADID threadID;
  441. hres = pScript->GetScriptThreadID(GetCurrentThreadId(), &threadID);
  442. if (SUCCEEDED(hres))
  443. g_scriptKillerTimer.ScheduleAssassination(pScript, expires, threadID);
  444. /************
  445. Doing it in the stream. Probably don't need to.
  446. LPSTREAM pStream;
  447. if (SUCCEEDED(hres) &&
  448. SUCCEEDED(CoMarshalInterThreadInterfaceInStream(IID_IActiveScript, pScript, &pStream)))
  449. {
  450. g_scriptKillerTimer.ScheduleAssassination(pStream, expires, threadID);
  451. }
  452. ***************/
  453. }
  454. hres = pScript->SetScriptState(SCRIPTSTATE_CONNECTED);
  455. if(FAILED(hres))
  456. {
  457. ERRORTRACE((LOG_ESS, "Failed to execute script. Error code 0x%X\n"
  458. "Scripting engine says: %S\n", hres,
  459. (LPCWSTR)m_wsErrorMessage));
  460. m_pErrorObj->ReportError(L"SetScriptState(SCRIPTSTATE_CONNECTED)", m_wsErrorLine, m_wsErrorMessage, hres, true);
  461. }
  462. else if (FAILED(pSite->GetScriptHResult()))
  463. {
  464. hres = pSite->GetScriptHResult();
  465. ERRORTRACE((LOG_ESS, "Error in script execution. Error code 0x%X\n"
  466. "Scripting engine says: %S\n", hres,
  467. (LPCWSTR)m_wsErrorMessage));
  468. m_pErrorObj->ReportError(L"SetScriptState(SCRIPTSTATE_CONNECTED)", m_wsErrorLine, m_wsErrorMessage, hres, true);
  469. }
  470. pScript->Close();
  471. pScript->Release();
  472. pSite->Release();
  473. return hres;
  474. }
  475. HRESULT CScriptSink::RunScriptFile(IWbemClassObject *pObj)
  476. {
  477. HRESULT hr = WBEM_E_INVALID_PARAMETER;
  478. if (m_wsScriptFileName.Length())
  479. {
  480. HANDLE hFile = CreateFileW((LPWSTR)m_wsScriptFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  481. if (hFile != INVALID_HANDLE_VALUE)
  482. {
  483. DWORD fSize;
  484. if (0xFFFFFFFF != (fSize = GetFileSize(hFile, &fSize)))
  485. {
  486. char* pBuf = new char[fSize +2];
  487. if (!pBuf)
  488. hr = WBEM_E_OUT_OF_MEMORY;
  489. else
  490. {
  491. ZeroMemory(pBuf, fSize+2);
  492. DWORD bitsRead;
  493. if (ReadFile(hFile, pBuf, fSize, &bitsRead, NULL))
  494. {
  495. hr = WBEM_S_NO_ERROR;
  496. const WCHAR ByteOrderMark = L'\xFEFF';
  497. // determine whether this is a unicode file
  498. if (((WCHAR*)pBuf)[0] == ByteOrderMark)
  499. m_wsScript.BindPtr((WCHAR*)pBuf);
  500. else
  501. {
  502. int length = strlen(pBuf) +1;
  503. // not unicode, do the conversion
  504. WCHAR* pWideBuf = new WCHAR[length];
  505. if (!pWideBuf)
  506. hr = WBEM_E_OUT_OF_MEMORY;
  507. else
  508. {
  509. if (!MultiByteToWideChar(CP_THREAD_ACP, MB_PRECOMPOSED, pBuf, length, pWideBuf, length))
  510. {
  511. ERRORTRACE((LOG_ESS, "Script: cannot convert %s, 0x%08X\n", pBuf, GetLastError()));
  512. hr = WBEM_E_FAILED;
  513. }
  514. else
  515. m_wsScript.BindPtr(pWideBuf);
  516. }
  517. // delete the old buffer - we saved a copy
  518. delete[] pBuf;
  519. }
  520. if (SUCCEEDED(hr))
  521. hr = RunScriptText(pObj);
  522. }
  523. else
  524. {
  525. ERRORTRACE((LOG_ESS, "Script: Cannot read %S, 0x%X\n", (LPWSTR)m_wsScriptFileName, GetLastError()));
  526. delete[] pBuf;
  527. hr = WBEM_E_FAILED;
  528. }
  529. }
  530. }
  531. CloseHandle(hFile);
  532. }
  533. else
  534. ERRORTRACE((LOG_ESS, "Script: Cannot Open %S, 0x%X\n", (LPWSTR)m_wsScriptFileName, GetLastError()));
  535. }
  536. return hr;
  537. }
  538. HRESULT STDMETHODCALLTYPE CScriptSink::XSink::IndicateToConsumer(
  539. IWbemClassObject* pLogicalConsumer, long lNumObjects,
  540. IWbemClassObject** apObjects)
  541. {
  542. PSID pSidSystem;
  543. SID_IDENTIFIER_AUTHORITY id = SECURITY_NT_AUTHORITY;
  544. if (AllocateAndInitializeSid(&id, 1,
  545. SECURITY_LOCAL_SYSTEM_RID,
  546. 0, 0,0,0,0,0,0,&pSidSystem))
  547. {
  548. // guilty until proven innocent
  549. HRESULT hr = WBEM_E_ACCESS_DENIED;
  550. // check to see if sid is either Local System or an admin of some sort...
  551. if ((EqualSid(pSidSystem, m_pObject->m_pSidCreator)) ||
  552. (S_OK == IsUserAdministrator(m_pObject->m_pSidCreator)))
  553. hr = WBEM_S_NO_ERROR;
  554. // We're done with this
  555. FreeSid(pSidSystem);
  556. if (FAILED(hr))
  557. return hr;
  558. }
  559. else
  560. return WBEM_E_OUT_OF_MEMORY;
  561. if (WMIScriptClassFactory::LimitReached())
  562. return RPC_E_DISCONNECTED;
  563. HRESULT hrOutter = WBEM_S_NO_ERROR;
  564. for(int i = 0; i < lNumObjects; i++)
  565. {
  566. HRESULT hrInner;
  567. if (m_pObject->m_wsScript.Length())
  568. hrInner = m_pObject->RunScriptText(apObjects[i]);
  569. else if (m_pObject->m_wsScriptFileName.Length())
  570. hrInner = m_pObject->RunScriptFile(apObjects[i]);
  571. else
  572. {
  573. m_pObject->m_pErrorObj->ReportError(L"IndicateToConsumer", L"ScriptText, ScriptFilename", NULL, WBEM_E_ILLEGAL_NULL, true);
  574. return WBEM_E_INVALID_PARAMETER;
  575. }
  576. if (FAILED(hrInner))
  577. {
  578. m_pObject->m_pErrorObj->ReportError(L"IndicateToConsumer", m_pObject->m_wsErrorLine, m_pObject->m_wsErrorMessage, hrInner, true);
  579. hrOutter = hrInner;
  580. // m_pObject->RaiseErrorStatus();
  581. }
  582. }
  583. return hrOutter;
  584. }
  585. void* CScriptSink::GetInterface(REFIID riid)
  586. {
  587. if(riid == IID_IWbemUnboundObjectSink)
  588. return &m_XSink;
  589. else return NULL;
  590. }