File indexing completed on 2024-06-09 05:22:33

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 // This code was taken from kmail-account-wizard
0008 
0009 #include "autoconfigkolabldap.h"
0010 
0011 #include <QDomDocument>
0012 
0013 AutoconfigKolabLdap::AutoconfigKolabLdap(QObject *parent)
0014     : AutoconfigKolabMail(parent)
0015 {
0016 }
0017 
0018 void AutoconfigKolabLdap::lookupInDb(bool auth, bool crypt)
0019 {
0020     if (serverType() == DataBase) {
0021         setServerType(IspAutoConfig);
0022     }
0023 
0024     startJob(lookupUrl(QStringLiteral("ldap"), QStringLiteral("1.0"), auth, crypt));
0025 }
0026 
0027 void AutoconfigKolabLdap::parseResult(const QDomDocument &document)
0028 {
0029     const QDomElement docElem = document.documentElement();
0030     const QDomNodeList l = docElem.elementsByTagName(QStringLiteral("ldapProvider"));
0031 
0032     if (l.isEmpty()) {
0033         Q_EMIT finished(false);
0034         return;
0035     }
0036 
0037     for (int i = 0; i < l.count(); ++i) {
0038         QDomElement e = l.at(i).toElement();
0039         ldapServer s = createLdapServer(e);
0040         if (s.isValid()) {
0041             mLdapServers[e.attribute(QStringLiteral("id"))] = s;
0042         }
0043     }
0044 
0045     Q_EMIT finished(true);
0046 }
0047 
0048 ldapServer AutoconfigKolabLdap::createLdapServer(const QDomElement &n)
0049 {
0050     QDomNode o = n.firstChild();
0051     ldapServer s;
0052     while (!o.isNull()) {
0053         QDomElement f = o.toElement();
0054         if (!f.isNull()) {
0055             const QString tagName(f.tagName());
0056             if (tagName == QLatin1String("hostname")) {
0057                 s.hostname = replacePlaceholders(f.text());
0058             } else if (tagName == QLatin1String("port")) {
0059                 s.port = f.text().toInt();
0060             } else if (tagName == QLatin1String("socketType")) {
0061                 const QString type(f.text());
0062                 if (type == QLatin1String("plain")) {
0063                     s.socketType = KLDAP::LdapServer::None;
0064                 } else if (type == QLatin1String("SSL")) {
0065                     s.socketType = KLDAP::LdapServer::SSL;
0066                 } else if (type == QLatin1String("TLS")) {
0067                     s.socketType = KLDAP::LdapServer::TLS;
0068                 }
0069             } else if (tagName == QLatin1String("authentication")) {
0070                 const QString type(f.text());
0071                 if (type == QLatin1String("anonyoum")) {
0072                     s.authentication = KLDAP::LdapServer::Anonymous;
0073                 } else if (type == QLatin1String("simple")) {
0074                     s.authentication = KLDAP::LdapServer::Simple;
0075                 } else if (type == QLatin1String("sasl")) {
0076                     s.authentication = KLDAP::LdapServer::SASL;
0077                 }
0078             } else if (tagName == QLatin1String("bindDn")) {
0079                 s.bindDn = f.text();
0080             } else if (tagName == QLatin1String("sasl-mech")) {
0081                 s.saslMech = f.text();
0082             } else if (tagName == QLatin1String("username")) {
0083                 s.username = f.text();
0084             } else if (tagName == QLatin1String("password")) {
0085                 s.password = f.text();
0086             } else if (tagName == QLatin1String("realm")) {
0087                 s.realm = f.text();
0088             } else if (tagName == QLatin1String("dn")) {
0089                 s.dn = f.text();
0090             } else if (tagName == QLatin1String("ldapVersion")) {
0091                 s.ldapVersion = f.text().toInt();
0092             } else if (tagName == QLatin1String("filter")) {
0093                 s.filter = f.text();
0094             } else if (tagName == QLatin1String("pagesize")) {
0095                 s.pageSize = f.text().toInt();
0096                 if (s.pageSize < 1 || s.pageSize > 9999999) {
0097                     s.pageSize = -1;
0098                 }
0099             } else if (tagName == QLatin1String("timelimit")) {
0100                 s.timeLimit = f.text().toInt();
0101                 if (s.timeLimit < 1 || s.timeLimit > 9999999) {
0102                     s.timeLimit = -1;
0103                 }
0104             } else if (tagName == QLatin1String("sizelimit")) {
0105                 s.sizeLimit = f.text().toInt();
0106                 if (s.sizeLimit < 1 || s.sizeLimit > 9999999) {
0107                     s.sizeLimit = -1;
0108                 }
0109             }
0110         }
0111         o = o.nextSibling();
0112     }
0113     return s;
0114 }
0115 
0116 QHash<QString, ldapServer> AutoconfigKolabLdap::ldapServers() const
0117 {
0118     return mLdapServers;
0119 }
0120 
0121 bool ldapServer::isValid() const
0122 {
0123     return port != -1;
0124 }