]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ldapauth.cpp
The rest of the server protocol interface and fix a warning in m_rline
[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         LDAP *conn;
46         
47 public:
48         ModuleLDAPAuth(InspIRCd* Me)
49         : Module::Module(Me)
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, const std::string &parameter)
64         {
65                 ConfigReader Conf(ServerInstance);
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                 
77                 if (scope == "base")
78                         searchscope = LDAP_SCOPE_BASE;
79                 else if (scope == "onelevel")
80                         searchscope = LDAP_SCOPE_ONELEVEL;
81                 else searchscope = LDAP_SCOPE_SUBTREE;
82                 
83                 Connect();
84         }
85
86         bool Connect()
87         {
88                 if (conn != NULL)
89                         ldap_unbind_ext(conn, NULL, NULL);
90                 int res, v = LDAP_VERSION3;
91                 res = ldap_initialize(&conn, ldapserver.c_str());
92                 if (res != LDAP_SUCCESS)
93                 {
94                         if (verbose)
95                                 ServerInstance->SNO->WriteToSnoMask('A', "LDAP connection failed: %s", ldap_err2string(res));
96                         conn = NULL;
97                         return false;                   
98                 }
99                 
100                 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
101                 if (res != LDAP_SUCCESS)
102                 {
103                         if (verbose)
104                                 ServerInstance->SNO->WriteToSnoMask('A', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
105                         ldap_unbind_ext(conn, NULL, NULL);
106                         conn = NULL;
107                         return false;
108                 }
109                 return true;
110         }
111
112         virtual int OnUserRegister(User* user)
113         {
114                 if ((!allowpattern.empty()) && (ServerInstance->MatchText(user->nick,allowpattern)))
115                 {
116                         user->Extend("ldapauthed");
117                         return 0;
118                 }
119
120                 if (!CheckCredentials(user))
121                 {
122                         User::QuitUser(ServerInstance,user,killreason);
123                         return 1;
124                 }
125                 return 0;
126         }
127
128         bool CheckCredentials(User* user)
129         {
130                 if (conn == NULL)
131                         if (!Connect())
132                                 return false;
133
134                 int res;
135                 char* authpass = strdup(password.c_str());
136                 // bind anonymously if no bind DN and authentication are given in the config
137                 struct berval cred;
138                 cred.bv_val = authpass;
139                 cred.bv_len = password.length();
140
141                 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
142                 {
143                         free(authpass);
144                         if (verbose)
145                                 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP bind failed: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
146                         ldap_unbind_ext(conn, NULL, NULL);
147                         conn = NULL;
148                         return false;
149                 }
150                 free(authpass);
151
152                 LDAPMessage *msg, *entry;
153                 std::string what = (attribute + "=" + user->nick);
154                 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
155                 {
156                         if (verbose)
157                                 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP search failed: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
158                         return false;
159                 }
160                 if (ldap_count_entries(conn, msg) > 1)
161                 {
162                         if (verbose)
163                                 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));
164                         ldap_msgfree(msg);
165                         return false;
166                 }
167                 if ((entry = ldap_first_entry(conn, msg)) == NULL)
168                 {
169                         if (verbose)
170                                 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));
171                         ldap_msgfree(msg);
172                         return false;
173                 }
174                 cred.bv_val = user->password;
175                 cred.bv_len = strlen(user->password);
176                 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
177                 {
178                         ldap_msgfree(msg);
179                         user->Extend("ldapauthed");
180                         return true;
181                 }
182                 else
183                 {
184                         if (verbose)
185                                 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (%s)", user->nick, user->ident, user->host, ldap_err2string(res));
186                         ldap_msgfree(msg);
187                         user->Extend("ldapauth_failed");
188                         return false;
189                 } 
190         }
191         
192         
193         virtual void OnUserDisconnect(User* user)
194         {
195                 user->Shrink("ldapauthed");
196                 user->Shrink("ldapauth_failed");                
197         }
198         
199         virtual bool OnCheckReady(User* user)
200         {
201                 return user->GetExt("ldapauthed");
202         }
203
204         virtual Version GetVersion()
205         {
206                 return Version(1,2,0,0,VF_VENDOR,API_VERSION);
207         }
208         
209 };
210
211 MODULE_INIT(ModuleLDAPAuth)