]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_mysql.cpp
Fix initialization of SSL certificate field on connect
[user/henk/code/inspircd.git] / src / modules / extra / m_mysql.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* Stop mysql wanting to use long long */
15 #define NO_CLIENT_LONG_LONG
16
17 #include "inspircd.h"
18 #include <mysql.h>
19 #include "sql.h"
20
21 #ifdef WINDOWS
22 # pragma comment(lib, "mysqlclient.lib")
23 # pragma comment(lib, "advapi32.lib")
24 # pragma comment(linker, "/NODEFAULTLIB:LIBCMT")
25 #endif
26
27 /* VERSION 3 API: With nonblocking (threaded) requests */
28
29 /* $ModDesc: SQL Service Provider module for all other m_sql* modules */
30 /* $CompileFlags: exec("mysql_config --include") */
31 /* $LinkerFlags: exec("mysql_config --libs_r") rpath("mysql_config --libs_r") */
32
33 /* THE NONBLOCKING MYSQL API!
34  *
35  * MySQL provides no nonblocking (asyncronous) API of its own, and its developers recommend
36  * that instead, you should thread your program. This is what i've done here to allow for
37  * asyncronous SQL requests via mysql. The way this works is as follows:
38  *
39  * The module spawns a thread via class Thread, and performs its mysql queries in this thread,
40  * using a queue with priorities. There is a mutex on either end which prevents two threads
41  * adjusting the queue at the same time, and crashing the ircd. Every 50 milliseconds, the
42  * worker thread wakes up, and checks if there is a request at the head of its queue.
43  * If there is, it processes this request, blocking the worker thread but leaving the ircd
44  * thread to go about its business as usual. During this period, the ircd thread is able
45  * to insert futher pending requests into the queue.
46  *
47  * Once the processing of a request is complete, it is removed from the incoming queue to
48  * an outgoing queue, and initialized as a 'response'. The worker thread then signals the
49  * ircd thread (via a loopback socket) of the fact a result is available, by sending the
50  * connection ID through the connection.
51  *
52  * The ircd thread then mutexes the queue once more, reads the outbound response off the head
53  * of the queue, and sends it on its way to the original calling module.
54  *
55  * XXX: You might be asking "why doesnt he just send the response from within the worker thread?"
56  * The answer to this is simple. The majority of InspIRCd, and in fact most ircd's are not
57  * threadsafe. This module is designed to be threadsafe and is careful with its use of threads,
58  * however, if we were to call a module's OnRequest even from within a thread which was not the
59  * one the module was originally instantiated upon, there is a chance of all hell breaking loose
60  * if a module is ever put in a re-enterant state (stack corruption could occur, crashes, data
61  * corruption, and worse, so DONT think about it until the day comes when InspIRCd is 100%
62  * gauranteed threadsafe!)
63  *
64  * For a diagram of this system please see http://wiki.inspircd.org/Mysql2
65  */
66
67 class SQLConnection;
68 class MySQLresult;
69 class DispatcherThread;
70
71 struct QQueueItem
72 {
73         SQLQuery* q;
74         std::string query;
75         SQLConnection* c;
76         QQueueItem(SQLQuery* Q, const std::string& S, SQLConnection* C) : q(Q), query(S), c(C) {}
77 };
78
79 struct RQueueItem
80 {
81         SQLQuery* q;
82         MySQLresult* r;
83         RQueueItem(SQLQuery* Q, MySQLresult* R) : q(Q), r(R) {}
84 };
85
86 typedef std::map<std::string, SQLConnection*> ConnMap;
87 typedef std::deque<QQueueItem> QueryQueue;
88 typedef std::deque<RQueueItem> ResultQueue;
89
90 /** MySQL module
91  *  */
92 class ModuleSQL : public Module
93 {
94  public:
95         DispatcherThread* Dispatcher;
96         QueryQueue qq;       // MUST HOLD MUTEX
97         ResultQueue rq;      // MUST HOLD MUTEX
98         ConnMap connections; // main thread only
99
100         ModuleSQL();
101         void init();
102         ~ModuleSQL();
103         void OnRehash(User* user);
104         void OnUnloadModule(Module* mod);
105         Version GetVersion();
106 };
107
108 class DispatcherThread : public SocketThread
109 {
110  private:
111         ModuleSQL* const Parent;
112  public:
113         DispatcherThread(ModuleSQL* CreatorModule) : Parent(CreatorModule) { }
114         ~DispatcherThread() { }
115         virtual void Run();
116         virtual void OnNotify();
117 };
118
119 #if !defined(MYSQL_VERSION_ID) || MYSQL_VERSION_ID<32224
120 #define mysql_field_count mysql_num_fields
121 #endif
122
123 /** Represents a mysql result set
124  */
125 class MySQLresult : public SQLResult
126 {
127  public:
128         SQLerror err;
129         int currentrow;
130         int rows;
131         std::vector<std::string> colnames;
132         std::vector<SQLEntries> fieldlists;
133
134         MySQLresult(MYSQL_RES* res, int affected_rows) : err(SQL_NO_ERROR), currentrow(0), rows(0)
135         {
136                 if (affected_rows >= 1)
137                 {
138                         rows = affected_rows;
139                         fieldlists.resize(rows);
140                 }
141                 unsigned int field_count = 0;
142                 if (res)
143                 {
144                         MYSQL_ROW row;
145                         int n = 0;
146                         while ((row = mysql_fetch_row(res)))
147                         {
148                                 if (fieldlists.size() < (unsigned int)rows+1)
149                                 {
150                                         fieldlists.resize(fieldlists.size()+1);
151                                 }
152                                 field_count = 0;
153                                 MYSQL_FIELD *fields = mysql_fetch_fields(res);
154                                 if(mysql_num_fields(res) == 0)
155                                         break;
156                                 if (fields && mysql_num_fields(res))
157                                 {
158                                         colnames.clear();
159                                         while (field_count < mysql_num_fields(res))
160                                         {
161                                                 std::string a = (fields[field_count].name ? fields[field_count].name : "");
162                                                 if (row[field_count])
163                                                         fieldlists[n].push_back(SQLEntry(row[field_count]));
164                                                 else
165                                                         fieldlists[n].push_back(SQLEntry());
166                                                 colnames.push_back(a);
167                                                 field_count++;
168                                         }
169                                         n++;
170                                 }
171                                 rows++;
172                         }
173                         mysql_free_result(res);
174                         res = NULL;
175                 }
176         }
177
178         MySQLresult(SQLerror& e) : err(e)
179         {
180
181         }
182
183         ~MySQLresult()
184         {
185         }
186
187         virtual int Rows()
188         {
189                 return rows;
190         }
191
192         virtual void GetCols(std::vector<std::string>& result)
193         {
194                 result.assign(colnames.begin(), colnames.end());
195         }
196
197         virtual SQLEntry GetValue(int row, int column)
198         {
199                 if ((row >= 0) && (row < rows) && (column >= 0) && (column < (int)fieldlists[row].size()))
200                 {
201                         return fieldlists[row][column];
202                 }
203                 return SQLEntry();
204         }
205
206         virtual bool GetRow(SQLEntries& result)
207         {
208                 if (currentrow < rows)
209                 {
210                         result.assign(fieldlists[currentrow].begin(), fieldlists[currentrow].end());
211                         currentrow++;
212                         return true;
213                 }
214                 else
215                 {
216                         result.clear();
217                         return false;
218                 }
219         }
220 };
221
222 /** Represents a connection to a mysql database
223  */
224 class SQLConnection : public SQLProvider
225 {
226  public:
227         reference<ConfigTag> config;
228         MYSQL *connection;
229         Mutex lock;
230
231         // This constructor creates an SQLConnection object with the given credentials, but does not connect yet.
232         SQLConnection(Module* p, ConfigTag* tag) : SQLProvider(p, "SQL/" + tag->getString("id")),
233                 config(tag), connection(NULL)
234         {
235         }
236
237         ~SQLConnection()
238         {
239                 Close();
240         }
241
242         // This method connects to the database using the credentials supplied to the constructor, and returns
243         // true upon success.
244         bool Connect()
245         {
246                 unsigned int timeout = 1;
247                 connection = mysql_init(connection);
248                 mysql_options(connection,MYSQL_OPT_CONNECT_TIMEOUT,(char*)&timeout);
249                 std::string host = config->getString("host");
250                 std::string user = config->getString("user");
251                 std::string pass = config->getString("pass");
252                 std::string dbname = config->getString("name");
253                 int port = config->getInt("port");
254                 bool rv = mysql_real_connect(connection, host.c_str(), user.c_str(), pass.c_str(), dbname.c_str(), port, NULL, 0);
255                 if (!rv)
256                         return rv;
257                 std::string initquery;
258                 if (config->readString("initialquery", initquery))
259                 {
260                         mysql_query(connection,initquery.c_str());
261                 }
262                 return true;
263         }
264
265         ModuleSQL* Parent()
266         {
267                 return (ModuleSQL*)(Module*)creator;
268         }
269
270         MySQLresult* DoBlockingQuery(const std::string& query)
271         {
272
273                 /* Parse the command string and dispatch it to mysql */
274                 if (CheckConnection() && !mysql_real_query(connection, query.data(), query.length()))
275                 {
276                         /* Successfull query */
277                         MYSQL_RES* res = mysql_use_result(connection);
278                         unsigned long rows = mysql_affected_rows(connection);
279                         return new MySQLresult(res, rows);
280                 }
281                 else
282                 {
283                         /* XXX: See /usr/include/mysql/mysqld_error.h for a list of
284                          * possible error numbers and error messages */
285                         SQLerror e(SQL_QREPLY_FAIL, ConvToStr(mysql_errno(connection)) + std::string(": ") + mysql_error(connection));
286                         return new MySQLresult(e);
287                 }
288         }
289
290         bool CheckConnection()
291         {
292                 if (!connection || mysql_ping(connection) != 0)
293                         return Connect();
294                 return true;
295         }
296
297         std::string GetError()
298         {
299                 return mysql_error(connection);
300         }
301
302         void Close()
303         {
304                 mysql_close(connection);
305         }
306
307         void submit(SQLQuery* q, const std::string& qs)
308         {
309                 Parent()->Dispatcher->LockQueue();
310                 Parent()->qq.push_back(QQueueItem(q, qs, this));
311                 Parent()->Dispatcher->UnlockQueueWakeup();
312         }
313
314         void submit(SQLQuery* call, const std::string& q, const ParamL& p)
315         {
316                 std::string res;
317                 unsigned int param = 0;
318                 for(std::string::size_type i = 0; i < q.length(); i++)
319                 {
320                         if (q[i] != '?')
321                                 res.push_back(q[i]);
322                         else
323                         {
324                                 if (param < p.size())
325                                 {
326                                         std::string parm = p[param++];
327                                         char buffer[MAXBUF];
328                                         mysql_escape_string(buffer, parm.c_str(), parm.length());
329 //                                      mysql_real_escape_string(connection, queryend, paramscopy[paramnum].c_str(), paramscopy[paramnum].length());
330                                         res.append(buffer);
331                                 }
332                         }
333                 }
334                 submit(call, res);
335         }
336
337         void submit(SQLQuery* call, const std::string& q, const ParamM& p)
338         {
339                 std::string res;
340                 for(std::string::size_type i = 0; i < q.length(); i++)
341                 {
342                         if (q[i] != '$')
343                                 res.push_back(q[i]);
344                         else
345                         {
346                                 std::string field;
347                                 i++;
348                                 while (i < q.length() && isalnum(q[i]))
349                                         field.push_back(q[i++]);
350                                 i--;
351
352                                 ParamM::const_iterator it = p.find(field);
353                                 if (it != p.end())
354                                 {
355                                         std::string parm = it->second;
356                                         char buffer[MAXBUF];
357                                         mysql_escape_string(buffer, parm.c_str(), parm.length());
358                                         res.append(buffer);
359                                 }
360                         }
361                 }
362                 submit(call, res);
363         }
364 };
365
366 ModuleSQL::ModuleSQL()
367 {
368         Dispatcher = NULL;
369 }
370
371 void ModuleSQL::init()
372 {
373         Dispatcher = new DispatcherThread(this);
374         ServerInstance->Threads->Start(Dispatcher);
375
376         Implementation eventlist[] = { I_OnRehash, I_OnUnloadModule };
377         ServerInstance->Modules->Attach(eventlist, this, 2);
378
379         OnRehash(NULL);
380 }
381
382 ModuleSQL::~ModuleSQL()
383 {
384         if (Dispatcher)
385         {
386                 Dispatcher->join();
387                 Dispatcher->OnNotify();
388                 delete Dispatcher;
389         }
390         for(ConnMap::iterator i = connections.begin(); i != connections.end(); i++)
391         {
392                 delete i->second;
393         }
394 }
395
396 void ModuleSQL::OnRehash(User* user)
397 {
398         ConnMap conns;
399         ConfigTagList tags = ServerInstance->Config->ConfTags("database");
400         for(ConfigIter i = tags.first; i != tags.second; i++)
401         {
402                 if (i->second->getString("module", "mysql") != "mysql")
403                         continue;
404                 std::string id = i->second->getString("id");
405                 ConnMap::iterator curr = connections.find(id);
406                 if (curr == connections.end())
407                 {
408                         SQLConnection* conn = new SQLConnection(this, i->second);
409                         conns.insert(std::make_pair(id, conn));
410                         ServerInstance->Modules->AddService(*conn);
411                 }
412                 else
413                 {
414                         conns.insert(*curr);
415                         connections.erase(curr);
416                 }
417         }
418
419         // now clean up the deleted databases
420         Dispatcher->LockQueue();
421         SQLerror err(SQL_BAD_DBID);
422         for(ConnMap::iterator i = connections.begin(); i != connections.end(); i++)
423         {
424                 ServerInstance->Modules->DelService(*i->second);
425                 // it might be running a query on this database. Wait for that to complete
426                 i->second->lock.Lock();
427                 i->second->lock.Unlock();
428                 // now remove all active queries to this DB
429                 for(unsigned int j = qq.size() - 1; j >= 0; j--)
430                 {
431                         if (qq[j].c == i->second)
432                         {
433                                 qq[j].q->OnError(err);
434                                 delete qq[j].q;
435                                 qq.erase(qq.begin() + j);
436                         }
437                 }
438                 // finally, nuke the connection
439                 delete i->second;
440         }
441         Dispatcher->UnlockQueue();
442         connections.swap(conns);
443 }
444
445 void ModuleSQL::OnUnloadModule(Module* mod)
446 {
447         SQLerror err(SQL_BAD_DBID);
448         Dispatcher->LockQueue();
449         unsigned int i = qq.size();
450         while (i > 0)
451         {
452                 i--;
453                 if (qq[i].q->creator == mod)
454                 {
455                         if (i == 0)
456                         {
457                                 // need to wait until the query is done
458                                 // (the result will be discarded)
459                                 qq[i].c->lock.Lock();
460                                 qq[i].c->lock.Unlock();
461                         }
462                         qq[i].q->OnError(err);
463                         delete qq[i].q;
464                         qq.erase(qq.begin() + i);
465                 }
466         }
467         Dispatcher->UnlockQueue();
468         // clean up any result queue entries
469         Dispatcher->OnNotify();
470 }
471
472 Version ModuleSQL::GetVersion()
473 {
474         return Version("MySQL support", VF_VENDOR);
475 }
476
477 void DispatcherThread::Run()
478 {
479         this->LockQueue();
480         while (!this->GetExitFlag())
481         {
482                 if (!Parent->qq.empty())
483                 {
484                         QQueueItem i = Parent->qq.front();
485                         i.c->lock.Lock();
486                         this->UnlockQueue();
487                         MySQLresult* res = i.c->DoBlockingQuery(i.query);
488                         i.c->lock.Unlock();
489
490                         /*
491                          * At this point, the main thread could be working on:
492                          *  Rehash - delete i.c out from under us. We don't care about that.
493                          *  UnloadModule - delete i.q and the qq item. Need to avoid reporting results.
494                          */
495
496                         this->LockQueue();
497                         if (Parent->qq.front().q == i.q)
498                         {
499                                 Parent->qq.pop_front();
500                                 Parent->rq.push_back(RQueueItem(i.q, res));
501                                 NotifyParent();
502                         }
503                         else
504                         {
505                                 // UnloadModule ate the query
506                                 delete res;
507                         }
508                 }
509                 else
510                 {
511                         /* We know the queue is empty, we can safely hang this thread until
512                          * something happens
513                          */
514                         this->WaitForQueue();
515                 }
516         }
517         this->UnlockQueue();
518 }
519
520 void DispatcherThread::OnNotify()
521 {
522         // this could unlock during the dispatch, but OnResult isn't expected to take that long
523         this->LockQueue();
524         for(ResultQueue::iterator i = Parent->rq.begin(); i != Parent->rq.end(); i++)
525         {
526                 MySQLresult* res = i->r;
527                 if (res->err.id == SQL_NO_ERROR)
528                         i->q->OnResult(*res);
529                 else
530                         i->q->OnError(res->err);
531                 delete i->q;
532                 delete i->r;
533         }
534         Parent->rq.clear();
535         this->UnlockQueue();
536 }
537
538 MODULE_INIT(ModuleSQL)