1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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
31 /* $ModDesc: Allow/Deny connections based upon answer from LDAP server */
32 /* $LinkerFlags: -lldap */
34 class ModuleLDAPAuth : public Module
37 std::string ldapserver;
47 Implementation eventlist[] = { I_OnRehash, I_OnPassCompare };
48 ServerInstance->Modules->Attach(eventlist, this, 2);
52 virtual ~ModuleLDAPAuth()
55 ldap_unbind_ext(conn, NULL, NULL);
58 virtual void OnRehash(User* user)
62 base = Conf.ReadValue("ldapoper", "baserdn", 0);
63 ldapserver = Conf.ReadValue("ldapoper", "server", 0);
64 std::string scope = Conf.ReadValue("ldapoper", "searchscope", 0);
65 username = Conf.ReadValue("ldapoper", "binddn", 0);
66 password = Conf.ReadValue("ldapoper", "bindauth", 0);
69 searchscope = LDAP_SCOPE_BASE;
70 else if (scope == "onelevel")
71 searchscope = LDAP_SCOPE_ONELEVEL;
72 else searchscope = LDAP_SCOPE_SUBTREE;
80 ldap_unbind_ext(conn, NULL, NULL);
81 int res, v = LDAP_VERSION3;
82 res = ldap_initialize(&conn, ldapserver.c_str());
83 if (res != LDAP_SUCCESS)
89 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
90 if (res != LDAP_SUCCESS)
92 ldap_unbind_ext(conn, NULL, NULL);
99 virtual ModResult OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
101 User* user = dynamic_cast<User*>(ex);
102 if (hashtype == "ldap")
104 if (LookupOper(user, data, input))
106 /* This is an ldap oper and has been found, claim the OPER command */
110 /* We don't know this oper! */
111 return MOD_RES_PASSTHRU;
114 bool LookupOper(User* user, const std::string &what, const std::string &opassword)
121 char* authpass = strdup(password.c_str());
122 // bind anonymously if no bind DN and authentication are given in the config
124 cred.bv_val = authpass;
125 cred.bv_len = password.length();
127 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
129 if (res == LDAP_SERVER_DOWN)
131 // Attempt to reconnect if the connection dropped
132 ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
134 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
137 if (res != LDAP_SUCCESS)
140 ldap_unbind_ext(conn, NULL, NULL);
147 LDAPMessage *msg, *entry;
148 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
152 if (ldap_count_entries(conn, msg) > 1)
157 if ((entry = ldap_first_entry(conn, msg)) == NULL)
162 authpass = strdup(opassword.c_str());
163 cred.bv_val = authpass;
164 cred.bv_len = opassword.length();
165 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
179 virtual Version GetVersion()
181 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR, API_VERSION);
186 MODULE_INIT(ModuleLDAPAuth)