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