2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2011 Pierre Carrier <pierre@spotify.com>
5 * Copyright (C) 2009-2010 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
7 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
8 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
9 * Copyright (C) 2008 Dennis Friis <peavey@inspircd.org>
10 * Copyright (C) 2007 Carsten Valdemar Munk <carsten.munk+inspircd@gmail.com>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 # pragma comment(lib, "ldap.lib")
35 # pragma comment(lib, "lber.lib")
38 /* $ModDesc: Allow/Deny connections based upon answer from LDAP server */
39 /* $LinkerFlags: -lldap */
41 class ModuleLDAPAuth : public Module
43 LocalIntExt ldapAuthed;
45 std::string attribute;
46 std::string ldapserver;
47 std::string allowpattern;
48 std::string killreason;
51 std::vector<std::string> whitelistedcidrs;
58 ModuleLDAPAuth() : ldapAuthed("ldapauth", this)
65 Implementation eventlist[] = { I_OnCheckReady, I_OnRehash, I_OnUserRegister };
66 ServerInstance->Modules->Attach(eventlist, this, 3);
73 ldap_unbind_ext(conn, NULL, NULL);
76 void OnRehash(User* user)
79 whitelistedcidrs.clear();
81 base = Conf.ReadValue("ldapauth", "baserdn", 0);
82 attribute = Conf.ReadValue("ldapauth", "attribute", 0);
83 ldapserver = Conf.ReadValue("ldapauth", "server", 0);
84 allowpattern = Conf.ReadValue("ldapauth", "allowpattern", 0);
85 killreason = Conf.ReadValue("ldapauth", "killreason", 0);
86 std::string scope = Conf.ReadValue("ldapauth", "searchscope", 0);
87 username = Conf.ReadValue("ldapauth", "binddn", 0);
88 password = Conf.ReadValue("ldapauth", "bindauth", 0);
89 verbose = Conf.ReadFlag("ldapauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
90 useusername = Conf.ReadFlag("ldapauth", "userfield", 0);
92 ConfigTagList whitelisttags = ServerInstance->Config->ConfTags("ldapwhitelist");
94 for (ConfigIter i = whitelisttags.first; i != whitelisttags.second; ++i)
96 std::string cidr = i->second->getString("cidr");
98 whitelistedcidrs.push_back(cidr);
103 searchscope = LDAP_SCOPE_BASE;
104 else if (scope == "onelevel")
105 searchscope = LDAP_SCOPE_ONELEVEL;
106 else searchscope = LDAP_SCOPE_SUBTREE;
114 ldap_unbind_ext(conn, NULL, NULL);
115 int res, v = LDAP_VERSION3;
116 res = ldap_initialize(&conn, ldapserver.c_str());
117 if (res != LDAP_SUCCESS)
120 ServerInstance->SNO->WriteToSnoMask('c', "LDAP connection failed: %s", ldap_err2string(res));
125 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
126 if (res != LDAP_SUCCESS)
129 ServerInstance->SNO->WriteToSnoMask('c', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
130 ldap_unbind_ext(conn, NULL, NULL);
137 ModResult OnUserRegister(LocalUser* user)
139 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
141 ldapAuthed.set(user,1);
142 return MOD_RES_PASSTHRU;
145 for (std::vector<std::string>::iterator i = whitelistedcidrs.begin(); i != whitelistedcidrs.end(); i++)
147 if (InspIRCd::MatchCIDR(user->GetIPString(), *i, ascii_case_insensitive_map))
149 ldapAuthed.set(user,1);
150 return MOD_RES_PASSTHRU;
154 if (!CheckCredentials(user))
156 ServerInstance->Users->QuitUser(user, killreason);
159 return MOD_RES_PASSTHRU;
162 bool CheckCredentials(LocalUser* user)
168 if (user->password.empty())
171 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (No password provided)", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
176 // bind anonymously if no bind DN and authentication are given in the config
178 cred.bv_val = const_cast<char*>(password.c_str());
179 cred.bv_len = password.length();
181 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
183 if (res == LDAP_SERVER_DOWN)
185 // Attempt to reconnect if the connection dropped
187 ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
189 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
192 if (res != LDAP_SUCCESS)
195 ServerInstance->SNO->WriteToSnoMask('c', "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));
196 ldap_unbind_ext(conn, NULL, NULL);
202 LDAPMessage *msg, *entry;
203 std::string what = (attribute + "=" + (useusername ? user->ident : user->nick));
204 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
206 // Do a second search, based on password, if it contains a :
207 // That is, PASS <user>:<password> will work.
208 size_t pos = user->password.find(":");
209 if (pos != std::string::npos)
211 std::string cutpassword = user->password.substr(0, pos);
212 res = ldap_search_ext_s(conn, base.c_str(), searchscope, cutpassword.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg);
216 // Trim the user: prefix, leaving just 'pass' for later password check
217 user->password = user->password.substr(pos + 1);
221 // It may have found based on user:pass check above.
225 ServerInstance->SNO->WriteToSnoMask('c', "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));
229 if (ldap_count_entries(conn, msg) > 1)
232 ServerInstance->SNO->WriteToSnoMask('c', "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));
236 if ((entry = ldap_first_entry(conn, msg)) == NULL)
239 ServerInstance->SNO->WriteToSnoMask('c', "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));
243 cred.bv_val = (char*)user->password.data();
244 cred.bv_len = user->password.length();
245 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
248 ldapAuthed.set(user,1);
254 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (%s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
260 ModResult OnCheckReady(LocalUser* user)
262 return ldapAuthed.get(user) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
267 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR);
272 MODULE_INIT(ModuleLDAPAuth)