Source code of Windows XP (NT5)
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.

592 lines
17 KiB

  1. //*************************************************************
  2. //
  3. // Resultant set of policy
  4. //
  5. // Microsoft Confidential
  6. // Copyright (c) Microsoft Corporation 1995
  7. // All rights reserved
  8. //
  9. // History: 15-July-2000 NishadM Created
  10. //
  11. //*************************************************************
  12. #include "uenv.h"
  13. #include <wbemcli.h>
  14. #include "SmartPtr.h"
  15. #include "WbemTime.h"
  16. //*************************************************************
  17. //
  18. // RsopSetPolicySettingStatus
  19. //
  20. // Purpose: Creates create instances of RSOP_PolicySettingStatus and
  21. // links them to RSOP_PolicySetting
  22. //
  23. // Parameters:
  24. // dwFlags - flags
  25. // pServices - namespace
  26. // pSettingInstance - instance of RSOP_PolicySetting
  27. // nLinks - number of setting statuses
  28. // pStatus - setting status information
  29. //
  30. // Returns: S_OK if successful
  31. //
  32. //*************************************************************
  33. HRESULT
  34. RsopSetPolicySettingStatus( DWORD,
  35. IWbemServices* pServices,
  36. IWbemClassObject* pSettingInstance,
  37. DWORD nLinks,
  38. POLICYSETTINGSTATUSINFO* pStatus )
  39. {
  40. HRESULT hr;
  41. //
  42. // validate arguments
  43. //
  44. if ( !pServices || !pSettingInstance || !nLinks || !pStatus )
  45. {
  46. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: invalid arguments" ) );
  47. return E_INVALIDARG;
  48. }
  49. //
  50. // get the RSoP_PolicySettingStatus class
  51. //
  52. XBStr bstr = L"RSoP_PolicySettingStatus";
  53. XInterface<IWbemClassObject> xClassStatus;
  54. hr = pServices->GetObject( bstr,
  55. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  56. 0,
  57. &xClassStatus,
  58. 0 );
  59. if ( FAILED( hr ) )
  60. {
  61. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: GetObject failed, 0x%x", hr ) );
  62. return hr;
  63. }
  64. //
  65. // spawn the RSoP_PolicySettingStatus instance
  66. //
  67. XInterface<IWbemClassObject> xInstStatus;
  68. hr = xClassStatus->SpawnInstance( 0, &xInstStatus );
  69. if ( FAILED (hr) )
  70. {
  71. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: SpawnInstance failed, 0x%x", hr ) );
  72. return hr;
  73. }
  74. //
  75. // get the RSoP_PolicySettingLink class
  76. //
  77. XInterface<IWbemClassObject> xClassLink;
  78. bstr = L"RSoP_PolicySettingLink";
  79. hr = pServices->GetObject( bstr,
  80. WBEM_FLAG_RETURN_WBEM_COMPLETE,
  81. 0,
  82. &xClassLink,
  83. 0 );
  84. if ( FAILED( hr ) )
  85. {
  86. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: GetObject failed, 0x%x", hr ) );
  87. return hr;
  88. }
  89. //
  90. // spawn the RSoP_PolicySettingLink class
  91. //
  92. XInterface<IWbemClassObject> xInstLink;
  93. hr = xClassLink->SpawnInstance( 0, &xInstLink );
  94. if ( FAILED (hr) )
  95. {
  96. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: SpawnInstance failed, 0x%x", hr ) );
  97. return hr;
  98. }
  99. //
  100. // get the class name of the instance
  101. //
  102. VARIANT varClass;
  103. VariantInit( &varClass );
  104. XVariant xVarClass(&varClass);
  105. bstr = L"__CLASS";
  106. hr = pSettingInstance->Get( bstr,
  107. 0,
  108. xVarClass,
  109. 0,
  110. 0 );
  111. if ( FAILED (hr) )
  112. {
  113. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: Get __CLASS failed, 0x%x", hr ) );
  114. return hr;
  115. }
  116. if ( varClass.vt == VT_NULL || varClass.vt == VT_EMPTY )
  117. {
  118. return E_UNEXPECTED;
  119. }
  120. //
  121. // get the [key] of the RSoP_PolicySetting instance
  122. //
  123. VARIANT varId;
  124. VariantInit( &varId );
  125. XVariant xVarId(&varId);
  126. XBStr bstrid = L"id";
  127. XBStr bstrPath = L"__RELPATH";
  128. hr = pSettingInstance->Get( bstrPath,
  129. 0,
  130. xVarId,
  131. 0,
  132. 0 );
  133. if ( FAILED (hr) )
  134. {
  135. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: Get id failed, 0x%x", hr ) );
  136. return hr;
  137. }
  138. LPWSTR szSetting = varId.bstrVal;
  139. //
  140. // set the setting value
  141. //
  142. VARIANT var;
  143. XBStr bstrVal;
  144. XBStr bstrsetting = L"setting";
  145. XBStr bstreventSource = L"eventSource";
  146. XBStr bstreventLogName = L"eventLogName";
  147. XBStr bstreventID = L"eventID";
  148. XBStr bstreventTime = L"eventTime";
  149. XBStr bstrstatus = L"status";
  150. XBStr bstrerrorCode = L"errorCode";
  151. //
  152. // for each info.
  153. //
  154. for ( DWORD i = 0 ; i < nLinks ; i++ )
  155. {
  156. //
  157. // RSoP_PolicySettingStatus
  158. //
  159. WCHAR szGuid[64];
  160. LPWSTR szPolicyStatusKey;
  161. if ( !pStatus[i].szKey )
  162. {
  163. //
  164. // caller did not specify a key. generate it.
  165. //
  166. GUID guid;
  167. //
  168. // create the [key]
  169. //
  170. hr = CoCreateGuid( &guid );
  171. if ( FAILED(hr) )
  172. {
  173. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: CoCreateGuid failed, 0x%x", hr ) );
  174. return hr;
  175. }
  176. wsprintf( szGuid,
  177. L"{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  178. guid.Data1,
  179. guid.Data2,
  180. guid.Data3,
  181. guid.Data4[0], guid.Data4[1],
  182. guid.Data4[2], guid.Data4[3],
  183. guid.Data4[4], guid.Data4[5],
  184. guid.Data4[6], guid.Data4[7] );
  185. bstrVal = szPolicyStatusKey = szGuid;
  186. }
  187. else
  188. {
  189. //
  190. // caller specified a key. use it.
  191. //
  192. bstrVal = szPolicyStatusKey = pStatus[i].szKey;
  193. }
  194. var.vt = VT_BSTR;
  195. var.bstrVal = bstrVal;
  196. //
  197. // set the id
  198. //
  199. hr = xInstStatus->Put( bstrid,
  200. 0,
  201. &var,
  202. 0 );
  203. if ( FAILED (hr) )
  204. {
  205. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: Put id failed, 0x%x", hr ) );
  206. return hr;
  207. }
  208. //
  209. // set the eventSource
  210. //
  211. if ( pStatus[i].szEventSource )
  212. {
  213. var.vt = VT_BSTR;
  214. bstrVal = pStatus[i].szEventSource;
  215. var.bstrVal = bstrVal;
  216. hr = xInstStatus->Put( bstreventSource,
  217. 0,
  218. &var,
  219. 0 );
  220. if ( FAILED (hr) )
  221. {
  222. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: Put eventSource failed, 0x%x", hr ) );
  223. return hr;
  224. }
  225. }
  226. //
  227. // set the eventLogName
  228. //
  229. if ( pStatus[i].szEventLogName )
  230. {
  231. var.vt = VT_BSTR;
  232. bstrVal = pStatus[i].szEventLogName;
  233. var.bstrVal = bstrVal;
  234. hr = xInstStatus->Put( bstreventLogName,
  235. 0,
  236. &var,
  237. 0 );
  238. if ( FAILED (hr) )
  239. {
  240. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: Put eventLogName failed, 0x%x", hr ) );
  241. return hr;
  242. }
  243. }
  244. //
  245. // set the eventID
  246. //
  247. var.vt = VT_I4;
  248. var.lVal = pStatus[i].dwEventID;
  249. hr = xInstStatus->Put( bstreventID,
  250. 0,
  251. &var,
  252. 0 );
  253. if ( FAILED (hr) )
  254. {
  255. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: Put eventID failed, 0x%x", hr ) );
  256. return hr;
  257. }
  258. XBStr xTimeLogged;
  259. //
  260. // convert SYSTEMTIME to WBEM time
  261. //
  262. hr = SystemTimeToWbemTime( pStatus[i].timeLogged, xTimeLogged );
  263. if ( FAILED (hr) )
  264. {
  265. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: SystemTimeToWbemTime failed, 0x%x", hr ) );
  266. return hr;
  267. }
  268. //
  269. // set the eventTime
  270. //
  271. var.vt = VT_BSTR;
  272. var.bstrVal = xTimeLogged;
  273. hr = xInstStatus->Put( bstreventTime,
  274. 0,
  275. &var,
  276. 0 );
  277. if ( FAILED (hr) )
  278. {
  279. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: Put eventTime failed, 0x%x", hr ) );
  280. return hr;
  281. }
  282. //
  283. // set the errorCode
  284. //
  285. var.vt = VT_I4;
  286. var.lVal = pStatus[i].dwErrorCode;
  287. hr = xInstStatus->Put( bstrerrorCode,
  288. 0,
  289. &var,
  290. 0 );
  291. if ( FAILED (hr) )
  292. {
  293. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: Put errorCode failed, 0x%x", hr ) );
  294. return hr;
  295. }
  296. //
  297. // set the status
  298. //
  299. var.vt = VT_I4;
  300. var.lVal = pStatus[i].status;
  301. hr = xInstStatus->Put( bstrstatus,
  302. 0,
  303. &var,
  304. 0 );
  305. if ( FAILED (hr) )
  306. {
  307. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: Put status failed, 0x%x", hr ) );
  308. return hr;
  309. }
  310. //
  311. // commit the RSOP_PolicySettingStatus
  312. //
  313. hr = pServices->PutInstance(xInstStatus,
  314. WBEM_FLAG_CREATE_OR_UPDATE,
  315. 0,
  316. 0 );
  317. if ( FAILED (hr) )
  318. {
  319. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: PutInstance RSOP_PolicySettingStatus failed, 0x%x", hr ) );
  320. return hr;
  321. }
  322. //
  323. // RSoP_PolicySettingLink
  324. //
  325. var.vt = VT_BSTR;
  326. bstrVal = szSetting;
  327. var.bstrVal = bstrVal;
  328. hr = xInstLink->Put(bstrsetting,
  329. 0,
  330. &var,
  331. 0);
  332. if ( FAILED (hr) )
  333. {
  334. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: Put setting failed, 0x%x", hr ) );
  335. return hr;
  336. }
  337. //
  338. // create the second key
  339. //
  340. WCHAR szStatus[96];
  341. //
  342. // e.g. RSoP_PolicySettingStatus.id="{00000000-0000-0000-000000000000}"
  343. //
  344. wsprintf( szStatus, L"RSoP_PolicySettingStatus.id=\"%s\"", szPolicyStatusKey );
  345. //
  346. // set the status
  347. //
  348. bstrVal = szStatus;
  349. var.bstrVal = bstrVal;
  350. hr = xInstLink->Put(bstrstatus,
  351. 0,
  352. &var,
  353. 0);
  354. if ( FAILED (hr) )
  355. {
  356. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: Put status failed, 0x%x", hr ) );
  357. return hr;
  358. }
  359. //
  360. // commit the RSOP_PolicySettingLink
  361. //
  362. hr = pServices->PutInstance(xInstLink,
  363. WBEM_FLAG_CREATE_OR_UPDATE,
  364. 0,
  365. 0 );
  366. if ( FAILED (hr) )
  367. {
  368. DebugMsg( ( DM_WARNING, L"SetPolicySettingStatus: PutInstance RSOP_PolicySettingLink failed, 0x%x", hr ) );
  369. return hr;
  370. }
  371. }
  372. return hr;
  373. }
  374. //*************************************************************
  375. //
  376. // RsopResetPolicySettingStatus
  377. //
  378. // Purpose: Creates create instances of RSOP_PolicySettingStatus and
  379. // links them to RSOP_PolicySetting
  380. //
  381. // Parameters:
  382. // dwFlags - flags
  383. // pServices - namespace
  384. // pSettingInstance - instance of RSOP_PolicySetting
  385. //
  386. // Returns: S_OK if successful
  387. //
  388. //*************************************************************
  389. #define RESET_QUERY_TEMPLATE L"REFERENCES OF {%s} WHERE ResultClass = RSOP_PolicySettingLink"
  390. HRESULT
  391. RsopResetPolicySettingStatus( DWORD,
  392. IWbemServices* pServices,
  393. IWbemClassObject* pSettingInstance )
  394. {
  395. // query for the RSOP_PolicySettingLink
  396. // for every RSOP_PolicySettingLink query for the RSoP_PolicySettingLink.status
  397. // delete the RSoP_PolicySettingStatus
  398. // delete the RSOP_PolicySettingLink
  399. if ( !pServices || !pSettingInstance )
  400. {
  401. return E_INVALIDARG;
  402. }
  403. //
  404. // get the [key] of the RSoP_PolicySetting instance
  405. //
  406. VARIANT varId;
  407. VariantInit( &varId );
  408. XVariant xVarId(&varId);
  409. XBStr bstrRelPath = L"__RELPATH";
  410. HRESULT hr;
  411. hr = pSettingInstance->Get( bstrRelPath,
  412. 0,
  413. xVarId,
  414. 0,
  415. 0 );
  416. if ( FAILED (hr) )
  417. {
  418. DebugMsg( ( DM_WARNING, L"RsopResetPolicySettingStatus: Get __RELPATH failed, 0x%x", hr ) );
  419. return hr;
  420. }
  421. if ( varId.vt == VT_NULL || varId.vt == VT_EMPTY )
  422. {
  423. return E_UNEXPECTED;
  424. }
  425. DWORD dwIdSize = wcslen(varId.bstrVal);
  426. //
  427. // Create a query for all references of the object
  428. //
  429. //
  430. // Query template
  431. //
  432. XPtrLF<WCHAR> szQuery = LocalAlloc( LPTR, sizeof(WCHAR) * ( dwIdSize ) + sizeof(RESET_QUERY_TEMPLATE) );
  433. if ( !szQuery )
  434. {
  435. DebugMsg( ( DM_WARNING, L"RsopResetPolicySettingStatus: LocalAlloc failed, 0x%x", GetLastError() ) );
  436. return E_OUTOFMEMORY;
  437. }
  438. wsprintf( szQuery, RESET_QUERY_TEMPLATE, varId.bstrVal );
  439. XBStr bstrLanguage = L"WQL";
  440. XBStr bstrQuery = szQuery;
  441. XInterface<IEnumWbemClassObject> pEnum;
  442. XBStr bstrPath = L"__PATH";
  443. XBStr bstrStatus = L"status";
  444. //
  445. // search for RSOP_ExtensionEventSourceLink
  446. //
  447. hr = pServices->ExecQuery( bstrLanguage,
  448. bstrQuery,
  449. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_ENSURE_LOCATABLE | WBEM_FLAG_RETURN_IMMEDIATELY,
  450. 0,
  451. &pEnum );
  452. if ( SUCCEEDED( hr ) )
  453. {
  454. DWORD dwReturned;
  455. do
  456. {
  457. XInterface<IWbemClassObject> xInst;
  458. dwReturned = 0;
  459. hr = pEnum->Next( WBEM_INFINITE,
  460. 1,
  461. &xInst,
  462. &dwReturned );
  463. if ( SUCCEEDED( hr ) && dwReturned == 1 )
  464. {
  465. //
  466. // delete RSoP_PolicySettingStatus
  467. //
  468. VARIANT varStatus;
  469. VariantInit( &varStatus );
  470. XVariant xVarStatus( &varStatus );
  471. hr = xInst->Get(bstrStatus,
  472. 0,
  473. &varStatus,
  474. 0,
  475. 0 );
  476. if ( SUCCEEDED( hr ) )
  477. {
  478. hr = pServices->DeleteInstance( varStatus.bstrVal,
  479. 0L,
  480. 0,
  481. 0 );
  482. if ( SUCCEEDED( hr ) )
  483. {
  484. //
  485. // delete RSoP_PolicySettingLink
  486. //
  487. VARIANT varLink;
  488. VariantInit( &varLink );
  489. hr = xInst->Get(bstrPath,
  490. 0L,
  491. &varLink,
  492. 0,
  493. 0 );
  494. if ( SUCCEEDED(hr) )
  495. {
  496. XVariant xVarLink( &varLink );
  497. hr = pServices->DeleteInstance( varLink.bstrVal,
  498. 0L,
  499. 0,
  500. 0 );
  501. if ( FAILED( hr ) )
  502. {
  503. return hr;
  504. }
  505. }
  506. }
  507. }
  508. }
  509. } while ( SUCCEEDED( hr ) && dwReturned == 1 );
  510. }
  511. if ( hr == S_FALSE )
  512. {
  513. hr = S_OK;
  514. }
  515. return hr;
  516. }