1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
11 * Written by Craig Edwards, Craig McLure, and others.
12 * This program is free but copyrighted software; see
13 * the file COPYING for details.
15 * ---------------------------------------------------
26 #include "m_sqlutils.h"
28 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
29 /* $ModDep: m_sqlv2.h m_sqlutils.h */
31 class ModuleSQLAuth : public Module
36 std::string usertable;
37 std::string userfield;
38 std::string passfield;
39 std::string encryption;
40 std::string killreason;
41 std::string allowpattern;
42 std::string databaseid;
47 ModuleSQLAuth(InspIRCd* Me)
48 : Module::Module(Me), Srv(Me)
50 SQLutils = Srv->FindFeature("SQLutils");
54 ServerInstance->Log(DEBUG, "Successfully got SQLutils pointer");
58 ServerInstance->Log(DEFAULT, "ERROR: This module requires a module offering the 'SQLutils' feature (usually m_sqlutils.so). Please load it and try again.");
59 throw ModuleException("This module requires a module offering the 'SQLutils' feature (usually m_sqlutils.so). Please load it and try again.");
65 void Implements(char* List)
67 List[I_OnUserDisconnect] = List[I_OnCheckReady] = List[I_OnRequest] = List[I_OnRehash] = List[I_OnUserRegister] = 1;
70 virtual void OnRehash(const std::string ¶meter)
72 ConfigReader Conf(Srv);
74 usertable = Conf.ReadValue("sqlauth", "usertable", 0); /* User table name */
75 databaseid = Conf.ReadValue("sqlauth", "dbid", 0); /* Database ID, given to the SQL service provider */
76 userfield = Conf.ReadValue("sqlauth", "userfield", 0); /* Field name where username can be found */
77 passfield = Conf.ReadValue("sqlauth", "passfield", 0); /* Field name where password can be found */
78 killreason = Conf.ReadValue("sqlauth", "killreason", 0); /* Reason to give when access is denied to a user (put your reg details here) */
79 allowpattern= Conf.ReadValue("sqlauth", "allowpattern",0 ); /* Allow nicks matching this pattern without requiring auth */
80 encryption = Conf.ReadValue("sqlauth", "encryption", 0); /* Name of sql function used to encrypt password, e.g. "md5" or "passwd".
81 * define, but leave blank if no encryption is to be used.
83 verbose = Conf.ReadFlag("sqlauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
85 if (encryption.find("(") == std::string::npos)
87 encryption.append("(");
91 virtual int OnUserRegister(userrec* user)
93 if ((allowpattern != "") && (Srv->MatchText(user->nick,allowpattern)))
95 user->Extend("sqlauthed");
99 if (!CheckCredentials(user))
101 userrec::QuitUser(Srv,user,killreason);
107 bool CheckCredentials(userrec* user)
111 target = Srv->FindFeature("SQL");
115 SQLrequest req = SQLreq(this, target, databaseid, "SELECT ? FROM ? WHERE ? = '?' AND ? = ?'?')", userfield, usertable, userfield, user->nick, passfield, encryption, user->password);
119 /* When we get the query response from the service provider we will be given an ID to play with,
120 * just an ID number which is unique to this query. We need a way of associating that ID with a userrec
121 * so we insert it into a map mapping the IDs to users.
122 * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
123 * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
124 * us to discard the query.
126 ServerInstance->Log(DEBUG, "Sent query, got given ID %lu", req.id);
128 AssociateUser(this, SQLutils, req.id, user).Send();
134 ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
137 Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, req.error.Str());
144 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");
149 virtual char* OnRequest(Request* request)
151 if(strcmp(SQLRESID, request->GetId()) == 0)
155 res = static_cast<SQLresult*>(request);
157 ServerInstance->Log(DEBUG, "Got SQL result (%s) with ID %lu", res->GetId(), res->id);
159 userrec* user = GetAssocUser(this, SQLutils, res->id).S().user;
160 UnAssociate(this, SQLutils, res->id).S();
164 if(res->error.Id() == NO_ERROR)
166 ServerInstance->Log(DEBUG, "Associated query ID %lu with user %s", res->id, user->nick);
167 ServerInstance->Log(DEBUG, "Got result with %d rows and %d columns", res->Rows(), res->Cols());
171 /* We got a row in the result, this is enough really */
172 user->Extend("sqlauthed");
176 /* No rows in result, this means there was no record matching the user */
177 Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick, user->ident, user->host);
178 user->Extend("sqlauth_failed");
183 ServerInstance->Log(DEBUG, "Query failed: %s", res->error.Str());
184 Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, res->error.Str());
185 user->Extend("sqlauth_failed");
190 ServerInstance->Log(DEBUG, "Got query with unknown ID, this probably means the user quit while the query was in progress");
194 if (!user->GetExt("sqlauthed"))
196 userrec::QuitUser(Srv,user,killreason);
201 ServerInstance->Log(DEBUG, "Got unsupported API version string: %s", request->GetId());
206 virtual void OnUserDisconnect(userrec* user)
208 user->Shrink("sqlauthed");
209 user->Shrink("sqlauth_failed");
212 virtual bool OnCheckReady(userrec* user)
214 return user->GetExt("sqlauthed");
217 virtual ~ModuleSQLAuth()
221 virtual Version GetVersion()
223 return Version(1,1,1,0,VF_VENDOR,API_VERSION);
228 class ModuleSQLAuthFactory : public ModuleFactory
231 ModuleSQLAuthFactory()
235 ~ModuleSQLAuthFactory()
239 virtual Module * CreateModule(InspIRCd* Me)
241 return new ModuleSQLAuth(Me);
247 extern "C" void * init_module( void )
249 return new ModuleSQLAuthFactory;