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