blob: 785d30564ec6e5f0eab62c06aeebbd56dcd57e1a [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>
Pierre Ossman0068a4f2015-11-09 15:48:19 +010020
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000021#include <rfb/Exception.h>
Pierre Ossmanc754cce2011-11-14 15:44:11 +000022#include <rfb/fenceTypes.h>
Pierre Ossman7638e9c2014-01-16 13:12:40 +010023#include <rfb/CMsgReader.h>
24#include <rfb/CMsgWriter.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000025#include <rfb/CSecurity.h>
Adam Tkac5a0caed2010-04-23 13:58:10 +000026#include <rfb/Security.h>
Pierre Ossman0068a4f2015-11-09 15:48:19 +010027#include <rfb/SecurityClient.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000028#include <rfb/CConnection.h>
29#include <rfb/util.h>
30
31#include <rfb/LogWriter.h>
32
Pierre Ossman0068a4f2015-11-09 15:48:19 +010033#include <rdr/InStream.h>
34#include <rdr/OutStream.h>
35
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036using namespace rfb;
37
38static LogWriter vlog("CConnection");
39
40CConnection::CConnection()
Adam Tkacf324dc42010-04-23 14:10:17 +000041 : csecurity(0), is(0), os(0), reader_(0), writer_(0),
Adam Tkac05a0cd62010-07-20 15:07:44 +000042 shared(false),
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000043 state_(RFBSTATE_UNINITIALISED), useProtocol3_3(false)
44{
Adam Tkacbfd66c12010-10-01 08:33:29 +000045 security = new SecurityClient();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046}
47
48CConnection::~CConnection()
49{
Adam Tkacf324dc42010-04-23 14:10:17 +000050 if (csecurity) csecurity->destroy();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051 delete reader_;
52 reader_ = 0;
53 delete writer_;
54 writer_ = 0;
55}
56
57void CConnection::setStreams(rdr::InStream* is_, rdr::OutStream* os_)
58{
59 is = is_;
60 os = os_;
61}
62
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000063void CConnection::initialiseProtocol()
64{
65 state_ = RFBSTATE_PROTOCOL_VERSION;
66}
67
68void 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
85void 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)) {
Pierre Ossmana7bbe9c2015-03-03 16:17:51 +0100100 vlog.error("Server gave unsupported RFB protocol version %d.%d",
101 cp.majorVersion, cp.minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102 state_ = RFBSTATE_INVALID;
Pierre Ossmana7bbe9c2015-03-03 16:17:51 +0100103 throw Exception("Server gave unsupported RFB protocol version %d.%d",
104 cp.majorVersion, cp.minorVersion);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000105 } else if (useProtocol3_3 || cp.beforeVersion(3,7)) {
106 cp.setVersion(3,3);
107 } else if (cp.afterVersion(3,8)) {
108 cp.setVersion(3,8);
109 }
110
111 cp.writeVersion(os);
112 state_ = RFBSTATE_SECURITY_TYPES;
113
114 vlog.info("Using RFB protocol version %d.%d",
115 cp.majorVersion, cp.minorVersion);
116}
117
118
119void CConnection::processSecurityTypesMsg()
120{
121 vlog.debug("processing security types message");
122
123 int secType = secTypeInvalid;
124
Adam Tkac05a0cd62010-07-20 15:07:44 +0000125 std::list<rdr::U8> secTypes;
126 secTypes = security->GetEnabledSecTypes();
127
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000128 if (cp.isVersion(3,3)) {
129
130 // legacy 3.3 server may only offer "vnc authentication" or "none"
131
132 secType = is->readU32();
133 if (secType == secTypeInvalid) {
134 throwConnFailedException();
135
136 } else if (secType == secTypeNone || secType == secTypeVncAuth) {
Adam Tkac05a0cd62010-07-20 15:07:44 +0000137 std::list<rdr::U8>::iterator i;
138 for (i = secTypes.begin(); i != secTypes.end(); i++)
139 if (*i == secType) {
140 secType = *i;
141 break;
142 }
143
144 if (i == secTypes.end())
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000145 secType = secTypeInvalid;
146 } else {
147 vlog.error("Unknown 3.3 security type %d", secType);
148 throw Exception("Unknown 3.3 security type");
149 }
150
151 } else {
152
153 // >=3.7 server will offer us a list
154
155 int nServerSecTypes = is->readU8();
156 if (nServerSecTypes == 0)
157 throwConnFailedException();
158
Adam Tkac05a0cd62010-07-20 15:07:44 +0000159 std::list<rdr::U8>::iterator j;
Adam Tkac05a0cd62010-07-20 15:07:44 +0000160
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000161 for (int i = 0; i < nServerSecTypes; i++) {
162 rdr::U8 serverSecType = is->readU8();
163 vlog.debug("Server offers security type %s(%d)",
Adam Tkac7cb47d62011-02-21 12:55:24 +0000164 secTypeName(serverSecType), serverSecType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000165
Adam Tkac7cb47d62011-02-21 12:55:24 +0000166 /*
167 * Use the first type sent by server which matches client's type.
168 * It means server's order specifies priority.
169 */
170 if (secType == secTypeInvalid) {
171 for (j = secTypes.begin(); j != secTypes.end(); j++)
172 if (*j == serverSecType) {
173 secType = *j;
174 break;
175 }
176 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177 }
178
179 // Inform the server of our decision
180 if (secType != secTypeInvalid) {
181 os->writeU8(secType);
182 os->flush();
Pierre Ossman71d66662014-11-11 13:42:51 +0100183 vlog.info("Choosing security type %s(%d)",secTypeName(secType),secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000184 }
185 }
186
187 if (secType == secTypeInvalid) {
188 state_ = RFBSTATE_INVALID;
189 vlog.error("No matching security types");
190 throw Exception("No matching security types");
191 }
192
193 state_ = RFBSTATE_SECURITY;
Adam Tkacf324dc42010-04-23 14:10:17 +0000194 csecurity = security->GetCSecurity(secType);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000195 processSecurityMsg();
196}
197
198void CConnection::processSecurityMsg()
199{
200 vlog.debug("processing security message");
Adam Tkacf324dc42010-04-23 14:10:17 +0000201 if (csecurity->processMsg(this)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000202 state_ = RFBSTATE_SECURITY_RESULT;
203 processSecurityResultMsg();
204 }
205}
206
207void CConnection::processSecurityResultMsg()
208{
209 vlog.debug("processing security result message");
210 int result;
Adam Tkacf324dc42010-04-23 14:10:17 +0000211 if (cp.beforeVersion(3,8) && csecurity->getType() == secTypeNone) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000212 result = secResultOK;
213 } else {
214 if (!is->checkNoWait(1)) return;
215 result = is->readU32();
216 }
217 switch (result) {
218 case secResultOK:
219 securityCompleted();
220 return;
221 case secResultFailed:
222 vlog.debug("auth failed");
223 break;
224 case secResultTooMany:
225 vlog.debug("auth failed - too many tries");
226 break;
227 default:
228 throw Exception("Unknown security result from server");
229 }
230 CharArray reason;
231 if (cp.beforeVersion(3,8))
Adam Tkacd36b6262009-09-04 10:57:20 +0000232 reason.buf = strDup("Authentication failure");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000233 else
234 reason.buf = is->readString();
235 state_ = RFBSTATE_INVALID;
236 throw AuthFailureException(reason.buf);
237}
238
239void CConnection::processInitMsg()
240{
241 vlog.debug("reading server initialisation");
242 reader_->readServerInit();
243}
244
245void CConnection::throwConnFailedException()
246{
247 state_ = RFBSTATE_INVALID;
248 CharArray reason;
249 reason.buf = is->readString();
250 throw ConnFailedException(reason.buf);
251}
252
253void CConnection::securityCompleted()
254{
255 state_ = RFBSTATE_INITIALISATION;
Pierre Ossman7638e9c2014-01-16 13:12:40 +0100256 reader_ = new CMsgReader(this, is);
257 writer_ = new CMsgWriter(&cp, os);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000258 vlog.debug("Authentication success!");
259 authSuccess();
260 writer_->writeClientInit(shared);
261}
262
263void CConnection::authSuccess()
264{
265}
266
267void CConnection::serverInit()
268{
269 state_ = RFBSTATE_NORMAL;
270 vlog.debug("initialisation done");
271}
Pierre Ossmanc754cce2011-11-14 15:44:11 +0000272
273void CConnection::fence(rdr::U32 flags, unsigned len, const char data[])
274{
275 CMsgHandler::fence(flags, len, data);
276
277 if (!(flags & fenceFlagRequest))
278 return;
279
280 // We cannot guarantee any synchronisation at this level
281 flags = 0;
282
283 writer()->writeFence(flags, len, data);
284}