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 * ---------------------------------------------------
22 #include "m_sqlutils.h"
24 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
25 /* $ModDep: m_sqlv2.h m_sqlutils.h */
27 class ModuleSQLAuth : public Module
32 std::string usertable;
33 std::string userfield;
34 std::string passfield;
35 std::string encryption;
36 std::string killreason;
37 std::string allowpattern;
38 std::string databaseid;
43 ModuleSQLAuth(InspIRCd* Me)
44 : Module::Module(Me), Srv(Me)
46 SQLutils = Srv->FindFeature("SQLutils");
50 ServerInstance->Log(DEBUG, "Successfully got SQLutils pointer");
54 ServerInstance->Log(DEFAULT, "ERROR: This module requires a module offering the 'SQLutils' feature (usually m_sqlutils.so). Please load it and try again.");
55 throw ModuleException("This module requires a module offering the 'SQLutils' feature (usually m_sqlutils.so). Please load it and try again.");
61 void Implements(char* List)
63 List[I_OnUserDisconnect] = List[I_OnCheckReady] = List[I_OnRequest] = List[I_OnRehash] = List[I_OnUserRegister] = 1;
66 virtual void OnRehash(const std::string ¶meter)
68 ConfigReader Conf(Srv);
70 usertable = Conf.ReadValue("sqlauth", "usertable", 0); /* User table name */
71 databaseid = Conf.ReadValue("sqlauth", "dbid", 0); /* Database ID, given to the SQL service provider */
72 userfield = Conf.ReadValue("sqlauth", "userfield", 0); /* Field name where username can be found */
73 passfield = Conf.ReadValue("sqlauth", "passfield", 0); /* Field name where password can be found */
74 killreason = Conf.ReadValue("sqlauth", "killreason", 0); /* Reason to give when access is denied to a user (put your reg details here) */
75 allowpattern= Conf.ReadValue("sqlauth", "allowpattern",0 ); /* Allow nicks matching this pattern without requiring auth */
76 encryption = Conf.ReadValue("sqlauth", "encryption", 0); /* Name of sql function used to encrypt password, e.g. "md5" or "passwd".
77 * define, but leave blank if no encryption is to be used.
79 verbose = Conf.ReadFlag("sqlauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
81 if (encryption.find("(") == std::string::npos)
83 encryption.append("(");
87 virtual int OnUserRegister(userrec* user)
89 if ((allowpattern != "") && (Srv->MatchText(user->nick,allowpattern)))
91 user->Extend("sqlauthed");
95 if (!CheckCredentials(user))
97 userrec::QuitUser(Srv,user,killreason);
103 bool CheckCredentials(userrec* user)
107 target = Srv->FindFeature("SQL");
111 SQLrequest req = SQLreq(this, target, databaseid, "SELECT ? FROM ? WHERE ? = '?' AND ? = ?'?')", userfield, usertable, userfield, user->nick, passfield, encryption, user->password);
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 userrec
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 ServerInstance->Log(DEBUG, "Sent query, got given ID %lu", req.id);
124 AssociateUser(this, SQLutils, req.id, user).Send();
130 ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
133 Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, req.error.Str());
140 ServerInstance->Log(SPARSE, "WARNING: Couldn't find SQL provider module. NOBODY will be allowed to connect until it comes back unless they match an exception");
145 virtual char* OnRequest(Request* request)
147 if(strcmp(SQLRESID, request->GetId()) == 0)
151 res = static_cast<SQLresult*>(request);
153 ServerInstance->Log(DEBUG, "Got SQL result (%s) with ID %lu", res->GetId(), res->id);
155 userrec* user = GetAssocUser(this, SQLutils, res->id).S().user;
156 UnAssociate(this, SQLutils, res->id).S();
160 if(res->error.Id() == NO_ERROR)
162 ServerInstance->Log(DEBUG, "Associated query ID %lu with user %s", res->id, user->nick);
163 ServerInstance->Log(DEBUG, "Got result with %d rows and %d columns", res->Rows(), res->Cols());
167 /* We got a row in the result, this is enough really */
168 user->Extend("sqlauthed");
172 /* No rows in result, this means there was no record matching the user */
173 Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick, user->ident, user->host);
174 user->Extend("sqlauth_failed");
179 ServerInstance->Log(DEBUG, "Query failed: %s", res->error.Str());
180 Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, res->error.Str());
181 user->Extend("sqlauth_failed");
186 ServerInstance->Log(DEBUG, "Got query with unknown ID, this probably means the user quit while the query was in progress");
190 if (!user->GetExt("sqlauthed"))
192 userrec::QuitUser(Srv,user,killreason);
197 ServerInstance->Log(DEBUG, "Got unsupported API version string: %s", request->GetId());
202 virtual void OnUserDisconnect(userrec* user)
204 user->Shrink("sqlauthed");
205 user->Shrink("sqlauth_failed");
208 virtual bool OnCheckReady(userrec* user)
210 return user->GetExt("sqlauthed");
213 virtual ~ModuleSQLAuth()
217 virtual Version GetVersion()
219 return Version(1,1,1,0,VF_VENDOR,API_VERSION);
224 class ModuleSQLAuthFactory : public ModuleFactory
227 ModuleSQLAuthFactory()
231 ~ModuleSQLAuthFactory()
235 virtual Module * CreateModule(InspIRCd* Me)
237 return new ModuleSQLAuth(Me);
243 extern "C" void * init_module( void )
245 return new ModuleSQLAuthFactory;