PDA

View Full Version : password problem with client and server



mate
19th July 2008, 10:27
hi,

a few days before, I have asked similar.
with the code below the client doesen't get the data with bad or good password.

the thread.cpp for the client:


#include "netthread.h"

NetThread::NetThread(const QString &hostName, quint16 port, QByteArray passw, QObject *parent) : QThread(parent), quit(false)
{
this->hostName = hostName;
this->port = port;
this->passw = passw;

QMutexLocker locker(&mutex);
}
void NetThread::run()
{
mutex.lock();
QString serverName = hostName;
quint16 serverPort = port;
mutex.unlock();
while(!quit)
{
const int Timeout = 5 * 1000;

qDebug() << serverName;
qDebug() << serverPort;

sslsocket = new QSslSocket;
sslsocket->connectToHostEncrypted(serverName, serverPort);
sslsocket->ignoreSslErrors();

if(!sslsocket->waitForEncrypted())
{
emit error(sslsocket->error(), sslsocket->errorString());
return;
}

sslsocket->write(passw);

qDebug() << sslsocket->bytesToWrite();
sslsocket->flush();

while (sslsocket->bytesAvailable() < (int)sizeof(quint16))
{
if (!sslsocket->waitForReadyRead(Timeout))
{
emit error(sslsocket->error(), sslsocket->errorString());
return;
}
}

quint16 blockSize;
QDataStream in(sslsocket);
in.setVersion(QDataStream::Qt_4_0);
in >> blockSize;

while(sslsocket->bytesAvailable() < blockSize)
{
if (!sslsocket->waitForReadyRead(Timeout))
{
emit error(sslsocket->error(), sslsocket->errorString());
return;
}
}
QMutexLocker locker(&mutex);

QByteArray logfile;
in >> logfile;
qDebug() << "NetThread: " << logfile.data();
emit newNet(logfile);

cond.wait(&mutex);
serverName = hostName;
serverPort = port;
}
}

and the thread.cpp for the server:


#include "thread.h"

SslThread::SslThread(int socketDescriptor, QString &log, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor)
{
logfile = new QFile(log);
pass_control = ":passwort";
security = false;
}
void SslThread::run()
{
serverSocket = new QSslSocket();
connect(serverSocket, SIGNAL(readyRead()), this, SLOT(lesen()));
while(true)
{
QByteArray pass("certificatpasswort");
serverSocket->setLocalCertificate("server.cert.crt");
serverSocket->setPrivateKey("privkey.pem",QSsl::Rsa,QSsl::Pem,pass);

if(serverSocket->setSocketDescriptor(socketDescriptor))
{
connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()));
serverSocket->startServerEncryption();
}
else
{
emit error(serverSocket->error());
delete serverSocket;
return;
}
qDebug() << serverSocket->peerAddress();

if(security)
{
qDebug() << "I'm inside";
if(!(logfile->open(QIODevice::ReadOnly | QIODevice::Text)))
{
qDebug() << "Lese Fehler!";
exit();
}
QByteArray log = logfile->readAll();

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << log.data();
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));


serverSocket->write(block);
qDebug() << serverSocket->bytesToWrite();
serverSocket->flush();
}

serverSocket->waitForDisconnected();
serverSocket->disconnectFromHost();
}
}
void SslThread::ready()
{
qDebug() << "Connection encrypted!";
}
char* SslThread::toChar(QString &string)
{
QByteArray ba = string.toLatin1();
char *c_str2 = ba.data();
return c_str2;
}
void SslThread::lesen()
{
QByteArray line = serverSocket->readAll();

//qDebug() << line;

if(line.startsWith(':'))
{
qDebug() << "password: " << line;
if(line != pass_control)
{
security = false;
//serverSocket->abort();
qDebug() << "password: " << line << "control: " << pass_control;
}
else
{
security = true;
}


}
//...
}

the server tells me
moon:/home/lard/Server# ./Server
QHostAddress( "127.0.0.1" )
Connection encrypted!
password: "passwort"
Password true

the server is never inside if(security) so he doesn't send the data.

and the client:
network operation timeout

i have tested much more versions of this, but i can't find a solution.
can someone please help?

mate

mate
19th July 2008, 19:20
ok, the problem was on the server-side. this works:


void SslThread::run()
{
serverSocket = new QSslSocket();
connect(serverSocket, SIGNAL(readyRead()), this, SLOT(read()));
while(true)
{
QByteArray pass("ssl-cert-password");
serverSocket->setLocalCertificate("server.cert.crt");
serverSocket->setPrivateKey("privkey.pem",QSsl::Rsa,QSsl::Pem,pass);

if(serverSocket->setSocketDescriptor(socketDescriptor))
{
connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()));
serverSocket->startServerEncryption();
}
else
{
emit error(serverSocket->error());
delete serverSocket;
return;
}
qDebug() << serverSocket->peerAddress();

serverSocket->waitForDisconnected();
serverSocket->disconnectFromHost();
}
}
...
void SslThread::read()
{
QByteArray line = serverSocket->readAll();

//qDebug() << line;

if(line.startsWith(':'))
{
qDebug() << "password: " << line;
if(line != pass_control)
{
serverSocket->abort();
qDebug() << "password: " << line << "control: " << pass_control;
}
else
{
if(!(logfile->open(QIODevice::ReadOnly | QIODevice::Text)))
{
qDebug() << "Error!";
exit();
}
QByteArray log = logfile->readAll();
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << log.data();
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));

qDebug() << serverSocket->bytesToWrite();
serverSocket->write(block);
serverSocket->flush();
qDebug() << serverSocket->bytesToWrite();
}
}
...