2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013 Adam <Adam@anope.org>
5 * Copyright (C) 2009 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
8 * Copyright (C) 2007 Carsten Valdemar Munk <carsten.munk+inspircd@gmail.com>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "modules/ldap.h"
32 class LDAPOperBase : public LDAPInterface
35 const std::string uid;
36 const std::string opername;
37 const std::string password;
39 void Fallback(User* user)
44 Command* oper_command = ServerInstance->Parser->GetHandler("OPER");
48 std::vector<std::string> params;
49 params.push_back(opername);
50 params.push_back(password);
51 oper_command->Handle(params, user);
56 User* user = ServerInstance->FindUUID(uid);
61 LDAPOperBase(Module* mod, const std::string& uuid, const std::string& oper, const std::string& pass)
63 , uid(uuid), opername(oper), password(pass)
67 void OnError(const LDAPResult& err) CXX11_OVERRIDE
69 ServerInstance->SNO->WriteToSnoMask('a', "Error searching LDAP server: %s", err.getError().c_str());
75 class BindInterface : public LDAPOperBase
78 BindInterface(Module* mod, const std::string& uuid, const std::string& oper, const std::string& pass)
79 : LDAPOperBase(mod, uuid, oper, pass)
83 void OnResult(const LDAPResult& r) CXX11_OVERRIDE
85 User* user = ServerInstance->FindUUID(uid);
86 OperIndex::iterator iter = ServerInstance->Config->oper_blocks.find(opername);
88 if (!user || iter == ServerInstance->Config->oper_blocks.end())
95 OperInfo* ifo = iter->second;
101 class SearchInterface : public LDAPOperBase
103 const std::string provider;
105 bool HandleResult(const LDAPResult& result)
107 dynamic_reference<LDAPProvider> LDAP(me, provider);
108 if (!LDAP || result.empty())
113 const LDAPAttributes& attr = result.get(0);
114 std::string bindDn = attr.get("dn");
118 LDAP->Bind(new BindInterface(this->creator, uid, opername, password), bindDn, password);
120 catch (LDAPException& ex)
122 ServerInstance->SNO->WriteToSnoMask('a', "Error searching LDAP server: " + ex.GetReason());
129 SearchInterface(Module* mod, const std::string& prov, User* user, const std::string& oper, const std::string& pass)
130 : LDAPOperBase(mod, user->uuid, oper, pass)
135 void OnResult(const LDAPResult& result) CXX11_OVERRIDE
137 if (!HandleResult(result))
143 class ModuleLDAPAuth : public Module
145 dynamic_reference<LDAPProvider> LDAP;
147 std::string attribute;
156 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
158 ConfigTag* tag = ServerInstance->Config->ConfValue("ldapoper");
160 LDAP.SetProvider("LDAP/" + tag->getString("dbid"));
161 base = tag->getString("baserdn");
162 attribute = tag->getString("attribute");
165 ModResult OnPreCommand(std::string& command, std::vector<std::string>& parameters, LocalUser* user, bool validated, const std::string& original_line) CXX11_OVERRIDE
167 if (validated && command == "OPER" && parameters.size() >= 2)
169 const std::string& opername = parameters[0];
170 const std::string& password = parameters[1];
172 OperIndex::iterator it = ServerInstance->Config->oper_blocks.find(opername);
173 if (it == ServerInstance->Config->oper_blocks.end())
174 return MOD_RES_PASSTHRU;
176 ConfigTag* tag = it->second->oper_block;
178 return MOD_RES_PASSTHRU;
180 std::string acceptedhosts = tag->getString("host");
181 std::string hostname = user->ident + "@" + user->host;
182 if (!InspIRCd::MatchMask(acceptedhosts, hostname, user->GetIPString()))
183 return MOD_RES_PASSTHRU;
186 return MOD_RES_PASSTHRU;
190 // First, bind as the manager so the following search will go through
191 LDAP->BindAsManager(NULL);
193 // Fire off the search
194 std::string what = attribute + "=" + opername;
195 LDAP->Search(new SearchInterface(this, LDAP.GetProvider(), user, opername, password), base, what);
198 catch (LDAPException& ex)
200 ServerInstance->SNO->WriteToSnoMask('a', "LDAP exception: " + ex.GetReason());
204 return MOD_RES_PASSTHRU;
207 Version GetVersion() CXX11_OVERRIDE
209 return Version("Adds the ability to authenticate opers via LDAP", VF_VENDOR);
213 MODULE_INIT(ModuleLDAPAuth)