Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 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 | // -=- WinVNC Version 4.0 Main Routine |
| 20 | |
| 21 | #include <winvnc/VNCServerWin32.h> |
| 22 | #include <winvnc/resource.h> |
| 23 | #include <winvnc/STrayIcon.h> |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 24 | |
| 25 | #include <os/Mutex.h> |
| 26 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 27 | #include <rfb_win32/ComputerName.h> |
| 28 | #include <rfb_win32/CurrentUser.h> |
| 29 | #include <rfb_win32/Service.h> |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 30 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 31 | #include <rfb/Hostname.h> |
| 32 | #include <rfb/LogWriter.h> |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace rfb; |
| 35 | using namespace win32; |
| 36 | using namespace winvnc; |
| 37 | using namespace network; |
| 38 | |
| 39 | static LogWriter vlog("VNCServerWin32"); |
| 40 | |
| 41 | |
Peter Åstrand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 42 | const TCHAR* winvnc::VNCServerWin32::RegConfigPath = _T("Software\\TigerVNC\\WinVNC4"); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 43 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 44 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 45 | static IntParameter port_number("PortNumber", |
| 46 | "TCP/IP port on which the server will accept connections", 5900); |
| 47 | static StringParameter hosts("Hosts", |
Pierre Ossman | 5b90c5f | 2015-03-17 13:45:27 +0100 | [diff] [blame] | 48 | "Filter describing which hosts are allowed access to this server", "+"); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 49 | static BoolParameter localHost("LocalHost", |
| 50 | "Only accept connections from via the local loop-back network interface", false); |
| 51 | static BoolParameter queryOnlyIfLoggedOn("QueryOnlyIfLoggedOn", |
| 52 | "Only prompt for a local user to accept incoming connections if there is a user logged on", false); |
Adam Tkac | e1f2a52 | 2010-05-13 13:12:40 +0000 | [diff] [blame] | 53 | static BoolParameter showTrayIcon("ShowTrayIcon", |
| 54 | "Show the configuration applet in the system tray icon", true); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | VNCServerWin32::VNCServerWin32() |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 58 | : command(NoCommand), |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 59 | commandEvent(CreateEvent(0, TRUE, FALSE, 0)), |
Samuel Mannehed | 60c4193 | 2014-02-07 14:53:24 +0000 | [diff] [blame] | 60 | sessionEvent(isServiceProcess() ? |
| 61 | CreateEvent(0, FALSE, FALSE, "Global\\SessionEventTigerVNC") : 0), |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 62 | vncServer(CStr(ComputerName().buf), &desktop), |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 63 | thread_id(-1), runServer(false), isDesktopStarted(false), |
Pierre Ossman | 4a4453f | 2018-10-09 10:23:59 +0200 | [diff] [blame^] | 64 | config(&sockMgr), rfbSock(&sockMgr), trayIcon(0), |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 65 | queryConnectDialog(0) |
| 66 | { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 67 | commandLock = new os::Mutex; |
| 68 | commandSig = new os::Condition(commandLock); |
| 69 | |
| 70 | runLock = new os::Mutex; |
| 71 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 72 | // Initialise the desktop |
| 73 | desktop.setStatusLocation(&isDesktopStarted); |
| 74 | |
| 75 | // Initialise the VNC server |
| 76 | vncServer.setQueryConnectionHandler(this); |
| 77 | |
| 78 | // Register the desktop's event to be handled |
| 79 | sockMgr.addEvent(desktop.getUpdateEvent(), &desktop); |
| 80 | |
| 81 | // Register the queued command event to be handled |
| 82 | sockMgr.addEvent(commandEvent, this); |
Samuel Mannehed | 60c4193 | 2014-02-07 14:53:24 +0000 | [diff] [blame] | 83 | if (sessionEvent) |
| 84 | sockMgr.addEvent(sessionEvent, this); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | VNCServerWin32::~VNCServerWin32() { |
| 88 | delete trayIcon; |
| 89 | |
| 90 | // Stop the SDisplay from updating our state |
| 91 | desktop.setStatusLocation(0); |
| 92 | |
| 93 | // Join the Accept/Reject dialog thread |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 94 | if (queryConnectDialog) { |
| 95 | queryConnectDialog->wait(); |
| 96 | delete queryConnectDialog; |
| 97 | } |
| 98 | |
| 99 | delete runLock; |
| 100 | |
| 101 | delete commandSig; |
| 102 | delete commandLock; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
Pierre Ossman | 79f82f9 | 2015-03-17 13:44:00 +0100 | [diff] [blame] | 106 | void VNCServerWin32::processAddressChange() { |
| 107 | if (!trayIcon) |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 108 | return; |
| 109 | |
| 110 | // Tool-tip prefix depends on server mode |
| 111 | const TCHAR* prefix = _T("VNC Server (User):"); |
| 112 | if (isServiceProcess()) |
| 113 | prefix = _T("VNC Server (Service):"); |
| 114 | |
| 115 | // Fetch the list of addresses |
| 116 | std::list<char*> addrs; |
Pierre Ossman | 57cab51 | 2015-03-17 13:39:39 +0100 | [diff] [blame] | 117 | if (rfbSock.isListening()) |
| 118 | TcpListener::getMyAddresses(&addrs); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 119 | else |
| 120 | addrs.push_front(strDup("Not accepting connections")); |
| 121 | |
| 122 | // Allocate space for the new tip |
| 123 | std::list<char*>::iterator i, next_i; |
| 124 | int length = _tcslen(prefix)+1; |
| 125 | for (i=addrs.begin(); i!= addrs.end(); i++) |
| 126 | length += strlen(*i) + 1; |
| 127 | |
| 128 | // Build the new tip |
| 129 | TCharArray toolTip(length); |
| 130 | _tcscpy(toolTip.buf, prefix); |
| 131 | for (i=addrs.begin(); i!= addrs.end(); i=next_i) { |
| 132 | next_i = i; next_i ++; |
Adam Tkac | 934f63c | 2009-10-12 15:54:59 +0000 | [diff] [blame] | 133 | TCharArray addr(*i); // Assumes ownership of string |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 134 | _tcscat(toolTip.buf, addr.buf); |
| 135 | if (next_i != addrs.end()) |
| 136 | _tcscat(toolTip.buf, _T(",")); |
| 137 | } |
| 138 | |
| 139 | // Pass the new tip to the tray icon |
| 140 | vlog.info("Refreshing tray icon"); |
| 141 | trayIcon->setToolTip(toolTip.buf); |
| 142 | } |
| 143 | |
| 144 | void VNCServerWin32::regConfigChanged() { |
| 145 | // -=- Make sure we're listening on the right ports. |
| 146 | rfbSock.setServer(&vncServer); |
| 147 | rfbSock.setPort(port_number, localHost); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 148 | |
| 149 | // -=- Update the TCP address filter for both ports, if open. |
| 150 | CharArray pattern(hosts.getData()); |
| 151 | rfbSock.setFilter(pattern.buf); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 152 | |
| 153 | // -=- Update the tray icon tooltip text with IP addresses |
Pierre Ossman | 79f82f9 | 2015-03-17 13:44:00 +0100 | [diff] [blame] | 154 | processAddressChange(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
| 158 | int VNCServerWin32::run() { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 159 | { |
| 160 | os::AutoMutex a(runLock); |
| 161 | thread_id = GetCurrentThreadId(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 162 | runServer = true; |
| 163 | } |
| 164 | |
| 165 | // - Create the tray icon (if possible) |
Adam Tkac | e1f2a52 | 2010-05-13 13:12:40 +0000 | [diff] [blame] | 166 | if (showTrayIcon) |
| 167 | trayIcon = new STrayIconThread(*this, IDI_ICON, IDI_CONNECTED, |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 168 | IDI_ICON_DISABLE, IDI_CONNECTED_DISABLE, |
| 169 | IDR_TRAY); |
| 170 | |
| 171 | // - Register for notification of configuration changes |
| 172 | config.setCallback(this); |
| 173 | if (isServiceProcess()) |
| 174 | config.setKey(HKEY_LOCAL_MACHINE, RegConfigPath); |
| 175 | else |
| 176 | config.setKey(HKEY_CURRENT_USER, RegConfigPath); |
| 177 | |
| 178 | // - Set the address-changed handler for the RFB socket |
| 179 | rfbSock.setAddressChangeNotifier(this); |
| 180 | |
| 181 | DWORD result = 0; |
| 182 | try { |
| 183 | vlog.debug("Entering message loop"); |
| 184 | |
| 185 | // - Run the server until we're told to quit |
| 186 | MSG msg; |
| 187 | int result = 0; |
| 188 | while (runServer) { |
| 189 | result = sockMgr.getMessage(&msg, NULL, 0, 0); |
| 190 | if (result < 0) |
| 191 | throw rdr::SystemException("getMessage", GetLastError()); |
| 192 | if (!isServiceProcess() && (result == 0)) |
| 193 | break; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 194 | TranslateMessage(&msg); |
| 195 | DispatchMessage(&msg); |
| 196 | } |
| 197 | |
| 198 | vlog.debug("Server exited cleanly"); |
| 199 | } catch (rdr::SystemException &s) { |
Pierre Ossman | cd5c82a | 2015-03-03 16:48:58 +0100 | [diff] [blame] | 200 | vlog.error("%s", s.str()); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 201 | result = s.err; |
| 202 | } catch (rdr::Exception &e) { |
Pierre Ossman | cd5c82a | 2015-03-03 16:48:58 +0100 | [diff] [blame] | 203 | vlog.error("%s", e.str()); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 206 | { |
| 207 | os::AutoMutex a(runLock); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 208 | runServer = false; |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 209 | thread_id = (DWORD)-1; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | return result; |
| 213 | } |
| 214 | |
| 215 | void VNCServerWin32::stop() { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 216 | os::AutoMutex a(runLock); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 217 | runServer = false; |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 218 | if (thread_id != (DWORD)-1) |
| 219 | PostThreadMessage(thread_id, WM_QUIT, 0, 0); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | |
| 223 | bool VNCServerWin32::disconnectClients(const char* reason) { |
| 224 | return queueCommand(DisconnectClients, reason, 0); |
| 225 | } |
| 226 | |
| 227 | bool VNCServerWin32::addNewClient(const char* client) { |
| 228 | TcpSocket* sock = 0; |
| 229 | try { |
| 230 | CharArray hostname; |
| 231 | int port; |
| 232 | getHostAndPort(client, &hostname.buf, &port, 5500); |
| 233 | vlog.error("port=%d", port); |
| 234 | sock = new TcpSocket(hostname.buf, port); |
| 235 | if (queueCommand(AddClient, sock, 0)) |
| 236 | return true; |
| 237 | delete sock; |
| 238 | } catch (...) { |
| 239 | delete sock; |
| 240 | } |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | bool VNCServerWin32::getClientsInfo(rfb::ListConnInfo* LCInfo) { |
| 245 | return queueCommand(GetClientsInfo, LCInfo, 0); |
| 246 | } |
| 247 | |
| 248 | bool VNCServerWin32::setClientsStatus(rfb::ListConnInfo* LCInfo) { |
| 249 | return queueCommand(SetClientsStatus, LCInfo, 0); |
| 250 | } |
| 251 | |
| 252 | VNCServerST::queryResult VNCServerWin32::queryConnection(network::Socket* sock, |
| 253 | const char* userName, |
| 254 | char** reason) |
| 255 | { |
| 256 | if (queryOnlyIfLoggedOn && CurrentUserToken().noUserLoggedOn()) |
| 257 | return VNCServerST::ACCEPT; |
| 258 | if (queryConnectDialog) { |
| 259 | *reason = rfb::strDup("Another connection is currently being queried."); |
| 260 | return VNCServerST::REJECT; |
| 261 | } |
| 262 | queryConnectDialog = new QueryConnectDialog(sock, userName, this); |
| 263 | queryConnectDialog->startDialog(); |
| 264 | return VNCServerST::PENDING; |
| 265 | } |
| 266 | |
| 267 | void VNCServerWin32::queryConnectionComplete() { |
| 268 | queueCommand(QueryConnectionComplete, 0, 0, false); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | bool VNCServerWin32::queueCommand(Command cmd, const void* data, int len, bool wait) { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 273 | os::AutoMutex a(commandLock); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 274 | while (command != NoCommand) |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 275 | commandSig->wait(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 276 | command = cmd; |
| 277 | commandData = data; |
| 278 | commandDataLen = len; |
| 279 | SetEvent(commandEvent); |
| 280 | if (wait) { |
| 281 | while (command != NoCommand) |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 282 | commandSig->wait(); |
| 283 | commandSig->signal(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | void VNCServerWin32::processEvent(HANDLE event_) { |
| 289 | ResetEvent(event_); |
| 290 | |
| 291 | if (event_ == commandEvent.h) { |
| 292 | // If there is no command queued then return immediately |
| 293 | { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 294 | os::AutoMutex a(commandLock); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 295 | if (command == NoCommand) |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | // Perform the required command |
| 300 | switch (command) { |
| 301 | |
| 302 | case DisconnectClients: |
| 303 | // Disconnect all currently active VNC Viewers |
| 304 | vncServer.closeClients((const char*)commandData); |
| 305 | break; |
| 306 | |
| 307 | case AddClient: |
| 308 | // Make a reverse connection to a VNC Viewer |
| 309 | sockMgr.addSocket((network::Socket*)commandData, &vncServer); |
| 310 | break; |
| 311 | case GetClientsInfo: |
| 312 | vncServer.getConnInfo((ListConnInfo*)commandData); |
| 313 | break; |
| 314 | case SetClientsStatus: |
| 315 | vncServer.setConnStatus((ListConnInfo*)commandData); |
| 316 | break; |
| 317 | |
| 318 | case QueryConnectionComplete: |
| 319 | // The Accept/Reject dialog has completed |
| 320 | // Get the result, then clean it up |
| 321 | vncServer.approveConnection(queryConnectDialog->getSock(), |
| 322 | queryConnectDialog->isAccepted(), |
| 323 | "Connection rejected by user"); |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 324 | queryConnectDialog->wait(); |
| 325 | delete queryConnectDialog; |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 326 | queryConnectDialog = 0; |
| 327 | break; |
| 328 | |
| 329 | default: |
| 330 | vlog.error("unknown command %d queued", command); |
| 331 | }; |
| 332 | |
| 333 | // Clear the command and signal completion |
| 334 | { |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 335 | os::AutoMutex a(commandLock); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 336 | command = NoCommand; |
Pierre Ossman | 338e73a | 2016-07-07 15:35:13 +0200 | [diff] [blame] | 337 | commandSig->signal(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 338 | } |
Samuel Mannehed | 60c4193 | 2014-02-07 14:53:24 +0000 | [diff] [blame] | 339 | } else if (event_ == sessionEvent.h) { |
| 340 | stop(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |