blob: d0490de5502c03fcad3ba65bc14d7b296f92f53a [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Pierre Ossmanc754cce2011-11-14 15:44:11 +00002 * Copyright 2011 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19#include <stdio.h>
20#include <string.h>
21#include <rfb/Exception.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000022#include <rfb/Security.h>
Constantin Kaplinskydafbb012007-04-05 08:43:25 +000023#include <rfb/msgTypes.h>
Pierre Ossmanc754cce2011-11-14 15:44:11 +000024#include <rfb/fenceTypes.h>
Pierre Ossman7638e9c2014-01-16 13:12:40 +010025#include <rfb/SMsgReader.h>
26#include <rfb/SMsgWriter.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000027#include <rfb/SConnection.h>
28#include <rfb/ServerCore.h>
Pierre Ossman48700812014-09-17 17:11:56 +020029#include <rfb/encodings.h>
30#include <rfb/EncodeManager.h>
Michal Srb8d1ee002014-11-10 09:15:41 +020031#include <rfb/SSecurity.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032
33#include <rfb/LogWriter.h>
34
35using namespace rfb;
36
37static LogWriter vlog("SConnection");
38
39// AccessRights values
Michal Srbb318b8f2014-11-24 13:18:28 +020040const SConnection::AccessRights SConnection::AccessView = 0x0001;
41const SConnection::AccessRights SConnection::AccessKeyEvents = 0x0002;
42const SConnection::AccessRights SConnection::AccessPtrEvents = 0x0004;
43const SConnection::AccessRights SConnection::AccessCutText = 0x0008;
44const SConnection::AccessRights SConnection::AccessSetDesktopSize = 0x0010;
Pierre Ossmane7be49b2014-12-02 14:33:17 +010045const SConnection::AccessRights SConnection::AccessNonShared = 0x0020;
Michal Srbb318b8f2014-11-24 13:18:28 +020046const SConnection::AccessRights SConnection::AccessDefault = 0x03ff;
47const SConnection::AccessRights SConnection::AccessNoQuery = 0x0400;
48const SConnection::AccessRights SConnection::AccessFull = 0xffff;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000049
50
Pierre Ossman7069bdd2015-02-06 14:41:58 +010051SConnection::SConnection()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000052 : readyForSetColourMapEntries(false),
53 is(0), os(0), reader_(0), writer_(0),
Michal Srbdccb5f72017-03-27 13:55:46 +030054 ssecurity(0), state_(RFBSTATE_UNINITIALISED),
Pierre Ossman48700812014-09-17 17:11:56 +020055 preferredEncoding(encodingRaw)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056{
57 defaultMajorVersion = 3;
58 defaultMinorVersion = 8;
59 if (rfb::Server::protocol3_3)
60 defaultMinorVersion = 3;
61
Pierre Ossman0d3ce872018-06-18 15:59:00 +020062 client.setVersion(defaultMajorVersion, defaultMinorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000063}
64
65SConnection::~SConnection()
66{
Pierre Ossman82d22e62018-09-21 15:26:37 +020067 if (ssecurity)
68 delete ssecurity;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000069 delete reader_;
70 reader_ = 0;
71 delete writer_;
72 writer_ = 0;
73}
74
75void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
76{
77 is = is_;
78 os = os_;
79}
80
81void SConnection::initialiseProtocol()
82{
Pierre Ossmanea7ede92018-06-18 16:51:53 +020083 char str[13];
84
85 sprintf(str, "RFB %03d.%03d\n", defaultMajorVersion, defaultMinorVersion);
86 os->writeBytes(str, 12);
87 os->flush();
88
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089 state_ = RFBSTATE_PROTOCOL_VERSION;
90}
91
92void SConnection::processMsg()
93{
94 switch (state_) {
95 case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break;
96 case RFBSTATE_SECURITY_TYPE: processSecurityTypeMsg(); break;
97 case RFBSTATE_SECURITY: processSecurityMsg(); break;
98 case RFBSTATE_INITIALISATION: processInitMsg(); break;
99 case RFBSTATE_NORMAL: reader_->readMsg(); break;
100 case RFBSTATE_QUERYING:
101 throw Exception("SConnection::processMsg: bogus data from client while "
102 "querying");
103 case RFBSTATE_UNINITIALISED:
104 throw Exception("SConnection::processMsg: not initialised yet?");
105 default:
106 throw Exception("SConnection::processMsg: invalid state");
107 }
108}
109
110void SConnection::processVersionMsg()
111{
Pierre Ossmanea7ede92018-06-18 16:51:53 +0200112 char verStr[13];
113 int majorVersion;
114 int minorVersion;
115
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116 vlog.debug("reading protocol version");
Pierre Ossmanea7ede92018-06-18 16:51:53 +0200117
118 if (!is->checkNoWait(12))
119 return;
120
121 is->readBytes(verStr, 12);
122 verStr[12] = '\0';
123
124 if (sscanf(verStr, "RFB %03d.%03d\n",
125 &majorVersion, &minorVersion) != 2) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000126 state_ = RFBSTATE_INVALID;
127 throw Exception("reading version failed: not an RFB client?");
128 }
Pierre Ossmanea7ede92018-06-18 16:51:53 +0200129
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200130 client.setVersion(majorVersion, minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000131
132 vlog.info("Client needs protocol version %d.%d",
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200133 client.majorVersion, client.minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000134
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200135 if (client.majorVersion != 3) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000136 // unknown protocol version
Pierre Ossman19225502017-10-12 15:05:07 +0200137 throwConnFailedException("Client needs protocol version %d.%d, server has %d.%d",
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200138 client.majorVersion, client.minorVersion,
Pierre Ossman19225502017-10-12 15:05:07 +0200139 defaultMajorVersion, defaultMinorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000140 }
141
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200142 if (client.minorVersion != 3 && client.minorVersion != 7 && client.minorVersion != 8) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000143 vlog.error("Client uses unofficial protocol version %d.%d",
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200144 client.majorVersion,client.minorVersion);
145 if (client.minorVersion >= 8)
146 client.minorVersion = 8;
147 else if (client.minorVersion == 7)
148 client.minorVersion = 7;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000149 else
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200150 client.minorVersion = 3;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000151 vlog.error("Assuming compatibility with version %d.%d",
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200152 client.majorVersion,client.minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000153 }
154
155 versionReceived();
156
157 std::list<rdr::U8> secTypes;
158 std::list<rdr::U8>::iterator i;
Michal Srbdccb5f72017-03-27 13:55:46 +0300159 secTypes = security.GetEnabledSecTypes();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000160
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200161 if (client.isVersion(3,3)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000162
163 // cope with legacy 3.3 client only if "no authentication" or "vnc
164 // authentication" is supported.
165 for (i=secTypes.begin(); i!=secTypes.end(); i++) {
166 if (*i == secTypeNone || *i == secTypeVncAuth) break;
167 }
168 if (i == secTypes.end()) {
Pierre Ossman19225502017-10-12 15:05:07 +0200169 throwConnFailedException("No supported security type for %d.%d client",
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200170 client.majorVersion, client.minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171 }
172
173 os->writeU32(*i);
174 if (*i == secTypeNone) os->flush();
175 state_ = RFBSTATE_SECURITY;
Pierre Ossmanad2b3c42018-09-21 15:31:11 +0200176 ssecurity = security.GetSSecurity(this, *i);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177 processSecurityMsg();
178 return;
179 }
180
181 // list supported security types for >=3.7 clients
182
183 if (secTypes.empty())
184 throwConnFailedException("No supported security types");
185
186 os->writeU8(secTypes.size());
187 for (i=secTypes.begin(); i!=secTypes.end(); i++)
188 os->writeU8(*i);
189 os->flush();
190 state_ = RFBSTATE_SECURITY_TYPE;
191}
192
193
194void SConnection::processSecurityTypeMsg()
195{
196 vlog.debug("processing security type message");
197 int secType = is->readU8();
198
Constantin Kaplinsky5fa9d222006-09-06 10:32:06 +0000199 processSecurityType(secType);
200}
201
202void SConnection::processSecurityType(int secType)
203{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204 // Verify that the requested security type should be offered
205 std::list<rdr::U8> secTypes;
206 std::list<rdr::U8>::iterator i;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000207
Michal Srbdccb5f72017-03-27 13:55:46 +0300208 secTypes = security.GetEnabledSecTypes();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209 for (i=secTypes.begin(); i!=secTypes.end(); i++)
210 if (*i == secType) break;
211 if (i == secTypes.end())
212 throw Exception("Requested security type not available");
213
214 vlog.info("Client requests security type %s(%d)",
215 secTypeName(secType),secType);
216
217 try {
218 state_ = RFBSTATE_SECURITY;
Pierre Ossmanad2b3c42018-09-21 15:31:11 +0200219 ssecurity = security.GetSSecurity(this, secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000220 } catch (rdr::Exception& e) {
Pierre Ossman19225502017-10-12 15:05:07 +0200221 throwConnFailedException("%s", e.str());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000222 }
223
224 processSecurityMsg();
225}
226
227void SConnection::processSecurityMsg()
228{
229 vlog.debug("processing security message");
230 try {
Pierre Ossmanad2b3c42018-09-21 15:31:11 +0200231 bool done = ssecurity->processMsg();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000232 if (done) {
233 state_ = RFBSTATE_QUERYING;
Michal Srb8d1ee002014-11-10 09:15:41 +0200234 setAccessRights(ssecurity->getAccessRights());
Henrik Anderssonc1cbc702016-01-27 14:00:44 +0100235 queryConnection(ssecurity->getUserName());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236 }
237 } catch (AuthFailureException& e) {
238 vlog.error("AuthFailureException: %s", e.str());
239 os->writeU32(secResultFailed);
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200240 if (!client.beforeVersion(3,8)) // 3.8 onwards have failure message
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000241 os->writeString(e.str());
242 os->flush();
243 throw;
244 }
245}
246
247void SConnection::processInitMsg()
248{
249 vlog.debug("reading client initialisation");
250 reader_->readClientInit();
251}
252
Pierre Ossman19225502017-10-12 15:05:07 +0200253void SConnection::throwConnFailedException(const char* format, ...)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000254{
Pierre Ossman19225502017-10-12 15:05:07 +0200255 va_list ap;
256 char str[256];
257
258 va_start(ap, format);
259 (void) vsnprintf(str, sizeof(str), format, ap);
260 va_end(ap);
261
262 vlog.info("Connection failed: %s", str);
263
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000264 if (state_ == RFBSTATE_PROTOCOL_VERSION) {
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200265 if (client.majorVersion == 3 && client.minorVersion == 3) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000266 os->writeU32(0);
Pierre Ossman19225502017-10-12 15:05:07 +0200267 os->writeString(str);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000268 os->flush();
269 } else {
270 os->writeU8(0);
Pierre Ossman19225502017-10-12 15:05:07 +0200271 os->writeString(str);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000272 os->flush();
273 }
274 }
Pierre Ossman19225502017-10-12 15:05:07 +0200275
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000276 state_ = RFBSTATE_INVALID;
Pierre Ossman19225502017-10-12 15:05:07 +0200277 throw ConnFailedException(str);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000278}
279
280void SConnection::writeConnFailedFromScratch(const char* msg,
281 rdr::OutStream* os)
282{
283 os->writeBytes("RFB 003.003\n", 12);
284 os->writeU32(0);
285 os->writeString(msg);
286 os->flush();
287}
288
Pierre Ossmanf38e2432015-02-11 13:47:58 +0100289void SConnection::setEncodings(int nEncodings, const rdr::S32* encodings)
Pierre Ossman48700812014-09-17 17:11:56 +0200290{
291 int i;
292
293 preferredEncoding = encodingRaw;
294 for (i = 0;i < nEncodings;i++) {
295 if (EncodeManager::supported(encodings[i])) {
296 preferredEncoding = encodings[i];
297 break;
298 }
299 }
300
301 SMsgHandler::setEncodings(nEncodings, encodings);
302}
303
Pierre Ossman5ae28212017-05-16 14:30:38 +0200304void SConnection::supportsQEMUKeyEvent()
305{
306 writer()->writeQEMUKeyEvent();
307}
308
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000309void SConnection::versionReceived()
310{
311}
312
313void SConnection::authSuccess()
314{
315}
316
317void SConnection::queryConnection(const char* userName)
318{
319 approveConnection(true);
320}
321
322void SConnection::approveConnection(bool accept, const char* reason)
323{
324 if (state_ != RFBSTATE_QUERYING)
325 throw Exception("SConnection::approveConnection: invalid state");
326
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200327 if (!client.beforeVersion(3,8) || ssecurity->getType() != secTypeNone) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000328 if (accept) {
329 os->writeU32(secResultOK);
330 } else {
331 os->writeU32(secResultFailed);
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200332 if (!client.beforeVersion(3,8)) { // 3.8 onwards have failure message
Pierre Ossman19225502017-10-12 15:05:07 +0200333 if (reason)
334 os->writeString(reason);
335 else
336 os->writeString("Authentication failure");
337 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000338 }
339 os->flush();
340 }
341
342 if (accept) {
343 state_ = RFBSTATE_INITIALISATION;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100344 reader_ = new SMsgReader(this, is);
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200345 writer_ = new SMsgWriter(&client, os);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000346 authSuccess();
347 } else {
348 state_ = RFBSTATE_INVALID;
Pierre Ossman19225502017-10-12 15:05:07 +0200349 if (reason)
350 throw AuthFailureException(reason);
351 else
352 throw AuthFailureException();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000353 }
354}
355
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000356void SConnection::clientInit(bool shared)
357{
Pierre Ossman96728352018-06-20 11:35:05 +0200358 writer_->writeServerInit(client.width(), client.height(),
359 client.pf(), client.name());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000360 state_ = RFBSTATE_NORMAL;
361}
362
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000363void SConnection::setPixelFormat(const PixelFormat& pf)
364{
365 SMsgHandler::setPixelFormat(pf);
366 readyForSetColourMapEntries = true;
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100367 if (!pf.trueColour)
368 writeFakeColourMap();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000369}
370
371void SConnection::framebufferUpdateRequest(const Rect& r, bool incremental)
372{
373 if (!readyForSetColourMapEntries) {
374 readyForSetColourMapEntries = true;
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200375 if (!client.pf().trueColour) {
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100376 writeFakeColourMap();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000377 }
378 }
379}
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000380
381void SConnection::fence(rdr::U32 flags, unsigned len, const char data[])
382{
383 if (!(flags & fenceFlagRequest))
384 return;
385
386 // We cannot guarantee any synchronisation at this level
387 flags = 0;
388
389 writer()->writeFence(flags, len, data);
390}
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000391
392void SConnection::enableContinuousUpdates(bool enable,
393 int x, int y, int w, int h)
394{
395}
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100396
397void SConnection::writeFakeColourMap(void)
398{
399 int i;
400 rdr::U16 red[256], green[256], blue[256];
401
402 for (i = 0;i < 256;i++)
Pierre Ossman0d3ce872018-06-18 15:59:00 +0200403 client.pf().rgbFromPixel(i, &red[i], &green[i], &blue[i]);
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100404
405 writer()->writeSetColourMapEntries(0, 256, red, green, blue);
406}