1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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;
44 ModuleLDAPAuth(InspIRCd* Me)
48 Implementation eventlist[] = { I_OnRehash, I_OnPassCompare };
49 ServerInstance->Modules->Attach(eventlist, this, 2);
53 virtual ~ModuleLDAPAuth()
56 ldap_unbind_ext(conn, NULL, NULL);
59 virtual void OnRehash(User* user, const std::string ¶meter)
61 ConfigReader Conf(ServerInstance);
63 base = Conf.ReadValue("ldapoper", "baserdn", 0);
64 ldapserver = Conf.ReadValue("ldapoper", "server", 0);
65 std::string scope = Conf.ReadValue("ldapoper", "searchscope", 0);
66 username = Conf.ReadValue("ldapoper", "binddn", 0);
67 password = Conf.ReadValue("ldapoper", "bindauth", 0);
70 searchscope = LDAP_SCOPE_BASE;
71 else if (scope == "onelevel")
72 searchscope = LDAP_SCOPE_ONELEVEL;
73 else searchscope = LDAP_SCOPE_SUBTREE;
81 ldap_unbind_ext(conn, NULL, NULL);
82 int res, v = LDAP_VERSION3;
83 res = ldap_initialize(&conn, ldapserver.c_str());
84 if (res != LDAP_SUCCESS)
90 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
91 if (res != LDAP_SUCCESS)
93 ldap_unbind_ext(conn, NULL, NULL);
100 virtual int OnPassCompare(Extensible* ex, const std::string &data, const std::string &input, const std::string &hashtype)
102 User* user = dynamic_cast<User*>(ex);
103 if (hashtype == "ldap")
105 if (LookupOper(user, data, input))
107 /* This is an ldap oper and has been found, claim the OPER command */
111 /* We don't know this oper! */
115 bool LookupOper(User* user, const std::string &what, const std::string &opassword)
122 char* authpass = strdup(password.c_str());
123 // bind anonymously if no bind DN and authentication are given in the config
125 cred.bv_val = authpass;
126 cred.bv_len = password.length();
128 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
131 ldap_unbind_ext(conn, NULL, NULL);
137 LDAPMessage *msg, *entry;
138 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
142 if (ldap_count_entries(conn, msg) > 1)
147 if ((entry = ldap_first_entry(conn, msg)) == NULL)
152 authpass = strdup(opassword.c_str());
153 cred.bv_val = authpass;
154 cred.bv_len = opassword.length();
155 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
169 virtual Version GetVersion()
171 return Version("$Id$", VF_VENDOR, API_VERSION);
176 MODULE_INIT(ModuleLDAPAuth)