1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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.");
55 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRequest, I_OnRehash, I_OnUserRegister };
56 ServerInstance->Modules->Attach(eventlist, this, 5);
59 virtual ~ModuleSQLAuth()
61 ServerInstance->Modules->DoneWithInterface("SQL");
62 ServerInstance->Modules->DoneWithInterface("SQLutils");
66 virtual void OnRehash(User* user, const std::string ¶meter)
68 ConfigReader Conf(ServerInstance);
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(User* user)
89 if ((!allowpattern.empty()) && (ServerInstance->MatchText(user->nick,allowpattern)))
91 user->Extend("sqlauthed");
95 if (!CheckCredentials(user))
97 User::QuitUser(ServerInstance,user,killreason);
103 bool CheckCredentials(User* user)
105 SQLrequest req = SQLrequest(this, SQLprovider, databaseid, SQLquery("SELECT ? FROM ? WHERE ? = '?' AND ? = ?'?')") % userfield % usertable % userfield % user->nick %
106 passfield % encryption % user->password);
110 /* When we get the query response from the service provider we will be given an ID to play with,
111 * just an ID number which is unique to this query. We need a way of associating that ID with a User
112 * so we insert it into a map mapping the IDs to users.
113 * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
114 * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
115 * us to discard the query.
117 AssociateUser(this, SQLutils, req.id, user).Send();
124 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, req.error.Str());
129 virtual const char* OnRequest(Request* request)
131 if(strcmp(SQLRESID, request->GetId()) == 0)
133 SQLresult* res = static_cast<SQLresult*>(request);
135 User* user = GetAssocUser(this, SQLutils, res->id).S().user;
136 UnAssociate(this, SQLutils, res->id).S();
140 if(res->error.Id() == NO_ERROR)
144 /* We got a row in the result, this is enough really */
145 user->Extend("sqlauthed");
149 /* No rows in result, this means there was no record matching the user */
150 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick, user->ident, user->host);
151 user->Extend("sqlauth_failed");
156 ServerInstance->SNO->WriteToSnoMask('A', "Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, res->error.Str());
157 user->Extend("sqlauth_failed");
165 if (!user->GetExt("sqlauthed"))
167 User::QuitUser(ServerInstance,user,killreason);
174 virtual void OnUserDisconnect(User* user)
176 user->Shrink("sqlauthed");
177 user->Shrink("sqlauth_failed");
180 virtual bool OnCheckReady(User* user)
182 return user->GetExt("sqlauthed");
185 virtual Version GetVersion()
187 return Version(1,1,1,0,VF_VENDOR,API_VERSION);
192 MODULE_INIT(ModuleSQLAuth)