]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlite3.cpp
Convert remaining InspIRCd::Log() calls to new logging system
[user/henk/code/inspircd.git] / src / modules / extra / m_sqlite3.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <sqlite3.h>
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19
20 #include "m_sqlv2.h"
21
22 /* $ModDesc: sqlite3 provider */
23 /* $CompileFlags: pkgconfversion("sqlite3","3.3") pkgconfincludes("sqlite3","/sqlite3.h","") */
24 /* $LinkerFlags: pkgconflibs("sqlite3","/libsqlite3.so","-lsqlite3") */
25 /* $ModDep: m_sqlv2.h */
26 /* $NoPedantic */
27
28 class SQLConn;
29 class SQLite3Result;
30 class ResultNotifier;
31
32 typedef std::map<std::string, SQLConn*> ConnMap;
33 typedef std::deque<classbase*> paramlist;
34 typedef std::deque<SQLite3Result*> ResultQueue;
35
36 ResultNotifier* resultnotify = NULL;
37
38
39 class ResultNotifier : public BufferedSocket
40 {
41         Module* mod;
42         insp_sockaddr sock_us;
43         socklen_t uslen;
44
45  public:
46         /* Create a socket on a random port. Let the tcp stack allocate us an available port */
47 #ifdef IPV6
48         ResultNotifier(InspIRCd* SI, Module* m) : BufferedSocket(SI, "::1", 0, true, 3000), mod(m)
49 #else
50         ResultNotifier(InspIRCd* SI, Module* m) : BufferedSocket(SI, "127.0.0.1", 0, true, 3000), mod(m)
51 #endif
52         {
53                 uslen = sizeof(sock_us);
54                 if (getsockname(this->fd,(sockaddr*)&sock_us,&uslen))
55                 {
56                         throw ModuleException("Could not create random listening port on localhost");
57                 }
58         }
59
60         ResultNotifier(InspIRCd* SI, Module* m, int newfd, char* ip) : BufferedSocket(SI, newfd, ip), mod(m)
61         {
62         }
63
64         /* Using getsockname and ntohs, we can determine which port number we were allocated */
65         int GetPort()
66         {
67 #ifdef IPV6
68                 return ntohs(sock_us.sin6_port);
69 #else
70                 return ntohs(sock_us.sin_port);
71 #endif
72         }
73
74         virtual int OnIncomingConnection(int newsock, char* ip)
75         {
76                 Dispatch();
77                 return false;
78         }
79
80         void Dispatch();
81 };
82
83
84 class SQLite3Result : public SQLresult
85 {
86   private:
87         int currentrow;
88         int rows;
89         int cols;
90
91         std::vector<std::string> colnames;
92         std::vector<SQLfieldList> fieldlists;
93         SQLfieldList emptyfieldlist;
94
95         SQLfieldList* fieldlist;
96         SQLfieldMap* fieldmap;
97
98   public:
99         SQLite3Result(Module* self, Module* to, unsigned int rid)
100         : SQLresult(self, to, rid), currentrow(0), rows(0), cols(0), fieldlist(NULL), fieldmap(NULL)
101         {
102         }
103
104         ~SQLite3Result()
105         {
106         }
107
108         void AddRow(int colsnum, char **dat, char **colname)
109         {
110                 colnames.clear();
111                 cols = colsnum;
112                 for (int i = 0; i < colsnum; i++)
113                 {
114                         fieldlists.resize(fieldlists.size()+1);
115                         colnames.push_back(colname[i]);
116                         SQLfield sf(dat[i] ? dat[i] : "", dat[i] ? false : true);
117                         fieldlists[rows].push_back(sf);
118                 }
119                 rows++;
120         }
121
122         void UpdateAffectedCount()
123         {
124                 rows++;
125         }
126
127         virtual int Rows()
128         {
129                 return rows;
130         }
131
132         virtual int Cols()
133         {
134                 return cols;
135         }
136
137         virtual std::string ColName(int column)
138         {
139                 if (column < (int)colnames.size())
140                 {
141                         return colnames[column];
142                 }
143                 else
144                 {
145                         throw SQLbadColName();
146                 }
147                 return "";
148         }
149
150         virtual int ColNum(const std::string &column)
151         {
152                 for (unsigned int i = 0; i < colnames.size(); i++)
153                 {
154                         if (column == colnames[i])
155                                 return i;
156                 }
157                 throw SQLbadColName();
158                 return 0;
159         }
160
161         virtual SQLfield GetValue(int row, int column)
162         {
163                 if ((row >= 0) && (row < rows) && (column >= 0) && (column < Cols()))
164                 {
165                         return fieldlists[row][column];
166                 }
167
168                 throw SQLbadColName();
169
170                 /* XXX: We never actually get here because of the throw */
171                 return SQLfield("",true);
172         }
173
174         virtual SQLfieldList& GetRow()
175         {
176                 if (currentrow < rows)
177                         return fieldlists[currentrow];
178                 else
179                         return emptyfieldlist;
180         }
181
182         virtual SQLfieldMap& GetRowMap()
183         {
184                 /* In an effort to reduce overhead we don't actually allocate the map
185                  * until the first time it's needed...so...
186                  */
187                 if(fieldmap)
188                 {
189                         fieldmap->clear();
190                 }
191                 else
192                 {
193                         fieldmap = new SQLfieldMap;
194                 }
195
196                 if (currentrow < rows)
197                 {
198                         for (int i = 0; i < Cols(); i++)
199                         {
200                                 fieldmap->insert(std::make_pair(ColName(i), GetValue(currentrow, i)));
201                         }
202                         currentrow++;
203                 }
204
205                 return *fieldmap;
206         }
207
208         virtual SQLfieldList* GetRowPtr()
209         {
210                 fieldlist = new SQLfieldList();
211
212                 if (currentrow < rows)
213                 {
214                         for (int i = 0; i < Rows(); i++)
215                         {
216                                 fieldlist->push_back(fieldlists[currentrow][i]);
217                         }
218                         currentrow++;
219                 }
220                 return fieldlist;
221         }
222
223         virtual SQLfieldMap* GetRowMapPtr()
224         {
225                 fieldmap = new SQLfieldMap();
226
227                 if (currentrow < rows)
228                 {
229                         for (int i = 0; i < Cols(); i++)
230                         {
231                                 fieldmap->insert(std::make_pair(colnames[i],GetValue(currentrow, i)));
232                         }
233                         currentrow++;
234                 }
235
236                 return fieldmap;
237         }
238
239         virtual void Free(SQLfieldMap* fm)
240         {
241                 delete fm;
242         }
243
244         virtual void Free(SQLfieldList* fl)
245         {
246                 delete fl;
247         }
248
249
250 };
251
252 class SQLConn : public classbase
253 {
254   private:
255         ResultQueue results;
256         InspIRCd* Instance;
257         Module* mod;
258         SQLhost host;
259         sqlite3* conn;
260
261   public:
262         SQLConn(InspIRCd* SI, Module* m, const SQLhost& hi)
263         : Instance(SI), mod(m), host(hi)
264         {
265                 if (OpenDB() != SQLITE_OK)
266                 {
267                         Instance->Logs->Log("m_sqlite3",DEFAULT, "WARNING: Could not open DB with id: " + host.id);
268                         CloseDB();
269                 }
270         }
271
272         ~SQLConn()
273         {
274                 CloseDB();
275         }
276
277         SQLerror Query(SQLrequest &req)
278         {
279                 /* Pointer to the buffer we screw around with substitution in */
280                 char* query;
281
282                 /* Pointer to the current end of query, where we append new stuff */
283                 char* queryend;
284
285                 /* Total length of the unescaped parameters */
286                 unsigned long paramlen;
287
288                 /* Total length of query, used for binary-safety in mysql_real_query */
289                 unsigned long querylength = 0;
290
291                 paramlen = 0;
292                 for(ParamL::iterator i = req.query.p.begin(); i != req.query.p.end(); i++)
293                 {
294                         paramlen += i->size();
295                 }
296
297                 /* To avoid a lot of allocations, allocate enough memory for the biggest the escaped query could possibly be.
298                  * sizeofquery + (totalparamlength*2) + 1
299                  *
300                  * The +1 is for null-terminating the string for mysql_real_escape_string
301                  */
302                 query = new char[req.query.q.length() + (paramlen*2) + 1];
303                 queryend = query;
304
305                 for(unsigned long i = 0; i < req.query.q.length(); i++)
306                 {
307                         if(req.query.q[i] == '?')
308                         {
309                                 if(req.query.p.size())
310                                 {
311                                         char* escaped;
312                                         escaped = sqlite3_mprintf("%q", req.query.p.front().c_str());
313                                         for (char* n = escaped; *n; n++)
314                                         {
315                                                 *queryend = *n;
316                                                 queryend++;
317                                         }
318                                         sqlite3_free(escaped);
319                                         req.query.p.pop_front();
320                                 }
321                                 else
322                                         break;
323                         }
324                         else
325                         {
326                                 *queryend = req.query.q[i];
327                                 queryend++;
328                         }
329                         querylength++;
330                 }
331                 *queryend = 0;
332                 req.query.q = query;
333
334                 SQLite3Result* res = new SQLite3Result(mod, req.GetSource(), req.id);
335                 res->dbid = host.id;
336                 res->query = req.query.q;
337                 paramlist params;
338                 params.push_back(this);
339                 params.push_back(res);
340
341                 char *errmsg = 0;
342                 sqlite3_update_hook(conn, QueryUpdateHook, &params);
343                 if (sqlite3_exec(conn, req.query.q.data(), QueryResult, &params, &errmsg) != SQLITE_OK)
344                 {
345                         std::string error(errmsg);
346                         sqlite3_free(errmsg);
347                         delete[] query;
348                         delete res;
349                         return SQLerror(QSEND_FAIL, error);
350                 }
351                 delete[] query;
352
353                 results.push_back(res);
354                 SendNotify();
355                 return SQLerror();
356         }
357
358         static int QueryResult(void *params, int argc, char **argv, char **azColName)
359         {
360                 paramlist* p = (paramlist*)params;
361                 ((SQLConn*)(*p)[0])->ResultReady(((SQLite3Result*)(*p)[1]), argc, argv, azColName);
362                 return 0;
363         }
364
365         static void QueryUpdateHook(void *params, int eventid, char const * azSQLite, char const * azColName, sqlite_int64 rowid)
366         {
367                 paramlist* p = (paramlist*)params;
368                 ((SQLConn*)(*p)[0])->AffectedReady(((SQLite3Result*)(*p)[1]));
369         }
370
371         void ResultReady(SQLite3Result *res, int cols, char **data, char **colnames)
372         {
373                 res->AddRow(cols, data, colnames);
374         }
375
376         void AffectedReady(SQLite3Result *res)
377         {
378                 res->UpdateAffectedCount();
379         }
380
381         int OpenDB()
382         {
383                 return sqlite3_open(host.host.c_str(), &conn);
384         }
385
386         void CloseDB()
387         {
388                 sqlite3_interrupt(conn);
389                 sqlite3_close(conn);
390         }
391
392         SQLhost GetConfHost()
393         {
394                 return host;
395         }
396
397         void SendResults()
398         {
399                 while (results.size())
400                 {
401                         SQLite3Result* res = results[0];
402                         if (res->GetDest())
403                         {
404                                 res->Send();
405                         }
406                         else
407                         {
408                                 /* If the client module is unloaded partway through a query then the provider will set
409                                  * the pointer to NULL. We cannot just cancel the query as the result will still come
410                                  * through at some point...and it could get messy if we play with invalid pointers...
411                                  */
412                                 delete res;
413                         }
414                         results.pop_front();
415                 }
416         }
417
418         void ClearResults()
419         {
420                 while (results.size())
421                 {
422                         SQLite3Result* res = results[0];
423                         delete res;
424                         results.pop_front();
425                 }
426         }
427
428         void SendNotify()
429         {
430                 int QueueFD;
431                 if ((QueueFD = socket(AF_FAMILY, SOCK_STREAM, 0)) == -1)
432                 {
433                         /* crap, we're out of sockets... */
434                         return;
435                 }
436
437                 insp_sockaddr addr;
438
439 #ifdef IPV6
440                 insp_aton("::1", &addr.sin6_addr);
441                 addr.sin6_family = AF_FAMILY;
442                 addr.sin6_port = htons(resultnotify->GetPort());
443 #else
444                 insp_inaddr ia;
445                 insp_aton("127.0.0.1", &ia);
446                 addr.sin_family = AF_FAMILY;
447                 addr.sin_addr = ia;
448                 addr.sin_port = htons(resultnotify->GetPort());
449 #endif
450
451                 if (connect(QueueFD, (sockaddr*)&addr,sizeof(addr)) == -1)
452                 {
453                         /* wtf, we cant connect to it, but we just created it! */
454                         return;
455                 }
456         }
457
458 };
459
460
461 class ModuleSQLite3 : public Module
462 {
463   private:
464         ConnMap connections;
465         unsigned long currid;
466
467   public:
468         ModuleSQLite3(InspIRCd* Me)
469         : Module::Module(Me), currid(0)
470         {
471                 ServerInstance->Modules->UseInterface("SQLutils");
472
473                 if (!ServerInstance->Modules->PublishFeature("SQL", this))
474                 {
475                         throw ModuleException("m_sqlite3: Unable to publish feature 'SQL'");
476                 }
477
478                 resultnotify = new ResultNotifier(ServerInstance, this);
479
480                 ReadConf();
481
482                 ServerInstance->Modules->PublishInterface("SQL", this);
483                 Implementation eventlist[] = { I_OnRequest, I_OnRehash };
484                 ServerInstance->Modules->Attach(eventlist, this, 2);
485         }
486
487         virtual ~ModuleSQLite3()
488         {
489                 ClearQueue();
490                 ClearAllConnections();
491                 resultnotify->SetFd(-1);
492                 resultnotify->state = I_ERROR;
493                 resultnotify->OnError(I_ERR_SOCKET);
494                 resultnotify->ClosePending = true;
495                 delete resultnotify;
496                 ServerInstance->Modules->UnpublishInterface("SQL", this);
497                 ServerInstance->Modules->UnpublishFeature("SQL");
498                 ServerInstance->Modules->DoneWithInterface("SQLutils");
499         }
500
501
502         void SendQueue()
503         {
504                 for (ConnMap::iterator iter = connections.begin(); iter != connections.end(); iter++)
505                 {
506                         iter->second->SendResults();
507                 }
508         }
509
510         void ClearQueue()
511         {
512                 for (ConnMap::iterator iter = connections.begin(); iter != connections.end(); iter++)
513                 {
514                         iter->second->ClearResults();
515                 }
516         }
517
518         bool HasHost(const SQLhost &host)
519         {
520                 for (ConnMap::iterator iter = connections.begin(); iter != connections.end(); iter++)
521                 {
522                         if (host == iter->second->GetConfHost())
523                                 return true;
524                 }
525                 return false;
526         }
527
528         bool HostInConf(const SQLhost &h)
529         {
530                 ConfigReader conf(ServerInstance);
531                 for(int i = 0; i < conf.Enumerate("database"); i++)
532                 {
533                         SQLhost host;
534                         host.id         = conf.ReadValue("database", "id", i);
535                         host.host       = conf.ReadValue("database", "hostname", i);
536                         host.port       = conf.ReadInteger("database", "port", i, true);
537                         host.name       = conf.ReadValue("database", "name", i);
538                         host.user       = conf.ReadValue("database", "username", i);
539                         host.pass       = conf.ReadValue("database", "password", i);
540                         host.ssl        = conf.ReadFlag("database", "ssl", "0", i);
541                         if (h == host)
542                                 return true;
543                 }
544                 return false;
545         }
546
547         void ReadConf()
548         {
549                 ClearOldConnections();
550
551                 ConfigReader conf(ServerInstance);
552                 for(int i = 0; i < conf.Enumerate("database"); i++)
553                 {
554                         SQLhost host;
555
556                         host.id         = conf.ReadValue("database", "id", i);
557                         host.host       = conf.ReadValue("database", "hostname", i);
558                         host.port       = conf.ReadInteger("database", "port", i, true);
559                         host.name       = conf.ReadValue("database", "name", i);
560                         host.user       = conf.ReadValue("database", "username", i);
561                         host.pass       = conf.ReadValue("database", "password", i);
562                         host.ssl        = conf.ReadFlag("database", "ssl", "0", i);
563
564                         if (HasHost(host))
565                                 continue;
566
567                         this->AddConn(host);
568                 }
569         }
570
571         void AddConn(const SQLhost& hi)
572         {
573                 if (HasHost(hi))
574                 {
575                         ServerInstance->Logs->Log("m_sqlite3",DEFAULT, "WARNING: A sqlite connection with id: %s already exists. Aborting database open attempt.", hi.id.c_str());
576                         return;
577                 }
578
579                 SQLConn* newconn;
580
581                 newconn = new SQLConn(ServerInstance, this, hi);
582
583                 connections.insert(std::make_pair(hi.id, newconn));
584         }
585
586         void ClearOldConnections()
587         {
588                 ConnMap::iterator iter,safei;
589                 for (iter = connections.begin(); iter != connections.end(); iter++)
590                 {
591                         if (!HostInConf(iter->second->GetConfHost()))
592                         {
593                                 delete iter->second;
594                                 safei = iter;
595                                 --iter;
596                                 connections.erase(safei);
597                         }
598                 }
599         }
600
601         void ClearAllConnections()
602         {
603                 ConnMap::iterator i;
604                 while ((i = connections.begin()) != connections.end())
605                 {
606                         connections.erase(i);
607                         delete i->second;
608                 }
609         }
610
611         virtual void OnRehash(User* user, const std::string &parameter)
612         {
613                 ReadConf();
614         }
615
616         virtual const char* OnRequest(Request* request)
617         {
618                 if(strcmp(SQLREQID, request->GetId()) == 0)
619                 {
620                         SQLrequest* req = (SQLrequest*)request;
621                         ConnMap::iterator iter;
622                         if((iter = connections.find(req->dbid)) != connections.end())
623                         {
624                                 req->id = NewID();
625                                 req->error = iter->second->Query(*req);
626                                 return SQLSUCCESS;
627                         }
628                         else
629                         {
630                                 req->error.Id(BAD_DBID);
631                                 return NULL;
632                         }
633                 }
634                 return NULL;
635         }
636
637         unsigned long NewID()
638         {
639                 if (currid+1 == 0)
640                         currid++;
641
642                 return ++currid;
643         }
644
645         virtual Version GetVersion()
646         {
647                 return Version(1,1,0,0,VF_VENDOR|VF_SERVICEPROVIDER,API_VERSION);
648         }
649
650 };
651
652 void ResultNotifier::Dispatch()
653 {
654         ((ModuleSQLite3*)mod)->SendQueue();
655 }
656
657 MODULE_INIT(ModuleSQLite3)
658