File indexing completed on 2025-01-26 04:52:15
0001 /* 0002 * SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "ldapclientsearchconfigreadconfigjob.h" 0008 #include "ldapclient_debug.h" 0009 0010 #include "kldapcore/ldapdn.h" 0011 #include <KConfig> 0012 #include <KLocalizedString> 0013 #include <qt6keychain/keychain.h> 0014 using namespace QKeychain; 0015 0016 using namespace KLDAPWidgets; 0017 LdapClientSearchConfigReadConfigJob::LdapClientSearchConfigReadConfigJob(QObject *parent) 0018 : QObject(parent) 0019 { 0020 } 0021 0022 LdapClientSearchConfigReadConfigJob::~LdapClientSearchConfigReadConfigJob() = default; 0023 0024 bool LdapClientSearchConfigReadConfigJob::canStart() const 0025 { 0026 return mServerIndex != -1 && mConfig.isValid(); 0027 } 0028 0029 void LdapClientSearchConfigReadConfigJob::readLdapClientConfigFinished() 0030 { 0031 Q_EMIT configLoaded(mServer); 0032 deleteLater(); 0033 } 0034 0035 void LdapClientSearchConfigReadConfigJob::start() 0036 { 0037 if (!canStart()) { 0038 // Failed ! 0039 readLdapClientConfigFinished(); 0040 return; 0041 } 0042 readConfig(); 0043 } 0044 0045 bool LdapClientSearchConfigReadConfigJob::active() const 0046 { 0047 return mActive; 0048 } 0049 0050 void LdapClientSearchConfigReadConfigJob::setActive(bool newActive) 0051 { 0052 mActive = newActive; 0053 } 0054 0055 int LdapClientSearchConfigReadConfigJob::serverIndex() const 0056 { 0057 return mServerIndex; 0058 } 0059 0060 void LdapClientSearchConfigReadConfigJob::setServerIndex(int newServerIndex) 0061 { 0062 mServerIndex = newServerIndex; 0063 } 0064 0065 KConfigGroup LdapClientSearchConfigReadConfigJob::config() const 0066 { 0067 return mConfig; 0068 } 0069 0070 void LdapClientSearchConfigReadConfigJob::setConfig(const KConfigGroup &newConfig) 0071 { 0072 mConfig = newConfig; 0073 } 0074 0075 void LdapClientSearchConfigReadConfigJob::readConfig() 0076 { 0077 QString prefix; 0078 if (mActive) { 0079 prefix = QStringLiteral("Selected"); 0080 } 0081 0082 const QString host = mConfig.readEntry(prefix + QStringLiteral("Host%1").arg(mServerIndex), QString()).trimmed(); 0083 if (!host.isEmpty()) { 0084 mServer.setHost(host); 0085 } 0086 0087 const int port = mConfig.readEntry(prefix + QStringLiteral("Port%1").arg(mServerIndex), 389); 0088 mServer.setPort(port); 0089 0090 const QString base = mConfig.readEntry(prefix + QStringLiteral("Base%1").arg(mServerIndex), QString()).trimmed(); 0091 if (!base.isEmpty()) { 0092 mServer.setBaseDn(KLDAPCore::LdapDN(base)); 0093 } 0094 0095 const QString user = mConfig.readEntry(prefix + QStringLiteral("User%1").arg(mServerIndex), QString()).trimmed(); 0096 if (!user.isEmpty()) { 0097 mServer.setUser(user); 0098 } 0099 0100 const QString bindDN = mConfig.readEntry(prefix + QStringLiteral("Bind%1").arg(mServerIndex), QString()).trimmed(); 0101 if (!bindDN.isEmpty()) { 0102 mServer.setBindDn(bindDN); 0103 } 0104 mServer.setTimeLimit(mConfig.readEntry(prefix + QStringLiteral("TimeLimit%1").arg(mServerIndex), 0)); 0105 mServer.setSizeLimit(mConfig.readEntry(prefix + QStringLiteral("SizeLimit%1").arg(mServerIndex), 0)); 0106 mServer.setPageSize(mConfig.readEntry(prefix + QStringLiteral("PageSize%1").arg(mServerIndex), 0)); 0107 mServer.setVersion(mConfig.readEntry(prefix + QStringLiteral("Version%1").arg(mServerIndex), 3)); 0108 0109 QString tmp = mConfig.readEntry(prefix + QStringLiteral("Security%1").arg(mServerIndex), QStringLiteral("None")); 0110 mServer.setSecurity(KLDAPCore::LdapServer::None); 0111 if (tmp == QLatin1StringView("SSL")) { 0112 mServer.setSecurity(KLDAPCore::LdapServer::SSL); 0113 } else if (tmp == QLatin1StringView("TLS")) { 0114 mServer.setSecurity(KLDAPCore::LdapServer::TLS); 0115 } 0116 0117 tmp = mConfig.readEntry(prefix + QStringLiteral("Auth%1").arg(mServerIndex), QStringLiteral("Anonymous")); 0118 mServer.setAuth(KLDAPCore::LdapServer::Anonymous); 0119 if (tmp == QLatin1StringView("Simple")) { 0120 mServer.setAuth(KLDAPCore::LdapServer::Simple); 0121 } else if (tmp == QLatin1StringView("SASL")) { 0122 mServer.setAuth(KLDAPCore::LdapServer::SASL); 0123 } 0124 0125 mServer.setMech(mConfig.readEntry(prefix + QStringLiteral("Mech%1").arg(mServerIndex), QString())); 0126 mServer.setFilter(mConfig.readEntry(prefix + QStringLiteral("UserFilter%1").arg(mServerIndex), QString())); 0127 mServer.setCompletionWeight(mConfig.readEntry(prefix + QStringLiteral("CompletionWeight%1").arg(mServerIndex), -1)); 0128 0129 const QString pwdBindBNEntry = prefix + QStringLiteral("PwdBind%1").arg(mServerIndex); 0130 0131 auto readJob = new ReadPasswordJob(QStringLiteral("ldapclient"), this); 0132 connect(readJob, &Job::finished, this, [this, pwdBindBNEntry](QKeychain::Job *baseJob) { 0133 auto job = qobject_cast<ReadPasswordJob *>(baseJob); 0134 Q_ASSERT(job); 0135 if (!job->error()) { 0136 mServer.setPassword(job->textData()); 0137 } else { 0138 qCWarning(LDAPCLIENT_LOG) << "We have an error during reading password " << job->errorString() << " password key " << pwdBindBNEntry; 0139 } 0140 readLdapClientConfigFinished(); 0141 }); 0142 readJob->setKey(pwdBindBNEntry); 0143 readJob->start(); 0144 } 0145 0146 #include "moc_ldapclientsearchconfigreadconfigjob.cpp"