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 attribute;
38 std::string ldapserver;
39 std::string allowpattern;
40 std::string killreason;
49 ModuleLDAPAuth(InspIRCd* Me)
53 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRehash, I_OnUserRegister };
54 ServerInstance->Modules->Attach(eventlist, this, 4);
58 virtual ~ModuleLDAPAuth()
61 ldap_unbind_ext(conn, NULL, NULL);
64 virtual void OnRehash(User* user, const std::string ¶meter)
66 ConfigReader Conf(ServerInstance);
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('A', "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('A', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
107 ldap_unbind_ext(conn, NULL, NULL);
114 virtual int OnUserRegister(User* user)
116 if ((!allowpattern.empty()) && (ServerInstance->MatchText(user->nick,allowpattern)))
118 user->Extend("ldapauthed");
122 if (!CheckCredentials(user))
124 ServerInstance->Users->QuitUser(user, killreason);
130 bool CheckCredentials(User* user)
137 char* authpass = strdup(password.c_str());
138 // bind anonymously if no bind DN and authentication are given in the config
140 cred.bv_val = authpass;
141 cred.bv_len = password.length();
143 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
147 ServerInstance->SNO->WriteToSnoMask('A', "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));
148 ldap_unbind_ext(conn, NULL, NULL);
154 LDAPMessage *msg, *entry;
155 std::string what = (attribute + "=" + (useusername ? user->ident : user->nick));
156 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
159 ServerInstance->SNO->WriteToSnoMask('A', "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));
162 if (ldap_count_entries(conn, msg) > 1)
165 ServerInstance->SNO->WriteToSnoMask('A', "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));
169 if ((entry = ldap_first_entry(conn, msg)) == NULL)
172 ServerInstance->SNO->WriteToSnoMask('A', "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));
176 cred.bv_val = (char*)user->password.data();
177 cred.bv_len = user->password.length();
178 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
181 user->Extend("ldapauthed");
187 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (%s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), ldap_err2string(res));
189 user->Extend("ldapauth_failed");
195 virtual void OnUserDisconnect(User* user)
197 user->Shrink("ldapauthed");
198 user->Shrink("ldapauth_failed");
201 virtual bool OnCheckReady(User* user)
203 return user->GetExt("ldapauthed");
206 virtual Version GetVersion()
208 return Version(1,2,0,0,VF_VENDOR,API_VERSION);
213 MODULE_INIT(ModuleLDAPAuth)