]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ldapoper.cpp
Remove references to old configure commands.
[user/henk/code/inspircd.git] / src / modules / extra / m_ldapoper.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  *
13  * Taken from the UnrealIRCd 4.0 SVN version, based on
14  * InspIRCd 1.1.x.
15  *
16  * UnrealIRCd 4.0 (C) 2007 Carsten Valdemar Munk
17  * This program is free but copyrighted software; see
18  *          the file COPYING for details.
19  *
20  * ---------------------------------------------------
21  * Heavily based on SQLauth
22  */
23
24 #include "inspircd.h"
25 #include "users.h"
26 #include "channels.h"
27 #include "modules.h"
28
29 #include <ldap.h>
30
31 #ifdef WINDOWS
32 # pragma comment(lib, "ldap.lib")
33 # pragma comment(lib, "lber.lib")
34 #endif
35
36 /* $ModDesc: Allow/Deny connections based upon answer from LDAP server */
37 /* $LinkerFlags: -lldap */
38
39 class ModuleLDAPAuth : public Module
40 {
41         std::string base;
42         std::string ldapserver;
43         std::string username;
44         std::string password;
45         int searchscope;
46         LDAP *conn;
47
48 public:
49         ModuleLDAPAuth()
50                 {
51                 conn = NULL;
52                 Implementation eventlist[] = { I_OnRehash, I_OnPassCompare };
53                 ServerInstance->Modules->Attach(eventlist, this, 2);
54                 OnRehash(NULL);
55         }
56
57         virtual ~ModuleLDAPAuth()
58         {
59                 if (conn)
60                         ldap_unbind_ext(conn, NULL, NULL);
61         }
62
63         virtual void OnRehash(User* user)
64         {
65                 ConfigReader Conf;
66
67                 base                    = Conf.ReadValue("ldapoper", "baserdn", 0);
68                 ldapserver              = Conf.ReadValue("ldapoper", "server", 0);
69                 std::string scope       = Conf.ReadValue("ldapoper", "searchscope", 0);
70                 username                = Conf.ReadValue("ldapoper", "binddn", 0);
71                 password                = Conf.ReadValue("ldapoper", "bindauth", 0);
72
73                 if (scope == "base")
74                         searchscope = LDAP_SCOPE_BASE;
75                 else if (scope == "onelevel")
76                         searchscope = LDAP_SCOPE_ONELEVEL;
77                 else searchscope = LDAP_SCOPE_SUBTREE;
78
79                 Connect();
80         }
81
82         bool Connect()
83         {
84                 if (conn != NULL)
85                         ldap_unbind_ext(conn, NULL, NULL);
86                 int res, v = LDAP_VERSION3;
87                 res = ldap_initialize(&conn, ldapserver.c_str());
88                 if (res != LDAP_SUCCESS)
89                 {
90                         conn = NULL;
91                         return false;
92                 }
93
94                 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
95                 if (res != LDAP_SUCCESS)
96                 {
97                         ldap_unbind_ext(conn, NULL, NULL);
98                         conn = NULL;
99                         return false;
100                 }
101                 return true;
102         }
103
104         virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
105         {
106                 if (hashtype == "ldap")
107                 {
108                         if (LookupOper(data, input))
109                                 /* This is an ldap oper and has been found, claim the OPER command */
110                                 return MOD_RES_ALLOW;
111                         else
112                                 return MOD_RES_DENY;
113                 }
114                 /* We don't know this oper! */
115                 return MOD_RES_PASSTHRU;
116         }
117
118         bool LookupOper(const std::string &what, const std::string &opassword)
119         {
120                 if (conn == NULL)
121                         if (!Connect())
122                                 return false;
123
124                 int res;
125                 char* authpass = strdup(password.c_str());
126                 // bind anonymously if no bind DN and authentication are given in the config
127                 struct berval cred;
128                 cred.bv_val = authpass;
129                 cred.bv_len = password.length();
130
131                 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
132                 {
133                         if (res == LDAP_SERVER_DOWN)
134                         {
135                                 // Attempt to reconnect if the connection dropped
136                                 ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
137                                 Connect();
138                                 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
139                         }
140
141                         if (res != LDAP_SUCCESS)
142                         {
143                                 free(authpass);
144                                 ldap_unbind_ext(conn, NULL, NULL);
145                                 conn = NULL;
146                                 return false;
147                         }
148                 }
149                 free(authpass);
150
151                 LDAPMessage *msg, *entry;
152                 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
153                 {
154                         return false;
155                 }
156                 if (ldap_count_entries(conn, msg) > 1)
157                 {
158                         ldap_msgfree(msg);
159                         return false;
160                 }
161                 if ((entry = ldap_first_entry(conn, msg)) == NULL)
162                 {
163                         ldap_msgfree(msg);
164                         return false;
165                 }
166                 authpass = strdup(opassword.c_str());
167                 cred.bv_val = authpass;
168                 cred.bv_len = opassword.length();
169                 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
170                 {
171                         free(authpass);
172                         ldap_msgfree(msg);
173                         return true;
174                 }
175                 else
176                 {
177                         free(authpass);
178                         ldap_msgfree(msg);
179                         return false;
180                 }
181         }
182
183         virtual Version GetVersion()
184         {
185                 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR);
186         }
187
188 };
189
190 MODULE_INIT(ModuleLDAPAuth)