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