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 9.9KB

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