home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 217 / DPCS0306DVD.ISO / Toolkit / Internet / FileZilla / Server / FileZilla_Server-0.9.11.exe / source / interface / OptionsSslPage.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-11-13  |  6.7 KB  |  239 lines

  1. // FileZilla Server - a Windows ftp server
  2.  
  3. // Copyright (C) 2002-2004 - Tim Kosse <tim.kosse@gmx.de>
  4.  
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9.  
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. #include "stdafx.h"
  20. #include "filezilla server.h"
  21. #include "OptionsDlg.h"
  22. #include "OptionsPage.h"
  23. #include "OptionsSslPage.h"
  24. #include "GenerateCertificateDlg.h"
  25. #include "../AsyncSslSocketLayer.h"
  26. #include <set>
  27.  
  28. COptionsSslPage::COptionsSslPage(COptionsDlg *pOptionsDlg, CWnd* pParent /*=NULL*/)
  29.     : COptionsPage(pOptionsDlg, COptionsSslPage::IDD, pParent)
  30.     , m_enabled(FALSE)
  31.     , m_allowExplicit(FALSE)
  32.     , m_certificate(_T(""))
  33.     , m_sslports(_T(""))
  34. {
  35. }
  36.  
  37. COptionsSslPage::~COptionsSslPage()
  38. {
  39. }
  40.  
  41. void COptionsSslPage::DoDataExchange(CDataExchange* pDX)
  42. {
  43.     COptionsPage::DoDataExchange(pDX);
  44.     DDX_Check(pDX, IDC_ENABLESSL, m_enabled);
  45.     DDX_Check(pDX, IDC_ALLOWEXPLICIT, m_allowExplicit);
  46.     DDX_Check(pDX, IDC_FORCEEXPLICIT, m_forceExplicit);
  47.     DDX_Check(pDX, IDC_FORCEPROTP, m_forceProtP);
  48.     DDX_Text(pDX, IDC_CERTIFICATE, m_certificate);
  49.     DDX_Text(pDX, IDC_PRIVATEKEY, m_key);
  50.     DDX_Text(pDX, IDC_KEYPASS, m_pass);
  51.     DDX_Text(pDX, IDC_SSLONLY, m_sslports);
  52.     DDX_Control(pDX, IDC_ALLOWEXPLICIT, m_cAllowExplicit);
  53.     DDX_Control(pDX, IDC_FORCEEXPLICIT, m_cForceExplicit);
  54.     DDX_Control(pDX, IDC_FORCEPROTP, m_cForceProtP);
  55.     DDX_Control(pDX, IDC_CERTIFICATE, m_cCertificate);
  56.     DDX_Control(pDX, IDC_CERTIFICATE_BROWSE, m_cCertificateBrowse);
  57.     DDX_Control(pDX, IDC_PRIVATEKEY, m_cKey);
  58.     DDX_Control(pDX, IDC_PRIVATEKEY_BROWSE, m_cKeyBrowse);
  59.     DDX_Control(pDX, IDC_SSLONLY, m_cSslports);
  60.     DDX_Control(pDX, IDC_KEYPASS, m_cPass);
  61. }
  62.  
  63. BEGIN_MESSAGE_MAP(COptionsSslPage, COptionsPage)
  64.     ON_BN_CLICKED(IDC_GENERATE, OnGenerate)
  65.     ON_BN_CLICKED(IDC_PRIVATEKEY_BROWSE, OnKeyBrowse)
  66.     ON_BN_CLICKED(IDC_CERTIFICATE_BROWSE, OnCertificateBrowse)
  67.     ON_BN_CLICKED(IDC_ENABLESSL, OnEnableSsl)
  68.     ON_BN_CLICKED(IDC_ALLOWEXPLICIT, OnEnableSsl)
  69. END_MESSAGE_MAP()
  70.  
  71. void COptionsSslPage::OnGenerate()
  72. {
  73.     UpdateData();
  74.     CGenerateCertificateDlg dlg;
  75.     if (dlg.DoModal() == IDOK)
  76.     {
  77.         m_key = dlg.m_file;
  78.         m_certificate = dlg.m_file;
  79.         UpdateData(FALSE);
  80.     }
  81. }
  82.  
  83. void COptionsSslPage::OnKeyBrowse()
  84. {
  85.     UpdateData();
  86.     CFileDialog dlg(TRUE);
  87.     if (dlg.DoModal() == IDOK)
  88.     {
  89.         m_key = dlg.GetPathName();
  90.         UpdateData(FALSE);
  91.     }
  92. }
  93.  
  94. void COptionsSslPage::OnCertificateBrowse()
  95. {
  96.     UpdateData();
  97.     CFileDialog dlg(TRUE);
  98.     if (dlg.DoModal() == IDOK)
  99.     {
  100.         m_certificate = dlg.GetPathName();
  101.         UpdateData(FALSE);
  102.     }
  103. }
  104.  
  105. void COptionsSslPage::OnEnableSsl()
  106. {
  107.     UpdateData();
  108.     m_cAllowExplicit.EnableWindow(m_enabled);
  109.     m_cCertificate.EnableWindow(m_enabled);
  110.     m_cCertificateBrowse.EnableWindow(m_enabled && m_pOptionsDlg->IsLocalConnection());
  111.     m_cKey.EnableWindow(m_enabled);
  112.     m_cKeyBrowse.EnableWindow(m_enabled && m_pOptionsDlg->IsLocalConnection());
  113.     m_cSslports.EnableWindow(m_enabled);
  114.     m_cForceExplicit.EnableWindow(m_enabled && m_allowExplicit);
  115.     m_cForceProtP.EnableWindow(m_enabled);
  116.     m_cPass.EnableWindow(m_enabled);
  117. }
  118.  
  119. BOOL COptionsSslPage::IsDataValid()
  120. {
  121.     UpdateData();
  122.  
  123.     std::set<int> portSet;
  124.     bool valid = true;
  125.  
  126.     CString ports = m_sslports;
  127.     ports.TrimLeft(" ,");
  128.  
  129.     int pos = ports.FindOneOf(_T(" ,"));
  130.     while (pos != -1 && valid)
  131.     {
  132.         int port = atoi(ports.Left(pos));
  133.         if (port < 1 || port > 65535)
  134.         {
  135.             valid = false;
  136.             break;
  137.         }
  138.         else
  139.             portSet.insert(port);
  140.         ports = ports.Mid(pos + 1);
  141.         ports.TrimLeft(" ,");
  142.         pos = ports.FindOneOf(_T(" ,"));
  143.     }
  144.     if (valid && ports != "")
  145.     {
  146.         int port = atoi(ports);
  147.         if (port < 1 || port > 65535)
  148.             valid = false;
  149.         else
  150.             portSet.insert(port);
  151.     }
  152.  
  153.     if (!valid && m_enabled)
  154.     {
  155.         m_pOptionsDlg->ShowPage(this);
  156.         m_cSslports.SetFocus();
  157.         AfxMessageBox("Invalid port found, please only enter ports in the range from 1 to 65535.");
  158.         return FALSE;
  159.     }
  160.  
  161.     m_sslports = "";
  162.     for (std::set<int>::const_iterator iter = portSet.begin(); iter != portSet.end(); iter++)
  163.     {
  164.         CString tmp;
  165.         tmp.Format("%d ", *iter);
  166.         m_sslports += tmp;
  167.     }
  168.     m_sslports.TrimRight(' ');
  169.     UpdateData(false);
  170.  
  171.     if (m_enabled && m_pOptionsDlg->IsLocalConnection())
  172.     {
  173.         CAsyncSslSocketLayer layer;
  174.         CString error;
  175.         int res = layer.SetCertKeyFile(m_certificate, m_key, m_pass, &error);
  176.         if (res == SSL_FAILURE_LOADDLLS)
  177.         {
  178.             m_pOptionsDlg->ShowPage(this);
  179.             AfxMessageBox("Failed to load SSL libraries");
  180.             return FALSE;
  181.         }
  182.         else if (res == SSL_FAILURE_INITSSL)
  183.         {
  184.             m_pOptionsDlg->ShowPage(this);
  185.             AfxMessageBox("Failed to initialize SSL libraries");
  186.             return FALSE;
  187.         }
  188.         else if (res == SSL_FAILURE_VERIFYCERT)
  189.         {
  190.             m_pOptionsDlg->ShowPage(this);
  191.             if (error != "")
  192.                 AfxMessageBox(error);
  193.             else
  194.                 AfxMessageBox("Failed to set certificate and private key");
  195.             return FALSE;
  196.         }
  197.         else if (res)
  198.         {
  199.             m_pOptionsDlg->ShowPage(this);
  200.             return FALSE;
  201.         }
  202.     }
  203.  
  204.     return true;
  205. }
  206.  
  207. void COptionsSslPage::SaveData()
  208. {
  209.     m_pOptionsDlg->SetOption(OPTION_ENABLESSL, m_enabled ? 1 : 0);
  210.     m_pOptionsDlg->SetOption(OPTION_SSLKEYFILE, m_key);
  211.     m_pOptionsDlg->SetOption(OPTION_SSLKEYPASS, m_pass);
  212.     m_pOptionsDlg->SetOption(OPTION_SSLCERTFILE, m_certificate);
  213.     m_pOptionsDlg->SetOption(OPTION_SSLPORTS, m_sslports);
  214.     m_pOptionsDlg->SetOption(OPTION_ALLOWEXPLICITSSL, m_allowExplicit ? 1 : 0);
  215.     m_pOptionsDlg->SetOption(OPTION_SSLFORCEEXPLICIT, m_forceExplicit ? 1 : 0);
  216.     m_pOptionsDlg->SetOption(OPTION_FORCEPROTP, m_forceProtP ? 1 : 0);
  217. }
  218.  
  219. void COptionsSslPage::LoadData()
  220. {
  221.     m_enabled = m_pOptionsDlg->GetOptionVal(OPTION_ENABLESSL) != 0;
  222.     m_key = m_pOptionsDlg->GetOption(OPTION_SSLKEYFILE);
  223.     m_pass = m_pOptionsDlg->GetOption(OPTION_SSLKEYPASS);
  224.     m_certificate = m_pOptionsDlg->GetOption(OPTION_SSLCERTFILE);
  225.     m_sslports = m_pOptionsDlg->GetOption(OPTION_SSLPORTS);
  226.     m_allowExplicit = m_pOptionsDlg->GetOptionVal(OPTION_ALLOWEXPLICITSSL) != 0;
  227.     m_forceExplicit = m_pOptionsDlg->GetOptionVal(OPTION_SSLFORCEEXPLICIT) != 0;
  228.     m_forceProtP = m_pOptionsDlg->GetOptionVal(OPTION_FORCEPROTP) != 0;
  229. }
  230.  
  231. BOOL COptionsSslPage::OnInitDialog()
  232. {
  233.     COptionsPage::OnInitDialog();
  234.  
  235.     OnEnableSsl();
  236.  
  237.     return TRUE;
  238. }
  239.