1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
* Copyright 2011-2019 Pierre Ossman for Cendio AB
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <rfb/Exception.h>
#include <rfb/Security.h>
#include <rfb/clipboardTypes.h>
#include <rfb/msgTypes.h>
#include <rfb/fenceTypes.h>
#include <rfb/SMsgReader.h>
#include <rfb/SMsgWriter.h>
#include <rfb/SConnection.h>
#include <rfb/ServerCore.h>
#include <rfb/encodings.h>
#include <rfb/EncodeManager.h>
#include <rfb/SSecurity.h>
#include <rfb/util.h>
#include <rfb/LogWriter.h>
using namespace rfb;
static LogWriter vlog("SConnection");
// AccessRights values
const SConnection::AccessRights SConnection::AccessView = 0x0001;
const SConnection::AccessRights SConnection::AccessKeyEvents = 0x0002;
const SConnection::AccessRights SConnection::AccessPtrEvents = 0x0004;
const SConnection::AccessRights SConnection::AccessCutText = 0x0008;
const SConnection::AccessRights SConnection::AccessSetDesktopSize = 0x0010;
const SConnection::AccessRights SConnection::AccessNonShared = 0x0020;
const SConnection::AccessRights SConnection::AccessDefault = 0x03ff;
const SConnection::AccessRights SConnection::AccessNoQuery = 0x0400;
const SConnection::AccessRights SConnection::AccessFull = 0xffff;
SConnection::SConnection()
: readyForSetColourMapEntries(false),
is(0), os(0), reader_(0), writer_(0), ssecurity(0),
authFailureTimer(this, &SConnection::handleAuthFailureTimeout),
state_(RFBSTATE_UNINITIALISED), preferredEncoding(encodingRaw),
accessRights(0x0000), hasRemoteClipboard(false),
hasLocalClipboard(false),
unsolicitedClipboardAttempt(false)
{
defaultMajorVersion = 3;
defaultMinorVersion = 8;
if (rfb::Server::protocol3_3)
defaultMinorVersion = 3;
client.setVersion(defaultMajorVersion, defaultMinorVersion);
}
SConnection::~SConnection()
{
cleanup();
}
void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
{
is = is_;
os = os_;
}
void SConnection::initialiseProtocol()
{
char str[13];
sprintf(str, "RFB %03d.%03d\n", defaultMajorVersion, defaultMinorVersion);
os->writeBytes((const uint8_t*)str, 12);
os->flush();
state_ = RFBSTATE_PROTOCOL_VERSION;
}
bool SConnection::processMsg()
{
switch (state_) {
case RFBSTATE_PROTOCOL_VERSION: return processVersionMsg(); break;
case RFBSTATE_SECURITY_TYPE: return processSecurityTypeMsg(); break;
case RFBSTATE_SECURITY: return processSecurityMsg(); break;
case RFBSTATE_SECURITY_FAILURE: return processSecurityFailure(); break;
case RFBSTATE_INITIALISATION: return processInitMsg(); break;
case RFBSTATE_NORMAL: return reader_->readMsg(); break;
case RFBSTATE_QUERYING:
throw Exception("SConnection::processMsg: bogus data from client while "
"querying");
case RFBSTATE_CLOSING:
throw Exception("SConnection::processMsg: called while closing");
case RFBSTATE_UNINITIALISED:
throw Exception("SConnection::processMsg: not initialised yet?");
default:
throw Exception("SConnection::processMsg: invalid state");
}
}
bool SConnection::processVersionMsg()
{
char verStr[13];
int majorVersion;
int minorVersion;
vlog.debug("reading protocol version");
if (!is->hasData(12))
return false;
is->readBytes((uint8_t*)verStr, 12);
verStr[12] = '\0';
if (sscanf(verStr, "RFB %03d.%03d\n",
&majorVersion, &minorVersion) != 2) {
state_ = RFBSTATE_INVALID;
throw Exception("reading version failed: not an RFB client?");
}
client.setVersion(majorVersion, minorVersion);
vlog.info("Client needs protocol version %d.%d",
client.majorVersion, client.minorVersion);
if (client.majorVersion != 3) {
// unknown protocol version
throwConnFailedException("Client needs protocol version %d.%d, server has %d.%d",
client.majorVersion, client.minorVersion,
defaultMajorVersion, defaultMinorVersion);
}
if (client.minorVersion != 3 && client.minorVersion != 7 && client.minorVersion != 8) {
vlog.error("Client uses unofficial protocol version %d.%d",
client.majorVersion,client.minorVersion);
if (client.minorVersion >= 8)
client.minorVersion = 8;
else if (client.minorVersion == 7)
client.minorVersion = 7;
else
client.minorVersion = 3;
vlog.error("Assuming compatibility with version %d.%d",
client.majorVersion,client.minorVersion);
}
versionReceived();
std::list<uint8_t> secTypes;
std::list<uint8_t>::iterator i;
secTypes = security.GetEnabledSecTypes();
if (client.isVersion(3,3)) {
// cope with legacy 3.3 client only if "no authentication" or "vnc
// authentication" is supported.
for (i=secTypes.begin(); i!=secTypes.end(); i++) {
if (*i == secTypeNone || *i == secTypeVncAuth) break;
}
if (i == secTypes.end()) {
throwConnFailedException("No supported security type for %d.%d client",
client.majorVersion, client.minorVersion);
}
os->writeU32(*i);
if (*i == secTypeNone) os->flush();
state_ = RFBSTATE_SECURITY;
ssecurity = security.GetSSecurity(this, *i);
return true;
}
// list supported security types for >=3.7 clients
if (secTypes.empty())
throwConnFailedException("No supported security types");
os->writeU8(secTypes.size());
for (i=secTypes.begin(); i!=secTypes.end(); i++)
os->writeU8(*i);
os->flush();
state_ = RFBSTATE_SECURITY_TYPE;
return true;
}
bool SConnection::processSecurityTypeMsg()
{
vlog.debug("processing security type message");
if (!is->hasData(1))
return false;
int secType = is->readU8();
processSecurityType(secType);
return true;
}
void SConnection::processSecurityType(int secType)
{
// Verify that the requested security type should be offered
std::list<uint8_t> secTypes;
std::list<uint8_t>::iterator i;
secTypes = security.GetEnabledSecTypes();
for (i=secTypes.begin(); i!=secTypes.end(); i++)
if (*i == secType) break;
if (i == secTypes.end())
throw Exception("Requested security type not available");
vlog.info("Client requests security type %s(%d)",
secTypeName(secType),secType);
try {
state_ = RFBSTATE_SECURITY;
ssecurity = security.GetSSecurity(this, secType);
} catch (rdr::Exception& e) {
throwConnFailedException("%s", e.str());
}
}
bool SConnection::processSecurityMsg()
{
vlog.debug("processing security message");
try {
if (!ssecurity->processMsg())
return false;
} catch (AuthFailureException& e) {
vlog.error("AuthFailureException: %s", e.str());
state_ = RFBSTATE_SECURITY_FAILURE;
// Introduce a slight delay of the authentication failure response
// to make it difficult to brute force a password
authFailureMsg = e.str();
authFailureTimer.start(100);
return true;
}
state_ = RFBSTATE_QUERYING;
setAccessRights(ssecurity->getAccessRights());
queryConnection(ssecurity->getUserName());
// If the connection got approved right away then we can continue
if (state_ == RFBSTATE_INITIALISATION)
return true;
// Otherwise we need to wait for the result
// (or give up if if was rejected)
return false;
}
bool SConnection::processSecurityFailure()
{
// Silently drop any data if we are currently delaying an
// authentication failure response as otherwise we would close
// the connection on unexpected data, and an attacker could use
// that to detect our delayed state.
if (!is->hasData(1))
return false;
is->skip(is->avail());
return true;
}
bool SConnection::processInitMsg()
{
vlog.debug("reading client initialisation");
return reader_->readClientInit();
}
bool SConnection::handleAuthFailureTimeout(Timer* /*t*/)
{
if (state_ != RFBSTATE_SECURITY_FAILURE) {
close("SConnection::handleAuthFailureTimeout: invalid state");
return false;
}
try {
os->writeU32(secResultFailed);
if (!client.beforeVersion(3,8)) { // 3.8 onwards have failure message
os->writeU32(authFailureMsg.size());
os->writeBytes((const uint8_t*)authFailureMsg.data(),
authFailureMsg.size());
}
os->flush();
} catch (rdr::Exception& e) {
close(e.str());
return false;
}
close(authFailureMsg.c_str());
return false;
}
void SConnection::throwConnFailedException(const char* format, ...)
{
va_list ap;
char str[256];
va_start(ap, format);
(void) vsnprintf(str, sizeof(str), format, ap);
va_end(ap);
vlog.info("Connection failed: %s", str);
if (state_ == RFBSTATE_PROTOCOL_VERSION) {
if (client.majorVersion == 3 && client.minorVersion == 3) {
os->writeU32(0);
os->writeU32(strlen(str));
os->writeBytes((const uint8_t*)str, strlen(str));
os->flush();
} else {
os->writeU8(0);
os->writeU32(strlen(str));
os->writeBytes((const uint8_t*)str, strlen(str));
os->flush();
}
}
state_ = RFBSTATE_INVALID;
throw ConnFailedException(str);
}
void SConnection::setAccessRights(AccessRights ar)
{
accessRights = ar;
}
bool SConnection::accessCheck(AccessRights ar) const
{
if (state_ < RFBSTATE_QUERYING)
throw Exception("SConnection::accessCheck: invalid state");
return (accessRights & ar) == ar;
}
void SConnection::setEncodings(int nEncodings, const int32_t* encodings)
{
int i;
preferredEncoding = encodingRaw;
for (i = 0;i < nEncodings;i++) {
if (EncodeManager::supported(encodings[i])) {
preferredEncoding = encodings[i];
break;
}
}
SMsgHandler::setEncodings(nEncodings, encodings);
if (client.supportsEncoding(pseudoEncodingExtendedClipboard)) {
uint32_t sizes[] = { 0 };
writer()->writeClipboardCaps(rfb::clipboardUTF8 |
rfb::clipboardRequest |
rfb::clipboardPeek |
rfb::clipboardNotify |
rfb::clipboardProvide,
sizes);
}
}
void SConnection::clientCutText(const char* str)
{
hasLocalClipboard = false;
clientClipboard = str;
hasRemoteClipboard = true;
handleClipboardAnnounce(true);
}
void SConnection::handleClipboardRequest(uint32_t flags)
{
if (!(flags & rfb::clipboardUTF8)) {
vlog.debug("Ignoring clipboard request for unsupported formats 0x%x", flags);
return;
}
if (!hasLocalClipboard) {
vlog.debug("Ignoring unexpected clipboard request");
return;
}
handleClipboardRequest();
}
void SConnection::handleClipboardPeek()
{
if (client.clipboardFlags() & rfb::clipboardNotify)
writer()->writeClipboardNotify(hasLocalClipboard ? rfb::clipboardUTF8 : 0);
}
void SConnection::handleClipboardNotify(uint32_t flags)
{
hasRemoteClipboard = false;
if (flags & rfb::clipboardUTF8) {
hasLocalClipboard = false;
handleClipboardAnnounce(true);
} else {
handleClipboardAnnounce(false);
}
}
void SConnection::handleClipboardProvide(uint32_t flags,
const size_t* lengths,
const uint8_t* const* data)
{
if (!(flags & rfb::clipboardUTF8)) {
vlog.debug("Ignoring clipboard provide with unsupported formats 0x%x", flags);
return;
}
// FIXME: This conversion magic should be in SMsgReader
if (!isValidUTF8((const char*)data[0], lengths[0])) {
vlog.error("Invalid UTF-8 sequence in clipboard - ignoring");
return;
}
clientClipboard = convertLF((const char*)data[0], lengths[0]);
hasRemoteClipboard = true;
// FIXME: Should probably verify that this data was actually requested
handleClipboardData(clientClipboard.c_str());
}
void SConnection::supportsQEMUKeyEvent()
{
writer()->writeQEMUKeyEvent();
}
void SConnection::versionReceived()
{
}
void SConnection::authSuccess()
{
}
void SConnection::queryConnection(const char* /*userName*/)
{
approveConnection(true);
}
void SConnection::approveConnection(bool accept, const char* reason)
{
if (state_ != RFBSTATE_QUERYING)
throw Exception("SConnection::approveConnection: invalid state");
if (!client.beforeVersion(3,8) || ssecurity->getType() != secTypeNone) {
if (accept) {
os->writeU32(secResultOK);
} else {
os->writeU32(secResultFailed);
if (!client.beforeVersion(3,8)) { // 3.8 onwards have failure message
if (!reason)
reason = "Authentication failure";
os->writeU32(strlen(reason));
os->writeBytes((const uint8_t*)reason, strlen(reason));
}
}
os->flush();
}
if (accept) {
state_ = RFBSTATE_INITIALISATION;
reader_ = new SMsgReader(this, is);
writer_ = new SMsgWriter(&client, os);
authSuccess();
} else {
state_ = RFBSTATE_INVALID;
if (reason)
throw AuthFailureException(reason);
else
throw AuthFailureException();
}
}
void SConnection::clientInit(bool /*shared*/)
{
writer_->writeServerInit(client.width(), client.height(),
client.pf(), client.name());
state_ = RFBSTATE_NORMAL;
}
void SConnection::close(const char* /*reason*/)
{
state_ = RFBSTATE_CLOSING;
cleanup();
}
void SConnection::setPixelFormat(const PixelFormat& pf)
{
SMsgHandler::setPixelFormat(pf);
readyForSetColourMapEntries = true;
if (!pf.trueColour)
writeFakeColourMap();
}
void SConnection::framebufferUpdateRequest(const Rect& /*r*/,
bool /*incremental*/)
{
if (!readyForSetColourMapEntries) {
readyForSetColourMapEntries = true;
if (!client.pf().trueColour) {
writeFakeColourMap();
}
}
}
void SConnection::fence(uint32_t flags, unsigned len,
const uint8_t data[])
{
if (!(flags & fenceFlagRequest))
return;
// We cannot guarantee any synchronisation at this level
flags = 0;
writer()->writeFence(flags, len, data);
}
void SConnection::enableContinuousUpdates(bool /*enable*/,
int /*x*/, int /*y*/,
int /*w*/, int /*h*/)
{
}
void SConnection::handleClipboardRequest()
{
}
void SConnection::handleClipboardAnnounce(bool /*available*/)
{
}
void SConnection::handleClipboardData(const char* /*data*/)
{
}
void SConnection::requestClipboard()
{
if (hasRemoteClipboard) {
handleClipboardData(clientClipboard.c_str());
return;
}
if (client.supportsEncoding(pseudoEncodingExtendedClipboard) &&
(client.clipboardFlags() & rfb::clipboardRequest))
writer()->writeClipboardRequest(rfb::clipboardUTF8);
}
void SConnection::announceClipboard(bool available)
{
hasLocalClipboard = available;
unsolicitedClipboardAttempt = false;
if (client.supportsEncoding(pseudoEncodingExtendedClipboard)) {
// Attempt an unsolicited transfer?
if (available &&
(client.clipboardSize(rfb::clipboardUTF8) > 0) &&
(client.clipboardFlags() & rfb::clipboardProvide)) {
vlog.debug("Attempting unsolicited clipboard transfer...");
unsolicitedClipboardAttempt = true;
handleClipboardRequest();
return;
}
if (client.clipboardFlags() & rfb::clipboardNotify) {
writer()->writeClipboardNotify(available ? rfb::clipboardUTF8 : 0);
return;
}
}
if (available)
handleClipboardRequest();
}
void SConnection::sendClipboardData(const char* data)
{
if (client.supportsEncoding(pseudoEncodingExtendedClipboard) &&
(client.clipboardFlags() & rfb::clipboardProvide)) {
// FIXME: This conversion magic should be in SMsgWriter
std::string filtered(convertCRLF(data));
size_t sizes[1] = { filtered.size() + 1 };
const uint8_t* data[1] = { (const uint8_t*)filtered.c_str() };
if (unsolicitedClipboardAttempt) {
unsolicitedClipboardAttempt = false;
if (sizes[0] > client.clipboardSize(rfb::clipboardUTF8)) {
vlog.debug("Clipboard was too large for unsolicited clipboard transfer");
if (client.clipboardFlags() & rfb::clipboardNotify)
writer()->writeClipboardNotify(rfb::clipboardUTF8);
return;
}
}
writer()->writeClipboardProvide(rfb::clipboardUTF8, sizes, data);
} else {
writer()->writeServerCutText(data);
}
}
void SConnection::cleanup()
{
delete ssecurity;
ssecurity = NULL;
delete reader_;
reader_ = NULL;
delete writer_;
writer_ = NULL;
}
void SConnection::writeFakeColourMap(void)
{
int i;
uint16_t red[256], green[256], blue[256];
for (i = 0;i < 256;i++)
client.pf().rgbFromPixel(i, &red[i], &green[i], &blue[i]);
writer()->writeSetColourMapEntries(0, 256, red, green, blue);
}
|