Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* 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 Kaplinsky | ce0907d | 2006-09-08 12:55:37 +0000 | [diff] [blame^] | 22 | #include <rfb/CapsList.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 23 | #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 | |
| 30 | using namespace rfb; |
| 31 | |
| 32 | static LogWriter vlog("SConnection"); |
| 33 | |
| 34 | // AccessRights values |
| 35 | const SConnection::AccessRights SConnection::AccessView = 0x0001; |
| 36 | const SConnection::AccessRights SConnection::AccessKeyEvents = 0x0002; |
| 37 | const SConnection::AccessRights SConnection::AccessPtrEvents = 0x0004; |
| 38 | const SConnection::AccessRights SConnection::AccessCutText = 0x0008; |
| 39 | const SConnection::AccessRights SConnection::AccessDefault = 0x03ff; |
| 40 | const SConnection::AccessRights SConnection::AccessNoQuery = 0x0400; |
| 41 | const SConnection::AccessRights SConnection::AccessFull = 0xffff; |
| 42 | |
| 43 | |
| 44 | SConnection::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 | |
| 58 | SConnection::~SConnection() |
| 59 | { |
| 60 | if (security) security->destroy(); |
| 61 | deleteReaderAndWriter(); |
| 62 | } |
| 63 | |
| 64 | void SConnection::deleteReaderAndWriter() |
| 65 | { |
| 66 | delete reader_; |
| 67 | reader_ = 0; |
| 68 | delete writer_; |
| 69 | writer_ = 0; |
| 70 | } |
| 71 | |
| 72 | void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_) |
| 73 | { |
| 74 | is = is_; |
| 75 | os = os_; |
| 76 | } |
| 77 | |
| 78 | void SConnection::initialiseProtocol() |
| 79 | { |
| 80 | cp.writeVersion(os); |
| 81 | state_ = RFBSTATE_PROTOCOL_VERSION; |
| 82 | } |
| 83 | |
| 84 | void SConnection::processMsg() |
| 85 | { |
| 86 | switch (state_) { |
| 87 | case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break; |
| 88 | case RFBSTATE_SECURITY_TYPE: processSecurityTypeMsg(); break; |
Constantin Kaplinsky | ce0907d | 2006-09-08 12:55:37 +0000 | [diff] [blame^] | 89 | case RFBSTATE_TIGHT_TUNN_TYPE: processTunnelTypeMsg(); break; |
| 90 | case RFBSTATE_TIGHT_AUTH_TYPE: processAuthTypeMsg(); break; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | |
| 104 | void 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 Kaplinsky | ce0907d | 2006-09-08 12:55:37 +0000 | [diff] [blame^] | 167 | // Add a special security type to advertise TightVNC protocol extensions. |
| 168 | secTypes.push_back(secTypeTight); |
| 169 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 170 | // 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 | |
| 183 | void SConnection::processSecurityTypeMsg() |
| 184 | { |
| 185 | vlog.debug("processing security type message"); |
| 186 | int secType = is->readU8(); |
| 187 | |
Constantin Kaplinsky | ce0907d | 2006-09-08 12:55:37 +0000 | [diff] [blame^] | 188 | 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 | |
| 201 | void 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) { |
| 210 | state_ = RFBSTATE_TIGHT_TUNN_TYPE; |
| 211 | } else { |
| 212 | offerAuthentication(); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // NOTE: This function is never called in current version. |
| 217 | void SConnection::processTunnelTypeMsg() |
| 218 | { |
| 219 | vlog.debug("processing tunneling type message (TightVNC extension)"); |
| 220 | int tunnelType = is->readU32(); |
| 221 | vlog.error("unsupported tunneling type %d requested, ignoring", tunnelType); |
| 222 | offerAuthentication(); |
| 223 | } |
| 224 | |
| 225 | void SConnection::offerAuthentication() |
| 226 | { |
| 227 | vlog.debug("offering list of authentication methods"); |
| 228 | |
| 229 | // FIXME: Security types and TightVNC's auth types should be distinguished |
| 230 | // although we use the same codes for NoAuth and VncAuth. |
| 231 | |
| 232 | // See processVersionMsg(), the code below is very similar. |
| 233 | std::list<rdr::U8> secTypes; |
| 234 | std::list<rdr::U8>::iterator i; |
| 235 | securityFactory->getSecTypes(&secTypes, reverseConnection); |
| 236 | |
| 237 | if (secTypes.empty()) |
| 238 | throwConnFailedException("No supported security types"); |
| 239 | |
| 240 | // FIXME: We should send an empty list for "no authentication". |
| 241 | |
| 242 | CapsList caps; |
| 243 | for (i = secTypes.begin(); i != secTypes.end(); i++) { |
| 244 | // FIXME: Use mapping instead (authType -> ProtocolCapability). |
| 245 | // Each auth type should provide its capability data. |
| 246 | // FIXME: Check that client will send auth type listed in capabilities. |
| 247 | // Currently, capabilities should duplicate secTypes exactly, |
| 248 | // but that may change in the future. |
| 249 | switch(*i) { |
| 250 | case secTypeNone: |
| 251 | caps.addStandard(secTypeNone, "NOAUTH__"); |
| 252 | break; |
| 253 | case secTypeVncAuth: |
| 254 | caps.addStandard(secTypeVncAuth, "VNCAUTH_"); |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | os->writeU32(caps.getSize()); |
| 259 | caps.write(os); |
| 260 | os->flush(); |
| 261 | |
| 262 | // FIXME: Capability list is never empty here, otherwise |
| 263 | // we would not expect authentication type message. |
| 264 | state_ = RFBSTATE_TIGHT_AUTH_TYPE; |
| 265 | } |
| 266 | |
| 267 | void SConnection::processAuthTypeMsg() |
| 268 | { |
| 269 | vlog.debug("processing authentication type message (TightVNC extension)"); |
| 270 | |
| 271 | // FIXME: Security types and TightVNC's auth types should be distinguished |
| 272 | // although we use the same codes for NoAuth and VncAuth. |
| 273 | |
| 274 | int secType = is->readU32(); |
Constantin Kaplinsky | 5fa9d22 | 2006-09-06 10:32:06 +0000 | [diff] [blame] | 275 | processSecurityType(secType); |
| 276 | } |
| 277 | |
Constantin Kaplinsky | ce0907d | 2006-09-08 12:55:37 +0000 | [diff] [blame^] | 278 | // |
| 279 | // End of TightVNC-specific code |
| 280 | // |
| 281 | |
Constantin Kaplinsky | 5fa9d22 | 2006-09-06 10:32:06 +0000 | [diff] [blame] | 282 | void SConnection::processSecurityType(int secType) |
| 283 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 284 | // Verify that the requested security type should be offered |
| 285 | std::list<rdr::U8> secTypes; |
| 286 | std::list<rdr::U8>::iterator i; |
| 287 | securityFactory->getSecTypes(&secTypes, reverseConnection); |
| 288 | for (i=secTypes.begin(); i!=secTypes.end(); i++) |
| 289 | if (*i == secType) break; |
| 290 | if (i == secTypes.end()) |
| 291 | throw Exception("Requested security type not available"); |
| 292 | |
| 293 | vlog.info("Client requests security type %s(%d)", |
| 294 | secTypeName(secType),secType); |
| 295 | |
| 296 | try { |
| 297 | state_ = RFBSTATE_SECURITY; |
| 298 | security = securityFactory->getSSecurity(secType, reverseConnection); |
| 299 | } catch (rdr::Exception& e) { |
| 300 | throwConnFailedException(e.str()); |
| 301 | } |
| 302 | |
| 303 | processSecurityMsg(); |
| 304 | } |
| 305 | |
| 306 | void SConnection::processSecurityMsg() |
| 307 | { |
| 308 | vlog.debug("processing security message"); |
| 309 | try { |
| 310 | bool done = security->processMsg(this); |
| 311 | if (done) { |
| 312 | state_ = RFBSTATE_QUERYING; |
| 313 | queryConnection(security->getUserName()); |
| 314 | } |
| 315 | } catch (AuthFailureException& e) { |
| 316 | vlog.error("AuthFailureException: %s", e.str()); |
| 317 | os->writeU32(secResultFailed); |
| 318 | if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message |
| 319 | os->writeString(e.str()); |
| 320 | os->flush(); |
| 321 | throw; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | void SConnection::processInitMsg() |
| 326 | { |
| 327 | vlog.debug("reading client initialisation"); |
| 328 | reader_->readClientInit(); |
| 329 | } |
| 330 | |
| 331 | void SConnection::throwConnFailedException(const char* msg) |
| 332 | { |
| 333 | vlog.info(msg); |
| 334 | if (state_ == RFBSTATE_PROTOCOL_VERSION) { |
| 335 | if (cp.majorVersion == 3 && cp.minorVersion == 3) { |
| 336 | os->writeU32(0); |
| 337 | os->writeString(msg); |
| 338 | os->flush(); |
| 339 | } else { |
| 340 | os->writeU8(0); |
| 341 | os->writeString(msg); |
| 342 | os->flush(); |
| 343 | } |
| 344 | } |
| 345 | state_ = RFBSTATE_INVALID; |
| 346 | throw ConnFailedException(msg); |
| 347 | } |
| 348 | |
| 349 | void SConnection::writeConnFailedFromScratch(const char* msg, |
| 350 | rdr::OutStream* os) |
| 351 | { |
| 352 | os->writeBytes("RFB 003.003\n", 12); |
| 353 | os->writeU32(0); |
| 354 | os->writeString(msg); |
| 355 | os->flush(); |
| 356 | } |
| 357 | |
| 358 | void SConnection::versionReceived() |
| 359 | { |
| 360 | } |
| 361 | |
| 362 | void SConnection::authSuccess() |
| 363 | { |
| 364 | } |
| 365 | |
| 366 | void SConnection::queryConnection(const char* userName) |
| 367 | { |
| 368 | approveConnection(true); |
| 369 | } |
| 370 | |
| 371 | void SConnection::approveConnection(bool accept, const char* reason) |
| 372 | { |
| 373 | if (state_ != RFBSTATE_QUERYING) |
| 374 | throw Exception("SConnection::approveConnection: invalid state"); |
| 375 | |
| 376 | if (!reason) reason = "Authentication failure"; |
| 377 | |
| 378 | if (!cp.beforeVersion(3,8) || security->getType() != secTypeNone) { |
| 379 | if (accept) { |
| 380 | os->writeU32(secResultOK); |
| 381 | } else { |
| 382 | os->writeU32(secResultFailed); |
| 383 | if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message |
| 384 | os->writeString(reason); |
| 385 | } |
| 386 | os->flush(); |
| 387 | } |
| 388 | |
| 389 | if (accept) { |
| 390 | state_ = RFBSTATE_INITIALISATION; |
| 391 | reader_ = new SMsgReaderV3(this, is); |
| 392 | writer_ = new SMsgWriterV3(&cp, os); |
| 393 | authSuccess(); |
| 394 | } else { |
| 395 | state_ = RFBSTATE_INVALID; |
| 396 | throw AuthFailureException(reason); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void SConnection::setInitialColourMap() |
| 401 | { |
| 402 | } |
| 403 | |
| 404 | void SConnection::clientInit(bool shared) |
| 405 | { |
| 406 | writer_->writeServerInit(); |
Constantin Kaplinsky | ce0907d | 2006-09-08 12:55:37 +0000 | [diff] [blame^] | 407 | |
| 408 | // FIXME: Is this a right place for this code? |
| 409 | if (cp.tightExtensionsEnabled) |
| 410 | sendInteractionCaps(); |
| 411 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 412 | state_ = RFBSTATE_NORMAL; |
| 413 | } |
| 414 | |
Constantin Kaplinsky | ce0907d | 2006-09-08 12:55:37 +0000 | [diff] [blame^] | 415 | // FIXME: Is this class a right place for this function? |
| 416 | void SConnection::sendInteractionCaps() |
| 417 | { |
| 418 | // Advertise support for non-standard server-to-client messages. |
| 419 | CapsList scaps; |
| 420 | int nServerMsgTypes = scaps.getSize(); |
| 421 | |
| 422 | // Advertise support for non-standard client-to-server messages. |
| 423 | CapsList ccaps; |
| 424 | int nClientMsgTypes = ccaps.getSize(); |
| 425 | |
| 426 | // Advertise all supported encoding types (except raw encoding). |
| 427 | CapsList ecaps; |
| 428 | // FIXME: Add actually registered encodings (and CopyRect which is special). |
| 429 | // Ideally, encoders themselves should provide capability info. |
| 430 | ecaps.addStandard(encodingCopyRect, "COPYRECT"); |
| 431 | ecaps.addStandard(encodingRRE, "RRE_____"); |
| 432 | ecaps.addStandard(encodingHextile, "HEXTILE_"); |
| 433 | ecaps.addStandard(encodingZRLE, "ZRLE____"); |
| 434 | ecaps.addTightExt(encodingTight, "TIGHT___"); |
| 435 | ecaps.addTightExt(pseudoEncodingCompressLevel0, "COMPRLVL"); |
| 436 | ecaps.addTightExt(pseudoEncodingQualityLevel0, "JPEGQLVL"); |
| 437 | ecaps.addTightExt(pseudoEncodingXCursor, "X11CURSR"); |
| 438 | ecaps.addTightExt(pseudoEncodingCursor, "RCHCURSR"); |
| 439 | ecaps.addTightExt(pseudoEncodingLastRect, "LASTRECT"); |
| 440 | ecaps.addStandard(pseudoEncodingDesktopSize, "NEWFBSIZ"); |
| 441 | |
| 442 | os->writeU16(scaps.getSize()); |
| 443 | os->writeU16(ccaps.getSize()); |
| 444 | os->writeU16(ecaps.getSize()); |
| 445 | os->writeU16(0); |
| 446 | if (scaps.getSize()) |
| 447 | scaps.write(os); |
| 448 | if (ccaps.getSize()) |
| 449 | ccaps.write(os); |
| 450 | if (ecaps.getSize()) |
| 451 | ecaps.write(os); |
| 452 | os->flush(); |
| 453 | } |
| 454 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 455 | void SConnection::setPixelFormat(const PixelFormat& pf) |
| 456 | { |
| 457 | SMsgHandler::setPixelFormat(pf); |
| 458 | readyForSetColourMapEntries = true; |
| 459 | } |
| 460 | |
| 461 | void SConnection::framebufferUpdateRequest(const Rect& r, bool incremental) |
| 462 | { |
| 463 | if (!readyForSetColourMapEntries) { |
| 464 | readyForSetColourMapEntries = true; |
| 465 | if (!cp.pf().trueColour) { |
| 466 | setInitialColourMap(); |
| 467 | } |
| 468 | } |
| 469 | } |