]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ldapauth.cpp
45202e6fa708c91965a137c6bd3b667a7f3a5264
[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         std::string base;
37         std::string attribute;
38         std::string ldapserver;
39         std::string allowpattern;
40         std::string killreason;
41         std::string username;
42         std::string password;
43         int searchscope;
44         bool verbose;
45         bool useusername;
46         LDAP *conn;
47
48 public:
49         ModuleLDAPAuth(InspIRCd* Me)
50         : Module(Me)
51         {
52                 conn = NULL;
53                 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRehash, I_OnUserRegister };
54                 ServerInstance->Modules->Attach(eventlist, this, 4);
55                 OnRehash(NULL);
56         }
57
58         virtual ~ModuleLDAPAuth()
59         {
60                 if (conn)
61                         ldap_unbind_ext(conn, NULL, NULL);
62         }
63
64         virtual void OnRehash(User* user)
65         {
66                 ConfigReader Conf(ServerInstance);
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         virtual ModResult OnUserRegister(User* user)
115         {
116                 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
117                 {
118                         user->Extend("ldapauthed");
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(User* user)
131         {
132                 if (conn == NULL)
133                         if (!Connect())
134                                 return false;
135
136                 int res;
137                 char* authpass = strdup(password.c_str());
138                 // bind anonymously if no bind DN and authentication are given in the config
139                 struct berval cred;
140                 cred.bv_val = authpass;
141                 cred.bv_len = password.length();
142
143                 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
144                 {
145                         if (res == LDAP_SERVER_DOWN)
146                         {
147                                 // Attempt to reconnect if the connection dropped
148                                 if (verbose)
149                                         ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
150                                 Connect();
151                                 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
152                         }
153
154                         if (res != LDAP_SUCCESS)
155                         {
156                                 if (verbose)
157                                         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));
158                                 free(authpass);
159                                 ldap_unbind_ext(conn, NULL, NULL);
160                                 conn = NULL;
161                                 return false;
162                         }
163                 }
164                 free(authpass);
165
166                 LDAPMessage *msg, *entry;
167                 std::string what = (attribute + "=" + (useusername ? user->ident : user->nick));
168                 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
169                 {
170                         if (verbose)
171                                 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));
172                         return false;
173                 }
174                 if (ldap_count_entries(conn, msg) > 1)
175                 {
176                         if (verbose)
177                                 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));
178                         ldap_msgfree(msg);
179                         return false;
180                 }
181                 if ((entry = ldap_first_entry(conn, msg)) == NULL)
182                 {
183                         if (verbose)
184                                 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));
185                         ldap_msgfree(msg);
186                         return false;
187                 }
188                 if (user->password.empty())
189                 {
190                         if (verbose)
191                                 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());
192                         user->Extend("ldapauth_failed");
193                         return false;
194                 }
195                 cred.bv_val = (char*)user->password.data();
196                 cred.bv_len = user->password.length();
197                 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
198                 {
199                         ldap_msgfree(msg);
200                         user->Extend("ldapauthed");
201                         return true;
202                 }
203                 else
204                 {
205                         if (verbose)
206                                 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));
207                         ldap_msgfree(msg);
208                         user->Extend("ldapauth_failed");
209                         return false;
210                 }
211         }
212
213
214         virtual void OnUserDisconnect(User* user)
215         {
216                 user->Shrink("ldapauthed");
217                 user->Shrink("ldapauth_failed");
218         }
219
220         virtual ModResult OnCheckReady(User* user)
221         {
222                 return user->GetExt("ldapauthed") ? MOD_RES_PASSTHRU : MOD_RES_DENY;
223         }
224
225         virtual Version GetVersion()
226         {
227                 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR, API_VERSION);
228         }
229
230 };
231
232 MODULE_INIT(ModuleLDAPAuth)