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
31 /* $ModDesc: Allow/Deny connections based upon answer from LDAP server */
32 /* $LinkerFlags: -lldap */
34 class ModuleLDAPAuth : public Module
36 LocalIntExt ldapAuthed;
38 std::string attribute;
39 std::string ldapserver;
40 std::string allowpattern;
41 std::string killreason;
50 ModuleLDAPAuth() : ldapAuthed("ldapauth", this)
57 Implementation eventlist[] = { I_OnCheckReady, I_OnRehash, I_OnUserRegister };
58 ServerInstance->Modules->Attach(eventlist, this, 3);
65 ldap_unbind_ext(conn, NULL, NULL);
68 void OnRehash(User* user)
72 base = Conf.ReadValue("ldapauth", "baserdn", 0);
73 attribute = Conf.ReadValue("ldapauth", "attribute", 0);
74 ldapserver = Conf.ReadValue("ldapauth", "server", 0);
75 allowpattern = Conf.ReadValue("ldapauth", "allowpattern", 0);
76 killreason = Conf.ReadValue("ldapauth", "killreason", 0);
77 std::string scope = Conf.ReadValue("ldapauth", "searchscope", 0);
78 username = Conf.ReadValue("ldapauth", "binddn", 0);
79 password = Conf.ReadValue("ldapauth", "bindauth", 0);
80 verbose = Conf.ReadFlag("ldapauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
81 useusername = Conf.ReadFlag("ldapauth", "userfield", 0);
84 searchscope = LDAP_SCOPE_BASE;
85 else if (scope == "onelevel")
86 searchscope = LDAP_SCOPE_ONELEVEL;
87 else searchscope = LDAP_SCOPE_SUBTREE;
95 ldap_unbind_ext(conn, NULL, NULL);
96 int res, v = LDAP_VERSION3;
97 res = ldap_initialize(&conn, ldapserver.c_str());
98 if (res != LDAP_SUCCESS)
101 ServerInstance->SNO->WriteToSnoMask('c', "LDAP connection failed: %s", ldap_err2string(res));
106 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
107 if (res != LDAP_SUCCESS)
110 ServerInstance->SNO->WriteToSnoMask('c', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
111 ldap_unbind_ext(conn, NULL, NULL);
118 ModResult OnUserRegister(LocalUser* user)
120 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
122 ldapAuthed.set(user,1);
123 return MOD_RES_PASSTHRU;
126 if (!CheckCredentials(user))
128 ServerInstance->Users->QuitUser(user, killreason);
131 return MOD_RES_PASSTHRU;
134 bool CheckCredentials(LocalUser* user)
141 // bind anonymously if no bind DN and authentication are given in the config
143 cred.bv_val = const_cast<char*>(password.c_str());
144 cred.bv_len = password.length();
146 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
148 if (res == LDAP_SERVER_DOWN)
150 // Attempt to reconnect if the connection dropped
152 ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
154 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
157 if (res != LDAP_SUCCESS)
160 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (LDAP bind failed: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
161 ldap_unbind_ext(conn, NULL, NULL);
167 LDAPMessage *msg, *entry;
168 std::string what = (attribute + "=" + (useusername ? user->ident : user->nick));
169 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
172 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (LDAP search failed: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
175 if (ldap_count_entries(conn, msg) > 1)
178 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (LDAP search returned more than one result: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
182 if ((entry = ldap_first_entry(conn, msg)) == NULL)
185 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (LDAP search returned no results: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
189 if (user->password.empty())
192 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (No password provided)", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
195 cred.bv_val = (char*)user->password.data();
196 cred.bv_len = user->password.length();
197 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
200 ldapAuthed.set(user,1);
206 ServerInstance->SNO->WriteToSnoMask('c', "Forbidden connection from %s!%s@%s (%s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
212 ModResult OnCheckReady(LocalUser* user)
214 return ldapAuthed.get(user) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
219 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR);
224 MODULE_INIT(ModuleLDAPAuth)