]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ldapauth.cpp
af676de44d27bede5fcbc6dd60c567c902d1c52b
[user/henk/code/inspircd.git] / src / modules / extra / m_ldapauth.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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 /* $ModDesc: Allow/Deny connections based upon answer from LDAP server */
32 /* $LinkerFlags: -lldap */
33
34 class ModuleLDAPAuth : public Module
35 {
36         LocalIntExt ldapAuthed;
37         std::string base;
38         std::string attribute;
39         std::string ldapserver;
40         std::string allowpattern;
41         std::string killreason;
42         std::string username;
43         std::string password;
44         int searchscope;
45         bool verbose;
46         bool useusername;
47         LDAP *conn;
48
49 public:
50         ModuleLDAPAuth() : ldapAuthed("ldapauth", this)
51         {
52                 conn = NULL;
53                 Implementation eventlist[] = { I_OnCheckReady, I_OnRehash, I_OnUserRegister };
54                 ServerInstance->Modules->Attach(eventlist, this, 4);
55                 OnRehash(NULL);
56         }
57
58         ~ModuleLDAPAuth()
59         {
60                 if (conn)
61                         ldap_unbind_ext(conn, NULL, NULL);
62         }
63
64         void OnRehash(User* user)
65         {
66                 ConfigReader Conf;
67
68                 base                    = Conf.ReadValue("ldapauth", "baserdn", 0);
69                 attribute               = Conf.ReadValue("ldapauth", "attribute", 0);
70                 ldapserver              = Conf.ReadValue("ldapauth", "server", 0);
71                 allowpattern            = Conf.ReadValue("ldapauth", "allowpattern", 0);
72                 killreason              = Conf.ReadValue("ldapauth", "killreason", 0);
73                 std::string scope       = Conf.ReadValue("ldapauth", "searchscope", 0);
74                 username                = Conf.ReadValue("ldapauth", "binddn", 0);
75                 password                = Conf.ReadValue("ldapauth", "bindauth", 0);
76                 verbose                 = Conf.ReadFlag("ldapauth", "verbose", 0);              /* Set to true if failed connects should be reported to operators */
77                 useusername             = Conf.ReadFlag("ldapauth", "userfield", 0);
78
79                 if (scope == "base")
80                         searchscope = LDAP_SCOPE_BASE;
81                 else if (scope == "onelevel")
82                         searchscope = LDAP_SCOPE_ONELEVEL;
83                 else searchscope = LDAP_SCOPE_SUBTREE;
84
85                 Connect();
86         }
87
88         bool Connect()
89         {
90                 if (conn != NULL)
91                         ldap_unbind_ext(conn, NULL, NULL);
92                 int res, v = LDAP_VERSION3;
93                 res = ldap_initialize(&conn, ldapserver.c_str());
94                 if (res != LDAP_SUCCESS)
95                 {
96                         if (verbose)
97                                 ServerInstance->SNO->WriteToSnoMask('c', "LDAP connection failed: %s", ldap_err2string(res));
98                         conn = NULL;
99                         return false;
100                 }
101
102                 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
103                 if (res != LDAP_SUCCESS)
104                 {
105                         if (verbose)
106                                 ServerInstance->SNO->WriteToSnoMask('c', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
107                         ldap_unbind_ext(conn, NULL, NULL);
108                         conn = NULL;
109                         return false;
110                 }
111                 return true;
112         }
113
114         ModResult OnUserRegister(LocalUser* user)
115         {
116                 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
117                 {
118                         ldapAuthed.set(user,1);
119                         return MOD_RES_PASSTHRU;
120                 }
121
122                 if (!CheckCredentials(user))
123                 {
124                         ServerInstance->Users->QuitUser(user, killreason);
125                         return MOD_RES_DENY;
126                 }
127                 return MOD_RES_PASSTHRU;
128         }
129
130         bool CheckCredentials(LocalUser* user)
131         {
132                 if (conn == NULL)
133                         if (!Connect())
134                                 return false;
135
136                 int res;
137                 // bind anonymously if no bind DN and authentication are given in the config
138                 struct berval cred;
139                 cred.bv_val = const_cast<char*>(password.c_str());
140                 cred.bv_len = password.length();
141
142                 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
143                 {
144                         if (res == LDAP_SERVER_DOWN)
145                         {
146                                 // Attempt to reconnect if the connection dropped
147                                 if (verbose)
148                                         ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
149                                 Connect();
150                                 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
151                         }
152
153                         if (res != LDAP_SUCCESS)
154                         {
155                                 if (verbose)
156                                         ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (LDAP bind failed: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
157                                 ldap_unbind_ext(conn, NULL, NULL);
158                                 conn = NULL;
159                                 return false;
160                         }
161                 }
162
163                 LDAPMessage *msg, *entry;
164                 std::string what = (attribute + "=" + (useusername ? user->ident : user->nick));
165                 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
166                 {
167                         if (verbose)
168                                 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (LDAP search failed: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
169                         return false;
170                 }
171                 if (ldap_count_entries(conn, msg) > 1)
172                 {
173                         if (verbose)
174                                 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (LDAP search returned more than one result: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
175                         ldap_msgfree(msg);
176                         return false;
177                 }
178                 if ((entry = ldap_first_entry(conn, msg)) == NULL)
179                 {
180                         if (verbose)
181                                 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (LDAP search returned no results: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
182                         ldap_msgfree(msg);
183                         return false;
184                 }
185                 if (user->password.empty())
186                 {
187                         if (verbose)
188                                 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (No password provided)", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
189                         return false;
190                 }
191                 cred.bv_val = (char*)user->password.data();
192                 cred.bv_len = user->password.length();
193                 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
194                 {
195                         ldap_msgfree(msg);
196                         ldapAuthed.set(user,1);
197                         return true;
198                 }
199                 else
200                 {
201                         if (verbose)
202                                 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (%s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
203                         ldap_msgfree(msg);
204                         return false;
205                 }
206         }
207
208         ModResult OnCheckReady(LocalUser* user)
209         {
210                 return ldapAuthed.get(user) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
211         }
212
213         Version GetVersion()
214         {
215                 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR);
216         }
217
218 };
219
220 MODULE_INIT(ModuleLDAPAuth)