summaryrefslogtreecommitdiffstats
path: root/src/client/net/serverconnection.cpp
blob: 0cbd4c2a4e3119fe80fa09b8bf8886903533025a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "serverconnection.h"
#include <QtCore>
#include <QPixmap>
#include <QGuiApplication>
#include <QHostInfo>
#include <unistd.h>
#include <cstdlib>
#include <sys/types.h>
#include <pwd.h>

//#define verbose
#include "../vnc/vncserver.h"

#include "../../shared/util.h"
#include "../../shared/settings.h"
#include "../util/platform/blankscreen.h"
#include "../clientapp/clientapp.h"

#define CHALLENGE_LEN 20

ServerConnection::ServerConnection(const QString& host, const quint16 port, const QByteArray& sessionName, const QByteArray& certHash, bool autoConnect) :
	QObject(NULL), _timerDelete(0), _jpegQuality(80), _authed(0), _autoConnect(autoConnect), _isLocalConnection(-1), _sessionName(sessionName), _certHash(certHash)
{
	_socket = new QSslSocket();
	_blank = new BlankScreen();
	connect(_socket, SIGNAL(encrypted()), this, SLOT(sock_connected()));
	connect(_socket, SIGNAL(readyRead()), this, SLOT(sock_dataArrival()));
	connect(_socket, SIGNAL(disconnected()), this, SLOT(sock_closed()));
	connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(sock_error(QAbstractSocket::SocketError)));
	connect(_socket,
	        SIGNAL(sslErrors(const QList<QSslError> &)),
	        this,
	        SLOT(sslErrors(const QList<QSslError> &))
	       );
	connect(_socket, &QSslSocket::peerVerifyError, [=](const QSslError &error) { qDebug() << "PVE:" << error.errorString(); });
	qDebug("Connecting to %s on port %d", host.toUtf8().data(), (int)port);
	_socket->ignoreSslErrors();
	_socket->connectToHostEncrypted(host, port);
	_timerId = startTimer(4000);
	_lastData = QDateTime::currentMSecsSinceEpoch() + PING_TIMEOUT_MS;
	_timerConnectionCheck = startTimer(5000);
	// Connect the vnc start/stop signal to this class, so we can tell the server about successful vnc server startup
	connect(VncServer::instance(), SIGNAL(started(int, QString&, QString&)), this, SLOT(onVncServerStartStop(int, QString&, QString&)));
}

ServerConnection::~ServerConnection()
{
	this->disconnectFromServer();
	if (_socket != NULL) {
		qCritical("**** SOCKET DELETE IN DESTRUCTOR");
		_socket->deleteLater();
	}
	qDebug("*** Server connection destroyed.");
	_blank->deleteLater();
	_blank = NULL;
}

/**
 * Send the given message to the server.
 */
void ServerConnection::sendMessage(NetworkMessage& message)
{
	if (_socket == NULL || _socket->state() != QAbstractSocket::ConnectedState)
		return;
	message.writeMessage(_socket);
	if (!message.writeComplete()) {
		qCritical() << "SendMessage to server failed!";
	}
}

void ServerConnection::sendAttention(bool on)
{
	NetworkMessage msg;
	msg.setField(_ID, _ATTENTION);
	msg.setField(_ENABLE, _BOOL(on));
	sendMessage(msg);
}

/**
 * Disconnect from current server.
 * Do some cleanup also, like stopping any VNC server/client
 * activity, then finally close the connection.
 */
void ServerConnection::disconnectFromServer()
{
	if (_timerDelete == 0) {
		VncServer::instance()->stop();
		emit closeVnc();
		emit disconnected(this);
		_timerDelete = startTimer(500);
		qDebug("Closing connection to server");
		if (_socket != NULL) {
			_socket->blockSignals(true);
			_socket->abort();
		}
	}
}

/**
 * Handles an incoming message by the server.
 * This is somewhat of a long mess, maybe split this up some day or
 * make it OOP with a huge amount fancy features.
 */
