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 * ---------------------------------------------------
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
24 LocalIntExt sqlAuthed;
28 std::string freeformquery;
29 std::string killreason;
30 std::string allowpattern;
31 std::string databaseid;
36 ModuleSQLAuth() : sqlAuthed("sqlauth", this)
38 SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
40 throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
42 ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_DATA, "SQL");
44 throw ModuleException("Can't find an SQL provider module. Please load one before attempting to load m_sqlauth.");
45 SQLprovider = prov->creator;
48 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRehash, I_OnUserRegister };
49 ServerInstance->Modules->Attach(eventlist, this, 4);
52 virtual ~ModuleSQLAuth()
56 void OnRehash(User* user)
60 databaseid = Conf.ReadValue("sqlauth", "dbid", 0); /* Database ID, given to the SQL service provider */
61 freeformquery = Conf.ReadValue("sqlauth", "query", 0); /* Field name where username can be found */
62 killreason = Conf.ReadValue("sqlauth", "killreason", 0); /* Reason to give when access is denied to a user (put your reg details here) */
63 allowpattern = Conf.ReadValue("sqlauth", "allowpattern",0 ); /* Allow nicks matching this pattern without requiring auth */
64 verbose = Conf.ReadFlag("sqlauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
67 ModResult OnUserRegister(LocalUser* user)
69 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
71 sqlAuthed.set(user, 1);
72 return MOD_RES_PASSTHRU;
75 if (!CheckCredentials(user))
77 ServerInstance->Users->QuitUser(user, killreason);
80 return MOD_RES_PASSTHRU;
83 bool CheckCredentials(LocalUser* user)
85 std::string thisquery = freeformquery;
86 std::string safepass = user->password;
87 std::string safegecos = user->fullname;
89 /* Search and replace the escaped nick and escaped pass into the query */
91 SearchAndReplace(safepass, std::string("\""), std::string("\\\""));
92 SearchAndReplace(safegecos, std::string("\""), std::string("\\\""));
94 SearchAndReplace(thisquery, std::string("$nick"), user->nick);
95 SearchAndReplace(thisquery, std::string("$pass"), safepass);
96 SearchAndReplace(thisquery, std::string("$host"), user->host);
97 SearchAndReplace(thisquery, std::string("$ip"), std::string(user->GetIPString()));
98 SearchAndReplace(thisquery, std::string("$gecos"), safegecos);
99 SearchAndReplace(thisquery, std::string("$ident"), user->ident);
100 SearchAndReplace(thisquery, std::string("$server"), std::string(user->server));
101 SearchAndReplace(thisquery, std::string("$uuid"), user->uuid);
103 HashProvider* md5 = ServerInstance->Modules->FindDataService<HashProvider>("hash/md5");
105 SearchAndReplace(thisquery, std::string("$md5pass"), md5->hexsum(user->password));
107 HashProvider* sha256 = ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256");
109 SearchAndReplace(thisquery, std::string("$sha256pass"), sha256->hexsum(user->password));
111 /* Build the query */
112 SQLrequest req = SQLrequest(this, SQLprovider, databaseid, SQLquery(thisquery));
115 /* When we get the query response from the service provider we will be given an ID to play with,
116 * just an ID number which is unique to this query. We need a way of associating that ID with a User
117 * so we insert it into a map mapping the IDs to users.
118 * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
119 * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
120 * us to discard the query.
122 AssociateUser(this, SQLutils, req.id, user).Send();
127 void OnRequest(Request& request)
129 if(strcmp(SQLRESID, request.id) == 0)
131 SQLresult* res = static_cast<SQLresult*>(&request);
133 User* user = GetAssocUser(this, SQLutils, res->id).S().user;
134 UnAssociate(this, SQLutils, res->id).S();
138 if(res->error.Id() == SQL_NO_ERROR)
142 /* We got a row in the result, this is enough really */
143 sqlAuthed.set(user, 1);
147 /* No rows in result, this means there was no record matching the user */
148 ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
153 ServerInstance->SNO->WriteGlobalSno('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());
161 if (!sqlAuthed.get(user))
163 ServerInstance->Users->QuitUser(user, killreason);
168 ModResult OnCheckReady(LocalUser* user)
170 return sqlAuthed.get(user) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
175 return Version("Allow/Deny connections based upon an arbitary SQL table", VF_VENDOR);
180 MODULE_INIT(ModuleSQLAuth)