1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
23 #include <sys/types.h>
24 #include <sys/socket.h>
35 #include "helperfuncs.h"
38 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
42 class ModuleSQLAuth : public Module
45 std::string usertable;
46 std::string userfield;
47 std::string passfield;
48 std::string encryption;
49 std::string killreason;
50 std::string allowpattern;
58 Conf = new ConfigReader();
59 usertable = Conf->ReadValue("sqlauth","usertable",0); // user table name
60 dbid = Conf->ReadInteger("sqlauth","dbid",0,true); // database id of a database configured in m_sql (see m_sql config)
61 userfield = Conf->ReadValue("sqlauth","userfield",0); // field name where username can be found
62 passfield = Conf->ReadValue("sqlauth","passfield",0); // field name where password can be found
63 killreason = Conf->ReadValue("sqlauth","killreason",0); // reason to give when access is denied to a user (put your reg details here)
64 encryption = Conf->ReadValue("sqlauth","encryption",0); // name of sql function used to encrypt password, e.g. "md5" or "passwd".
65 // define, but leave blank if no encryption is to be used.
66 WallOperFail = Conf->ReadFlag("sqlauth","verbose",0); // set to 1 if failed connects should be reported to operators
67 allowpattern = Conf->ReadValue("sqlauth","allowpattern",0); // allow nicks matching the pattern without requiring auth
69 SQLModule = Srv->FindModule("m_sql.so");
71 Srv->Log(DEFAULT,"WARNING: m_sqlauth.so could not initialize because m_sql.so is not loaded. Load the module and rehash your server.");
81 virtual void OnRehash()
86 virtual void OnUserRegister(userrec* user)
88 if ((allowpattern != "") && (Srv->MatchText(user->nick,allowpattern)))
91 if (!CheckCredentials(user->nick,user->password))
94 WriteOpers("Forbidden connection from %s!%s@%s (invalid login/password)",user->nick,user->ident,user->host);
95 Srv->QuitUser(user,killreason);
99 bool CheckCredentials(std::string username, std::string password)
103 // is the sql module loaded? If not, we don't attempt to do anything.
107 // sanitize the password (we dont want any mysql insertion exploits!)
108 std::string temp = "";
109 for (unsigned int q = 0; q < password.length(); q++)
111 if (password[q] == '\'')
115 else if (password[q] == '"')
117 temp = temp + "\\\"";
119 else temp = temp + password[q];
123 // Create a request containing the SQL query and send it to m_sql.so
124 SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT * FROM "+usertable+" WHERE "+userfield+"='"+username+"' AND "+passfield+"="+encryption+"('"+password+"')");
125 Request queryrequest((char*)query, this, SQLModule);
126 SQLResult* result = (SQLResult*)queryrequest.Send();
128 // Did we get "OK" as a result?
129 if (result->GetType() == SQL_OK)
132 // if we did, this means we may now request a row... there should be only one row for each user, so,
133 // we don't need to loop to fetch multiple rows.
134 SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
135 Request rowquery((char*)rowrequest, this, SQLModule);
136 SQLResult* rowresult = (SQLResult*)rowquery.Send();
138 // did we get a row? If we did, we can now do something with the fields
139 if (rowresult->GetType() == SQL_ROW)
141 if (rowresult->GetField(userfield) == username)
143 // because the query directly asked for the password hash, we do not need to check it -
144 // if it didnt match it wont be returned in the first place from the SELECT.
145 // This just checks we didnt get an empty row by accident.
152 // we didn't have a row.
163 query->SetQueryType(SQL_DONE);
164 query->SetConnID(dbid);
165 Request donerequest((char*)query, this, SQLModule);
171 virtual ~ModuleSQLAuth()
176 virtual Version GetVersion()
178 return Version(1,0,0,1,VF_VENDOR);
183 class ModuleSQLAuthFactory : public ModuleFactory
186 ModuleSQLAuthFactory()
190 ~ModuleSQLAuthFactory()
194 virtual Module * CreateModule()
196 return new ModuleSQLAuth;
202 extern "C" void * init_module( void )
204 return new ModuleSQLAuthFactory;