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 OperQuery : public SQL::Query
27 const std::string uid, username, password;
28 OperQuery(Module* me, const std::string& u, const std::string& un, const std::string& pw)
36 void OnResult(SQL::Result& res) CXX11_OVERRIDE
38 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "result for %s", uid.c_str());
39 User* user = ServerInstance->FindNick(uid);
43 // multiple rows may exist
45 while (res.GetRow(row))
47 if (OperUser(user, row[0], row[1]))
50 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "no matches for %s (checked %d rows)", uid.c_str(), res.Rows());
51 // nobody succeeded... fall back to OPER
55 void OnError(SQL::Error& error) CXX11_OVERRIDE
57 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "query failed (%s)", error.ToString());
63 User* user = ServerInstance->FindNick(uid);
67 Command* oper_command = ServerInstance->Parser.GetHandler("OPER");
71 std::vector<std::string> params;
72 params.push_back(username);
73 params.push_back(password);
74 oper_command->Handle(params, user);
78 ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "BUG: WHAT?! Why do we have no OPER command?!");
82 bool OperUser(User* user, const std::string &pattern, const std::string &type)
84 ServerConfig::OperIndex::const_iterator iter = ServerInstance->Config->OperTypes.find(type);
85 if (iter == ServerInstance->Config->OperTypes.end())
87 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "bad type '%s' in returned row for oper %s", type.c_str(), username.c_str());
90 OperInfo* ifo = iter->second;
92 std::string hostname(user->ident);
94 hostname.append("@").append(user->GetRealHost());
96 if (InspIRCd::MatchMask(pattern, hostname, user->GetIPString()))
98 /* Opertype and host match, looks like this is it. */
108 class ModuleSQLOper : public Module
111 std::string hashtype;
112 dynamic_reference<SQL::Provider> SQL;
115 ModuleSQLOper() : SQL(this, "SQL") {}
117 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
119 ConfigTag* tag = ServerInstance->Config->ConfValue("sqloper");
121 std::string dbid = tag->getString("dbid");
123 SQL.SetProvider("SQL");
125 SQL.SetProvider("SQL/" + dbid);
127 hashtype = tag->getString("hash");
128 query = tag->getString("query", "SELECT hostname as host, type FROM ircd_opers WHERE username='$username' AND password='$password' AND active=1;");
131 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
133 if (validated && command == "OPER" && parameters.size() >= 2)
137 LookupOper(user, parameters[0], parameters[1]);
138 /* Query is in progress, it will re-invoke OPER if needed */
141 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "database not present");
143 return MOD_RES_PASSTHRU;
146 void LookupOper(User* user, const std::string &username, const std::string &password)
148 HashProvider* hash = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
150 SQL::ParamMap userinfo;
151 SQL::PopulateUserInfo(user, userinfo);
152 userinfo["username"] = username;
153 userinfo["password"] = hash ? hash->Generate(password) : password;
155 SQL->Submit(new OperQuery(this, user->uuid, username, password), query, userinfo);
158 Version GetVersion() CXX11_OVERRIDE
160 return Version("Allows storage of oper credentials in an SQL table", VF_VENDOR);
164 MODULE_INIT(ModuleSQLOper)