]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_ldap.cpp
Allow 'tls' to be used in /REHASH.
[user/henk/code/inspircd.git] / src / modules / extra / m_ldap.cpp
index fc1bee939c9369a07b6741d9ed712b0000665cd2..80a055f199c08b3739c5ba778c86b367142de988 100644 (file)
@@ -1,8 +1,11 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
- *   Copyright (C) 2013-2015 Adam <Adam@anope.org>
- *   Copyright (C) 2003-2015 Anope Team <team@anope.org>
+ *   Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
+ *   Copyright (C) 2019 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2016-2019 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2014, 2016 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2013-2016 Adam <Adam@anope.org>
  *
  * This file is part of InspIRCd.  InspIRCd is free software: you can
  * redistribute it and/or modify it under the terms of the GNU General Public
@@ -19,7 +22,9 @@
 
 /// $LinkerFlags: -llber -lldap_r
 
+/// $PackageInfo: require_system("arch") libldap
 /// $PackageInfo: require_system("centos") openldap-devel
+/// $PackageInfo: require_system("debian") libldap2-dev
 /// $PackageInfo: require_system("ubuntu") libldap2-dev
 
 #include "inspircd.h"
 
 // Ignore OpenLDAP deprecation warnings on OS X Yosemite and newer.
 #if defined __APPLE__
+# pragma GCC diagnostic push
 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 #endif
 
 #include <ldap.h>
 
+#ifdef __APPLE__
+# pragma GCC diagnostic pop
+#endif
+
 #ifdef _WIN32
 # pragma comment(lib, "libldap_r.lib")
 # pragma comment(lib, "liblber.lib")
@@ -68,6 +78,7 @@ class LDAPRequest
        }
 
        virtual int run() = 0;
+       virtual std::string info() = 0;
 };
 
 class LDAPBind : public LDAPRequest
@@ -84,6 +95,7 @@ class LDAPBind : public LDAPRequest
        }
 
        int run() CXX11_OVERRIDE;
+       std::string info() CXX11_OVERRIDE;
 };
 
 class LDAPSearch : public LDAPRequest
@@ -103,6 +115,7 @@ class LDAPSearch : public LDAPRequest
        }
 
        int run() CXX11_OVERRIDE;
+       std::string info() CXX11_OVERRIDE;
 };
 
 class LDAPAdd : public LDAPRequest
@@ -120,6 +133,7 @@ class LDAPAdd : public LDAPRequest
        }
 
        int run() CXX11_OVERRIDE;
+       std::string info() CXX11_OVERRIDE;
 };
 
 class LDAPDel : public LDAPRequest
@@ -135,6 +149,7 @@ class LDAPDel : public LDAPRequest
        }
 
        int run() CXX11_OVERRIDE;
+       std::string info() CXX11_OVERRIDE;
 };
 
 class LDAPModify : public LDAPRequest
@@ -152,6 +167,7 @@ class LDAPModify : public LDAPRequest
        }
 
        int run() CXX11_OVERRIDE;
+       std::string info() CXX11_OVERRIDE;
 };
 
 class LDAPCompare : public LDAPRequest
@@ -169,6 +185,7 @@ class LDAPCompare : public LDAPRequest
        }
 
        int run() CXX11_OVERRIDE;
+       std::string info() CXX11_OVERRIDE;
 };
 
 class LDAPService : public LDAPProvider, public SocketThread
@@ -240,6 +257,17 @@ class LDAPService : public LDAPProvider, public SocketThread
                Connect();
        }
 
