diff options
author | Javier S. Pedro <maemo@javispedro.com> | 2012-09-30 19:41:17 +0200 |
---|---|---|
committer | Javier S. Pedro <maemo@javispedro.com> | 2012-09-30 19:41:17 +0200 |
commit | 15253d1995ea49b114ef5c627e15b661dbd602b2 (patch) | |
tree | 1111441406a0b043f45ab19fe42e567be36d4d09 /distfoldd/agent.cc | |
parent | c8f89279026af6bb03ef3d66e44e859fb7208925 (diff) | |
download | distfold-15253d1995ea49b114ef5c627e15b661dbd602b2.tar.gz distfold-15253d1995ea49b114ef5c627e15b661dbd602b2.zip |
add some trivial authentication
Diffstat (limited to 'distfoldd/agent.cc')
-rw-r--r-- | distfoldd/agent.cc | 65 |
1 files changed, 62 insertions, 3 deletions
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 <QtCrypto> + #include "agent.h" #ifdef Q_OS_UNIX @@ -5,8 +7,8 @@ #include <utime.h> #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<QSslError>)), SLOT(handleSslErrors(QList<QSslError>))); @@ -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<quint32*>(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<int>(sizeof(quint32))) return PROTO_BAD; + const quint32 *p = reinterpret_cast<const quint32*>(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<char>(result)); + return ba; +} + +Agent::AuthResult Agent::decodeAuthReply(const QByteArray& ba) +{ + if (ba.size() != 1) return AUTH_FAILED; + const quint8 *p = reinterpret_cast<const quint8*>(ba.constData()); + + return static_cast<AuthResult>(*p); +} + QFileInfoList Agent::scanFiles(const QDir& dir) { QFileInfoList all; |