blob: cc15d7c45f74d9629ad83f5716cce89c36815c4b [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 delete reader_;
71 reader_ = 0;
72 delete writer_;
73 writer_ = 0;
74}
75
76void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
77{
78 is = is_;
79 os = os_;
80}
81
82void SConnection::initialiseProtocol()
83{
84 cp.writeVersion(os);
85 state_ = RFBSTATE_PROTOCOL_VERSION;
86}
87
88void SConnection::processMsg()
89{
90 switch (state_) {
91 case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break;
92 case RFBSTATE_SECURITY_TYPE: processSecurityTypeMsg(); break;
93 case RFBSTATE_SECURITY: processSecurityMsg(); break;
94 case RFBSTATE_INITIALISATION: processInitMsg(); break;
95 case RFBSTATE_NORMAL: reader_->readMsg(); break;
96 case RFBSTATE_QUERYING:
97 throw Exception("SConnection::processMsg: bogus data from client while "
98 "querying");
99 case RFBSTATE_UNINITIALISED:
100 throw Exception("SConnection::processMsg: not initialised yet?");
101 default:
102 throw Exception("SConnection::processMsg: invalid state");
103 }
104}
105
106void SConnection::processVersionMsg()
107{
108 vlog.debug("reading protocol version");
109 bool done;
110 if (!cp.readVersion(is, &done)) {
111 state_ = RFBSTATE_INVALID;
112 throw Exception("reading version failed: not an RFB client?");
113 }
114 if (!done) return;
115
116 vlog.info("Client needs protocol version %d.%d",
117 cp.majorVersion, cp.minorVersion);
118
119 if (cp.majorVersion != 3) {
120 // unknown protocol version
121 char msg[256];
122 sprintf(msg,"Error: client needs protocol version %d.%d, server has %d.%d",
123 cp.majorVersion, cp.minorVersion,
124 defaultMajorVersion, defaultMinorVersion);
125 throwConnFailedException(msg);
126 }
127
128 if (cp.minorVersion != 3 && cp.minorVersion != 7 && cp.minorVersion != 8) {
129 vlog.error("Client uses unofficial protocol version %d.%d",
130 cp.majorVersion,cp.minorVersion);
131 if (cp.minorVersion >= 8)
132 cp.minorVersion = 8;
133 else if (cp.minorVersion == 7)
134 cp.minorVersion = 7;
135 else
136 cp.minorVersion = 3;
137 vlog.error("Assuming compatibility with version %d.%d",
138 cp.majorVersion,cp.minorVersion);
139 }
140
141 versionReceived();
142
143 std::list<rdr::U8> secTypes;
144 std::list<rdr::U8>::iterator i;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000145 secTypes = security->GetEnabledSecTypes();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000146
147 if (cp.isVersion(3,3)) {
148
149 // cope with legacy 3.3 client only if "no authentication" or "vnc
150 // authentication" is supported.
151 for (i=secTypes.begin(); i!=secTypes.end(); i++) {
152 if (*i == secTypeNone || *i == secTypeVncAuth) break;
153 }
154 if (i == secTypes.end()) {
155 char msg[256];
156 sprintf(msg,"No supported security type for %d.%d client",
157 cp.majorVersion, cp.minorVersion);
158 throwConnFailedException(msg);
159 }
160
161 os->writeU32(*i);
162 if (*i == secTypeNone) os->flush();
163 state_ = RFBSTATE_SECURITY;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000164 ssecurity = security->GetSSecurity(*i);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000165 processSecurityMsg();
166 return;
167 }
168
169 // list supported security types for >=3.7 clients
170
171 if (secTypes.empty())
172 throwConnFailedException("No supported security types");
173
174 os->writeU8(secTypes.size());
175 for (i=secTypes.begin(); i!=secTypes.end(); i++)
176 os->writeU8(*i);
177 os->flush();
178 state_ = RFBSTATE_SECURITY_TYPE;
179}
180
181
182void SConnection::processSecurityTypeMsg()
183{
184 vlog.debug("processing security type message");
185 int secType = is->readU8();
186
Constantin Kaplinsky5fa9d222006-09-06 10:32:06 +0000187 processSecurityType(secType);
188}
189
190void SConnection::processSecurityType(int secType)
191{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000192 // Verify that the requested security type should be offered
193 std::list<rdr::U8> secTypes;
194 std::list<rdr::U8>::iterator i;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000195
196 secTypes = security->GetEnabledSecTypes();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000197 for (i=secTypes.begin(); i!=secTypes.end(); i++)
198 if (*i == secType) break;
199 if (i == secTypes.end())
200 throw Exception("Requested security type not available");
201
202 vlog.info("Client requests security type %s(%d)",
203 secTypeName(secType),secType);
204
205 try {
206 state_ = RFBSTATE_SECURITY;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000207 ssecurity = security->GetSSecurity(secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000208 } catch (rdr::Exception& e) {
209 throwConnFailedException(e.str());
210 }
211
212 processSecurityMsg();
213}
214
215void SConnection::processSecurityMsg()
216{
217 vlog.debug("processing security message");
218 try {
Adam Tkaca6578bf2010-04-23 14:07:41 +0000219 bool done = ssecurity->processMsg(this);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000220 if (done) {
221 state_ = RFBSTATE_QUERYING;
Adam Tkaca6578bf2010-04-23 14:07:41 +0000222 queryConnection(ssecurity->getUserName());
Michal Srb8d1ee002014-11-10 09:15:41 +0200223 setAccessRights(ssecurity->getAccessRights());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000224 }
225 } catch (AuthFailureException& e) {
226 vlog.error("AuthFailureException: %s", e.str());
227 os->writeU32(secResultFailed);
228 if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message
229 os->writeString(e.str());
230 os->flush();
231 throw;
232 }
233}
234
235void SConnection::processInitMsg()
236{
237 vlog.debug("reading client initialisation");
238 reader_->readClientInit();
239}
240
241void SConnection::throwConnFailedException(const char* msg)
242{
Pierre Ossmanad8609a2012-04-26 09:04:14 +0000243 vlog.info("%s", msg);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000244 if (state_ == RFBSTATE_PROTOCOL_VERSION) {
245 if (cp.majorVersion == 3 && cp.minorVersion == 3) {
246 os->writeU32(0);
247 os->writeString(msg);
248 os->flush();
249 } else {
250 os->writeU8(0);
251 os->writeString(msg);
252 os->flush();
253 }
254 }
255 state_ = RFBSTATE_INVALID;
256 throw ConnFailedException(msg);
257}
258
259void SConnection::writeConnFailedFromScratch(const char* msg,
260 rdr::OutStream* os)
261{
262 os->writeBytes("RFB 003.003\n", 12);
263 os->writeU32(0);
264 os->writeString(msg);
265 os->flush();
266}
267
Pierre Ossmanf38e2432015-02-11 13:47:58 +0100268void SConnection::setEncodings(int nEncodings, const rdr::S32* encodings)
Pierre Ossman48700812014-09-17 17:11:56 +0200269{
270 int i;
271
272 preferredEncoding = encodingRaw;
273 for (i = 0;i < nEncodings;i++) {
274 if (EncodeManager::supported(encodings[i])) {
275 preferredEncoding = encodings[i];
276 break;
277 }
278 }
279
280 SMsgHandler::setEncodings(nEncodings, encodings);
281}
282
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000283void SConnection::versionReceived()
284{
285}
286
287void SConnection::authSuccess()
288{
289}
290
291void SConnection::queryConnection(const char* userName)
292{
293 approveConnection(true);
294}
295
296void SConnection::approveConnection(bool accept, const char* reason)
297{
298 if (state_ != RFBSTATE_QUERYING)
299 throw Exception("SConnection::approveConnection: invalid state");
300
301 if (!reason) reason = "Authentication failure";
302
Adam Tkaca6578bf2010-04-23 14:07:41 +0000303 if (!cp.beforeVersion(3,8) || ssecurity->getType() != secTypeNone) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000304 if (accept) {
305 os->writeU32(secResultOK);
306 } else {
307 os->writeU32(secResultFailed);
308 if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message
309 os->writeString(reason);
310 }
311 os->flush();
312 }
313
314 if (accept) {
315 state_ = RFBSTATE_INITIALISATION;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100316 reader_ = new SMsgReader(this, is);
317 writer_ = new SMsgWriter(&cp, os);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000318 authSuccess();
319 } else {
320 state_ = RFBSTATE_INVALID;
321 throw AuthFailureException(reason);
322 }
323}
324
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000325void SConnection::clientInit(bool shared)
326{
327 writer_->writeServerInit();
328 state_ = RFBSTATE_NORMAL;
329}
330
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000331void SConnection::setPixelFormat(const PixelFormat& pf)
332{
333 SMsgHandler::setPixelFormat(pf);
334 readyForSetColourMapEntries = true;
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100335 if (!pf.trueColour)
336 writeFakeColourMap();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000337}
338
339void SConnection::framebufferUpdateRequest(const Rect& r, bool incremental)
340{
341 if (!readyForSetColourMapEntries) {
342 readyForSetColourMapEntries = true;
343 if (!cp.pf().trueColour) {
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100344 writeFakeColourMap();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000345 }
346 }
347}
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000348
349void SConnection::fence(rdr::U32 flags, unsigned len, const char data[])
350{
351 if (!(flags & fenceFlagRequest))
352 return;
353
354 // We cannot guarantee any synchronisation at this level
355 flags = 0;
356
357 writer()->writeFence(flags, len, data);
358}
Pierre Ossmanc898d9a2011-11-14 16:22:23 +0000359
360void SConnection::enableContinuousUpdates(bool enable,
361 int x, int y, int w, int h)
362{
363}
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100364
365void SConnection::writeFakeColourMap(void)
366{
367 int i;
368 rdr::U16 red[256], green[256], blue[256];
369
370 for (i = 0;i < 256;i++)
371 cp.pf().rgbFromPixel(i, &red[i], &green[i], &blue[i]);
372
373 writer()->writeSetColourMapEntries(0, 256, red, green, blue);
374}