]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqloper.cpp
Fixed removal of mesh linking from core
[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()
59         {
60                 Srv = new Server;
61                 Conf = new ConfigReader();
62                 ReadConfig();
63         }
64
65         virtual void OnRehash()
66         {
67                 delete Conf;
68                 Conf = new ConfigReader();
69                 ReadConfig();
70         }
71
72         virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
73         {
74                 if (command == "OPER")
75                 {
76                         if (LookupOper(parameters[0],parameters[1],user))
77                                 return 1;
78                 }
79                 return 0;
80         }
81
82         bool LookupOper(std::string username, std::string password, userrec* user)
83         {
84                 bool found = false;
85
86                 // is the sql module loaded? If not, we don't attempt to do anything.
87                 if (!SQLModule)
88                         return false;
89
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++)
93                 {
94                         if (password[q] == '\'')
95                         {
96                                 temp = temp + "\'";
97                         }
98                         else if (password[q] == '"')
99                         {
100                                 temp = temp + "\\\"";
101                         }
102                         else temp = temp + password[q];
103                 }
104                 password = temp;
105                 temp = "";
106                 for (unsigned int v = 0; v < username.length(); v++)
107                 {
108                         if (username[v] == '\'')
109                         {
110                                 temp = temp + "\'";
111                         }
112                         if (username[v] == '"')
113                         {
114                                 temp = temp + "\\\"";
115                         }
116                         else temp = temp + username[v];
117                 }
118                 username = temp;
119
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();
124
125                 // Did we get "OK" as a result?
126                 if (result->GetType() == SQL_OK)
127                 {
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();
134
135                         // did we get a row? If we did, we can now do something with the fields
136                         if (rowresult->GetType() == SQL_ROW)
137                         {
138                                 if (rowresult->GetField("username") == username)
139                                 {
140                                         found = true;
141                                         // oper up the user.
142                                         for (int j =0; j < Conf->Enumerate("type"); j++)
143                                         {
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"))));
147                                                 {
148                                                         /* found this oper's opertype */
149                                                         std::string HostName = Conf->ReadValue("type","host",j);
150                                                         if (HostName != "")
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'))
156                                                         {
157                                                                 strcat(user->modes,"o");
158                                                                 WriteServ(user->fd,"MODE %s :+o",user->nick);
159                                                                 Module* Logger = Srv->FindModule("m_sqllog.so");
160                                                                 if (Logger)
161                                                                         Logger->OnOper(user);
162                                                                 AddOper(user);
163                                                                 log(DEFAULT,"OPER: %s!%s@%s opered as type: %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
164                                                         }
165                                                         break;
166                                                 }
167                                         }
168
169                                 }
170                                 delete rowresult;
171                         }
172                         else
173                         {
174                                 // we didn't have a row.
175                                 found = false;
176                         }
177                         delete rowrequest;
178                         delete result;
179                 }
180                 else
181                 {
182                         // the query was bad
183                         found = false;
184                 }
185                 query->SetQueryType(SQL_DONE);
186                 query->SetConnID(dbid);
187                 Request donerequest((char*)query, this, SQLModule);
188                 donerequest.Send();
189                 delete query;
190                 return found;
191         }
192
193         virtual ~ModuleSQLOper()
194         {
195                 delete Conf;
196                 delete Srv;
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()
218         {
219                 return new ModuleSQLOper;
220         }
221         
222 };
223
224
225 extern "C" void * init_module( void )
226 {
227         return new ModuleSQLOperFactory;
228 }
229