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.

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