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.

671 lines
16 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. root.cpp
  7. Root node information (the root node is not displayed
  8. in the MMC framework but contains information such as
  9. all of the subnodes in this snapin).
  10. FILE HISTORY:
  11. */
  12. #include "stdafx.h"
  13. #include "util.h"
  14. #include "root.h"
  15. #include "reg.h"
  16. /*---------------------------------------------------------------------------
  17. RootHandler implementation
  18. ---------------------------------------------------------------------------*/
  19. IMPLEMENT_ADDREF_RELEASE(RootHandler)
  20. DEBUG_DECLARE_INSTANCE_COUNTER(RootHandler)
  21. HRESULT RootHandler::QueryInterface(REFIID riid, LPVOID *ppv)
  22. {
  23. // Is the pointer bad?
  24. if (ppv == NULL)
  25. return E_INVALIDARG;
  26. // Place NULL in *ppv in case of failure
  27. *ppv = NULL;
  28. // This is the non-delegating IUnknown implementation
  29. if (riid == IID_IUnknown)
  30. *ppv = (LPVOID) this;
  31. else if (riid == IID_IPersistStreamInit)
  32. *ppv = (IPersistStreamInit *) this;
  33. // If we're going to return an interface, AddRef it first
  34. if (*ppv)
  35. {
  36. ((LPUNKNOWN) *ppv)->AddRef();
  37. return hrOK;
  38. }
  39. else
  40. return BaseRouterHandler::QueryInterface(riid, ppv);
  41. }
  42. RootHandler::RootHandler(ITFSComponentData *pCompData)
  43. : BaseRouterHandler(pCompData)
  44. {
  45. m_spTFSCompData.Set(pCompData);
  46. DEBUG_INCREMENT_INSTANCE_COUNTER(RootHandler)
  47. }
  48. HRESULT RootHandler::Init()
  49. {
  50. return hrOK;
  51. }
  52. /*!--------------------------------------------------------------------------
  53. RootHandler::ConstructNode
  54. Initializes the root node (sets it up).
  55. Author: KennT
  56. ---------------------------------------------------------------------------*/
  57. HRESULT RootHandler::ConstructNode(ITFSNode *pNode)
  58. {
  59. HRESULT hr = hrOK;
  60. if (pNode == NULL)
  61. return hrOK;
  62. COM_PROTECT_TRY
  63. {
  64. // Need to initialize the data for the root node
  65. pNode->SetData(TFS_DATA_IMAGEINDEX, IMAGE_IDX_FOLDER_CLOSED);
  66. pNode->SetData(TFS_DATA_OPENIMAGEINDEX, IMAGE_IDX_FOLDER_OPEN);
  67. pNode->SetData(TFS_DATA_SCOPEID, 0);
  68. pNode->SetData(TFS_DATA_COOKIE, 0);
  69. }
  70. COM_PROTECT_CATCH
  71. return hr;
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////
  74. //// IPersistStream interface members
  75. STDMETHODIMP RootHandler::GetClassID
  76. (
  77. CLSID *pClassID
  78. )
  79. {
  80. ASSERT(pClassID != NULL);
  81. // Copy the CLSID for this snapin
  82. *pClassID = CLSID_IPXAdminExtension;
  83. return hrOK;
  84. }
  85. STDMETHODIMP RootHandler::IsDirty()
  86. {
  87. SPITFSNode spNode;
  88. m_spTFSCompData->GetRootNode(&spNode);
  89. return (spNode->GetData(TFS_DATA_DIRTY) || GetConfigStream()->GetDirty()) ? hrOK : hrFalse;
  90. }
  91. STDMETHODIMP RootHandler::Load
  92. (
  93. IStream *pStm
  94. )
  95. {
  96. Assert(pStm);
  97. HRESULT hr = hrOK;
  98. CString st;
  99. BOOL fServer;
  100. COM_PROTECT_TRY
  101. {
  102. hr = GetConfigStream()->LoadFrom(pStm);
  103. }
  104. COM_PROTECT_CATCH;
  105. return hr;
  106. }
  107. STDMETHODIMP RootHandler::Save
  108. (
  109. IStream *pStm,
  110. BOOL fClearDirty
  111. )
  112. {
  113. HRESULT hr = S_OK;
  114. SPITFSNode spNode;
  115. Assert(pStm);
  116. COM_PROTECT_TRY
  117. {
  118. if (fClearDirty)
  119. {
  120. m_spTFSCompData->GetRootNode(&spNode);
  121. spNode->SetData(TFS_DATA_DIRTY, FALSE);
  122. }
  123. hr = GetConfigStream()->SaveTo(pStm);
  124. }
  125. COM_PROTECT_CATCH;
  126. return hr;
  127. }
  128. STDMETHODIMP RootHandler::GetSizeMax
  129. (
  130. ULARGE_INTEGER *pcbSize
  131. )
  132. {
  133. ULONG cbSize;
  134. HRESULT hr = hrOK;
  135. COM_PROTECT_TRY
  136. {
  137. hr = GetConfigStream()->GetSize(&cbSize);
  138. if (FHrSucceeded(hr))
  139. {
  140. pcbSize->HighPart = 0;
  141. pcbSize->LowPart = cbSize;
  142. }
  143. }
  144. COM_PROTECT_CATCH;
  145. return hr;
  146. }
  147. STDMETHODIMP RootHandler::InitNew()
  148. {
  149. HRESULT hr = hrOK;
  150. COM_PROTECT_TRY
  151. {
  152. hr = GetConfigStream()->InitNew();
  153. }
  154. COM_PROTECT_CATCH;
  155. return hr;
  156. }
  157. // for RtrMgrInfo access
  158. HRESULT RootHandler::AddRtrObj(LONG_PTR ulConnId, REFIID riid, IUnknown * pRtrObj)
  159. {
  160. HRESULT hr = hrOK;
  161. RtrObjRecord rtrObj;
  162. COM_PROTECT_TRY
  163. {
  164. if (m_mapRtrObj.Lookup(ulConnId, rtrObj))
  165. {
  166. // connection id already in the list.
  167. Trace1("RootHandler::AddRtrObj - %lx already in the list!", ulConnId);
  168. return E_INVALIDARG;
  169. }
  170. rtrObj.m_riid = riid;
  171. rtrObj.m_spUnk.Set(pRtrObj);
  172. m_mapRtrObj.SetAt(ulConnId, rtrObj);
  173. }
  174. COM_PROTECT_CATCH
  175. return hr;
  176. }
  177. HRESULT RootHandler::RemoveRtrObj(LONG_PTR ulConnId)
  178. {
  179. HRESULT hr = hrOK;
  180. COM_PROTECT_TRY
  181. {
  182. if (m_mapRtrObj.RemoveKey(ulConnId) == 0)
  183. {
  184. // element not in the list
  185. Trace1("RootHandler::RemoveRtrObj - %lx not in the list!", ulConnId);
  186. return E_INVALIDARG;
  187. }
  188. }
  189. COM_PROTECT_CATCH
  190. return hr;
  191. }
  192. HRESULT RootHandler::GetRtrObj(LONG_PTR ulConnId, IUnknown ** ppRtrObj)
  193. {
  194. HRESULT hr = hrOK;
  195. RtrObjRecord rtrObj;
  196. COM_PROTECT_TRY
  197. {
  198. if (m_mapRtrObj.Lookup(ulConnId, rtrObj) == 0)
  199. {
  200. // entry not in the list
  201. Trace1("RootHandler::GetRtrObj - %lx not in the list!", ulConnId);
  202. return E_INVALIDARG;
  203. }
  204. if (ppRtrObj)
  205. {
  206. *ppRtrObj = rtrObj.m_spUnk;
  207. (*ppRtrObj)->AddRef();
  208. }
  209. }
  210. COM_PROTECT_CATCH
  211. return hr;
  212. }
  213. HRESULT RootHandler::SetProtocolAdded(LONG_PTR ulConnId, BOOL fProtocolAdded)
  214. {
  215. HRESULT hr = hrOK;
  216. RtrObjRecord rtrObj;
  217. COM_PROTECT_TRY
  218. {
  219. if (m_mapRtrObj.Lookup(ulConnId, rtrObj) == 0)
  220. {
  221. // entry not in the list
  222. Trace1("RootHandler::SetProtocolAdded - %lx not in the list!", ulConnId);
  223. return E_INVALIDARG;
  224. }
  225. rtrObj.m_fAddedProtocolNode = fProtocolAdded;
  226. m_mapRtrObj.SetAt(ulConnId, rtrObj);
  227. }
  228. COM_PROTECT_CATCH
  229. return hr;
  230. }
  231. BOOL RootHandler::IsProtocolAdded(LONG_PTR ulConnId)
  232. {
  233. HRESULT hr = hrOK;
  234. RtrObjRecord rtrObj;
  235. BOOL bAdded = FALSE;
  236. COM_PROTECT_TRY
  237. {
  238. if (m_mapRtrObj.Lookup(ulConnId, rtrObj) == 0)
  239. {
  240. // entry not in the list
  241. Trace1("RootHandler::IsProtocolAdded - %lx not in the list!", ulConnId);
  242. return bAdded;
  243. }
  244. bAdded = rtrObj.m_fAddedProtocolNode;
  245. }
  246. COM_PROTECT_CATCH
  247. return bAdded;
  248. }
  249. HRESULT RootHandler::RemoveAllRtrObj()
  250. {
  251. HRESULT hr = hrOK;
  252. POSITION pos;
  253. RtrObjRecord rtrObj;
  254. LONG_PTR ulKey;
  255. COM_PROTECT_TRY
  256. {
  257. pos = m_mapRtrObj.GetStartPosition();
  258. while (pos)
  259. {
  260. m_mapRtrObj.GetNextAssoc(pos, ulKey, rtrObj);
  261. if (rtrObj.m_riid == IID_IRtrMgrInfo)
  262. {
  263. SPIRtrMgrInfo spRm;
  264. Verify( FHrOK(spRm.HrQuery(rtrObj.m_spUnk)) );
  265. spRm->RtrUnadvise(ulKey);
  266. }
  267. else if (rtrObj.m_riid == IID_IRouterInfo)
  268. {
  269. SPIRouterInfo spRouter;
  270. Verify( FHrOK(spRouter.HrQuery(rtrObj.m_spUnk)) );
  271. spRouter->RtrUnadvise(ulKey);
  272. }
  273. else if (rtrObj.m_riid == IID_IRouterRefresh)
  274. {
  275. SPIRouterInfo spRouter;
  276. SPIRouterRefresh spRefresh;
  277. Verify( FHrOK(spRouter.HrQuery(rtrObj.m_spUnk)) );
  278. Verify( FHrOK(spRouter->GetRefreshObject(&spRefresh)) );
  279. if (ulKey)
  280. spRefresh->UnadviseRefresh(ulKey);
  281. }
  282. else
  283. {
  284. Panic0("Unknown type in RtrObjMap!");
  285. }
  286. }
  287. }
  288. COM_PROTECT_CATCH
  289. return hr;
  290. }
  291. /*!--------------------------------------------------------------------------
  292. RootHandler::AddScopeItem
  293. This will add the hScopeItem into the map (using the pszMachineName
  294. as the key).
  295. If the machine name already exists, then the hScopeItem entry is
  296. overwritten.
  297. This is added so that we can differentiate between the various
  298. nodes (in the mulitple instance case).
  299. Author: KennT
  300. ---------------------------------------------------------------------------*/
  301. HRESULT RootHandler::AddScopeItem(LPCTSTR pszMachineName, HSCOPEITEM hScopeItem)
  302. {
  303. HRESULT hr = hrOK;
  304. Assert(pszMachineName);
  305. COM_PROTECT_TRY
  306. {
  307. m_mapScopeItem.SetAt(pszMachineName, (LPVOID) hScopeItem);
  308. }
  309. COM_PROTECT_CATCH;
  310. return hr;
  311. }
  312. /*!--------------------------------------------------------------------------
  313. RootHandler::GetScopeItem
  314. Looks up the scope item associated with this machine name.
  315. Returns hrOK if a scope item is found.
  316. Returns hrFalse if there is no scope item for this name.
  317. Returns else otherwise.
  318. Author: KennT
  319. ---------------------------------------------------------------------------*/
  320. HRESULT RootHandler::GetScopeItem(LPCTSTR pszMachineName, HSCOPEITEM *phScopeItem)
  321. {
  322. HRESULT hr = hrFalse;
  323. LPVOID pv;
  324. Assert(phScopeItem);
  325. *phScopeItem = NULL;
  326. if (m_mapScopeItem.Lookup(pszMachineName, pv))
  327. {
  328. *phScopeItem = (HSCOPEITEM) pv;
  329. hr = hrOK;
  330. }
  331. return hr;
  332. }
  333. /*!--------------------------------------------------------------------------
  334. RootHandler::RemoveScopeItem
  335. -
  336. Author: KennT
  337. ---------------------------------------------------------------------------*/
  338. HRESULT RootHandler::RemoveScopeItem(HSCOPEITEM hScopeItem)
  339. {
  340. HRESULT hr = hrFalse;
  341. CString stKey;
  342. POSITION pos = NULL;
  343. LPVOID pv = NULL;
  344. for (pos = m_mapScopeItem.GetStartPosition(); pos != NULL; )
  345. {
  346. stKey.Empty();
  347. pv = NULL;
  348. m_mapScopeItem.GetNextAssoc(pos, stKey, pv);
  349. if ((HSCOPEITEM) pv == hScopeItem)
  350. {
  351. Trace2("Removing (%s,%x)\n", (LPCTSTR) stKey, hScopeItem);
  352. m_mapScopeItem.RemoveKey(stKey);
  353. hr = hrOK;
  354. break;
  355. }
  356. }
  357. return hr;
  358. }
  359. HRESULT RootHandler::SetComputerAddedAsLocal(LONG_PTR ulConnId, BOOL fComputerAddedAsLocal)
  360. {
  361. HRESULT hr = hrOK;
  362. RtrObjRecord rtrObj;
  363. COM_PROTECT_TRY
  364. {
  365. if (m_mapRtrObj.Lookup(ulConnId, rtrObj) == 0)
  366. {
  367. // entry not in the list
  368. Trace1("RootHandler::SetComputerAddedAsLocal - %lx not in the list!", ulConnId);
  369. return E_INVALIDARG;
  370. }
  371. rtrObj.m_fComputerAddedAsLocal = fComputerAddedAsLocal;
  372. m_mapRtrObj.SetAt(ulConnId, rtrObj);
  373. }
  374. COM_PROTECT_CATCH
  375. return hr;
  376. }
  377. BOOL RootHandler::IsComputerAddedAsLocal(LONG_PTR ulConnId)
  378. {
  379. HRESULT hr = hrOK;
  380. RtrObjRecord rtrObj;
  381. BOOL bAdded = FALSE;
  382. COM_PROTECT_TRY
  383. {
  384. if (m_mapRtrObj.Lookup(ulConnId, rtrObj) == 0)
  385. {
  386. // entry not in the list
  387. Trace1("RootHandler::IsComputerAddedAsLocal - %lx not in the list!", ulConnId);
  388. return bAdded;
  389. }
  390. bAdded = rtrObj.m_fComputerAddedAsLocal;
  391. }
  392. COM_PROTECT_CATCH
  393. return bAdded;
  394. }
  395. /*!--------------------------------------------------------------------------
  396. RootHandler::AddCookie
  397. -
  398. Author: KennT
  399. ---------------------------------------------------------------------------*/
  400. HRESULT RootHandler::AddCookie(HSCOPEITEM hScopeItem, MMC_COOKIE cookie)
  401. {
  402. HRESULT hr = hrOK;
  403. COM_PROTECT_TRY
  404. {
  405. m_mapNode.SetAt((LPVOID) hScopeItem, (LPVOID) cookie);
  406. }
  407. COM_PROTECT_CATCH;
  408. return hr;
  409. }
  410. /*!--------------------------------------------------------------------------
  411. RootHandler::GetCookie
  412. -
  413. Author: KennT
  414. ---------------------------------------------------------------------------*/
  415. HRESULT RootHandler::GetCookie(HSCOPEITEM hScopeItem, MMC_COOKIE *pCookie)
  416. {
  417. HRESULT hr = hrFalse;
  418. LPVOID pv;
  419. *pCookie = NULL;
  420. if (m_mapNode.Lookup((LPVOID) hScopeItem, pv))
  421. {
  422. *pCookie = (MMC_COOKIE) pv;
  423. hr = hrOK;
  424. }
  425. return hr;
  426. }
  427. /*!--------------------------------------------------------------------------
  428. RootHandler::RemoveCookie
  429. -
  430. Author: KennT
  431. ---------------------------------------------------------------------------*/
  432. HRESULT RootHandler::RemoveCookie(HSCOPEITEM hScopeItem)
  433. {
  434. HRESULT hr = hrOK;
  435. if (m_mapNode.RemoveKey((LPVOID) hScopeItem) == 0)
  436. hr = hrFalse;
  437. return hr;
  438. }
  439. /*!--------------------------------------------------------------------------
  440. RootHandler::CompareNodeToMachineName
  441. Dummy function.
  442. Author: KennT
  443. ---------------------------------------------------------------------------*/
  444. HRESULT RootHandler::CompareNodeToMachineName(ITFSNode *pNode,
  445. LPCTSTR pszMachineName)
  446. {
  447. Panic0("This should be overriden!");
  448. return hrFalse;
  449. }
  450. /*!--------------------------------------------------------------------------
  451. RootHandler::RemoveNode
  452. -
  453. Author: KennT
  454. ---------------------------------------------------------------------------*/
  455. HRESULT RootHandler::RemoveNode(ITFSNode *pNode,
  456. LPCTSTR pszMachineName)
  457. {
  458. Assert(pNode);
  459. SPITFSNodeEnum spNodeEnum;
  460. SPITFSNode spNode;
  461. HRESULT hr = hrOK;
  462. // Windows NT Bug : 246822
  463. // Due to the server list programming model, we need to setup
  464. // the proper scopeitem (so that MMC adds this to the proper
  465. // node).
  466. // Get the proper scope item for this node.
  467. // ----------------------------------------------------------------
  468. HSCOPEITEM hScopeItem = 0;
  469. HSCOPEITEM hOldScopeItem = 0;
  470. Verify( GetScopeItem(pszMachineName, &hScopeItem) == hrOK);
  471. // Get the old one and save it. place the new one in the node.
  472. // ----------------------------------------------------
  473. hOldScopeItem = pNode->GetData(TFS_DATA_SCOPEID);
  474. pNode->SetData(TFS_DATA_SCOPEID, hScopeItem);
  475. CORg( pNode->GetEnum(&spNodeEnum) );
  476. for (; spNodeEnum->Next(1, &spNode, NULL) == hrOK; spNode.Release())
  477. {
  478. if (CompareNodeToMachineName(spNode, pszMachineName) == hrOK)
  479. {
  480. pNode->RemoveChild(spNode);
  481. spNode->Destroy();
  482. break;
  483. }
  484. }
  485. Error:
  486. pNode->SetData(TFS_DATA_SCOPEID, hOldScopeItem);
  487. return hr;
  488. }
  489. /*!--------------------------------------------------------------------------
  490. RootHandler::RemoveAllNodes
  491. -
  492. Author: KennT
  493. ---------------------------------------------------------------------------*/
  494. HRESULT RootHandler::RemoveAllNodes(ITFSNode *pNode)
  495. {
  496. Assert(pNode);
  497. SPITFSNodeEnum spNodeEnum;
  498. SPITFSNode spNode;
  499. HRESULT hr = hrOK;
  500. CORg( pNode->GetEnum(&spNodeEnum) );
  501. for (; spNodeEnum->Next(1, &spNode, NULL) == hrOK; spNode.Release())
  502. {
  503. pNode->RemoveChild(spNode);
  504. spNode->Destroy();
  505. }
  506. Error:
  507. return hr;
  508. }
  509. /*!--------------------------------------------------------------------------
  510. RootHandler::OnRemoveChildren
  511. -
  512. Author: KennT
  513. ---------------------------------------------------------------------------*/
  514. HRESULT RootHandler::OnRemoveChildren(ITFSNode *pNode,
  515. LPDATAOBJECT pdo,
  516. LPARAM arg,
  517. LPARAM lParam)
  518. {
  519. MMC_COOKIE cookie;
  520. HRESULT hr = hrOK;
  521. SPITFSNode spChild;
  522. // Map the scopeitem to the cookie
  523. // ----------------------------------------------------------------
  524. if ( FHrOK(GetCookie((HSCOPEITEM) arg, &cookie)) )
  525. {
  526. // Remove this node
  527. // --------------------------------------------------------
  528. m_spNodeMgr->FindNode(cookie, &spChild);
  529. Assert(spChild);
  530. if (spChild)
  531. {
  532. pNode->RemoveChild(spChild);
  533. spChild->Destroy();
  534. spChild.Release();
  535. RemoveScopeItem((HSCOPEITEM) arg);
  536. RemoveCookie((HSCOPEITEM) arg);
  537. }
  538. }
  539. return hr;
  540. }