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.

Legacy.cxx 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include <vncconfig/Legacy.h>
  19. #include <rfb/LogWriter.h>
  20. #include <rfb_win32/CurrentUser.h>
  21. using namespace rfb;
  22. using namespace win32;
  23. static LogWriter vlog("Legacy");
  24. void LegacyPage::LoadPrefs()
  25. {
  26. // VNC 3.3.3R3 Preferences Algorithm, as described by vncProperties.cpp
  27. // - Load user-specific settings, based on logged-on user name,
  28. // from HKLM/Software/ORL/WinVNC3/<user>. If they don't exist,
  29. // try again with username "Default".
  30. // - Load system-wide settings from HKLM/Software/ORL/WinVNC3.
  31. // - If AllowProperties is non-zero then load the user's own
  32. // settings from HKCU/Software/ORL/WinVNC3.
  33. // Get the name of the current user
  34. std::string username;
  35. try {
  36. username = UserName();
  37. } catch (rdr::SystemException& e) {
  38. if (e.err != ERROR_NOT_LOGGED_ON)
  39. throw;
  40. }
  41. // Open and read the WinVNC3 registry key
  42. allowProperties = true;
  43. RegKey winvnc3;
  44. try {
  45. winvnc3.openKey(HKEY_LOCAL_MACHINE, "Software\\ORL\\WinVNC3");
  46. int debugMode = winvnc3.getInt("DebugMode", 0);
  47. const char* debugTarget = 0;
  48. if (debugMode & 2) debugTarget = "file";
  49. if (debugMode & 4) debugTarget = "stderr";
  50. if (debugTarget) {
  51. char logSetting[32];
  52. sprintf(logSetting, "*:%s:%d", debugTarget, winvnc3.getInt("DebugLevel", 0));
  53. regKey.setString("Log", logSetting);
  54. }
  55. std::string authHosts = winvnc3.getString("AuthHosts", "");
  56. if (!authHosts.empty()) {
  57. std::string newHosts;
  58. // Reformat AuthHosts to Hosts. Wish I'd left the format the same. :( :( :(
  59. try {
  60. // Split the AuthHosts string into patterns to match
  61. std::vector<std::string> patterns;
  62. patterns = rfb::strSplit(authHosts.c_str(), ':');
  63. for (size_t i = 0; i < patterns.size(); i++) {
  64. if (!patterns[i].empty()) {
  65. int bits = 0;
  66. char pattern[1+4*4+4];
  67. pattern[0] = patterns[i][0];
  68. pattern[1] = 0;
  69. // Split the pattern into IP address parts and process
  70. std::vector<std::string> parts;
  71. parts = rfb::strSplit(&patterns[i][1], '.');
  72. for (size_t j = 0; j < parts.size(); j++) {
  73. if (bits)
  74. strcat(pattern, ".");
  75. if (parts[j].size() > 3)
  76. throw rdr::Exception("Invalid IP address part");
  77. if (!parts[j].empty()) {
  78. strcat(pattern, parts[j].c_str());
  79. bits += 8;
  80. }
  81. }
  82. // Pad out the address specification if required
  83. int addrBits = bits;
  84. while (addrBits < 32) {
  85. if (addrBits) strcat(pattern, ".");
  86. strcat(pattern, "0");
  87. addrBits += 8;
  88. }
  89. // Append the number of bits to match
  90. char buf[4];
  91. sprintf(buf, "/%d", bits);
  92. strcat(pattern, buf);
  93. // Append this pattern to the Hosts value
  94. if (!newHosts.empty())
  95. newHosts += ",";
  96. newHosts += pattern;
  97. }
  98. }
  99. // Finally, save the Hosts value
  100. regKey.setString("Hosts", newHosts.c_str());
  101. } catch (rdr::Exception&) {
  102. MsgBox(0, "Unable to convert AuthHosts setting to Hosts format.",
  103. MB_ICONWARNING | MB_OK);
  104. }
  105. } else {
  106. regKey.setString("Hosts", "+");
  107. }
  108. regKey.setBool("LocalHost", winvnc3.getBool("LoopbackOnly", false));
  109. // *** check AllowLoopback?
  110. if (winvnc3.getBool("AuthRequired", true))
  111. regKey.setString("SecurityTypes", "VncAuth");
  112. else
  113. regKey.setString("SecurityTypes", "None");
  114. int connectPriority = winvnc3.getInt("ConnectPriority", 0);
  115. regKey.setBool("DisconnectClients", connectPriority == 0);
  116. regKey.setBool("AlwaysShared", connectPriority == 1);
  117. regKey.setBool("NeverShared", connectPriority == 2);
  118. } catch(rdr::Exception&) {
  119. }
  120. // Open the local, default-user settings
  121. allowProperties = true;
  122. try {
  123. RegKey userKey;
  124. userKey.openKey(winvnc3, "Default");
  125. vlog.info("loading Default prefs");
  126. LoadUserPrefs(userKey);
  127. } catch(rdr::Exception& e) {
  128. vlog.error("error reading Default settings:%s", e.str());
  129. }
  130. // Open the local, user-specific settings
  131. if (userSettings && !username.empty()) {
  132. try {
  133. RegKey userKey;
  134. userKey.openKey(winvnc3, username.c_str());
  135. vlog.info("loading local User prefs");
  136. LoadUserPrefs(userKey);
  137. } catch(rdr::Exception& e) {
  138. vlog.error("error reading local User settings:%s", e.str());
  139. }
  140. // Open the user's own settings
  141. if (allowProperties) {
  142. try {
  143. RegKey userKey;
  144. userKey.openKey(HKEY_CURRENT_USER, "Software\\ORL\\WinVNC3");
  145. vlog.info("loading global User prefs");
  146. LoadUserPrefs(userKey);
  147. } catch(rdr::Exception& e) {
  148. vlog.error("error reading global User settings:%s", e.str());
  149. }
  150. }
  151. }
  152. // Disable the Options menu item if appropriate
  153. regKey.setBool("DisableOptions", !allowProperties);
  154. }
  155. void LegacyPage::LoadUserPrefs(const RegKey& key)
  156. {
  157. regKey.setInt("PortNumber", key.getBool("SocketConnect") ? key.getInt("PortNumber", 5900) : 0);
  158. if (key.getBool("AutoPortSelect", false)) {
  159. MsgBox(0, "The AutoPortSelect setting is not supported by this release."
  160. "The port number will default to 5900.",
  161. MB_ICONWARNING | MB_OK);
  162. regKey.setInt("PortNumber", 5900);
  163. }
  164. regKey.setInt("IdleTimeout", key.getInt("IdleTimeout", 0));
  165. regKey.setBool("RemoveWallpaper", key.getBool("RemoveWallpaper"));
  166. regKey.setBool("DisableEffects", key.getBool("DisableEffects"));
  167. if (key.getInt("QuerySetting", 2) != 2) {
  168. regKey.setBool("QueryConnect", key.getInt("QuerySetting") > 2);
  169. MsgBox(0, "The QuerySetting option has been replaced by QueryConnect."
  170. "Please see the documentation for details of the QueryConnect option.",
  171. MB_ICONWARNING | MB_OK);
  172. }
  173. regKey.setInt("QueryTimeout", key.getInt("QueryTimeout", 10));
  174. std::vector<uint8_t> passwd;
  175. passwd = key.getBinary("Password");
  176. regKey.setBinary("Password", passwd.data(), passwd.size());
  177. bool enableInputs = key.getBool("InputsEnabled", true);
  178. regKey.setBool("AcceptKeyEvents", enableInputs);
  179. regKey.setBool("AcceptPointerEvents", enableInputs);
  180. regKey.setBool("AcceptCutText", enableInputs);
  181. regKey.setBool("SendCutText", enableInputs);
  182. switch (key.getInt("LockSetting", 0)) {
  183. case 0: regKey.setString("DisconnectAction", "None"); break;
  184. case 1: regKey.setString("DisconnectAction", "Lock"); break;
  185. case 2: regKey.setString("DisconnectAction", "Logoff"); break;
  186. };
  187. regKey.setBool("DisableLocalInputs", key.getBool("LocalInputsDisabled", false));
  188. // *** ignore polling preferences
  189. // PollUnderCursor, PollForeground, OnlyPollConsole, OnlyPollOnEvent
  190. regKey.setBool("UseHooks", !key.getBool("PollFullScreen", false));
  191. if (key.isValue("AllowShutdown"))
  192. MsgBox(0, "The AllowShutdown option is not supported by this release.", MB_ICONWARNING | MB_OK);
  193. if (key.isValue("AllowEditClients"))
  194. MsgBox(0, "The AllowEditClients option is not supported by this release.", MB_ICONWARNING | MB_OK);
  195. allowProperties = key.getBool("AllowProperties", allowProperties);
  196. }