Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CConnection: Modify UserPasswdGetter and UserMsgBox interface to CCon…
Browse files Browse the repository at this point in the history
…nection

Problems with the original code: A process can only establish one connection.
After modification, multiple connections can be supported.
KangLin committed Aug 20, 2024
1 parent 445e023 commit 63171c7
Showing 21 changed files with 106 additions and 151 deletions.
29 changes: 28 additions & 1 deletion common/rfb/CConnection.h
Original file line number Diff line number Diff line change
@@ -36,6 +36,18 @@ namespace rfb {
class CMsgWriter;
class CSecurity;

enum class MsgBoxFlags{
M_OK = 0,
M_OKCANCEL = 1,
M_YESNO = 4,
M_ICONERROR = 0x10,
M_ICONQUESTION = 0x20,
M_ICONWARNING = 0x30,
M_ICONINFORMATION = 0x40,
M_DEFBUTTON1 = 0,
M_DEFBUTTON2 = 0x100
};

class CConnection : public CMsgHandler {
public:

@@ -111,7 +123,7 @@ namespace rfb {
void serverCutText(const char* str) override;

void handleClipboardCaps(uint32_t flags,
const uint32_t* lengths) override;
const uint32_t* lengths) override;
void handleClipboardRequest(uint32_t flags) override;
void handleClipboardPeek() override;
void handleClipboardNotify(uint32_t flags) override;
@@ -121,6 +133,21 @@ namespace rfb {

// Methods to be overridden in a derived class

//
// \brief getUserPasswd gets the username and password.
// This might involve a dialog, getpass(), etc.
// \param secure: Indicates whether this connection is secure
// \param user: user name buffer pointer. the pointer may be nullptr,
// in which case no user name will be retrieved.
// \param password: password buffer pointer.
//
virtual void getUserPasswd(bool secure,
std::string* user,
std::string* password) = 0;
virtual bool showMsgBox(MsgBoxFlags flags,
const char *title,
const char *text) = 0;

// authSuccess() is called when authentication has succeeded.
virtual void authSuccess();

10 changes: 0 additions & 10 deletions common/rfb/CSecurity.h
Original file line number Diff line number Diff line change
@@ -38,9 +38,6 @@
#ifndef __RFB_CSECURITY_H__
#define __RFB_CSECURITY_H__

#include <rfb/UserPasswdGetter.h>
#include <rfb/UserMsgBox.h>

namespace rfb {
class CConnection;
class CSecurity {
@@ -51,13 +48,6 @@ namespace rfb {
virtual int getType() const = 0;
virtual bool isSecure() const { return false; }

/*
* Use variable directly instead of dumb get/set methods.
* It MUST be set by viewer.
*/
static UserPasswdGetter *upg;
static UserMsgBox *msg;

protected:
CConnection* cc;
};
2 changes: 1 addition & 1 deletion common/rfb/CSecurityDH.cxx
Original file line number Diff line number Diff line change
@@ -108,7 +108,7 @@ void CSecurityDH::writeCredentials()
std::string password;
rdr::RandomStream rs;

(CSecurity::upg)->getUserPasswd(isSecure(), &username, &password);
cc->getUserPasswd(isSecure(), &username, &password);

std::vector<uint8_t> bBytes(keyLength);
if (!rs.hasData(keyLength))
2 changes: 1 addition & 1 deletion common/rfb/CSecurityMSLogonII.cxx
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@ void CSecurityMSLogonII::writeCredentials()
std::string password;
rdr::RandomStream rs;

(CSecurity::upg)->getUserPasswd(isSecure(), &username, &password);
cc->getUserPasswd(isSecure(), &username, &password);

std::vector<uint8_t> bBytes(8);
if (!rs.hasData(8))
3 changes: 1 addition & 2 deletions common/rfb/CSecurityPlain.cxx
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@

#include <rfb/CConnection.h>
#include <rfb/CSecurityPlain.h>
#include <rfb/UserPasswdGetter.h>

#include <rdr/OutStream.h>

@@ -36,7 +35,7 @@ bool CSecurityPlain::processMsg()
std::string username;
std::string password;

(CSecurity::upg)->getUserPasswd(cc->isSecure(), &username, &password);
cc->getUserPasswd(cc->isSecure(), &username, &password);

// Return the response to the server
os->writeU32(username.size());
7 changes: 3 additions & 4 deletions common/rfb/CSecurityRSAAES.cxx
Original file line number Diff line number Diff line change
@@ -38,7 +38,6 @@
#include <rfb/CConnection.h>
#include <rfb/LogWriter.h>
#include <rfb/Exception.h>
#include <rfb/UserMsgBox.h>
#include <rfb/util.h>
#include <rdr/AESInStream.h>
#include <rdr/AESOutStream.h>
@@ -215,7 +214,7 @@ void CSecurityRSAAES::verifyServer()
"Fingerprint: %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\n"
"Please verify that the information is correct and press \"Yes\". "
"Otherwise press \"No\"", f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7]);
if (!msg->showMsgBox(UserMsgBox::M_YESNO, title, text.c_str()))
if (!cc->showMsgBox(MsgBoxFlags::M_YESNO, title, text.c_str()))
throw Exception("server key mismatch");
}

@@ -438,9 +437,9 @@ void CSecurityRSAAES::writeCredentials()
std::string password;

if (subtype == secTypeRA2UserPass)
(CSecurity::upg)->getUserPasswd(isSecure(), &username, &password);
cc->getUserPasswd(isSecure(), &username, &password);
else
(CSecurity::upg)->getUserPasswd(isSecure(), nullptr, &password);
cc->getUserPasswd(isSecure(), nullptr, &password);

if (subtype == secTypeRA2UserPass) {
if (username.size() > 255)
2 changes: 0 additions & 2 deletions common/rfb/CSecurityRSAAES.h
Original file line number Diff line number Diff line change
@@ -27,13 +27,11 @@
#include <nettle/rsa.h>
#include <rfb/CSecurity.h>
#include <rfb/Security.h>
#include <rfb/UserMsgBox.h>
#include <rdr/InStream.h>
#include <rdr/OutStream.h>
#include <rdr/RandomStream.h>

namespace rfb {
class UserMsgBox;
class CSecurityRSAAES : public CSecurity {
public:
CSecurityRSAAES(CConnection* cc, uint32_t secType,
22 changes: 10 additions & 12 deletions common/rfb/CSecurityTLS.cxx
Original file line number Diff line number Diff line change
@@ -38,7 +38,6 @@
#include <rfb/CConnection.h>
#include <rfb/LogWriter.h>
#include <rfb/Exception.h>
#include <rfb/UserMsgBox.h>
#include <rfb/util.h>
#include <rdr/TLSException.h>
#include <rdr/TLSInStream.h>
@@ -442,7 +441,7 @@ void CSecurityTLS::checkSession()
"Do you want to make an exception for this "
"server?", info.data);

if (!msg->showMsgBox(UserMsgBox::M_YESNO,
if (!cc->showMsgBox(MsgBoxFlags::M_YESNO,
"Unknown certificate issuer",
text.c_str()))
throw AuthCancelledException();
@@ -462,8 +461,7 @@ void CSecurityTLS::checkSession()
"\n"
"Do you want to make an exception for this "
"server?", info.data);

if (!msg->showMsgBox(UserMsgBox::M_YESNO,
if (!cc->showMsgBox(MsgBoxFlags::M_YESNO,
"Certificate is not yet valid",
text.c_str()))
throw AuthCancelledException();
@@ -482,7 +480,7 @@ void CSecurityTLS::checkSession()
"Do you want to make an exception for this "
"server?", info.data);

if (!msg->showMsgBox(UserMsgBox::M_YESNO,
if (!cc->showMsgBox(MsgBoxFlags::M_YESNO,
"Expired certificate",
text.c_str()))
throw AuthCancelledException();
@@ -501,7 +499,7 @@ void CSecurityTLS::checkSession()
"Do you want to make an exception for this "
"server?", info.data);

if (!msg->showMsgBox(UserMsgBox::M_YESNO,
if (!cc->showMsgBox(MsgBoxFlags::M_YESNO,
"Insecure certificate algorithm",
text.c_str()))
throw AuthCancelledException();
@@ -526,7 +524,7 @@ void CSecurityTLS::checkSession()
"Do you want to make an exception for this "
"server?", client->getServerName(), info.data);

if (!msg->showMsgBox(UserMsgBox::M_YESNO,
if (!cc->showMsgBox(MsgBoxFlags::M_YESNO,
"Certificate hostname mismatch",
text.c_str()))
throw AuthCancelledException();
@@ -552,7 +550,7 @@ void CSecurityTLS::checkSession()
"Do you want to make an exception for this "
"server?", info.data);

if (!msg->showMsgBox(UserMsgBox::M_YESNO,
if (!cc->showMsgBox(MsgBoxFlags::M_YESNO,
"Unexpected server certificate",
text.c_str()))
throw AuthCancelledException();
@@ -575,7 +573,7 @@ void CSecurityTLS::checkSession()
"Do you want to make an exception for this "
"server?", info.data);

if (!msg->showMsgBox(UserMsgBox::M_YESNO,
if (!cc->showMsgBox(MsgBoxFlags::M_YESNO,
"Unexpected server certificate",
text.c_str()))
throw AuthCancelledException();
@@ -596,7 +594,7 @@ void CSecurityTLS::checkSession()
"Do you want to make an exception for this "
"server?", info.data);

if (!msg->showMsgBox(UserMsgBox::M_YESNO,
if (!cc->showMsgBox(MsgBoxFlags::M_YESNO,
"Unexpected server certificate",
text.c_str()))
throw AuthCancelledException();
@@ -617,7 +615,7 @@ void CSecurityTLS::checkSession()
"Do you want to make an exception for this "
"server?", info.data);

if (!msg->showMsgBox(UserMsgBox::M_YESNO,
if (!cc->showMsgBox(MsgBoxFlags::M_YESNO,
"Unexpected server certificate",
text.c_str()))
throw AuthCancelledException();
@@ -644,7 +642,7 @@ void CSecurityTLS::checkSession()
"Do you want to make an exception for this "
"server?", client->getServerName(), info.data);

if (!msg->showMsgBox(UserMsgBox::M_YESNO,
if (!cc->showMsgBox(MsgBoxFlags::M_YESNO,
"Unexpected server certificate",
text.c_str()))
throw AuthCancelledException();
1 change: 0 additions & 1 deletion common/rfb/CSecurityTLS.h
Original file line number Diff line number Diff line change
@@ -28,7 +28,6 @@

#include <rfb/CSecurity.h>
#include <rfb/Security.h>
#include <rfb/UserMsgBox.h>
#include <rdr/InStream.h>
#include <rdr/OutStream.h>
#include <gnutls/gnutls.h>
2 changes: 1 addition & 1 deletion common/rfb/CSecurityVncAuth.cxx
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ bool CSecurityVncAuth::processMsg()
uint8_t challenge[vncAuthChallengeSize];
is->readBytes(challenge, vncAuthChallengeSize);
std::string passwd;
(CSecurity::upg)->getUserPasswd(cc->isSecure(), nullptr, &passwd);
cc->getUserPasswd(cc->isSecure(), nullptr, &passwd);

// Calculate the correct response
uint8_t key[8];
10 changes: 0 additions & 10 deletions common/rfb/SecurityClient.cxx
Original file line number Diff line number Diff line change
@@ -40,11 +40,6 @@

using namespace rfb;

UserPasswdGetter *CSecurity::upg = nullptr;
#if defined(HAVE_GNUTLS) || defined(HAVE_NETTLE)
UserMsgBox *CSecurity::msg = nullptr;
#endif

StringParameter SecurityClient::secTypes
("SecurityTypes",
"Specify which security scheme to use (None, VncAuth, Plain"
@@ -66,11 +61,6 @@ ConfViewer);

CSecurity* SecurityClient::GetCSecurity(CConnection* cc, uint32_t secType)
{
assert (CSecurity::upg != nullptr); /* (upg == nullptr) means bug in the viewer */
#if defined(HAVE_GNUTLS) || defined(HAVE_NETTLE)
assert (CSecurity::msg != nullptr);
#endif

if (!IsSupported(secType))
goto bail;

41 changes: 0 additions & 41 deletions common/rfb/UserMsgBox.h

This file was deleted.

36 changes: 0 additions & 36 deletions common/rfb/UserPasswdGetter.h

This file was deleted.

12 changes: 12 additions & 0 deletions tests/perf/decperf.cxx
Original file line number Diff line number Diff line change
@@ -75,13 +75,16 @@ class CConn : public rfb::CConnection {
void setColourMapEntries(int, int, uint16_t*) override;
void bell() override;
void serverCutText(const char*) override;
virtual void getUserPasswd(bool secure, std::string *user, std::string *password) override;
virtual bool showMsgBox(rfb::MsgBoxFlags flags, const char *title, const char *text) override;

public:
double cpuTime;

protected:
rdr::FileInStream *in;
DummyOutStream *out;

};

DummyOutStream::DummyOutStream()
@@ -174,6 +177,15 @@ void CConn::serverCutText(const char*)
{
}

void CConn::getUserPasswd(bool, std::string *, std::string *)
{
}

bool CConn::showMsgBox(rfb::MsgBoxFlags, const char *, const char *)
{
return true;
}

struct stats
{
double decodeTime;
11 changes: 11 additions & 0 deletions tests/perf/encperf.cxx
Original file line number Diff line number Diff line change
@@ -108,6 +108,8 @@ class CConn : public rfb::CConnection {
void setColourMapEntries(int, int, uint16_t*) override;
void bell() override;
void serverCutText(const char*) override;
virtual void getUserPasswd(bool secure, std::string *user, std::string *password) override;
virtual bool showMsgBox(rfb::MsgBoxFlags flags, const char *title, const char *text) override;

public:
double decodeTime;
@@ -279,6 +281,15 @@ void CConn::serverCutText(const char*)
{
}

void CConn::getUserPasswd(bool, std::string *, std::string *)
{
}

bool CConn::showMsgBox(rfb::MsgBoxFlags, const char *, const char *)
{
return true;
}

Manager::Manager(class rfb::SConnection *conn_) :
EncodeManager(conn_)
{
17 changes: 16 additions & 1 deletion vncviewer/CConn.cxx
Original file line number Diff line number Diff line change
@@ -275,7 +275,7 @@ void CConn::socketEvent(FL_SOCKET fd, void *data)
vlog.info("%s", e.str());
disconnect();
} catch (rfb::AuthFailureException& e) {
reset_password_data();
cc->resetPassword();
vlog.error(_("Authentication failed: %s"), e.str());
abort_connection(_("Failed to authenticate with the server. Reason "
"given by the server:\n\n%s"), e.str());
@@ -606,3 +606,18 @@ void CConn::handleUpdateTimeout(void *data)

Fl::repeat_timeout(1.0, handleUpdateTimeout, data);
}

bool CConn::showMsgBox(MsgBoxFlags flags, const char *title, const char *text)
{
return dlg.showMsgBox(flags, title, text);
}

void CConn::getUserPasswd(bool secure, std::string *user, std::string *password)
{
dlg.getUserPasswd(secure, user, password);
}

void CConn::resetPassword()
{
dlg.resetPassword();
}
10 changes: 10 additions & 0 deletions vncviewer/CConn.h
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@

#include <rfb/CConnection.h>
#include <rdr/FdInStream.h>
#include "UserDialog.h"

namespace network { class Socket; }

@@ -44,6 +45,11 @@ class CConn : public rfb::CConnection
// Callback when socket is ready (or broken)
static void socketEvent(FL_SOCKET fd, void *data);

// UserMsgBox interface
virtual bool showMsgBox(rfb::MsgBoxFlags flags, const char *title, const char *text) override;
// UserPasswdGetter interface
virtual void getUserPasswd(bool secure, std::string *user, std::string *password) override;

// CConnection callback methods
void initDone() override;

@@ -76,6 +82,8 @@ class CConn : public rfb::CConnection
void handleClipboardAnnounce(bool available) override;
void handleClipboardData(const char* data) override;

void resetPassword();

private:

void resizeFramebuffer() override;
@@ -105,6 +113,8 @@ class CConn : public rfb::CConnection
struct timeval updateStartTime;
size_t updateStartPos;
unsigned long long bpsEstimate;

UserDialog dlg;
};

#endif
14 changes: 7 additions & 7 deletions vncviewer/UserDialog.cxx
Original file line number Diff line number Diff line change
@@ -248,7 +248,7 @@ void UserDialog::getUserPasswd(bool secure_, std::string* user,
throw rfb::AuthCancelledException();
}

bool UserDialog::showMsgBox(int flags, const char* title, const char* text)
bool UserDialog::showMsgBox(MsgBoxFlags flags, const char* title, const char* text)
{
char buffer[1024];

@@ -260,15 +260,15 @@ bool UserDialog::showMsgBox(int flags, const char* title, const char* text)

fl_message_title(title);

switch (flags & 0xf) {
case M_OKCANCEL:
switch ((MsgBoxFlags)((int)flags & 0xf)) {
case MsgBoxFlags::M_OKCANCEL:
return fl_choice("%s", nullptr, fl_ok, fl_cancel, buffer) == 1;
case M_YESNO:
case MsgBoxFlags::M_YESNO:
return fl_choice("%s", nullptr, fl_yes, fl_no, buffer) == 1;
case M_OK:
case MsgBoxFlags::M_OK:
default:
if (((flags & 0xf0) == M_ICONERROR) ||
((flags & 0xf0) == M_ICONWARNING))
if ((((int)flags & 0xf0) == (int)MsgBoxFlags::M_ICONERROR) ||
(((int)flags & 0xf0) == (int)MsgBoxFlags::M_ICONWARNING))
fl_alert("%s", buffer);
else
fl_message("%s", buffer);
13 changes: 5 additions & 8 deletions vncviewer/UserDialog.h
Original file line number Diff line number Diff line change
@@ -19,11 +19,9 @@
#ifndef __USERDIALOG_H__
#define __USERDIALOG_H__

#include <rfb/UserPasswdGetter.h>
#include <rfb/UserMsgBox.h>
#include <rfb/CConnection.h>

class UserDialog : public rfb::UserPasswdGetter,
public rfb::UserMsgBox
class UserDialog
{
public:
UserDialog();
@@ -32,15 +30,14 @@ class UserDialog : public rfb::UserPasswdGetter,
// UserPasswdGetter callbacks

void getUserPasswd(bool secure, std::string* user,
std::string* password) override;
std::string* password);

// UserMsgBox callbacks

bool showMsgBox(int flags, const char* title, const char* text) override;
bool showMsgBox(rfb::MsgBoxFlags flags, const char* title, const char* text);

void resetPassword();

private:
private:
std::string savedUsername;
std::string savedPassword;
};
12 changes: 0 additions & 12 deletions vncviewer/vncviewer.cxx
Original file line number Diff line number Diff line change
@@ -93,8 +93,6 @@ static bool exitMainloop = false;
static char *exitError = nullptr;
static bool fatalError = false;

static UserDialog dlg;

static const char *about_text()
{
static char buffer[1024];
@@ -171,11 +169,6 @@ bool should_disconnect()
return exitMainloop;
}

void reset_password_data()
{
dlg.resetPassword();
}

void about_vncviewer()
{
fl_message_title(_("About TigerVNC Viewer"));
@@ -748,11 +741,6 @@ int main(int argc, char** argv)
vlog.error(_("Could not create VNC state directory: %s"), strerror(errno));
}

CSecurity::upg = &dlg;
#if defined(HAVE_GNUTLS) || defined(HAVE_NETTLE)
CSecurity::msg = &dlg;
#endif

Socket *sock = nullptr;

#ifndef WIN32
1 change: 0 additions & 1 deletion vncviewer/vncviewer.h
Original file line number Diff line number Diff line change
@@ -33,7 +33,6 @@ void abort_connection_with_unexpected_error(const rdr::Exception &);

void disconnect();
bool should_disconnect();
void reset_password_data();

void about_vncviewer();

0 comments on commit 63171c7

Please sign in to comment.