]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqloper.cpp
TESTME: Fix to this to make it work with newer OnPreCommand event (a parameter was...
[user/henk/code/inspircd.git] / src / modules / extra / m_sqloper.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/time.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <poll.h>
31 #include "users.h"
32 #include "channels.h"
33 #include "modules.h"
34 #include "inspircd.h"
35 #include "helperfuncs.h"
36 #include "m_sql.h"
37
38 /* $ModDesc: Allows storage of oper credentials in an SQL table */
39
40 /* Required for the FOREACH_MOD alias (OnOper event) */
41 extern int MODCOUNT;
42 extern std::vector<Module*> modules;
43 extern std::vector<ircd_module*> factory;
44
45 Server *Srv;
46
47 class ModuleSQLOper : public Module
48 {
49         ConfigReader* Conf;
50         unsigned long dbid;
51         Module* SQLModule;
52
53  public:
54         bool ReadConfig()
55         {
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");
58                 if (!SQLModule)
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.");
60                 return (SQLModule);
61         }
62
63         ModuleSQLOper(Server* Me)
64                 : Module::Module(Me)
65         {
66                 Srv = Me;
67                 Conf = new ConfigReader();
68                 ReadConfig();
69         }
70
71         virtual void OnRehash(std::string parameter)
72         {
73                 delete Conf;
74                 Conf = new ConfigReader();
75                 ReadConfig();
76         }
77
78         void Implements(char* List)
79         {
80                 List[I_OnRehash] = List[I_OnPreCommand] = 1;
81         }
82
83         virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated)
84         {
85                 if ((command == "OPER") && (validated))
86                 {
87                         if (LookupOper(parameters[0],parameters[1],user))
88                                 return 1;
89                 }
90                 return 0;
91         }
92
93         bool LookupOper(std::string username, std::string password, userrec* user)
94         {
95                 bool found = false;
96
97                 // is the sql module loaded? If not, we don't attempt to do anything.
98                 if (!SQLModule)
99                         return false;
100
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++)
104                 {
105                         if (password[q] == '\'')
106                         {
107                                 temp = temp + "\'";
108                         }
109                         else if (password[q] == '"')
110                         {
111                                 temp = temp + "\\\"";
112                         }
113                         else temp = temp + password[q];
114                 }
115                 password = temp;
116                 temp = "";
117                 for (unsigned int v = 0; v < username.length(); v++)
118                 {
119                         if (username[v] == '\'')
120                         {
121                                 temp = temp + "\'";
122                         }
123                         if (username[v] == '"')
124                         {
125                                 temp = temp + "\\\"";
126                         }
127                         else temp = temp + username[v];
128                 }
129                 username = temp;
130
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();
135
136                 // Did we get "OK" as a result?
137                 if (result->GetType() == SQL_OK)
138                 {
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();
145
146                         // did we get a row? If we did, we can now do something with the fields
147                         if (rowresult->GetType() == SQL_ROW)
148                         {
149                                 if (rowresult->GetField("username") == username)
150                                 {
151                                         found = true;
152                                         // oper up the user.
153                                         for (int j =0; j < Conf->Enumerate("type"); j++)
154                                         {
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"))));
158                                                 {
159                                                         /* found this oper's opertype */
160                                                         std::string HostName = Conf->ReadValue("type","host",j);
161                                                         if (HostName != "")
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'))
167                                                         {
168                                                                 strcat(user->modes,"o");
169                                                                 WriteServ(user->fd,"MODE %s :+o",user->nick);
170                                                                 FOREACH_MOD(I_OnOper,OnOper(user,rowresult->GetField("type")));
171                                                                 AddOper(user);
172                                                                 log(DEFAULT,"OPER: %s!%s@%s opered as type: %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
173                                                         }
174                                                         break;
175                                                 }
176                                         }
177
178                                 }
179                                 delete rowresult;
180                         }
181                         else
182                         {
183                                 // we didn't have a row.
184                                 found = false;
185                         }
186                         delete rowrequest;
187                         delete result;
188                 }
189                 else
190                 {
191                         // the query was bad
192                         found = false;
193                 }
194                 query->SetQueryType(SQL_DONE);
195                 query->SetConnID(dbid);
196                 Request donerequest((char*)query, this, SQLModule);
197                 donerequest.Send();
198                 delete query;
199                 return found;
200         }
201
202         virtual ~ModuleSQLOper()
203         {
204                 delete Conf;
205         }
206         
207         virtual Version GetVersion()
208         {
209                 return Version(1,0,0,1,VF_VENDOR);
210         }
211         
212 };
213
214 class ModuleSQLOperFactory : public ModuleFactory
215 {
216  public:
217         ModuleSQLOperFactory()
218         {
219         }
220         
221         ~ModuleSQLOperFactory()
222         {
223         }
224         
225         virtual Module * CreateModule(Server* Me)
226         {
227                 return new ModuleSQLOper(Me);
228         }
229         
230 };
231
232
233 extern "C" void * init_module( void )
234 {
235         return new ModuleSQLOperFactory;
236 }
237