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.

415 lines
10 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 2002
  6. //
  7. // File: simdata.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. /////////////////////////////////////////////////////////////////////
  11. // SimData.cpp - Implementation of Security Identity Mapping
  12. //
  13. // HISTORY
  14. // 23-Jun-97 t-danm Creation.
  15. /////////////////////////////////////////////////////////////////////
  16. #include "stdafx.h"
  17. #include "common.h"
  18. #include "dsutil.h"
  19. #include "helpids.h"
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25. /////////////////////////////////////////////////////////////////////
  26. TCHAR szSchemaSim[] = _T("altSecurityIdentities"); // per Murlis 6/16/98
  27. /////////////////////////////////////////////////////////////////////
  28. void CSimEntry::SetString(CString& rstrData)
  29. {
  30. m_strData = rstrData;
  31. LPCTSTR pszT = (LPCTSTR)rstrData;
  32. if (_wcsnicmp(szX509, pszT, wcslen (szX509)) == 0)
  33. {
  34. m_eDialogTarget = eX509;
  35. }
  36. else if (_wcsnicmp(szKerberos, pszT, wcslen (szKerberos)) == 0)
  37. {
  38. m_eDialogTarget = eKerberos;
  39. }
  40. else
  41. {
  42. m_eDialogTarget = eOther;
  43. TRACE1("INFO: Unknown string type \"%s\".\n", pszT);
  44. }
  45. } // CSimEntry::SetString()
  46. /////////////////////////////////////////////////////////////////////
  47. /////////////////////////////////////////////////////////////////////
  48. CSimData::CSimData()
  49. : m_hwndParent (0)
  50. {
  51. m_fIsDirty = FALSE;
  52. m_pSimEntryList = NULL;
  53. m_paPage1 = new CSimX509PropPage;
  54. m_paPage1->m_pData = this;
  55. m_paPage2 = new CSimKerberosPropPage;
  56. m_paPage2->m_pData = this;
  57. #ifdef _DEBUG
  58. m_paPage3 = new CSimOtherPropPage;
  59. m_paPage3->m_pData = this;
  60. #endif
  61. }
  62. CSimData::~CSimData()
  63. {
  64. delete m_paPage1;
  65. delete m_paPage2;
  66. #ifdef _DEBUG
  67. delete m_paPage3;
  68. #endif
  69. FlushSimList();
  70. }
  71. /////////////////////////////////////////////////////////////////////
  72. void CSimData::FlushSimList()
  73. {
  74. // Delete the list
  75. CSimEntry * pSimEntry = m_pSimEntryList;
  76. while (pSimEntry != NULL)
  77. {
  78. CSimEntry * pSimEntryT = pSimEntry;
  79. pSimEntry = pSimEntry->m_pNext;
  80. delete pSimEntryT;
  81. }
  82. m_pSimEntryList = NULL;
  83. }
  84. /////////////////////////////////////////////////////////////////////
  85. BOOL CSimData::FInit(CString strUserPath, CString strADsIPath, HWND hwndParent)
  86. {
  87. m_hwndParent = hwndParent;
  88. m_strUserPath = strUserPath;
  89. m_strADsIPath = strADsIPath;
  90. if (!FQuerySimData())
  91. {
  92. ReportErrorEx (hwndParent,IDS_SIM_ERR_CANNOT_READ_SIM_DATA,S_OK,
  93. MB_OK | MB_ICONERROR, NULL, 0);
  94. return FALSE;
  95. }
  96. return TRUE;
  97. }
  98. /////////////////////////////////////////////////////////////////////
  99. // Return FALSE if some data could not be written.
  100. // Otherwise return TRUE.
  101. BOOL CSimData::FOnApply(HWND hwndParent)
  102. {
  103. if (!m_fIsDirty)
  104. return TRUE;
  105. HRESULT hr = FUpdateSimData ();
  106. if ( FAILED (hr) )
  107. {
  108. ReportErrorEx (hwndParent, IDS_SIM_ERR_CANNOT_WRITE_SIM_DATA, hr,
  109. MB_OK | MB_ICONERROR, NULL, 0);
  110. return FALSE;
  111. }
  112. // Re-load the data
  113. (void)FQuerySimData();
  114. // We have successfully written all the data
  115. m_fIsDirty = FALSE; // Clear the dirty bit
  116. return TRUE;
  117. }
  118. /////////////////////////////////////////////////////////////////////
  119. void CSimData::GetUserAccountName(OUT CString * pstrName)
  120. {
  121. ASSERT(pstrName != NULL);
  122. *pstrName = m_strUserPath;
  123. }
  124. /////////////////////////////////////////////////////////////////////
  125. // Query the database for the list of security identities.
  126. //
  127. // Return FALSE if an error occured.
  128. //
  129. BOOL CSimData::FQuerySimData()
  130. {
  131. CWaitCursor wait;
  132. FlushSimList();
  133. HRESULT hr;
  134. IADs * pADs = NULL;
  135. hr = DSAdminOpenObject(m_strADsIPath,
  136. IID_IADs,
  137. OUT (void **)&pADs,
  138. TRUE /*bServer*/);
  139. if (FAILED(hr))
  140. {
  141. ASSERT(pADs == NULL);
  142. return FALSE;
  143. }
  144. ASSERT(pADs != NULL);
  145. CComVariant vtData;
  146. // Read data from database
  147. hr = pADs->Get (CComBSTR (szSchemaSim), OUT &vtData);
  148. if (FAILED(hr))
  149. {
  150. if (hr == E_ADS_PROPERTY_NOT_FOUND)
  151. hr = S_OK;
  152. }
  153. else
  154. {
  155. CStringList stringlist;
  156. hr = HrVariantToStringList(IN vtData, OUT stringlist);
  157. if (FAILED(hr))
  158. goto End;
  159. POSITION pos = stringlist.GetHeadPosition();
  160. while (pos != NULL)
  161. {
  162. (void)PAddSimEntry(stringlist.GetNext(INOUT pos));
  163. } // while
  164. } // if...else
  165. End:
  166. if (pADs != NULL)
  167. pADs->Release();
  168. return SUCCEEDED(hr);
  169. } // FQuerySimData()
  170. /////////////////////////////////////////////////////////////////////
  171. // Update the list of security identities to the database.
  172. //
  173. // Return FALSE if an error occured.
  174. //
  175. HRESULT CSimData::FUpdateSimData()
  176. {
  177. CWaitCursor wait;
  178. IADs * pADs = NULL;
  179. HRESULT hr = DSAdminOpenObject(m_strADsIPath,
  180. IID_IADs,
  181. OUT (void **)&pADs,
  182. TRUE /*bServer*/);
  183. if (FAILED(hr))
  184. {
  185. ASSERT(pADs == NULL);
  186. // NTRAID# 448521 dsa.msc: Name Mapping Dlg: Buttons do nothing (and
  187. // other UI oddness) if an object is moved from "under" dlg
  188. // return FALSE;
  189. return hr;
  190. }
  191. ASSERT(pADs != NULL);
  192. // Build the string list
  193. CStringList stringlist;
  194. for (const CSimEntry * pSimEntry = m_pSimEntryList;
  195. pSimEntry != NULL;
  196. pSimEntry = pSimEntry->m_pNext)
  197. {
  198. switch (pSimEntry->m_eDialogTarget)
  199. {
  200. case eNone:
  201. ASSERT(FALSE && "Invalid Data");
  202. case eNil:
  203. case eOther:
  204. continue;
  205. } // switch
  206. stringlist.AddHead(pSimEntry->PchGetString());
  207. } // for
  208. CComVariant vtData;
  209. hr = HrStringListToVariant(OUT vtData, IN stringlist);
  210. if ( SUCCEEDED (hr) )
  211. {
  212. // Put data back to database
  213. hr = pADs->Put (CComBSTR (szSchemaSim), IN vtData);
  214. if ( SUCCEEDED (hr) )
  215. {
  216. // Persist the data (write to database)
  217. hr = pADs->SetInfo();
  218. }
  219. }
  220. if (pADs != NULL)
  221. pADs->Release();
  222. return hr;
  223. } // FUpdateSimData()
  224. /////////////////////////////////////////////////////////////////////
  225. // Allocate a new CSimEntry node to the linked list
  226. //
  227. CSimEntry * CSimData::PAddSimEntry(CString& rstrData)
  228. {
  229. CSimEntry * pSimEntry = new CSimEntry;
  230. if (pSimEntry)
  231. {
  232. pSimEntry->SetString(rstrData);
  233. pSimEntry->m_pNext = m_pSimEntryList;
  234. }
  235. m_pSimEntryList = pSimEntry;
  236. return pSimEntry;
  237. }
  238. /////////////////////////////////////////////////////////////////////
  239. void CSimData::DeleteSimEntry(CSimEntry * pSimEntryDelete)
  240. {
  241. CSimEntry * p = m_pSimEntryList;
  242. CSimEntry * pPrev = NULL;
  243. while (p != NULL)
  244. {
  245. if (p == pSimEntryDelete)
  246. {
  247. if (pPrev == NULL)
  248. {
  249. ASSERT(pSimEntryDelete == m_pSimEntryList);
  250. m_pSimEntryList = p->m_pNext;
  251. }
  252. else
  253. {
  254. pPrev->m_pNext = p->m_pNext;
  255. }
  256. delete pSimEntryDelete;
  257. return;
  258. }
  259. pPrev = p;
  260. p = p->m_pNext;
  261. }
  262. TRACE0("ERROR: CSimData::DeleteSimEntry() - Node not found.\n");
  263. } // DeleteSimEntry()
  264. /////////////////////////////////////////////////////////////////////
  265. void CSimData::AddEntriesToListview(HWND hwndListview, DIALOG_TARGET_ENUM eDialogTarget)
  266. {
  267. CSimEntry * pSimEntry = m_pSimEntryList;
  268. while (pSimEntry != NULL)
  269. {
  270. if (pSimEntry->m_eDialogTarget == eDialogTarget)
  271. {
  272. ListView_AddString(hwndListview, pSimEntry->PchGetString());
  273. }
  274. pSimEntry = pSimEntry->m_pNext;
  275. }
  276. } // AddEntriesToListview()
  277. /////////////////////////////////////////////////////////////////////
  278. void CSimData::DoModal()
  279. {
  280. CThemeContextActivator activator;
  281. CWnd parentWnd;
  282. VERIFY (parentWnd.Attach (m_hwndParent));
  283. CSimPropertySheet ps(IDS_SIM_SECURITY_IDENTITY_MAPPING, &parentWnd);
  284. ps.AddPage(m_paPage1);
  285. ps.AddPage(m_paPage2);
  286. #ifdef _DEBUG
  287. ps.AddPage(m_paPage3);
  288. #endif
  289. (void)ps.DoModal();
  290. parentWnd.Detach ();
  291. }
  292. /////////////////////////////////////////////////////////////////////////////
  293. // CSimPropertySheet
  294. IMPLEMENT_DYNAMIC(CSimPropertySheet, CPropertySheet)
  295. CSimPropertySheet::CSimPropertySheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
  296. :CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
  297. {
  298. }
  299. CSimPropertySheet::CSimPropertySheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
  300. :CPropertySheet(pszCaption, pParentWnd, iSelectPage)
  301. {
  302. }
  303. CSimPropertySheet::~CSimPropertySheet()
  304. {
  305. }
  306. BEGIN_MESSAGE_MAP(CSimPropertySheet, CPropertySheet)
  307. //{{AFX_MSG_MAP(CSimPropertySheet)
  308. // NOTE - the ClassWizard will add and remove mapping macros here.
  309. //}}AFX_MSG_MAP
  310. ON_MESSAGE(WM_HELP, OnHelp)
  311. END_MESSAGE_MAP()
  312. /////////////////////////////////////////////////////////////////////////////
  313. // CSimPropertySheet message handlers
  314. BOOL CSimPropertySheet::OnInitDialog()
  315. {
  316. BOOL bResult = CPropertySheet::OnInitDialog();
  317. DWORD dwStyle = GetWindowLong (m_hWnd, GWL_STYLE);
  318. dwStyle |= DS_CONTEXTHELP;
  319. SetWindowLong (m_hWnd, GWL_STYLE, dwStyle);
  320. DWORD dwExStyle = GetWindowLong (m_hWnd, GWL_EXSTYLE);
  321. dwExStyle |= WS_EX_CONTEXTHELP;
  322. SetWindowLong (m_hWnd, GWL_EXSTYLE, dwExStyle);
  323. return bResult;
  324. }
  325. BOOL CSimPropertySheet::OnHelp(WPARAM /*wParam*/, LPARAM lParam)
  326. {
  327. const LPHELPINFO pHelpInfo = (LPHELPINFO)lParam;
  328. if (pHelpInfo && pHelpInfo->iContextType == HELPINFO_WINDOW)
  329. {
  330. DoContextHelp ((HWND) pHelpInfo->hItemHandle);
  331. }
  332. return TRUE;
  333. }
  334. void CSimPropertySheet::DoContextHelp (HWND hWndControl)
  335. {
  336. const int IDC_COMM_APPLYNOW = 12321;
  337. const int IDH_COMM_APPLYNOW = 28447;
  338. const DWORD aHelpIDs_PropSheet[]=
  339. {
  340. IDC_COMM_APPLYNOW, IDH_COMM_APPLYNOW,
  341. 0, 0
  342. };
  343. if ( !::WinHelp (
  344. hWndControl,
  345. IDC_COMM_APPLYNOW == ::GetDlgCtrlID (hWndControl) ?
  346. L"windows.hlp" : DSADMIN_CONTEXT_HELP_FILE,
  347. HELP_WM_HELP,
  348. (DWORD_PTR) aHelpIDs_PropSheet) )
  349. {
  350. TRACE1 ("WinHelp () failed: 0x%x\n", GetLastError ());
  351. }
  352. }