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/>.
24 /* $ModDesc: Allows storage of oper credentials in an SQL table */
26 static bool OneOfMatches(const char* host, const char* ip, const std::string& hostlist)
28 std::stringstream hl(hostlist);
32 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
40 class OpMeQuery : public SQLQuery
43 const std::string uid, username, password;
44 OpMeQuery(Module* me, const std::string& u, const std::string& un, const std::string& pw)
45 : SQLQuery(me), uid(u), username(un), password(pw)
49 void OnResult(SQLResult& res)
51 ServerInstance->Logs->Log("m_sqloper",DEBUG, "SQLOPER: result for %s", uid.c_str());
52 User* user = ServerInstance->FindNick(uid);
56 // multiple rows may exist
58 while (res.GetRow(row))
60 if (OperUser(user, row[0], row[1]))
63 ServerInstance->Logs->Log("m_sqloper",DEBUG, "SQLOPER: no matches for %s (checked %d rows)", uid.c_str(), res.Rows());
64 // nobody succeeded... fall back to OPER
68 void OnError(SQLerror& error)
70 ServerInstance->Logs->Log("m_sqloper",DEFAULT, "SQLOPER: query failed (%s)", error.Str());
76 User* user = ServerInstance->FindNick(uid);
80 Command* oper_command = ServerInstance->Parser->GetHandler("OPER");
84 std::vector<std::string> params;
85 params.push_back(username);
86 params.push_back(password);
87 oper_command->Handle(params, user);
91 ServerInstance->Logs->Log("m_sqloper",SPARSE, "BUG: WHAT?! Why do we have no OPER command?!");
95 bool OperUser(User* user, const std::string &pattern, const std::string &type)
97 OperIndex::iterator iter = ServerInstance->Config->oper_blocks.find(" " + type);
98 if (iter == ServerInstance->Config->oper_blocks.end())
100 ServerInstance->Logs->Log("m_sqloper",DEFAULT, "SQLOPER: bad type '%s' in returned row for oper %s", type.c_str(), username.c_str());
103 OperInfo* ifo = iter->second;
105 std::string hostname(user->ident);
107 hostname.append("@").append(user->host);
109 if (OneOfMatches(hostname.c_str(), user->GetIPString().c_str(), pattern.c_str()))
111 /* Opertype and host match, looks like this is it. */
121 class ModuleSQLOper : public Module
124 std::string hashtype;
125 dynamic_reference<SQLProvider> SQL;
128 ModuleSQLOper() : SQL(this, "SQL") {}
134 Implementation eventlist[] = { I_OnRehash, I_OnPreCommand };
135 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
138 void OnRehash(User* user)
140 ConfigTag* tag = ServerInstance->Config->ConfValue("sqloper");
142 std::string dbid = tag->getString("dbid");
144 SQL.SetProvider("SQL");
146 SQL.SetProvider("SQL/" + dbid);
148 hashtype = tag->getString("hash");
149 query = tag->getString("query", "SELECT hostname as host, type FROM ircd_opers WHERE username='$username' AND password='$password'");
152 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line)
154 if (validated && command == "OPER" && parameters.size() >= 2)
158 LookupOper(user, parameters[0], parameters[1]);
159 /* Query is in progress, it will re-invoke OPER if needed */
162 ServerInstance->Logs->Log("m_sqloper",DEFAULT, "SQLOPER: database not present");
164 return MOD_RES_PASSTHRU;
167 void LookupOper(User* user, const std::string &username, const std::string &password)
169 HashProvider* hash = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
172 SQL->PopulateUserInfo(user, userinfo);
173 userinfo["username"] = username;
174 userinfo["password"] = hash ? hash->hexsum(password) : password;
176 SQL->submit(new OpMeQuery(this, user->uuid, username, password), query, userinfo);
181 return Version("Allows storage of oper credentials in an SQL table", VF_VENDOR);
185 MODULE_INIT(ModuleSQLOper)