2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Robin Burchell <robin+git@viroteck.net>
5 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2007 Carsten Valdemar Munk <carsten.munk+inspircd@gmail.com>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 # pragma comment(lib, "ldap.lib")
32 # pragma comment(lib, "lber.lib")
35 /* $ModDesc: Adds the ability to authenticate opers via LDAP */
36 /* $LinkerFlags: -lldap */
38 class ModuleLDAPAuth : public Module
41 std::string ldapserver;
51 Implementation eventlist[] = { I_OnRehash, I_OnPassCompare };
52 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
56 virtual ~ModuleLDAPAuth()
59 ldap_unbind_ext(conn, NULL, NULL);
62 virtual void OnRehash(User* user)
64 ConfigTag* tag = ServerInstance->Config->ConfValue("ldapoper");
66 base = tag->getString("baserdn");
67 ldapserver = tag->getString("server");
68 std::string scope = tag->getString("searchscope");
69 username = tag->getString("binddn");
70 password = tag->getString("bindaut");
73 searchscope = LDAP_SCOPE_BASE;
74 else if (scope == "onelevel")
75 searchscope = LDAP_SCOPE_ONELEVEL;
76 else searchscope = LDAP_SCOPE_SUBTREE;
84 ldap_unbind_ext(conn, NULL, NULL);
85 int res, v = LDAP_VERSION3;
86 res = ldap_initialize(&conn, ldapserver.c_str());
87 if (res != LDAP_SUCCESS)
93 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
94 if (res != LDAP_SUCCESS)
96 ldap_unbind_ext(conn, NULL, NULL);
103 virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
105 if (hashtype == "ldap")
107 if (LookupOper(data, input))
108 /* This is an ldap oper and has been found, claim the OPER command */
109 return MOD_RES_ALLOW;
113 /* We don't know this oper! */
114 return MOD_RES_PASSTHRU;
117 bool LookupOper(const std::string &what, const std::string &opassword)
124 char* authpass = strdup(password.c_str());
125 // bind anonymously if no bind DN and authentication are given in the config
127 cred.bv_val = authpass;
128 cred.bv_len = password.length();
130 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
132 if (res == LDAP_SERVER_DOWN)
134 // Attempt to reconnect if the connection dropped
135 ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
137 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
140 if (res != LDAP_SUCCESS)
143 ldap_unbind_ext(conn, NULL, NULL);
150 LDAPMessage *msg, *entry;
151 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
155 if (ldap_count_entries(conn, msg) > 1)
160 if ((entry = ldap_first_entry(conn, msg)) == NULL)
165 authpass = strdup(opassword.c_str());
166 cred.bv_val = authpass;
167 cred.bv_len = opassword.length();
168 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
182 virtual Version GetVersion()
184 return Version("Adds the ability to authenticate opers via LDAP", VF_VENDOR);
189 MODULE_INIT(ModuleLDAPAuth)