core: Track upgrade step within schema version
[quassel.git] / src / core / ldapauthenticator.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 /* This file contains an implementation of an LDAP Authenticator, as an example
22  * of what a custom external auth provider could do.
23  *
24  * It's based off of this pull request for quassel by abustany:
25  * https://github.com/quassel/quassel/pull/4/
26  *
27  */
28
29 #pragma once
30
31 #include "authenticator.h"
32 #include "core.h"
33
34 // Link against LDAP.
35 /* We should use openldap on windows if at all possible, rather than trying to
36  * write some kind of compatiblity routine.
37 #ifdef Q_CC_MSVC
38 #include <windows.h>
39 #include <winldap.h>
40 #else*/
41 #include <ldap.h>
42 //#endif
43
44 // Default LDAP server port.
45 constexpr int DEFAULT_LDAP_PORT = 389;
46
47 class LdapAuthenticator : public Authenticator
48 {
49     Q_OBJECT
50
51 public:
52     LdapAuthenticator(QObject* parent = nullptr);
53     ~LdapAuthenticator() override;
54
55 public slots:
56     /* General */
57     bool isAvailable() const override;
58     QString backendId() const override;
59     QString displayName() const override;
60     QString description() const override;
61     QVariantList setupData() const override;
62
63     bool canChangePassword() const override { return false; }
64
65     bool setup(const QVariantMap& settings, const QProcessEnvironment& environment, bool loadFromEnvironment) override;
66     State init(const QVariantMap& settings, const QProcessEnvironment& environment, bool loadFromEnvironment) override;
67     UserId validateUser(const QString& user, const QString& password) override;
68
69 protected:
70     void setAuthProperties(const QVariantMap& properties, const QProcessEnvironment& environment, bool loadFromEnvironment);
71     bool ldapConnect();
72     void ldapDisconnect();
73     bool ldapAuth(const QString& username, const QString& password);
74
75     // Protected methods for retrieving info about the LDAP connection.
76     QString hostName() const { return _hostName; }
77     int port() const { return _port; }
78     QString bindDN() const { return _bindDN; }
79     QString baseDN() const { return _baseDN; }
80
81 private:
82     QString _hostName;
83     int _port;
84     QString _bindDN;
85     QString _baseDN;
86     QString _filter;
87     QString _bindPassword;
88     QString _uidAttribute;
89
90     // The actual connection object.
91     LDAP* _connection{nullptr};
92 };