Anope IRC Services  Version 2.0
m_ldap_oper.cpp
Go to the documentation of this file.
1 #include "module.h"
2 #include "modules/ldap.h"
3 
4 static std::set<Oper *> my_opers;
6 
7 class IdentifyInterface : public LDAPInterface
8 {
9  std::map<LDAPQuery, Anope::string> requests;
10 
11  public:
13  {
14  }
15 
16  void Add(LDAPQuery id, const Anope::string &nick)
17  {
18  this->requests[id] = nick;
19  }
20 
22  {
23  std::map<LDAPQuery, Anope::string>::iterator it = this->requests.find(r.id);
24  if (it == this->requests.end())
25  return;
26  User *u = User::Find(it->second);
27  this->requests.erase(it);
28 
29 
30  if (!u || !u->Account())
31  return;
32 
33  NickCore *nc = u->Account();
34 
35  try
36  {
37  const LDAPAttributes &attr = r.get(0);
38 
39  const Anope::string &opertype = attr.get(opertype_attribute);
40 
41  OperType *ot = OperType::Find(opertype);
42  if (ot != NULL && (nc->o == NULL || ot != nc->o->ot))
43  {
44  Oper *o = nc->o;
45  if (o != NULL && my_opers.count(o) > 0)
46  {
47  my_opers.erase(o);
48  delete o;
49  }
50  o = new Oper(u->nick, ot);
51  my_opers.insert(o);
52  nc->o = o;
53  Log(this->owner) << "m_ldap_oper: Tied " << u->nick << " (" << nc->display << ") to opertype " << ot->GetName();
54  }
55  }
56  catch (const LDAPException &ex)
57  {
58  if (nc->o != NULL)
59  {
60  if (my_opers.count(nc->o) > 0)
61  {
62  my_opers.erase(nc->o);
63  delete nc->o;
64  }
65  nc->o = NULL;
66 
67  Log() << "Removed services operator from " << u->nick << " (" << nc->display << ")";
68  }
69  }
70  }
71 
73  {
74  this->requests.erase(r.id);
75  }
76 };
77 
78 class LDAPOper : public Module
79 {
82 
87  public:
88  LDAPOper(const Anope::string &modname, const Anope::string &creator) :
89  Module(modname, creator, EXTRA | VENDOR), ldap("LDAPProvider", "ldap/main"), iinterface(this)
90  {
91 
92  }
93 
95  {
96  Configuration::Block *config = Config->GetModule(this);
97 
98  this->binddn = config->Get<const Anope::string>("binddn");
99  this->password = config->Get<const Anope::string>("password");
100  this->basedn = config->Get<const Anope::string>("basedn");
101  this->filter = config->Get<const Anope::string>("filter");
102  opertype_attribute = config->Get<const Anope::string>("opertype_attribute");
103 
104  for (std::set<Oper *>::iterator it = my_opers.begin(), it_end = my_opers.end(); it != it_end; ++it)
105  delete *it;
106  my_opers.clear();
107  }
108 
110  {
111  try
112  {
113  if (!this->ldap)
114  throw LDAPException("No LDAP interface. Is m_ldap loaded and configured correctly?");
115  else if (this->basedn.empty() || this->filter.empty() || opertype_attribute.empty())
116  throw LDAPException("Could not search LDAP for opertype settings, invalid configuration.");
117 
118  if (!this->binddn.empty())
119  this->ldap->Bind(NULL, this->binddn.replace_all_cs("%a", u->Account()->display), this->password.c_str());
120  LDAPQuery id = this->ldap->Search(&this->iinterface, this->basedn, this->filter.replace_all_cs("%a", u->Account()->display));
121  this->iinterface.Add(id, u->nick);
122  }
123  catch (const LDAPException &ex)
124  {
125  Log() << ex.GetReason();
126  }
127  }
128 
130  {
131  if (nc->o != NULL && my_opers.count(nc->o) > 0)
132  {
133  my_opers.erase(nc->o);
134  delete nc->o;
135  nc->o = NULL;
136  }
137  }
138 };
139 
void OnNickIdentify(User *u) anope_override
Anope::string basedn
Definition: m_ldap_oper.cpp:85
Definition: opertype.h:18
const Anope::string & GetName() const
Definition: opertype.cpp:113
void Add(LDAPQuery id, IdentifyInfo *ii)
Definition: users.h:34
std::map< LDAPQuery, Anope::string > requests
Definition: m_ldap_oper.cpp:9
IdentifyInterface(Module *m)
Definition: m_ldap_oper.cpp:12
void OnReload(Configuration::Conf *conf) anope_override
Definition: m_ldap_oper.cpp:94
const Anope::string & get(const Anope::string &attr) const
Definition: ldap.h:45
static OperType * Find(const Anope::string &name)
Definition: opertype.cpp:42
Anope::string filter
Definition: m_ldap_oper.cpp:86
void OnResult(const LDAPResult &r) anope_override
Definition: m_ldap_oper.cpp:21
void OnDelCore(NickCore *nc) anope_override
static std::set< Oper * > my_opers
Definition: m_ldap_oper.cpp:4
Definition: Config.cs:26
OperType * ot
Definition: opertype.h:23
Anope::string password
Definition: m_ldap_oper.cpp:84
void OnError(const LDAPResult &r) anope_override
Definition: m_ldap_oper.cpp:72
string replace_all_cs(const string &_orig, const string &_repl) const
Definition: anope.h:229
int LDAPQuery
Definition: ldap.h:4
Anope::string display
Definition: account.h:113
Oper * o
Definition: account.h:131
Anope::string binddn
Definition: m_ldap_oper.cpp:83
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
#define MODULE_INIT(x)
Definition: modules.h:45
void Add(LDAPQuery id, const Anope::string &nick)
Definition: m_ldap_oper.cpp:16
Module * owner
Definition: ldap.h:112
static Anope::string opertype_attribute
Definition: m_ldap_oper.cpp:5
Anope::string nick
Definition: users.h:62
static User * Find(const Anope::string &name, bool nick_only=false)
Definition: users.cpp:815
NickCore * Account() const
Definition: users.cpp:422
virtual const Anope::string & GetReason() const
Definition: anope.h:672
IdentifyInterface iinterface
Definition: m_ldap_oper.cpp:81
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
T Get(const Anope::string &tag)
Definition: config.h:44
ServiceReference< LDAPProvider > ldap
Definition: m_ldap_oper.cpp:80
Definition: modules.h:163
LDAPOper(const Anope::string &modname, const Anope::string &creator)
Definition: m_ldap_oper.cpp:88