blob: 6b810559664fb57cf50ff45b46c3d39abc6cd136 [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
62 cp.setVersion(defaultMajorVersion, defaultMinorVersion);
63}
64
65SConnection::~SConnection()
66{
Adam Tkaca6578bf2010-04-23 14:07:41 +000067 if (ssecurity) ssecurity->destroy();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000068 delete reader_;
69 reader_ = 0;
70 delete writer_;
71 writer_ = 0;
72}
73
74void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
75{
76 is = is_;
77 os = os_;
78}
79
80void SConnection::initialiseProtocol()
81{
82 cp.writeVersion(os);
83 state_ = RFBSTATE_PROTOCOL_VERSION;
84}
85
86void SConnection::processMsg()
87{
88 switch (state_) {
89 case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break;
90 case RFBSTATE_SECURITY_TYPE: processSecurityTypeMsg(); break;
91 case RFBSTATE_SECURITY: processSecurityMsg(); break;
92 case RFBSTATE_INITIALISATION: processInitMsg(); break;
93 case RFBSTATE_NORMAL: reader_->readMsg(); break;
94 case RFBSTATE_QUERYING:
95 throw Exception("SConnection::processMsg: bogus data from client while "
96 "querying");
97 case RFBSTATE_UNINITIALISED:
98 throw Exception("SConnection::processMsg: not initialised yet?");
99 default:
100 throw Exception("SConnection::processMsg: invalid state");
101 }
102}
103
104void SConnection::processVersionMsg()
105{
106 vlog.debug("reading protocol version");
107 bool done;
108 if (!cp.readVersion(is, &done)) {
109 state_ = RFBSTATE_INVALID;
110 throw Exception("reading version failed: not an RFB client?");
111 }
112 if (!done) return;
113
114 vlog.info("Client needs protocol version %d.%d",
115 cp.majorVersion, cp.minorVersion);
116
117 if (cp.majorVersion != 3) {
118 // unknown protocol version
Pierre Ossman19225502017-10-12 15:05:07 +0200119 throwConnFailedException("Client needs protocol version %d.%d, server has %d.%d",
120 cp.majorVersion, cp.minorVersion,
121 defaultMajorVersion, defaultMinorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122 }
123
124 if (cp.minorVersion != 3 && cp.minorVersion != 7 && cp.minorVersion != 8) {
125 vlog.error("Client uses unofficial protocol version %d.%d",
126 cp.majorVersion,cp.minorVersion);
127 if (cp.minorVersion >= 8)
128 cp.minorVersion = 8;
129 else if (cp.minorVersion == 7)
130 cp.minorVersion = 7;
131 else
132 cp.minorVersion = 3;
133 vlog.error("Assuming compatibility with version %d.%d",
134 cp.majorVersion,cp.minorVersion);
135 }
136
137 versionReceived();
138
139 std::list<rdr::U8> secTypes;
140 std::list<rdr::U8>::iterator i;
Michal Srbdccb5f72017-03-27 13:55:46 +0300141 secTypes = security.GetEnabledSecTypes();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000142
143 if (cp.isVersion(3,3)) {
144
145 // cope with legacy 3.3 client only if "no authentication" or "vnc
146 // authentication" is supported.
147 for (i=secTypes.begin(); i!=secTypes.end(); i++) {
148 if (*i == secTypeNone || *i == secTypeVncAuth) break;
149 }
150 if (i == secTypes.end()) {
Pierre Ossman19225502017-10-12 15:05:07 +0200151 throwConnFailedException("No supported security type for %d.%d client",
152 cp.majorVersion, cp.minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000153 }
154
155 os->writeU32(*i);
156 if (*i == secTypeNone) os->flush();
157 state_ = RFBSTATE_SECURITY;
Michal Srbdccb5f72017-03-27 13:55:46 +0300158 ssecurity = security.GetSSecurity(*i);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000159 processSecurityMsg();
160 return;
161 }
162
163 // list supported security types for >=3.7 clients
164
165 if (secTypes.empty())
166 throwConnFailedException("No supported security types");
167
168 os->writeU8(secTypes.size());
169 for (i=secTypes.begin(); i!=secTypes.end(); i++)
170 os->writeU8(*i);
171 os->flush();
172 state_ = RFBSTATE_SECURITY_TYPE;
173}
174
175
176void SConnection::processSecurityTypeMsg()
177{
178 vlog.debug("processing security type message");
179 int secType = is->readU8();
180
Constantin Kaplinsky5fa9d222006-09-06 10:32:06 +0000181 processSecurityType(secType);
182}
183
184void SConnection::processSecurityType(int secType)
185{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 // Verify that the requested security type should be offered
187 std::list<rdr::U8> secTypes;
188 std::list<rdr::U8>::iterator i;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000189
Michal Srbdccb5f72017-03-27 13:55:46 +0300190 secTypes = security.GetEnabledSecTypes();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000191 for (i=secTypes.begin(); i!=secTypes.end(); i++)
192 if (*i == secType) break;
193 if (i == secTypes.end())
194 throw Exception("Requested security type not available");
195
196 vlog.info("Client requests security type %s(%d)",
197 secTypeName(secType),secType);
198
199 try {
200 state_ = RFBSTATE_SECURITY;
Michal Srbdccb5f72017-03-27 13:55:46 +0300201 ssecurity = security.GetSSecurity(secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 } catch (rdr::Exception& e) {
Pierre Ossman19225502017-10-12 15:05:07 +0200203 throwConnFailedException("%s", e.str());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000204 }
205
206 processSecurityMsg();
207}
208
209void SConnection::processSecurityMsg()
210{
211 vlog.debug("processing security message");
212 try {
Adam Tkaca6578bf2010-04-23 14:07:41 +0000213 bool done = ssecurity->processMsg(this);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214 if (done) {
215 state_ = RFBSTATE_QUERYING;
Michal Srb8d1ee002014-11-10 09:15:41 +0200216 setAccessRights(ssecurity->getAccessRights());
Henrik Anderssonc1cbc702016-01-27 14:00:44 +0100217 queryConnection(ssecurity->getUserName());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000218 }
219 } catch (AuthFailureException& e) {
220 vlog.error("AuthFailureException: %s", e.str());
221 os->writeU32(secResultFailed);
222 if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message
223 os->writeString(e.str());
224 os->flush();
225 throw;
226 }
227}
228
229void SConnection::processInitMsg()
230{
231 vlog.debug("reading client initialisation");
232 reader_->readClientInit();
233}
234
Pierre Ossman19225502017-10-12 15:05:07 +0200235void SConnection::throwConnFailedException(const char* format, ...)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236{
Pierre Ossman19225502017-10-12 15:05:07 +0200237 va_list ap;
238 char str[256];
239
240 va_start(ap, format);
241 (void) vsnprintf(str, sizeof(str), format, ap);
242 va_end(ap);
243
244 vlog.info("Connection failed: %s", str);
245
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000246 if (state_ == RFBSTATE_PROTOCOL_VERSION) {
247 if (cp.majorVersion == 3 && cp.minorVersion == 3) {
248 os->writeU32(0);
Pierre Ossman19225502017-10-12 15:05:07 +0200249 os->writeString(str);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000250 os->flush();
251 } else {
252 os->writeU8(0);
Pierre Ossman19225502017-10-12 15:05:07 +0200253 os->writeString(str);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000254 os->flush();
255 }
256 }
Pierre Ossman19225502017-10-12 15:05:07 +0200257
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000258 state_ = RFBSTATE_INVALID;
Pierre Ossman19225502017-10-12 15:05:07 +0200259 throw ConnFailedException(str);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000260}
261
262void SConnection::writeConnFailedFromScratch(const char* msg,
263 rdr::OutStream* os)
264{
265 os->writeBytes("RFB 003.003\n", 12);
266 os->writeU32(0);
267 os->writeString(msg);
268 os->flush();
269}
270
Pierre Ossmanf38e2432015-02-11 13:47:58 +0100271void SConnection::setEncodings(int nEncodings, const rdr::S32* encodings)
Pierre Ossman48700812014-09-17 17:11:56 +0200272{
273 int i;
274
275 preferredEncoding = encodingRaw;
276 for (i = 0;i < nEncodings;i++) {
277 if (EncodeManager::supported(encodings[i])) {
278 preferredEncoding = encodings[i];
279 break;
280 }
281 }
282
283 SMsgHandler::setEncodings(nEncodings, encodings);
284}
285
Pierre Ossman5ae28212017-05-16 14:30:38 +0200286void SConnection::supportsQEMUKeyEvent()
287{
288 writer()->writeQEMUKeyEvent();
289}
290
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000291void SConnection::versionReceived()
292{
293}
294
295void SConnection::authSuccess()
296{
297}
298
299void SConnection::queryConnection(const char* userName)
300{
301 approveConnection(true);
302}
303
304void SConnection::approveConnection(bool accept, const char* reason)
305{
306 if (state_ != RFBSTATE_QUERYING)
307 throw Exception("SConnection::approveConnection: invalid state");
308
Adam Tkaca6578bf2010-04-23 14:07:41 +0000309 if (!cp.beforeVersion(3,8) || ssecurity->getType() != secTypeNone) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000310 if (accept) {
311 os->writeU32(secResultOK);
312 } else {
313 os->writeU32(secResultFailed);
Pierre Ossman19225502017-10-12 15:05:07 +0200314 if (!cp.beforeVersion(3,8)) { // 3.8 onwards have failure message
315 if (reason)
316 os->writeString(reason);
317 else
318 os->writeString("Authentication failure");
319 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000320 }
321 os->flush();
322 }
323
324 if (accept) {
325 state_ = RFBSTATE_INITIALISATION;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100326 reader_ = new SMsgReader(this, is);
327 writer_ = new SMsgWriter(&cp, os);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000328 authSuccess();
329 } else {
330 state_ = RFBSTATE_INVALID;
Pierre Ossman19225502017-10-12 15:05:07 +0200331 if (reason)
332 throw AuthFailureException(reason);
333 else
334 throw AuthFailureException();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000335 }
336}
337
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000338void SConnection::clientInit(bool shared)
339{
340 writer_->writeServerInit();
341 state_ = RFBSTATE_NORMAL;
342}
343
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000344void SConnection::setPixelFormat(const PixelFormat& pf)
345{
346 SMsgHandler::setPixelFormat(pf);
347 readyForSetColourMapEntries = true;
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100348 if (!pf.trueColour)
349 writeFakeColourMap();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000350}
351
352void SConnection::framebufferUpdateRequest(const Rect& r, bool incremental)
353{
354 if (!readyForSetColourMapEntries) {
355 readyForSetColourMapEntries = true;
356 if (!cp.pf().trueColour) {
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100357 writeFakeColourMap();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000358 }
359 }
360}
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000361
362void SConnection::fence(rdr::U32 flags, unsigned len, const char data[])
363{
364 if (!(flags & fenceFlagRequest))
365 return;
366
367 // We cannot guarantee any synchronisation at this level
368 flags = 0;
369
370 writer()->writeFence(flags, len, data);
371}
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000372
373void SConnection::enableContinuousUpdates(bool enable,
374 int x, int y, int w, int h)
375{
376}
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100377
378void SConnection::writeFakeColourMap(void)
379{
380 int i;
381 rdr::U16 red[256], green[256], blue[256];
382
383 for (i = 0;i < 256;i++)
384 cp.pf().rgbFromPixel(i, &red[i], &green[i], &blue[i]);
385
386 writer()->writeSetColourMapEntries(0, 256, red, green, blue);
387}