1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 * ---------------------------------------------------
19 #include "m_sqlutils.h"
22 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
23 /* $ModDep: m_sqlv2.h m_sqlutils.h m_hash.h */
25 class ModuleSQLAuth : public Module
30 std::string freeformquery;
31 std::string killreason;
32 std::string allowpattern;
33 std::string databaseid;
38 ModuleSQLAuth(InspIRCd* Me)
41 ServerInstance->Modules->UseInterface("SQLutils");
42 ServerInstance->Modules->UseInterface("SQL");
44 SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
46 throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
48 SQLprovider = ServerInstance->Modules->FindFeature("SQL");
50 throw ModuleException("Can't find an SQL provider module. Please load one before attempting to load m_sqlauth.");
53 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRequest, I_OnRehash, I_OnUserRegister };
54 ServerInstance->Modules->Attach(eventlist, this, 5);
57 virtual ~ModuleSQLAuth()
59 ServerInstance->Modules->DoneWithInterface("SQL");
60 ServerInstance->Modules->DoneWithInterface("SQLutils");
64 virtual void OnRehash(User* user, const std::string ¶meter)
66 ConfigReader Conf(ServerInstance);
68 databaseid = Conf.ReadValue("sqlauth", "dbid", 0); /* Database ID, given to the SQL service provider */
69 freeformquery = Conf.ReadValue("sqlauth", "query", 0); /* Field name where username can be found */
70 killreason = Conf.ReadValue("sqlauth", "killreason", 0); /* Reason to give when access is denied to a user (put your reg details here) */
71 allowpattern = Conf.ReadValue("sqlauth", "allowpattern",0 ); /* Allow nicks matching this pattern without requiring auth */
72 verbose = Conf.ReadFlag("sqlauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
75 virtual int OnUserRegister(User* user)
77 if ((!allowpattern.empty()) && (ServerInstance->MatchText(user->nick,allowpattern)))
79 user->Extend("sqlauthed");
83 if (!CheckCredentials(user))
85 User::QuitUser(ServerInstance,user,killreason);
90 void SearchAndReplace(std::string& newline, const std::string &find, const std::string &replace)
92 std::string::size_type x = newline.find(find);
93 while (x != std::string::npos)
95 newline.erase(x, find.length());
97 newline.insert(x, replace);
98 x = newline.find(find);
103 bool CheckCredentials(User* user)
105 std::string thisquery = freeformquery;
106 std::string safepass = user->password;
108 /* Search and replace the escaped nick and escaped pass into the query */
110 SearchAndReplace(safepass, "\"", "");
112 SearchAndReplace(thisquery, "$nick", user->nick);
113 SearchAndReplace(thisquery, "$pass", safepass);
114 SearchAndReplace(thisquery, "$host", user->host);
115 SearchAndReplace(thisquery, "$ip", user->GetIPString());
117 Module* HashMod = ServerInstance->Modules->Find("m_md5.so");
121 HashResetRequest(this, HashMod).Send();
122 SearchAndReplace(thisquery, "$md5pass", HashSumRequest(this, HashMod, user->password).Send());
125 HashMod = ServerInstance->Modules->Find("m_sha256.so");
129 HashResetRequest(this, HashMod).Send();
130 SearchAndReplace(thisquery, "$sha256pass", HashSumRequest(this, HashMod, user->password).Send());
133 /* Build the query */
134 SQLrequest req = SQLrequest(this, SQLprovider, databaseid, SQLquery(thisquery));
138 /* When we get the query response from the service provider we will be given an ID to play with,
139 * just an ID number which is unique to this query. We need a way of associating that ID with a User
140 * so we insert it into a map mapping the IDs to users.
141 * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
142 * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
143 * us to discard the query.
145 AssociateUser(this, SQLutils, req.id, user).Send();
152 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, req.error.Str());
157 virtual const char* OnRequest(Request* request)
159 if(strcmp(SQLRESID, request->GetId()) == 0)
161 SQLresult* res = static_cast<SQLresult*>(request);
163 User* user = GetAssocUser(this, SQLutils, res->id).S().user;
164 UnAssociate(this, SQLutils, res->id).S();
168 if(res->error.Id() == NO_ERROR)
172 /* We got a row in the result, this is enough really */
173 user->Extend("sqlauthed");
177 /* No rows in result, this means there was no record matching the user */
178 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick, user->ident, user->host);
179 user->Extend("sqlauth_failed");
184 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, res->error.Str());
185 user->Extend("sqlauth_failed");
193 if (!user->GetExt("sqlauthed"))
195 User::QuitUser(ServerInstance,user,killreason);
202 virtual void OnUserDisconnect(User* user)
204 user->Shrink("sqlauthed");
205 user->Shrink("sqlauth_failed");
208 virtual bool OnCheckReady(User* user)
210 return user->GetExt("sqlauthed");
213 virtual Version GetVersion()
215 return Version(1,2,1,0,VF_VENDOR,API_VERSION);
220 MODULE_INIT(ModuleSQLAuth)