blob: daed8f8fdf39bdd50296224000ff88e7f0925737 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18#include <stdio.h>
19#include <string.h>
20#include <rfb/Exception.h>
21#include <rfb/secTypes.h>
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +000022#include <rfb/CapsList.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000023#include <rfb/SMsgReaderV3.h>
24#include <rfb/SMsgWriterV3.h>
25#include <rfb/SConnection.h>
26#include <rfb/ServerCore.h>
27
28#include <rfb/LogWriter.h>
29
30using namespace rfb;
31
32static LogWriter vlog("SConnection");
33
34// AccessRights values
35const SConnection::AccessRights SConnection::AccessView = 0x0001;
36const SConnection::AccessRights SConnection::AccessKeyEvents = 0x0002;
37const SConnection::AccessRights SConnection::AccessPtrEvents = 0x0004;
38const SConnection::AccessRights SConnection::AccessCutText = 0x0008;
39const SConnection::AccessRights SConnection::AccessDefault = 0x03ff;
40const SConnection::AccessRights SConnection::AccessNoQuery = 0x0400;
41const SConnection::AccessRights SConnection::AccessFull = 0xffff;
42
43
44SConnection::SConnection(SSecurityFactory* secFact, bool reverseConnection_)
45 : readyForSetColourMapEntries(false),
46 is(0), os(0), reader_(0), writer_(0),
47 security(0), securityFactory(secFact), state_(RFBSTATE_UNINITIALISED),
48 reverseConnection(reverseConnection_)
49{
50 defaultMajorVersion = 3;
51 defaultMinorVersion = 8;
52 if (rfb::Server::protocol3_3)
53 defaultMinorVersion = 3;
54
55 cp.setVersion(defaultMajorVersion, defaultMinorVersion);
56}
57
58SConnection::~SConnection()
59{
60 if (security) security->destroy();
61 deleteReaderAndWriter();
62}
63
64void SConnection::deleteReaderAndWriter()
65{
66 delete reader_;
67 reader_ = 0;
68 delete writer_;
69 writer_ = 0;
70}
71
72void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
73{
74 is = is_;
75 os = os_;
76}
77
78void SConnection::initialiseProtocol()
79{
80 cp.writeVersion(os);
81 state_ = RFBSTATE_PROTOCOL_VERSION;
82}
83
84void SConnection::processMsg()
85{
86 switch (state_) {
87 case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break;
88 case RFBSTATE_SECURITY_TYPE: processSecurityTypeMsg(); break;
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +000089 case RFBSTATE_TIGHT_TUNN_TYPE: processTunnelTypeMsg(); break;
90 case RFBSTATE_TIGHT_AUTH_TYPE: processAuthTypeMsg(); break;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000091 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
119 char msg[256];
120 sprintf(msg,"Error: client needs protocol version %d.%d, server has %d.%d",
121 cp.majorVersion, cp.minorVersion,
122 defaultMajorVersion, defaultMinorVersion);
123 throwConnFailedException(msg);
124 }
125
126 if (cp.minorVersion != 3 && cp.minorVersion != 7 && cp.minorVersion != 8) {
127 vlog.error("Client uses unofficial protocol version %d.%d",
128 cp.majorVersion,cp.minorVersion);
129 if (cp.minorVersion >= 8)
130 cp.minorVersion = 8;
131 else if (cp.minorVersion == 7)
132 cp.minorVersion = 7;
133 else
134 cp.minorVersion = 3;
135 vlog.error("Assuming compatibility with version %d.%d",
136 cp.majorVersion,cp.minorVersion);
137 }
138
139 versionReceived();
140
141 std::list<rdr::U8> secTypes;
142 std::list<rdr::U8>::iterator i;
143 securityFactory->getSecTypes(&secTypes, reverseConnection);
144
145 if (cp.isVersion(3,3)) {
146
147 // cope with legacy 3.3 client only if "no authentication" or "vnc
148 // authentication" is supported.
149 for (i=secTypes.begin(); i!=secTypes.end(); i++) {
150 if (*i == secTypeNone || *i == secTypeVncAuth) break;
151 }
152 if (i == secTypes.end()) {
153 char msg[256];
154 sprintf(msg,"No supported security type for %d.%d client",
155 cp.majorVersion, cp.minorVersion);
156 throwConnFailedException(msg);
157 }
158
159 os->writeU32(*i);
160 if (*i == secTypeNone) os->flush();
161 state_ = RFBSTATE_SECURITY;
162 security = securityFactory->getSSecurity(*i, reverseConnection);
163 processSecurityMsg();
164 return;
165 }
166
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +0000167 // Add a special security type to advertise TightVNC protocol extensions.
168 secTypes.push_back(secTypeTight);
169
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000170 // list supported security types for >=3.7 clients
171
172 if (secTypes.empty())
173 throwConnFailedException("No supported security types");
174
175 os->writeU8(secTypes.size());
176 for (i=secTypes.begin(); i!=secTypes.end(); i++)
177 os->writeU8(*i);
178 os->flush();
179 state_ = RFBSTATE_SECURITY_TYPE;
180}
181
182
183void SConnection::processSecurityTypeMsg()
184{
185 vlog.debug("processing security type message");
186 int secType = is->readU8();
187
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +0000188 if (secType == secTypeTight) {
189 vlog.info("Enabling TightVNC protocol extensions");
190 cp.tightExtensionsEnabled = true;
191 offerTunneling();
192 } else {
193 processSecurityType(secType);
194 }
195}
196
197//
198// TightVNC-specific protocol initialization (tunneling, authentication)
199//
200
201void SConnection::offerTunneling()
202{
203 vlog.debug("offering list of tunneling methods");
204 int nTypes = 0;
205
206 // Advertise our tunneling capabilities (currently, nothing to advertise).
207 os->writeU32(nTypes);
208
209 if (nTypes) {
Constantin Kaplinsky4ad04c12006-09-12 06:37:33 +0000210 // NOTE: Never executed in current version.
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +0000211 state_ = RFBSTATE_TIGHT_TUNN_TYPE;
212 } else {
213 offerAuthentication();
214 }
215}
216
217// NOTE: This function is never called in current version.
218void SConnection::processTunnelTypeMsg()
219{
220 vlog.debug("processing tunneling type message (TightVNC extension)");
221 int tunnelType = is->readU32();
222 vlog.error("unsupported tunneling type %d requested, ignoring", tunnelType);
223 offerAuthentication();
224}
225
226void SConnection::offerAuthentication()
227{
228 vlog.debug("offering list of authentication methods");
229
230 // FIXME: Security types and TightVNC's auth types should be distinguished
231 // although we use the same codes for NoAuth and VncAuth.
232
233 // See processVersionMsg(), the code below is very similar.
234 std::list<rdr::U8> secTypes;
235 std::list<rdr::U8>::iterator i;
236 securityFactory->getSecTypes(&secTypes, reverseConnection);
237
238 if (secTypes.empty())
239 throwConnFailedException("No supported security types");
240
241 // FIXME: We should send an empty list for "no authentication".
242
243 CapsList caps;
244 for (i = secTypes.begin(); i != secTypes.end(); i++) {
245 // FIXME: Use mapping instead (authType -> ProtocolCapability).
246 // Each auth type should provide its capability data.
247 // FIXME: Check that client will send auth type listed in capabilities.
248 // Currently, capabilities should duplicate secTypes exactly,
249 // but that may change in the future.
250 switch(*i) {
251 case secTypeNone:
252 caps.addStandard(secTypeNone, "NOAUTH__");
253 break;
254 case secTypeVncAuth:
255 caps.addStandard(secTypeVncAuth, "VNCAUTH_");
256 break;
257 }
258 }
259 os->writeU32(caps.getSize());
260 caps.write(os);
261 os->flush();
262
263 // FIXME: Capability list is never empty here, otherwise
264 // we would not expect authentication type message.
265 state_ = RFBSTATE_TIGHT_AUTH_TYPE;
266}
267
268void SConnection::processAuthTypeMsg()
269{
270 vlog.debug("processing authentication type message (TightVNC extension)");
271
272 // FIXME: Security types and TightVNC's auth types should be distinguished
273 // although we use the same codes for NoAuth and VncAuth.
274
275 int secType = is->readU32();
Constantin Kaplinsky5fa9d222006-09-06 10:32:06 +0000276 processSecurityType(secType);
277}
278
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +0000279//
280// End of TightVNC-specific code
281//
282
Constantin Kaplinsky5fa9d222006-09-06 10:32:06 +0000283void SConnection::processSecurityType(int secType)
284{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000285 // Verify that the requested security type should be offered
286 std::list<rdr::U8> secTypes;
287 std::list<rdr::U8>::iterator i;
288 securityFactory->getSecTypes(&secTypes, reverseConnection);
289 for (i=secTypes.begin(); i!=secTypes.end(); i++)
290 if (*i == secType) break;
291 if (i == secTypes.end())
292 throw Exception("Requested security type not available");
293
294 vlog.info("Client requests security type %s(%d)",
295 secTypeName(secType),secType);
296
297 try {
298 state_ = RFBSTATE_SECURITY;
299 security = securityFactory->getSSecurity(secType, reverseConnection);
300 } catch (rdr::Exception& e) {
301 throwConnFailedException(e.str());
302 }
303
304 processSecurityMsg();
305}
306
307void SConnection::processSecurityMsg()
308{
309 vlog.debug("processing security message");
310 try {
311 bool done = security->processMsg(this);
312 if (done) {
313 state_ = RFBSTATE_QUERYING;
314 queryConnection(security->getUserName());
315 }
316 } catch (AuthFailureException& e) {
317 vlog.error("AuthFailureException: %s", e.str());
318 os->writeU32(secResultFailed);
319 if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message
320 os->writeString(e.str());
321 os->flush();
322 throw;
323 }
324}
325
326void SConnection::processInitMsg()
327{
328 vlog.debug("reading client initialisation");
329 reader_->readClientInit();
330}
331
332void SConnection::throwConnFailedException(const char* msg)
333{
334 vlog.info(msg);
335 if (state_ == RFBSTATE_PROTOCOL_VERSION) {
336 if (cp.majorVersion == 3 && cp.minorVersion == 3) {
337 os->writeU32(0);
338 os->writeString(msg);
339 os->flush();
340 } else {
341 os->writeU8(0);
342 os->writeString(msg);
343 os->flush();
344 }
345 }
346 state_ = RFBSTATE_INVALID;
347 throw ConnFailedException(msg);
348}
349
350void SConnection::writeConnFailedFromScratch(const char* msg,
351 rdr::OutStream* os)
352{
353 os->writeBytes("RFB 003.003\n", 12);
354 os->writeU32(0);
355 os->writeString(msg);
356 os->flush();
357}
358
359void SConnection::versionReceived()
360{
361}
362
363void SConnection::authSuccess()
364{
365}
366
367void SConnection::queryConnection(const char* userName)
368{
369 approveConnection(true);
370}
371
372void SConnection::approveConnection(bool accept, const char* reason)
373{
374 if (state_ != RFBSTATE_QUERYING)
375 throw Exception("SConnection::approveConnection: invalid state");
376
377 if (!reason) reason = "Authentication failure";
378
379 if (!cp.beforeVersion(3,8) || security->getType() != secTypeNone) {
380 if (accept) {
381 os->writeU32(secResultOK);
382 } else {
383 os->writeU32(secResultFailed);
384 if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message
385 os->writeString(reason);
386 }
387 os->flush();
388 }
389
390 if (accept) {
391 state_ = RFBSTATE_INITIALISATION;
392 reader_ = new SMsgReaderV3(this, is);
393 writer_ = new SMsgWriterV3(&cp, os);
394 authSuccess();
395 } else {
396 state_ = RFBSTATE_INVALID;
397 throw AuthFailureException(reason);
398 }
399}
400
401void SConnection::setInitialColourMap()
402{
403}
404
405void SConnection::clientInit(bool shared)
406{
407 writer_->writeServerInit();
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +0000408
409 // FIXME: Is this a right place for this code?
410 if (cp.tightExtensionsEnabled)
411 sendInteractionCaps();
412
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000413 state_ = RFBSTATE_NORMAL;
414}
415
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +0000416// FIXME: Is this class a right place for this function?
417void SConnection::sendInteractionCaps()
418{
419 // Advertise support for non-standard server-to-client messages.
420 CapsList scaps;
421 int nServerMsgTypes = scaps.getSize();
422
423 // Advertise support for non-standard client-to-server messages.
424 CapsList ccaps;
425 int nClientMsgTypes = ccaps.getSize();
426
427 // Advertise all supported encoding types (except raw encoding).
428 CapsList ecaps;
Constantin Kaplinsky4ad04c12006-09-12 06:37:33 +0000429
430 // First, add true encodings.
431 for (unsigned int i = 1; i <= encodingMax; i++) {
432 if (Encoder::supported(i)) {
433 // FIXME: Ideally, encoders themselves should provide capability info.
434 switch(i) {
435 case encodingRRE:
436 ecaps.addStandard(encodingRRE, "RRE_____");
437 break;
438 case encodingCoRRE:
439 ecaps.addStandard(encodingCoRRE, "CORRE___");
440 break;
441 case encodingHextile:
442 ecaps.addStandard(encodingHextile, "HEXTILE_");
443 break;
444 case encodingZRLE:
445 ecaps.addStandard(encodingZRLE, "ZRLE____");
446 break;
447 case encodingTight:
448 ecaps.addTightExt(encodingTight, "TIGHT___");
449 break;
450 }
451 }
452 }
453
454 // CopyRect is special - Encoder::supported() returns 0 for it,
455 // that's why we add it here explicitly.
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +0000456 ecaps.addStandard(encodingCopyRect, "COPYRECT");
Constantin Kaplinsky4ad04c12006-09-12 06:37:33 +0000457
458 // Add supported pseudo encodings as well.
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +0000459 ecaps.addTightExt(pseudoEncodingCompressLevel0, "COMPRLVL");
460 ecaps.addTightExt(pseudoEncodingQualityLevel0, "JPEGQLVL");
461 ecaps.addTightExt(pseudoEncodingXCursor, "X11CURSR");
462 ecaps.addTightExt(pseudoEncodingCursor, "RCHCURSR");
463 ecaps.addTightExt(pseudoEncodingLastRect, "LASTRECT");
464 ecaps.addStandard(pseudoEncodingDesktopSize, "NEWFBSIZ");
465
466 os->writeU16(scaps.getSize());
467 os->writeU16(ccaps.getSize());
468 os->writeU16(ecaps.getSize());
469 os->writeU16(0);
470 if (scaps.getSize())
471 scaps.write(os);
472 if (ccaps.getSize())
473 ccaps.write(os);
474 if (ecaps.getSize())
475 ecaps.write(os);
476 os->flush();
477}
478
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000479void SConnection::setPixelFormat(const PixelFormat& pf)
480{
481 SMsgHandler::setPixelFormat(pf);
482 readyForSetColourMapEntries = true;
483}
484
485void SConnection::framebufferUpdateRequest(const Rect& r, bool incremental)
486{
487 if (!readyForSetColourMapEntries) {
488 readyForSetColourMapEntries = true;
489 if (!cp.pf().trueColour) {
490 setInitialColourMap();
491 }
492 }
493}