From 15253d1995ea49b114ef5c627e15b661dbd602b2 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Sun, 30 Sep 2012 19:41:17 +0200 Subject: add some trivial authentication --- distfoldd/agent.cc | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) (limited to 'distfoldd/agent.cc') diff --git a/distfoldd/agent.cc b/distfoldd/agent.cc index a718230..8baa85b 100644 --- a/distfoldd/agent.cc +++ b/distfoldd/agent.cc @@ -1,3 +1,5 @@ +#include + #include "agent.h" #ifdef Q_OS_UNIX @@ -5,8 +7,8 @@ #include #endif -Agent::Agent(QSslSocket *socket, const QDir& dir, SyncFlags flags, QObject *parent) : - QObject(parent), _dir(dir), _subPath("/"), _flags(flags), _socket(socket) +Agent::Agent(QSslSocket *socket, const QDir& dir, const QString& passwd, SyncFlags flags, QObject *parent) : + QObject(parent), _dir(dir), _subPath("/"), _passwd(passwd), _flags(flags), _socket(socket) { connect(_socket, SIGNAL(readyRead()), SLOT(handleDataAvailable())); connect(_socket, SIGNAL(sslErrors(QList)), SLOT(handleSslErrors(QList))); @@ -73,7 +75,7 @@ bool Agent::equalDateTime(const QDateTime& dt1, const QDateTime& dt2) void Agent::setLocalFileDateTime(const QString &path, const QDateTime &dt) { -#ifdef Q_OS_UNIX +#if defined(Q_OS_UNIX) const char *filename = path.toLocal8Bit().constData(); struct utimbuf times; times.actime = dt.toTime_t(); @@ -82,6 +84,8 @@ void Agent::setLocalFileDateTime(const QString &path, const QDateTime &dt) if (rc != 0) { qWarning() << "Could not set local mtime of" << path; } +#elif defined(Q_OS_WIN32) + // TODO SetFileAttributes, SetFileTime #endif } @@ -135,6 +139,61 @@ bool Agent::morePathDepthThan(const QString& s1, const QString& s2) return d1 > d2; } +QByteArray Agent::encodeHelloMessage(const QByteArray& client_challenge) +{ + QByteArray ba(sizeof(quint32), '\0'); + quint32 *p = reinterpret_cast(ba.data()); + *p = PROTO_CURRENT; + ba.append(client_challenge); + return ba; +} + +Agent::ProtoVersion Agent::decodeHelloMessage(const QByteArray& ba, QByteArray *client_challenge) +{ + if (ba.size() < static_cast(sizeof(quint32))) return PROTO_BAD; + const quint32 *p = reinterpret_cast(ba.constData()); + + switch (*p) { + case PROTO_1: + *client_challenge = ba.mid(sizeof(quint32)); + return PROTO_1; + default: + return PROTO_BAD; + } +} + +QByteArray Agent::hmacSha1(const QByteArray& key, const QByteArray& message) +{ + QCA::MessageAuthenticationCode mac("hmac(sha1)", QCA::SymmetricKey(key)); + mac.update(message); + return mac.final().toByteArray(); +} + +QByteArray Agent::generateChallenge() +{ + const int challenge_size = 10; + return QCA::Random::randomArray(challenge_size).toByteArray(); +} + +QByteArray Agent::generateChallengeResponse(const QByteArray& server_challenge, const QByteArray& client_challenge) +{ + return hmacSha1(_passwd.toUtf8(), server_challenge + client_challenge); +} + +QByteArray Agent::encodeAuthReply(AuthResult result) +{ + QByteArray ba(sizeof(quint8), static_cast(result)); + return ba; +} + +Agent::AuthResult Agent::decodeAuthReply(const QByteArray& ba) +{ + if (ba.size() != 1) return AUTH_FAILED; + const quint8 *p = reinterpret_cast(ba.constData()); + + return static_cast(*p); +} + QFileInfoList Agent::scanFiles(const QDir& dir) { QFileInfoList all; -- cgit v1.2.3