1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/*
* Anope IRC Services
*
* Copyright (C) 2011-2017 Anope Team <team@anope.org>
*
* This file is part of Anope. Anope is free software; you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software
* Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see see <http://www.gnu.org/licenses/>.
*/
#include "module.h"
#include "modules/ldap.h"
static std::set<Oper *> my_opers;
static Anope::string opertype_attribute;
class IdentifyInterface : public LDAPInterface
{
Reference<User> u;
public:
IdentifyInterface(Module *m, User *user) : LDAPInterface(m), u(user)
{
}
void OnResult(const LDAPResult &r) override
{
if (!u || !u->Account())
return;
NickServ::Account *nc = u->Account();
try
{
const LDAPAttributes &attr = r.get(0);
const Anope::string &opertype = attr.get(opertype_attribute);
OperType *ot = OperType::Find(opertype);
if (ot != NULL && (nc->o == NULL || ot != nc->o->ot))
{
Oper *o = nc->o;
if (o != NULL && my_opers.count(o) > 0)
{
my_opers.erase(o);
delete o;
}
o = new Oper(u->nick, ot);
my_opers.insert(o);
nc->o = o;
Log(this->owner) << "Tied " << u->nick << " (" << nc->GetDisplay() << ") to opertype " << ot->GetName();
}
}
catch (const LDAPException &ex)
{
if (nc->o != NULL)
{
if (my_opers.count(nc->o) > 0)
{
my_opers.erase(nc->o);
delete nc->o;
}
nc->o = NULL;
Log(this->owner) << "Removed services operator from " << u->nick << " (" << nc->GetDisplay() << ")";
}
}
}
void OnError(const LDAPResult &r) override
{
}
void OnDelete() anope_override
{
delete this;
}
};
class LDAPOper : public Module
, public EventHook<Event::NickIdentify>
, public EventHook<Event::DelCore>
{
ServiceReference<LDAPProvider> ldap;
Anope::string binddn;
Anope::string password;
Anope::string basedn;
Anope::string filter;
public:
LDAPOper(const Anope::string &modname, const Anope::string &creator)
: Module(modname, creator, EXTRA | VENDOR)
, ldap("LDAPProvider", "ldap/main")
{
}
void OnReload(Configuration::Conf *conf) override
{
Configuration::Block *config = Config->GetModule(this);
this->binddn = config->Get<Anope::string>("binddn");
this->password = config->Get<Anope::string>("password");
this->basedn = config->Get<Anope::string>("basedn");
this->filter = config->Get<Anope::string>("filter");
opertype_attribute = config->Get<Anope::string>("opertype_attribute");
for (std::set<Oper *>::iterator it = my_opers.begin(), it_end = my_opers.end(); it != it_end; ++it)
delete *it;
my_opers.clear();
}
void OnNickIdentify(User *u) override
{
try
{
if (!this->ldap)
throw LDAPException("No LDAP interface. Is m_ldap loaded and configured correctly?");
else if (this->basedn.empty() || this->filter.empty() || opertype_attribute.empty())
throw LDAPException("Could not search LDAP for opertype settings, invalid configuration.");
if (!this->binddn.empty())
this->ldap->Bind(NULL, this->binddn.replace_all_cs("%a", u->Account()->GetDisplay()), this->password.c_str());
this->ldap->Search(new IdentifyInterface(this, u), this->basedn, this->filter.replace_all_cs("%a", u->Account()->GetDisplay()));
}
catch (const LDAPException &ex)
{
Log() << ex.GetReason();
}
}
void OnDelCore(NickServ::Account *nc) override
{
if (nc->o != NULL && my_opers.count(nc->o) > 0)
{
my_opers.erase(nc->o);
delete nc->o;
nc->o = NULL;
}
}
};
MODULE_INIT(LDAPOper)
|