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: Allows storage of oper credentials in an SQL table */
39 class ModuleSQLOper : public Module
48 dbid = Conf->ReadInteger("sqloper","dbid",0,true); // database id of a database configured in m_sql (see m_sql config)
49 SQLModule = Srv->FindModule("m_sql.so");
51 Srv->Log(DEFAULT,"WARNING: m_sqloper.so could not initialize because m_sql.so is not loaded. Load the module and rehash your server.");
58 Conf = new ConfigReader();
62 virtual void OnRehash()
65 Conf = new ConfigReader();
69 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
71 if (command == "OPER")
73 if (LookupOper(parameters[0],parameters[1],user))
79 bool LookupOper(std::string username, std::string password, userrec* user)
83 // is the sql module loaded? If not, we don't attempt to do anything.
87 // sanitize the password (we dont want any mysql insertion exploits!)
88 std::string temp = "";
89 for (int q = 0; q < password.length(); q++)
91 if (password[q] == '\'')
95 else if (password[q] == '"')
99 else temp = temp + password[q];
103 for (int v = 0; v < username.length(); v++)
105 if (username[v] == '\'')
109 if (username[v] == '"')
111 temp = temp + "\\\"";
113 else temp = temp + username[v];
117 // Create a request containing the SQL query and send it to m_sql.so
118 SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT username,password,hostname,type FROM ircd_opers WHERE username='"+username+"' AND password=md5('"+password+"')");
119 Request queryrequest((char*)query, this, SQLModule);
120 SQLResult* result = (SQLResult*)queryrequest.Send();
122 // Did we get "OK" as a result?
123 if (result->GetType() == SQL_OK)
125 Srv->Log(DEBUG,"An SQL based oper exists");
126 // if we did, this means we may now request a row... there should be only one row for each user, so,
127 // we don't need to loop to fetch multiple rows.
128 SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
129 Request rowquery((char*)rowrequest, this, SQLModule);
130 SQLResult* rowresult = (SQLResult*)rowquery.Send();
132 // did we get a row? If we did, we can now do something with the fields
133 if (rowresult->GetType() == SQL_ROW)
135 if (rowresult->GetField("username") == username)
139 for (int j =0; j < Conf->Enumerate("type"); j++)
141 std::string TypeName = Conf->ReadValue("type","name",j);
142 std::string pattern = std::string(user->ident) + "@" + std::string(user->host);
143 if ((TypeName == rowresult->GetField("type")) && (Srv->MatchText(pattern,rowresult->GetField("hostname"))));
145 /* found this oper's opertype */
146 Srv->MeshSendAll("| "+std::string(user->nick)+" "+TypeName);
147 std::string HostName = Conf->ReadValue("type","host",j);
148 Srv->ChangeHost(user,HostName);
149 strlcpy(user->oper,TypeName.c_str(),NICKMAX);
150 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
151 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,rowresult->GetField("type").c_str());
152 if (!strchr(user->modes,'o'))
154 strcat(user->modes,"o");
155 WriteServ(user->fd,"MODE %s :+o",user->nick);
156 Srv->MeshSendAll("M "+std::string(user->nick)+" +o");
157 Module* Logger = Srv->FindModule("m_sqllog.so");
159 Logger->OnOper(user);
160 log(DEFAULT,"OPER: %s!%s@%s opered as type: %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
171 // we didn't have a row.
182 query->SetQueryType(SQL_DONE);
183 query->SetConnID(dbid);
184 Request donerequest((char*)query, this, SQLModule);
190 virtual ~ModuleSQLOper()
196 virtual Version GetVersion()
198 return Version(1,0,0,1,VF_VENDOR);
203 class ModuleSQLOperFactory : public ModuleFactory
206 ModuleSQLOperFactory()
210 ~ModuleSQLOperFactory()
214 virtual Module * CreateModule()
216 return new ModuleSQLOper;
222 extern "C" void * init_module( void )
224 return new ModuleSQLOperFactory;