- target = ServerInstance->Modules->FindFeature("SQL");
-
- if (target)
- {
- hashymodules::iterator x = hashers.find(hashtype);
- if (x == hashers.end())
- return false;
-
- /* Make an MD5 hash of the password for using in the query */
- std::string md5_pass_hash = HashRequest(this, x->second, password).hex();
-
- /* We generate our own sum here because some database providers (e.g. SQLite) dont have a builtin md5/sha256 function,
- * also hashing it in the module and only passing a remote query containing a hash is more secure.
- */
- SQLrequest req = SQLrequest(this, target, databaseid,
- SQLquery("SELECT username, password, hostname, type FROM ircd_opers WHERE username = '?' AND password='?'") % username % md5_pass_hash);
-
- /* When we get the query response from the service provider we will be given an ID to play with,
- * just an ID number which is unique to this query. We need a way of associating that ID with a User
- * so we insert it into a map mapping the IDs to users.
- * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
- * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
- * us to discard the query.
- */
- AssociateUser(this, SQLutils, req.id, user).Send();
-
- saved_user.set(user, username);
- saved_pass.set(user, password);
-
- return true;
- }
- else
- {
- ServerInstance->Logs->Log("m_sqloper",SPARSE, "WARNING: Couldn't find SQL provider module. NOBODY will be able to oper up unless their o:line is statically configured");
- return false;
- }
- }
-
- void OnRequest(Request& request)
- {
- if (strcmp(SQLRESID, request.id) == 0)
- {
- SQLresult* res = static_cast<SQLresult*>(&request);
-
- User* user = GetAssocUser(this, SQLutils, res->id).S().user;
- UnAssociate(this, SQLutils, res->id).S();
-
- if (user)
- {
- std::string* tried_user = saved_user.get(user);
- std::string* tried_pass = saved_pass.get(user);
- if (res->error.Id() == SQL_NO_ERROR)
- {
- if (res->Rows())
- {
- /* We got a row in the result, this means there was a record for the oper..
- * now we just need to check if their host matches, and if it does then
- * oper them up.
- *
- * We now (previous versions of the module didn't) support multiple SQL
- * rows per-oper in the same way the config file does, all rows will be tried
- * until one is found which matches. This is useful to define several different
- * hosts for a single oper.
- *
- * The for() loop works as SQLresult::GetRowMap() returns an empty map when there
- * are no more rows to return.
- */
-
- for (SQLfieldMap& row = res->GetRowMap(); row.size(); row = res->GetRowMap())
- {
- if (OperUser(user, row["hostname"].d, row["type"].d))
- {
- /* If/when one of the rows matches, stop checking and return */
- saved_user.unset(user);
- saved_pass.unset(user);
- }
- if (tried_user && tried_pass)
- {
- LoginFail(user, *tried_user, *tried_pass);
- saved_user.unset(user);
- saved_pass.unset(user);
- }
- }
- }
- else
- {
- /* No rows in result, this means there was no oper line for the user,
- * we should have already checked the o:lines so now we need an
- * "insufficient awesomeness" (invalid credentials) error
- */
- if (tried_user && tried_pass)
- {
- LoginFail(user, *tried_user, *tried_pass);
- saved_user.unset(user);
- saved_pass.unset(user);
- }
- }
- }
- else
- {
- /* This one shouldn't happen, the query failed for some reason.
- * We have to fail the /oper request and give them the same error
- * as above.
- */
- if (tried_user && tried_pass)
- {
- LoginFail(user, *tried_user, *tried_pass);
- saved_user.unset(user);
- saved_pass.unset(user);
- }
-
- }
- }
- }
- }
-
- void LoginFail(User* user, const std::string &username, const std::string &pass)
- {