summaryrefslogtreecommitdiffstats
path: root/src/userldapdata.cpp
blob: 4382800b872f5037d13121240b6e61f39c790036 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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);
}

}