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.

521 lines
15 KiB

  1. // CertCtl.cpp : Implementation of the CCertmapCtrl OLE control class.
  2. #include "stdafx.h"
  3. #include "certmap.h"
  4. #include "CertCtl.h"
  5. #include "CertPpg.h"
  6. extern "C"
  7. {
  8. #include <wincrypt.h>
  9. #include <schannel.h>
  10. }
  11. // persistence and mapping includes
  12. #include "Iismap.hxx"
  13. #include "Iiscmr.hxx"
  14. #include "WrapMaps.h"
  15. #include "ListRow.h"
  16. #include "ChkLstCt.h"
  17. #include "wrapmb.h"
  18. #include "Map11Pge.h"
  19. #include "MapWPge.h"
  20. //#include <iiscnfg.h>
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26. IMPLEMENT_DYNCREATE(CCertmapCtrl, COleControl)
  27. /////////////////////////////////////////////////////////////////////////////
  28. // Message map
  29. BEGIN_MESSAGE_MAP(CCertmapCtrl, COleControl)
  30. //{{AFX_MSG_MAP(CCertmapCtrl)
  31. //}}AFX_MSG_MAP
  32. ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  33. END_MESSAGE_MAP()
  34. /////////////////////////////////////////////////////////////////////////////
  35. // Dispatch map
  36. BEGIN_DISPATCH_MAP(CCertmapCtrl, COleControl)
  37. //{{AFX_DISPATCH_MAP(CCertmapCtrl)
  38. DISP_FUNCTION(CCertmapCtrl, "SetServerInstance", SetServerInstance, VT_EMPTY, VTS_BSTR)
  39. DISP_FUNCTION(CCertmapCtrl, "SetMachineName", SetMachineName, VT_EMPTY, VTS_BSTR)
  40. DISP_STOCKFUNC_DOCLICK()
  41. DISP_STOCKPROP_FONT()
  42. DISP_STOCKPROP_ENABLED()
  43. DISP_STOCKPROP_BORDERSTYLE()
  44. DISP_STOCKPROP_CAPTION()
  45. //}}AFX_DISPATCH_MAP
  46. END_DISPATCH_MAP()
  47. /////////////////////////////////////////////////////////////////////////////
  48. // Event map
  49. BEGIN_EVENT_MAP(CCertmapCtrl, COleControl)
  50. //{{AFX_EVENT_MAP(CCertmapCtrl)
  51. EVENT_STOCK_CLICK()
  52. EVENT_STOCK_KEYUP()
  53. //}}AFX_EVENT_MAP
  54. END_EVENT_MAP()
  55. /////////////////////////////////////////////////////////////////////////////
  56. // Property pages
  57. // TODO: Add more property pages as needed. Remember to increase the count!
  58. BEGIN_PROPPAGEIDS(CCertmapCtrl, 2)
  59. PROPPAGEID(CCertmapPropPage::guid)
  60. PROPPAGEID(CLSID_CFontPropPage)
  61. END_PROPPAGEIDS(CCertmapCtrl)
  62. /////////////////////////////////////////////////////////////////////////////
  63. // Initialize class factory and guid
  64. IMPLEMENT_OLECREATE_EX(CCertmapCtrl, "CERTMAP.CertmapCtrl.1",
  65. 0xbbd8f29b, 0x6f61, 0x11d0, 0xa2, 0x6e, 0x8, 0, 0x2b, 0x2c, 0x6f, 0x32)
  66. /////////////////////////////////////////////////////////////////////////////
  67. // Type library ID and version
  68. IMPLEMENT_OLETYPELIB(CCertmapCtrl, _tlid, _wVerMajor, _wVerMinor)
  69. /////////////////////////////////////////////////////////////////////////////
  70. // Interface IDs
  71. const IID BASED_CODE IID_DCertmap =
  72. { 0xbbd8f299, 0x6f61, 0x11d0, { 0xa2, 0x6e, 0x8, 0, 0x2b, 0x2c, 0x6f, 0x32 } };
  73. const IID BASED_CODE IID_DCertmapEvents =
  74. { 0xbbd8f29a, 0x6f61, 0x11d0, { 0xa2, 0x6e, 0x8, 0, 0x2b, 0x2c, 0x6f, 0x32 } };
  75. /////////////////////////////////////////////////////////////////////////////
  76. // Control type information
  77. static const DWORD BASED_CODE _dwCertmapOleMisc =
  78. OLEMISC_ACTIVATEWHENVISIBLE |
  79. OLEMISC_SETCLIENTSITEFIRST |
  80. OLEMISC_INSIDEOUT |
  81. OLEMISC_CANTLINKINSIDE |
  82. OLEMISC_ACTSLIKEBUTTON |
  83. OLEMISC_RECOMPOSEONRESIZE;
  84. IMPLEMENT_OLECTLTYPE(CCertmapCtrl, IDS_CERTMAP, _dwCertmapOleMisc)
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CCertmapCtrl::CCertmapCtrlFactory::UpdateRegistry -
  87. // Adds or removes system registry entries for CCertmapCtrl
  88. BOOL CCertmapCtrl::CCertmapCtrlFactory::UpdateRegistry(BOOL bRegister)
  89. {
  90. // TODO: Verify that your control follows apartment-model threading rules.
  91. // Refer to MFC TechNote 64 for more information.
  92. // If your control does not conform to the apartment-model rules, then
  93. // you must modify the code below, changing the 6th parameter from
  94. // afxRegApartmentThreading to 0.
  95. if (bRegister)
  96. return AfxOleRegisterControlClass(
  97. AfxGetInstanceHandle(),
  98. m_clsid,
  99. m_lpszProgID,
  100. IDS_CERTMAP,
  101. IDB_CERTMAP,
  102. afxRegApartmentThreading,
  103. _dwCertmapOleMisc,
  104. _tlid,
  105. _wVerMajor,
  106. _wVerMinor);
  107. else
  108. return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  109. }
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CCertmapCtrl::CCertmapCtrl - Constructor
  112. CCertmapCtrl::CCertmapCtrl():
  113. m_fUpdateFont( FALSE ),
  114. m_hAccel( NULL ),
  115. m_cAccel( 0 )
  116. {
  117. InitializeIIDs(&IID_DCertmap, &IID_DCertmapEvents);
  118. }
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CCertmapCtrl::~CCertmapCtrl - Destructor
  121. CCertmapCtrl::~CCertmapCtrl()
  122. {
  123. if ( m_hAccel )
  124. DestroyAcceleratorTable( m_hAccel );
  125. m_hAccel = NULL;
  126. }
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CCertmapCtrl::OnDraw - Drawing function
  129. void CCertmapCtrl::OnDraw( CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid )
  130. {
  131. DoSuperclassPaint(pdc, rcBounds);
  132. /*
  133. CFont* pOldFont;
  134. // select the stock font, recording the old one
  135. pOldFont = SelectStockFont( pdc );
  136. // do the superclass draw
  137. DoSuperclassPaint(pdc, rcBounds);
  138. // restore the old font - sneakily getting the correct font object
  139. pOldFont = pdc->SelectObject(pOldFont);
  140. // we want the button window to continue drawing in the correct font even
  141. // when we are not using OnDraw. i.e. when it is being pushed down. This
  142. // means we need to set the CWnd::SetFont() method.
  143. if ( m_fUpdateFont )
  144. {
  145. m_fUpdateFont = FALSE;
  146. CWnd::SetFont( pOldFont );
  147. }
  148. DoSuperclassPaint(pdc, rcBounds);
  149. */
  150. }
  151. /////////////////////////////////////////////////////////////////////////////
  152. // CCertmapCtrl::DoPropExchange - Persistence support
  153. void CCertmapCtrl::DoPropExchange( CPropExchange* pPX )
  154. {
  155. ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  156. COleControl::DoPropExchange(pPX);
  157. }
  158. /////////////////////////////////////////////////////////////////////////////
  159. // CCertmapCtrl::OnResetState - Reset control to default state
  160. void CCertmapCtrl::OnResetState()
  161. {
  162. COleControl::OnResetState(); // Resets defaults found in DoPropExchange
  163. }
  164. /////////////////////////////////////////////////////////////////////////////
  165. // CCertmapCtrl message handlers
  166. //---------------------------------------------------------------------------
  167. BOOL CCertmapCtrl::PreCreateWindow(CREATESTRUCT& cs)
  168. {
  169. if ( cs.style & WS_CLIPSIBLINGS )
  170. cs.style ^= WS_CLIPSIBLINGS;
  171. cs.lpszClass = _T("BUTTON");
  172. return COleControl::PreCreateWindow(cs);
  173. }
  174. /////////////////////////////////////////////////////////////////////////////
  175. // CAppsCtrl::IsSubclassedControl - This is a subclassed control
  176. BOOL CCertmapCtrl::IsSubclassedControl()
  177. {
  178. return TRUE;
  179. }
  180. /////////////////////////////////////////////////////////////////////////////
  181. // CAppsCtrl::OnOcmCommand - Handle command messages
  182. LRESULT CCertmapCtrl::OnOcmCommand(WPARAM wParam, LPARAM lParam)
  183. {
  184. #ifdef _WIN32
  185. WORD wNotifyCode = HIWORD(wParam);
  186. #else
  187. WORD wNotifyCode = HIWORD(lParam);
  188. #endif
  189. return 0;
  190. }
  191. //---------------------------------------------------------------------------
  192. void CCertmapCtrl::OnClick(USHORT iButton)
  193. {
  194. // in case there are any errors, prepare the error string
  195. CString sz;
  196. sz.LoadString( IDS_ERR_CERTMAP_TITLE );
  197. // free the existing name, and copy in the new one
  198. // tjp: you should compare if the old name matches the current name
  199. // and only then free and malloc the new name -- chances are that
  200. // the names are the same +++ all the free/malloc can fragment mem.
  201. free((void*)AfxGetApp()->m_pszAppName);
  202. AfxGetApp()->m_pszAppName = _tcsdup(sz);
  203. // this is the whole purpose of the control
  204. RunMappingDialog();
  205. // we are not in the business of telling the host to do
  206. // something here, so just don't fire anything off.
  207. COleControl::OnClick(iButton);
  208. }
  209. //---------------------------------------------------------------------------
  210. void CCertmapCtrl::RunMappingDialog()
  211. {
  212. //
  213. // UNICODE/ANSI conversion - RonaldM
  214. //
  215. // prepare the machine name pointer
  216. USES_CONVERSION;
  217. OLECHAR * poch = NULL;
  218. if ( !m_szMachineName.IsEmpty() )
  219. {
  220. // allocate the name buffer, no need to free
  221. poch = T2OLE((LPTSTR)(LPCTSTR)m_szMachineName);
  222. if ( !poch )
  223. {
  224. MessageBeep(0);
  225. return;
  226. }
  227. }
  228. // initialize the metabase wrappings - pass in the name of the target machine
  229. // if one has been specified
  230. //
  231. // Changed to generic metabase wrapper class - RonaldM
  232. //
  233. //IMSAdminBase * pMB = FInitMetabaseWrapper( poch );
  234. //if ( !pMB )
  235. IMSAdminBase * pMB = NULL;
  236. if (!FInitMetabaseWrapperEx( poch, &pMB ))
  237. {
  238. MessageBeep(0);
  239. return;
  240. }
  241. // the 1:1 mapping and rule-based mapping are panes in a single dialog window.
  242. // first we must build the propertysheet dialog and add the panes
  243. // pointers to the pages (construction may throw, so we need to be careful)
  244. CMap11Page page11mapping;
  245. CMapWildcardsPge pageWildMapping;
  246. // declare the property sheet
  247. CPropertySheet propsheet( IDS_MAP_SHEET_TITLE );
  248. // Things could throw here, so better protect it.
  249. try
  250. {
  251. // if there is nothing in the MB_Path, default to the first instance
  252. if ( m_szServerInstance.IsEmpty() )
  253. m_szServerInstance = _T("/LM/W3SVC/1");
  254. // I am assuming that the last character is NOT a '/' Thus, if that is what is
  255. // there, we need to remove it. Otherwise, the path gets messed up later
  256. if ( m_szServerInstance.Right(1) == _T('/') )
  257. m_szServerInstance = m_szServerInstance.Left( m_szServerInstance.GetLength()-1 );
  258. // tell the pages about the metabase path property
  259. page11mapping.m_szMBPath = m_szServerInstance;
  260. pageWildMapping.m_szMBPath = m_szServerInstance;
  261. // do any other initializing of the pages
  262. page11mapping.FInit(pMB);
  263. pageWildMapping.FInit(pMB);
  264. }
  265. catch ( CException * pException )
  266. {
  267. pException->Delete();
  268. }
  269. // add the pages to the sheet
  270. propsheet.AddPage( &page11mapping );
  271. propsheet.AddPage( &pageWildMapping );
  272. // turn on help
  273. propsheet.m_psh.dwFlags |= PSH_HASHELP;
  274. page11mapping.m_psp.dwFlags |= PSP_HASHELP;
  275. pageWildMapping.m_psp.dwFlags |= PSP_HASHELP;
  276. // Things could (potentially maybe) throw here, so better protect it.
  277. try
  278. {
  279. // run the propdsheet dialog
  280. // let the host container know that we are putting up a modal dialog
  281. PreModalDialog();
  282. // run the dialog
  283. // tjp: should we not test the outcome of the dialog?
  284. // could the user ESCAPE out of it w/o doing anything?
  285. propsheet.DoModal();
  286. // let the host container know we are done with the modality
  287. PostModalDialog();
  288. }
  289. catch ( CException * pException )
  290. {
  291. pException->Delete();
  292. }
  293. // close the metabase wrappings
  294. //
  295. // Changed to generic wrapper -- RonaldM
  296. FCloseMetabaseWrapperEx(&pMB);
  297. }
  298. //---------------------------------------------------------------------------
  299. void CCertmapCtrl::SetServerInstance(LPCTSTR szServerInstance)
  300. {
  301. m_szServerInstance = szServerInstance;
  302. }
  303. //---------------------------------------------------------------------------
  304. void CCertmapCtrl::SetMachineName(LPCTSTR szMachine)
  305. {
  306. m_szMachineName = szMachine;
  307. }
  308. //---------------------------------------------------------------------------
  309. void CCertmapCtrl::OnFontChanged()
  310. {
  311. m_fUpdateFont = TRUE;
  312. COleControl::OnFontChanged();
  313. }
  314. //---------------------------------------------------------------------------
  315. void CCertmapCtrl::OnAmbientPropertyChange(DISPID dispid)
  316. {
  317. BOOL flag;
  318. UINT style;
  319. // do the right thing depending on the dispid
  320. switch ( dispid )
  321. {
  322. case DISPID_AMBIENT_DISPLAYASDEFAULT:
  323. if ( GetAmbientProperty( DISPID_AMBIENT_DISPLAYASDEFAULT, VT_BOOL, &flag ) )
  324. {
  325. style = GetWindowLong(
  326. GetSafeHwnd(), // handle of window
  327. GWL_STYLE // offset of value to retrieve
  328. );
  329. if ( flag )
  330. style |= BS_DEFPUSHBUTTON;
  331. else
  332. style ^= BS_DEFPUSHBUTTON;
  333. SetWindowLong(
  334. GetSafeHwnd(), // handle of window
  335. GWL_STYLE, // offset of value to retrieve
  336. style
  337. );
  338. Invalidate(TRUE);
  339. }
  340. break;
  341. };
  342. COleControl::OnAmbientPropertyChange(dispid);
  343. }
  344. //---------------------------------------------------------------------------
  345. // an important method where we tell the container how to deal with us.
  346. // pControlInfo is passed in by the container, although we are responsible
  347. // for maintining the hAccel structure
  348. void CCertmapCtrl::OnGetControlInfo(LPCONTROLINFO pControlInfo)
  349. {
  350. // do a rudimentary check to see if we understand pControlInfo
  351. if ( !pControlInfo || pControlInfo->cb < sizeof(CONTROLINFO) )
  352. return;
  353. // set the accelerator handle into place
  354. pControlInfo->hAccel = m_hAccel;
  355. pControlInfo->cAccel = m_cAccel;
  356. // when we have focus, we do want the enter key
  357. pControlInfo->dwFlags = CTRLINFO_EATS_RETURN;
  358. }
  359. //---------------------------------------------------------------------------
  360. // the ole control container object specifically filters out the space
  361. // key so we do not get it as a OnMnemonic call. Thus we need to look
  362. // for it ourselves
  363. void CCertmapCtrl::OnKeyUpEvent(USHORT nChar, USHORT nShiftState)
  364. {
  365. if ( nChar == _T(' ') )
  366. {
  367. OnClick((USHORT)GetDlgCtrlID());
  368. }
  369. COleControl::OnKeyUpEvent(nChar, nShiftState);
  370. }
  371. //---------------------------------------------------------------------------
  372. void CCertmapCtrl::OnMnemonic(LPMSG pMsg)
  373. {
  374. OnClick((USHORT)GetDlgCtrlID());
  375. COleControl::OnMnemonic(pMsg);
  376. }
  377. //---------------------------------------------------------------------------
  378. void CCertmapCtrl::OnTextChanged()
  379. {
  380. DWORD i;
  381. ACCEL accel;
  382. BOOL f;
  383. BOOL flag;
  384. int iAccel;
  385. // get the new text
  386. CString sz = InternalGetText();
  387. sz.MakeLower();
  388. // if the handle has already been allocated, free it
  389. if ( m_hAccel )
  390. {
  391. DestroyAcceleratorTable( m_hAccel );
  392. m_hAccel = NULL;
  393. m_cAccel = 0;
  394. }
  395. // if there is a & character, then declare the accelerator
  396. iAccel = sz.Find(_T('&'));
  397. if ( iAccel >= 0 )
  398. {
  399. // fill in the accererator record
  400. accel.fVirt = FALT;
  401. accel.key = sz.GetAt(iAccel + 1);
  402. accel.cmd = (USHORT)GetDlgCtrlID();
  403. m_hAccel = CreateAcceleratorTable( &accel, 1 );
  404. if ( m_hAccel )
  405. m_cAccel = 1;
  406. }
  407. // finish with the default handling.
  408. COleControl::OnTextChanged();
  409. }