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))
64 std::vector<KeyVal>* items;
65 reference<ConfigTag> tag = ConfigTag::create("oper", "<m_sqloper>", 0, items);
66 for(unsigned int i=0; i < cols.size(); i++)
69 items->insert(std::make_pair(cols[i], row[i]));
72 if (OperUser(user, row[0], row[1]))
76 ServerInstance->Logs->Log("m_sqloper",DEBUG, "SQLOPER: no matches for %s (checked %d rows)", uid.c_str(), res.Rows());
77 // nobody succeeded... fall back to OPER
81 void OnError(SQLerror& error)
83 ServerInstance->Logs->Log("m_sqloper",DEFAULT, "SQLOPER: query failed (%s)", error.Str());
89 User* user = ServerInstance->FindNick(uid);
93 Command* oper_command = ServerInstance->Parser->GetHandler("OPER");
97 std::vector<std::string> params;
98 params.push_back(username);
99 params.push_back(password);
100 oper_command->Handle(params, user);
104 ServerInstance->Logs->Log("m_sqloper",SPARSE, "BUG: WHAT?! Why do we have no OPER command?!");
108 bool OperUser(User* user, const std::string &pattern, const std::string &type)
110 OperIndex::iterator iter = ServerInstance->Config->oper_blocks.find(" " + type);
111 if (iter == ServerInstance->Config->oper_blocks.end())
113 ServerInstance->Logs->Log("m_sqloper",DEFAULT, "SQLOPER: bad type '%s' in returned row for oper %s", type.c_str(), username.c_str());
116 OperInfo* ifo = iter->second;
118 std::string hostname(user->ident);
120 hostname.append("@").append(user->host);
122 if (OneOfMatches(hostname.c_str(), user->GetIPString(), pattern))
124 /* Opertype and host match, looks like this is it. */
134 class ModuleSQLOper : public Module
137 std::string hashtype;
138 dynamic_reference<SQLProvider> SQL;
141 ModuleSQLOper() : SQL(this, "SQL") {}
147 Implementation eventlist[] = { I_OnRehash, I_OnPreCommand };
148 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
151 void OnRehash(User* user)
153 ConfigTag* tag = ServerInstance->Config->ConfValue("sqloper");
155 std::string dbid = tag->getString("dbid");
157 SQL.SetProvider("SQL");
159 SQL.SetProvider("SQL/" + dbid);
161 hashtype = tag->getString("hash");
162 query = tag->getString("query", "SELECT hostname as host, type FROM ircd_opers WHERE username='$username' AND password='$password'");
165 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line)
167 if (validated && command == "OPER" && parameters.size() >= 2)
171 LookupOper(user, parameters[0], parameters[1]);
172 /* Query is in progress, it will re-invoke OPER if needed */
175 ServerInstance->Logs->Log("m_sqloper",DEFAULT, "SQLOPER: database not present");
177 return MOD_RES_PASSTHRU;
180 void LookupOper(User* user, const std::string &username, const std::string &password)
182 HashProvider* hash = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
185 SQL->PopulateUserInfo(user, userinfo);
186 userinfo["username"] = username;
187 userinfo["password"] = hash ? hash->hexsum(password) : password;
189 SQL->submit(new OpMeQuery(this, user->uuid, username, password), query, userinfo);
194 return Version("Allows storage of oper credentials in an SQL table", VF_VENDOR);
199 MODULE_INIT(ModuleSQLOper)