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)
53 Implementation eventlist[] = { I_OnCheckReady, I_OnRehash, I_OnUserRegister };
54 ServerInstance->Modules->Attach(eventlist, this, 4);
61 ldap_unbind_ext(conn, NULL, NULL);
64 void OnRehash(User* user)
68 base = Conf.ReadValue("ldapauth", "baserdn", 0);
69 attribute = Conf.ReadValue("ldapauth", "attribute", 0);
70 ldapserver = Conf.ReadValue("ldapauth", "server", 0);
71 allowpattern = Conf.ReadValue("ldapauth", "allowpattern", 0);
72 killreason = Conf.ReadValue("ldapauth", "killreason", 0);
73 std::string scope = Conf.ReadValue("ldapauth", "searchscope", 0);
74 username = Conf.ReadValue("ldapauth", "binddn", 0);
75 password = Conf.ReadValue("ldapauth", "bindauth", 0);
76 verbose = Conf.ReadFlag("ldapauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
77 useusername = Conf.ReadFlag("ldapauth", "userfield", 0);
80 searchscope = LDAP_SCOPE_BASE;
81 else if (scope == "onelevel")
82 searchscope = LDAP_SCOPE_ONELEVEL;
83 else searchscope = LDAP_SCOPE_SUBTREE;
91 ldap_unbind_ext(conn, NULL, NULL);
92 int res, v = LDAP_VERSION3;
93 res = ldap_initialize(&conn, ldapserver.c_str());
94 if (res != LDAP_SUCCESS)
97 ServerInstance->SNO->WriteToSnoMask('c', "LDAP connection failed: %s", ldap_err2string(res));
102 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
103 if (res != LDAP_SUCCESS)
106 ServerInstance->SNO->WriteToSnoMask('c', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
107 ldap_unbind_ext(conn, NULL, NULL);
114 ModResult OnUserRegister(LocalUser* user)
116 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
118 ldapAuthed.set(user,1);
119 return MOD_RES_PASSTHRU;
122 if (!CheckCredentials(user))
124 ServerInstance->Users->QuitUser(user, killreason);
127 return MOD_RES_PASSTHRU;
130 bool CheckCredentials(LocalUser* user)
137 // bind anonymously if no bind DN and authentication are given in the config
139 cred.bv_val = const_cast<char*>(password.c_str());
140 cred.bv_len = password.length();
142 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
144 if (res == LDAP_SERVER_DOWN)
146 // Attempt to reconnect if the connection dropped
148 ServerInstance->SNO->WriteToSnoMask('a', "LDAP server has gone away - reconnecting...");
150 res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
153 if (res != LDAP_SUCCESS)
156 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));
157 ldap_unbind_ext(conn, NULL, NULL);
163 LDAPMessage *msg, *entry;
164 std::string what = (attribute + "=" + (useusername ? user->ident : user->nick));
165 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
168 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));
171 if (ldap_count_entries(conn, msg) > 1)
174 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));
178 if ((entry = ldap_first_entry(conn, msg)) == NULL)
181 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));
185 if (user->password.empty())
188 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());
191 cred.bv_val = (char*)user->password.data();
192 cred.bv_len = user->password.length();
193 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
196 ldapAuthed.set(user,1);
202 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));
208 ModResult OnCheckReady(LocalUser* user)
210 return ldapAuthed.get(user) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
215 return Version("Allow/Deny connections based upon answer from LDAP server", VF_VENDOR);
220 MODULE_INIT(ModuleLDAPAuth)