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& db, const std::string& q, const std::string& u, const std::string& un, const std::string& pw)
39 : SQLQuery(me, db, q), uid(u), username(un), password(pw)
41 ServerInstance->Logs->Log("m_sqloper",DEBUG, "SQLOPER: db=%s query=\"%s\"", db.c_str(), q.c_str());
44 void OnResult(SQLResult& res)
46 ServerInstance->Logs->Log("m_sqloper",DEBUG, "SQLOPER: result on db=%s for %s", dbid.c_str(), uid.c_str());
47 User* user = ServerInstance->FindNick(uid);
51 // multiple rows may exist
53 while (res.GetRow(row))
59 std::vector<KeyVal>* items;
60 reference<ConfigTag> tag = ConfigTag::create("oper", "<m_sqloper>", 0, items);
61 for(unsigned int i=0; i < cols.size(); i++)
64 items->insert(std::make_pair(cols[i], row[i]));
67 if (OperUser(user, row[0], row[1]))
71 ServerInstance->Logs->Log("m_sqloper",DEBUG, "SQLOPER: no matches for %s (checked %d rows)", uid.c_str(), res.Rows());
72 // nobody succeeded... fall back to OPER
76 void OnError(SQLerror& error)
78 ServerInstance->Logs->Log("m_sqloper",DEFAULT, "SQLOPER: query failed (%s)", error.Str());
84 User* user = ServerInstance->FindNick(uid);
88 Command* oper_command = ServerInstance->Parser->GetHandler("OPER");
92 std::vector<std::string> params;
93 params.push_back(username);
94 params.push_back(password);
95 oper_command->Handle(params, user);
99 ServerInstance->Logs->Log("m_sqloper",SPARSE, "BUG: WHAT?! Why do we have no OPER command?!");
103 bool OperUser(User* user, const std::string &pattern, const std::string &type)
105 OperIndex::iterator iter = ServerInstance->Config->oper_blocks.find(" " + type);
106 if (iter == ServerInstance->Config->oper_blocks.end())
108 ServerInstance->Logs->Log("m_sqloper",DEFAULT, "SQLOPER: bad type '%s' in returned row for oper %s", type.c_str(), username.c_str());
111 OperInfo* ifo = iter->second;
113 std::string hostname(user->ident);
115 hostname.append("@").append(user->host);
117 if (OneOfMatches(hostname.c_str(), user->GetIPString(), pattern.c_str()))
119 /* Opertype and host match, looks like this is it. */
129 class ModuleSQLOper : public Module
131 std::string databaseid;
133 std::string hashtype;
134 dynamic_reference<SQLProvider> SQL;
137 ModuleSQLOper() : SQL(this, "SQL") {}
143 Implementation eventlist[] = { I_OnRehash, I_OnPreCommand };
144 ServerInstance->Modules->Attach(eventlist, this, 2);
147 void OnRehash(User* user)
149 ConfigTag* tag = ServerInstance->Config->ConfValue("sqloper");
151 databaseid = tag->getString("dbid");
152 hashtype = tag->getString("hash");
153 query = tag->getString("query", "SELECT hostname as host, type FROM ircd_opers WHERE username='$username' AND password='$password'");
156 ModResult OnPreCommand(std::string &command, std::vector<std::string> ¶meters, LocalUser *user, bool validated, const std::string &original_line)
158 if (validated && command == "OPER" && parameters.size() == 2 && SQL)
160 LookupOper(user, parameters[0], parameters[1]);
161 /* Query is in progress, it will re-invoke OPER if needed */
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, databaseid, SQL->FormatQuery(query, userinfo), user->uuid, username, password));
181 return Version("Allows storage of oper credentials in an SQL table", VF_VENDOR);
186 MODULE_INIT(ModuleSQLOper)