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> |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 21 | #include <rfb/fenceTypes.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 22 | #include <rfb/CMsgReaderV3.h> |
| 23 | #include <rfb/CMsgWriterV3.h> |
| 24 | #include <rfb/CSecurity.h> |
Adam Tkac | 5a0caed | 2010-04-23 13:58:10 +0000 | [diff] [blame] | 25 | #include <rfb/Security.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | #include <rfb/CConnection.h> |
| 27 | #include <rfb/util.h> |
| 28 | |
| 29 | #include <rfb/LogWriter.h> |
| 30 | |
| 31 | using namespace rfb; |
| 32 | |
| 33 | static LogWriter vlog("CConnection"); |
| 34 | |
| 35 | CConnection::CConnection() |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 36 | : csecurity(0), is(0), os(0), reader_(0), writer_(0), |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 37 | shared(false), |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 38 | state_(RFBSTATE_UNINITIALISED), useProtocol3_3(false) |
| 39 | { |
Adam Tkac | bfd66c1 | 2010-10-01 08:33:29 +0000 | [diff] [blame] | 40 | security = new SecurityClient(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | CConnection::~CConnection() |
| 44 | { |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 45 | if (csecurity) csecurity->destroy(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 46 | deleteReaderAndWriter(); |
| 47 | } |
| 48 | |
| 49 | void CConnection::deleteReaderAndWriter() |
| 50 | { |
| 51 | delete reader_; |
| 52 | reader_ = 0; |
| 53 | delete writer_; |
| 54 | writer_ = 0; |
| 55 | } |
| 56 | |
| 57 | void CConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_) |
| 58 | { |
| 59 | is = is_; |
| 60 | os = os_; |
| 61 | } |
| 62 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 63 | void CConnection::initialiseProtocol() |
| 64 | { |
| 65 | state_ = RFBSTATE_PROTOCOL_VERSION; |
| 66 | } |
| 67 | |
| 68 | void CConnection::processMsg() |
| 69 | { |
| 70 | switch (state_) { |
| 71 | |
| 72 | case RFBSTATE_PROTOCOL_VERSION: processVersionMsg(); break; |
| 73 | case RFBSTATE_SECURITY_TYPES: processSecurityTypesMsg(); break; |
| 74 | case RFBSTATE_SECURITY: processSecurityMsg(); break; |
| 75 | case RFBSTATE_SECURITY_RESULT: processSecurityResultMsg(); break; |
| 76 | case RFBSTATE_INITIALISATION: processInitMsg(); break; |
| 77 | case RFBSTATE_NORMAL: reader_->readMsg(); break; |
| 78 | case RFBSTATE_UNINITIALISED: |
| 79 | throw Exception("CConnection::processMsg: not initialised yet?"); |
| 80 | default: |
| 81 | throw Exception("CConnection::processMsg: invalid state"); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void CConnection::processVersionMsg() |
| 86 | { |
| 87 | vlog.debug("reading protocol version"); |
| 88 | bool done; |
| 89 | if (!cp.readVersion(is, &done)) { |
| 90 | state_ = RFBSTATE_INVALID; |
| 91 | throw Exception("reading version failed: not an RFB server?"); |
| 92 | } |
| 93 | if (!done) return; |
| 94 | |
| 95 | vlog.info("Server supports RFB protocol version %d.%d", |
| 96 | cp.majorVersion, cp.minorVersion); |
| 97 | |
| 98 | // The only official RFB protocol versions are currently 3.3, 3.7 and 3.8 |
| 99 | if (cp.beforeVersion(3,3)) { |
| 100 | char msg[256]; |
| 101 | sprintf(msg,"Server gave unsupported RFB protocol version %d.%d", |
| 102 | cp.majorVersion, cp.minorVersion); |
Pierre Ossman | ad8609a | 2012-04-26 09:04:14 +0000 | [diff] [blame] | 103 | vlog.error("%s", msg); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 104 | state_ = RFBSTATE_INVALID; |
| 105 | throw Exception(msg); |
| 106 | } else if (useProtocol3_3 || cp.beforeVersion(3,7)) { |
| 107 | cp.setVersion(3,3); |
| 108 | } else if (cp.afterVersion(3,8)) { |
| 109 | cp.setVersion(3,8); |
| 110 | } |
| 111 | |
| 112 | cp.writeVersion(os); |
| 113 | state_ = RFBSTATE_SECURITY_TYPES; |
| 114 | |
| 115 | vlog.info("Using RFB protocol version %d.%d", |
| 116 | cp.majorVersion, cp.minorVersion); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | void CConnection::processSecurityTypesMsg() |
| 121 | { |
| 122 | vlog.debug("processing security types message"); |
| 123 | |
| 124 | int secType = secTypeInvalid; |
| 125 | |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 126 | std::list<rdr::U8> secTypes; |
| 127 | secTypes = security->GetEnabledSecTypes(); |
| 128 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 129 | if (cp.isVersion(3,3)) { |
| 130 | |
| 131 | // legacy 3.3 server may only offer "vnc authentication" or "none" |
| 132 | |
| 133 | secType = is->readU32(); |
| 134 | if (secType == secTypeInvalid) { |
| 135 | throwConnFailedException(); |
| 136 | |
| 137 | } else if (secType == secTypeNone || secType == secTypeVncAuth) { |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 138 | std::list<rdr::U8>::iterator i; |
| 139 | for (i = secTypes.begin(); i != secTypes.end(); i++) |
| 140 | if (*i == secType) { |
| 141 | secType = *i; |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | if (i == secTypes.end()) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 146 | secType = secTypeInvalid; |
| 147 | } else { |
| 148 | vlog.error("Unknown 3.3 security type %d", secType); |
| 149 | throw Exception("Unknown 3.3 security type"); |
| 150 | } |
| 151 | |
| 152 | } else { |
| 153 | |
| 154 | // >=3.7 server will offer us a list |
| 155 | |
| 156 | int nServerSecTypes = is->readU8(); |
| 157 | if (nServerSecTypes == 0) |
| 158 | throwConnFailedException(); |
| 159 | |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 160 | std::list<rdr::U8>::iterator j; |
Adam Tkac | 05a0cd6 | 2010-07-20 15:07:44 +0000 | [diff] [blame] | 161 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 162 | for (int i = 0; i < nServerSecTypes; i++) { |
| 163 | rdr::U8 serverSecType = is->readU8(); |
| 164 | vlog.debug("Server offers security type %s(%d)", |
Adam Tkac | 7cb47d6 | 2011-02-21 12:55:24 +0000 | [diff] [blame] | 165 | secTypeName(serverSecType), serverSecType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 166 | |
Adam Tkac | 7cb47d6 | 2011-02-21 12:55:24 +0000 | [diff] [blame] | 167 | /* |
| 168 | * Use the first type sent by server which matches client's type. |
| 169 | * It means server's order specifies priority. |
| 170 | */ |
| 171 | if (secType == secTypeInvalid) { |
| 172 | for (j = secTypes.begin(); j != secTypes.end(); j++) |
| 173 | if (*j == serverSecType) { |
| 174 | secType = *j; |
| 175 | break; |
| 176 | } |
| 177 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Inform the server of our decision |
| 181 | if (secType != secTypeInvalid) { |
| 182 | os->writeU8(secType); |
| 183 | os->flush(); |
| 184 | vlog.debug("Choosing security type %s(%d)",secTypeName(secType),secType); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (secType == secTypeInvalid) { |
| 189 | state_ = RFBSTATE_INVALID; |
| 190 | vlog.error("No matching security types"); |
| 191 | throw Exception("No matching security types"); |
| 192 | } |
| 193 | |
| 194 | state_ = RFBSTATE_SECURITY; |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 195 | csecurity = security->GetCSecurity(secType); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 196 | processSecurityMsg(); |
| 197 | } |
| 198 | |
| 199 | void CConnection::processSecurityMsg() |
| 200 | { |
| 201 | vlog.debug("processing security message"); |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 202 | if (csecurity->processMsg(this)) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 203 | state_ = RFBSTATE_SECURITY_RESULT; |
| 204 | processSecurityResultMsg(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void CConnection::processSecurityResultMsg() |
| 209 | { |
| 210 | vlog.debug("processing security result message"); |
| 211 | int result; |
Adam Tkac | f324dc4 | 2010-04-23 14:10:17 +0000 | [diff] [blame] | 212 | if (cp.beforeVersion(3,8) && csecurity->getType() == secTypeNone) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 213 | result = secResultOK; |
| 214 | } else { |
| 215 | if (!is->checkNoWait(1)) return; |
| 216 | result = is->readU32(); |
| 217 | } |
| 218 | switch (result) { |
| 219 | case secResultOK: |
| 220 | securityCompleted(); |
| 221 | return; |
| 222 | case secResultFailed: |
| 223 | vlog.debug("auth failed"); |
| 224 | break; |
| 225 | case secResultTooMany: |
| 226 | vlog.debug("auth failed - too many tries"); |
| 227 | break; |
| 228 | default: |
| 229 | throw Exception("Unknown security result from server"); |
| 230 | } |
| 231 | CharArray reason; |
| 232 | if (cp.beforeVersion(3,8)) |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 233 | reason.buf = strDup("Authentication failure"); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 234 | else |
| 235 | reason.buf = is->readString(); |
| 236 | state_ = RFBSTATE_INVALID; |
| 237 | throw AuthFailureException(reason.buf); |
| 238 | } |
| 239 | |
| 240 | void CConnection::processInitMsg() |
| 241 | { |
| 242 | vlog.debug("reading server initialisation"); |
| 243 | reader_->readServerInit(); |
| 244 | } |
| 245 | |
| 246 | void CConnection::throwConnFailedException() |
| 247 | { |
| 248 | state_ = RFBSTATE_INVALID; |
| 249 | CharArray reason; |
| 250 | reason.buf = is->readString(); |
| 251 | throw ConnFailedException(reason.buf); |
| 252 | } |
| 253 | |
| 254 | void CConnection::securityCompleted() |
| 255 | { |
| 256 | state_ = RFBSTATE_INITIALISATION; |
| 257 | reader_ = new CMsgReaderV3(this, is); |
| 258 | writer_ = new CMsgWriterV3(&cp, os); |
| 259 | vlog.debug("Authentication success!"); |
| 260 | authSuccess(); |
| 261 | writer_->writeClientInit(shared); |
| 262 | } |
| 263 | |
| 264 | void CConnection::authSuccess() |
| 265 | { |
| 266 | } |
| 267 | |
| 268 | void CConnection::serverInit() |
| 269 | { |
| 270 | state_ = RFBSTATE_NORMAL; |
| 271 | vlog.debug("initialisation done"); |
| 272 | } |
Pierre Ossman | c754cce | 2011-11-14 15:44:11 +0000 | [diff] [blame] | 273 | |
| 274 | void CConnection::fence(rdr::U32 flags, unsigned len, const char data[]) |
| 275 | { |
| 276 | CMsgHandler::fence(flags, len, data); |
| 277 | |
| 278 | if (!(flags & fenceFlagRequest)) |
| 279 | return; |
| 280 | |
| 281 | // We cannot guarantee any synchronisation at this level |
| 282 | flags = 0; |
| 283 | |
| 284 | writer()->writeFence(flags, len, data); |
| 285 | } |