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 /* $ModDesc: Allows storage of oper credentials in an SQL table */
26 class OpMeQuery : public SQLQuery
29 const std::string uid, username, password;
30 OpMeQuery(Module* me, const std::string& u, const std::string& un, const std::string& pw)
31 : SQLQuery(me), uid(u), username(un), password(pw)
35 void OnResult(SQLResult& res) CXX11_OVERRIDE
37 ServerInstance->Logs->Log("m_sqloper",LOG_DEBUG, "SQLOPER: result for %s", uid.c_str());
38 User* user = ServerInstance->FindNick(uid);
42 // multiple rows may exist
44 while (res.GetRow(row))
46 if (OperUser(user, row[0], row[1]))
49 ServerInstance->Logs->Log("m_sqloper",LOG_DEBUG, "SQLOPER: no matches for %s (checked %d rows)", uid.c_str(), res.Rows());
50 // nobody succeeded... fall back to OPER
54 void OnError(SQLerror& error) CXX11_OVERRIDE
56 ServerInstance->Logs->Log("m_sqloper",LOG_DEFAULT, "SQLOPER: query failed (%s)", error.Str());
62 User* user = ServerInstance->FindNick(uid);
66 Command* oper_command = ServerInstance->Parser->GetHandler("OPER");
70 std::vector<std::string> params;
71 params.push_back(username);
72 params.push_back(password);
73 oper_command->Handle(params, user);
77 ServerInstance->Logs->Log("m_sqloper",LOG_SPARSE, "BUG: WHAT?! Why do we have no OPER command?!");
81 bool OperUser(User* user, const std::string &pattern, const std::string &type)
83 OperIndex::iterator iter = ServerInstance->Config->oper_blocks.find(" " + type);
84 if (iter == ServerInstance->Config->oper_blocks.end())
86 ServerInstance->Logs->Log("m_sqloper",LOG_DEFAULT, "SQLOPER: bad type '%s' in returned row for oper %s", type.c_str(), username.c_str());
89 OperInfo* ifo = iter->second;
91 std::string hostname(user->ident);
93 hostname.append("@").append(user->host);
95 if (InspIRCd::MatchMask(pattern, hostname, user->GetIPString()))
97 /* Opertype and host match, looks like this is it. */
107 class ModuleSQLOper : public Module
110 std::string hashtype;
111 dynamic_reference<SQLProvider> SQL;
114 ModuleSQLOper() : SQL(this, "SQL") {}
116 void init() CXX11_OVERRIDE
120 Implementation eventlist[] = { I_OnRehash, I_OnPreCommand };
121 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
124 void OnRehash(User* user) CXX11_OVERRIDE
126 ConfigTag* tag = ServerInstance->Config->ConfValue("sqloper");
128 std::string dbid = tag->getString("dbid");
130 SQL.SetProvider("SQL");
132 SQL.SetProvider("SQL/" + dbid);
134 hashtype = tag->getString("hash");
135 query = tag->getString("query", "SELECT hostname as host, type FROM ircd_opers WHERE username='$username' AND password='$password'");
138 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
140 if (validated && command == "OPER" && parameters.size() >= 2)
144 LookupOper(user, parameters[0], parameters[1]);
145 /* Query is in progress, it will re-invoke OPER if needed */
148 ServerInstance->Logs->Log("m_sqloper",LOG_DEFAULT, "SQLOPER: database not present");
150 return MOD_RES_PASSTHRU;
153 void LookupOper(User* user, const std::string &username, const std::string &password)
155 HashProvider* hash = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
158 SQL->PopulateUserInfo(user, userinfo);
159 userinfo["username"] = username;
160 userinfo["password"] = hash ? hash->hexsum(password) : password;
162 SQL->submit(new OpMeQuery(this, user->uuid, username, password), query, userinfo);
165 Version GetVersion() CXX11_OVERRIDE
167 return Version("Allows storage of oper credentials in an SQL table", VF_VENDOR);
171 MODULE_INIT(ModuleSQLOper)