Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 2 | * Copyright 2011 Pierre Ossman for Cendio AB |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 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 Tkac | 5a0caed | 2010-04-23 13:58:10 +0000 | [diff] [blame] | 22 | #include <rfb/Security.h> |
Constantin Kaplinsky | dafbb01 | 2007-04-05 08:43:25 +0000 | [diff] [blame] | 23 | #include <rfb/msgTypes.h> |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 24 | #include <rfb/fenceTypes.h> |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 25 | #include <rfb/SMsgReader.h> |
| 26 | #include <rfb/SMsgWriter.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 27 | #include <rfb/SConnection.h> |
| 28 | #include <rfb/ServerCore.h> |
Pierre Ossman | 4870081 | 2014-09-17 17:11:56 +0200 | [diff] [blame^] | 29 | #include <rfb/encodings.h> |
| 30 | #include <rfb/EncodeManager.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 31 | |
| 32 | #include <rfb/LogWriter.h> |
| 33 | |
| 34 | using namespace rfb; |
| 35 | |
| 36 | static LogWriter vlog("SConnection"); |
| 37 | |
| 38 | // AccessRights values |
| 39 | const SConnection::AccessRights SConnection::AccessView = 0x0001; |
| 40 | const SConnection::AccessRights SConnection::AccessKeyEvents = 0x0002; |
| 41 | const SConnection::AccessRights SConnection::AccessPtrEvents = 0x0004; |
| 42 | const SConnection::AccessRights SConnection::AccessCutText = 0x0008; |
| 43 | const SConnection::AccessRights SConnection::AccessDefault = 0x03ff; |
| 44 | const SConnection::AccessRights SConnection::AccessNoQuery = 0x0400; |
| 45 | const SConnection::AccessRights SConnection::AccessFull = 0xffff; |
| 46 | |
| 47 | |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 48 | SConnection::SConnection(bool reverseConnection_) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 49 | : readyForSetColourMapEntries(false), |
| 50 | is(0), os(0), reader_(0), writer_(0), |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 51 | security(0), ssecurity(0), state_(RFBSTATE_UNINITIALISED), |
Pierre Ossman | 4870081 | 2014-09-17 17:11:56 +0200 | [diff] [blame^] | 52 | reverseConnection(reverseConnection_), |
| 53 | preferredEncoding(encodingRaw) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 54 | { |
| 55 | defaultMajorVersion = 3; |
| 56 | defaultMinorVersion = 8; |
| 57 | if (rfb::Server::protocol3_3) |
| 58 | defaultMinorVersion = 3; |
| 59 | |
| 60 | cp.setVersion(defaultMajorVersion, defaultMinorVersion); |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 61 | |
Adam Tkac | bfd66c1 | 2010-10-01 08:33:29 +0000 | [diff] [blame] | 62 | security = new SecurityServer(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | SConnection::~SConnection() |
| 66 | { |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 67 | if (ssecurity) ssecurity->destroy(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 68 | deleteReaderAndWriter(); |
| 69 | } |
| 70 | |
| 71 | void SConnection::deleteReaderAndWriter() |
| 72 | { |
| 73 | delete reader_; |
| 74 | reader_ = 0; |
| 75 | delete writer_; |
| 76 | writer_ = 0; |
| 77 | } |
| 78 | |
| 79 | void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_) |
| 80 | { |
| 81 | is = is_; |
| 82 | os = os_; |
| 83 | } |
| 84 | |
| 85 | void SConnection::initialiseProtocol() |
| 86 | { |
| 87 | cp.writeVersion(os); |
| 88 | state_ = RFBSTATE_PROTOCOL_VERSION; |
| 89 | } |
| 90 | |
| 91 | void SConnection::processMsg() |
| 92 | { |
| 93 | switch (state_) { |
| 94 | case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break; |
| 95 | case RFBSTATE_SECURITY_TYPE: processSecurityTypeMsg(); break; |
| 96 | case RFBSTATE_SECURITY: processSecurityMsg(); break; |
| 97 | case RFBSTATE_INITIALISATION: processInitMsg(); break; |
| 98 | case RFBSTATE_NORMAL: reader_->readMsg(); break; |
| 99 | case RFBSTATE_QUERYING: |
| 100 | throw Exception("SConnection::processMsg: bogus data from client while " |
| 101 | "querying"); |
| 102 | case RFBSTATE_UNINITIALISED: |
| 103 | throw Exception("SConnection::processMsg: not initialised yet?"); |
| 104 | default: |
| 105 | throw Exception("SConnection::processMsg: invalid state"); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void SConnection::processVersionMsg() |
| 110 | { |
| 111 | vlog.debug("reading protocol version"); |
| 112 | bool done; |
| 113 | if (!cp.readVersion(is, &done)) { |
| 114 | state_ = RFBSTATE_INVALID; |
| 115 | throw Exception("reading version failed: not an RFB client?"); |
| 116 | } |
| 117 | if (!done) return; |
| 118 | |
| 119 | vlog.info("Client needs protocol version %d.%d", |
| 120 | cp.majorVersion, cp.minorVersion); |
| 121 | |
| 122 | if (cp.majorVersion != 3) { |
| 123 | // unknown protocol version |
| 124 | char msg[256]; |
| 125 | sprintf(msg,"Error: client needs protocol version %d.%d, server has %d.%d", |
| 126 | cp.majorVersion, cp.minorVersion, |
| 127 | defaultMajorVersion, defaultMinorVersion); |
| 128 | throwConnFailedException(msg); |
| 129 | } |
| 130 | |
| 131 | if (cp.minorVersion != 3 && cp.minorVersion != 7 && cp.minorVersion != 8) { |
| 132 | vlog.error("Client uses unofficial protocol version %d.%d", |
| 133 | cp.majorVersion,cp.minorVersion); |
| 134 | if (cp.minorVersion >= 8) |
| 135 | cp.minorVersion = 8; |
| 136 | else if (cp.minorVersion == 7) |
| 137 | cp.minorVersion = 7; |
| 138 | else |
| 139 | cp.minorVersion = 3; |
| 140 | vlog.error("Assuming compatibility with version %d.%d", |
| 141 | cp.majorVersion,cp.minorVersion); |
| 142 | } |
| 143 | |
| 144 | versionReceived(); |
| 145 | |
| 146 | std::list<rdr::U8> secTypes; |
| 147 | std::list<rdr::U8>::iterator i; |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 148 | secTypes = security->GetEnabledSecTypes(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 149 | |
| 150 | if (cp.isVersion(3,3)) { |
| 151 | |
| 152 | // cope with legacy 3.3 client only if "no authentication" or "vnc |
| 153 | // authentication" is supported. |
| 154 | for (i=secTypes.begin(); i!=secTypes.end(); i++) { |
| 155 | if (*i == secTypeNone || *i == secTypeVncAuth) break; |
| 156 | } |
| 157 | if (i == secTypes.end()) { |
| 158 | char msg[256]; |
| 159 | sprintf(msg,"No supported security type for %d.%d client", |
| 160 | cp.majorVersion, cp.minorVersion); |
| 161 | throwConnFailedException(msg); |
| 162 | } |
| 163 | |
| 164 | os->writeU32(*i); |
| 165 | if (*i == secTypeNone) os->flush(); |
| 166 | state_ = RFBSTATE_SECURITY; |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 167 | ssecurity = security->GetSSecurity(*i); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 168 | processSecurityMsg(); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | // list supported security types for >=3.7 clients |
| 173 | |
| 174 | if (secTypes.empty()) |
| 175 | throwConnFailedException("No supported security types"); |
| 176 | |
| 177 | os->writeU8(secTypes.size()); |
| 178 | for (i=secTypes.begin(); i!=secTypes.end(); i++) |
| 179 | os->writeU8(*i); |
| 180 | os->flush(); |
| 181 | state_ = RFBSTATE_SECURITY_TYPE; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | void SConnection::processSecurityTypeMsg() |
| 186 | { |
| 187 | vlog.debug("processing security type message"); |
| 188 | int secType = is->readU8(); |
| 189 | |
Constantin Kaplinsky | 5fa9d22 | 2006-09-06 10:32:06 +0000 | [diff] [blame] | 190 | processSecurityType(secType); |
| 191 | } |
| 192 | |
| 193 | void SConnection::processSecurityType(int secType) |
| 194 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 195 | // Verify that the requested security type should be offered |
| 196 | std::list<rdr::U8> secTypes; |
| 197 | std::list<rdr::U8>::iterator i; |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 198 | |
| 199 | secTypes = security->GetEnabledSecTypes(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 200 | for (i=secTypes.begin(); i!=secTypes.end(); i++) |
| 201 | if (*i == secType) break; |
| 202 | if (i == secTypes.end()) |
| 203 | throw Exception("Requested security type not available"); |
| 204 | |
| 205 | vlog.info("Client requests security type %s(%d)", |
| 206 | secTypeName(secType),secType); |
| 207 | |
| 208 | try { |
| 209 | state_ = RFBSTATE_SECURITY; |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 210 | ssecurity = security->GetSSecurity(secType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 211 | } catch (rdr::Exception& e) { |
| 212 | throwConnFailedException(e.str()); |
| 213 | } |
| 214 | |
| 215 | processSecurityMsg(); |
| 216 | } |
| 217 | |
| 218 | void SConnection::processSecurityMsg() |
| 219 | { |
| 220 | vlog.debug("processing security message"); |
| 221 | try { |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 222 | bool done = ssecurity->processMsg(this); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 223 | if (done) { |
| 224 | state_ = RFBSTATE_QUERYING; |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 225 | queryConnection(ssecurity->getUserName()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 226 | } |
| 227 | } catch (AuthFailureException& e) { |
| 228 | vlog.error("AuthFailureException: %s", e.str()); |
| 229 | os->writeU32(secResultFailed); |
| 230 | if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message |
| 231 | os->writeString(e.str()); |
| 232 | os->flush(); |
| 233 | throw; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void SConnection::processInitMsg() |
| 238 | { |
| 239 | vlog.debug("reading client initialisation"); |
| 240 | reader_->readClientInit(); |
| 241 | } |
| 242 | |
| 243 | void SConnection::throwConnFailedException(const char* msg) |
| 244 | { |
Pierre Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame] | 245 | vlog.info("%s", msg); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 246 | if (state_ == RFBSTATE_PROTOCOL_VERSION) { |
| 247 | if (cp.majorVersion == 3 && cp.minorVersion == 3) { |
| 248 | os->writeU32(0); |
| 249 | os->writeString(msg); |
| 250 | os->flush(); |
| 251 | } else { |
| 252 | os->writeU8(0); |
| 253 | os->writeString(msg); |
| 254 | os->flush(); |
| 255 | } |
| 256 | } |
| 257 | state_ = RFBSTATE_INVALID; |
| 258 | throw ConnFailedException(msg); |
| 259 | } |
| 260 | |
| 261 | void SConnection::writeConnFailedFromScratch(const char* msg, |
| 262 | rdr::OutStream* os) |
| 263 | { |
| 264 | os->writeBytes("RFB 003.003\n", 12); |
| 265 | os->writeU32(0); |
| 266 | os->writeString(msg); |
| 267 | os->flush(); |
| 268 | } |
| 269 | |
Pierre Ossman | 4870081 | 2014-09-17 17:11:56 +0200 | [diff] [blame^] | 270 | void SConnection::setEncodings(int nEncodings, rdr::S32* encodings) |
| 271 | { |
| 272 | int i; |
| 273 | |
| 274 | preferredEncoding = encodingRaw; |
| 275 | for (i = 0;i < nEncodings;i++) { |
| 276 | if (EncodeManager::supported(encodings[i])) { |
| 277 | preferredEncoding = encodings[i]; |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | SMsgHandler::setEncodings(nEncodings, encodings); |
| 283 | } |
| 284 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 285 | void SConnection::versionReceived() |
| 286 | { |
| 287 | } |
| 288 | |
| 289 | void SConnection::authSuccess() |
| 290 | { |
| 291 | } |
| 292 | |
| 293 | void SConnection::queryConnection(const char* userName) |
| 294 | { |
| 295 | approveConnection(true); |
| 296 | } |
| 297 | |
| 298 | void SConnection::approveConnection(bool accept, const char* reason) |
| 299 | { |
| 300 | if (state_ != RFBSTATE_QUERYING) |
| 301 | throw Exception("SConnection::approveConnection: invalid state"); |
| 302 | |
| 303 | if (!reason) reason = "Authentication failure"; |
| 304 | |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 305 | if (!cp.beforeVersion(3,8) || ssecurity->getType() != secTypeNone) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 306 | if (accept) { |
| 307 | os->writeU32(secResultOK); |
| 308 | } else { |
| 309 | os->writeU32(secResultFailed); |
| 310 | if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message |
| 311 | os->writeString(reason); |
| 312 | } |
| 313 | os->flush(); |
| 314 | } |
| 315 | |
| 316 | if (accept) { |
| 317 | state_ = RFBSTATE_INITIALISATION; |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 318 | reader_ = new SMsgReader(this, is); |
| 319 | writer_ = new SMsgWriter(&cp, os); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 320 | authSuccess(); |
| 321 | } else { |
| 322 | state_ = RFBSTATE_INVALID; |
| 323 | throw AuthFailureException(reason); |
| 324 | } |
| 325 | } |
| 326 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 327 | void SConnection::clientInit(bool shared) |
| 328 | { |
| 329 | writer_->writeServerInit(); |
| 330 | state_ = RFBSTATE_NORMAL; |
| 331 | } |
| 332 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 333 | void SConnection::setPixelFormat(const PixelFormat& pf) |
| 334 | { |
| 335 | SMsgHandler::setPixelFormat(pf); |
| 336 | readyForSetColourMapEntries = true; |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 337 | if (!pf.trueColour) |
| 338 | writeFakeColourMap(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void SConnection::framebufferUpdateRequest(const Rect& r, bool incremental) |
| 342 | { |
| 343 | if (!readyForSetColourMapEntries) { |
| 344 | readyForSetColourMapEntries = true; |
| 345 | if (!cp.pf().trueColour) { |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 346 | writeFakeColourMap(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | } |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 350 | |
| 351 | void SConnection::fence(rdr::U32 flags, unsigned len, const char data[]) |
| 352 | { |
| 353 | if (!(flags & fenceFlagRequest)) |
| 354 | return; |
| 355 | |
| 356 | // We cannot guarantee any synchronisation at this level |
| 357 | flags = 0; |
| 358 | |
| 359 | writer()->writeFence(flags, len, data); |
| 360 | } |
Pierre Ossman | c898d9a | 2011-11-14 16:22:23 +0000 | [diff] [blame] | 361 | |
| 362 | void SConnection::enableContinuousUpdates(bool enable, |
| 363 | int x, int y, int w, int h) |
| 364 | { |
| 365 | } |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 366 | |
| 367 | void SConnection::writeFakeColourMap(void) |
| 368 | { |
| 369 | int i; |
| 370 | rdr::U16 red[256], green[256], blue[256]; |
| 371 | |
| 372 | for (i = 0;i < 256;i++) |
| 373 | cp.pf().rgbFromPixel(i, &red[i], &green[i], &blue[i]); |
| 374 | |
| 375 | writer()->writeSetColourMapEntries(0, 256, red, green, blue); |
| 376 | } |