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.

314 lines
9.2 KiB

  1. // ChooseServerSite.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "certwiz.h"
  5. #include "Certificat.h"
  6. #include "CertUtil.h"
  7. #include "ChooseServerSite.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. #define COL_SITE_INSTANCE 0
  14. #define COL_SITE_DESC 1
  15. #define COL_SITE_INSTANCE_WID 50
  16. #define COL_SITE_DESC_WID 100
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CChooseServerSite
  19. CChooseServerSite::CChooseServerSite(BOOL bShowOnlyCertSites, CString& strSiteReturned,CCertificate * pCert,IN CWnd * pParent OPTIONAL)
  20. : CDialog(CChooseServerSite::IDD,pParent)
  21. {
  22. m_ShowOnlyCertSites = bShowOnlyCertSites;
  23. m_strSiteReturned = strSiteReturned;
  24. m_pCert = pCert;
  25. //{{AFX_DATA_INIT(CChooseServerSite)
  26. //}}AFX_DATA_INIT
  27. }
  28. CChooseServerSite::~CChooseServerSite()
  29. {
  30. }
  31. void CChooseServerSite::DoDataExchange(CDataExchange* pDX)
  32. {
  33. CDialog::DoDataExchange(pDX);
  34. //{{AFX_DATA_MAP(CChooseServerSite)
  35. DDX_Control(pDX, IDC_SITE_LIST, m_ServerSiteList);
  36. //}}AFX_DATA_MAP
  37. }
  38. BEGIN_MESSAGE_MAP(CChooseServerSite, CDialog)
  39. //{{AFX_MSG_MAP(CChooseServerSite)
  40. ON_NOTIFY(NM_CLICK, IDC_SITE_LIST, OnClickSiteList)
  41. ON_NOTIFY(NM_DBLCLK, IDC_SITE_LIST, OnDblClickSiteList)
  42. ON_NOTIFY(LVN_KEYDOWN, IDC_SITE_LIST, OnKeydown)
  43. //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CChooseServerSite message handlers
  47. BOOL CChooseServerSite::OnInitDialog()
  48. {
  49. CDialog::OnInitDialog();
  50. HRESULT hr;
  51. CString MachineName_Remote;
  52. CString UserName_Remote;
  53. CString UserPassword_Remote;
  54. CString SiteToExclude;
  55. CMapStringToString MetabaseSiteKeyWithSiteDescValueList;
  56. CStringListEx strlDataPaths;
  57. CCertListCtrl* pControl = (CCertListCtrl *)CWnd::FromHandle(GetDlgItem(IDC_SITE_LIST)->m_hWnd);
  58. CRect rcControl;
  59. pControl->GetClientRect(&rcControl);
  60. // make the list have column headers
  61. CString str;
  62. str= _T("");
  63. str.LoadString(IDS_SITE_NUM_COLUMN);
  64. m_ServerSiteList.InsertColumn(COL_SITE_INSTANCE, str, LVCFMT_LEFT, COL_SITE_INSTANCE_WID);
  65. str.LoadString(IDS_WEB_SITE_COLUMN);
  66. m_ServerSiteList.InsertColumn(COL_SITE_DESC, str, LVCFMT_LEFT, rcControl.Width() - COL_SITE_INSTANCE_WID);
  67. m_ServerSiteList.AdjustStyle();
  68. // Use machine/username/userpassword
  69. // to connect to the machine
  70. // and enumerate all the sites on that machine.
  71. // return back a string1=string2 pair
  72. // string1 = /w3svc/1
  73. // string2 = "site description"
  74. // present a dialog so the user can choose which one they want...
  75. // m_ServerSiteInstance = /w3svc/1
  76. // m_ServerSiteDescription = "site description"
  77. MachineName_Remote = m_pCert->m_MachineName_Remote;
  78. UserName_Remote = m_pCert->m_UserName_Remote;
  79. m_pCert->m_UserPassword_Remote.CopyTo(UserPassword_Remote);
  80. SiteToExclude = m_pCert->m_WebSiteInstanceName;
  81. if (m_ShowOnlyCertSites)
  82. {
  83. hr = EnumSitesWithCertInstalled(MachineName_Remote,UserName_Remote,UserPassword_Remote,m_pCert->m_WebSiteInstanceName,SiteToExclude,&strlDataPaths);
  84. }
  85. else
  86. {
  87. hr = EnumSites(MachineName_Remote,UserName_Remote,UserPassword_Remote,m_pCert->m_WebSiteInstanceName,SiteToExclude,&strlDataPaths);
  88. }
  89. if (!strlDataPaths.IsEmpty())
  90. {
  91. POSITION pos;
  92. CString name;
  93. CString value = _T("");
  94. CString SiteInstance;
  95. int item = 0;
  96. LV_ITEMW lvi;
  97. //
  98. // set up the fields in the list view item struct that don't change from item to item
  99. //
  100. memset(&lvi, 0, sizeof(LV_ITEMW));
  101. lvi.mask = LVIF_TEXT;
  102. // loop thru the list and display all the stuff on a dialog box...
  103. pos = strlDataPaths.GetHeadPosition();
  104. while (pos)
  105. {
  106. int i = 0;
  107. name = strlDataPaths.GetAt(pos);
  108. value = _T("");
  109. SiteInstance.Format(_T("%d"), CMetabasePath::GetInstanceNumber(name));
  110. lvi.iItem = item;
  111. lvi.iSubItem = COL_SITE_INSTANCE;
  112. lvi.pszText = (LPTSTR)(LPCTSTR)SiteInstance;
  113. lvi.cchTextMax = SiteInstance.GetLength();
  114. i = m_ServerSiteList.InsertItem(&lvi);
  115. ASSERT(i != -1);
  116. lvi.iItem = i;
  117. lvi.iSubItem = COL_SITE_DESC;
  118. lvi.pszText = (LPTSTR)(LPCTSTR)value;
  119. lvi.cchTextMax = value.GetLength();
  120. VERIFY(m_ServerSiteList.SetItem(&lvi));
  121. // set item data with the pointer to the Strings
  122. CString * pDataItemString = new CString(name);
  123. VERIFY(m_ServerSiteList.SetItemData(item, (LONG_PTR)pDataItemString));
  124. item++;
  125. strlDataPaths.GetNext(pos);
  126. }
  127. FillListWithMetabaseSiteDesc();
  128. }
  129. GetDlgItem(IDOK)->EnableWindow(FALSE);
  130. return TRUE;
  131. }
  132. BOOL CChooseServerSite::FillListWithMetabaseSiteDesc()
  133. {
  134. int count = m_ServerSiteList.GetItemCount();
  135. CString strMetabaseKey;
  136. CString value = _T("");
  137. CString strDescription;
  138. HRESULT hr = E_FAIL;
  139. CString MachineName_Remote;
  140. CString UserName_Remote;
  141. CString UserPassword_Remote;
  142. MachineName_Remote = m_pCert->m_MachineName_Remote;
  143. UserName_Remote = m_pCert->m_UserName_Remote;
  144. m_pCert->m_UserPassword_Remote.CopyTo(UserPassword_Remote);
  145. CString * pMetabaseKey;
  146. for (int index = 0; index < count; index++)
  147. {
  148. pMetabaseKey = (CString *) m_ServerSiteList.GetItemData(index);
  149. if (pMetabaseKey)
  150. {
  151. strMetabaseKey = *pMetabaseKey;
  152. // Go get the site's description;
  153. if (TRUE == GetServerComment(MachineName_Remote,UserName_Remote,UserPassword_Remote,strMetabaseKey,strDescription,&hr))
  154. {
  155. value = strDescription;
  156. }
  157. else
  158. {
  159. value = strMetabaseKey;
  160. }
  161. m_ServerSiteList.SetItemText(index, COL_SITE_DESC,value);
  162. }
  163. }
  164. return TRUE;
  165. }
  166. void CChooseServerSite::OnDblClickSiteList(NMHDR* pNMHDR, LRESULT* pResult)
  167. {
  168. // Get the hash for the certificate that is clicked on...
  169. m_Index = m_ServerSiteList.GetSelectedIndex();
  170. if (m_Index != -1)
  171. {
  172. // Get the metabase key..
  173. CString * pMetabaseKey = NULL;
  174. pMetabaseKey = (CString *) m_ServerSiteList.GetItemData(m_Index);
  175. if (pMetabaseKey)
  176. {
  177. m_strSiteReturned = *pMetabaseKey;
  178. // use the metabase key to lookup the hash
  179. // find cert in store
  180. CRYPT_HASH_BLOB * pHash = NULL;
  181. HRESULT hr;
  182. // go lookup the certhash from the metabase
  183. if (0 == _tcsicmp(m_pCert->m_MachineName_Remote,m_pCert->m_MachineName))
  184. {
  185. pHash = GetInstalledCertHash(m_pCert->m_MachineName_Remote,m_strSiteReturned,m_pCert->GetEnrollObject(),&hr);
  186. if (pHash)
  187. {
  188. ViewCertificateDialog(pHash,m_hWnd);
  189. if (pHash){CoTaskMemFree(pHash);}
  190. }
  191. }
  192. }
  193. GetDlgItem(IDOK)->EnableWindow(TRUE);
  194. }
  195. return;
  196. }
  197. void CChooseServerSite::OnOK()
  198. {
  199. m_Index = m_ServerSiteList.GetSelectedIndex();
  200. m_strSiteReturned = _T("");
  201. if (m_Index != -1)
  202. {
  203. CString * pMetabaseKey = NULL;
  204. pMetabaseKey = (CString *) m_ServerSiteList.GetItemData(m_Index);
  205. if (pMetabaseKey)
  206. {
  207. m_strSiteReturned = *pMetabaseKey;
  208. if (m_ShowOnlyCertSites)
  209. {
  210. CString csPasswordTemp;
  211. m_pCert->m_UserPassword_Remote.CopyTo(csPasswordTemp);
  212. // check if the returned site has an exportable certificate on it...
  213. if (FALSE == IsCertExportableOnRemoteMachine(m_pCert->m_MachineName_Remote,m_pCert->m_UserName_Remote,csPasswordTemp,m_strSiteReturned))
  214. {
  215. // tell the user that certificat that they chose is not exportable.
  216. CString buf;
  217. buf.LoadString(IDS_CERT_NOT_EXPORTABLE);
  218. AfxMessageBox(buf, MB_OK);
  219. return;
  220. }
  221. }
  222. }
  223. }
  224. CDialog::OnOK();
  225. }
  226. void CChooseServerSite::OnClickSiteList(NMHDR* pNMHDR, LRESULT* pResult)
  227. {
  228. m_Index = m_ServerSiteList.GetSelectedIndex();
  229. if (m_Index != -1)
  230. {
  231. GetDlgItem(IDOK)->EnableWindow(TRUE);
  232. }
  233. else
  234. {
  235. GetDlgItem(IDOK)->EnableWindow(FALSE);
  236. }
  237. *pResult = 0;
  238. }
  239. void CChooseServerSite::OnDestroy()
  240. {
  241. // before dialog will be desroyed we need to delete all
  242. // the item data pointers
  243. int count = m_ServerSiteList.GetItemCount();
  244. for (int index = 0; index < count; index++)
  245. {
  246. CString * pData = (CString *) m_ServerSiteList.GetItemData(index);
  247. delete pData;
  248. }
  249. CDialog::OnDestroy();
  250. }
  251. void CChooseServerSite::OnKeydown(NMHDR* pNMHDR, LRESULT* pResult)
  252. {
  253. m_Index = m_ServerSiteList.GetSelectedIndex();
  254. if (m_Index != -1)
  255. {
  256. GetDlgItem(IDOK)->EnableWindow(TRUE);
  257. }
  258. else
  259. {
  260. GetDlgItem(IDOK)->EnableWindow(FALSE);
  261. }
  262. *pResult = 0;
  263. return;
  264. }