2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "modules/sql.h"
22 #include "modules/hash.h"
23 #include "modules/ssl.h"
31 class AuthQuery : public SQLQuery
34 const std::string uid;
35 LocalIntExt& pendingExt;
37 AuthQuery(Module* me, const std::string& u, LocalIntExt& e, bool v)
38 : SQLQuery(me), uid(u), pendingExt(e), verbose(v)
42 void OnResult(SQLResult& res) CXX11_OVERRIDE
44 User* user = ServerInstance->FindNick(uid);
49 pendingExt.set(user, AUTH_STATE_NONE);
54 ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s (SQL query returned no matches)", user->GetFullRealHost().c_str());
55 pendingExt.set(user, AUTH_STATE_FAIL);
59 void OnError(SQLerror& error) CXX11_OVERRIDE
61 User* user = ServerInstance->FindNick(uid);
64 pendingExt.set(user, AUTH_STATE_FAIL);
66 ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s (SQL query failed: %s)", user->GetFullRealHost().c_str(), error.Str());
70 class ModuleSQLAuth : public Module
72 LocalIntExt pendingExt;
73 dynamic_reference<SQLProvider> SQL;
75 std::string freeformquery;
76 std::string killreason;
77 std::string allowpattern;
81 ModuleSQLAuth() : pendingExt("sqlauth-wait", this), SQL(this, "SQL")
85 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
87 ConfigTag* conf = ServerInstance->Config->ConfValue("sqlauth");
88 std::string dbid = conf->getString("dbid");
90 SQL.SetProvider("SQL");
92 SQL.SetProvider("SQL/" + dbid);
93 freeformquery = conf->getString("query");
94 killreason = conf->getString("killreason");
95 allowpattern = conf->getString("allowpattern");
96 verbose = conf->getBool("verbose");
99 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
101 // Note this is their initial (unresolved) connect block
102 ConfigTag* tag = user->MyClass->config;
103 if (!tag->getBool("usesqlauth", true))
104 return MOD_RES_PASSTHRU;
106 if (!allowpattern.empty() && InspIRCd::Match(user->nick,allowpattern))
107 return MOD_RES_PASSTHRU;
109 if (pendingExt.get(user))
110 return MOD_RES_PASSTHRU;
114 ServerInstance->SNO->WriteGlobalSno('a', "Forbiding connection from %s (SQL database not present)", user->GetFullRealHost().c_str());
115 ServerInstance->Users->QuitUser(user, killreason);
116 return MOD_RES_PASSTHRU;
119 pendingExt.set(user, AUTH_STATE_BUSY);
122 SQL->PopulateUserInfo(user, userinfo);
123 userinfo["pass"] = user->password;
125 HashProvider* md5 = ServerInstance->Modules->FindDataService<HashProvider>("hash/md5");
127 userinfo["md5pass"] = md5->hexsum(user->password);
129 HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
131 userinfo["sha256pass"] = sha256->hexsum(user->password);
133 const std::string certfp = SSLClientCert::GetFingerprint(&user->eh);
134 userinfo["certfp"] = certfp;
136 SQL->submit(new AuthQuery(this, user->uuid, pendingExt, verbose), freeformquery, userinfo);
138 return MOD_RES_PASSTHRU;
141 ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
143 switch (pendingExt.get(user))
145 case AUTH_STATE_NONE:
146 return MOD_RES_PASSTHRU;
147 case AUTH_STATE_BUSY:
149 case AUTH_STATE_FAIL:
150 ServerInstance->Users->QuitUser(user, killreason);
153 return MOD_RES_PASSTHRU;
156 Version GetVersion() CXX11_OVERRIDE
158 return Version("Allow/Deny connections based upon an arbitrary SQL table", VF_VENDOR);
162 MODULE_INIT(ModuleSQLAuth)