blob: bfff70cfda2bc7448c275d40d32e63c1ac06dd89 [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),
Adam Tkaca6578bf2010-04-23 14:07:41 +000054 security(0), 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);
Adam Tkaca6578bf2010-04-23 14:07:41 +000063
Adam Tkacbfd66c12010-10-01 08:33:29 +000064 security = new SecurityServer();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000065}
66
67SConnection::~SConnection()
68{
Adam Tkaca6578bf2010-04-23 14:07:41 +000069 if (ssecurity) ssecurity->destroy();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000070 deleteReaderAndWriter();
71}
72
73void SConnection::deleteReaderAndWriter()
74{
75 delete reader_;
76 reader_ = 0;
77 delete writer_;
78 writer_ = 0;
79}
80
81void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
82{
83 is = is_;
84 os = os_;
85}
86
87void SConnection::initialiseProtocol()
88{
89 cp.writeVersion(os);
90 state_ = RFBSTATE_PROTOCOL_VERSION;
91}
92
93void SConnection::processMsg()
94{
95 switch (state_) {
96 case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break;
97 case RFBSTATE_SECURITY_TYPE: processSecurityTypeMsg(); break;
98 case RFBSTATE_SECURITY: processSecurityMsg(); break;
99 case RFBSTATE_INITIALISATION: processInitMsg(); break;
100 case RFBSTATE_NORMAL: reader_->readMsg(); break;
101 case RFBSTATE_QUERYING:
102 throw Exception("SConnection::processMsg: bogus data from client while "
103 "querying");
104 case RFBSTATE_UNINITIALISED:
105 throw Exception("SConnection::processMsg: not initialised yet?");
106 default:
107 throw Exception("SConnection::processMsg: invalid state");
108 }
109}
110
111void SConnection::processVersionMsg()
112{
113 vlog.debug("reading protocol version");
114 bool done;
115 if (!cp.readVersion(is, &done)) {
116 state_ = RFBSTATE_INVALID;
117 throw Exception("reading version failed: not an RFB client?");
118 }
119 if (!done) return;
120
121 vlog.info("Client needs protocol version %d.%d",
122 cp.majorVersion, cp.minorVersion);
123
124 if (cp.majorVersion != 3) {
125 // unknown protocol version
126 char msg[256];
127 sprintf(msg,"Error: client needs protocol version %d.%d, server has %d.%d",
128 cp.majorVersion, cp.minorVersion,
129 defaultMajorVersion, defaultMinorVersion);
130 throwConnFailedException(msg);
131 }
132
133 if (cp.minorVersion != 3 && cp.minorVersion != 7 && cp.minorVersion != 8) {
134 vlog.error("Client uses unofficial protocol version %d.%d",
135 cp.majorVersion,cp.minorVersion);
136 if (cp.minorVersion >= 8)
137 cp.minorVersion = 8;
138 else if (cp.minorVersion == 7)
139 cp.minorVersion = 7;
140 else
141 cp.minorVersion = 3;
142 vlog.error("Assuming compatibility with version %d.%d",
143 cp.majorVersion,cp.minorVersion);
144 }
145
146 versionReceived();
147
148 std::list<rdr::U8> secTypes;
149 std::list<rdr::U8>::iterator i;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000150 secTypes = security->GetEnabledSecTypes();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000151
152 if (cp.isVersion(3,3)) {
153
154 // cope with legacy 3.3 client only if "no authentication" or "vnc
155 // authentication" is supported.
156 for (i=secTypes.begin(); i!=secTypes.end(); i++) {
157 if (*i == secTypeNone || *i == secTypeVncAuth) break;
158 }
159 if (i == secTypes.end()) {
160 char msg[256];
161 sprintf(msg,"No supported security type for %d.%d client",
162 cp.majorVersion, cp.minorVersion);
163 throwConnFailedException(msg);
164 }
165
166 os->writeU32(*i);
167 if (*i == secTypeNone) os->flush();
168 state_ = RFBSTATE_SECURITY;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000169 ssecurity = security->GetSSecurity(*i);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000170 processSecurityMsg();
171 return;
172 }
173
174 // list supported security types for >=3.7 clients
175
176 if (secTypes.empty())
177 throwConnFailedException("No supported security types");
178
179 os->writeU8(secTypes.size());
180 for (i=secTypes.begin(); i!=secTypes.end(); i++)
181 os->writeU8(*i);
182 os->flush();
183 state_ = RFBSTATE_SECURITY_TYPE;
184}
185
186
187void SConnection::processSecurityTypeMsg()
188{
189 vlog.debug("processing security type message");
190 int secType = is->readU8();
191
Constantin Kaplinsky5fa9d222006-09-06 10:32:06 +0000192 processSecurityType(secType);
193}
194
195void SConnection::processSecurityType(int secType)
196{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000197 // Verify that the requested security type should be offered
198 std::list<rdr::U8> secTypes;
199 std::list<rdr::U8>::iterator i;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000200
201 secTypes = security->GetEnabledSecTypes();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 for (i=secTypes.begin(); i!=secTypes.end(); i++)
203 if (*i == secType) break;
204 if (i == secTypes.end())
205 throw Exception("Requested security type not available");
206
207 vlog.info("Client requests security type %s(%d)",
208 secTypeName(secType),secType);
209
210 try {
211 state_ = RFBSTATE_SECURITY;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000212 ssecurity = security->GetSSecurity(secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000213 } catch (rdr::Exception& e) {
214 throwConnFailedException(e.str());
215 }
216
217 processSecurityMsg();
218}
219
220void SConnection::processSecurityMsg()
221{
222 vlog.debug("processing security message");
223 try {
Adam Tkaca6578bf2010-04-23 14:07:41 +0000224 bool done = ssecurity->processMsg(this);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000225 if (done) {
226 state_ = RFBSTATE_QUERYING;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000227 queryConnection(ssecurity->getUserName());
Michal Srb8d1ee002014-11-10 09:15:41 +0200228 setAccessRights(ssecurity->getAccessRights());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000229 }
230 } catch (AuthFailureException& e) {
231 vlog.error("AuthFailureException: %s", e.str());
232 os->writeU32(secResultFailed);
233 if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message
234 os->writeString(e.str());
235 os->flush();
236 throw;
237 }
238}
239
240void SConnection::processInitMsg()
241{
242 vlog.debug("reading client initialisation");
243 reader_->readClientInit();
244}
245
246void SConnection::throwConnFailedException(const char* msg)
247{
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000248 vlog.info("%s", msg);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000249 if (state_ == RFBSTATE_PROTOCOL_VERSION) {
250 if (cp.majorVersion == 3 && cp.minorVersion == 3) {
251 os->writeU32(0);
252 os->writeString(msg);
253 os->flush();
254 } else {
255 os->writeU8(0);
256 os->writeString(msg);
257 os->flush();
258 }
259 }
260 state_ = RFBSTATE_INVALID;
261 throw ConnFailedException(msg);
262}
263
264void SConnection::writeConnFailedFromScratch(const char* msg,
265 rdr::OutStream* os)
266{
267 os->writeBytes("RFB 003.003\n", 12);
268 os->writeU32(0);
269 os->writeString(msg);
270 os->flush();
271}
272
Pierre Ossmanf38e2432015-02-11 13:47:58 +0100273void SConnection::setEncodings(int nEncodings, const rdr::S32* encodings)
Pierre Ossman48700812014-09-17 17:11:56 +0200274{
275 int i;
276
277 preferredEncoding = encodingRaw;
278 for (i = 0;i < nEncodings;i++) {
279 if (EncodeManager::supported(encodings[i])) {
280 preferredEncoding = encodings[i];
281 break;
282 }
283 }
284
285 SMsgHandler::setEncodings(nEncodings, encodings);
286}
287
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000288void SConnection::versionReceived()
289{
290}
291
292void SConnection::authSuccess()
293{
294}
295
296void SConnection::queryConnection(const char* userName)
297{
298 approveConnection(true);
299}
300
301void SConnection::approveConnection(bool accept, const char* reason)
302{
303 if (state_ != RFBSTATE_QUERYING)
304 throw Exception("SConnection::approveConnection: invalid state");
305
306 if (!reason) reason = "Authentication failure";
307
Adam Tkaca6578bf2010-04-23 14:07:41 +0000308 if (!cp.beforeVersion(3,8) || ssecurity->getType() != secTypeNone) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000309 if (accept) {
310 os->writeU32(secResultOK);
311 } else {
312 os->writeU32(secResultFailed);
313 if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message
314 os->writeString(reason);
315 }
316 os->flush();
317 }
318
319 if (accept) {
320 state_ = RFBSTATE_INITIALISATION;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100321 reader_ = new SMsgReader(this, is);
322 writer_ = new SMsgWriter(&cp, os);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000323 authSuccess();
324 } else {
325 state_ = RFBSTATE_INVALID;
326 throw AuthFailureException(reason);
327 }
328}
329
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000330void SConnection::clientInit(bool shared)
331{
332 writer_->writeServerInit();
333 state_ = RFBSTATE_NORMAL;
334}
335
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000336void SConnection::setPixelFormat(const PixelFormat& pf)
337{
338 SMsgHandler::setPixelFormat(pf);
339 readyForSetColourMapEntries = true;
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100340 if (!pf.trueColour)
341 writeFakeColourMap();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000342}
343
344void SConnection::framebufferUpdateRequest(const Rect& r, bool incremental)
345{
346 if (!readyForSetColourMapEntries) {
347 readyForSetColourMapEntries = true;
348 if (!cp.pf().trueColour) {
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100349 writeFakeColourMap();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000350 }
351 }
352}
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000353
354void SConnection::fence(rdr::U32 flags, unsigned len, const char data[])
355{
356 if (!(flags & fenceFlagRequest))
357 return;
358
359 // We cannot guarantee any synchronisation at this level
360 flags = 0;
361
362 writer()->writeFence(flags, len, data);
363}
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000364
365void SConnection::enableContinuousUpdates(bool enable,
366 int x, int y, int w, int h)
367{
368}
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100369
370void SConnection::writeFakeColourMap(void)
371{
372 int i;
373 rdr::U16 red[256], green[256], blue[256];
374
375 for (i = 0;i < 256;i++)
376 cp.pf().rgbFromPixel(i, &red[i], &green[i], &blue[i]);
377
378 writer()->writeSetColourMapEntries(0, 256, red, green, blue);
379}