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.

Authentication.h 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This 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. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifndef WINVNCCONF_AUTHENTICATION
  19. #define WINVNCCONF_AUTHENTICATION
  20. #include <windows.h>
  21. #include <commctrl.h>
  22. #include <vncconfig/PasswordDialog.h>
  23. #include <rfb_win32/Registry.h>
  24. #include <rfb_win32/SecurityPage.h>
  25. #include <rfb_win32/MsgBox.h>
  26. #include <rfb/ServerCore.h>
  27. #include <rfb/Security.h>
  28. #include <rfb/SecurityServer.h>
  29. #include <rfb/SSecurityVncAuth.h>
  30. #ifdef HAVE_GNUTLS
  31. #include <rfb/SSecurityTLS.h>
  32. #endif
  33. static rfb::BoolParameter queryOnlyIfLoggedOn("QueryOnlyIfLoggedOn",
  34. "Only prompt for a local user to accept incoming connections if there is a user logged on", false);
  35. namespace rfb {
  36. namespace win32 {
  37. class SecPage : public SecurityPage {
  38. public:
  39. SecPage(const RegKey& rk)
  40. : SecurityPage(NULL), regKey(rk) {
  41. security = new SecurityServer();
  42. }
  43. void initDialog() {
  44. SecurityPage::initDialog();
  45. setItemChecked(IDC_QUERY_CONNECT, rfb::Server::queryConnect);
  46. setItemChecked(IDC_QUERY_LOGGED_ON, queryOnlyIfLoggedOn);
  47. onCommand(IDC_AUTH_NONE, 0);
  48. }
  49. bool onCommand(int id, int cmd) {
  50. SecurityPage::onCommand(id, cmd);
  51. setChanged(true);
  52. if (id == IDC_AUTH_VNC_PASSWD) {
  53. PasswordDialog passwdDlg(regKey, registryInsecure);
  54. passwdDlg.showDialog(handle);
  55. } else if (id == IDC_LOAD_CERT) {
  56. const char* title = "X509Cert";
  57. const char* filter =
  58. "X.509 Certificates (*.crt;*.cer;*.pem)\0*.crt;*.cer;*.pem\0All\0*.*\0";
  59. showFileChooser(regKey, title, filter, handle);
  60. } else if (id == IDC_LOAD_CERTKEY) {
  61. const char* title = "X509Key";
  62. const char* filter = "X.509 Keys (*.key;*.pem)\0*.key;*.pem\0All\0*.*\0";
  63. showFileChooser(regKey, title, filter, handle);
  64. } else if (id == IDC_QUERY_LOGGED_ON) {
  65. enableItem(IDC_QUERY_LOGGED_ON, enableQueryOnlyIfLoggedOn());
  66. }
  67. return true;
  68. }
  69. bool onOk() {
  70. SecurityPage::onOk();
  71. if (isItemChecked(IDC_AUTH_VNC))
  72. verifyVncPassword(regKey);
  73. else if (haveVncPassword() &&
  74. MsgBox(0, "The VNC authentication method is disabled, but a password is still stored for it.\n"
  75. "Do you want to remove the VNC authentication password from the registry?",
  76. MB_ICONWARNING | MB_YESNO) == IDYES) {
  77. regKey.setBinary("Password", 0, 0);
  78. }
  79. #ifdef HAVE_GNUTLS
  80. if (isItemChecked(IDC_ENC_X509)) {
  81. SSecurityTLS::X509_CertFile.setParam(regKey.getString("X509Cert").c_str());
  82. SSecurityTLS::X509_CertFile.setParam(regKey.getString("X509Key").c_str());
  83. }
  84. #endif
  85. regKey.setString("SecurityTypes", security->ToString());
  86. regKey.setBool("QueryConnect", isItemChecked(IDC_QUERY_CONNECT));
  87. regKey.setBool("QueryOnlyIfLoggedOn", isItemChecked(IDC_QUERY_LOGGED_ON));
  88. return true;
  89. }
  90. void setWarnPasswdInsecure(bool warn) {
  91. registryInsecure = warn;
  92. }
  93. bool enableQueryOnlyIfLoggedOn() {
  94. return isItemChecked(IDC_QUERY_CONNECT);
  95. }
  96. static bool haveVncPassword() {
  97. std::string password, passwordReadOnly;
  98. SSecurityVncAuth::vncAuthPasswd.getVncAuthPasswd(&password, &passwordReadOnly);
  99. return !password.empty();
  100. }
  101. static void verifyVncPassword(const RegKey& regKey) {
  102. if (!haveVncPassword()) {
  103. MsgBox(0, "The VNC authentication method is enabled, but no password is specified.\n"
  104. "The password dialog will now be shown.", MB_ICONINFORMATION | MB_OK);
  105. PasswordDialog passwd(regKey, registryInsecure);
  106. passwd.showDialog();
  107. }
  108. }
  109. virtual void loadX509Certs(void) {}
  110. virtual void enableX509Dialogs(void) {
  111. enableItem(IDC_LOAD_CERT, true);
  112. enableItem(IDC_LOAD_CERTKEY, true);
  113. }
  114. virtual void disableX509Dialogs(void) {
  115. enableItem(IDC_LOAD_CERT, false);
  116. enableItem(IDC_LOAD_CERTKEY, false);
  117. }
  118. virtual void loadVncPasswd() {
  119. enableItem(IDC_AUTH_VNC_PASSWD, isItemChecked(IDC_AUTH_VNC));
  120. }
  121. protected:
  122. RegKey regKey;
  123. static bool registryInsecure;
  124. private:
  125. inline void modifyAuthMethod(int enc_idc, int auth_idc, bool enable)
  126. {
  127. setItemChecked(enc_idc, enable);
  128. setItemChecked(auth_idc, enable);
  129. }
  130. inline bool showFileChooser(const RegKey& /*rk*/,
  131. const char* title,
  132. const char* filter,
  133. HWND hwnd)
  134. {
  135. OPENFILENAME ofn;
  136. char filename[MAX_PATH];
  137. ZeroMemory(&ofn, sizeof(ofn));
  138. ZeroMemory(&filename, sizeof(filename));
  139. filename[0] = '\0';
  140. ofn.lStructSize = sizeof(ofn);
  141. ofn.hwndOwner = hwnd;
  142. ofn.lpstrFile = filename;
  143. ofn.nMaxFile = sizeof(filename);
  144. ofn.lpstrFilter = (char*)filter;
  145. ofn.nFilterIndex = 1;
  146. ofn.lpstrFileTitle = NULL;
  147. ofn.nMaxFileTitle = 0;
  148. ofn.lpstrTitle = (char*)title;
  149. ofn.lpstrInitialDir = NULL;
  150. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  151. if (GetOpenFileName(&ofn)==TRUE) {
  152. regKey.setString(title, filename);
  153. return true;
  154. }
  155. return false;
  156. }
  157. };
  158. };
  159. bool SecPage::registryInsecure = false;
  160. };
  161. #endif