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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include <rfb/Password.h>
  34. static rfb::BoolParameter queryOnlyIfLoggedOn("QueryOnlyIfLoggedOn",
  35. "Only prompt for a local user to accept incoming connections if there is a user logged on", false);
  36. namespace rfb {
  37. namespace win32 {
  38. class SecPage : public SecurityPage {
  39. public:
  40. SecPage(const RegKey& rk)
  41. : SecurityPage(NULL), regKey(rk) {
  42. security = new SecurityServer();
  43. }
  44. void initDialog() {
  45. SecurityPage::initDialog();
  46. setItemChecked(IDC_QUERY_CONNECT, rfb::Server::queryConnect);
  47. setItemChecked(IDC_QUERY_LOGGED_ON, queryOnlyIfLoggedOn);
  48. onCommand(IDC_AUTH_NONE, 0);
  49. }
  50. bool onCommand(int id, int cmd) {
  51. SecurityPage::onCommand(id, cmd);
  52. setChanged(true);
  53. if (id == IDC_AUTH_VNC_PASSWD) {
  54. PasswordDialog passwdDlg(regKey, registryInsecure);
  55. passwdDlg.showDialog(handle);
  56. } else if (id == IDC_LOAD_CERT) {
  57. const char* title = "X509Cert";
  58. const char* filter =
  59. "X.509 Certificates (*.crt;*.cer;*.pem)\0*.crt;*.cer;*.pem\0All\0*.*\0";
  60. showFileChooser(regKey, title, filter, handle);
  61. } else if (id == IDC_LOAD_CERTKEY) {
  62. const char* title = "X509Key";
  63. const char* filter = "X.509 Keys (*.key;*.pem)\0*.key;*.pem\0All\0*.*\0";
  64. showFileChooser(regKey, title, filter, handle);
  65. } else if (id == IDC_QUERY_LOGGED_ON) {
  66. enableItem(IDC_QUERY_LOGGED_ON, enableQueryOnlyIfLoggedOn());
  67. }
  68. return true;
  69. }
  70. bool onOk() {
  71. SecurityPage::onOk();
  72. if (isItemChecked(IDC_AUTH_VNC))
  73. verifyVncPassword(regKey);
  74. else if (haveVncPassword() &&
  75. MsgBox(0, "The VNC authentication method is disabled, but a password is still stored for it.\n"
  76. "Do you want to remove the VNC authentication password from the registry?",
  77. MB_ICONWARNING | MB_YESNO) == IDYES) {
  78. regKey.setBinary("Password", 0, 0);
  79. }
  80. #ifdef HAVE_GNUTLS
  81. if (isItemChecked(IDC_ENC_X509)) {
  82. SSecurityTLS::X509_CertFile.setParam(regKey.getString("X509Cert").c_str());
  83. SSecurityTLS::X509_CertFile.setParam(regKey.getString("X509Key").c_str());
  84. }
  85. #endif
  86. regKey.setString("SecurityTypes", security->ToString());
  87. regKey.setBool("QueryConnect", isItemChecked(IDC_QUERY_CONNECT));
  88. regKey.setBool("QueryOnlyIfLoggedOn", isItemChecked(IDC_QUERY_LOGGED_ON));
  89. return true;
  90. }
  91. void setWarnPasswdInsecure(bool warn) {
  92. registryInsecure = warn;
  93. }
  94. bool enableQueryOnlyIfLoggedOn() {
  95. return isItemChecked(IDC_QUERY_CONNECT);
  96. }
  97. static bool haveVncPassword() {
  98. PlainPasswd password, passwordReadOnly;
  99. SSecurityVncAuth::vncAuthPasswd.getVncAuthPasswd(&password, &passwordReadOnly);
  100. return password.buf && strlen(password.buf) != 0;
  101. }
  102. static void verifyVncPassword(const RegKey& regKey) {
  103. if (!haveVncPassword()) {
  104. MsgBox(0, "The VNC authentication method is enabled, but no password is specified.\n"
  105. "The password dialog will now be shown.", MB_ICONINFORMATION | MB_OK);
  106. PasswordDialog passwd(regKey, registryInsecure);
  107. passwd.showDialog();
  108. }
  109. }
  110. virtual void loadX509Certs(void) {}
  111. virtual void enableX509Dialogs(void) {
  112. enableItem(IDC_LOAD_CERT, true);
  113. enableItem(IDC_LOAD_CERTKEY, true);
  114. }
  115. virtual void disableX509Dialogs(void) {
  116. enableItem(IDC_LOAD_CERT, false);
  117. enableItem(IDC_LOAD_CERTKEY, false);
  118. }
  119. virtual void loadVncPasswd() {
  120. enableItem(IDC_AUTH_VNC_PASSWD, isItemChecked(IDC_AUTH_VNC));
  121. }
  122. protected:
  123. RegKey regKey;
  124. static bool registryInsecure;
  125. private:
  126. inline void modifyAuthMethod(int enc_idc, int auth_idc, bool enable)
  127. {
  128. setItemChecked(enc_idc, enable);
  129. setItemChecked(auth_idc, enable);
  130. }
  131. inline bool showFileChooser(const RegKey& /*rk*/,
  132. const char* title,
  133. const char* filter,
  134. HWND hwnd)
  135. {
  136. OPENFILENAME ofn;
  137. char filename[MAX_PATH];
  138. ZeroMemory(&ofn, sizeof(ofn));
  139. ZeroMemory(&filename, sizeof(filename));
  140. filename[0] = '\0';
  141. ofn.lStructSize = sizeof(ofn);
  142. ofn.hwndOwner = hwnd;
  143. ofn.lpstrFile = filename;
  144. ofn.nMaxFile = sizeof(filename);
  145. ofn.lpstrFilter = (char*)filter;
  146. ofn.nFilterIndex = 1;
  147. ofn.lpstrFileTitle = NULL;
  148. ofn.nMaxFileTitle = 0;
  149. ofn.lpstrTitle = (char*)title;
  150. ofn.lpstrInitialDir = NULL;
  151. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  152. if (GetOpenFileName(&ofn)==TRUE) {
  153. regKey.setString(title, filename);
  154. return true;
  155. }
  156. return false;
  157. }
  158. };
  159. };
  160. bool SecPage::registryInsecure = false;
  161. };
  162. #endif