]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_ldapoper.cpp
45f03aa8ef6c6df4e5c441c554af1377996583af
[user/henk/code/inspircd.git] / src / modules / extra / m_ldapoper.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 ldapserver;
38         std::string username;
39         std::string password;
40         int searchscope;
41         LDAP *conn;
42
43 public:
44         ModuleLDAPAuth()
45                 {
46                 conn = NULL;
47                 Implementation eventlist[] = { I_OnRehash, I_OnPassCompare };
48                 ServerInstance->Modules->Attach(eventlist, this, 2);
49                 OnRehash(NULL);
50         }
51
52         virtual ~ModuleLDAPAuth()
53         {
54                 if (conn)
55                         ldap_unbind_ext(conn, NULL, NULL);
56         }
57
58         virtual void OnRehash(User* user)
59         {
60                 ConfigReader Conf;
61
62                 base                    = Conf.ReadValue("ldapoper", "baserdn", 0);
63                 ldapserver              = Conf.ReadValue("ldapoper", "server", 0);
64                 std::string scope       = Conf.ReadValue("ldapoper", "searchscope", 0);
65                 username                = Conf.ReadValue("ldapoper", "binddn", 0);
66                 password                = Conf.ReadValue("ldapoper", "bindauth", 0);
67
68                 if (scope == "base")
69                         searchscope = LDAP_SCOPE_BASE;
70                 else if (scope == "onelevel")
71                         searchscope = LDAP_SCOPE_ONELEVEL;
72                 else searchscope = LDAP_SCOPE_SUBTREE;
73
74                 Connect();
75         }
76
77         bool Connect()
78         {
79                 if (conn != NULL)
80                         ldap_unbind_ext(conn, NULL, NULL);
81                 int res, v = LDAP_VERSION3;
82                 res = ldap_initialize(&conn, ldapserver.c_str());
83                 if (res != LDAP_SUCCESS)
84                 {
85                         conn = NULL;
86                         return false;
87                 }
88
89                 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
90                 if (res != LDAP_SUCCESS)
91                 {
92                         ldap_unbind_ext(conn, NULL, NULL);
93                         conn = NULL;
94                         return false;
95                 }
96                 return true;
97         }
98
99         virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
100         {
101                 if (hashtype == "ldap")
102                 {
103                         if (LookupOper(data, input))
104                                 /* This is an ldap oper and has been found, claim the OPER command */
105                                 return MOD_RES_ALLOW;
106                         else
107                                 return MOD_RES_DENY;
108                 }
109                 /* We don't know this oper! */
110                 return MOD_RES_PASSTHRU;
111         }
112
113         bool LookupOper(const std::string &what, const std::string &opassword)
114         {
115                 if (conn == NULL)
116                         if (!Connect())
117                                 return false;
118
119                 int res;
120                 char* authpass = strdup(password.c_str());
121                 // bind anonymously if no bind DN and authentication are given in the config
122                 struct berval cred;
123                 cred.bv_val = authpass;
124                 cred.bv_len = password.length();
125
126                 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
127                 {
128                         if (res == LDAP_SERVER_DOWN)
129                         {
130                                 // Attempt to reconnect if the connection dropped
131                                 ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
132                                 Connect();
133                                 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
134                         }
135
136                         if (res != LDAP_SUCCESS)
137                         {
138                                 free(authpass);
139                                 ldap_unbind_ext(conn, NULL, NULL);
140                                 conn = NULL;
141                                 return false;
142                         }
143                 }
144                 free(authpass);
145
146                 LDAPMessage *msg, *entry;
147                 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
148                 {
149                         return false;
150                 }
151                 if (ldap_count_entries(conn, msg) > 1)
152                 {
153                         ldap_msgfree(msg);
154                         return false;
155                 }
156                 if ((entry = ldap_first_entry(conn, msg)) == NULL)
157                 {
158                         ldap_msgfree(msg);
159                         return false;
160                 }
161                 authpass = strdup(opassword.c_str());
162                 cred.bv_val = authpass;
163                 cred.bv_len = opassword.length();
164                 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
165                 {
166                         free(authpass);
167                         ldap_msgfree(msg);
168                         return true;
169                 }
170                 else
171                 {
172                         free(authpass);
173                         ldap_msgfree(msg);
174                         return false;
175                 }
176         }
177
178         virtual Version GetVersion()
179         {
180                 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR);
181         }
182
183 };
184
185 MODULE_INIT(ModuleLDAPAuth)