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 * ---------------------------------------------------
21 #include <sys/types.h>
22 #include <sys/socket.h>
35 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
39 class ModuleSQLAuth : public Module
42 std::string usertable;
43 std::string userfield;
44 std::string passfield;
45 std::string encryption;
46 std::string killreason;
47 std::string allowpattern;
55 Conf = new ConfigReader();
56 usertable = Conf->ReadValue("sqlauth","usertable",0); // user table name
57 dbid = Conf->ReadInteger("sqlauth","dbid",0,true); // database id of a database configured in m_sql (see m_sql config)
58 userfield = Conf->ReadValue("sqlauth","userfield",0); // field name where username can be found
59 passfield = Conf->ReadValue("sqlauth","passfield",0); // field name where password can be found
60 killreason = Conf->ReadValue("sqlauth","killreason",0); // reason to give when access is denied to a user (put your reg details here)
61 encryption = Conf->ReadValue("sqlauth","encryption",0); // name of sql function used to encrypt password, e.g. "md5" or "passwd".
62 // define, but leave blank if no encryption is to be used.
63 WallOperFail = Conf->ReadFlag("sqlauth","verbose",0); // set to 1 if failed connects should be reported to operators
64 allowpattern = Conf->ReadValue("sqlauth","allowpattern",0); // allow nicks matching the pattern without requiring auth
66 SQLModule = Srv->FindModule("m_sql.so");
68 Srv->Log(DEFAULT,"WARNING: m_sqlauth.so could not initialize because m_sql.so is not loaded. Load the module and rehash your server.");
78 virtual void OnRehash()
83 virtual void OnUserRegister(userrec* user)
85 if ((allowpattern != "") && (Srv->MatchText(user->nick,allowpattern)))
88 if (!CheckCredentials(user->nick,user->password))
91 WriteOpers("Forbidden connection from %s!%s@%s (invalid login/password)",user->nick,user->ident,user->host);
92 Srv->QuitUser(user,killreason);
96 bool CheckCredentials(std::string username, std::string password)
100 // is the sql module loaded? If not, we don't attempt to do anything.
104 // sanitize the password (we dont want any mysql insertion exploits!)
105 std::string temp = "";
106 for (int q = 0; q < password.length(); q++)
108 if (password[q] == '\'')
112 else if (password[q] == '"')
114 temp = temp + "\\\"";
116 else temp = temp + password[q];
120 // Create a request containing the SQL query and send it to m_sql.so
121 SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT * FROM "+usertable+" WHERE "+userfield+"='"+username+"' AND "+passfield+"="+encryption+"('"+password+"')");
122 Request queryrequest((char*)query, this, SQLModule);
123 SQLResult* result = (SQLResult*)queryrequest.Send();
125 // Did we get "OK" as a result?
126 if (result->GetType() == SQL_OK)
129 // if we did, this means we may now request a row... there should be only one row for each user, so,
130 // we don't need to loop to fetch multiple rows.
131 SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
132 Request rowquery((char*)rowrequest, this, SQLModule);
133 SQLResult* rowresult = (SQLResult*)rowquery.Send();
135 // did we get a row? If we did, we can now do something with the fields
136 if (rowresult->GetType() == SQL_ROW)
138 if (rowresult->GetField(userfield) == username)
140 // because the query directly asked for the password hash, we do not need to check it -
141 // if it didnt match it wont be returned in the first place from the SELECT.
142 // This just checks we didnt get an empty row by accident.
149 // we didn't have a row.
160 query->SetQueryType(SQL_DONE);
161 query->SetConnID(dbid);
162 Request donerequest((char*)query, this, SQLModule);
168 virtual ~ModuleSQLAuth()
173 virtual Version GetVersion()
175 return Version(1,0,0,1,VF_VENDOR);
180 class ModuleSQLAuthFactory : public ModuleFactory
183 ModuleSQLAuthFactory()
187 ~ModuleSQLAuthFactory()
191 virtual Module * CreateModule()
193 return new ModuleSQLAuth;
199 extern "C" void * init_module( void )
201 return new ModuleSQLAuthFactory;