1 /***************************************************************************
2 * Copyright (C) 2005-2016 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 /* This file contains an implementation of an LDAP Authenticator, as an example
22 * of what a custom external auth provider could do.
24 * It's based off of this pull request for quassel by abustany:
25 * https://github.com/quassel/quassel/pull/4/
29 #include "ldapauthenticator.h"
35 /* We should use openldap on windows if at all possible, rather than trying to
36 * write some kind of compatiblity routine.
44 LdapAuthenticator::LdapAuthenticator(QObject *parent)
45 : Authenticator(parent),
51 LdapAuthenticator::~LdapAuthenticator()
53 if (_connection != 0) {
54 ldap_unbind_ext(_connection, 0, 0);
59 bool LdapAuthenticator::isAvailable() const
61 // FIXME: probably this should test if we can speak to the LDAP server.
66 QString LdapAuthenticator::backendId() const
68 // We identify the backend to use for the monolithic core by this identifier.
69 // so only change this string if you _really_ have to and make sure the core
70 // setup for the mono client still works ;)
71 return QString("LDAP");
75 QString LdapAuthenticator::displayName() const
81 QString LdapAuthenticator::description() const
83 return tr("Authenticate users using an LDAP server.");
87 QVariantList LdapAuthenticator::setupData() const
89 // The parameters needed for LDAP.
91 data << "Hostname" << tr("Hostname") << QString{"ldap://localhost"}
92 << "Port" << tr("Port") << DEFAULT_LDAP_PORT
93 << "BindDN" << tr("Bind DN") << QString{}
94 << "BindPassword" << tr("Bind Password") << QString{}
95 << "BaseDN" << tr("Base DN") << QString{}
96 << "Filter" << tr("Filter") << QString{}
97 << "UidAttribute" << tr("UID Attribute") << QString{"uid"}
103 void LdapAuthenticator::setAuthProperties(const QVariantMap &properties)
105 _hostName = properties["Hostname"].toString();
106 _port = properties["Port"].toInt();
107 _bindDN = properties["BindDN"].toString();
108 _bindPassword = properties["BindPassword"].toString();
109 _baseDN = properties["BaseDN"].toString();
110 _filter = properties["Filter"].toString();
111 _uidAttribute = properties["UidAttribute"].toString();
114 // TODO: this code is sufficiently general that in the future, perhaps an abstract
115 // class should be created implementing it.
116 // i.e. a provider that does its own thing and then pokes at the current storage
117 // through the default core method.
118 UserId LdapAuthenticator::validateUser(const QString &username, const QString &password)
120 bool result = ldapAuth(username, password);
125 // LDAP is case-insensitive, thus we will lowercase the username, in spite of
126 // a better solution :(
127 const QString lUsername = username.toLower();
129 // If auth succeeds, but the user has not logged into quassel previously, make
130 // a new user for them and return that ID.
131 // Users created via LDAP have empty passwords, but authenticator column = LDAP.
132 // On the other hand, if auth succeeds and the user already exists, do a final
133 // cross-check to confirm we're using the right auth provider.
134 UserId quasselId = Core::validateUser(lUsername, QString());
135 if (!quasselId.isValid()) {
136 return Core::addUser(lUsername, QString(), backendId());
138 else if (!(Core::checkAuthProvider(quasselId, backendId()))) {
145 bool LdapAuthenticator::setup(const QVariantMap &settings)
147 setAuthProperties(settings);
148 bool status = ldapConnect();
153 Authenticator::State LdapAuthenticator::init(const QVariantMap &settings)
155 setAuthProperties(settings);
157 bool status = ldapConnect();
159 quInfo() << qPrintable(backendId()) << "authenticator cannot connect.";
163 quInfo() << qPrintable(backendId()) << "authenticator is ready.";
167 // Method based on abustany LDAP quassel patch.
168 bool LdapAuthenticator::ldapConnect()
170 if (_connection != 0) {
174 int res, v = LDAP_VERSION3;
177 QByteArray serverURIArray;
179 // Convert info to hostname:port.
180 serverURI = _hostName + ":" + QString::number(_port);
181 serverURIArray = serverURI.toLocal8Bit();
182 res = ldap_initialize(&_connection, serverURIArray);
184 if (res != LDAP_SUCCESS) {
185 qWarning() << "Could not connect to LDAP server:" << ldap_err2string(res);
189 res = ldap_set_option(_connection, LDAP_OPT_PROTOCOL_VERSION, (void*)&v);
191 if (res != LDAP_SUCCESS) {
192 qWarning() << "Could not set LDAP protocol version to v3:" << ldap_err2string(res);
193 ldap_unbind_ext(_connection, 0, 0);
202 void LdapAuthenticator::ldapDisconnect()
204 if (_connection == 0) {
208 ldap_unbind_ext(_connection, 0, 0);
213 bool LdapAuthenticator::ldapAuth(const QString &username, const QString &password)
215 if (password.isEmpty()) {
221 // Attempt to establish a connection.
222 if (_connection == 0) {
223 if (!ldapConnect()) {
230 // Convert some things to byte arrays as needed.
231 QByteArray bindPassword = _bindPassword.toLocal8Bit();
232 QByteArray bindDN = _bindDN.toLocal8Bit();
233 QByteArray baseDN = _baseDN.toLocal8Bit();
234 QByteArray uidAttribute = _uidAttribute.toLocal8Bit();
236 cred.bv_val = (bindPassword.size() > 0 ? bindPassword.data() : NULL);
237 cred.bv_len = bindPassword.size();
239 res = ldap_sasl_bind_s(_connection, bindDN.size() > 0 ? bindDN.constData() : 0, LDAP_SASL_SIMPLE, &cred, 0, 0, 0);
241 if (res != LDAP_SUCCESS) {
242 qWarning() << "Refusing connection from" << username << "(LDAP bind failed:" << ldap_err2string(res) << ")";
247 LDAPMessage *msg = NULL, *entry = NULL;
249 const QByteArray ldapQuery = "(&(" + uidAttribute + '=' + username.toLocal8Bit() + ")" + _filter.toLocal8Bit() + ")";
251 res = ldap_search_ext_s(_connection, baseDN.constData(), LDAP_SCOPE_SUBTREE, ldapQuery.constData(), 0, 0, 0, 0, 0, 0, &msg);
253 if (res != LDAP_SUCCESS) {
254 qWarning() << "Refusing connection from" << username << "(LDAP search failed:" << ldap_err2string(res) << ")";
258 if (ldap_count_entries(_connection, msg) > 1) {
259 qWarning() << "Refusing connection from" << username << "(LDAP search returned more than one result)";
264 entry = ldap_first_entry(_connection, msg);
267 qWarning() << "Refusing connection from" << username << "(LDAP search returned no results)";
272 QByteArray passwordArray = password.toLocal8Bit();
273 cred.bv_val = passwordArray.data();
274 cred.bv_len = password.size();
276 char *userDN = ldap_get_dn(_connection, entry);
278 res = ldap_sasl_bind_s(_connection, userDN, LDAP_SASL_SIMPLE, &cred, 0, 0, 0);
280 if (res != LDAP_SUCCESS) {
281 qWarning() << "Refusing connection from" << username << "(LDAP authentication failed)";
282 ldap_memfree(userDN);
287 // The original implementation had requiredAttributes. I have not included this code
288 // but it would be easy to re-add if someone wants this feature.
289 // Ben Rosser <bjr@acm.jhu.edu> (12/23/15).
291 ldap_memfree(userDN);