summaryrefslogblamecommitdiffstats
path: root/src/userldapdata.cpp
blob: 4382800b872f5037d13121240b6e61f39c790036 (plain) (tree)













































































                                                                                                   
#include "userldapdata.h"

#include <QSet>
#include <QFile>
#include <QDebug>
#include <QDir>

#define INCBREAK if (++i >= len) break

namespace {

QSet<QString> _entries;

}

namespace UserLdapData {

bool init(QString inputFile)
{
    _entries.clear();
    if (inputFile.isEmpty()) {
        inputFile = QDir::homePath() + "/.openslx/ldap";
    }
    QFile file(inputFile);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "Cannot read" << file.fileName();
        return false;
    }
    while (!file.atEnd()) {
        QByteArray ba = file.readLine();
        const char *p = ba.constData();
        const int len = ba.size();
        bool b64 = false;
        int dataStart = -1;
        int keyEnd = -1;
        for (int i = 1; i < len; ++i) {
            if (p[i] != ':')
                continue;
            keyEnd = i;
            INCBREAK;
            if (p[i] == ':') {
                INCBREAK;
                b64 = true;
            }
            if (p[i] != ' ')
                continue;
            dataStart = i + 1;
            break;
        }
        if (dataStart == -1)
            continue;
        int dataLen = len - dataStart;
        while (dataLen > 0 && p[dataStart + dataLen - 1] == '\n') {
            dataLen--;
        }
        QString value = QString::fromUtf8(p, keyEnd).toLower() + ":";
        if (b64) {
            value += QString::fromUtf8(QByteArray::fromBase64(QByteArray(p + dataStart, dataLen)));
        } else {
            value += QString::fromUtf8(p + dataStart, dataLen);
        }
        _entries.insert(value);
    }
    return true;
}

bool isEmpty()
{
    return _entries.isEmpty();
}

bool isAllowed(const QString& attribute, const QString& value)
{
    const QString keyLow(attribute.toLower() + ":" + value);
    return _entries.contains(keyLow);
}

}