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.

865 lines
22 KiB

  1. // group.cpp: implementation of the CRGroups class.
  2. //
  3. // Copyright (c)1997-1999 Microsoft Corporation
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "group.h"
  8. #include "persistmgr.h"
  9. #include <io.h>
  10. #include "requestobject.h"
  11. /*
  12. Routine Description:
  13. Name:
  14. CRGroups::CRGroups
  15. Functionality:
  16. This is the constructor. Pass along the parameters to the base class
  17. Virtual:
  18. No (you know that, constructor won't be virtual!)
  19. Arguments:
  20. pKeyChain - Pointer to the ISceKeyChain COM interface which is prepared
  21. by the caller who constructs this instance.
  22. pNamespace - Pointer to WMI namespace of our provider (COM interface).
  23. Passed along by the caller. Must not be NULL.
  24. pCtx - Pointer to WMI context object (COM interface). Passed along
  25. by the caller. It's up to WMI whether this interface pointer is NULL or not.
  26. Return Value:
  27. None as any constructor
  28. Notes:
  29. if you create any local members, think about initialize them here
  30. */
  31. CRGroups::CRGroups (
  32. IN ISceKeyChain * pKeyChain,
  33. IN IWbemServices * pNamespace,
  34. IN IWbemContext * pCtx
  35. )
  36. :
  37. CGenericClass(pKeyChain, pNamespace, pCtx)
  38. {
  39. }
  40. /*
  41. Routine Description:
  42. Name:
  43. CRGroups::~CRGroups
  44. Functionality:
  45. Destructor. Necessary as good C++ discipline since we have virtual functions.
  46. Virtual:
  47. Yes.
  48. Arguments:
  49. none as any destructor
  50. Return Value:
  51. None as any destructor
  52. Notes:
  53. if you create any local members, think about whether
  54. there is any need for a non-trivial destructor
  55. */
  56. CRGroups::~CRGroups ()
  57. {
  58. }
  59. /*
  60. Routine Description:
  61. Name:
  62. CRGroups::CreateObject
  63. Functionality:
  64. Create WMI objects (Sce_RestrictedGroup). Depending on parameter atAction,
  65. this creation may mean:
  66. (a) Get a single instance (atAction == ACTIONTYPE_GET)
  67. (b) Get several instances satisfying some criteria (atAction == ACTIONTYPE_QUERY)
  68. (c) Delete an instance (atAction == ACTIONTYPE_DELETE)
  69. Virtual:
  70. Yes.
  71. Arguments:
  72. pHandler - COM interface pointer for notifying WMI for creation result.
  73. atAction - Get single instance ACTIONTYPE_GET
  74. Get several instances ACTIONTYPE_QUERY
  75. Delete a single instance ACTIONTYPE_DELETE
  76. Return Value:
  77. Success: it must return success code (use SUCCEEDED to test). It is
  78. not guaranteed to return WBEM_NO_ERROR. The returned objects are indicated to WMI,
  79. not directly passed back via parameters.
  80. Failure: Various errors may occurs. Except WBEM_E_NOT_FOUND, any such error should indicate
  81. the failure of getting the wanted instance. If WBEM_E_NOT_FOUND is returned in querying
  82. situations, this may not be an error depending on caller's intention.
  83. Notes:
  84. */
  85. HRESULT
  86. CRGroups::CreateObject (
  87. IN IWbemObjectSink * pHandler,
  88. IN ACTIONTYPE atAction
  89. )
  90. {
  91. //
  92. // we know how to:
  93. // Get single instance ACTIONTYPE_GET
  94. // Delete a single instance ACTIONTYPE_DELETE
  95. // Get several instances ACTIONTYPE_QUERY
  96. //
  97. if ( ACTIONTYPE_GET != atAction &&
  98. ACTIONTYPE_DELETE != atAction &&
  99. ACTIONTYPE_QUERY != atAction )
  100. {
  101. return WBEM_E_NOT_SUPPORTED;
  102. }
  103. //
  104. // We must have the pStorePath property because that is where
  105. // our instance is stored.
  106. // m_srpKeyChain->GetKeyPropertyValue WBEM_S_FALSE if the key is not recognized
  107. // So, we need to test against WBEM_S_FALSE if the property is mandatory
  108. //
  109. CComVariant varGroupName;
  110. CComVariant varStorePath;
  111. HRESULT hr = m_srpKeyChain->GetKeyPropertyValue(pStorePath, &varStorePath);
  112. if (SUCCEEDED(hr))
  113. {
  114. hr = m_srpKeyChain->GetKeyPropertyValue(pGroupName, &varGroupName);
  115. if (FAILED(hr))
  116. {
  117. return hr;
  118. }
  119. else if (hr == WBEM_S_FALSE && (ACTIONTYPE_QUERY != atAction) )
  120. {
  121. return WBEM_E_NOT_FOUND;
  122. }
  123. }
  124. else
  125. {
  126. return hr;
  127. }
  128. //
  129. // if we have a valid store path
  130. //
  131. if (varStorePath.vt == VT_BSTR)
  132. {
  133. //
  134. // create a store with that path
  135. //
  136. CSceStore SceStore;
  137. hr = SceStore.SetPersistPath(varStorePath.bstrVal);
  138. if ( SUCCEEDED(hr) )
  139. {
  140. //
  141. // make sure the store (just a file) really exists. The raw path
  142. // may contain env variables, so we need the expanded path
  143. //
  144. DWORD dwAttrib = GetFileAttributes(SceStore.GetExpandedPath());
  145. if ( dwAttrib != -1 )
  146. {
  147. //
  148. // Make sure that the store type matches
  149. //
  150. if ( SceStore.GetStoreType() < SCE_INF_FORMAT ||
  151. SceStore.GetStoreType() > SCE_JET_ANALYSIS_REQUIRED )
  152. {
  153. hr = WBEM_E_INVALID_PARAMETER;
  154. }
  155. //
  156. // everything is ready. We will do a delete or construct depending on the action type.
  157. //
  158. if ( SUCCEEDED(hr) )
  159. {
  160. if ( ACTIONTYPE_DELETE == atAction )
  161. {
  162. hr = DeleteInstance(pHandler, &SceStore, varGroupName.bstrVal);
  163. }
  164. else
  165. {
  166. BOOL bPostFilter=TRUE;
  167. DWORD dwCount = 0;
  168. m_srpKeyChain->GetKeyPropertyCount(&dwCount);
  169. if ( varGroupName.vt == VT_EMPTY && dwCount == 1 )
  170. {
  171. //
  172. // it's a get single instance
  173. //
  174. bPostFilter = FALSE;
  175. }
  176. hr = ConstructInstance(pHandler,
  177. &SceStore,
  178. varStorePath.bstrVal,
  179. (varGroupName.vt == VT_BSTR) ? varGroupName.bstrVal : NULL,
  180. bPostFilter
  181. );
  182. }
  183. }
  184. }
  185. else
  186. {
  187. hr = WBEM_E_NOT_FOUND;
  188. }
  189. }
  190. }
  191. return hr;
  192. }
  193. /*
  194. Routine Description:
  195. Name:
  196. CRGroups::PutInst
  197. Functionality:
  198. Put an instance as instructed by WMI. Since this class implements Sce_RestrictedGroup,
  199. which is persistence oriented, this will cause the Sce_RestrictedGroup object's property
  200. information to be saved in our store.
  201. Virtual:
  202. Yes.
  203. Arguments:
  204. pInst - COM interface pointer to the WMI class (Sce_RestrictedGroup) object.
  205. pHandler - COM interface pointer for notifying WMI of any events.
  206. pCtx - COM interface pointer. This interface is just something we pass around.
  207. WMI may mandate it (not now) in the future. But we never construct
  208. such an interface and so, we just pass around for various WMI API's
  209. Return Value:
  210. Success: it must return success code (use SUCCEEDED to test). It is
  211. not guaranteed to return WBEM_NO_ERROR.
  212. Failure: Various errors may occurs. Any such error should indicate the failure of persisting
  213. the instance.
  214. Notes:
  215. Since GetProperty will return a success code (WBEM_S_RESET_TO_DEFAULT) when the
  216. requested property is not present, don't simply use SUCCEEDED or FAILED macros
  217. to test for the result of retrieving a property.
  218. */
  219. HRESULT
  220. CRGroups::PutInst (
  221. IN IWbemClassObject * pInst,
  222. IN IWbemObjectSink * pHandler,
  223. IN IWbemContext * pCtx
  224. )
  225. {
  226. HRESULT hr = WBEM_E_INVALID_PARAMETER;
  227. CComBSTR bstrGroup;
  228. PSCE_NAME_LIST pnlAdd=NULL;
  229. DWORD mode;
  230. CSceStore SceStore;
  231. //
  232. // CScePropertyMgr helps us to access WMI object's properties
  233. // create an instance and attach the WMI object to it.
  234. // This will always succeed.
  235. //
  236. CScePropertyMgr ScePropMgr;
  237. ScePropMgr.Attach(pInst);
  238. //
  239. // the use of the macro SCE_PROV_IfErrorGotoCleanup cause
  240. // a "goto CleanUp;" with hr set to the return value from
  241. // the function (macro parameter)
  242. //
  243. // get group name, can't be NULL
  244. SCE_PROV_IfErrorGotoCleanup(ScePropMgr.GetProperty(pGroupName, &bstrGroup));
  245. if ( hr == WBEM_S_RESET_TO_DEFAULT)
  246. {
  247. hr = WBEM_E_ILLEGAL_NULL;
  248. goto CleanUp;
  249. }
  250. // get mode, must be defined
  251. SCE_PROV_IfErrorGotoCleanup(ScePropMgr.GetProperty(pMode, &mode));
  252. if ( hr == WBEM_S_RESET_TO_DEFAULT)
  253. {
  254. hr = WBEM_E_ILLEGAL_NULL;
  255. goto CleanUp;
  256. }
  257. //
  258. // get AddList
  259. //
  260. SCE_PROV_IfErrorGotoCleanup(ScePropMgr.GetProperty(pAddList, &pnlAdd));
  261. //
  262. // ignore RemoveList for now (since we don't support the mode)
  263. //
  264. //
  265. // Attach the WMI object instance to the store and let the store know that
  266. // it's store is given by the pStorePath property of the instance.
  267. //
  268. SCE_PROV_IfErrorGotoCleanup(SceStore.SetPersistProperties(pInst, pStorePath));
  269. //
  270. // now save the info to file
  271. //
  272. hr = SaveSettingsToStore(&SceStore,
  273. bstrGroup,
  274. mode,
  275. pnlAdd,
  276. NULL
  277. );
  278. CleanUp:
  279. if ( pnlAdd )
  280. {
  281. SceFreeMemory(pnlAdd, SCE_STRUCT_NAME_LIST);
  282. }
  283. return hr;
  284. }
  285. /*
  286. Routine Description:
  287. Name:
  288. CRGroups::ConstructInstance
  289. Functionality:
  290. This is private function to create an instance of Sce_RestrictedGroup.
  291. Virtual:
  292. No.
  293. Arguments:
  294. pHandler - COM interface pointer for notifying WMI of any events.
  295. pSceStore - Pointer to our store. It must have been appropriately set up.
  296. wszLogStorePath - store path, a key property of Sce_RestrictedGroup class.
  297. wszGroupName - a corresponding key property of Sce_RestrictedGroup class.
  298. bPostFilter - Controls how WMI will be informed with pHandler->SetStatus.
  299. Return Value:
  300. Success: it must return success code (use SUCCEEDED to test). It is
  301. not guaranteed to return WBEM_NO_ERROR.
  302. Failure: Various errors may occurs. Any such error should indicate the creating the instance.
  303. Notes:
  304. */
  305. HRESULT
  306. CRGroups::ConstructInstance (
  307. IN IWbemObjectSink * pHandler,
  308. IN CSceStore * pSceStore,
  309. IN LPCWSTR wszLogStorePath,
  310. IN LPCWSTR wszGroupName OPTIONAL,
  311. IN BOOL bPostFilter
  312. )
  313. {
  314. if (pSceStore == NULL)
  315. {
  316. return WBEM_E_INVALID_PARAMETER;
  317. }
  318. //
  319. // ask SCE to read a gigantic structure out from the store. Only SCE
  320. // knows now to release the memory. Don't just delete it! Use our CSceStore
  321. // to do the releasing (FreeSecurityProfileInfo)
  322. //
  323. PSCE_PROFILE_INFO pInfo=NULL;
  324. HRESULT hr = pSceStore->GetSecurityProfileInfo(
  325. AREA_GROUP_MEMBERSHIP,
  326. &pInfo,
  327. NULL
  328. );
  329. if (SUCCEEDED(hr))
  330. {
  331. //
  332. // we have to search for the user right name in the returned list
  333. //
  334. PSCE_GROUP_MEMBERSHIP pGroups = pInfo->pGroupMembership;
  335. if (wszGroupName)
  336. {
  337. //
  338. // for all the groups, copy the group names
  339. //
  340. while ( pGroups)
  341. {
  342. if ( pGroups->GroupName == NULL )
  343. {
  344. continue;
  345. }
  346. if ( _wcsicmp(pGroups->GroupName, wszGroupName)== 0 )
  347. {
  348. break;
  349. }
  350. pGroups = pGroups->Next;
  351. }
  352. }
  353. PSCE_GROUP_MEMBERSHIP pTmpGrp = pGroups;
  354. //
  355. // if the Group information buffer is empty, treat it as "not found"
  356. //
  357. if ( pGroups == NULL )
  358. {
  359. hr = WBEM_E_NOT_FOUND;
  360. goto CleanUp;
  361. }
  362. //
  363. // the use of the macro SCE_PROV_IfErrorGotoCleanup cause
  364. // a "goto CleanUp;" with hr set to the return value from
  365. // the function (macro parameter)
  366. //
  367. CComBSTR bstrLogOut;
  368. SCE_PROV_IfErrorGotoCleanup(MakeSingleBackSlashPath(wszLogStorePath, L'\\', &bstrLogOut));
  369. //
  370. // CScePropertyMgr helps us to access WMI object's properties.
  371. //
  372. CScePropertyMgr ScePropMgr;
  373. for ( ; pTmpGrp != NULL; pTmpGrp = pTmpGrp->Next )
  374. {
  375. CComPtr<IWbemClassObject> srpObj;
  376. SCE_PROV_IfErrorGotoCleanup(SpawnAnInstance(&srpObj));
  377. //
  378. // attach a different WMI object to the property mgr.
  379. // This will always succeed.
  380. //
  381. ScePropMgr.Attach(srpObj);
  382. SCE_PROV_IfErrorGotoCleanup(ScePropMgr.PutProperty(pStorePath, bstrLogOut));
  383. SCE_PROV_IfErrorGotoCleanup(ScePropMgr.PutProperty(pGroupName, pTmpGrp->GroupName));
  384. //
  385. // hardcode the mode for now
  386. //
  387. DWORD mode = 1;
  388. SCE_PROV_IfErrorGotoCleanup(ScePropMgr.PutProperty(pMode, mode));
  389. if ( pTmpGrp->pMembers )
  390. {
  391. SCE_PROV_IfErrorGotoCleanup(ScePropMgr.PutProperty(pAddList, pTmpGrp->pMembers));
  392. }
  393. //
  394. // ignore RemoveList for now
  395. //
  396. //
  397. // do the necessary gestures to WMI.
  398. // the use of WBEM_STATUS_REQUIREMENTS in SetStatus is not documented by WMI
  399. // at this point. Consult WMI team for detail if you suspect problems with
  400. // the use of WBEM_STATUS_REQUIREMENTS
  401. //
  402. if ( !bPostFilter ) {
  403. pHandler->SetStatus(WBEM_STATUS_REQUIREMENTS, S_FALSE, NULL, NULL);
  404. } else {
  405. pHandler->SetStatus(WBEM_STATUS_REQUIREMENTS, S_OK, NULL, NULL);
  406. }
  407. //
  408. // pass the new instance to WMI
  409. //
  410. hr = pHandler->Indicate(1, &srpObj);
  411. if ( wszGroupName )
  412. {
  413. //
  414. // get single instance only
  415. //
  416. break;
  417. }
  418. }
  419. }
  420. CleanUp:
  421. pSceStore->FreeSecurityProfileInfo(pInfo);
  422. return hr;
  423. }
  424. /*
  425. Routine Description:
  426. Name:
  427. CRGroups::DeleteInstance
  428. Functionality:
  429. remove an instance of Sce_RestrictedGroup from the specified store.
  430. Virtual:
  431. No.
  432. Arguments:
  433. pHandler - COM interface pointer for notifying WMI of any events.
  434. pSceStore - Pointer to our store. It must have been appropriately set up.
  435. wszGroupName - property of the Sce_RestrictedGroup class.
  436. Return Value:
  437. Success: WBEM_NO_ERROR.
  438. Failure: WBEM_E_INVALID_PARAMETER.
  439. Notes:
  440. */
  441. HRESULT
  442. CRGroups::DeleteInstance (
  443. IN IWbemObjectSink *pHandler,
  444. IN CSceStore* pSceStore,
  445. IN LPCWSTR wszGroupName
  446. )
  447. {
  448. if (pSceStore == NULL)
  449. {
  450. return WBEM_E_INVALID_PARAMETER;
  451. }
  452. pSceStore->DeleteSectionFromStore(wszGroupName);
  453. return WBEM_NO_ERROR;
  454. }
  455. /*
  456. Routine Description:
  457. Name:
  458. CRGroups::SaveSettingsToStore
  459. Functionality:
  460. With all the properties of a Sce_RestrictedGroup, this function just saves
  461. the instance properties to our store.
  462. Virtual:
  463. No.
  464. Arguments:
  465. pSceStore - the store.
  466. wszGroupName - a corresponding key property of Sce_RestrictedGroup class.
  467. mode - another corresponding property of the Sce_RestrictedGroup class.
  468. pnlAdd - another corresponding property of the Sce_RestrictedGroup class.
  469. pnlRemove - another corresponding property of the Sce_RestrictedGroup class.
  470. Return Value:
  471. Success: it must return success code (use SUCCEEDED to test). It is
  472. not guaranteed to return WBEM_NO_ERROR.
  473. Failure: Various errors may occurs. Any error indicates the failure to save the instance.
  474. Notes:
  475. */
  476. HRESULT
  477. CRGroups::SaveSettingsToStore (
  478. IN CSceStore * pSceStore,
  479. IN LPCWSTR wszGroupName,
  480. IN DWORD mode,
  481. IN PSCE_NAME_LIST pnlAdd,
  482. IN PSCE_NAME_LIST pnlRemove
  483. )
  484. {
  485. DWORD dwDump;
  486. //
  487. // For a new .inf file. Write an empty buffer to the file
  488. // will creates the file with right header/signature/unicode format
  489. // this is harmless for existing files.
  490. // For database store, this is a no-op.
  491. //
  492. HRESULT hr = pSceStore->WriteSecurityProfileInfo(
  493. AreaBogus,
  494. (PSCE_PROFILE_INFO)&dwDump,
  495. NULL,
  496. false
  497. );
  498. if (FAILED(hr))
  499. {
  500. return hr;
  501. }
  502. //
  503. // ask SCE to read a gigantic structure out from the store. Only SCE
  504. // knows now to release the memory. Don't just delete it! Use our CSceStore
  505. // to do the releasing (FreeSecurityProfileInfo)
  506. //
  507. PSCE_PROFILE_INFO pInfo=NULL;
  508. hr = pSceStore->GetSecurityProfileInfo(
  509. AREA_GROUP_MEMBERSHIP,
  510. &pInfo,
  511. NULL
  512. );
  513. if ( SUCCEEDED(hr) )
  514. {
  515. //
  516. // for INF format, we have to search for the servic name in the returned array
  517. //
  518. PSCE_GROUP_MEMBERSHIP pGroups= pInfo->pGroupMembership;
  519. PSCE_GROUP_MEMBERSHIP pParent=NULL;
  520. DWORD i=0;
  521. while ( pGroups )
  522. {
  523. if ( pGroups->GroupName == NULL )
  524. {
  525. continue;
  526. }
  527. if ( _wcsicmp(pGroups->GroupName, wszGroupName)== 0 )
  528. {
  529. break;
  530. }
  531. pParent = pGroups;
  532. pGroups = pGroups->Next;
  533. }
  534. if ( pGroups )
  535. {
  536. //
  537. // find it
  538. //
  539. if ( mode == SCE_NO_VALUE )
  540. {
  541. //
  542. // delete it
  543. //
  544. if ( pParent )
  545. {
  546. pParent->Next = pGroups->Next;
  547. }
  548. else
  549. {
  550. pInfo->pGroupMembership = pGroups->Next;
  551. }
  552. //
  553. // the following worries me: where do we get the knowledge that we need to free this memory?
  554. // free buffer
  555. //
  556. pGroups->Next = NULL;
  557. SceFreeMemory(pGroups, SCE_STRUCT_GROUP);
  558. }
  559. else
  560. {
  561. //
  562. // modify it
  563. //
  564. //
  565. // the following worries me: where do we get the knowledge that we need to free this memory?
  566. //
  567. if ( pGroups->pMembers )
  568. {
  569. SceFreeMemory(pGroups->pMembers, SCE_STRUCT_NAME_LIST);
  570. }
  571. pGroups->pMembers = pnlAdd;
  572. }
  573. if ( SUCCEEDED(hr) )
  574. {
  575. hr = pSceStore->WriteSecurityProfileInfo(
  576. AREA_GROUP_MEMBERSHIP,
  577. pInfo,
  578. NULL,
  579. false
  580. );
  581. }
  582. if ( mode != SCE_NO_VALUE )
  583. {
  584. //
  585. // reset the buffer
  586. //
  587. pGroups->pMembers = NULL;
  588. }
  589. }
  590. else
  591. {
  592. //
  593. // not found
  594. //
  595. if ( mode == SCE_NO_VALUE )
  596. {
  597. //
  598. // try to delete non exist object
  599. //
  600. hr = WBEM_E_NOT_FOUND;
  601. }
  602. else
  603. {
  604. //
  605. // add this one in
  606. //
  607. SCE_GROUP_MEMBERSHIP addGroup;
  608. addGroup.GroupName = (PWSTR)wszGroupName;
  609. addGroup.pMembers = pnlAdd;
  610. addGroup.pMemberOf = NULL;
  611. addGroup.pPrivilegesHeld = NULL;
  612. addGroup.Status = 0;
  613. addGroup.Next = NULL;
  614. //
  615. // set the temp buffer pointer to pInfo to set to the store
  616. //
  617. pGroups = pInfo->pGroupMembership;
  618. pInfo->pGroupMembership = &addGroup;
  619. //
  620. // append this item to the section
  621. //
  622. hr = pSceStore->WriteSecurityProfileInfo(
  623. AREA_GROUP_MEMBERSHIP,
  624. pInfo,
  625. NULL,
  626. true
  627. );
  628. //
  629. // reset the buffer pointer
  630. //
  631. pInfo->pGroupMembership = pGroups;
  632. }
  633. }
  634. }
  635. if (pInfo != NULL)
  636. {
  637. pSceStore->FreeSecurityProfileInfo(pInfo);
  638. }
  639. return hr;
  640. }