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