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.

372 lines
11 KiB

  1. /*++
  2. Module Name:
  3. IPStream.cpp
  4. Abstract:
  5. This module contains the implementation for CDfsSnapinScopeManager.
  6. This class implements IPersistStream interface for the above.
  7. --*/
  8. #include "stdafx.h"
  9. #include "DfsGUI.h"
  10. #include "DfsScope.h"
  11. #include "utils.h"
  12. #include <lmdfs.h>
  13. STDMETHODIMP
  14. CDfsSnapinScopeManager::GetClassID(
  15. OUT struct _GUID* o_pClsid
  16. )
  17. /*++
  18. Routine Description:
  19. Return the snapin CLSID.
  20. Arguments:
  21. o_pClsid - The clsid is returned here.
  22. --*/
  23. {
  24. *o_pClsid = CLSID_DfsSnapinScopeManager;
  25. return S_OK;
  26. }
  27. STDMETHODIMP
  28. CDfsSnapinScopeManager::IsDirty(
  29. )
  30. /*++
  31. Routine Description:
  32. Use to check if the object has been changed since last save.
  33. Returns S_OK if it has, otherwise returns S_FALSE
  34. Return value:
  35. S_OK, if the object has changed. i.e., dirty
  36. S_FALSE, if the object has not changed, i.e., not dirty.
  37. --*/
  38. {
  39. return m_pMmcDfsAdmin->GetDirty() ? S_OK : S_FALSE;
  40. }
  41. STDMETHODIMP
  42. CDfsSnapinScopeManager::Load(
  43. IN LPSTREAM i_pStream
  44. )
  45. /*++
  46. Routine Description:
  47. Used to load the snap-in from a saved file(.MSC file).
  48. We set dirty to false(to disbale save), if load succeeds completely
  49. Arguments:
  50. i_pStream - Pointer to an IPersistStream object from which the saved information is to
  51. be read.
  52. --*/
  53. {
  54. RETURN_INVALIDARG_IF_NULL(i_pStream);
  55. CWaitCursor WaitCursor;
  56. // Get the size of data that was stored
  57. ULONG ulDataLen = 0;
  58. ULONG uBytesRead = 0;
  59. HRESULT hr = i_pStream->Read(&ulDataLen, sizeof (ULONG), &uBytesRead);
  60. RETURN_IF_FAILED(hr);
  61. if (ulDataLen <= 0) // No use in continuing if no data is there
  62. {
  63. // do we have a local dfsroot?
  64. TCHAR szLocalComputerName[MAX_COMPUTERNAME_LENGTH + 1] = {0};
  65. DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
  66. GetComputerName(szLocalComputerName, &dwSize);
  67. ROOTINFOLIST DfsRootList;
  68. if (S_OK == GetMultiDfsRoots(&DfsRootList, szLocalComputerName) && !DfsRootList.empty())
  69. {
  70. for (ROOTINFOLIST::iterator i = DfsRootList.begin(); i != DfsRootList.end(); i++)
  71. {
  72. CComPtr<IDfsRoot> pDfsRoot;
  73. hr = CoCreateInstance(CLSID_DfsRoot, NULL, CLSCTX_INPROC_SERVER, IID_IDfsRoot, (void**) &pDfsRoot);
  74. if(SUCCEEDED(hr))
  75. {
  76. hr = pDfsRoot->Initialize((*i)->bstrRootName);
  77. if (S_OK == hr)
  78. (void)m_pMmcDfsAdmin->AddDfsRootToList(pDfsRoot);
  79. }
  80. }
  81. }
  82. FreeRootInfoList(&DfsRootList);
  83. /*
  84. CComBSTR bstrRootEntryPath;
  85. hr = IsHostingDfsRoot(szLocalComputerName, &bstrRootEntryPath);
  86. if (S_OK == hr)
  87. {
  88. CComPtr<IDfsRoot> pDfsRoot;
  89. hr = CoCreateInstance (CLSID_DfsRoot, NULL, CLSCTX_INPROC_SERVER, IID_IDfsRoot, (void**) &pDfsRoot);
  90. if(SUCCEEDED(hr))
  91. {
  92. hr = pDfsRoot->Initialize(bstrRootEntryPath);
  93. if (S_OK == hr)
  94. (void)m_pMmcDfsAdmin->AddDfsRootToList(pDfsRoot);
  95. }
  96. } */
  97. } else
  98. {
  99. bool bSomeLoadFailed = false;
  100. BYTE* pStreamData = NULL;
  101. do {
  102. pStreamData = new BYTE [ulDataLen]; // Allocate memory for the data to be read
  103. BREAK_OUTOFMEMORY_IF_NULL(pStreamData, &hr);
  104. // Read the data from the stream
  105. hr = i_pStream->Read(pStreamData, ulDataLen, &uBytesRead);
  106. BREAK_IF_FAILED(hr);
  107. BYTE* pData = pStreamData;
  108. BYTE* pDataEnd = pStreamData + ulDataLen;
  109. // Start by reading the first machines name
  110. ULONG nVersion = 0;
  111. TCHAR *lpszDfsName = (LPTSTR)pData;
  112. if (*lpszDfsName == _T('\\'))
  113. {
  114. nVersion = 0;
  115. } else
  116. {
  117. ULONG nVer = *(ULONG UNALIGNED *)pData;
  118. if (nVer == 1)
  119. {
  120. nVersion = 1;
  121. pData += sizeof(ULONG);
  122. } else
  123. {
  124. hr = S_FALSE; // corrupted console file
  125. break;
  126. }
  127. }
  128. do
  129. {
  130. lpszDfsName = (LPTSTR)pData;
  131. pData += sizeof(TCHAR) * (_tcslen(lpszDfsName) + 1);
  132. CComPtr<IDfsRoot> pDfsRoot;
  133. hr = CoCreateInstance (CLSID_DfsRoot, NULL, CLSCTX_INPROC_SERVER, IID_IDfsRoot, (void**) &pDfsRoot);
  134. BREAK_IF_FAILED(hr);
  135. // retrieve link filtering settings
  136. ULONG ulMaxLimit = FILTERDFSLINKS_MAXLIMIT_DEFAULT;
  137. FILTERDFSLINKS_TYPE lFilterType = FILTERDFSLINKS_TYPE_NO_FILTER;
  138. TCHAR *pszFilterName = NULL;
  139. if (nVersion == 1)
  140. {
  141. ulMaxLimit = *((ULONG UNALIGNED *)pData);
  142. pData += sizeof(ULONG);
  143. lFilterType = *((enum FILTERDFSLINKS_TYPE UNALIGNED *)pData);
  144. pData += sizeof(lFilterType);
  145. if (lFilterType != FILTERDFSLINKS_TYPE_NO_FILTER)
  146. {
  147. pszFilterName = (LPTSTR)pData;
  148. pData += sizeof(TCHAR) * (_tcslen(pszFilterName) + 1);
  149. }
  150. }
  151. hr = pDfsRoot->Initialize(lpszDfsName);
  152. if (S_OK == hr)
  153. {
  154. CComBSTR bstrDfsRootEntryPath;
  155. hr = pDfsRoot->get_RootEntryPath(&bstrDfsRootEntryPath);
  156. if (SUCCEEDED(hr))
  157. {
  158. // If already present in the list, just ignore this entry
  159. hr = m_pMmcDfsAdmin->IsAlreadyInList(bstrDfsRootEntryPath);
  160. if (S_OK != hr)
  161. {
  162. (void) m_pMmcDfsAdmin->AddDfsRootToList(
  163. pDfsRoot, ulMaxLimit, lFilterType, pszFilterName);
  164. }
  165. }
  166. }
  167. else
  168. {
  169. DisplayMessageBoxWithOK(IDS_MSG_FAILED_TO_INITIALIZE_DFSROOT, lpszDfsName);
  170. bSomeLoadFailed = true; // Since we could not create a dfsroot
  171. }
  172. } while (pData < pDataEnd);
  173. } while (false);
  174. if (pStreamData)
  175. delete [] pStreamData;
  176. m_pMmcDfsAdmin->SetDirty(bSomeLoadFailed); // Cause we just read the whole dfsroot list from file
  177. }
  178. return hr;
  179. }
  180. STDMETHODIMP
  181. CDfsSnapinScopeManager::Save(
  182. OUT LPSTREAM o_pStream,
  183. IN BOOL i_bClearDirty
  184. )
  185. /*++
  186. Routine Description:
  187. Used to save the snap-in to a .MSC file. This uses a IPersistStream object.
  188. Arguments:
  189. o_pStream - Pointer to an IPersistStream object to which the saved information is to
  190. be written.
  191. i_bClearDirty - A flag indication whether the dirty flag should be cleared
  192. --*/
  193. {
  194. RETURN_INVALIDARG_IF_NULL(o_pStream);
  195. ULONG nVersion = 1;
  196. DFS_ROOT_LIST* lpDfsRootList = NULL;
  197. HRESULT hr = m_pMmcDfsAdmin->GetList (&lpDfsRootList);
  198. RETURN_IF_FAILED(hr);
  199. ULONG ulDataLen = 0;
  200. DFS_ROOT_LIST::iterator i;
  201. for (i = lpDfsRootList->begin(); i != lpDfsRootList->end(); i++)
  202. {
  203. ulDataLen +=
  204. ((_tcslen((*i)->m_bstrRootEntryPath) + 1) * sizeof (TCHAR)) + // to hold RootEntryPath
  205. sizeof(ULONG) + // to hold LinkFilterMaxLimit
  206. sizeof(enum FILTERDFSLINKS_TYPE); // to hold LinkFilterType
  207. if ((*i)->m_pMmcDfsRoot->get_LinkFilterType() != FILTERDFSLINKS_TYPE_NO_FILTER)
  208. {
  209. BSTR bstr = (*i)->m_pMmcDfsRoot->get_LinkFilterName();
  210. ulDataLen += ((bstr ? _tcslen(bstr) : 0) + 1) * sizeof(TCHAR); // to hold LinkFilterName
  211. }
  212. }
  213. if (!ulDataLen)
  214. return hr; // no root to presist, return
  215. ulDataLen += sizeof(nVersion); // to hold the version number
  216. // Allocate data
  217. BYTE* pStreamData = new BYTE [ulDataLen];
  218. RETURN_OUTOFMEMORY_IF_NULL(pStreamData);
  219. // Prepare the data
  220. BYTE* pData = pStreamData;
  221. ZeroMemory(pStreamData, ulDataLen);
  222. // hold version number
  223. memcpy(pData, &nVersion, sizeof(nVersion));
  224. pData += sizeof(nVersion);
  225. int len = 0;
  226. for (i = lpDfsRootList->begin(); i != lpDfsRootList->end(); i++)
  227. {
  228. // hold RootEntryPath
  229. len = (_tcslen((*i)->m_bstrRootEntryPath) + 1) * sizeof(TCHAR);
  230. memcpy(pData, (*i)->m_bstrRootEntryPath, len);
  231. pData += len;
  232. // hold LinkFilterMaxLimit
  233. ULONG ulLinkFilterMaxLimit = (*i)->m_pMmcDfsRoot->get_LinkFilterMaxLimit();
  234. memcpy(pData, &ulLinkFilterMaxLimit, sizeof(ulLinkFilterMaxLimit));
  235. pData += sizeof(ulLinkFilterMaxLimit);
  236. // hold LinkFilterType
  237. FILTERDFSLINKS_TYPE lLinkFilterType = (*i)->m_pMmcDfsRoot->get_LinkFilterType();
  238. memcpy(pData, &lLinkFilterType, sizeof(lLinkFilterType));
  239. pData += sizeof(lLinkFilterType);
  240. // hold LinkFilterName
  241. if (lLinkFilterType != FILTERDFSLINKS_TYPE_NO_FILTER)
  242. {
  243. BSTR bstr = (*i)->m_pMmcDfsRoot->get_LinkFilterName();
  244. len = ((bstr ? _tcslen(bstr) : 0) + 1) * sizeof(TCHAR);
  245. memcpy(pData, (bstr ? bstr : _T("")), len);
  246. pData += len;
  247. }
  248. }
  249. // Write the data length to the stream
  250. ULONG uBytesWritten = 0;
  251. hr = o_pStream->Write(&ulDataLen, sizeof(ulDataLen), &uBytesWritten);
  252. if(SUCCEEDED(hr))
  253. {
  254. // Now write the data to the stream
  255. hr = o_pStream->Write(pStreamData, ulDataLen, &uBytesWritten);
  256. }
  257. if (pStreamData)
  258. delete [] pStreamData;
  259. if (i_bClearDirty)
  260. m_pMmcDfsAdmin->SetDirty(false);
  261. return hr;
  262. }
  263. STDMETHODIMP
  264. CDfsSnapinScopeManager::GetSizeMax(
  265. OUT ULARGE_INTEGER* o_pulSize
  266. )
  267. /*++
  268. Routine Description:
  269. Return the size of the data we will write to the stream.
  270. Arguments:
  271. o_ulcbSize - Return the size of data in the low byte of this variable
  272. --*/
  273. {
  274. RETURN_INVALIDARG_IF_NULL(o_pulSize);
  275. DFS_ROOT_LIST* lpDfsRootList = NULL;
  276. HRESULT hr = m_pMmcDfsAdmin->GetList (&lpDfsRootList);
  277. RETURN_IF_FAILED(hr);
  278. ULONG ulDataLen = 0;
  279. for (DFS_ROOT_LIST::iterator i = lpDfsRootList->begin(); i != lpDfsRootList->end(); i++)
  280. {
  281. ulDataLen += (_tcslen ((*i)->m_bstrRootEntryPath) + 1) * sizeof (TCHAR);
  282. }
  283. o_pulSize->LowPart = ulDataLen; // Return the size in the low bit
  284. o_pulSize->HighPart = 0;
  285. return hr;
  286. }