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