ldap.cpp 8.17 KB
Newer Older
1
/*
2
    SPDX-FileCopyrightText: 2010-2020 Laurent Montel <montel@kde.org>
3

4
    SPDX-License-Identifier: LGPL-2.0-or-later
5
6
7
*/

#include "ldap.h"
Laurent Montel's avatar
Laurent Montel committed
8
9
#include <KLDAP/LdapClientSearchConfig>
#include <KLDAP/AddHostDialog>
Laurent Montel's avatar
Laurent Montel committed
10
#include <KLDAP/LdapClientSearchConfigWriteConfigJob>
Laurent Montel's avatar
Laurent Montel committed
11
#include <KLDAP/LdapClientSearchConfigReadConfigJob>
12
13
14
15
16
17
18

#include <KConfig>
#include <KConfigGroup>
#include <KLocalizedString>

Ldap::Ldap(QObject *parent)
    : SetupObject(parent)
Laurent Montel's avatar
Laurent Montel committed
19
    , m_clientSearchConfig(new KLDAP::LdapClientSearchConfig)
20
21
22
23
24
25
26
27
    , m_port(389)
    , m_security(KLDAP::LdapServer::None)
    , m_version(3)
    , m_pageSize(0)
    , m_timeLimit(0)
    , m_sizeLimit(0)
    , m_entry(-1)
    , m_editMode(false)
28
29
30
31
32
{
}

Ldap::~Ldap()
{
33
    delete m_clientSearchConfig;
34
35
}

36
37
38
KConfig *Ldap::config() const
{
    return m_clientSearchConfig->config();
39
40
41
42
}

void Ldap::create()
{
43
    //TODO: use ldapclientsearchconfig to write config
Laurent Montel's avatar
Laurent Montel committed
44
    Q_EMIT info(i18n("Setting up LDAP server..."));
45

46
    if (m_server.isEmpty()) {
47
        Q_EMIT error(i18n("Needed parameters are missing for LDAP config: server '%1'", m_server));
48
49
50
        if (m_editMode) {
            edit();
        }
51
52
53
        return;
    }

54
    QString host = m_server;
55
56

    // Figure out the basedn
57
58
59
60
61
    QString basedn = m_baseDn.isEmpty() ? host : m_baseDn;
    if (m_baseDn.isEmpty() && !m_user.isEmpty()) {
        // If the user gave a full email address, the domain name
        // of that overrides the server name for the ldap dn
        const QString user = m_user;
Laurent Montel's avatar
Laurent Montel committed
62
        int pos = user.indexOf(QLatin1Char('@'));
63
64
65
66
67
68
69
        if (pos > 0) {
            const QString h = user.mid(pos + 1);
            if (!h.isEmpty()) {
                // The user did type in a domain on the email address. Use that
                basedn = h;
                host = h;
            }
70
71
72
        }
    }

Laurent Montel's avatar
Laurent Montel committed
73
    basedn.replace(QLatin1Char('.'), QStringLiteral(",dc="));
74

Laurent Montel's avatar
Laurent Montel committed
75
    if (!basedn.startsWith(QLatin1String("dc="))) {
Laurent Montel's avatar
Laurent Montel committed
76
        basedn.prepend(QLatin1String("dc="));
77
    }
78
79

    // Set the changes
80
81
    KConfig *c = config();
    KConfigGroup group = c->group(QStringLiteral("LDAP"));
82
83
    bool hasMyServer = false;
    uint selHosts = group.readEntry("NumSelectedHosts", 0);
Laurent Montel's avatar
Laurent Montel committed
84
    for (uint i = 0; i < selHosts && !hasMyServer; ++i) {
Laurent Montel's avatar
Laurent Montel committed
85
        if (group.readEntry(QStringLiteral("SelectedHost%1").arg(i), QString()) == host) {
86
            hasMyServer = true;
87
            m_entry = i;
88
        }
89
90
    }

91
    if (!hasMyServer) {
92
93
        m_entry = selHosts;
        group.writeEntry(QStringLiteral("NumSelectedHosts"), selHosts + 1);
Laurent Montel's avatar
Laurent Montel committed
94
95
        group.writeEntry(QStringLiteral("SelectedHost%1").arg(selHosts), host);
        group.writeEntry(QStringLiteral("SelectedBase%1").arg(selHosts), basedn);
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        group.writeEntry(QStringLiteral("SelectedPort%1").arg(selHosts), m_port);
        group.writeEntry(QStringLiteral("SelectedVersion%1").arg(selHosts), m_version);
        group.writeEntry(QStringLiteral("SelectedSecurity%1").arg(selHosts), securityString());

        if (m_pageSize > 0) {
            group.writeEntry(QStringLiteral("SelectedPageSize%1").arg(selHosts), m_pageSize);
        }

        if (m_timeLimit > 0) {
            group.writeEntry(QStringLiteral("SelectedTimeLimit%1").arg(selHosts), m_timeLimit);
        }

        if (m_sizeLimit > 0) {
            group.writeEntry(QStringLiteral("SelectedSizeLimit%1").arg(selHosts), m_sizeLimit);
        }

112
        if (!m_authMethod.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
113
114
115
            group.writeEntry(QStringLiteral("SelectedAuth%1").arg(selHosts), m_authMethod);
            group.writeEntry(QStringLiteral("SelectedBind%1").arg(selHosts), m_bindDn);
            group.writeEntry(QStringLiteral("SelectedPwdBind%1").arg(selHosts), m_password);
116
117
118
            group.writeEntry(QStringLiteral("SelectedRealm%1").arg(selHosts), m_realm);
            group.writeEntry(QStringLiteral("SelectedUser%1").arg(selHosts), m_user);
            group.writeEntry(QStringLiteral("SelectedMech%1").arg(selHosts), m_mech);
119
        }
120
        c->sync();
121
    }
122
123
    if (m_editMode) {
        edit();
124
    }
Laurent Montel's avatar
Laurent Montel committed
125
    Q_EMIT finished(i18n("LDAP set up."));
126
127
}

