MainFrm.cpp
上传用户:jinandeyu
上传日期:2007-01-05
资源大小:620k
文件大小:25k
源码类别:

远程控制编程

开发平台:

WINDOWS

  1. /*  Back Orifice 2000 - Remote Administration Suite
  2.     Copyright (C) 1999, Cult Of The Dead Cow
  3.     This program is free software; you can redistribute it and/or modify
  4.     it under the terms of the GNU General Public License as published by
  5.     the Free Software Foundation; either version 2 of the License, or
  6.     (at your option) any later version.
  7.     This program is distributed in the hope that it will be useful,
  8.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.     GNU General Public License for more details.
  11.     You should have received a copy of the GNU General Public License
  12.     along with this program; if not, write to the Free Software
  13.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  14. The author of this program may be contacted at dildog@l0pht.com. */
  15. // MainFrm.cpp : implementation of the CMainFrame class
  16. //
  17. #include "stdafx.h"
  18. #include "bo2kgui.h"
  19. #include "MainFrm.h"
  20. #include "pluginconfig.h"
  21. #include <auth.h>
  22. #include <nullauth.h>
  23. #include <iohandler.h>
  24. #include <io_simpleudp.h>
  25. #include <io_simpletcp.h>
  26. #include <encryption.h>
  27. #include <xorencrypt.h>
  28. #include <deshash.h>
  29. #include <dll_load.h>
  30. #include <plugins.h>
  31. #include <interactiveconnect.h>
  32. #include <interactivelisten.h>
  33. #include "Splash.h"
  34. #ifdef _DEBUG
  35. #define new DEBUG_NEW
  36. #undef THIS_FILE
  37. static char THIS_FILE[] = __FILE__;
  38. #endif
  39. #define WM_CDIALOG_DO_MODAL (WM_USER+123)
  40. #define WM_TASKBAR_CALLBACK (WM_USER+124)
  41. typedef struct {
  42. CDialog *pDialog;
  43. int nRet;
  44. HANDLE hTrigger;
  45. } CDIALOGDOMODALPARAM;
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CMainFrame
  48. IMPLEMENT_DYNCREATE(CMainFrame, CCJFrameWnd)
  49. BEGIN_MESSAGE_MAP(CMainFrame, CCJFrameWnd)
  50. //{{AFX_MSG_MAP(CMainFrame)
  51. ON_WM_CREATE()
  52. ON_COMMAND(IDM_SERVERLIST, OnServerlist)
  53. ON_UPDATE_COMMAND_UI(IDM_SERVERLIST, OnUpdateServerlist)
  54. ON_WM_ERASEBKGND()
  55. ON_COMMAND(IDM_PLUGINS, OnPlugins)
  56. ON_COMMAND(IDM_GRADIENTS, OnGradients)
  57. ON_UPDATE_COMMAND_UI(IDM_GRADIENTS, OnUpdateGradients)
  58. ON_COMMAND(IDM_MENUTYPE, OnMenuType)
  59. ON_UPDATE_COMMAND_UI(IDM_MENUTYPE, OnUpdateMenuType)
  60. ON_COMMAND(ID_SERVER_NEW, OnServerNew)
  61. ON_UPDATE_COMMAND_UI(ID_SERVER_NEW, OnUpdateServerNew)
  62. ON_COMMAND(ID_SERVER_EDIT, OnServerEdit)
  63. ON_UPDATE_COMMAND_UI(ID_SERVER_EDIT, OnUpdateServerEdit)
  64. ON_COMMAND(ID_SERVER_DELETE, OnServerDelete)
  65. ON_UPDATE_COMMAND_UI(ID_SERVER_DELETE, OnUpdateServerDelete)
  66. ON_WM_ACTIVATE()
  67. // ON_COMMAND(IDM_SYSTRAY, OnSysTray)
  68. // ON_UPDATE_COMMAND_UI(IDM_SYSTRAY, OnUpdateSysTray)
  69. // ON_MESSAGE(WM_TASKBAR_CALLBACK, OnTrayNotification)
  70. ON_COMMAND_RANGE(IDM_PLUGIN_0,IDM_PLUGIN_4095,OnPluginMenu)
  71. ON_WM_TIMER()
  72. ON_WM_SHOWWINDOW()
  73. //}}AFX_MSG_MAP
  74. END_MESSAGE_MAP()
  75. static UINT indicators[] =
  76. {
  77. ID_SEPARATOR,           // status line indicator
  78. ID_INDICATOR_CAPS,
  79. ID_INDICATOR_NUM,
  80. ID_INDICATOR_SCRL,
  81. };
  82. /////////////////////////////////////////////////////////////////////////////
  83. // The global handler objects
  84. CIOHandler *g_pIOHandler;
  85. CEncryptionHandler *g_pEncryptionHandler;
  86. CAuthHandler *g_pAuthHandler;
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CMainFrame construction/destruction
  89. CMainFrame::CMainFrame()
  90. {
  91. // TODO: add member initialization code here
  92. m_pWndServerList=NULL;
  93. m_bGradients=FALSE;
  94. // m_bSysTray=FALSE;
  95. // m_pTrayIcon=NULL;
  96. g_pIOHandler=NULL;
  97. g_pEncryptionHandler=NULL;
  98. g_pAuthHandler=NULL;
  99. m_arrPluginInfo.RemoveAll();
  100. m_arrVarInfo.RemoveAll();
  101. }
  102. CMainFrame::~CMainFrame()
  103. {
  104. }
  105. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  106. {
  107. if (CCJFrameWnd::OnCreate(lpCreateStruct) == -1)
  108. return -1;
  109. if (!m_wndStatusBar.Create(this) ||
  110. !m_wndStatusBar.SetIndicators(indicators,
  111.   sizeof(indicators)/sizeof(UINT)))
  112. {
  113. TRACE0("Failed to create status barn");
  114. return -1;      // fail to create
  115. }
  116. // Get config
  117. m_bGradients=AfxGetApp()->GetProfileInt("Config","Gradients",FALSE);
  118. m_bScreenReader=AfxGetApp()->GetProfileInt("Config","Compatible Menus",TRUE);
  119. // m_bSysTray=AfxGetApp()->GetProfileInt("Config","System Tray",0);
  120. // Start up toolbars and docking windows
  121. CSize defaultSize(GetSystemMetrics(SM_CXSCREEN)/4,GetSystemMetrics(SM_CYSCREEN)/5);
  122. SetInitialSize(defaultSize.cy,defaultSize.cy,defaultSize.cx,defaultSize.cx);
  123. EnableDocking(CBRS_ALIGN_ANY);
  124. EnableDockingSizeBar(CBRS_ALIGN_ANY);
  125. if(InitializeToolbars()==-1) return FALSE;
  126. if(InitializeDockingWindows()==-1) return FALSE;
  127. // Load up built-in IO and plugins
  128. if(InitializePlugins()==-1) return FALSE;
  129. CSplash spl;
  130. spl.DoModal();
  131. return 0;
  132. }
  133. int CMainFrame::InitializeToolbars()
  134. {
  135. // Create main toolbar
  136. if (!m_wndTBMain.Create(this) ||
  137. !m_wndTBMain.LoadToolBar(IDR_MAINFRAME)) {
  138. TRACE0("Failed to create main toolbarn");
  139. return -1;      // fail to create
  140. }
  141. m_wndTBMain.SetWindowText(_T("Main toolbar"));
  142. m_wndTBMain.SetBarStyle(m_wndTBMain.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  143. m_wndTBMain.EnableDocking(CBRS_ALIGN_ANY);
  144. m_wndTBMain.SetButtonSize(CSize(24,22));
  145. // Create main menu bar
  146. m_menuBar.m_bAutoRemoveFrameMenu=FALSE;
  147. m_menuBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP, IDR_MAINFRAME);
  148. m_menuBar.LoadMenu(GetMenu()->GetSafeHmenu());
  149. m_menuBar.SetWindowText(_T("Menu"));
  150. m_menuBar.SetBarStyle(m_menuBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  151. m_menuBar.EnableDocking(CBRS_ALIGN_TOP | CBRS_ALIGN_BOTTOM);
  152. m_menuBar.SetButtonSize(CSize(24,22));
  153. m_menuManager.Install(this);
  154. m_menuManager.LoadToolbar(IDR_MAINFRAME);
  155. if(m_bScreenReader) {
  156. m_menuManager.m_bShowButtons=FALSE;
  157. m_menuManager.Refresh();
  158. m_menuBar.ShowWindow(SW_HIDE);
  159. DockControlBar(&m_wndTBMain);
  160. } else {
  161. m_menuManager.m_bShowButtons=TRUE;
  162. m_menuManager.Refresh();
  163. m_menuBar.ShowWindow(SW_SHOW);
  164. SetMenu(NULL);
  165. DockControlBar(&m_menuBar);
  166. DockControlBarLeftOf(&m_wndTBMain,&m_menuBar);
  167. DockControlBar(&m_wndTBMain);
  168. }
  169. return 0;
  170. }
  171. int CMainFrame::InitializeDockingWindows()
  172. {
  173. // Create workspace dockbar
  174. CString title("Server List");
  175. if (!m_wndServerListBDB.Create(this,  IDD_SERVERLIST, title)) {
  176. TRACE0("Failed to create dialogbarn");
  177. return -1;      // fail to create
  178. }
  179. m_wndServerListBDB.SetBarStyle((m_wndServerListBDB.GetBarStyle() & ~CBRS_ALIGN_ANY) |
  180. CBRS_TOOLTIPS | CBRS_BOTTOM | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  181. m_wndServerListBDB.EnableDockingOnSizeBar(CBRS_ALIGN_ANY);
  182. m_pWndServerList=new CServerList();
  183. m_pWndServerList->Create(CServerList::IDD,&m_wndServerListBDB);
  184. m_wndServerListBDB.SetDialog(m_pWndServerList);
  185. m_pWndServerList->UseGradient(m_bGradients);
  186. DockSizeBar(&m_wndServerListBDB);
  187. CMenu menu;
  188. menu.Attach(m_menuBar.GetMenu());
  189. menu.CheckMenuItem(IDD_SERVERLIST, MF_CHECKED);
  190. menu.Detach();
  191. return 0;
  192. }
  193. /////////////////////////////////////////////////////////////////////////////
  194. // CMainFrame diagnostics
  195. #ifdef _DEBUG
  196. void CMainFrame::AssertValid() const
  197. {
  198. CCJFrameWnd::AssertValid();
  199. }
  200. void CMainFrame::Dump(CDumpContext& dc) const
  201. {
  202. CCJFrameWnd::Dump(dc);
  203. }
  204. #endif //_DEBUG
  205. /////////////////////////////////////////////////////////////////////////////
  206. // CMainFrame message handlers
  207. void CMainFrame::OnServerlist() 
  208. {
  209. if(m_wndServerListBDB.IsWindowVisible()) {
  210. ShowControlBar(&m_wndServerListBDB, FALSE, FALSE);
  211. } else {
  212. ShowControlBar(&m_wndServerListBDB, TRUE, FALSE);
  213. }
  214. }
  215. void CMainFrame::OnUpdateServerlist(CCmdUI* pCmdUI) 
  216. {
  217. if(m_wndServerListBDB.IsWindowVisible()) {
  218. pCmdUI->SetCheck(1);
  219. } else {
  220. pCmdUI->SetCheck(0);
  221. }
  222. }
  223. BOOL CMainFrame::DestroyWindow() 
  224. {
  225. // Clean up server list
  226. if(m_pWndServerList) {
  227. m_pWndServerList->CleanUpAndClose();
  228. delete m_pWndServerList;
  229. m_pWndServerList=NULL;
  230. }
  231. // Kill all the plugins
  232. TerminatePlugins();
  233. // AfxGetApp()->WriteProfileInt("Config","System Tray",m_bSysTray);
  234. AfxGetApp()->WriteProfileInt("Config","Compatible Menus",m_bScreenReader);
  235. AfxGetApp()->WriteProfileInt("Config","Gradients",m_bGradients);
  236. // Delete the tray icon
  237. // if(m_pTrayIcon) {
  238. // delete m_pTrayIcon;
  239. // m_pTrayIcon=NULL;
  240. // }
  241. return CCJFrameWnd::DestroyWindow();
  242. }
  243. BOOL CMainFrame::OnEraseBkgnd(CDC* pDC) 
  244. {
  245. return TRUE;
  246. //return CCJFrameWnd::OnEraseBkgnd(pDC);
  247. }
  248. void CMainFrame::OnPlugins() 
  249. {
  250. CPluginConfig cpg;
  251. cpg.DoModal();
  252. }
  253. void CMainFrame::OnGradients() 
  254. {
  255. m_bGradients=!m_bGradients;
  256. m_pWndServerList->UseGradient(m_bGradients);
  257. }
  258. void CMainFrame::OnUpdateGradients(CCmdUI* pCmdUI) 
  259. {
  260. if(m_bGradients) pCmdUI->SetCheck(1);
  261. else pCmdUI->SetCheck(0);
  262. }
  263. void CMainFrame::OnMenuType() 
  264. {
  265. m_bScreenReader=!m_bScreenReader;
  266. if(m_bScreenReader) {
  267. m_menuManager.m_bShowButtons=FALSE;
  268. m_menuManager.Refresh();
  269. DockControlBar(&m_menuBar);
  270. m_menuBar.ShowWindow(FALSE);
  271. SetMenu(CMenu::FromHandle(m_menuBar.GetMenu()));
  272. FloatControlBar(&m_wndTBMain,CPoint(0,0));
  273. DockControlBar(&m_wndTBMain);
  274. } else {
  275. m_menuManager.m_bShowButtons=TRUE;
  276. m_menuManager.Refresh();
  277. SetMenu(NULL);
  278. m_menuBar.ShowWindow(TRUE);
  279. DockControlBar(&m_menuBar);
  280. DockControlBarLeftOf(&m_wndTBMain,&m_menuBar);
  281. DockControlBar(&m_wndTBMain);
  282. }
  283. }
  284. void CMainFrame::OnUpdateMenuType(CCmdUI* pCmdUI) 
  285. {
  286. if(m_bScreenReader)
  287. pCmdUI->SetCheck(1);
  288. else 
  289. pCmdUI->SetCheck(0);
  290. }
  291. void CMainFrame::OnServerNew()
  292. {
  293. m_pWndServerList->OnNewmachine();
  294. }
  295. void CMainFrame::OnUpdateServerNew(CCmdUI* pCmdUI)
  296. {
  297. pCmdUI->Enable(m_pWndServerList->GetDlgItem(ID_NEWMACHINE)->IsWindowEnabled());
  298. }
  299. void CMainFrame::OnServerEdit()
  300. {
  301. m_pWndServerList->OnEditmachine();
  302. }
  303. void CMainFrame::OnUpdateServerEdit(CCmdUI* pCmdUI)
  304. {
  305. pCmdUI->Enable(m_pWndServerList->GetDlgItem(ID_EDITMACHINE)->IsWindowEnabled());
  306. }
  307. void CMainFrame::OnServerDelete()
  308. {
  309. m_pWndServerList->OnDeletemachine();
  310. }
  311. void CMainFrame::OnUpdateServerDelete(CCmdUI* pCmdUI)
  312. {
  313. pCmdUI->Enable(m_pWndServerList->GetDlgItem(ID_DELETEMACHINE)->IsWindowEnabled());
  314. }
  315. void CMainFrame::OnSysTray()
  316. {
  317. /* m_bSysTray=!m_bSysTray;
  318. if(m_bSysTray) {
  319. m_pTrayIcon=new CTrayIcon(IDR_TRAYICON);
  320. m_pTrayIcon->SetNotificationWnd(this,WM_TASKBAR_CALLBACK);
  321. } else {
  322. delete m_pTrayIcon;
  323. m_pTrayIcon=NULL;
  324. }*/
  325. }
  326. void CMainFrame::OnUpdateSysTray(CCmdUI* pCmdUI)
  327. {
  328. // pCmdUI->SetCheck(m_bSysTray);
  329. }
  330. int CMainFrame::RegisterClientMenu(LPCSTR szCategory, LPCSTR szComName, TYPEOF_ClientMenu *pProc)
  331. {
  332. CMenu menu, *pMenu, *pSubMenu;
  333. // Add to plugins menu
  334. menu.Attach(m_menuBar.GetMenu());
  335. pMenu=menu.GetSubMenu(3);
  336. if(pMenu==NULL) return -1;
  337. menu.Detach();
  338. // Find/create submenu for plugin
  339. int i,count;
  340. count=pMenu->GetMenuItemCount();
  341. for(i=2;i<count;i++) {
  342. CString str;
  343. pMenu->GetMenuString(i,str,MF_BYPOSITION);
  344. if(str.CompareNoCase(szCategory)==0) break;
  345. }
  346. if(i==count) {
  347. pSubMenu=new CMenu();
  348. if(pSubMenu==NULL) return -1;
  349. pSubMenu->CreatePopupMenu();
  350. pMenu->InsertMenu(i,MF_POPUP|MF_STRING|MF_ENABLED,(UINT)pSubMenu->GetSafeHmenu(),szCategory);
  351. } else {
  352. pSubMenu=pMenu->GetSubMenu(i);
  353. }
  354. // Find empty id;
  355. int nId;
  356. nId=0;
  357. count=m_arrPluginMenuItems.GetSize();
  358. i=0;
  359. while(i!=count) {
  360. for(i=0;i<count;i++) {
  361. if(m_arrPluginMenuItems[i].nId==nId) {
  362. nId++;
  363. break;
  364. }
  365. }
  366. }
  367. // Insert item for plugin
  368. PLUGINMENUITEM pmi;
  369. pmi.nId=nId;
  370. pmi.pProc=pProc;
  371. m_arrPluginMenuItems.Add(pmi);
  372. pSubMenu->AppendMenu(MF_STRING|MF_ENABLED,IDM_PLUGIN_0+nId,szComName);
  373. return 0;
  374. }
  375. int CMainFrame::UnregisterClientMenu(LPCSTR szCategory, LPCSTR szComName)
  376. {
  377. CMenu menu, *pMenu, *pSubMenu;
  378. // Remove from plugins menu
  379. menu.Attach(m_menuBar.GetMenu());
  380. pMenu=menu.GetSubMenu(3);
  381. if(pMenu==NULL) return -1;
  382. menu.Detach();
  383. // Find submenu for plugin
  384. int k,count;
  385. count=pMenu->GetMenuItemCount();
  386. for(k=2;k<count;k++) {
  387. CString str;
  388. pMenu->GetMenuString(k,str,MF_BYPOSITION);
  389. if(str.CompareNoCase(szCategory)==0) break;
  390. }
  391. if(k==count) {
  392. return -1;
  393. } else {
  394. pSubMenu=pMenu->GetSubMenu(k);
  395. }
  396. // Find menu item
  397. int i;
  398. count=pSubMenu->GetMenuItemCount();
  399. for(i=0;i<count;i++) {
  400. CString str;
  401. pSubMenu->GetMenuString(i,str,MF_BYPOSITION);
  402. if(str.CompareNoCase(szComName)==0) break;
  403. }
  404. if(i==count) return -1;
  405. // Remove menu item
  406. int j,pcount,nId;
  407. nId=pSubMenu->GetMenuItemID(i)-IDM_PLUGIN_0;
  408. pSubMenu->RemoveMenu(i,MF_BYPOSITION);
  409. if(count==1) {
  410. pMenu->RemoveMenu(k,MF_BYPOSITION);
  411. delete pSubMenu;
  412. }
  413. pcount=m_arrPluginMenuItems.GetSize();
  414. for(j=pcount-1;j>=0;j--) {
  415. if(m_arrPluginMenuItems[j].nId==nId) {
  416. m_arrPluginMenuItems.RemoveAt(j);
  417. break;
  418. }
  419. }
  420. if(j==-1) return -1;
  421. return 0;
  422. }
  423. int CMainFrame::InitializePlugins()
  424. {
  425. // Start DLL Loader
  426. InitializeDLLLoad();
  427. // Start IO Subsystem
  428. g_pIOHandler=new CIOHandler();
  429. if(g_pIOHandler==NULL) return -1;
  430. // Start Encryption Subsystem
  431. g_pEncryptionHandler=new CEncryptionHandler();
  432. if(g_pEncryptionHandler==NULL) return -1;
  433. // Start Authentication Subsystem
  434. g_pAuthHandler=new CAuthHandler();
  435. if(g_pAuthHandler==NULL) return -1;
  436. // Add built-in stuff
  437. g_pIOHandler->Insert(GetSimpleUdpIOHandler());
  438. g_pIOHandler->Insert(GetSimpleTcpIOHandler());
  439. g_pEncryptionHandler->Insert(GetXOREncryptionEngine());
  440. //g_pEncryptionHandler->Insert(GetDESEncryptionEngine());
  441. g_pAuthHandler->Insert(GetNullAuthHandler());
  442. // Install plugins
  443. int i,count;
  444. count=AfxGetApp()->GetProfileInt("Plugins","Count",0);
  445. for(i=0;i<count;i++) {
  446. CString strKey,strValue;
  447. strKey.Format("Plugin_%d",i);
  448. strValue=AfxGetApp()->GetProfileString("Plugins",strKey,NULL);
  449. AddPlugin(strValue);
  450. }
  451. // Add variables for client executable
  452. char *base=(char *)AfxGetInstanceHandle();
  453. PIMAGE_OPTIONAL_HEADER poh=(PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET(base);
  454. AddVariables(base, poh->SizeOfImage, -1);
  455. LoadPluginConfig(-1);
  456. return 0;
  457. }
  458. int CMainFrame::TerminatePlugins()
  459. {
  460. // Save client config
  461. SavePluginConfig(-1);
  462. // Unload plugins
  463. int i,count;
  464. count=m_arrPluginInfo.GetSize();
  465. AfxGetApp()->WriteProfileInt("Plugins","Count",count);
  466. for(i=(count-1);i>=0;i--) {
  467. CString strKey;
  468. strKey.Format("Plugin_%d",i);
  469. AfxGetApp()->WriteProfileString("Plugins",strKey,m_arrPluginInfo[i].svFilename);
  470. RemovePlugin(i);
  471. }
  472. // Purge extra registry keys
  473. i=count; 
  474. while(1) {
  475. CString strKey;
  476. strKey.Format("Plugin_%d",i);
  477. if(AfxGetApp()->GetProfileString("Plugins",strKey).IsEmpty()) break;
  478. AfxGetApp()->WriteProfileString("Plugins",strKey,NULL);
  479. i++;
  480. }
  481. // Terminate Auth Subsystem
  482. if(g_pAuthHandler!=NULL) {
  483. for(i=0;i<MAX_AUTH_HANDLERS;i++) {
  484. g_pAuthHandler->Remove(i);
  485. }
  486. delete g_pAuthHandler;
  487. }
  488. // Terminate Encryption Subsystem
  489. if(g_pEncryptionHandler!=NULL) {
  490. for(i=0;i<MAX_ENCRYPTION_ENGINES;i++) {
  491. g_pEncryptionHandler->Remove(i);
  492. }
  493. delete g_pEncryptionHandler;
  494. }
  495. // Terminate IO Subsystem
  496. if(g_pIOHandler!=NULL) {
  497. for(i=0;i<MAX_IO_HANDLERS;i++) {
  498. g_pIOHandler->Remove(i);
  499. }
  500. delete g_pIOHandler;
  501. }
  502. // Stop DLL Loader
  503. KillDLLLoad();
  504. return 0;
  505. }
  506. int CMainFrame::AddPlugin(CString strPath)
  507. {
  508. PLUGIN_INFO pi;
  509. pi.pPluginImage=NULL;
  510. pi.dwPluginLen=0;
  511. pi.svFilename[0]=0;
  512. pi.svDescription[0]=0;
  513. pi.wVersionLo=0;
  514. pi.wVersionHi=0;
  515. pi.wBOVersionLo=0;
  516. pi.wBOVersionHi=0;
  517. // Load Plugin DLL 
  518. HINSTANCE hDLL;
  519. hDLL=LoadLibrary(strPath);
  520. if(hDLL==NULL)
  521. hDLL=LoadDLL(strPath);
  522. if(hDLL!=NULL) {
  523. pi.pPluginImage=(BYTE *)hDLL;
  524. PIMAGE_OPTIONAL_HEADER poh=(PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET(pi.pPluginImage);
  525. pi.dwPluginLen=poh->SizeOfImage;
  526. // Call plugin version information function
  527. PLUGIN_VERSION pv;
  528. TYPEOF_PluginVersion *PluginVersion=(TYPEOF_PluginVersion *)GetDLLProcAddress(hDLL,"PluginVersion");
  529. if(PluginVersion!=NULL) {
  530. if(PluginVersion(&pv)) {
  531. // Copy plugin version information
  532. lstrcpyn(pi.svFilename,strPath,MAX_PATH+1);
  533. lstrcpyn(pi.svDescription,pv.svDescription,256);
  534. pi.wVersionLo=pv.wVersionLo;
  535. pi.wVersionHi=pv.wVersionHi;
  536. pi.wBOVersionLo=pv.wBOVersionLo;
  537. pi.wBOVersionHi=pv.wBOVersionHi;
  538. }
  539. }
  540. // Extract variable list
  541. int nPlugin=m_arrPluginInfo.Add(pi);
  542. if(nPlugin>=0) {
  543. AddVariables((char *)pi.pPluginImage,pi.dwPluginLen,nPlugin);
  544. LoadPluginConfig(nPlugin);
  545. } else {
  546. FreeDLL(hDLL);
  547. FreeLibrary(hDLL);
  548. }
  549. // Start plugin
  550. PLUGIN_LINKAGE pl;
  551. pl.pDispatchCommand=NULL;
  552. pl.pEncryptionHandler=g_pEncryptionHandler;
  553. pl.pIOHandler=g_pIOHandler;
  554. pl.pIssueAuthCommandRequest=&IssueAuthCommandRequest;
  555. pl.pIssueAuthCommandReply=&IssueAuthCommandReply;
  556. pl.pRegisterClientMenu=&::RegisterClientMenu;
  557. pl.pRegisterCommand=NULL;
  558. pl.pUnregisterClientMenu=&::UnregisterClientMenu;
  559. pl.pUnregisterCommand=NULL;
  560. pl.pListenAuthSocket=&ListenAuthSocket;
  561. pl.pConnectAuthSocket=&ConnectAuthSocket;
  562. pl.pInteractiveConnect=&InteractiveConnect;
  563. pl.pInteractiveListen=&InteractiveListen;
  564. TYPEOF_InstallPlugin *InstallPlugin=(TYPEOF_InstallPlugin *)GetDLLProcAddress(hDLL,"InstallPlugin");
  565. if(InstallPlugin(pl)==-1) {
  566. AfxMessageBox("Error installing plugin.",MB_OK|MB_ICONWARNING|MB_SETFOREGROUND|MB_TOPMOST);
  567. RemovePlugin(nPlugin);
  568. return -1;
  569. }
  570. return nPlugin;
  571. }
  572. return -1;
  573. }
  574. int CMainFrame::RemovePlugin(int nPlugin)
  575. {
  576. if(nPlugin>=m_arrPluginInfo.GetSize()) return -1;
  577. if(nPlugin<0) return -1;
  578. HMODULE hDLL=(HMODULE)m_arrPluginInfo[nPlugin].pPluginImage;
  579. // Terminate plugin
  580. TYPEOF_TerminatePlugin *TerminatePlugin=(TYPEOF_TerminatePlugin *)GetDLLProcAddress(hDLL,"TerminatePlugin");
  581. TerminatePlugin();
  582. // Save plugin configuration to registry
  583. if(SavePluginConfig(nPlugin)==-1) {
  584. AfxMessageBox("Unable to save plugin config to registry",MB_OK|MB_SETFOREGROUND|MB_TOPMOST|MB_ICONWARNING);
  585. }
  586. // Remove from memory and plugin info list
  587. FreeDLL(hDLL);
  588. FreeLibrary(hDLL);
  589. m_arrPluginInfo.RemoveAt(nPlugin);
  590. return 0;
  591. }
  592. void CMainFrame::AddVariables(char *pBuffer, DWORD dwLen, int nPlugin)
  593. {
  594. DWORD pos;
  595. for(pos=0;pos<dwLen-10;pos++) {
  596. if(memcmp(pBuffer+pos,"<**CFG**>",9)==0) {
  597. pos+=9;
  598. // Get category name
  599. VARIABLE_INFO vi;
  600. lstrcpyn(vi.svCategory,pBuffer+pos,256);
  601. pos+=(lstrlen(pBuffer+pos)+1);
  602. // Go through all variables
  603. while(*(pBuffer+pos)!='') {
  604. DWORD dwValPos;
  605. vi.VarType=*(pBuffer+pos);
  606. if(vi.VarType=='B') {
  607. pos+=2;
  608. // Get Name and Value
  609. DWORD dwStart=pos;
  610. while(*(pBuffer+pos)!='=') pos++;
  611. lstrcpyn(vi.svVarName,pBuffer+dwStart,min(pos-dwStart+1,256));
  612. pos++;
  613. dwValPos=pos;
  614. lstrcpyn(vi.svVarValue,pBuffer+pos,256);
  615. vi.nStrLen=1;
  616. pos+=2;
  617. } else if(vi.VarType=='N') {
  618. // Get Number Range
  619. pos+=2;
  620. vi.nNumLo=atoi(pBuffer+pos);
  621. while(*(pBuffer+pos)!=',') pos++;
  622. pos++;
  623. vi.nStrLen=pos;
  624. vi.nNumHi=atoi(pBuffer+pos);
  625. while(*(pBuffer+pos)!=']') pos++;
  626. vi.nStrLen=pos-vi.nStrLen;
  627. pos+=2;
  628. // Get Name and Value
  629. DWORD dwStart=pos;
  630. while(*(pBuffer+pos)!='=') pos++;
  631. lstrcpyn(vi.svVarName,pBuffer+dwStart,min(pos-dwStart+1,256));
  632. pos++;
  633. dwValPos=pos;
  634. lstrcpyn(vi.svVarValue,pBuffer+pos,256);
  635. pos+=(vi.nStrLen+1);
  636. } else if(vi.VarType=='S') {
  637. // Get Number Range
  638. pos+=2;
  639. vi.nStrLen=atoi(pBuffer+pos);
  640. while(*(pBuffer+pos)!=']') pos++;
  641. pos+=2;
  642. // Get Name and Value
  643. DWORD dwStart=pos;
  644. while(*(pBuffer+pos)!='=') pos++;
  645. lstrcpyn(vi.svVarName,pBuffer+dwStart,min(pos-dwStart+1,256));
  646. pos++;
  647. lstrcpyn(vi.svVarValue,pBuffer+pos,256);
  648. dwValPos=pos;
  649. pos+=(vi.nStrLen+1);
  650. } else {
  651. AfxMessageBox("Error parsing variables. Variable list could not be determined.");
  652. return;
  653. }
  654. // Add to variable info array
  655. vi.nPlugin=nPlugin;
  656. vi.dwPos=dwValPos;
  657. m_arrVarInfo.Add(vi);
  658. }
  659. }
  660. }
  661. }
  662. int CMainFrame::LoadPluginConfig(int nNum)
  663. {
  664. int i,count;
  665. count=m_arrVarInfo.GetSize();
  666. char *base;
  667. if(nNum==-1) {
  668. base=(char *)AfxGetInstanceHandle();
  669. } else {
  670. base=(char *)m_arrPluginInfo[nNum].pPluginImage;
  671. }
  672. for(i=0;i<count;i++) {
  673. VARIABLE_INFO vi=m_arrVarInfo[i];
  674. if(vi.nPlugin==nNum) {
  675. CString str;
  676. str=AfxGetApp()->GetProfileString(vi.svCategory,vi.svVarName,base+vi.dwPos);
  677. lstrcpyn(base+vi.dwPos,str,vi.nStrLen+1);
  678. }
  679. }
  680. return 0;
  681. }
  682. int CMainFrame::SavePluginConfig(int nNum)
  683. {
  684. int i,count;
  685. count=m_arrVarInfo.GetSize();
  686. char *base;
  687. if(nNum==-1) {
  688. base=(char *)AfxGetInstanceHandle();
  689. } else {
  690. base=(char *)m_arrPluginInfo[nNum].pPluginImage;
  691. }
  692. for(i=0;i<count;i++) {
  693. VARIABLE_INFO vi=m_arrVarInfo[i];
  694. if(vi.nPlugin==nNum) {
  695. AfxGetApp()->WriteProfileString(vi.svCategory,vi.svVarName,base+vi.dwPos);
  696. }
  697. }
  698. return 0;
  699. }
  700. void CMainFrame::OnPluginMenu(UINT nID) 
  701. {
  702. // Find id
  703. int i,count;
  704. count=m_arrPluginMenuItems.GetSize();
  705. for(i=0;i<count;i++) {
  706. if(m_arrPluginMenuItems[i].nId==(int)(nID-IDM_PLUGIN_0)) break;
  707. }
  708. if(i==count) return;
  709. // Call plugin procedure
  710. int nRet=m_arrPluginMenuItems[i].pProc(GetSafeHwnd());
  711. if(nRet==-1) {
  712. AfxMessageBox("An error occurred while starting up the plugin.nThings may not proceed as planned.n",MB_OK|MB_ICONWARNING|MB_SETFOREGROUND|MB_TOPMOST);
  713. }
  714. }
  715. BOOL CMainFrame::PreTranslateMessage(MSG *pMsg)
  716. {
  717. m_menuBar.TranslateFrameMessage(pMsg);
  718. return CCJFrameWnd::PreTranslateMessage(pMsg);
  719. }
  720. LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  721. {
  722. switch(message) {
  723. case WM_CDIALOG_DO_MODAL: 
  724. {
  725. CDialog *cdlg=((CDIALOGDOMODALPARAM *)lParam)->pDialog;
  726. int nRet=cdlg->DoModal();
  727. ((CDIALOGDOMODALPARAM *)lParam)->nRet=nRet;
  728. SetEvent(((CDIALOGDOMODALPARAM *)lParam)->hTrigger);
  729. return TRUE;
  730. }
  731. }
  732. return CCJFrameWnd::WindowProc(message, wParam, lParam);
  733. }
  734. int CMainFrame::DoDialogModal(CDialog *dlg)
  735. {
  736. MSG msg;
  737. CDIALOGDOMODALPARAM cddmp;
  738. cddmp.pDialog=dlg;
  739. cddmp.nRet=-1;
  740. cddmp.hTrigger=CreateEvent(NULL,TRUE,FALSE,NULL);
  741. PostMessage(WM_CDIALOG_DO_MODAL,0,(LPARAM)&cddmp);
  742. while(WaitForSingleObject(cddmp.hTrigger,0)!=WAIT_OBJECT_0) {
  743. if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
  744. TranslateMessage(&msg);
  745. DispatchMessage(&msg);
  746. }
  747. Sleep(20);
  748. }
  749. CloseHandle(cddmp.hTrigger);
  750. return cddmp.nRet;
  751. }
  752. // Global callbacks for plugins
  753. int RegisterClientMenu(LPCSTR szCategory, LPCSTR szComName, TYPEOF_ClientMenu *pProc)
  754. {
  755. return ((CMainFrame *)AfxGetMainWnd())->RegisterClientMenu(szCategory,szComName, pProc);
  756. }
  757. int UnregisterClientMenu(LPCSTR szCategory, LPCSTR szComName)
  758. {
  759. return ((CMainFrame *)AfxGetMainWnd())->UnregisterClientMenu(szCategory,szComName);
  760. }
  761. int InteractiveConnect(HWND hParent, LPCSTR svBindStr,LPCSTR svNetMod,LPCSTR svEncryption,LPCSTR svAuth,char *svRBindStr,char *svRNetMod,char *svREncryption,char *svRAuth)
  762. {
  763. CMainFrame *pcmf=(CMainFrame *)AfxGetMainWnd();
  764. CWnd *pWnd=CWnd::FromHandle(hParent);
  765. CInteractiveConnect cic(pWnd);
  766. cic.m_strSvrAddr=svBindStr;
  767. cic.m_strSelectedIO=svNetMod;
  768. cic.m_strSelectedEnc=svEncryption;
  769. cic.m_strSelectedAuth=svAuth;
  770. if(pcmf->DoDialogModal(&cic)==IDOK) {
  771. lstrcpyn(svRBindStr,cic.m_strSvrAddr,256);
  772. lstrcpyn(svRNetMod,cic.m_strSelectedIO,256);
  773. lstrcpyn(svREncryption,cic.m_strSelectedEnc,256);
  774. lstrcpyn(svRAuth,cic.m_strSelectedAuth,256);
  775. return 0;
  776. }
  777. return -1;
  778. }
  779. int InteractiveListen(HWND hParent, LPCSTR svBindStr,LPCSTR svNetMod,LPCSTR svEncryption,LPCSTR svAuth,char *svRBindStr,char *svRNetMod,char *svREncryption,char *svRAuth)
  780. {
  781. CMainFrame *pcmf=(CMainFrame *)AfxGetMainWnd();
  782. CWnd *pWnd=CWnd::FromHandle(hParent);
  783. CInteractiveListen cil(pWnd);
  784.  
  785. cil.m_strSvrAddr=svBindStr;
  786. cil.m_strSelectedIO=svNetMod;
  787. cil.m_strSelectedEnc=svEncryption;
  788. cil.m_strSelectedAuth=svAuth;
  789. if(pcmf->DoDialogModal(&cil)==IDOK) {
  790. lstrcpyn(svRBindStr,cil.m_strSvrAddr,256);
  791. lstrcpyn(svRNetMod,cil.m_strSelectedIO,256);
  792. lstrcpyn(svREncryption,cil.m_strSelectedEnc,256);
  793. lstrcpyn(svRAuth,cil.m_strSelectedAuth,256);
  794. return 0;
  795. }
  796. return -1;
  797. }
  798. void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
  799. {
  800. CCJFrameWnd::OnActivate(nState, pWndOther, bMinimized);
  801. }
  802. /*LRESULT CMainFrame::OnTrayNotification(WPARAM uID, LPARAM lEvent)
  803. {
  804.    // let tray icon do default stuff
  805.    return m_pTrayIcon->OnTrayNotification(uID, lEvent);
  806. }
  807. */