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;
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()
60 ldap_unbind_ext(conn, NULL, NULL);
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 username = Conf.ReadValue("ldapauth", "binddn", 0);
74 password = Conf.ReadValue("ldapauth", "bindauth", 0);
75 verbose = Conf.ReadFlag("ldapauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
78 searchscope = LDAP_SCOPE_BASE;
79 else if (scope == "onelevel")
80 searchscope = LDAP_SCOPE_ONELEVEL;
81 else searchscope = LDAP_SCOPE_SUBTREE;
89 ldap_unbind_ext(conn, NULL, NULL);
90 int res, v = LDAP_VERSION3;
91 res = ldap_initialize(&conn, ldapserver.c_str());
92 if (res != LDAP_SUCCESS)
95 ServerInstance->SNO->WriteToSnoMask('A', "LDAP connection failed: %s", ldap_err2string(res));
100 res = ldap_set_option(conn, LDAP_OPT_PROTOCOL_VERSION, (void *)&v);
101 if (res != LDAP_SUCCESS)
104 ServerInstance->SNO->WriteToSnoMask('A', "LDAP set protocol to v3 failed: %s", ldap_err2string(res));
105 ldap_unbind_ext(conn, NULL, NULL);
112 virtual int OnUserRegister(User* user)
114 if ((!allowpattern.empty()) && (ServerInstance->MatchText(user->nick,allowpattern)))
116 user->Extend("ldapauthed");
120 if (!CheckCredentials(user))
122 User::QuitUser(ServerInstance,user,killreason);
128 bool CheckCredentials(User* user)
135 char* authpass = strdup(password.c_str());
136 // bind anonymously if no bind DN and authentication are given in the config
138 cred.bv_val = authpass;
139 cred.bv_len = password.length();
141 if ((res = ldap_sasl_bind_s(conn, username.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) != LDAP_SUCCESS)
145 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP bind failed: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
146 ldap_unbind_ext(conn, NULL, NULL);
152 LDAPMessage *msg, *entry;
153 std::string what = (attribute + "=" + user->nick);
154 if ((res = ldap_search_ext_s(conn, base.c_str(), searchscope, what.c_str(), NULL, 0, NULL, NULL, NULL, 0, &msg)) != LDAP_SUCCESS)
157 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (LDAP search failed: %s)", user->nick, user->ident, user->host, ldap_err2string(res));
160 if (ldap_count_entries(conn, msg) > 1)
163 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));
167 if ((entry = ldap_first_entry(conn, msg)) == NULL)
170 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));
174 cred.bv_val = user->password;
175 cred.bv_len = strlen(user->password);
176 if ((res = ldap_sasl_bind_s(conn, ldap_get_dn(conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL)) == LDAP_SUCCESS)
179 user->Extend("ldapauthed");
185 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (%s)", user->nick, user->ident, user->host, ldap_err2string(res));
187 user->Extend("ldapauth_failed");
193 virtual void OnUserDisconnect(User* user)
195 user->Shrink("ldapauthed");
196 user->Shrink("ldapauth_failed");
199 virtual bool OnCheckReady(User* user)
201 return user->GetExt("ldapauthed");
204 virtual Version GetVersion()
206 return Version(1,2,0,0,VF_VENDOR,API_VERSION);
211 MODULE_INIT(ModuleLDAPAuth)