]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ldapauth.cpp
603ea86385c2181ad6b1dceecd512f660a19abb7
[user/henk/code/inspircd.git] / src / modules / extra / m_ldapauth.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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, const std::string &parameter)
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('A', "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('A', "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 int OnUserRegister(User* user)
115         {
116                 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
117                 {
118                         user->Extend("ldapauthed");
119                         return 0;
120                 }
121
122                 if (!CheckCredentials(user))
123                 {
124                         ServerInstance->Users->QuitUser(user, killreason);
125                         return 1;
126                 }
127                 return 0;
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                         free(authpass);
146                         if (verbose)
147                                 ServerInstance->SNO->WriteToSnoMask('A', "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));
148                         ldap_unbind_ext(conn, NULL, NULL);
149                         conn = NULL;
150                         return false;
151                 }
152                 free(authpass);
153
154                 LDAPMessage *msg, *entry;
155                 std::string what = (attribute + "=" + (useusername ? user->ident : user->nick));
156                 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
157                 {
158                         if (verbose)
159                                 ServerInstance->SNO->WriteToSnoMask('A', "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));
160                         return false;
161                 }
162                 if (ldap_count_entries(conn, msg) > 1)
163                 {
164                         if (verbose)
165                                 ServerInstance->SNO->WriteToSnoMask('A', "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));
166                         ldap_msgfree(msg);
167                         return false;
168                 }
169                 if ((entry = ldap_first_entry(conn, msg)) == NULL)
170                 {
171                         if (verbose)
172                                 ServerInstance->SNO->WriteToSnoMask('A', "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));
173                         ldap_msgfree(msg);
174                         return false;
175                 }
176                 if (user->password.empty())
177                 {
178                         if (verbose)
179                                 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (No password provided)", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
180                         user->Extend("ldapauth_failed");
181                         return false;
182                 }
183                 cred.bv_val = (char*)user->password.data();
184                 cred.bv_len = user->password.length();
185                 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
186                 {
187                         ldap_msgfree(msg);
188                         user->Extend("ldapauthed");
189                         return true;
190                 }
191                 else
192                 {
193                         if (verbose)
194                                 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (%s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
195                         ldap_msgfree(msg);
196                         user->Extend("ldapauth_failed");
197                         return false;
198                 }
199         }
200
201
202         virtual void OnUserDisconnect(User* user)
203         {
204                 user->Shrink("ldapauthed");
205                 user->Shrink("ldapauth_failed");
206         }
207
208         virtual bool OnCheckReady(User* user)
209         {
210                 return user->GetExt("ldapauthed");
211         }
212
213         virtual Version GetVersion()
214         {
215                 return Version("$Id$", VF_VENDOR, API_VERSION);
216         }
217
218 };
219
220 MODULE_INIT(ModuleLDAPAuth)