void ServerConnection::handleMsg()
{
	_lastData = QDateTime::currentMSecsSinceEpoch() + PING_TIMEOUT_MS;
	const QString &id = _fromServer.getFieldString(_ID);

	if (_authed == 0) {
		if (id == _CHALLENGE) {
			// Initial challenge request by server
			emit stateChange(ConnectWindow::AwaitingChallengeResponse);
			_myChallenge.resize(CHALLENGE_LEN);
			for (int i = 0; i < CHALLENGE_LEN; ++i) {
				_myChallenge[i] = (char)(qrand() & 0xff);
			}
			QByteArray serverChallenge(_fromServer.getFieldBytes(_CHALLENGE));
			_toServer.reset();
			_toServer.setField(_ID, _CHALLENGE);
			_toServer.setField(_HASH, genSha1(&_sessionName, &serverChallenge));
			_toServer.setField(_CHALLENGE, _myChallenge);
			_toServer.writeMessage(_socket);
			qDebug("Received challenge, replying, sending own challenge step <- 1");
			_authed = 1;
		}
		return;
	}

	if (_authed == 1) {
		if (id == _CHALLENGE) {
			qDebug("Received challenge reply");
			if (_timerId != 0) {
				killTimer(_timerId);
				_timerId = 0;
			}
			// Check challenge response
			QByteArray serverhash(_fromServer.getFieldBytes(_HASH));
			if (serverhash != genSha1(&_sessionName, &_myChallenge)
			        && !_autoConnect) {
				qDebug("invalid. STOP.");
				emit stateChange(ConnectWindow::InvalidSslHash);
				this->disconnectFromServer();
				return;
			}
			emit stateChange(ConnectWindow::LoggingIn);
			const char *user = getpwuid(getuid())->pw_name;
			if (user == NULL || *user == '\0')
				user = getenv("USER");
			if (user == NULL || *user == '\0')
				user = getenv("USERNAME");
			if (user == NULL || *user == '\0')
				user = "Hans Affe";
			_toServer.reset();
			_toServer.setField(_ID, _LOGIN);
			_toServer.setField("HOST", QHostInfo::localHostName());
			_toServer.setField("NAME", QString(user));
			qDebug() << "logging into manager, exam mode is " << clientApp->isExamMode();
			_toServer.setField(_EXAMMODE, clientApp->isExamMode() ? __TRUE : __FALSE);
			/* TODO: (Question) Why is this here not using sendMessage() ? */
			qDebug("Sending login request!");
			if (_toServer.writeMessage(_socket)) {
				_authed = 2;
				qDebug("valid, step <- 2");
			} else {
				this->disconnectFromServer();
			}
		}
		return;
	}

	if (_authed == 2) {
		if (id == _LOGIN) {
			qDebug("login accepted, step <- 3");
			_authed = 3;
			emit stateChange(ConnectWindow::Connected);
		}
		return;
	}

	// message THUMB - server requests screenshot as thumbnail
	if (id == _THUMB) {
		if (clientApp->isExamMode()) {
			QByteArray emptyArray;
			_toServer.setField(_ID, _THUMB);
			_toServer.setField(_IMG, emptyArray);
			sendMessage(_toServer);
			return;
		}
		int x = _fromServer.getFieldString(_X).toInt();
		int y = _fromServer.getFieldString(_Y).toInt();

		QRect primaryRect = QGuiApplication::primaryScreen()->geometry();
		// Limit requested size so it won't easily leak sensitive information
		if (x < 32) {
			x = 32;
		} else if (x > primaryRect.width() / 8) {
			x = primaryRect.width() / 8;
		}
		if (y < 18) {
			y = 18;
		} else if (y > primaryRect.height() / 8) {
			y = primaryRect.height() / 8;
		}

		QPixmap desktop(
			QGuiApplication::primaryScreen()->grabWindow(0,
				primaryRect.x(), primaryRect.y(), primaryRect.width(), primaryRect.height()
			).scaled(x, y, Qt::KeepAspectRatio, Qt::SmoothTransformation
		));
		QByteArray bytes;
		QBuffer jpgBuffer(&bytes);
		jpgBuffer.open(QIODevice::WriteOnly);
		if (desktop.save(&jpgBuffer, "JPG", _jpegQuality)) { // writes pixmap into bytes in JPG format
			// Try to adjust quality so we stay between 3 and 4.5 KB
			if (_jpegQuality < 90 && bytes.size() < 3000)
				_jpegQuality += 7;
			else if (_jpegQuality > 40 && bytes.size() > 4500)
				_jpegQuality -= 7;
		} else {
			// FALLBACK
			bytes.clear();
			QBuffer pngBuffer(&bytes);
			pngBuffer.open(QIODevice::WriteOnly);
			if (!desktop.save(&pngBuffer, "PNG")) { // writes pixmap into bytes in PNG format
				qDebug("Could not convert screenshot to PNG nor JPG");
				return; // Failed :-(
			}
		}
		_toServer.reset();
		_toServer.setField(_ID, _THUMB);
		_toServer.setField(_IMG, bytes);
		sendMessage(_toServer);
	} // message VNCSERVER - start local vncserver
	else if (id == _VNCSERVER) {
		if (clientApp->isExamMode()) {
			qDebug() << "denied request for vnc server (exam mode)";
			return;
		}
		const bool enable = (_fromServer.getFieldString("ENABLE").toInt() != 0);
		if (enable) {
			emit closeVnc(); // In case we are watching some other client, stop doing so
			VncServer::instance()->start();
		} else {
			VncServer::instance()->stop();
		}
	} else if (id == _VNCCLIENT) {
		if (clientApp->isExamMode()) {
			qDebug() << "denied request for vnc projection (exam mode)";
			return;
		}
		const QString host(_fromServer.getFieldString("HOST"));
		const int port = _fromServer.getFieldString("PORT").toInt();
		if (host.isEmpty() || port <= 0) {
			emit closeVnc();
		} else {
			emit openVnc(host, port, _fromServer.getFieldString("ROPASS"), true, true, _fromServer.getFieldString("CAPTION"), _fromServer.getFieldString("CLIENTID").toInt(), _fromServer.getFieldBytes(_THUMB));
		}
	} else if (id == _LOCK) {
		const bool enable = (_fromServer.getFieldString("ENABLE").toInt() != 0);
		if (enable && !clientApp->isConnectedToLocalManager()) {
			_blank->lock(_fromServer.getFieldString("MESSAGE"));
		} else {
			_blank->unlock();
		}
	} else if (id == _ATTENTION) {
		emit attentionChanged(_fromServer.getFieldString(_ENABLE) == __TRUE);
	}
}