128
QString Ldap::securityString()
129
{
130
131
132
133
134
135
136
    switch (m_security) {
    case KLDAP::LdapServer::None:
        return QStringLiteral("None");
    case KLDAP::LdapServer::SSL:
        return QStringLiteral("SSL");
    case KLDAP::LdapServer::TLS:
        return QStringLiteral("TLS");
137
    }
Laurent Montel's avatar
Laurent Montel committed
138
    return {};
139
140
141
142
}

void Ldap::destroy()
{
Laurent Montel's avatar
Laurent Montel committed
143
    Q_EMIT info(i18n("LDAP not configuring."));
144
145
146
147
148
    if (m_entry >= 0) {
        KConfig *c = config();
        KConfigGroup group = c->group(QStringLiteral("LDAP"));
        int cSelHosts = group.readEntry(QStringLiteral("NumSelectedHosts"), 0);
        int cHosts = group.readEntry(QStringLiteral("NumHosts"), 0);
Laurent Montel's avatar
Laurent Montel committed
149
150
        QVector<KLDAP::LdapServer> selHosts;
        QVector<KLDAP::LdapServer> hosts;
151
152
153
154
155
156
157
        for (int i = 0; i < cSelHosts; ++i) {
            if (i != m_entry) {
                KLDAP::LdapServer server;
                m_clientSearchConfig->readConfig(server, group, i, true);
                selHosts.append(server);
            }
        }
Laurent Montel's avatar
Laurent Montel committed
158
        hosts.reserve(cHosts);
159
160
161
162
163
164
165
        for (int i = 0; i < cHosts; ++i) {
            KLDAP::LdapServer server;
            m_clientSearchConfig->readConfig(server, group, i, false);
            hosts.append(server);
        }

        c->deleteGroup(QStringLiteral("LDAP"));
Laurent Montel's avatar
Laurent Montel committed
166
        group = KConfigGroup(c, QStringLiteral("LDAP"));
167
168

        for (int i = 0; i < cSelHosts - 1; ++i) {
Laurent Montel's avatar
Laurent Montel committed
169
170
171
172
173
174
            auto job = new KLDAP::LdapClientSearchConfigWriteConfigJob;
            job->setActive(true);
            job->setConfig(group);
            job->setServer(selHosts.at(i));
            job->setServerIndex(i);
            job->start();
175
176
177
        }

        for (int i = 0; i < cHosts; ++i) {
Laurent Montel's avatar
Laurent Montel committed
178
179
180
181
182
183
            auto job = new KLDAP::LdapClientSearchConfigWriteConfigJob;
            job->setActive(false);
            job->setConfig(group);
            job->setServer(hosts.at(i));
            job->setServerIndex(i);
            job->start();
184
185
186
187
188
189
190
191
        }

        group.writeEntry(QStringLiteral("NumSelectedHosts"), cSelHosts - 1);
        group.writeEntry(QStringLiteral("NumHosts"), cHosts);
        c->sync();

        Q_EMIT info(i18n("Removed LDAP entry."));
    }
192
193
}

