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.

521 lines
10 KiB

  1. // ScriptSupport.cpp: implementation for our scripting support class CScriptSupport
  2. //
  3. // Copyright (c)1997-2001 Microsoft Corporation
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "ScriptSupport.h"
  8. /*
  9. Routine Description:
  10. Name:
  11. CScriptSupport::CScriptSupport
  12. Functionality:
  13. Constructor
  14. Virtual:
  15. No
  16. Arguments:
  17. None
  18. Return Value:
  19. None
  20. Notes:
  21. */
  22. CScriptSupport::CScriptSupport ()
  23. {
  24. //
  25. // this is not part of WMI provider, so, we need to do the security
  26. //
  27. HRESULT hr = ::CoInitializeSecurity(
  28. NULL,
  29. -1,
  30. NULL,
  31. NULL,
  32. RPC_C_AUTHN_LEVEL_CONNECT,
  33. RPC_C_IMP_LEVEL_IDENTIFY,
  34. NULL,
  35. EOAC_NONE,
  36. 0
  37. );
  38. }
  39. /*
  40. Routine Description:
  41. Name:
  42. CScriptSupport::~CScriptSupport
  43. Functionality:
  44. Destructor
  45. Virtual:
  46. Yes.
  47. Arguments:
  48. None
  49. Return Value:
  50. None
  51. Notes:
  52. */
  53. CScriptSupport::~CScriptSupport ()
  54. {
  55. }
  56. /*
  57. Routine Description:
  58. Name:
  59. CScriptSupport::InterfaceSupportsErrorInfo
  60. Functionality:
  61. Queries if we support IErrorInfo
  62. Virtual:
  63. Yes (part of ISupportErrorInfo)
  64. Arguments:
  65. riid - Interface ID (guid).
  66. Return Value:
  67. S_OK
  68. Notes:
  69. $undone:shawnwu, This is just testing for now. Don't check in the code.
  70. */
  71. STDMETHODIMP
  72. CScriptSupport::InterfaceSupportsErrorInfo (
  73. REFIID riid
  74. )
  75. {
  76. //return (riid == IID_INetSecProvMgr) ? NOERROR : ResultFromScode(S_FALSE);
  77. return S_FALSE;
  78. }
  79. /*
  80. Routine Description:
  81. Name:
  82. CScriptSupport::get_RandomPortLower
  83. Functionality:
  84. Get the random port's lower bound.
  85. Virtual:
  86. Yes (part of INetSecProvMgr)
  87. Arguments:
  88. plLower - Receivs the lower bound of random port range.
  89. Return Value:
  90. S_OK
  91. Notes:
  92. $undone:shawnwu, This is just testing for now. Don't check in the code.
  93. */
  94. STDMETHODIMP
  95. CScriptSupport::get_RandomPortLower (
  96. OUT long * plLower
  97. )
  98. {
  99. *plLower = 65000;
  100. return S_OK;
  101. }
  102. /*
  103. Routine Description:
  104. Name:
  105. CScriptSupport::gett_RandomPortUpper
  106. Functionality:
  107. Get the random port's upper bound.
  108. Virtual:
  109. Yes (part of INetSecProvMgr)
  110. Arguments:
  111. plUpper - Receivs the upper bound of random port range.
  112. Return Value:
  113. S_OK
  114. Notes:
  115. $undone:shawnwu, This is just testing for now. Don't check in the code.
  116. */
  117. STDMETHODIMP
  118. CScriptSupport::get_RandomPortUpper (
  119. OUT long * plUpper
  120. )
  121. {
  122. *plUpper = 65012;
  123. return S_OK;
  124. }
  125. /*
  126. Routine Description:
  127. Name:
  128. CScriptSupport::GetNamespace
  129. Functionality:
  130. Private helper for finding the provider's service interface given its namespace string.
  131. Virtual:
  132. No.
  133. Arguments:
  134. bstrNamespace - Namespace string.
  135. ppNS - Receives the namespace.
  136. Return Value:
  137. Success: S_OK.
  138. Failure: other error codes.
  139. Notes:
  140. $undone:shawnwu, This is just testing for now. Don't check in the code.
  141. */
  142. HRESULT
  143. CScriptSupport::GetNamespace (
  144. IN BSTR bstrNamespace,
  145. OUT IWbemServices ** ppNS
  146. )
  147. {
  148. if (ppNS == NULL)
  149. {
  150. return E_INVALIDARG;
  151. }
  152. *ppNS = NULL;
  153. CComPtr<IWbemLocator> srpLocator;
  154. HRESULT hr = ::CoCreateInstance(CLSID_WbemLocator,
  155. 0,
  156. CLSCTX_INPROC_SERVER,
  157. IID_IWbemLocator,
  158. (LPVOID *) &srpLocator
  159. );
  160. if (SUCCEEDED(hr) && srpLocator)
  161. {
  162. //
  163. // Ask the locator to find the SCE provider.
  164. //
  165. CComPtr<IWbemServices> srpNamespace;
  166. hr = srpLocator->ConnectServer(bstrNamespace, NULL, NULL, NULL, 0, NULL, NULL, &srpNamespace);
  167. if (SUCCEEDED(hr) && srpNamespace)
  168. {
  169. //
  170. // Set the proxy so that impersonation of the client occurs.
  171. //
  172. hr = ::CoSetProxyBlanket(
  173. srpNamespace,
  174. RPC_C_AUTHN_WINNT,
  175. RPC_C_AUTHZ_NONE,
  176. NULL,
  177. RPC_C_AUTHN_LEVEL_CALL,
  178. RPC_C_IMP_LEVEL_IMPERSONATE,
  179. NULL,
  180. EOAC_NONE
  181. );
  182. if (SUCCEEDED(hr))
  183. {
  184. *ppNS = srpNamespace.Detach();
  185. hr = S_OK;
  186. }
  187. }
  188. }
  189. return hr;
  190. }
  191. /*
  192. Routine Description:
  193. Name:
  194. CScriptSupport::ExecuteQuery
  195. Functionality:
  196. Execute the given query.
  197. Virtual:
  198. Yes (part of INetSecProvMgr)
  199. Arguments:
  200. bstrNaemspace - The provider namespace.
  201. bstrQuery - The query to be executed.
  202. plSucceeded - Receives the execution result. It is = 1 if succeeded and 0 otherwise.
  203. Return Value:
  204. S_OK
  205. Notes:
  206. $undone:shawnwu, This is just testing for now. Don't check in the code.
  207. */
  208. STDMETHODIMP
  209. CScriptSupport::ExecuteQuery (
  210. IN BSTR bstrNamespace,
  211. IN BSTR bstrQuery,
  212. IN BSTR bstrDelimiter,
  213. IN BSTR bstrPropName,
  214. OUT BSTR * pbstrResult
  215. )
  216. {
  217. *pbstrResult = 0;
  218. CComPtr<IWbemServices> srpNamespace;
  219. HRESULT hr = GetNamespace(bstrNamespace, &srpNamespace);
  220. if (SUCCEEDED(hr))
  221. {
  222. CComPtr<IEnumWbemClassObject> srpEnum;
  223. //
  224. // querying the objects
  225. //
  226. hr = srpNamespace->ExecQuery(L"WQL",
  227. bstrQuery,
  228. WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
  229. NULL,
  230. &srpEnum
  231. );
  232. if (SUCCEEDED(hr))
  233. {
  234. //
  235. // find out how many we have
  236. //
  237. ULONG nEnum = 0;
  238. CComVariant var;
  239. //
  240. // push the property values to this vector for later packing
  241. //
  242. std::vector<BSTR> vecPropValues;
  243. //
  244. // total length of the result string, we need a 0 terminator, that is it is initialized to 1
  245. //
  246. long lTotLen = 1;
  247. //
  248. // Length of the delimiter
  249. //
  250. long lDelLen = wcslen(bstrDelimiter);
  251. while (SUCCEEDED(hr))
  252. {
  253. CComPtr<IWbemClassObject> srpObj;
  254. hr = srpEnum->Next(WBEM_INFINITE, 1, &srpObj, &nEnum);
  255. if (SUCCEEDED(hr) && srpObj != NULL)
  256. {
  257. hr = srpObj->Get(bstrPropName, 0, &var, NULL, NULL);
  258. if (SUCCEEDED(hr) && var.vt != VT_EMPTY && var.vt != VT_NULL)
  259. {
  260. CComVariant varResult;
  261. if (SUCCEEDED(::VariantChangeType(&varResult, &var, 0, VT_BSTR)))
  262. {
  263. vecPropValues.push_back(varResult.bstrVal);
  264. lTotLen += wcslen(varResult.bstrVal) + lDelLen;
  265. varResult.vt = VT_EMPTY;
  266. }
  267. }
  268. }
  269. else
  270. {
  271. break;
  272. }
  273. }
  274. *pbstrResult = ::SysAllocStringLen(NULL, lTotLen);
  275. if (*pbstrResult != NULL)
  276. {
  277. //
  278. // running head of copying
  279. //
  280. LPWSTR pDest = *pbstrResult;
  281. pDest[0] = L'\0';
  282. for (int i = 0; i < vecPropValues.size(); i++)
  283. {
  284. wcscpy(pDest, vecPropValues[i]);
  285. pDest += wcslen(vecPropValues[i]);
  286. wcscpy(pDest, bstrDelimiter);
  287. pDest += lDelLen;
  288. }
  289. }
  290. }
  291. }
  292. return S_OK;
  293. }
  294. /*
  295. Routine Description:
  296. Name:
  297. CScriptSupport::GetProperty
  298. Functionality:
  299. Get the property value of the given object's given property.
  300. Virtual:
  301. Yes (part of INetSecProvMgr)
  302. Arguments:
  303. bstrNaemspace - The provider namespace.
  304. bstrObjectPath - The path for the object.
  305. bstrPropName - The name of the property.
  306. pvarValue - Receives the value in string format.
  307. Return Value:
  308. S_OK
  309. Notes:
  310. $undone:shawnwu, This is just testing for now. Don't check in the code.
  311. */
  312. STDMETHODIMP
  313. CScriptSupport::GetProperty (
  314. IN BSTR bstrNamespace,
  315. IN BSTR bstrObjectPath,
  316. IN BSTR bstrPropName,
  317. OUT VARIANT * pvarValue
  318. )
  319. {
  320. ::VariantInit(pvarValue);
  321. CComPtr<IWbemServices> srpNamespace;
  322. HRESULT hr = GetNamespace(bstrNamespace, &srpNamespace);
  323. if (SUCCEEDED(hr))
  324. {
  325. CComPtr<IWbemClassObject> srpObj;
  326. hr = srpNamespace->GetObject(
  327. bstrObjectPath,
  328. 0,
  329. NULL,
  330. &srpObj,
  331. NULL
  332. );
  333. if (SUCCEEDED(hr))
  334. {
  335. hr = srpObj->Get(bstrPropName, 0, pvarValue, NULL, NULL);
  336. }
  337. }
  338. return hr;
  339. }