1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
13 * Taken from the UnrealIRCd 4.0 SVN version, based on
16 * UnrealIRCd 4.0 (C) 2007 Carsten Valdemar Munk
17 * This program is free but copyrighted software; see
18 * the file COPYING for details.
20 * ---------------------------------------------------
21 * Heavily based on SQLauth
32 # pragma comment(lib, "ldap.lib")
33 # pragma comment(lib, "lber.lib")
36 /* $ModDesc: Allow/Deny connections based upon answer from LDAP server */
37 /* $LinkerFlags: -lldap */
39 class ModuleLDAPAuth : public Module
42 std::string ldapserver;
52 Implementation eventlist[] = { I_OnRehash, I_OnPassCompare };
53 ServerInstance->Modules->Attach(eventlist, this, 2);
57 virtual ~ModuleLDAPAuth()
60 ldap_unbind_ext(conn, NULL, NULL);
63 virtual void OnRehash(User* user)
67 base = Conf.ReadValue("ldapoper", "baserdn", 0);
68 ldapserver = Conf.ReadValue("ldapoper", "server", 0);
69 std::string scope = Conf.ReadValue("ldapoper", "searchscope", 0);
70 username = Conf.ReadValue("ldapoper", "binddn", 0);
71 password = Conf.ReadValue("ldapoper", "bindauth", 0);
74 searchscope = LDAP_SCOPE_BASE;
75 else if (scope == "onelevel")
76 searchscope = LDAP_SCOPE_ONELEVEL;
77 else searchscope = LDAP_SCOPE_SUBTREE;
85 ldap_unbind_ext(conn, NULL, NULL);
86 int res, v = LDAP_VERSION3;
87 res = ldap_initialize(&conn, ldapserver.c_str());
88 if (res != LDAP_SUCCESS)
94 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
95 if (res != LDAP_SUCCESS)
97 ldap_unbind_ext(conn, NULL, NULL);
104 virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
106 if (hashtype == "ldap")
108 if (LookupOper(data, input))
109 /* This is an ldap oper and has been found, claim the OPER command */
110 return MOD_RES_ALLOW;
114 /* We don't know this oper! */
115 return MOD_RES_PASSTHRU;
118 bool LookupOper(const std::string &what, const std::string &opassword)
125 char* authpass = strdup(password.c_str());
126 // bind anonymously if no bind DN and authentication are given in the config
128 cred.bv_val = authpass;
129 cred.bv_len = password.length();
131 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
133 if (res == LDAP_SERVER_DOWN)
135 // Attempt to reconnect if the connection dropped
136 ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
138 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
141 if (res != LDAP_SUCCESS)
144 ldap_unbind_ext(conn, NULL, NULL);
151 LDAPMessage *msg, *entry;
152 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
156 if (ldap_count_entries(conn, msg) > 1)
161 if ((entry = ldap_first_entry(conn, msg)) == NULL)
166 authpass = strdup(opassword.c_str());
167 cred.bv_val = authpass;
168 cred.bv_len = opassword.length();
169 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
183 virtual Version GetVersion()
185 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR);
190 MODULE_INIT(ModuleLDAPAuth)