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("m_sqloper", LOG_DEBUG, "SQLOPER: 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("m_sqloper", LOG_DEBUG, "SQLOPER: 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("m_sqloper", LOG_DEFAULT, "SQLOPER: 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("m_sqloper", 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 OperIndex::iterator iter = ServerInstance->Config->oper_blocks.find(" " + type);
82 if (iter == ServerInstance->Config->oper_blocks.end())
84 ServerInstance->Logs->Log("m_sqloper", LOG_DEFAULT, "SQLOPER: 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 init() CXX11_OVERRIDE
118 Implementation eventlist[] = { I_OnRehash, I_OnPreCommand };
119 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
122 void OnRehash(User* user) CXX11_OVERRIDE
124 ConfigTag* tag = ServerInstance->Config->ConfValue("sqloper");
126 std::string dbid = tag->getString("dbid");
128 SQL.SetProvider("SQL");
130 SQL.SetProvider("SQL/" + dbid);
132 hashtype = tag->getString("hash");
133 query = tag->getString("query", "SELECT hostname as host, type FROM ircd_opers WHERE username='$username' AND password='$password'");
136 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
138 if (validated && command == "OPER" && parameters.size() >= 2)
142 LookupOper(user, parameters[0], parameters[1]);
143 /* Query is in progress, it will re-invoke OPER if needed */
146 ServerInstance->Logs->Log("m_sqloper", LOG_DEFAULT, "SQLOPER: database not present");
148 return MOD_RES_PASSTHRU;
151 void LookupOper(User* user, const std::string &username, const std::string &password)
153 HashProvider* hash = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
156 SQL->PopulateUserInfo(user, userinfo);
157 userinfo["username"] = username;
158 userinfo["password"] = hash ? hash->hexsum(password) : password;
160 SQL->submit(new OpMeQuery(this, user->uuid, username, password), query, userinfo);
163 Version GetVersion() CXX11_OVERRIDE
165 return Version("Allows storage of oper credentials in an SQL table", VF_VENDOR);
169 MODULE_INIT(ModuleSQLOper)