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 * ---------------------------------------------------
19 #include "m_sqlutils.h"
21 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
22 /* $ModDep: m_sqlv2.h m_sqlutils.h */
24 class ModuleSQLAuth : public Module
30 std::string usertable;
31 std::string userfield;
32 std::string passfield;
33 std::string encryption;
34 std::string killreason;
35 std::string allowpattern;
36 std::string databaseid;
41 ModuleSQLAuth(InspIRCd* Me)
42 : Module::Module(Me), Srv(Me)
44 ServerInstance->UseInterface("SQLutils");
45 ServerInstance->UseInterface("SQL");
47 SQLutils = ServerInstance->FindModule("m_sqlutils.so");
49 throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
51 SQLprovider = Srv->FindFeature("SQL");
53 throw ModuleException("Can't find an SQL provider module. Please load one before attempting to load m_sqlauth.");
58 virtual ~ModuleSQLAuth()
60 ServerInstance->DoneWithInterface("SQL");
61 ServerInstance->DoneWithInterface("SQLutils");
64 void Implements(char* List)
66 List[I_OnUserDisconnect] = List[I_OnCheckReady] = List[I_OnRequest] = List[I_OnRehash] = List[I_OnUserRegister] = 1;
69 virtual void OnRehash(userrec* user, const std::string ¶meter)
71 ConfigReader Conf(Srv);
73 usertable = Conf.ReadValue("sqlauth", "usertable", 0); /* User table name */
74 databaseid = Conf.ReadValue("sqlauth", "dbid", 0); /* Database ID, given to the SQL service provider */
75 userfield = Conf.ReadValue("sqlauth", "userfield", 0); /* Field name where username can be found */
76 passfield = Conf.ReadValue("sqlauth", "passfield", 0); /* Field name where password can be found */
77 killreason = Conf.ReadValue("sqlauth", "killreason", 0); /* Reason to give when access is denied to a user (put your reg details here) */
78 allowpattern= Conf.ReadValue("sqlauth", "allowpattern",0 ); /* Allow nicks matching this pattern without requiring auth */
79 encryption = Conf.ReadValue("sqlauth", "encryption", 0); /* Name of sql function used to encrypt password, e.g. "md5" or "passwd".
80 * define, but leave blank if no encryption is to be used.
82 verbose = Conf.ReadFlag("sqlauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
84 if (encryption.find("(") == std::string::npos)
86 encryption.append("(");
90 virtual int OnUserRegister(userrec* user)
92 if ((allowpattern != "") && (Srv->MatchText(user->nick,allowpattern)))
94 user->Extend("sqlauthed");
98 if (!CheckCredentials(user))
100 userrec::QuitUser(Srv,user,killreason);
106 bool CheckCredentials(userrec* user)
108 SQLrequest req = SQLreq(this, SQLprovider, databaseid, "SELECT ? FROM ? WHERE ? = '?' AND ? = ?'?')", userfield, usertable, userfield, user->nick, passfield, encryption, user->password);
112 /* When we get the query response from the service provider we will be given an ID to play with,
113 * just an ID number which is unique to this query. We need a way of associating that ID with a userrec
114 * so we insert it into a map mapping the IDs to users.
115 * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
116 * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
117 * us to discard the query.
119 AssociateUser(this, SQLutils, req.id, user).Send();
126 Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, req.error.Str());
131 virtual char* OnRequest(Request* request)
133 if(strcmp(SQLRESID, request->GetId()) == 0)
135 SQLresult* res = static_cast<SQLresult*>(request);
137 userrec* user = GetAssocUser(this, SQLutils, res->id).S().user;
138 UnAssociate(this, SQLutils, res->id).S();
142 if(res->error.Id() == NO_ERROR)
146 /* We got a row in the result, this is enough really */
147 user->Extend("sqlauthed");
151 /* No rows in result, this means there was no record matching the user */
152 Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick, user->ident, user->host);
153 user->Extend("sqlauth_failed");
158 Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, res->error.Str());
159 user->Extend("sqlauth_failed");
167 if (!user->GetExt("sqlauthed"))
169 userrec::QuitUser(Srv,user,killreason);
176 virtual void OnUserDisconnect(userrec* user)
178 user->Shrink("sqlauthed");
179 user->Shrink("sqlauth_failed");
182 virtual bool OnCheckReady(userrec* user)
184 return user->GetExt("sqlauthed");
187 virtual Version GetVersion()
189 return Version(1,1,1,0,VF_VENDOR,API_VERSION);
194 class ModuleSQLAuthFactory : public ModuleFactory
197 ModuleSQLAuthFactory()
201 ~ModuleSQLAuthFactory()
205 virtual Module * CreateModule(InspIRCd* Me)
207 return new ModuleSQLAuth(Me);
213 extern "C" void * init_module( void )
215 return new ModuleSQLAuthFactory;