void ServerConnection::checkLocalConnection()
{
	if (_socket == NULL) {
		return;
	}
	if (_socket->peerAddress() == _socket->localAddress()) {
		_isLocalConnection = 1;
	} else {
		_isLocalConnection = 0;
	}
}

/*
 * Override
 */

void ServerConnection::timerEvent(QTimerEvent *event)
{
	if (event->timerId() == _timerConnectionCheck) {
		if (_lastData < QDateTime::currentMSecsSinceEpoch()) {
			qDebug() << "Ping timeout";
			this->disconnectFromServer();
			killTimer(_timerConnectionCheck);
		}
	} else if (event->timerId() == _timerId) {
		qDebug() << "Connect timeout";
		killTimer(_timerId);
		_timerId = 0;
		this->disconnectFromServer();
	} else if (event->timerId() == _timerDelete) {
		if (_socket == NULL || _socket->state() == QAbstractSocket::UnconnectedState) {
			if (_socket != NULL)
				_socket->deleteLater();
			_socket = NULL;
			killTimer(_timerDelete);
			this->deleteLater();
			return;
		}
		_socket->abort();
		qDebug("A socket is still pending...");
	} else
		killTimer(event->timerId());
}

/*
 * Slots
 */

/**
 * This slot is triggered by the vnc server runner once the external VNC
 * server was succesfully started, or was terminated (either planned or
 * crashed).
 */
