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
30 #define LDAP_DEPRECATED 1
33 /* $ModDesc: Allow/Deny connections based upon answer from LDAP server */
34 /* $LinkerFlags: -lldap */
36 class ModuleLDAPAuth : public Module
39 std::string attribute;
40 std::string ldapserver;
41 std::string allowpattern;
42 std::string killreason;
48 ModuleLDAPAuth(InspIRCd* Me)
52 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRehash, I_OnUserRegister };
53 ServerInstance->Modules->Attach(eventlist, this, 4);
57 virtual ~ModuleLDAPAuth()
63 virtual void OnRehash(User* user, const std::string ¶meter)
65 ConfigReader Conf(ServerInstance);
67 base = Conf.ReadValue("ldapauth", "baserdn", 0);
68 attribute = Conf.ReadValue("ldapauth", "attribute", 0);
69 ldapserver = Conf.ReadValue("ldapauth", "server", 0);
70 allowpattern = Conf.ReadValue("ldapauth", "allowpattern", 0);
71 killreason = Conf.ReadValue("ldapauth", "killreason", 0);
72 std::string scope = Conf.ReadValue("ldapauth", "searchscope", 0);
73 verbose = Conf.ReadFlag("ldapauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
76 searchscope = LDAP_SCOPE_BASE;
77 else if (scope == "onelevel")
78 searchscope = LDAP_SCOPE_ONELEVEL;
79 else searchscope = LDAP_SCOPE_SUBTREE;
88 int res, v = LDAP_VERSION3;
89 res = ldap_initialize(&conn, ldapserver.c_str());
90 if (res != LDAP_SUCCESS)
93 ServerInstance->SNO->WriteToSnoMask('A', "LDAP connection failed: %s", ldap_err2string(res));
98 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
99 if (res != LDAP_SUCCESS)
102 ServerInstance->SNO->WriteToSnoMask('A', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
110 virtual int OnUserRegister(User* user)
112 if ((!allowpattern.empty()) && (ServerInstance->MatchText(user->nick,allowpattern)))
114 user->Extend("ldapauthed");
118 if (!CheckCredentials(user))
120 User::QuitUser(ServerInstance,user,killreason);
126 bool CheckCredentials(User* user)
134 if ((res = ldap_simple_bind_s(conn, "", "")) != LDAP_SUCCESS)
137 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP bind anonymously failed: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
142 LDAPMessage *msg, *entry;
143 std::string what = (attribute + "=" + user->nick);
144 if ((res = ldap_search_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, &msg)) != LDAP_SUCCESS)
147 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP search failed: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
150 if (ldap_count_entries(conn, msg) > 1)
153 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP search returned more than one result: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
157 if ((entry = ldap_first_entry(conn, msg)) == NULL)
160 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP search returned no results: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
164 if ((res = ldap_simple_bind_s(conn, ldap_get_dn(conn, entry), user->password)) == LDAP_SUCCESS)
167 user->Extend("ldapauthed");
173 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (%s)", user->nick, user->ident, user->host, ldap_err2string(res));
175 user->Extend("ldapauth_failed");
181 virtual void OnUserDisconnect(User* user)
183 user->Shrink("ldapauthed");
184 user->Shrink("ldapauth_failed");
187 virtual bool OnCheckReady(User* user)
189 return user->GetExt("ldapauthed");
192 virtual Version GetVersion()
194 return Version(4,0,0,0,VF_VENDOR,API_VERSION);
199 MODULE_INIT(ModuleLDAPAuth)