]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqloper.cpp
Review and optimize
[user/henk/code/inspircd.git] / src / modules / extra / m_sqloper.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire 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 Server *Srv;
41
42 class ModuleSQLOper : public Module
43 {
44         ConfigReader* Conf;
45         unsigned long dbid;
46         Module* SQLModule;
47
48  public:
49         bool ReadConfig()
50         {
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");
53                 if (!SQLModule)
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.");
55                 return (SQLModule);
56         }
57
58         ModuleSQLOper(Server* Me)
59                 : Module::Module(Me)
60         {
61                 Srv = Me;
62                 Conf = new ConfigReader();
63                 ReadConfig();
64         }
65
66         virtual void OnRehash(std::string parameter)
67         {
68                 delete Conf;
69                 Conf = new ConfigReader();
70                 ReadConfig();
71         }
72
73         virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
74         {
75                 if (command == "OPER")
76                 {
77                         if (LookupOper(parameters[0],parameters[1],user))
78                                 return 1;
79                 }
80                 return 0;
81         }
82
83         bool LookupOper(std::string username, std::string password, userrec* user)
84         {
85                 bool found = false;
86
87                 // is the sql module loaded? If not, we don't attempt to do anything.
88                 if (!SQLModule)
89                         return false;
90
91                 // sanitize the password (we dont want any mysql insertion exploits!)
92                 std::string temp = "";
93                 for (unsigned int q = 0; q < password.length(); q++)
94                 {
95                         if (password[q] == '\'')
96                         {
97                                 temp = temp + "\'";
98                         }
99                         else if (password[q] == '"')
100                         {
101                                 temp = temp + "\\\"";
102                         }
103                         else temp = temp + password[q];
104                 }
105                 password = temp;
106                 temp = "";
107                 for (unsigned int v = 0; v < username.length(); v++)
108                 {
109                         if (username[v] == '\'')
110                         {
111                                 temp = temp + "\'";
112                         }
113                         if (username[v] == '"')
114                         {
115                                 temp = temp + "\\\"";
116                         }
117                         else temp = temp + username[v];
118                 }
119                 username = temp;
120
121                 // Create a request containing the SQL query and send it to m_sql.so
122                 SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT username,password,hostname,type FROM ircd_opers WHERE username='"+username+"' AND password=md5('"+password+"')");
123                 Request queryrequest((char*)query, this, SQLModule);
124                 SQLResult* result = (SQLResult*)queryrequest.Send();
125
126                 // Did we get "OK" as a result?
127                 if (result->GetType() == SQL_OK)
128                 {
129                         Srv->Log(DEBUG,"An SQL based oper exists");
130                         // if we did, this means we may now request a row... there should be only one row for each user, so,
131                         // we don't need to loop to fetch multiple rows.
132                         SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
133                         Request rowquery((char*)rowrequest, this, SQLModule);
134                         SQLResult* rowresult = (SQLResult*)rowquery.Send();
135
136                         // did we get a row? If we did, we can now do something with the fields
137                         if (rowresult->GetType() == SQL_ROW)
138                         {
139                                 if (rowresult->GetField("username") == username)
140                                 {
141                                         found = true;
142                                         // oper up the user.
143                                         for (int j =0; j < Conf->Enumerate("type"); j++)
144                                         {
145                                                 std::string TypeName = Conf->ReadValue("type","name",j);
146                                                 std::string pattern = std::string(user->ident) + "@" + std::string(user->host);
147                                                 if ((TypeName == rowresult->GetField("type")) && (Srv->MatchText(pattern,rowresult->GetField("hostname"))));
148                                                 {
149                                                         /* found this oper's opertype */
150                                                         std::string HostName = Conf->ReadValue("type","host",j);
151                                                         if (HostName != "")
152                                                                 Srv->ChangeHost(user,HostName);
153                                                         strlcpy(user->oper,TypeName.c_str(),NICKMAX);
154                                                         WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
155                                                         WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,rowresult->GetField("type").c_str());
156                                                         if (!strchr(user->modes,'o'))
157                                                         {
158                                                                 strcat(user->modes,"o");
159                                                                 WriteServ(user->fd,"MODE %s :+o",user->nick);
160                                                                 Module* Logger = Srv->FindModule("m_sqllog.so");
161                                                                 if (Logger)
162                                                                         Logger->OnOper(user,rowresult->GetField("type"));
163                                                                 AddOper(user);
164                                                                 log(DEFAULT,"OPER: %s!%s@%s opered as type: %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
165                                                         }
166                                                         break;
167                                                 }
168                                         }
169
170                                 }
171                                 delete rowresult;
172                         }
173                         else
174                         {
175                                 // we didn't have a row.
176                                 found = false;
177                         }
178                         delete rowrequest;
179                         delete result;
180                 }
181                 else
182                 {
183                         // the query was bad
184                         found = false;
185                 }
186                 query->SetQueryType(SQL_DONE);
187                 query->SetConnID(dbid);
188                 Request donerequest((char*)query, this, SQLModule);
189                 donerequest.Send();
190                 delete query;
191                 return found;
192         }
193
194         virtual ~ModuleSQLOper()
195         {
196                 delete Conf;
197         }
198         
199         virtual Version GetVersion()
200         {
201                 return Version(1,0,0,1,VF_VENDOR);
202         }
203         
204 };
205
206 class ModuleSQLOperFactory : public ModuleFactory
207 {
208  public:
209         ModuleSQLOperFactory()
210         {
211         }
212         
213         ~ModuleSQLOperFactory()
214         {
215         }
216         
217         virtual Module * CreateModule(Server* Me)
218         {
219                 return new ModuleSQLOper(Me);
220         }
221         
222 };
223
224
225 extern "C" void * init_module( void )
226 {
227         return new ModuleSQLOperFactory;
228 }
229