+       int SetOption(int option, const void* value)
+       {
+               int ret = ldap_set_option(this->con, option, value);
+               if (ret != LDAP_OPT_SUCCESS)
+               {
+                       ldap_unbind_ext(this->con, NULL, NULL);
+                       this->con = NULL;
+               }
+               return ret;
+       }
+
        void QueueRequest(LDAPRequest* r)
        {
                this->LockQueue();
@@ -257,13 +285,13 @@ class LDAPService : public LDAPProvider, public SocketThread
                , con(NULL), config(tag), last_connect(0)
        {
                std::string scope = config->getString("searchscope");
-               if (scope == "base")
+               if (stdalgo::string::equalsci(scope, "base"))
                        searchscope = LDAP_SCOPE_BASE;
-               else if (scope == "onelevel")
+               else if (stdalgo::string::equalsci(scope, "onelevel"))
                        searchscope = LDAP_SCOPE_ONELEVEL;
                else
                        searchscope = LDAP_SCOPE_SUBTREE;
-               timeout = config->getInt("timeout", 5);
+               timeout = config->getDuration("timeout", 5);
 
                Connect();
        }
@@ -311,22 +339,14 @@ class LDAPService : public LDAPProvider, public SocketThread
                        throw LDAPException("Unable to connect to LDAP service " + this->name + ": " + ldap_err2string(i));
 
                const int version = LDAP_VERSION3;
-               i = ldap_set_option(this->con, LDAP_OPT_PROTOCOL_VERSION, &version);
+               i = SetOption(LDAP_OPT_PROTOCOL_VERSION, &version);
                if (i != LDAP_OPT_SUCCESS)
-               {
-                       ldap_unbind_ext(this->con, NULL, NULL);
-                       this->con = NULL;
                        throw LDAPException("Unable to set protocol version for " + this->name + ": " + ldap_err2string(i));
-               }
 
                const struct timeval tv = { 0, 0 };
-               i = ldap_set_option(this->con, LDAP_OPT_NETWORK_TIMEOUT, &tv);
+               i = SetOption(LDAP_OPT_NETWORK_TIMEOUT, &tv);
                if (i != LDAP_OPT_SUCCESS)
-               {
-                       ldap_unbind_ext(this->con, NULL, NULL);
-                       this->con = NULL;
                        throw LDAPException("Unable to set timeout for " + this->name + ": " + ldap_err2string(i));
-               }
        }
 
        void BindAsManager(LDAPInterface* i) CXX11_OVERRIDE
@@ -383,7 +403,7 @@ class LDAPService : public LDAPProvider, public SocketThread
 
                if (res != LDAP_SUCCESS)
                {
-                       ldap_result->error = ldap_err2string(res);
+                       ldap_result->error = InspIRCd::Format("%s (%s)", ldap_err2string(res), req->info().c_str());
                        return;
                }
 
@@ -532,7 +552,7 @@ class ModuleLDAP : public Module
                {
                        const reference<ConfigTag>& tag = i->second;
 
-                       if (tag->getString("module") != "ldap")
+                       if (!stdalgo::string::equalsci(tag->getString("module"), "ldap"))
                                continue;
 
                        std::string id = tag->getString("id");
@@ -616,7 +636,7 @@ class ModuleLDAP : public Module
 
        Version GetVersion() CXX11_OVERRIDE
        {
-               return Version("LDAP support", VF_VENDOR);
+               return Version("Provides the ability for LDAP modules to query a LDAP directory.", VF_VENDOR);
        }
 };
 
@@ -633,11 +653,21 @@ int LDAPBind::run()
        return i;
 }
 
+std::string LDAPBind::info()
+{
+       return "bind dn=" + who;
+}
+
 int LDAPSearch::run()
 {
        return ldap_search_ext_s(service->GetConnection(), base.c_str(), searchscope, filter.c_str(), NULL, 0, NULL, NULL, &tv, 0, &message);
 }
 
+std::string LDAPSearch::info()
+{
+       return "search base=" + base + " filter=" + filter;
+}
+
 int LDAPAdd::run()
 {
        LDAPMod** mods = LDAPService::BuildMods(attributes);
@@ -646,11 +676,21 @@ int LDAPAdd::run()
        return i;
 }
 
+std::string LDAPAdd::info()
+{
+       return "add dn=" + dn;
+}
+
 int LDAPDel::run()
 {
        return ldap_delete_ext_s(service->GetConnection(), dn.c_str(), NULL, NULL);
 }
 
+std::string LDAPDel::info()
+{
+       return "del dn=" + dn;
+}
+
 int LDAPModify::run()
 {
        LDAPMod** mods = LDAPService::BuildMods(attributes);
@@ -659,6 +699,11 @@ int LDAPModify::run()
        return i;
 }
 
+std::string LDAPModify::info()
+{
+       return "modify base=" + base;
+}
+
 int LDAPCompare::run()
 {
        berval cred;
@@ -670,7 +715,11 @@ int LDAPCompare::run()
        free(cred.bv_val);
 
        return ret;
+}
 
+std::string LDAPCompare::info()
+{
+       return "compare dn=" + dn + " attr=" + attr;
 }
 
 MODULE_INIT(ModuleLDAP)