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> |
| 29 | |
| 30 | #include <rfb/LogWriter.h> |
| 31 | |
| 32 | using namespace rfb; |
| 33 | |
| 34 | static LogWriter vlog("SConnection"); |
| 35 | |
| 36 | // AccessRights values |
| 37 | const SConnection::AccessRights SConnection::AccessView = 0x0001; |
| 38 | const SConnection::AccessRights SConnection::AccessKeyEvents = 0x0002; |
| 39 | const SConnection::AccessRights SConnection::AccessPtrEvents = 0x0004; |
| 40 | const SConnection::AccessRights SConnection::AccessCutText = 0x0008; |
| 41 | const SConnection::AccessRights SConnection::AccessDefault = 0x03ff; |
| 42 | const SConnection::AccessRights SConnection::AccessNoQuery = 0x0400; |
| 43 | const SConnection::AccessRights SConnection::AccessFull = 0xffff; |
| 44 | |
| 45 | |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 46 | SConnection::SConnection(bool reverseConnection_) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 47 | : readyForSetColourMapEntries(false), |
| 48 | is(0), os(0), reader_(0), writer_(0), |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 49 | security(0), ssecurity(0), state_(RFBSTATE_UNINITIALISED), |
Pierre Ossman | 02e43d7 | 2009-03-05 11:57:11 +0000 | [diff] [blame] | 50 | reverseConnection(reverseConnection_) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 51 | { |
| 52 | defaultMajorVersion = 3; |
| 53 | defaultMinorVersion = 8; |
| 54 | if (rfb::Server::protocol3_3) |
| 55 | defaultMinorVersion = 3; |
| 56 | |
| 57 | cp.setVersion(defaultMajorVersion, defaultMinorVersion); |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 58 | |
Adam Tkac | bfd66c1 | 2010-10-01 08:33:29 +0000 | [diff] [blame] | 59 | security = new SecurityServer(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | SConnection::~SConnection() |
| 63 | { |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 64 | if (ssecurity) ssecurity->destroy(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 65 | deleteReaderAndWriter(); |
| 66 | } |
| 67 | |
| 68 | void SConnection::deleteReaderAndWriter() |
| 69 | { |
| 70 | delete reader_; |
| 71 | reader_ = 0; |
| 72 | delete writer_; |
| 73 | writer_ = 0; |
| 74 | } |
| 75 | |
| 76 | void SConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_) |
| 77 | { |
| 78 | is = is_; |
| 79 | os = os_; |
| 80 | } |
| 81 | |
| 82 | void SConnection::initialiseProtocol() |
| 83 | { |
| 84 | cp.writeVersion(os); |
| 85 | state_ = RFBSTATE_PROTOCOL_VERSION; |
| 86 | } |
| 87 | |
| 88 | void 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 | |
| 106 | void 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 Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 145 | secTypes = security->GetEnabledSecTypes(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 146 | |
| 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 Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 164 | ssecurity = security->GetSSecurity(*i); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 165 | 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 | |
| 182 | void SConnection::processSecurityTypeMsg() |
| 183 | { |
| 184 | vlog.debug("processing security type message"); |
| 185 | int secType = is->readU8(); |
| 186 | |
Constantin Kaplinsky | 5fa9d22 | 2006-09-06 10:32:06 +0000 | [diff] [blame] | 187 | processSecurityType(secType); |
| 188 | } |
| 189 | |
| 190 | void SConnection::processSecurityType(int secType) |
| 191 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 192 | // Verify that the requested security type should be offered |
| 193 | std::list<rdr::U8> secTypes; |
| 194 | std::list<rdr::U8>::iterator i; |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 195 | |
| 196 | secTypes = security->GetEnabledSecTypes(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 197 | 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 Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 207 | ssecurity = security->GetSSecurity(secType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 208 | } catch (rdr::Exception& e) { |
| 209 | throwConnFailedException(e.str()); |
| 210 | } |
| 211 | |
| 212 | processSecurityMsg(); |
| 213 | } |
| 214 | |
| 215 | void SConnection::processSecurityMsg() |
| 216 | { |
| 217 | vlog.debug("processing security message"); |
| 218 | try { |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 219 | bool done = ssecurity->processMsg(this); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 220 | if (done) { |
| 221 | state_ = RFBSTATE_QUERYING; |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 222 | queryConnection(ssecurity->getUserName()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 223 | } |
| 224 | } catch (AuthFailureException& e) { |
| 225 | vlog.error("AuthFailureException: %s", e.str()); |
| 226 | os->writeU32(secResultFailed); |
| 227 | if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message |
| 228 | os->writeString(e.str()); |
| 229 | os->flush(); |
| 230 | throw; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void SConnection::processInitMsg() |
| 235 | { |
| 236 | vlog.debug("reading client initialisation"); |
| 237 | reader_->readClientInit(); |
| 238 | } |
| 239 | |
| 240 | void SConnection::throwConnFailedException(const char* msg) |
| 241 | { |
Pierre Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame] | 242 | vlog.info("%s", msg); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 243 | if (state_ == RFBSTATE_PROTOCOL_VERSION) { |
| 244 | if (cp.majorVersion == 3 && cp.minorVersion == 3) { |
| 245 | os->writeU32(0); |
| 246 | os->writeString(msg); |
| 247 | os->flush(); |
| 248 | } else { |
| 249 | os->writeU8(0); |
| 250 | os->writeString(msg); |
| 251 | os->flush(); |
| 252 | } |
| 253 | } |
| 254 | state_ = RFBSTATE_INVALID; |
| 255 | throw ConnFailedException(msg); |
| 256 | } |
| 257 | |
| 258 | void SConnection::writeConnFailedFromScratch(const char* msg, |
| 259 | rdr::OutStream* os) |
| 260 | { |
| 261 | os->writeBytes("RFB 003.003\n", 12); |
| 262 | os->writeU32(0); |
| 263 | os->writeString(msg); |
| 264 | os->flush(); |
| 265 | } |
| 266 | |
| 267 | void SConnection::versionReceived() |
| 268 | { |
| 269 | } |
| 270 | |
| 271 | void SConnection::authSuccess() |
| 272 | { |
| 273 | } |
| 274 | |
| 275 | void SConnection::queryConnection(const char* userName) |
| 276 | { |
| 277 | approveConnection(true); |
| 278 | } |
| 279 | |
| 280 | void SConnection::approveConnection(bool accept, const char* reason) |
| 281 | { |
| 282 | if (state_ != RFBSTATE_QUERYING) |
| 283 | throw Exception("SConnection::approveConnection: invalid state"); |
| 284 | |
| 285 | if (!reason) reason = "Authentication failure"; |
| 286 | |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 287 | if (!cp.beforeVersion(3,8) || ssecurity->getType() != secTypeNone) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 288 | if (accept) { |
| 289 | os->writeU32(secResultOK); |
| 290 | } else { |
| 291 | os->writeU32(secResultFailed); |
| 292 | if (!cp.beforeVersion(3,8)) // 3.8 onwards have failure message |
| 293 | os->writeString(reason); |
| 294 | } |
| 295 | os->flush(); |
| 296 | } |
| 297 | |
| 298 | if (accept) { |
| 299 | state_ = RFBSTATE_INITIALISATION; |
Pierre Ossman | 7638e9c | 2014-01-16 13:12:40 +0100 | [diff] [blame] | 300 | reader_ = new SMsgReader(this, is); |
| 301 | writer_ = new SMsgWriter(&cp, os); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 302 | authSuccess(); |
| 303 | } else { |
| 304 | state_ = RFBSTATE_INVALID; |
| 305 | throw AuthFailureException(reason); |
| 306 | } |
| 307 | } |
| 308 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 309 | void SConnection::clientInit(bool shared) |
| 310 | { |
| 311 | writer_->writeServerInit(); |
| 312 | state_ = RFBSTATE_NORMAL; |
| 313 | } |
| 314 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 315 | void SConnection::setPixelFormat(const PixelFormat& pf) |
| 316 | { |
| 317 | SMsgHandler::setPixelFormat(pf); |
| 318 | readyForSetColourMapEntries = true; |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 319 | if (!pf.trueColour) |
| 320 | writeFakeColourMap(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void SConnection::framebufferUpdateRequest(const Rect& r, bool incremental) |
| 324 | { |
| 325 | if (!readyForSetColourMapEntries) { |
| 326 | readyForSetColourMapEntries = true; |
| 327 | if (!cp.pf().trueColour) { |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 328 | writeFakeColourMap(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 332 | |
| 333 | void SConnection::fence(rdr::U32 flags, unsigned len, const char data[]) |
| 334 | { |
| 335 | if (!(flags & fenceFlagRequest)) |
| 336 | return; |
| 337 | |
| 338 | // We cannot guarantee any synchronisation at this level |
| 339 | flags = 0; |
| 340 | |
| 341 | writer()->writeFence(flags, len, data); |
| 342 | } |
Pierre Ossman | c898d9a | 2011-11-14 16:22:23 +0000 | [diff] [blame] | 343 | |
| 344 | void SConnection::enableContinuousUpdates(bool enable, |
| 345 | int x, int y, int w, int h) |
| 346 | { |
| 347 | } |
Pierre Ossman | b6b4dc6 | 2014-01-20 15:05:21 +0100 | [diff] [blame] | 348 | |
| 349 | void SConnection::writeFakeColourMap(void) |
| 350 | { |
| 351 | int i; |
| 352 | rdr::U16 red[256], green[256], blue[256]; |
| 353 | |
| 354 | for (i = 0;i < 256;i++) |
| 355 | cp.pf().rgbFromPixel(i, &red[i], &green[i], &blue[i]); |
| 356 | |
| 357 | writer()->writeSetColourMapEntries(0, 256, red, green, blue); |
| 358 | } |