1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 * ---------------------------------------------------
16 #include "m_sqlutils.h"
19 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
20 /* $ModDep: m_sqlv2.h m_sqlutils.h m_hash.h */
22 class ModuleSQLAuth : public Module
27 std::string freeformquery;
28 std::string killreason;
29 std::string allowpattern;
30 std::string databaseid;
35 ModuleSQLAuth(InspIRCd* Me)
38 ServerInstance->Modules->UseInterface("SQLutils");
39 ServerInstance->Modules->UseInterface("SQL");
41 SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
43 throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
45 SQLprovider = ServerInstance->Modules->FindFeature("SQL");
47 throw ModuleException("Can't find an SQL provider module. Please load one before attempting to load m_sqlauth.");
50 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRequest, I_OnRehash, I_OnUserRegister };
51 ServerInstance->Modules->Attach(eventlist, this, 5);
54 virtual ~ModuleSQLAuth()
56 ServerInstance->Modules->DoneWithInterface("SQL");
57 ServerInstance->Modules->DoneWithInterface("SQLutils");
61 virtual void OnRehash(User* user, const std::string ¶meter)
63 ConfigReader Conf(ServerInstance);
65 databaseid = Conf.ReadValue("sqlauth", "dbid", 0); /* Database ID, given to the SQL service provider */
66 freeformquery = Conf.ReadValue("sqlauth", "query", 0); /* Field name where username can be found */
67 killreason = Conf.ReadValue("sqlauth", "killreason", 0); /* Reason to give when access is denied to a user (put your reg details here) */
68 allowpattern = Conf.ReadValue("sqlauth", "allowpattern",0 ); /* Allow nicks matching this pattern without requiring auth */
69 verbose = Conf.ReadFlag("sqlauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
72 virtual int OnUserRegister(User* user)
74 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
76 user->Extend("sqlauthed");
80 if (!CheckCredentials(user))
82 ServerInstance->Users->QuitUser(user, killreason);
88 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
90 std::string::size_type x = newline.find(find);
91 while (x != std::string::npos)
93 newline.erase(x, find.length());
95 newline.insert(x, replace);
96 x = newline.find(find);
100 bool CheckCredentials(User* user)
102 std::string thisquery = freeformquery;
103 std::string safepass = user->password;
105 /* Search and replace the escaped nick and escaped pass into the query */
107 SearchAndReplace(safepass, "\"", "");
109 SearchAndReplace(thisquery, "$nick", user->nick);
110 SearchAndReplace(thisquery, "$pass", safepass);
111 SearchAndReplace(thisquery, "$host", user->host);
112 SearchAndReplace(thisquery, "$ip", user->GetIPString());
114 Module* HashMod = ServerInstance->Modules->Find("m_md5.so");
118 HashResetRequest(this, HashMod).Send();
119 SearchAndReplace(thisquery, "$md5pass", HashSumRequest(this, HashMod, user->password).Send());
122 HashMod = ServerInstance->Modules->Find("m_sha256.so");
126 HashResetRequest(this, HashMod).Send();
127 SearchAndReplace(thisquery, "$sha256pass", HashSumRequest(this, HashMod, user->password).Send());
130 /* Build the query */
131 SQLrequest req = SQLrequest(this, SQLprovider, databaseid, SQLquery(thisquery));
135 /* When we get the query response from the service provider we will be given an ID to play with,
136 * just an ID number which is unique to this query. We need a way of associating that ID with a User
137 * so we insert it into a map mapping the IDs to users.
138 * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
139 * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
140 * us to discard the query.
142 AssociateUser(this, SQLutils, req.id, user).Send();
149 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), req.error.Str());
154 virtual const char* OnRequest(Request* request)
156 if(strcmp(SQLRESID, request->GetId()) == 0)
158 SQLresult* res = static_cast<SQLresult*>(request);
160 User* user = GetAssocUser(this, SQLutils, res->id).S().user;
161 UnAssociate(this, SQLutils, res->id).S();
165 if(res->error.Id() == SQL_NO_ERROR)
169 /* We got a row in the result, this is enough really */
170 user->Extend("sqlauthed");
174 /* No rows in result, this means there was no record matching the user */
175 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
176 user->Extend("sqlauth_failed");
181 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), res->error.Str());
182 user->Extend("sqlauth_failed");
190 if (!user->GetExt("sqlauthed"))
192 ServerInstance->Users->QuitUser(user, killreason);
199 virtual void OnUserDisconnect(User* user)
201 user->Shrink("sqlauthed");
202 user->Shrink("sqlauth_failed");
205 virtual bool OnCheckReady(User* user)
207 return user->GetExt("sqlauthed");
210 virtual Version GetVersion()
212 return Version("$Id$", VF_VENDOR, API_VERSION);
217 MODULE_INIT(ModuleSQLAuth)