]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ldapauth.cpp
clean this up now Brain says it works :)
[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         int searchscope;
42         bool verbose;
43         LDAP *conn;
44         
45 public:
46         ModuleLDAPAuth(InspIRCd* Me)
47         : Module::Module(Me)
48         {
49                 conn = NULL;
50                 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRehash, I_OnUserRegister };
51                 ServerInstance->Modules->Attach(eventlist, this, 4);
52                 OnRehash(NULL,"");
53         }
54
55         virtual ~ModuleLDAPAuth()
56         {
57                 if (conn)
58                         ldap_unbind_ext(conn, NULL, NULL);
59         }
60
61         virtual void OnRehash(User* user, const std::string &parameter)
62         {
63                 ConfigReader Conf(ServerInstance);
64                 
65                 base            = Conf.ReadValue("ldapauth", "baserdn", 0);
66                 attribute       = Conf.ReadValue("ldapauth", "attribute", 0); 
67                 ldapserver      = Conf.ReadValue("ldapauth", "server", 0);
68                 allowpattern    = Conf.ReadValue("ldapauth", "allowpattern", 0);
69                 killreason      = Conf.ReadValue("ldapauth", "killreason", 0);
70                 std::string scope       = Conf.ReadValue("ldapauth", "searchscope", 0);
71                 verbose         = Conf.ReadFlag("ldapauth", "verbose", 0);              /* Set to true if failed connects should be reported to operators */
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                         if (verbose)
91                                 ServerInstance->SNO->WriteToSnoMask('A', "LDAP connection failed: %s", ldap_err2string(res));
92                         conn = NULL;
93                         return false;                   
94                 }
95                 
96                 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
97                 if (res != LDAP_SUCCESS)
98                 {
99                         if (verbose)
100                                 ServerInstance->SNO->WriteToSnoMask('A', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
101                         ldap_unbind_ext(conn, NULL, NULL);
102                         conn = NULL;
103                         return false;
104                 }
105                 return true;
106         }
107
108         virtual int OnUserRegister(User* user)
109         {
110                 if ((!allowpattern.empty()) && (ServerInstance->MatchText(user->nick,allowpattern)))
111                 {
112                         user->Extend("ldapauthed");
113                         return 0;
114                 }
115
116                 if (!CheckCredentials(user))
117                 {
118                         User::QuitUser(ServerInstance,user,killreason);
119                         return 1;
120                 }
121                 return 0;
122         }
123
124         bool CheckCredentials(User* user)
125         {
126                 if (conn == NULL)
127                         if (!Connect())
128                                 return false;
129
130                 int res;
131                 // bind anonymously
132                 struct berval cred; cred.bv_val = ""; cred.bv_len = 0;
133                 if ((res = ldap_sasl_bind_s(conn, "", LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
134                 {       
135                         if (verbose)
136                                 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP bind anonymously failed: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
137                         ldap_unbind_ext(conn, NULL, NULL);
138                         conn = NULL;
139                         return false;
140                 }
141                 LDAPMessage *msg, *entry;
142                 std::string what = (attribute + "=" + user->nick);
143                 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
144                 {
145                         if (verbose)
146                                 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP search failed: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
147                         return false;
148                 }
149                 if (ldap_count_entries(conn, msg) > 1)
150                 {
151                         if (verbose)
152                                 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP search returned more than one result: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
153                         ldap_msgfree(msg);
154                         return false;
155                 }
156                 if ((entry = ldap_first_entry(conn, msg)) == NULL)
157                 {
158                         if (verbose)
159                                 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP search returned no results: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
160                         ldap_msgfree(msg);
161                         return false;
162                 }
163                 cred.bv_val = user->password; cred.bv_len = strlen(user->password);
164                 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
165                 {
166                         ldap_msgfree(msg);
167                         user->Extend("ldapauthed");
168                         return true;
169                 }
170                 else
171                 {
172                         if (verbose)
173                                 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (%s)", user->nick, user->ident, user->host, ldap_err2string(res));
174                         ldap_msgfree(msg);
175                         user->Extend("ldapauth_failed");
176                         return false;
177                 } 
178         }
179         
180         
181         virtual void OnUserDisconnect(User* user)
182         {
183                 user->Shrink("ldapauthed");
184                 user->Shrink("ldapauth_failed");                
185         }
186         
187         virtual bool OnCheckReady(User* user)
188         {
189                 return user->GetExt("ldapauthed");
190         }
191
192         virtual Version GetVersion()
193         {
194                 return Version(1,2,0,0,VF_VENDOR,API_VERSION);
195         }
196         
197 };
198
199 MODULE_INIT(ModuleLDAPAuth)