1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 /* $ModDesc: Allows storage of oper credentials in an SQL table */
20 static bool OneOfMatches(const char* host, const char* ip, const std::string& hostlist)
22 std::stringstream hl(hostlist);
26 if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
34 class OpMeQuery : public SQLQuery
37 const std::string uid, username, password;
38 OpMeQuery(Module* me, const std::string& u, const std::string& un, const std::string& pw)
39 : SQLQuery(me), uid(u), username(un), password(pw)
43 void OnResult(SQLResult& res)
45 ServerInstance->Logs->Log("m_sqloper",DEBUG, "SQLOPER: result for %s", uid.c_str());
46 User* user = ServerInstance->FindNick(uid);
50 // multiple rows may exist
52 while (res.GetRow(row))
58 std::vector<KeyVal>* items;
59 reference<ConfigTag> tag = ConfigTag::create("oper", "<m_sqloper>", 0, items);
60 for(unsigned int i=0; i < cols.size(); i++)
63 items->insert(std::make_pair(cols[i], row[i]));
66 if (OperUser(user, row[0], row[1]))
70 ServerInstance->Logs->Log("m_sqloper",DEBUG, "SQLOPER: no matches for %s (checked %d rows)", uid.c_str(), res.Rows());
71 // nobody succeeded... fall back to OPER
75 void OnError(SQLerror& error)
77 ServerInstance->Logs->Log("m_sqloper",DEFAULT, "SQLOPER: query failed (%s)", error.Str());
83 User* user = ServerInstance->FindNick(uid);
87 Command* oper_command = ServerInstance->Parser->GetHandler("OPER");
91 std::vector<std::string> params;
92 params.push_back(username);
93 params.push_back(password);
94 oper_command->Handle(params, user);
98 ServerInstance->Logs->Log("m_sqloper",SPARSE, "BUG: WHAT?! Why do we have no OPER command?!");
102 bool OperUser(User* user, const std::string &pattern, const std::string &type)
104 OperIndex::iterator iter = ServerInstance->Config->oper_blocks.find(" " + type);
105 if (iter == ServerInstance->Config->oper_blocks.end())
107 ServerInstance->Logs->Log("m_sqloper",DEFAULT, "SQLOPER: bad type '%s' in returned row for oper %s", type.c_str(), username.c_str());
110 OperInfo* ifo = iter->second;
112 std::string hostname(user->ident);
114 hostname.append("@").append(user->host);
116 if (OneOfMatches(hostname.c_str(), user->GetIPString(), pattern.c_str()))
118 /* Opertype and host match, looks like this is it. */
128 class ModuleSQLOper : public Module
131 std::string hashtype;
132 dynamic_reference<SQLProvider> SQL;
135 ModuleSQLOper() : SQL(this, "SQL") {}
141 Implementation eventlist[] = { I_OnRehash, I_OnPreCommand };
142 ServerInstance->Modules->Attach(eventlist, this, 2);
145 void OnRehash(User* user)
147 ConfigTag* tag = ServerInstance->Config->ConfValue("sqloper");
149 std::string dbid = tag->getString("dbid");
151 SQL.SetProvider("SQL");
153 SQL.SetProvider("SQL/" + dbid);
155 hashtype = tag->getString("hash");
156 query = tag->getString("query", "SELECT hostname as host, type FROM ircd_opers WHERE username='$username' AND password='$password'");
159 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line)
161 if (validated && command == "OPER" && parameters.size() >= 2)
165 LookupOper(user, parameters[0], parameters[1]);
166 /* Query is in progress, it will re-invoke OPER if needed */
169 ServerInstance->Logs->Log("m_sqloper",DEFAULT, "SQLOPER: database not present");
171 return MOD_RES_PASSTHRU;
174 void LookupOper(User* user, const std::string &username, const std::string &password)
176 HashProvider* hash = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
179 SQL->PopulateUserInfo(user, userinfo);
180 userinfo["username"] = username;
181 userinfo["password"] = hash ? hash->hexsum(password) : password;
183 SQL->submit(new OpMeQuery(this, user->uuid, username, password), query, userinfo);
188 return Version("Allows storage of oper credentials in an SQL table", VF_VENDOR);
193 MODULE_INIT(ModuleSQLOper)