2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2020 Christos Triantafyllidis <ctria@users.noreply.github.com>
5 * Copyright (C) 2018-2020 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2014, 2018 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2013-2014 Adam <Adam@anope.org>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "modules/ldap.h"
31 class LDAPOperBase : public LDAPInterface
34 const std::string uid;
35 const std::string opername;
36 const std::string password;
38 void Fallback(User* user)
43 Command* oper_command = ServerInstance->Parser.GetHandler("OPER");
47 CommandBase::Params params;
48 params.push_back(opername);
49 params.push_back(password);
50 ClientProtocol::TagMap tags;
51 oper_command->Handle(user, CommandBase::Params(params, tags));
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)
164 void OnResult(const LDAPResult& r) CXX11_OVERRIDE
166 dynamic_reference<LDAPProvider> LDAP(me, provider);
171 LDAP->Search(new SearchInterface(this->creator, provider, user, opername, password), base, what);
173 catch (LDAPException& ex)
175 ServerInstance->SNO->WriteToSnoMask('a', "Error searching LDAP server: " + ex.GetReason());
181 void OnError(const LDAPResult& err) CXX11_OVERRIDE
183 ServerInstance->SNO->WriteToSnoMask('a', "Error binding as manager to LDAP server: " + err.getError());
188 class ModuleLDAPOper : public Module
190 dynamic_reference<LDAPProvider> LDAP;
192 std::string attribute;
201 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
203 ConfigTag* tag = ServerInstance->Config->ConfValue("ldapoper");
205 LDAP.SetProvider("LDAP/" + tag->getString("dbid"));
206 base = tag->getString("baserdn");
207 attribute = tag->getString("attribute");
210 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
212 if (validated && command == "OPER" && parameters.size() >= 2)
214 const std::string& opername = parameters[0];
215 const std::string& password = parameters[1];
217 ServerConfig::OperIndex::const_iterator it = ServerInstance->Config->oper_blocks.find(opername);
218 if (it == ServerInstance->Config->oper_blocks.end())
219 return MOD_RES_PASSTHRU;
221 ConfigTag* tag = it->second->oper_block;
223 return MOD_RES_PASSTHRU;
225 std::string acceptedhosts = tag->getString("host");
226 if (!InspIRCd::MatchMask(acceptedhosts, user->MakeHost(), user->MakeHostIP()))
227 return MOD_RES_PASSTHRU;
230 return MOD_RES_PASSTHRU;
234 std::string what = attribute + "=" + opername;
235 LDAP->BindAsManager(new AdminBindInterface(this, LDAP.GetProvider(), user->uuid, opername, password, base, what));
238 catch (LDAPException& ex)
240 ServerInstance->SNO->WriteToSnoMask('a', "LDAP exception: " + ex.GetReason());
244 return MOD_RES_PASSTHRU;
247 Version GetVersion() CXX11_OVERRIDE
249 return Version("Allows server operators to be authenticated against an LDAP database.", VF_VENDOR);
253 MODULE_INIT(ModuleLDAPOper)