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;
49 std::vector<std::string> whitelistedcidrs;
56 ModuleLDAPAuth() : ldapAuthed("ldapauth", this)
63 Implementation eventlist[] = { I_OnCheckReady, I_OnRehash, I_OnUserRegister };
64 ServerInstance->Modules->Attach(eventlist, this, 3);
71 ldap_unbind_ext(conn, NULL, NULL);
74 void OnRehash(User* user)
77 whitelistedcidrs.clear();
79 base = Conf.ReadValue("ldapauth", "baserdn", 0);
80 attribute = Conf.ReadValue("ldapauth", "attribute", 0);
81 ldapserver = Conf.ReadValue("ldapauth", "server", 0);
82 allowpattern = Conf.ReadValue("ldapauth", "allowpattern", 0);
83 killreason = Conf.ReadValue("ldapauth", "killreason", 0);
84 std::string scope = Conf.ReadValue("ldapauth", "searchscope", 0);
85 username = Conf.ReadValue("ldapauth", "binddn", 0);
86 password = Conf.ReadValue("ldapauth", "bindauth", 0);
87 verbose = Conf.ReadFlag("ldapauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
88 useusername = Conf.ReadFlag("ldapauth", "userfield", 0);
90 ConfigTagList whitelisttags = ServerInstance->Config->ConfTags("ldapwhitelist");
92 for (ConfigIter i = whitelisttags.first; i != whitelisttags.second; ++i)
94 std::string cidr = i->second->getString("cidr");
96 whitelistedcidrs.push_back(cidr);
101 searchscope = LDAP_SCOPE_BASE;
102 else if (scope == "onelevel")
103 searchscope = LDAP_SCOPE_ONELEVEL;
104 else searchscope = LDAP_SCOPE_SUBTREE;
112 ldap_unbind_ext(conn, NULL, NULL);
113 int res, v = LDAP_VERSION3;
114 res = ldap_initialize(&conn, ldapserver.c_str());
115 if (res != LDAP_SUCCESS)
118 ServerInstance->SNO->WriteToSnoMask('c', "LDAP connection failed: %s", ldap_err2string(res));
123 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
124 if (res != LDAP_SUCCESS)
127 ServerInstance->SNO->WriteToSnoMask('c', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
128 ldap_unbind_ext(conn, NULL, NULL);
135 ModResult OnUserRegister(LocalUser* user)
137 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
139 ldapAuthed.set(user,1);
140 return MOD_RES_PASSTHRU;
143 for (std::vector<std::string>::iterator i = whitelistedcidrs.begin(); i != whitelistedcidrs.end(); i++)
145 if (InspIRCd::MatchCIDR(user->GetIPString(), *i, ascii_case_insensitive_map))
147 ldapAuthed.set(user,1);
148 return MOD_RES_PASSTHRU;
152 if (!CheckCredentials(user))
154 ServerInstance->Users->QuitUser(user, killreason);
157 return MOD_RES_PASSTHRU;
160 bool CheckCredentials(LocalUser* user)
166 if (user->password.empty())
169 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());
174 // bind anonymously if no bind DN and authentication are given in the config
176 cred.bv_val = const_cast<char*>(password.c_str());
177 cred.bv_len = password.length();
179 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
181 if (res == LDAP_SERVER_DOWN)
183 // Attempt to reconnect if the connection dropped
185 ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
187 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
190 if (res != LDAP_SUCCESS)
193 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));
194 ldap_unbind_ext(conn, NULL, NULL);
200 LDAPMessage *msg, *entry;
201 std::string what = (attribute + "=" + (useusername ? user->ident : user->nick));
202 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
204 // Do a second search, based on password, if it contains a :
205 // That is, PASS <user>:<password> will work.
206 size_t pos = user->password.find(":");
207 if (pos != std::string::npos)
209 res = ldap_search_ext_s(conn, base.c_str(), searchscope, user->password.substr(0, pos).c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg);
213 // Trim the user: prefix, leaving just 'pass' for later password check
214 user->password = user->password.substr(pos + 1);
218 // It may have found based on user:pass check above.
222 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));
226 if (ldap_count_entries(conn, msg) > 1)
229 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));
233 if ((entry = ldap_first_entry(conn, msg)) == NULL)
236 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));
240 cred.bv_val = (char*)user->password.data();
241 cred.bv_len = user->password.length();
242 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
245 ldapAuthed.set(user,1);
251 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));
257 ModResult OnCheckReady(LocalUser* user)
259 return ldapAuthed.get(user) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
264 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR);
269 MODULE_INIT(ModuleLDAPAuth)