194
195
196
void Ldap::edit()
{
    if (m_entry < 0) {
197
        Q_EMIT error(i18n("No config found to edit"));
198
199
200
201
202
        return;
    }

    KLDAP::LdapServer server;
    KLDAP::LdapClientSearchConfig clientSearchConfig;
203
    KConfigGroup group = clientSearchConfig.config()->group(QStringLiteral("LDAP"));
Laurent Montel's avatar
Laurent Montel committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

    KLDAP::LdapClientSearchConfigReadConfigJob *job = new KLDAP::LdapClientSearchConfigReadConfigJob(this);
    connect(job, &KLDAP::LdapClientSearchConfigReadConfigJob::configLoaded, this, [this, group](KLDAP::LdapServer server) {
        KLDAP::AddHostDialog dlg(&server, nullptr);

        if (dlg.exec() && !server.host().isEmpty()) { //krazy:exclude=crashy
            auto job = new KLDAP::LdapClientSearchConfigWriteConfigJob;
            job->setActive(true);
            job->setConfig(group);
            job->setServer(server);
            job->setServerIndex(m_entry);
            job->start();
        }
    });
    job->setActive(true);
    job->setConfig(group);
    job->setServerIndex(m_entry);
    job->start();
222
223
224
225
226
227
228
229
230
231
232
233
}

void Ldap::setUser(const QString &user)
{
    m_user = user;
}

void Ldap::setServer(const QString &server)
{
    m_server = server;
}

234
235
void Ldap::setBaseDn(const QString &baseDn)
{
236
    m_baseDn = baseDn;
237
238
}

239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
void Ldap::setAuthenticationMethod(const QString &meth)
{
    m_authMethod = meth;
}

void Ldap::setBindDn(const QString &bindDn)
{
    m_bindDn = bindDn;
}

void Ldap::setPassword(const QString &password)
{
    m_password = password;
}

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
void Ldap::setPageSize(const int pageSize)
{
    m_pageSize = pageSize;
}

void Ldap::setPort(const int port)
{
    m_port = port;
}

void Ldap::setRealm(const QString &realm)
{
    m_realm = realm;
}

void Ldap::setSaslMech(const QString &saslmech)
{
    m_mech = saslmech;
}

void Ldap::setSecurity(const KLDAP::LdapServer::Security security)
{
    m_security = security;
}

void Ldap::setSizeLimit(const int sizeLimit)
{
    m_sizeLimit = sizeLimit;
}

void Ldap::setTimeLimit(const int timeLimit)
{
    m_timeLimit = timeLimit;
}

void Ldap::setVersion(const int version)
{
    m_version = version;
}

294
295
296
297
void Ldap::setEditMode(const bool editMode)
{
    m_editMode = editMode;
}