]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ldapauth.cpp
Remove InspIRCd* parameters and fields
[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()
50                 {
51                 conn = NULL;
52                 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRehash, I_OnUserRegister };
53                 ServerInstance->Modules->Attach(eventlist, this, 4);
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("ldapauth", "baserdn", 0);
68                 attribute               = Conf.ReadValue("ldapauth", "attribute", 0);
69                 ldapserver              = Conf.ReadValue("ldapauth", "server", 0);
70                 allowpattern            = Conf.ReadValue("ldapauth", "allowpattern", 0);
71                 killreason              = Conf.ReadValue("ldapauth", "killreason", 0);
72                 std::string scope       = Conf.ReadValue("ldapauth", "searchscope", 0);
73                 username                = Conf.ReadValue("ldapauth", "binddn", 0);
74                 password                = Conf.ReadValue("ldapauth", "bindauth", 0);
75                 verbose                 = Conf.ReadFlag("ldapauth", "verbose", 0);              /* Set to true if failed connects should be reported to operators */
76                 useusername             = Conf.ReadFlag("ldapauth", "userfield", 0);
77
78                 if (scope == "base")
79                         searchscope = LDAP_SCOPE_BASE;
80                 else if (scope == "onelevel")
81                         searchscope = LDAP_SCOPE_ONELEVEL;
82                 else searchscope = LDAP_SCOPE_SUBTREE;
83
84                 Connect();
85         }
86
87         bool Connect()
88         {
89                 if (conn != NULL)
90                         ldap_unbind_ext(conn, NULL, NULL);
91                 int res, v = LDAP_VERSION3;
92                 res = ldap_initialize(&conn, ldapserver.c_str());
93                 if (res != LDAP_SUCCESS)
94                 {
95                         if (verbose)
96                                 ServerInstance->SNO->WriteToSnoMask('c', "LDAP connection failed: %s", ldap_err2string(res));
97                         conn = NULL;
98                         return false;
99                 }
100
101                 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
102                 if (res != LDAP_SUCCESS)
103                 {
104                         if (verbose)
105                                 ServerInstance->SNO->WriteToSnoMask('c', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
106                         ldap_unbind_ext(conn, NULL, NULL);
107                         conn = NULL;
108                         return false;
109                 }
110                 return true;
111         }
112
113         virtual ModResult OnUserRegister(User* user)
114         {
115                 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
116                 {
117                         user->Extend("ldapauthed");
118                         return MOD_RES_PASSTHRU;
119                 }
120
121                 if (!CheckCredentials(user))
122                 {
123                         ServerInstance->Users->QuitUser(user, killreason);
124                         return MOD_RES_DENY;
125                 }
126                 return MOD_RES_PASSTHRU;
127         }
128
129         bool CheckCredentials(User* user)
130         {
131                 if (conn == NULL)
132                         if (!Connect())
133                                 return false;
134
135                 int res;
136                 char* authpass = strdup(password.c_str());
137                 // bind anonymously if no bind DN and authentication are given in the config
138                 struct berval cred;
139                 cred.bv_val = authpass;
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                                 free(authpass);
158                                 ldap_unbind_ext(conn, NULL, NULL);
159                                 conn = NULL;
160                                 return false;
161                         }
162                 }
163                 free(authpass);
164
165                 LDAPMessage *msg, *entry;
166                 std::string what = (attribute + "=" + (useusername ? user->ident : user->nick));
167                 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
168                 {
169                         if (verbose)
170                                 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));
171                         return false;
172                 }
173                 if (ldap_count_entries(conn, msg) > 1)
174                 {
175                         if (verbose)
176                                 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));
177                         ldap_msgfree(msg);
178                         return false;
179                 }
180                 if ((entry = ldap_first_entry(conn, msg)) == NULL)
181                 {
182                         if (verbose)
183                                 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));
184                         ldap_msgfree(msg);
185                         return false;
186                 }
187                 if (user->password.empty())
188                 {
189                         if (verbose)
190                                 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());
191                         user->Extend("ldapauth_failed");
192                         return false;
193                 }
194                 cred.bv_val = (char*)user->password.data();
195                 cred.bv_len = user->password.length();
196                 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
197                 {
198                         ldap_msgfree(msg);
199                         user->Extend("ldapauthed");
200                         return true;
201                 }
202                 else
203                 {
204                         if (verbose)
205                                 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));
206                         ldap_msgfree(msg);
207                         user->Extend("ldapauth_failed");
208                         return false;
209                 }
210         }
211
212
213         virtual void OnUserDisconnect(User* user)
214         {
215                 user->Shrink("ldapauthed");
216                 user->Shrink("ldapauth_failed");
217         }
218
219         virtual ModResult OnCheckReady(User* user)
220         {
221                 return user->GetExt("ldapauthed") ? MOD_RES_PASSTHRU : MOD_RES_DENY;
222         }
223
224         virtual Version GetVersion()
225         {
226                 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR, API_VERSION);
227         }
228
229 };
230
231 MODULE_INIT(ModuleLDAPAuth)