8415496fb1de7f6373c4570000d390c55cf0bdca
[quassel.git] / src / core / ldapauthenticator.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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
33 #include "core.h"
34
35 // Link against LDAP.
36 /* We should use openldap on windows if at all possible, rather than trying to
37  * write some kind of compatiblity routine.
38 #ifdef Q_CC_MSVC
39 #include <windows.h>
40 #include <winldap.h>
41 #else*/
42 #include <ldap.h>
43 //#endif
44
45 // Default LDAP server port.
46 #define DEFAULT_LDAP_PORT 389
47
48 class LdapAuthenticator : public Authenticator
49 {
50     Q_OBJECT
51
52 public:
53     LdapAuthenticator(QObject *parent = 0);
54     virtual ~LdapAuthenticator();
55
56 public slots:
57     /* General */
58     bool isAvailable() const;
59     QString backendId() const;
60     QString displayName() const;
61     QString description() const;
62     virtual QStringList setupKeys() const;
63     virtual QVariantMap setupDefaults() const;
64
65     virtual inline bool canChangePassword() const { return false; }
66
67     bool setup(const QVariantMap &settings = QVariantMap());
68     State init(const QVariantMap &settings = QVariantMap());
69     UserId validateUser(const QString &user, const QString &password);
70
71 protected:
72     virtual void setAuthProperties(const QVariantMap &properties);
73     bool ldapConnect();
74     void ldapDisconnect();
75     bool ldapAuth(const QString &username, const QString &password);
76
77     // Protected methods for retrieving info about the LDAP connection.
78     inline virtual QString hostName() { return _hostName; }
79     inline virtual int port() { return _port; }
80     inline virtual QString bindDN() { return _bindDN; }
81     inline virtual QString baseDN() { return _baseDN; }
82
83 private:
84     QString _hostName;
85     int _port;
86     QString _bindDN;
87     QString _baseDN;
88     QString _filter;
89     QString _bindPassword;
90     QString _uidAttribute;
91
92     // The actual connection object.
93     LDAP *_connection;
94
95 };