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
41 LocalIntExt ldapAuthed;
43 std::string attribute;
44 std::string ldapserver;
45 std::string allowpattern;
46 std::string killreason;
55 ModuleLDAPAuth() : ldapAuthed("ldapauth", this)
62 Implementation eventlist[] = { I_OnCheckReady, I_OnRehash, I_OnUserRegister };
63 ServerInstance->Modules->Attach(eventlist, this, 3);
70 ldap_unbind_ext(conn, NULL, NULL);
73 void OnRehash(User* user)
77 base = Conf.ReadValue("ldapauth", "baserdn", 0);
78 attribute = Conf.ReadValue("ldapauth", "attribute", 0);
79 ldapserver = Conf.ReadValue("ldapauth", "server", 0);
80 allowpattern = Conf.ReadValue("ldapauth", "allowpattern", 0);
81 killreason = Conf.ReadValue("ldapauth", "killreason", 0);
82 std::string scope = Conf.ReadValue("ldapauth", "searchscope", 0);
83 username = Conf.ReadValue("ldapauth", "binddn", 0);
84 password = Conf.ReadValue("ldapauth", "bindauth", 0);
85 verbose = Conf.ReadFlag("ldapauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
86 useusername = Conf.ReadFlag("ldapauth", "userfield", 0);
89 searchscope = LDAP_SCOPE_BASE;
90 else if (scope == "onelevel")
91 searchscope = LDAP_SCOPE_ONELEVEL;
92 else searchscope = LDAP_SCOPE_SUBTREE;
100 ldap_unbind_ext(conn, NULL, NULL);
101 int res, v = LDAP_VERSION3;
102 res = ldap_initialize(&conn, ldapserver.c_str());
103 if (res != LDAP_SUCCESS)
106 ServerInstance->SNO->WriteToSnoMask('c', "LDAP connection failed: %s", ldap_err2string(res));
111 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
112 if (res != LDAP_SUCCESS)
115 ServerInstance->SNO->WriteToSnoMask('c', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
116 ldap_unbind_ext(conn, NULL, NULL);
123 ModResult OnUserRegister(LocalUser* user)
125 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
127 ldapAuthed.set(user,1);
128 return MOD_RES_PASSTHRU;
131 if (!CheckCredentials(user))
133 ServerInstance->Users->QuitUser(user, killreason);
136 return MOD_RES_PASSTHRU;
139 bool CheckCredentials(LocalUser* user)
145 if (user->password.empty())
148 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());
153 // bind anonymously if no bind DN and authentication are given in the config
155 cred.bv_val = const_cast<char*>(password.c_str());
156 cred.bv_len = password.length();
158 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
160 if (res == LDAP_SERVER_DOWN)
162 // Attempt to reconnect if the connection dropped
164 ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
166 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
169 if (res != LDAP_SUCCESS)
172 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));
173 ldap_unbind_ext(conn, NULL, NULL);
179 LDAPMessage *msg, *entry;
180 std::string what = (attribute + "=" + (useusername ? user->ident : user->nick));
181 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
183 // Do a second search, based on password, if it contains a :
184 // That is, PASS <user>:<password> will work.
185 size_t pos = user->password.find(":");
186 if (pos != std::string::npos)
188 res = ldap_search_ext_s(conn, base.c_str(), searchscope, user->password.substr(0, pos).c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg);
192 // Trim the user: prefix, leaving just 'pass' for later password check
193 user->password = user->password.substr(pos + 1);
197 // It may have found based on user:pass check above.
201 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));
205 if (ldap_count_entries(conn, msg) > 1)
208 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));
212 if ((entry = ldap_first_entry(conn, msg)) == NULL)
215 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));
219 cred.bv_val = (char*)user->password.data();
220 cred.bv_len = user->password.length();
221 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
224 ldapAuthed.set(user,1);
230 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));
236 ModResult OnCheckReady(LocalUser* user)
238 return ldapAuthed.get(user) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
243 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR);
248 MODULE_INIT(ModuleLDAPAuth)