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 ServerConfig::OperIndex::const_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, const std::string &uuid, const std::string& oper, const std::string& pass)
130 : LDAPOperBase(mod, uuid, oper, pass)
135 void OnResult(const LDAPResult& result) CXX11_OVERRIDE
137 if (!HandleResult(result))
143 class AdminBindInterface : public LDAPInterface
145 const std::string provider;
146 const std::string user;
147 const std::string opername;
148 const std::string password;
149 const std::string base;
150 const std::string what;
153 AdminBindInterface(Module* c, const std::string& p, const std::string& u, const std::string& o, const std::string& pa, const std::string& b, const std::string& w)
154 : LDAPInterface(c), provider(p), user(u), opername(p), password(pa), base(b), what(w)
158 void OnResult(const LDAPResult& r) CXX11_OVERRIDE
160 dynamic_reference<LDAPProvider> LDAP(me, provider);
165 LDAP->Search(new SearchInterface(this->creator, provider, user, opername, password), base, what);
167 catch (LDAPException& ex)
169 ServerInstance->SNO->WriteToSnoMask('a', "Error searching LDAP server: " + ex.GetReason());
175 void OnError(const LDAPResult& err) CXX11_OVERRIDE
177 ServerInstance->SNO->WriteToSnoMask('a', "Error binding as manager to LDAP server: " + err.getError());
182 class ModuleLDAPAuth : public Module
184 dynamic_reference<LDAPProvider> LDAP;
186 std::string attribute;
195 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
197 ConfigTag* tag = ServerInstance->Config->ConfValue("ldapoper");
199 LDAP.SetProvider("LDAP/" + tag->getString("dbid"));
200 base = tag->getString("baserdn");
201 attribute = tag->getString("attribute");
204 ModResult OnPreCommand(std::string& command, std::vector<std::string>& parameters, LocalUser* user, bool validated, const std::string& original_line) CXX11_OVERRIDE
206 if (validated && command == "OPER" && parameters.size() >= 2)
208 const std::string& opername = parameters[0];
209 const std::string& password = parameters[1];
211 ServerConfig::OperIndex::const_iterator it = ServerInstance->Config->oper_blocks.find(opername);
212 if (it == ServerInstance->Config->oper_blocks.end())
213 return MOD_RES_PASSTHRU;
215 ConfigTag* tag = it->second->oper_block;
217 return MOD_RES_PASSTHRU;
219 std::string acceptedhosts = tag->getString("host");
220 std::string hostname = user->ident + "@" + user->host;
221 if (!InspIRCd::MatchMask(acceptedhosts, hostname, user->GetIPString()))
222 return MOD_RES_PASSTHRU;
225 return MOD_RES_PASSTHRU;
229 std::string what = attribute + "=" + opername;
230 LDAP->BindAsManager(new AdminBindInterface(this, LDAP.GetProvider(), user->uuid, opername, password, base, what));
233 catch (LDAPException& ex)
235 ServerInstance->SNO->WriteToSnoMask('a', "LDAP exception: " + ex.GetReason());
239 return MOD_RES_PASSTHRU;
242 Version GetVersion() CXX11_OVERRIDE
244 return Version("Adds the ability to authenticate opers via LDAP", VF_VENDOR);
248 MODULE_INIT(ModuleLDAPAuth)