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