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: Allows storage of oper credentials in an SQL table */
42 class ModuleSQLOper : public Module
51 dbid = Conf->ReadInteger("sqloper","dbid",0,true); // database id of a database configured in m_sql (see m_sql config)
52 SQLModule = Srv->FindModule("m_sql.so");
54 Srv->Log(DEFAULT,"WARNING: m_sqloper.so could not initialize because m_sql.so is not loaded. Load the module and rehash your server.");
61 Conf = new ConfigReader();
65 virtual void OnRehash()
68 Conf = new ConfigReader();
72 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
74 if (command == "OPER")
76 if (LookupOper(parameters[0],parameters[1],user))
82 bool LookupOper(std::string username, std::string password, userrec* user)
86 // is the sql module loaded? If not, we don't attempt to do anything.
90 // sanitize the password (we dont want any mysql insertion exploits!)
91 std::string temp = "";
92 for (unsigned int q = 0; q < password.length(); q++)
94 if (password[q] == '\'')
98 else if (password[q] == '"')
100 temp = temp + "\\\"";
102 else temp = temp + password[q];
106 for (unsigned int v = 0; v < username.length(); v++)
108 if (username[v] == '\'')
112 if (username[v] == '"')
114 temp = temp + "\\\"";
116 else temp = temp + username[v];
120 // Create a request containing the SQL query and send it to m_sql.so
121 SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT username,password,hostname,type FROM ircd_opers WHERE username='"+username+"' AND password=md5('"+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)
128 Srv->Log(DEBUG,"An SQL based oper exists");
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("username") == username)
142 for (int j =0; j < Conf->Enumerate("type"); j++)
144 std::string TypeName = Conf->ReadValue("type","name",j);
145 std::string pattern = std::string(user->ident) + "@" + std::string(user->host);
146 if ((TypeName == rowresult->GetField("type")) && (Srv->MatchText(pattern,rowresult->GetField("hostname"))));
148 /* found this oper's opertype */
149 std::string HostName = Conf->ReadValue("type","host",j);
151 Srv->ChangeHost(user,HostName);
152 strlcpy(user->oper,TypeName.c_str(),NICKMAX);
153 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
154 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,rowresult->GetField("type").c_str());
155 if (!strchr(user->modes,'o'))
157 strcat(user->modes,"o");
158 WriteServ(user->fd,"MODE %s :+o",user->nick);
159 Module* Logger = Srv->FindModule("m_sqllog.so");
161 Logger->OnOper(user);
163 log(DEFAULT,"OPER: %s!%s@%s opered as type: %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
174 // we didn't have a row.
185 query->SetQueryType(SQL_DONE);
186 query->SetConnID(dbid);
187 Request donerequest((char*)query, this, SQLModule);
193 virtual ~ModuleSQLOper()
199 virtual Version GetVersion()
201 return Version(1,0,0,1,VF_VENDOR);
206 class ModuleSQLOperFactory : public ModuleFactory
209 ModuleSQLOperFactory()
213 ~ModuleSQLOperFactory()
217 virtual Module * CreateModule()
219 return new ModuleSQLOper;
225 extern "C" void * init_module( void )
227 return new ModuleSQLOperFactory;