summaryrefslogtreecommitdiffstats
path: root/win/vncconfig/vncconfig.cxx
blob: b1d0eef3ec4822f4d0d8e99be930b233246a2919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#include <winsock2.h>
#include <windows.h>
#include <commctrl.h>
#include <string.h>

#include "resource.h"
#include <rfb/Logger_stdio.h>
#include <rfb/LogWriter.h>
#include <rfb_win32/Dialog.h>
#include <rfb_win32/RegConfig.h>
#include <rfb_win32/CurrentUser.h>

using namespace rfb;
using namespace rfb::win32;

static LogWriter vlog("main");


#include <vncconfig/Authentication.h>
#include <vncconfig/Connections.h>
#include <vncconfig/Sharing.h>
#include <vncconfig/Hooking.h>
#include <vncconfig/Inputs.h>
#include <vncconfig/Legacy.h>
#include <vncconfig/Desktop.h>


TStr rfb::win32::AppName("TigerVNC Configuration");


#ifdef _DEBUG
BoolParameter captureDialogs("CaptureDialogs", "", false);
#endif

HKEY configKey = HKEY_CURRENT_USER;


void
processParams(int argc, char* argv[]) {
  for (int i=1; i<argc; i++) {
    if (strcasecmp(argv[i], "-service") == 0) {
      configKey = HKEY_LOCAL_MACHINE;
    } else if (strcasecmp(argv[i], "-user") == 0) {
      configKey = HKEY_CURRENT_USER;
    } else {
      // Try to process <option>=<value>, or -<bool>
      if (Configuration::setParam(argv[i], true))
        continue;
      // Try to process -<option> <value>
      if ((argv[i][0] == '-') && (i+1 < argc)) {
        if (Configuration::setParam(&argv[i][1], argv[i+1], true)) {
          i++;
          continue;
        }
      }
    }
  }
}


int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, char* cmdLine, int cmdShow) {

  // Configure debugging output
#ifdef _DEBUG
  AllocConsole();
  freopen("CONIN$","rb",stdin);
  freopen("CONOUT$","wb",stdout);
  freopen("CONOUT$","wb",stderr);
  setbuf(stderr, 0);
  initStdIOLoggers();
  LogWriter vlog("main");
  logParams.setParam("*:stderr:100");
  vlog.info("Starting vncconfig applet");
#endif

  Configuration::enableServerParams();

  try {
    try {
      // Process command-line args
      int argc = __argc;
      char** argv = __argv;
      processParams(argc, argv);

      /* *** Required if we wish to use IP address control
      INITCOMMONCONTROLSEX icce;
      icce.dwSize = sizeof(icce);
      icce.dwICC = ICC_INTERNET_CLASSES;
      InitCommonControlsEx(&icce);
      */

      // Create the required configuration registry key
      RegKey rootKey;
      rootKey.createKey(configKey, _T("Software\\TigerVNC\\WinVNC4"));
  
      // Override whatever security it already had (NT only)
      bool warnOnChangePassword = false;
      try {
        AccessEntries access;
        Sid::Administrators adminSID;
        Sid::SYSTEM systemSID;
        access.addEntry(adminSID, KEY_ALL_ACCESS, GRANT_ACCESS);
        access.addEntry(systemSID, KEY_ALL_ACCESS, GRANT_ACCESS);
        UserSID userSID;
        if (configKey == HKEY_CURRENT_USER)
          access.addEntry(userSID, KEY_ALL_ACCESS, GRANT_ACCESS);
        AccessControlList acl(CreateACL(access));

        // Set the DACL, and don't allow the key to inherit its parent's DACL
        rootKey.setDACL(acl, false);
      } catch (rdr::SystemException& e) {
        // Something weird happens on NT 4.0 SP5 but I can't reproduce it on other
        // NT 4.0 service pack revisions.
        if (e.err == ERROR_INVALID_PARAMETER) {
          MsgBox(0, _T("Windows reported an error trying to secure the VNC Server settings for this user.  ")
                    _T("Your settings may not be secure!"), MB_ICONWARNING | MB_OK);
        } else if (e.err != ERROR_CALL_NOT_IMPLEMENTED &&
                   e.err != ERROR_NOT_LOGGED_ON) {
          // If the call is not implemented, ignore the error and continue
          // If we are on Win9x and no user is logged on, ignore error and continue
          throw;
        }
        warnOnChangePassword = true;
      }

      // Start a RegConfig thread, to load in existing settings
      RegConfigThread config;
      config.start(configKey, _T("Software\\TigerVNC\\WinVNC4"));

      // Build the dialog
      std::list<PropSheetPage*> pages;
      SecPage auth(rootKey); pages.push_back(&auth);
      auth.setWarnPasswdInsecure(warnOnChangePassword);
      ConnectionsPage conn(rootKey); pages.push_back(&conn);
      InputsPage inputs(rootKey); pages.push_back(&inputs);
      SharingPage sharing(rootKey); pages.push_back(&sharing);
      DesktopPage desktop(rootKey); pages.push_back(&desktop);
      HookingPage hooks(rootKey); pages.push_back(&hooks);
      LegacyPage legacy(rootKey, configKey == HKEY_CURRENT_USER); pages.push_back(&legacy);

      // Load the default icon to use
      HICON icon = (HICON)LoadImage(inst, MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, LR_SHARED);

      // Create the PropertySheet handler
      const TCHAR* propSheetTitle = _T("VNC Server Properties (Service-Mode)");
      if (configKey == HKEY_CURRENT_USER)
        propSheetTitle = _T("VNC Server Properties (User-Mode)");
      PropSheet sheet(inst, propSheetTitle, pages, icon);

#ifdef _DEBUG
      vlog.debug("capture dialogs=%s", captureDialogs ? "true" : "false");
      sheet.showPropSheet(0, true, false, captureDialogs);
#else
      sheet.showPropSheet(0, true, false);
#endif
    } catch (rdr::SystemException& e) {
      switch (e.err) {
      case ERROR_ACCESS_DENIED:
        MsgBox(0, _T("You do not have sufficient access rights to run the VNC Configuration applet"),
               MB_ICONSTOP | MB_OK);
        return 1;
      };
      throw;
    }

  } catch (rdr::Exception& e) {
    MsgBox(NULL, TStr(e.str()), MB_ICONEXCLAMATION | MB_OK);
    return 1;
  }

  return 0;
}