blob: 6c9e1c5ad8dd76a242755f40e6f9138e9c84191d [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
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000019#include <windows.h>
20#include <commctrl.h>
21#include <string.h>
22#ifdef WIN32
23#define strcasecmp _stricmp
24#endif
25
26#include "resource.h"
27#include <rfb/Logger_stdio.h>
28#include <rfb/LogWriter.h>
29#include <rfb/SSecurityFactoryStandard.h>
30#include <rfb_win32/Dialog.h>
31#include <rfb_win32/RegConfig.h>
32#include <rfb_win32/CurrentUser.h>
33
34using namespace rfb;
35using namespace rfb::win32;
36
37static LogWriter vlog("main");
38
39
40#include <vncconfig/Authentication.h>
41#include <vncconfig/Connections.h>
42#include <vncconfig/Sharing.h>
43#include <vncconfig/Hooking.h>
44#include <vncconfig/Inputs.h>
45#include <vncconfig/Legacy.h>
46#include <vncconfig/Desktop.h>
47
48
49TStr rfb::win32::AppName("VNC Config");
50
51
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000052#ifdef _DEBUG
53BoolParameter captureDialogs("CaptureDialogs", "", false);
54#endif
55
56HKEY configKey = HKEY_CURRENT_USER;
57
58
59void
60processParams(int argc, char* argv[]) {
61 for (int i=1; i<argc; i++) {
62 if (strcasecmp(argv[i], "-service") == 0) {
63 configKey = HKEY_LOCAL_MACHINE;
64 } else if (strcasecmp(argv[i], "-user") == 0) {
65 configKey = HKEY_CURRENT_USER;
66 } else {
67 // Try to process <option>=<value>, or -<bool>
68 if (Configuration::setParam(argv[i], true))
69 continue;
70 // Try to process -<option> <value>
71 if ((argv[i][0] == '-') && (i+1 < argc)) {
72 if (Configuration::setParam(&argv[i][1], argv[i+1], true)) {
73 i++;
74 continue;
75 }
76 }
77 }
78 }
79}
80
81
82int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, char* cmdLine, int cmdShow) {
83
84 // Configure debugging output
85#ifdef _DEBUG
86 AllocConsole();
87 freopen("CONIN$","rb",stdin);
88 freopen("CONOUT$","wb",stdout);
89 freopen("CONOUT$","wb",stderr);
90 setbuf(stderr, 0);
91 initStdIOLoggers();
92 LogWriter vlog("main");
93 logParams.setParam("*:stderr:100");
94 vlog.info("Starting vncconfig applet");
95#endif
96
97 try {
98 try {
99 // Process command-line args
100 int argc = __argc;
101 char** argv = __argv;
102 processParams(argc, argv);
103
104 /* *** Required if we wish to use IP address control
105 INITCOMMONCONTROLSEX icce;
106 icce.dwSize = sizeof(icce);
107 icce.dwICC = ICC_INTERNET_CLASSES;
108 InitCommonControlsEx(&icce);
109 */
110
111 // Create the required configuration registry key
112 RegKey rootKey;
Peter Åstrand9fb4e0e2004-12-30 10:03:00 +0000113 rootKey.createKey(configKey, _T("Software\\TightVNC\\WinVNC4"));
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000114
115 // Override whatever security it already had (NT only)
116 bool warnOnChangePassword = false;
117 try {
118 AccessEntries access;
Constantin Kaplinsky7f8d7742006-05-11 05:29:14 +0000119 Sid::Administrators adminSID;
120 Sid::SYSTEM systemSID;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000121 access.addEntry(adminSID, KEY_ALL_ACCESS, GRANT_ACCESS);
122 access.addEntry(systemSID, KEY_ALL_ACCESS, GRANT_ACCESS);
Constantin Kaplinsky7f8d7742006-05-11 05:29:14 +0000123 UserSID userSID;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000124 if (configKey == HKEY_CURRENT_USER)
Constantin Kaplinsky7f8d7742006-05-11 05:29:14 +0000125 access.addEntry(userSID, KEY_ALL_ACCESS, GRANT_ACCESS);
126 AccessControlList acl(CreateACL(access));
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000127
128 // Set the DACL, and don't allow the key to inherit its parent's DACL
129 rootKey.setDACL(acl, false);
130 } catch (rdr::SystemException& e) {
131 // Something weird happens on NT 4.0 SP5 but I can't reproduce it on other
132 // NT 4.0 service pack revisions.
133 if (e.err == ERROR_INVALID_PARAMETER) {
134 MsgBox(0, _T("Windows reported an error trying to secure the VNC Server settings for this user. ")
135 _T("Your settings may not be secure!"), MB_ICONWARNING | MB_OK);
136 } else if (e.err != ERROR_CALL_NOT_IMPLEMENTED &&
137 e.err != ERROR_NOT_LOGGED_ON) {
138 // If the call is not implemented, ignore the error and continue
139 // If we are on Win9x and no user is logged on, ignore error and continue
140 throw;
141 }
142 warnOnChangePassword = true;
143 }
144
Constantin Kaplinsky7f8d7742006-05-11 05:29:14 +0000145 // Start a RegConfig thread, to load in existing settings
146 RegConfigThread config;
147 config.start(configKey, _T("Software\\TightVNC\\WinVNC4"));
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000148
149 // Build the dialog
150 std::list<PropSheetPage*> pages;
151 AuthenticationPage auth(rootKey); pages.push_back(&auth);
152 auth.setWarnPasswdInsecure(warnOnChangePassword);
153 ConnectionsPage conn(rootKey); pages.push_back(&conn);
154 InputsPage inputs(rootKey); pages.push_back(&inputs);
155 SharingPage sharing(rootKey); pages.push_back(&sharing);
156 DesktopPage desktop(rootKey); pages.push_back(&desktop);
157 HookingPage hooks(rootKey); pages.push_back(&hooks);
158 LegacyPage legacy(rootKey, configKey == HKEY_CURRENT_USER); pages.push_back(&legacy);
159
160 // Load the default icon to use
161 HICON icon = (HICON)LoadImage(inst, MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);
162
163 // Create the PropertySheet handler
164 TCHAR* propSheetTitle = _T("VNC Server Properties (Service-Mode)");
165 if (configKey == HKEY_CURRENT_USER)
166 propSheetTitle = _T("VNC Server Properties (User-Mode)");
167 PropSheet sheet(inst, propSheetTitle, pages, icon);
168
169#ifdef _DEBUG
170 vlog.debug("capture dialogs=%s", captureDialogs ? "true" : "false");
171 sheet.showPropSheet(0, true, false, captureDialogs);
172#else
173 sheet.showPropSheet(0, true, false);
174#endif
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000175 } catch (rdr::SystemException& e) {
176 switch (e.err) {
177 case ERROR_ACCESS_DENIED:
178 MsgBox(0, _T("You do not have sufficient access rights to run the VNC Configuration applet"),
179 MB_ICONSTOP | MB_OK);
180 return 1;
181 };
182 throw;
183 }
Constantin Kaplinsky7f8d7742006-05-11 05:29:14 +0000184
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000185 } catch (rdr::Exception& e) {
186 MsgBox(NULL, TStr(e.str()), MB_ICONEXCLAMATION | MB_OK);
187 return 1;
188 }
Constantin Kaplinsky7f8d7742006-05-11 05:29:14 +0000189
190 return 0;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000191}