1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd 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: Allows storage of oper credentials in an SQL table */
40 /* Required for the FOREACH_MOD alias (OnOper event) */
42 extern std::vector<Module*> modules;
43 extern std::vector<ircd_module*> factory;
47 class ModuleSQLOper : public Module
56 dbid = Conf->ReadInteger("sqloper","dbid",0,true); // database id of a database configured in m_sql (see m_sql config)
57 SQLModule = Srv->FindModule("m_sql.so");
59 Srv->Log(DEFAULT,"WARNING: m_sqloper.so could not initialize because m_sql.so is not loaded. Load the module and rehash your server.");
63 ModuleSQLOper(Server* Me)
67 Conf = new ConfigReader();
71 virtual void OnRehash(std::string parameter)
74 Conf = new ConfigReader();
78 void Implements(char* List)
80 List[I_OnRehash] = List[I_OnPreCommand] = 1;
83 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated)
85 if ((command == "OPER") && (validated))
87 if (LookupOper(parameters[0],parameters[1],user))
93 bool LookupOper(std::string username, std::string password, userrec* user)
97 // is the sql module loaded? If not, we don't attempt to do anything.
101 // sanitize the password (we dont want any mysql insertion exploits!)
102 std::string temp = "";
103 for (unsigned int q = 0; q < password.length(); q++)
105 if (password[q] == '\'')
109 else if (password[q] == '"')
111 temp = temp + "\\\"";
113 else temp = temp + password[q];
117 for (unsigned int v = 0; v < username.length(); v++)
119 if (username[v] == '\'')
123 if (username[v] == '"')
125 temp = temp + "\\\"";
127 else temp = temp + username[v];
131 // Create a request containing the SQL query and send it to m_sql.so
132 SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT username,password,hostname,type FROM ircd_opers WHERE username='"+username+"' AND password=md5('"+password+"')");
133 Request queryrequest((char*)query, this, SQLModule);
134 SQLResult* result = (SQLResult*)queryrequest.Send();
136 // Did we get "OK" as a result?
137 if (result->GetType() == SQL_OK)
139 Srv->Log(DEBUG,"An SQL based oper exists");
140 // if we did, this means we may now request a row... there should be only one row for each user, so,
141 // we don't need to loop to fetch multiple rows.
142 SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
143 Request rowquery((char*)rowrequest, this, SQLModule);
144 SQLResult* rowresult = (SQLResult*)rowquery.Send();
146 // did we get a row? If we did, we can now do something with the fields
147 if (rowresult->GetType() == SQL_ROW)
149 if (rowresult->GetField("username") == username)
153 for (int j =0; j < Conf->Enumerate("type"); j++)
155 std::string TypeName = Conf->ReadValue("type","name",j);
156 std::string pattern = std::string(user->ident) + "@" + std::string(user->host);
157 if ((TypeName == rowresult->GetField("type")) && (Srv->MatchText(pattern,rowresult->GetField("hostname"))));
159 /* found this oper's opertype */
160 std::string HostName = Conf->ReadValue("type","host",j);
162 Srv->ChangeHost(user,HostName);
163 strlcpy(user->oper,TypeName.c_str(),NICKMAX);
164 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
165 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,rowresult->GetField("type").c_str());
166 if (!strchr(user->modes,'o'))
168 strcat(user->modes,"o");
169 WriteServ(user->fd,"MODE %s :+o",user->nick);
170 FOREACH_MOD(I_OnOper,OnOper(user,rowresult->GetField("type")));
172 log(DEFAULT,"OPER: %s!%s@%s opered as type: %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
183 // we didn't have a row.
194 query->SetQueryType(SQL_DONE);
195 query->SetConnID(dbid);
196 Request donerequest((char*)query, this, SQLModule);
202 virtual ~ModuleSQLOper()
207 virtual Version GetVersion()
209 return Version(1,0,0,1,VF_VENDOR);
214 class ModuleSQLOperFactory : public ModuleFactory
217 ModuleSQLOperFactory()
221 ~ModuleSQLOperFactory()
225 virtual Module * CreateModule(Server* Me)
227 return new ModuleSQLOper(Me);
233 extern "C" void * init_module( void )
235 return new ModuleSQLOperFactory;