1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 #include "configreader.h"
21 #include "m_sqlutils.h"
23 #include "commands/cmd_oper.h"
25 /* $ModDesc: Allows storage of oper credentials in an SQL table */
26 /* $ModDep: m_sqlv2.h m_sqlutils.h */
28 class ModuleSQLOper : public Module
33 std::string databaseid;
36 ModuleSQLOper(InspIRCd* Me)
37 : Module::Module(Me), Srv(Me)
39 ServerInstance->UseInterface("SQLutils");
40 ServerInstance->UseInterface("SQL");
41 ServerInstance->UseInterface("HashRequest");
43 /* Attempt to locate the md5 service provider, bail if we can't find it */
44 HashModule = ServerInstance->FindModule("m_md5.so");
46 throw ModuleException("Can't find m_md5.so. Please load m_md5.so before m_sqloper.so.");
48 SQLutils = ServerInstance->FindModule("m_sqlutils.so");
50 throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqloper.so.");
55 virtual ~ModuleSQLOper()
57 ServerInstance->DoneWithInterface("SQL");
58 ServerInstance->DoneWithInterface("SQLutils");
59 ServerInstance->DoneWithInterface("HashRequest");
62 void Implements(char* List)
64 List[I_OnRequest] = List[I_OnRehash] = List[I_OnPreCommand] = 1;
67 virtual void OnRehash(userrec* user, const std::string ¶meter)
69 ConfigReader Conf(Srv);
71 databaseid = Conf.ReadValue("sqloper", "dbid", 0); /* Database ID of a database configured for the service provider module */
74 virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
76 if ((validated) && (command == "OPER"))
78 if (LookupOper(user, parameters[0], parameters[1]))
80 /* Returning true here just means the query is in progress, or on it's way to being
81 * in progress. Nothing about the /oper actually being successful..
82 * If the oper lookup fails later, we pass the command to the original handler
83 * for /oper by calling its Handle method directly.
91 bool LookupOper(userrec* user, const std::string &username, const std::string &password)
95 target = Srv->FindFeature("SQL");
99 /* Reset hash module first back to MD5 standard state */
100 HashResetRequest(this, HashModule).Send();
101 /* Make an MD5 hash of the password for using in the query */
102 std::string md5_pass_hash = HashSumRequest(this, HashModule, password.c_str()).Send();
104 /* We generate our own MD5 sum here because some database providers (e.g. SQLite) dont have a builtin md5 function,
105 * also hashing it in the module and only passing a remote query containing a hash is more secure.
108 SQLrequest req = SQLreq(this, target, databaseid, "SELECT username, password, hostname, type FROM ircd_opers WHERE username = '?' AND password='?'", username, md5_pass_hash);
112 /* When we get the query response from the service provider we will be given an ID to play with,
113 * just an ID number which is unique to this query. We need a way of associating that ID with a userrec
114 * so we insert it into a map mapping the IDs to users.
115 * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
116 * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
117 * us to discard the query.
119 AssociateUser(this, SQLutils, req.id, user).Send();
121 user->Extend("oper_user", strdup(username.c_str()));
122 user->Extend("oper_pass", strdup(password.c_str()));
133 ServerInstance->Log(SPARSE, "WARNING: Couldn't find SQL provider module. NOBODY will be able to oper up unless their o:line is statically configured");
138 virtual char* OnRequest(Request* request)
140 if (strcmp(SQLRESID, request->GetId()) == 0)
142 SQLresult* res = static_cast<SQLresult*>(request);
144 userrec* user = GetAssocUser(this, SQLutils, res->id).S().user;
145 UnAssociate(this, SQLutils, res->id).S();
147 char* tried_user = NULL;
148 char* tried_pass = NULL;
150 user->GetExt("oper_user", tried_user);
151 user->GetExt("oper_pass", tried_pass);
155 if (res->error.Id() == NO_ERROR)
159 /* We got a row in the result, this means there was a record for the oper..
160 * now we just need to check if their host matches, and if it does then
163 * We now (previous versions of the module didn't) support multiple SQL
164 * rows per-oper in the same way the config file does, all rows will be tried
165 * until one is found which matches. This is useful to define several different
166 * hosts for a single oper.
168 * The for() loop works as SQLresult::GetRowMap() returns an empty map when there
169 * are no more rows to return.
172 for (SQLfieldMap& row = res->GetRowMap(); row.size(); row = res->GetRowMap())
174 if (OperUser(user, row["username"].d, row["password"].d, row["hostname"].d, row["type"].d))
176 /* If/when one of the rows matches, stop checking and return */
179 if (tried_user && tried_pass)
181 LoginFail(user, tried_user, tried_pass);
184 user->Shrink("oper_user");
185 user->Shrink("oper_pass");
191 /* No rows in result, this means there was no oper line for the user,
192 * we should have already checked the o:lines so now we need an
193 * "insufficient awesomeness" (invalid credentials) error
195 if (tried_user && tried_pass)
197 LoginFail(user, tried_user, tried_pass);
200 user->Shrink("oper_user");
201 user->Shrink("oper_pass");
207 /* This one shouldn't happen, the query failed for some reason.
208 * We have to fail the /oper request and give them the same error
211 if (tried_user && tried_pass)
213 LoginFail(user, tried_user, tried_pass);
216 user->Shrink("oper_user");
217 user->Shrink("oper_pass");
229 void LoginFail(userrec* user, const std::string &username, const std::string &pass)
231 command_t* oper_command = ServerInstance->Parser->GetHandler("OPER");
235 const char* params[] = { username.c_str(), pass.c_str() };
236 oper_command->Handle(params, 2, user);
240 ServerInstance->Log(DEBUG, "BUG: WHAT?! Why do we have no OPER command?!");
244 bool OperUser(userrec* user, const std::string &username, const std::string &password, const std::string &pattern, const std::string &type)
246 ConfigReader Conf(Srv);
248 for (int j = 0; j < Conf.Enumerate("type"); j++)
250 std::string tname = Conf.ReadValue("type","name",j);
251 std::string hostname(user->ident);
253 hostname.append("@").append(user->host);
255 if ((tname == type) && OneOfMatches(hostname.c_str(), user->GetIPString(), pattern.c_str()))
257 /* Opertype and host match, looks like this is it. */
258 std::string operhost = Conf.ReadValue("type", "host", j);
261 user->ChangeDisplayedHost(operhost.c_str());
263 Srv->SNO->WriteToSnoMask('o',"%s (%s@%s) is now an IRC operator of type %s", user->nick, user->ident, user->host, type.c_str());
264 user->WriteServ("381 %s :You are now an IRC operator of type %s", user->nick, type.c_str());
266 if (!user->modes[UM_OPERATOR])
276 virtual Version GetVersion()
278 return Version(1,1,1,0,VF_VENDOR,API_VERSION);
283 class ModuleSQLOperFactory : public ModuleFactory
286 ModuleSQLOperFactory()
290 ~ModuleSQLOperFactory()
294 virtual Module * CreateModule(InspIRCd* Me)
296 return new ModuleSQLOper(Me);
302 extern "C" void * init_module( void )
304 return new ModuleSQLOperFactory;