File indexing completed on 2024-12-22 05:07:41

0001 /*
0002     SPDX-FileCopyrightText: 2010-2023 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 // This code was taken from kmail-account-wizard
0008 
0009 #include "ldap.h"
0010 #include "restoreldapsettingsjob.h"
0011 #include <KLDAP/AddHostDialog>
0012 #include <KLDAP/LdapClientSearchConfig>
0013 #include <KLDAP/LdapClientSearchConfigReadConfigJob>
0014 #include <KLDAP/LdapClientSearchConfigWriteConfigJob>
0015 
0016 #include <KConfig>
0017 #include <KConfigGroup>
0018 #include <KLocalizedString>
0019 
0020 Ldap::Ldap(QObject *parent)
0021     : SetupObject(parent)
0022     , m_clientSearchConfig(new KLDAP::LdapClientSearchConfig)
0023 {
0024 }
0025 
0026 Ldap::~Ldap()
0027 {
0028     delete m_clientSearchConfig;
0029 }
0030 
0031 KConfig *Ldap::config() const
0032 {
0033     return m_clientSearchConfig->config();
0034 }
0035 
0036 void Ldap::create()
0037 {
0038     // TODO: use ldapclientsearchconfig to write config
0039     Q_EMIT info(i18n("Setting up LDAP server..."));
0040 
0041     if (m_server.isEmpty()) {
0042         Q_EMIT error(i18n("Needed parameters are missing for LDAP config: server '%1'", m_server));
0043         if (m_editMode) {
0044             edit();
0045         }
0046         return;
0047     }
0048 
0049     QString host = m_server;
0050 
0051     // Figure out the basedn
0052     QString basedn = m_baseDn.isEmpty() ? host : m_baseDn;
0053     if (m_baseDn.isEmpty() && !m_user.isEmpty()) {
0054         // If the user gave a full email address, the domain name
0055         // of that overrides the server name for the ldap dn
0056         const QString user = m_user;
0057         int pos = user.indexOf(QLatin1Char('@'));
0058         if (pos > 0) {
0059             const QString h = user.mid(pos + 1);
0060             if (!h.isEmpty()) {
0061                 // The user did type in a domain on the email address. Use that
0062                 basedn = h;
0063                 host = h;
0064             }
0065         }
0066     }
0067 
0068     basedn.replace(QLatin1Char('.'), QStringLiteral(",dc="));
0069 
0070     if (!basedn.startsWith(QLatin1String("dc="))) {
0071         basedn.prepend(QLatin1String("dc="));
0072     }
0073 
0074     // Set the changes
0075     KConfig *c = config();
0076     KConfigGroup group = c->group(QStringLiteral("LDAP"));
0077     bool hasMyServer = false;
0078     const int selHosts = group.readEntry("NumSelectedHosts", 0);
0079     for (int i = 0; i < selHosts && !hasMyServer; ++i) {
0080         if (group.readEntry(QStringLiteral("SelectedHost%1").arg(i), QString()) == host) {
0081             hasMyServer = true;
0082             m_entry = i;
0083         }
0084     }
0085 
0086     if (!hasMyServer) {
0087         m_entry = selHosts;
0088         group.writeEntry(QStringLiteral("NumSelectedHosts"), selHosts + 1);
0089         group.writeEntry(QStringLiteral("SelectedHost%1").arg(selHosts), host);
0090         group.writeEntry(QStringLiteral("SelectedBase%1").arg(selHosts), basedn);
0091         group.writeEntry(QStringLiteral("SelectedPort%1").arg(selHosts), m_port);
0092         group.writeEntry(QStringLiteral("SelectedVersion%1").arg(selHosts), m_version);
0093         group.writeEntry(QStringLiteral("SelectedSecurity%1").arg(selHosts), securityString());
0094 
0095         if (m_pageSize > 0) {
0096             group.writeEntry(QStringLiteral("SelectedPageSize%1").arg(selHosts), m_pageSize);
0097         }
0098 
0099         if (m_timeLimit > 0) {
0100             group.writeEntry(QStringLiteral("SelectedTimeLimit%1").arg(selHosts), m_timeLimit);
0101         }
0102 
0103         if (m_sizeLimit > 0) {
0104             group.writeEntry(QStringLiteral("SelectedSizeLimit%1").arg(selHosts), m_sizeLimit);
0105         }
0106 
0107         if (!m_authMethod.isEmpty()) {
0108             group.writeEntry(QStringLiteral("SelectedAuth%1").arg(selHosts), m_authMethod);
0109             group.writeEntry(QStringLiteral("SelectedBind%1").arg(selHosts), m_bindDn);
0110             group.writeEntry(QStringLiteral("SelectedPwdBind%1").arg(selHosts), m_password);
0111             group.writeEntry(QStringLiteral("SelectedRealm%1").arg(selHosts), m_realm);
0112             group.writeEntry(QStringLiteral("SelectedUser%1").arg(selHosts), m_user);
0113             group.writeEntry(QStringLiteral("SelectedMech%1").arg(selHosts), m_mech);
0114         }
0115         c->sync();
0116     }
0117     if (m_editMode) {
0118         edit();
0119     }
0120     Q_EMIT finished(i18n("LDAP set up."));
0121 }
0122 
0123 QString Ldap::securityString()
0124 {
0125     switch (m_security) {
0126     case KLDAP::LdapServer::None:
0127         return QStringLiteral("None");
0128     case KLDAP::LdapServer::SSL:
0129         return QStringLiteral("SSL");
0130     case KLDAP::LdapServer::TLS:
0131         return QStringLiteral("TLS");
0132     }
0133     return {};
0134 }
0135 
0136 void Ldap::destroy()
0137 {
0138     Q_EMIT info(i18n("LDAP not configuring."));
0139     if (m_entry >= 0) {
0140         KConfig *c = config();
0141         auto job = new RestoreLdapSettingsJob(this);
0142         job->setEntry(m_entry);
0143         job->setConfig(c);
0144         connect(job, &RestoreLdapSettingsJob::restoreDone, this, &Ldap::slotRestoreDone);
0145         job->start();
0146     }
0147 }
0148 
0149 void Ldap::slotRestoreDone()
0150 {
0151     Q_EMIT info(i18n("Removed LDAP entry."));
0152 }
0153 
0154 void Ldap::edit()
0155 {
0156     if (m_entry < 0) {
0157         Q_EMIT error(i18n("No config found to edit"));
0158         return;
0159     }
0160 
0161     KLDAP::LdapServer server;
0162     KLDAP::LdapClientSearchConfig clientSearchConfig;
0163     KConfigGroup group = clientSearchConfig.config()->group(QStringLiteral("LDAP"));
0164 
0165     auto *job = new KLDAP::LdapClientSearchConfigReadConfigJob(this);
0166     connect(job, &KLDAP::LdapClientSearchConfigReadConfigJob::configLoaded, this, [this, group](KLDAP::LdapServer server) {
0167         KLDAP::AddHostDialog dlg(&server, nullptr);
0168 
0169         if (dlg.exec() && !server.host().isEmpty()) { // krazy:exclude=crashy
0170             auto job = new KLDAP::LdapClientSearchConfigWriteConfigJob;
0171             job->setActive(true);
0172             job->setConfig(group);
0173             job->setServer(server);
0174             job->setServerIndex(m_entry);
0175             job->start();
0176         }
0177     });
0178     job->setActive(true);
0179     job->setConfig(group);
0180     job->setServerIndex(m_entry);
0181     job->start();
0182 }
0183 
0184 void Ldap::setUser(const QString &user)
0185 {
0186     m_user = user;
0187 }
0188 
0189 void Ldap::setServer(const QString &server)
0190 {
0191     m_server = server;
0192 }
0193 
0194 void Ldap::setBaseDn(const QString &baseDn)
0195 {
0196     m_baseDn = baseDn;
0197 }
0198 
0199 void Ldap::setAuthenticationMethod(const QString &meth)
0200 {
0201     m_authMethod = meth;
0202 }
0203 
0204 void Ldap::setBindDn(const QString &bindDn)
0205 {
0206     m_bindDn = bindDn;
0207 }
0208 
0209 void Ldap::setPassword(const QString &password)
0210 {
0211     m_password = password;
0212 }
0213 
0214 void Ldap::setPageSize(const int pageSize)
0215 {
0216     m_pageSize = pageSize;
0217 }
0218 
0219 void Ldap::setPort(const int port)
0220 {
0221     m_port = port;
0222 }
0223 
0224 void Ldap::setRealm(const QString &realm)
0225 {
0226     m_realm = realm;
0227 }
0228 
0229 void Ldap::setSaslMech(const QString &saslmech)
0230 {
0231     m_mech = saslmech;
0232 }
0233 
0234 void Ldap::setSecurity(const KLDAP::LdapServer::Security security)
0235 {
0236     m_security = security;
0237 }
0238 
0239 void Ldap::setSizeLimit(const int sizeLimit)
0240 {
0241     m_sizeLimit = sizeLimit;
0242 }
0243 
0244 void Ldap::setTimeLimit(const int timeLimit)
0245 {
0246     m_timeLimit = timeLimit;
0247 }
0248 
0249 void Ldap::setVersion(const int version)
0250 {
0251     m_version = version;
0252 }
0253 
0254 void Ldap::setEditMode(const bool editMode)
0255 {
0256     m_editMode = editMode;
0257 }