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