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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
#include "module.h"
#include "nickserv.h"
#include "ldap.h"
static Module *me;
static Anope::string basedn;
static Anope::string search_filter;
static Anope::string object_class;
static Anope::string email_attribute;
static Anope::string username_attribute;
struct IdentifyInfo
{
dynamic_reference<User> user;
IdentifyRequest *req;
service_reference<LDAPProvider> lprov;
bool admin_bind;
Anope::string dn;
IdentifyInfo(User *u, IdentifyRequest *r, service_reference<LDAPProvider> &lp) : user(u), req(r), lprov(lp), admin_bind(true)
{
req->Hold(me);
}
~IdentifyInfo()
{
req->Release(me);
}
};
class IdentifyInterface : public LDAPInterface
{
std::map<LDAPQuery, IdentifyInfo *> requests;
public:
IdentifyInterface(Module *m) : LDAPInterface(m) { }
void Add(LDAPQuery id, IdentifyInfo *ii)
{
std::map<LDAPQuery, IdentifyInfo *>::iterator it = this->requests.find(id);
if (it != this->requests.end())
delete it->second;
this->requests[id] = ii;
}
void OnResult(const LDAPResult &r) anope_override
{
std::map<LDAPQuery, IdentifyInfo *>::iterator it = this->requests.find(r.id);
if (it == this->requests.end())
return;
IdentifyInfo *ii = it->second;
this->requests.erase(it);
if (!ii->lprov)
{
delete ii;
return;
}
switch (r.type)
{
case LDAPResult::QUERY_SEARCH:
{
if (!r.empty())
{
try
{
const LDAPAttributes &attr = r.get(0);
ii->dn = attr.get("dn");
Log(LOG_DEBUG) << "m_ldap_authenticationn: binding as " << ii->dn;
LDAPQuery id = ii->lprov->Bind(this, ii->dn, ii->req->GetPassword());
this->Add(id, ii);
return;
}
catch (const LDAPException &ex)
{
Log() << "m_ldap_authentication: Error binding after search: " << ex.GetReason();
}
}
break;
}
case LDAPResult::QUERY_BIND:
{
if (ii->admin_bind)
{
Anope::string sf = search_filter.replace_all_cs("%account", ii->req->GetAccount()).replace_all_cs("%object_class", object_class);
try
{
Log(LOG_DEBUG) << "m_ldap_authentication: searching for " << sf;
LDAPQuery id = ii->lprov->Search(this, basedn, sf);
this->Add(id, ii);
ii->admin_bind = false;
return;
}
catch (const LDAPException &ex)
{
Log() << "m_ldap_authentication: Unable to search for " << sf << ": " << ex.GetReason();
}
}
else
{
NickAlias *na = findnick(ii->req->GetAccount());
if (na == NULL)
{
na = new NickAlias(ii->req->GetAccount(), new NickCore(ii->req->GetAccount()));
if (ii->user)
{
if (Config->NSAddAccessOnReg)
na->nc->AddAccess(create_mask(ii->user));
const BotInfo *bi = findbot(Config->NickServ);
if (bi)
ii->user->SendMessage(bi, _("Your account \002%s\002 has been successfully created."), na->nick.c_str());
}
}
na->nc->Extend("m_ldap_authentication_dn", new ExtensibleItemClass<Anope::string>(ii->dn));
ii->req->Success(me);
}
break;
}
default:
break;
}
delete ii;
}
void OnError(const LDAPResult &r) anope_override
{
std::map<LDAPQuery, IdentifyInfo *>::iterator it = this->requests.find(r.id);
if (it == this->requests.end())
return;
IdentifyInfo *ii = it->second;
this->requests.erase(it);
delete ii;
}
};
class OnIdentifyInterface : public LDAPInterface
{
std::map<LDAPQuery, Anope::string> requests;
public:
OnIdentifyInterface(Module *m) : LDAPInterface(m) { }
void Add(LDAPQuery id, const Anope::string &nick)
{
this->requests[id] = nick;
}
void OnResult(const LDAPResult &r) anope_override
{
std::map<LDAPQuery, Anope::string>::iterator it = this->requests.find(r.id);
if (it == this->requests.end())
return;
User *u = finduser(it->second);
this->requests.erase(it);
if (!u || !u->Account() || r.empty())
return;
try
{
const LDAPAttributes &attr = r.get(0);
Anope::string email = attr.get(email_attribute);
if (!email.equals_ci(u->Account()->email))
{
u->Account()->email = email;
BotInfo *bi = findbot(Config->NickServ);
if (bi)
u->SendMessage(bi, _("Your email has been updated to \002%s\002"), email.c_str());
Log() << "m_ldap_authentication: Updated email address for " << u->nick << " (" << u->Account()->display << ") to " << email;
}
}
catch (const LDAPException &ex)
{
Log() << "m_ldap_authentication: " << ex.GetReason();
}
}
void OnError(const LDAPResult &r) anope_override
{
this->requests.erase(r.id);
Log() << "m_ldap_authentication: " << r.error;
}
};
class OnRegisterInterface : public LDAPInterface
{
public:
OnRegisterInterface(Module *m) : LDAPInterface(m) { }
void OnResult(const LDAPResult &r) anope_override
{
Log() << "m_ldap_authentication: Successfully added newly created account to LDAP";
}
void OnError(const LDAPResult &r) anope_override
{
Log() << "m_ldap_authentication: Error adding newly created account to LDAP: " << r.getError();
}
};
class NSIdentifyLDAP : public Module
{
service_reference<LDAPProvider> ldap;
IdentifyInterface iinterface;
OnIdentifyInterface oninterface;
OnRegisterInterface orinterface;
Anope::string password_attribute;
bool disable_register;
Anope::string disable_reason;
public:
NSIdentifyLDAP(const Anope::string &modname, const Anope::string &creator) :
Module(modname, creator, SUPPORTED), ldap("LDAPProvider", "ldap/main"), iinterface(this), oninterface(this), orinterface(this)
{
this->SetAuthor("Anope");
me = this;
Implementation i[] = { I_OnReload, I_OnPreCommand, I_OnCheckAuthentication, I_OnNickIdentify, I_OnNickRegister };
ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
ModuleManager::SetPriority(this, PRIORITY_FIRST);
OnReload();
}
void OnReload() anope_override
{
ConfigReader config;
basedn = config.ReadValue("m_ldap_authentication", "basedn", "", 0);
search_filter = config.ReadValue("m_ldap_authentication", "search_filter", "", 0);
object_class = config.ReadValue("m_ldap_authentication", "object_class", "", 0);
username_attribute = config.ReadValue("m_ldap_authentication", "username_attribute", "", 0);
this->password_attribute = config.ReadValue("m_ldap_authentication", "password_attribute", "", 0);
email_attribute = config.ReadValue("m_ldap_authentication", "email_attribute", "", 0);
this->disable_register = config.ReadFlag("m_ldap_authentication", "disable_ns_register", "false", 0);
this->disable_reason = config.ReadValue("m_ldap_authentication", "disable_reason", "", 0);
}
EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector<Anope::string> ¶ms) anope_override
{
if (this->disable_register && !this->disable_reason.empty() && command->name == "nickserv/register")
{
source.Reply(_(this->disable_reason.c_str()));
return EVENT_STOP;
}
return EVENT_CONTINUE;
}
void OnCheckAuthentication(User *u, IdentifyRequest *req) anope_override
{
if (!this->ldap)
return;
IdentifyInfo *ii = new IdentifyInfo(u, req, this->ldap);
try
{
LDAPQuery id = this->ldap->BindAsAdmin(&this->iinterface);
this->iinterface.Add(id, ii);
}
catch (const LDAPException &ex)
{
delete ii;
Log() << "m_ldap_authentication: " << ex.GetReason();
}
}
void OnNickIdentify(User *u) anope_override
{
if (email_attribute.empty() || !this->ldap || !u->Account()->HasExt("m_ldap_authentication_dn"))
return;
Anope::string *dn = u->Account()->GetExt<ExtensibleItemClass<Anope::string> *>("m_ldap_authentication_dn");
if (!dn || dn->empty())
return;
try
{
LDAPQuery id = this->ldap->Search(&this->oninterface, *dn, "(" + email_attribute + "=*)");
this->oninterface.Add(id, u->nick);
}
catch (const LDAPException &ex)
{
Log() << "m_ldap_authentication: " << ex.GetReason();
}
}
void OnNickRegister(NickAlias *na) anope_override
{
if (this->disable_register || !this->ldap)
return;
try
{
this->ldap->BindAsAdmin(NULL);
LDAPMods attributes;
attributes.resize(4);
attributes[0].name = "objectClass";
attributes[0].values.push_back("top");
attributes[0].values.push_back(object_class);
attributes[1].name = username_attribute;
attributes[1].values.push_back(na->nick);
if (!na->nc->email.empty())
{
attributes[2].name = email_attribute;
attributes[2].values.push_back(na->nc->email);
}
attributes[3].name = this->password_attribute;
attributes[3].values.push_back(na->nc->pass);
Anope::string new_dn = username_attribute + "=" + na->nick + "," + basedn;
this->ldap->Add(&this->orinterface, new_dn, attributes);
}
catch (const LDAPException &ex)
{
Log() << "m_ldap_authentication: " << ex.GetReason();
}
}
};
MODULE_INIT(NSIdentifyLDAP)
|