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
29 std::string usertable;
30 std::string userfield;
31 std::string passfield;
32 std::string encryption;
33 std::string killreason;
34 std::string allowpattern;
35 std::string databaseid;
40 ModuleSQLAuth(InspIRCd* Me)
43 ServerInstance->Modules->UseInterface("SQLutils");
44 ServerInstance->Modules->UseInterface("SQL");
46 SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
48 throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
50 SQLprovider = ServerInstance->Modules->FindFeature("SQL");
52 throw ModuleException("Can't find an SQL provider module. Please load one before attempting to load m_sqlauth.");
57 virtual ~ModuleSQLAuth()
59 ServerInstance->Modules->DoneWithInterface("SQL");
60 ServerInstance->Modules->DoneWithInterface("SQLutils");
63 void Implements(char* List)
65 List[I_OnUserDisconnect] = List[I_OnCheckReady] = List[I_OnRequest] = List[I_OnRehash] = List[I_OnUserRegister] = 1;
68 virtual void OnRehash(userrec* user, const std::string ¶meter)
70 ConfigReader Conf(ServerInstance);
72 usertable = Conf.ReadValue("sqlauth", "usertable", 0); /* User table name */
73 databaseid = Conf.ReadValue("sqlauth", "dbid", 0); /* Database ID, given to the SQL service provider */
74 userfield = Conf.ReadValue("sqlauth", "userfield", 0); /* Field name where username can be found */
75 passfield = Conf.ReadValue("sqlauth", "passfield", 0); /* Field name where password can be found */
76 killreason = Conf.ReadValue("sqlauth", "killreason", 0); /* Reason to give when access is denied to a user (put your reg details here) */
77 allowpattern= Conf.ReadValue("sqlauth", "allowpattern",0 ); /* Allow nicks matching this pattern without requiring auth */
78 encryption = Conf.ReadValue("sqlauth", "encryption", 0); /* Name of sql function used to encrypt password, e.g. "md5" or "passwd".
79 * define, but leave blank if no encryption is to be used.
81 verbose = Conf.ReadFlag("sqlauth", "verbose", 0); /* Set to true if failed connects should be reported to operators */
83 if (encryption.find("(") == std::string::npos)
85 encryption.append("(");
89 virtual int OnUserRegister(userrec* user)
91 if ((!allowpattern.empty()) && (ServerInstance->MatchText(user->nick,allowpattern)))
93 user->Extend("sqlauthed");
97 if (!CheckCredentials(user))
99 userrec::QuitUser(ServerInstance,user,killreason);
105 bool CheckCredentials(userrec* user)
107 SQLrequest req = SQLreq(this, SQLprovider, databaseid, "SELECT ? FROM ? WHERE ? = '?' AND ? = ?'?')", userfield, usertable, userfield, user->nick, passfield, encryption, user->password);
111 /* When we get the query response from the service provider we will be given an ID to play with,
112 * just an ID number which is unique to this query. We need a way of associating that ID with a userrec
113 * so we insert it into a map mapping the IDs to users.
114 * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
115 * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
116 * us to discard the query.
118 AssociateUser(this, SQLutils, req.id, user).Send();
125 ServerInstance->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, req.error.Str());
130 virtual char* OnRequest(Request* request)
132 if(strcmp(SQLRESID, request->GetId()) == 0)
134 SQLresult* res = static_cast<SQLresult*>(request);
136 userrec* user = GetAssocUser(this, SQLutils, res->id).S().user;
137 UnAssociate(this, SQLutils, res->id).S();
141 if(res->error.Id() == NO_ERROR)
145 /* We got a row in the result, this is enough really */
146 user->Extend("sqlauthed");
150 /* No rows in result, this means there was no record matching the user */
151 ServerInstance->WriteOpers("Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick, user->ident, user->host);
152 user->Extend("sqlauth_failed");
157 ServerInstance->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, res->error.Str());
158 user->Extend("sqlauth_failed");
166 if (!user->GetExt("sqlauthed"))
168 userrec::QuitUser(ServerInstance,user,killreason);
175 virtual void OnUserDisconnect(userrec* user)
177 user->Shrink("sqlauthed");
178 user->Shrink("sqlauth_failed");
181 virtual bool OnCheckReady(userrec* user)
183 return user->GetExt("sqlauthed");
186 virtual Version GetVersion()
188 return Version(1,1,1,0,VF_VENDOR,API_VERSION);
193 MODULE_INIT(ModuleSQLAuth);