void ServerConnection::onVncServerStartStop(int port, QString& ropass, QString& rwpass)
{
	_toServer.reset();
	_toServer.setField(_ID, _VNCSERVER);
	if (port <= 0) {
		_toServer.setField("PORT", QByteArray("0"));
	} else {
		_toServer.setField("PORT", QString::number(port));
		_toServer.setField("ROPASS", ropass);
		_toServer.setField("RWPASS", rwpass);
	}
	sendMessage(_toServer);
}

/**
 * This slot is triggered once the internal VNC viewer has started or stopped
 * displaying a VNC stream. We'll inform the server about the state change.
 */
void ServerConnection::onVncViewerStartStop(const bool started, const int clientId)
{
	_toServer.reset();
	_toServer.setField(_ID, _VNCCLIENT);
	if (started)
		_toServer.setField("ENABLED", __TRUE);
	else
		_toServer.setField("ENABLED", __FALSE);
	_toServer.setField("CLIENTID", QString::number(clientId));
	sendMessage(_toServer);
}

/**
 * An ssl error happened. If it's an expected one, we ignore it
 * and keep going. Otherwise the connection will be terminated.
 */
void ServerConnection::sslErrors(const QList<QSslError> & errors)
{
	_socket->ignoreSslErrors();
	for (QList<QSslError>::const_iterator it = errors.begin(); it != errors.end(); it++) {
		const QSslError &err = *it;
		qDebug("Connect SSL: %s", qPrintable(err.errorString()));
		if (err.error() == QSslError::HostNameMismatch)
			continue; // We don't pay attention to hostnames for validation
		if (err.error() == QSslError::SelfSignedCertificate)
			continue; // Also, this will always be the case; we check the fingerprint later
		if (err.error() == QSslError::CertificateNotYetValid || err.error() == QSslError::CertificateExpired)
			continue;
		// Some other SSL error - do not ignore
		qDebug() << "FATAL!";
		_socket->ignoreSslErrors(QList<QSslError>());
		return;
	}
}

void ServerConnection::sock_dataArrival()
{
	if (_socket == NULL || _socket->state() != QAbstractSocket::ConnectedState) {
		qDebug("dataArrival called in bad state");
		return;
	}

	while (_socket->bytesAvailable() > 0) {
		int retval = _fromServer.readMessage(_socket); // let the message read data from socket
		if (retval == NM_READ_FAILED) { // error parsing msg, disconnect client!
			this->disconnectFromServer();
			return;
		}
		if (retval == NM_READ_INCOMPLETE)
			return;
		if (_fromServer.readComplete()) { // message is complete
			this->handleMsg();
			if (_socket == NULL)
				return;
			_fromServer.reset();
		}
	}
}

void ServerConnection::sock_closed()
{
	// should this be unreliable in some way i suggest using the signal "stateChanged()" instead
	// and check if the state changed to unconnected.
	qDebug("Socket was closed... oh well..");
	this->disconnectFromServer();
}

void ServerConnection::sock_error(QAbstractSocket::SocketError errcode)
{
	qDebug("Connection error: %d", (int)errcode);
	this->disconnectFromServer();
}

void ServerConnection::sock_connected()
{
	qDebug() << "Connection to server established and encrypted";
	QByteArray cert(_socket->peerCertificate().digest(QCryptographicHash::Sha1));
	if (_certHash != cert) {
		emit stateChange(ConnectWindow::InvalidCert);
		this->disconnectFromServer();
		return;
	}
	emit stateChange(ConnectWindow::AwaitingChallenge);
}