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