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"
24 class OpMeQuery : public SQLQuery
27 const std::string uid, username, password;
28 OpMeQuery(Module* me, const std::string& u, const std::string& un, const std::string& pw)
29 : SQLQuery(me), uid(u), username(un), password(pw)
33 void OnResult(SQLResult& res) CXX11_OVERRIDE
35 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "result for %s", uid.c_str());
36 User* user = ServerInstance->FindNick(uid);
40 // multiple rows may exist
42 while (res.GetRow(row))
44 if (OperUser(user, row[0], row[1]))
47 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "no matches for %s (checked %d rows)", uid.c_str(), res.Rows());
48 // nobody succeeded... fall back to OPER
52 void OnError(SQLerror& error) CXX11_OVERRIDE
54 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "query failed (%s)", error.Str());
60 User* user = ServerInstance->FindNick(uid);
64 Command* oper_command = ServerInstance->Parser.GetHandler("OPER");
68 std::vector<std::string> params;
69 params.push_back(username);
70 params.push_back(password);
71 oper_command->Handle(params, user);
75 ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "BUG: WHAT?! Why do we have no OPER command?!");
79 bool OperUser(User* user, const std::string &pattern, const std::string &type)
81 ServerConfig::OperIndex::const_iterator iter = ServerInstance->Config->OperTypes.find(type);
82 if (iter == ServerInstance->Config->OperTypes.end())
84 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "bad type '%s' in returned row for oper %s", type.c_str(), username.c_str());
87 OperInfo* ifo = iter->second;
89 std::string hostname(user->ident);
91 hostname.append("@").append(user->host);
93 if (InspIRCd::MatchMask(pattern, hostname, user->GetIPString()))
95 /* Opertype and host match, looks like this is it. */
105 class ModuleSQLOper : public Module
108 std::string hashtype;
109 dynamic_reference<SQLProvider> SQL;
112 ModuleSQLOper() : SQL(this, "SQL") {}
114 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
116 ConfigTag* tag = ServerInstance->Config->ConfValue("sqloper");
118 std::string dbid = tag->getString("dbid");
120 SQL.SetProvider("SQL");
122 SQL.SetProvider("SQL/" + dbid);
124 hashtype = tag->getString("hash");
125 query = tag->getString("query", "SELECT hostname as host, type FROM ircd_opers WHERE username='$username' AND password='$password'");
128 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
130 if (validated && command == "OPER" && parameters.size() >= 2)
134 LookupOper(user, parameters[0], parameters[1]);
135 /* Query is in progress, it will re-invoke OPER if needed */
138 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "database not present");
140 return MOD_RES_PASSTHRU;
143 void LookupOper(User* user, const std::string &username, const std::string &password)
145 HashProvider* hash = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
148 SQL->PopulateUserInfo(user, userinfo);
149 userinfo["username"] = username;
150 userinfo["password"] = hash ? hash->Generate(password) : password;
152 SQL->submit(new OpMeQuery(this, user->uuid, username, password), query, userinfo);
155 Version GetVersion() CXX11_OVERRIDE
157 return Version("Allows storage of oper credentials in an SQL table", VF_VENDOR);
161 MODULE_INIT(ModuleSQLOper)