ldapclient.cpp 8.06 KB
Newer Older
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
/* kldapclient.cpp - LDAP access
 *      Copyright (C) 2002 Klarälvdalens Datakonsult AB
 *
 *      Author: Steffen Hansen <hansen@kde.org>
 *
 *      Ported to KABC by Daniel Molkentin <molkentin@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "ldapclient.h"
#include "ldapclient_debug.h"

#include <kldap/ldapobject.h>
#include <kldap/ldapserver.h>
#include <kldap/ldapurl.h>
#include <kldap/ldif.h>

#include <kio/job.h>

#include <QPointer>

using namespace KLDAP;

class Q_DECL_HIDDEN LdapClient::Private
{
public:
    Private(LdapClient *qq)
Laurent Montel's avatar
Laurent Montel committed
42
        : q(qq)
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    {
    }

    ~Private()
    {
        q->cancelQuery();
    }

    void startParseLDIF();
    void parseLDIF(const QByteArray &data);
    void endParseLDIF();
    void finishCurrentObject();

    void slotData(KIO::Job *, const QByteArray &data);
    void slotInfoMessage(KJob *, const QString &info, const QString &);
    void slotDone();

Laurent Montel's avatar
Laurent Montel committed
60
    LdapClient *q = nullptr;
61
62
63
64
65

    KLDAP::LdapServer mServer;
    QString mScope;
    QStringList mAttrs;

Laurent Montel's avatar
Laurent Montel committed
66
    QPointer<KJob> mJob = nullptr;
Laurent Montel's avatar
Laurent Montel committed
67
    bool mActive = false;
68
69
70

    KLDAP::LdapObject mCurrentObject;
    KLDAP::Ldif mLdif;
Laurent Montel's avatar
Laurent Montel committed
71
72
    int mClientNumber = 0;
    int mCompletionWeight = 0;
73
74
75
};

LdapClient::LdapClient(int clientNumber, QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
76
77
    : QObject(parent)
    , d(new Private(this))
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{
    d->mClientNumber = clientNumber;
    d->mCompletionWeight = 50 - d->mClientNumber;
}

LdapClient::~LdapClient()
{
    delete d;
}

bool LdapClient::isActive() const
{
    return d->mActive;
}

void LdapClient::setServer(const KLDAP::LdapServer &server)
{
    d->mServer = server;
}

const KLDAP::LdapServer LdapClient::server() const
{
    return d->mServer;
}

void LdapClient::setAttributes(const QStringList &attrs)
{
    d->mAttrs = attrs;
    d->mAttrs << QStringLiteral("objectClass"); // via objectClass we detect distribution lists
}

QStringList LdapClient::attributes() const
{
    return d->mAttrs;
}

void LdapClient::setScope(const QString &scope)
{
    d->mScope = scope;
}

void LdapClient::startQuery(const QString &filter)
{
    cancelQuery();
    KLDAP::LdapUrl url;

    url = d->mServer.url();

    url.setAttributes(d->mAttrs);
    url.setScope(d->mScope == QLatin1String("one") ? KLDAP::LdapUrl::One : KLDAP::LdapUrl::Sub);
    const QString userFilter = url.filter();
    QString finalFilter = filter;
    // combine the filter set by the user in the config dialog (url.filter()) and the filter from this query
    if (!userFilter.isEmpty()) {
        finalFilter = QLatin1String("&(") + finalFilter + QLatin1String(")(") + userFilter + QLatin1Char(')');
    }
    url.setFilter(QLatin1Char('(') + finalFilter + QLatin1Char(')'));

    qCDebug(LDAPCLIENT_LOG) << "LdapClient: Doing query:" << url.toDisplayString();

    d->startParseLDIF();
    d->mActive = true;
Laurent Montel's avatar
Laurent Montel committed
140
141
    KIO::TransferJob *transfertJob = KIO::get(url, KIO::NoReload, KIO::HideProgressInfo);
    d->mJob = transfertJob;
Laurent Montel's avatar
Laurent Montel committed
142
143
144
    connect(transfertJob, &KIO::TransferJob::data, this, [this](KIO::Job *job, const QByteArray &data) {
        d->slotData(job, data);
    });
Laurent Montel's avatar
Laurent Montel committed
145
146
147
148
    connect(d->mJob.data(), &KIO::TransferJob::infoMessage, this,
            [this](KJob *job, const QString &str, const QString &val) {
        d->slotInfoMessage(job, str, val);
    });
Laurent Montel's avatar
Laurent Montel committed
149
    connect(d->mJob.data(), &KIO::TransferJob::result,
Laurent Montel's avatar
Laurent Montel committed
150
151
152
            this, [this]() {
        d->slotDone();
    });
153
154
155
156
157
158
}

void LdapClient::cancelQuery()
{
    if (d->mJob) {
        d->mJob->kill();
Laurent Montel's avatar
Laurent Montel committed
159
        d->mJob = nullptr;
160
161
162
163
164
165
166
167
168
169
    }

    d->mActive = false;
}

void LdapClient::Private::slotData(KIO::Job *, const QByteArray &data)
{
    parseLDIF(data);
}

Laurent Montel's avatar
Laurent Montel committed
170
void LdapClient::Private::slotInfoMessage(KJob *, const QString &info, const QString &)
171
{
Laurent Montel's avatar
Laurent Montel committed
172
    qCDebug(LDAPCLIENT_LOG) << "Job said :" << info;
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
}

void LdapClient::Private::slotDone()
{
    endParseLDIF();
    mActive = false;
    if (!mJob) {
        return;
    }
    int err = mJob->error();
    if (err && err != KIO::ERR_USER_CANCELED) {
        Q_EMIT q->error(mJob->errorString());
    }
    Q_EMIT q->done();
}

void LdapClient::Private::startParseLDIF()
{
    mCurrentObject.clear();
    mLdif.startParsing();
}

void LdapClient::Private::endParseLDIF()
{
}

void LdapClient::Private::finishCurrentObject()
{
    mCurrentObject.setDn(mLdif.dn());
    KLDAP::LdapAttrValue objectclasses;
Laurent Montel's avatar
Laurent Montel committed
203
    const KLDAP::LdapAttrMap::ConstIterator end = mCurrentObject.attributes().constEnd();
204
    for (KLDAP::LdapAttrMap::ConstIterator it = mCurrentObject.attributes().constBegin();
Laurent Montel's avatar
Laurent Montel committed
205
         it != end; ++it) {
206
207
208
209
210
211
212
        if (it.key().toLower() == QLatin1String("objectclass")) {
            objectclasses = it.value();
            break;
        }
    }

    bool groupofnames = false;
Laurent Montel's avatar
Laurent Montel committed
213
    const KLDAP::LdapAttrValue::ConstIterator endValue(objectclasses.constEnd());
214
    for (KLDAP::LdapAttrValue::ConstIterator it = objectclasses.constBegin();
Laurent Montel's avatar
Laurent Montel committed
215
         it != endValue; ++it) {
216
217
218
219
220
221
222
223
224
225
226
227
        const QByteArray sClass = (*it).toLower();
        if (sClass == "groupofnames" || sClass == "kolabgroupofnames") {
            groupofnames = true;
        }
    }

    if (groupofnames) {
        KLDAP::LdapAttrMap::ConstIterator it = mCurrentObject.attributes().find(QStringLiteral("mail"));
        if (it == mCurrentObject.attributes().end()) {
            // No explicit mail address found so far?
            // Fine, then we use the address stored in the DN.
            QString sMail;
228
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
229
            const QStringList lMail = mCurrentObject.dn().toString().split(QStringLiteral(",dc="), QString::SkipEmptyParts);
230
231
232
#else
            const QStringList lMail = mCurrentObject.dn().toString().split(QStringLiteral(",dc="), Qt::SkipEmptyParts);
#endif
233
234
            const int n = lMail.count();
            if (n) {
235
                if (lMail.first().startsWith(QLatin1String("cn="), Qt::CaseInsensitive)) {
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
                    sMail = lMail.first().simplified().mid(3);
                    if (1 < n) {
                        sMail.append(QLatin1Char('@'));
                    }
                    for (int i = 1; i < n; ++i) {
                        sMail.append(lMail.at(i));
                        if (i < n - 1) {
                            sMail.append(QLatin1Char('.'));
                        }
                    }
                    mCurrentObject.addValue(QStringLiteral("mail"), sMail.toUtf8());
                }
            }
        }
    }
    Q_EMIT q->result(*q, mCurrentObject);
    mCurrentObject.clear();
}

void LdapClient::Private::parseLDIF(const QByteArray &data)
{
    //qCDebug(LDAPCLIENT_LOG) <<"LdapClient::parseLDIF(" << QCString(data.data(), data.size()+1) <<" )";
Laurent Montel's avatar
Laurent Montel committed
258
    if (!data.isEmpty()) {
259
260
261
262
263
264
265
266
267
        mLdif.setLdif(data);
    } else {
        mLdif.endLdif();
    }
    KLDAP::Ldif::ParseValue ret;
    QString name;
    do {
        ret = mLdif.nextItem();
        switch (ret) {
Laurent Montel's avatar
Laurent Montel committed
268
269
        case KLDAP::Ldif::Item:
        {
270
271
272
            name = mLdif.attr();
            const QByteArray value = mLdif.value();
            mCurrentObject.addValue(name, value);
Laurent Montel's avatar
Laurent Montel committed
273
            break;
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
        }
        case KLDAP::Ldif::EndEntry:
            finishCurrentObject();
            break;
        default:
            break;
        }
    } while (ret != KLDAP::Ldif::MoreData);
}

int LdapClient::clientNumber() const
{
    return d->mClientNumber;
}

int LdapClient::completionWeight() const
{
    return d->mCompletionWeight;
}

void LdapClient::setCompletionWeight(int weight)
{
    d->mCompletionWeight = weight;
}

#include "moc_ldapclient.cpp"