1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
26 #include "configreader.h"
30 /* $ModDesc: PostgreSQL Service Provider module for all other m_sql* modules, uses v2 of the SQL API */
31 /* $CompileFlags: -Iexec("pg_config --includedir") eval("my $s = `pg_config --version`;$s =~ /^.*?(\d+)\.(\d+)\.(\d+).*?$/;my $v = hex(sprintf("0x%02x%02x%02x", $1, $2, $3));print "-DPGSQL_HAS_ESCAPECONN" if(($v >= 0x080104) or (($v >= 0x07030F) and ($v < 0x080000)));") */
32 /* $LinkerFlags: -Lexec("pg_config --libdir") -lpq */
33 /* $ModDep: m_sqlv2.h */
36 /* SQLConn rewritten by peavey to
37 * use EventHandler instead of
38 * InspSocket. This is much neater
39 * and gives total control of destroy
40 * and delete of resources.
43 /* Forward declare, so we can have the typedef neatly at the top */
46 typedef std::map<std::string, SQLConn*> ConnMap;
48 /* CREAD, Connecting and wants read event
49 * CWRITE, Connecting and wants write event
50 * WREAD, Connected/Working and wants read event
51 * WWRITE, Connected/Working and wants write event
52 * RREAD, Resetting and wants read event
53 * RWRITE, Resetting and wants write event
55 enum SQLstatus { CREAD, CWRITE, WREAD, WWRITE, RREAD, RWRITE };
57 /** SQLhost::GetDSN() - Overload to return correct DSN for PostgreSQL
59 std::string SQLhost::GetDSN()
61 std::ostringstream conninfo("connect_timeout = '2'");
64 conninfo << " hostaddr = '" << ip << "'";
67 conninfo << " port = '" << port << "'";
70 conninfo << " dbname = '" << name << "'";
73 conninfo << " user = '" << user << "'";
76 conninfo << " password = '" << pass << "'";
80 conninfo << " sslmode = 'require'";
84 conninfo << " sslmode = 'disable'";
87 return conninfo.str();
90 class ReconnectTimer : public InspTimer
95 ReconnectTimer(InspIRCd* SI, Module* m)
96 : InspTimer(5, SI->Time(), false), mod(m)
99 virtual void Tick(time_t TIME);
103 /** Used to resolve sql server hostnames
105 class SQLresolver : public Resolver
111 SQLresolver(Module* m, InspIRCd* Instance, const SQLhost& hi, bool &cached)
112 : Resolver(Instance, hi.host, DNS_QUERY_FORWARD, cached, (Module*)m), host(hi), mod(m)
116 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached);
118 virtual void OnError(ResolverError e, const std::string &errormessage)
120 ServerInstance->Log(DEBUG, "PgSQL: DNS lookup failed (%s), dying horribly", errormessage.c_str());
124 /** QueryQueue, a queue of queries waiting to be executed.
125 * This maintains two queues internally, one for 'priority'
126 * queries and one for less important ones. Each queue has
127 * new queries appended to it and ones to execute are popped
128 * off the front. This keeps them flowing round nicely and no
129 * query should ever get 'stuck' for too long. If there are
130 * queries in the priority queue they will be executed first,
131 * 'unimportant' queries will only be executed when the
132 * priority queue is empty.
134 * We store lists of SQLrequest's here, by value as we want to avoid storing
135 * any data allocated inside the client module (in case that module is unloaded
136 * while the query is in progress).
138 * Because we want to work on the current SQLrequest in-situ, we need a way
139 * of accessing the request we are currently processing, QueryQueue::front(),
140 * but that call needs to always return the same request until that request
141 * is removed from the queue, this is what the 'which' variable is. New queries are
142 * always added to the back of one of the two queues, but if when front()
143 * is first called then the priority queue is empty then front() will return
144 * a query from the normal queue, but if a query is then added to the priority
145 * queue then front() must continue to return the front of the *normal* queue
146 * until pop() is called.
149 class QueryQueue : public classbase
152 typedef std::deque<SQLrequest> ReqDeque;
154 ReqDeque priority; /* The priority queue */
155 ReqDeque normal; /* The 'normal' queue */
156 enum { PRI, NOR, NON } which; /* Which queue the currently active element is at the front of */
164 void push(const SQLrequest &q)
167 priority.push_back(q);
174 if((which == PRI) && priority.size())
176 priority.pop_front();
178 else if((which == NOR) && normal.size())
186 /* Silently do nothing if there was no element to pop() */
194 return priority.front();
196 return normal.front();
201 return priority.front();
207 return normal.front();
210 /* This will probably result in a segfault,
211 * but the caller should have checked totalsize()
212 * first so..meh - moron :p
215 return priority.front();
219 std::pair<int, int> size()
221 return std::make_pair(priority.size(), normal.size());
226 return priority.size() + normal.size();
229 void PurgeModule(Module* mod)
231 DoPurgeModule(mod, priority);
232 DoPurgeModule(mod, normal);
236 void DoPurgeModule(Module* mod, ReqDeque& q)
238 for(ReqDeque::iterator iter = q.begin(); iter != q.end(); iter++)
240 if(iter->GetSource() == mod)
242 if(iter->id == front().id)
244 /* It's the currently active query.. :x */
245 iter->SetSource(NULL);
249 /* It hasn't been executed yet..just remove it */
250 iter = q.erase(iter);
257 /** PgSQLresult is a subclass of the mostly-pure-virtual class SQLresult.
258 * All SQL providers must create their own subclass and define it's methods using that
259 * database library's data retriveal functions. The aim is to avoid a slow and inefficient process
260 * of converting all data to a common format before it reaches the result structure. This way
261 * data is passes to the module nearly as directly as if it was using the API directly itself.
264 class PgSQLresult : public SQLresult
271 SQLfieldList* fieldlist;
272 SQLfieldMap* fieldmap;
274 PgSQLresult(Module* self, Module* to, unsigned long id, PGresult* result)
275 : SQLresult(self, to, id), res(result), currentrow(0), fieldlist(NULL), fieldmap(NULL)
277 rows = PQntuples(res);
278 cols = PQnfields(res);
283 /* If we allocated these, free them... */
297 return atoi(PQcmdTuples(res));
307 return PQnfields(res);
310 virtual std::string ColName(int column)
312 char* name = PQfname(res, column);
314 return (name) ? name : "";
317 virtual int ColNum(const std::string &column)
319 int n = PQfnumber(res, column.c_str());
323 throw SQLbadColName();
331 virtual SQLfield GetValue(int row, int column)
333 char* v = PQgetvalue(res, row, column);
337 return SQLfield(std::string(v, PQgetlength(res, row, column)), PQgetisnull(res, row, column));
341 throw SQLbadColName();
345 virtual SQLfieldList& GetRow()
347 /* In an effort to reduce overhead we don't actually allocate the list
348 * until the first time it's needed...so...
356 fieldlist = new SQLfieldList;
359 if(currentrow < PQntuples(res))
361 int cols = PQnfields(res);
363 for(int i = 0; i < cols; i++)
365 fieldlist->push_back(GetValue(currentrow, i));
374 virtual SQLfieldMap& GetRowMap()
376 /* In an effort to reduce overhead we don't actually allocate the map
377 * until the first time it's needed...so...
385 fieldmap = new SQLfieldMap;
388 if(currentrow < PQntuples(res))
390 int cols = PQnfields(res);
392 for(int i = 0; i < cols; i++)
394 fieldmap->insert(std::make_pair(ColName(i), GetValue(currentrow, i)));
403 virtual SQLfieldList* GetRowPtr()
405 SQLfieldList* fl = new SQLfieldList;
407 if(currentrow < PQntuples(res))
409 int cols = PQnfields(res);
411 for(int i = 0; i < cols; i++)
413 fl->push_back(GetValue(currentrow, i));
422 virtual SQLfieldMap* GetRowMapPtr()
424 SQLfieldMap* fm = new SQLfieldMap;
426 if(currentrow < PQntuples(res))
428 int cols = PQnfields(res);
430 for(int i = 0; i < cols; i++)
432 fm->insert(std::make_pair(ColName(i), GetValue(currentrow, i)));
441 virtual void Free(SQLfieldMap* fm)
446 virtual void Free(SQLfieldList* fl)
452 /** SQLConn represents one SQL session.
454 class SQLConn : public EventHandler
458 SQLhost confhost; /* The <database> entry */
459 Module* us; /* Pointer to the SQL provider itself */
460 PGconn* sql; /* PgSQL database connection handle */
461 SQLstatus status; /* PgSQL database connection status */
462 bool qinprog; /* If there is currently a query in progress */
463 QueryQueue queue; /* Queue of queries waiting to be executed on this connection */
464 time_t idle; /* Time we last heard from the database */
467 SQLConn(InspIRCd* SI, Module* self, const SQLhost& hi)
468 : EventHandler(), Instance(SI), confhost(hi), us(self), sql(NULL), status(CWRITE), qinprog(false)
470 idle = this->Instance->Time();
473 Instance->Log(DEFAULT, "WARNING: Could not connect to database with id: " + ConvToStr(hi.id));
483 virtual void HandleEvent(EventType et, int errornum)
506 if(!(sql = PQconnectStart(confhost.GetDSN().c_str())))
509 if(PQstatus(sql) == CONNECTION_BAD)
512 if(PQsetnonblocking(sql, 1) == -1)
515 /* OK, we've initalised the connection, now to get it hooked into the socket engine
516 * and then start polling it.
518 this->fd = PQsocket(sql);
523 if (!this->Instance->SE->AddFd(this))
525 Instance->Log(DEBUG, "BUG: Couldn't add pgsql socket to socket engine");
529 /* Socket all hooked into the engine, now to tell PgSQL to start connecting */
535 switch(PQconnectPoll(sql))
537 case PGRES_POLLING_WRITING:
538 Instance->SE->WantWrite(this);
541 case PGRES_POLLING_READING:
544 case PGRES_POLLING_FAILED:
546 case PGRES_POLLING_OK:
548 return DoConnectedPoll();
554 bool DoConnectedPoll()
556 if(!qinprog && queue.totalsize())
558 /* There's no query currently in progress, and there's queries in the queue. */
559 SQLrequest& query = queue.front();
563 if(PQconsumeInput(sql))
565 /* We just read stuff from the server, that counts as it being alive
566 * so update the idle-since time :p
568 idle = this->Instance->Time();
572 /* Nothing happens here */
576 /* Grab the request we're processing */
577 SQLrequest& query = queue.front();
579 /* Get a pointer to the module we're about to return the result to */
580 Module* to = query.GetSource();
582 /* Fetch the result.. */
583 PGresult* result = PQgetResult(sql);
585 /* PgSQL would allow a query string to be sent which has multiple
586 * queries in it, this isn't portable across database backends and
587 * we don't want modules doing it. But just in case we make sure we
588 * drain any results there are and just use the last one.
589 * If the module devs are behaving there will only be one result.
591 while (PGresult* temp = PQgetResult(sql))
599 /* ..and the result */
600 PgSQLresult reply(us, to, query.id, result);
602 /* Fix by brain, make sure the original query gets sent back in the reply */
603 reply.query = query.query.q;
605 switch(PQresultStatus(result))
607 case PGRES_EMPTY_QUERY:
608 case PGRES_BAD_RESPONSE:
609 case PGRES_FATAL_ERROR:
610 reply.error.Id(QREPLY_FAIL);
611 reply.error.Str(PQresultErrorMessage(result));
613 /* No action, other values are not errors */
618 /* PgSQLresult's destructor will free the PGresult */
622 /* If the client module is unloaded partway through a query then the provider will set
623 * the pointer to NULL. We cannot just cancel the query as the result will still come
624 * through at some point...and it could get messy if we play with invalid pointers...
636 /* I think we'll assume this means the server died...it might not,
637 * but I think that any error serious enough we actually get here
638 * deserves to reconnect [/excuse]
639 * Returning true so the core doesn't try and close the connection.
648 switch(PQresetPoll(sql))
650 case PGRES_POLLING_WRITING:
651 Instance->SE->WantWrite(this);
654 case PGRES_POLLING_READING:
657 case PGRES_POLLING_FAILED:
659 case PGRES_POLLING_OK:
661 return DoConnectedPoll();
669 /* Always return true here, false would close the socket - we need to do that ourselves with the pgsql API */
675 /* Always return true here, false would close the socket - we need to do that ourselves with the pgsql API */
684 void DelayReconnect();
690 if((status == CREAD) || (status == CWRITE))
694 else if((status == RREAD) || (status == RWRITE))
700 ret = DoConnectedPoll();
705 SQLerror DoQuery(SQLrequest &req)
707 if((status == WREAD) || (status == WWRITE))
711 /* Parse the command string and dispatch it */
713 /* Pointer to the buffer we screw around with substitution in */
715 /* Pointer to the current end of query, where we append new stuff */
717 /* Total length of the unescaped parameters */
718 unsigned int paramlen;
722 for(ParamL::iterator i = req.query.p.begin(); i != req.query.p.end(); i++)
724 paramlen += i->size();
727 /* To avoid a lot of allocations, allocate enough memory for the biggest the escaped query could possibly be.
728 * sizeofquery + (totalparamlength*2) + 1
730 * The +1 is for null-terminating the string for PQsendQuery()
733 query = new char[req.query.q.length() + (paramlen*2) + 1];
736 /* Okay, now we have a buffer large enough we need to start copying the query into it and escaping and substituting
737 * the parameters into it...
740 for(unsigned int i = 0; i < req.query.q.length(); i++)
742 if(req.query.q[i] == '?')
744 /* We found a place to substitute..what fun.
745 * Use the PgSQL calls to escape and write the
746 * escaped string onto the end of our query buffer,
747 * then we "just" need to make sure queryend is
748 * pointing at the right place.
751 if(req.query.p.size())
756 #ifdef PGSQL_HAS_ESCAPECONN
757 len = PQescapeStringConn(sql, queryend, req.query.p.front().c_str(), req.query.p.front().length(), &error);
759 len = PQescapeString (queryend, req.query.p.front().c_str(), req.query.p.front().length());
763 Instance->Log(DEBUG, "BUG: Apparently PQescapeStringConn() failed somehow...don't know how or what to do...");
766 /* Incremenet queryend to the end of the newly escaped parameter */
769 /* Remove the parameter we just substituted in */
770 req.query.p.pop_front();
774 Instance->Log(DEBUG, "BUG: Found a substitution location but no parameter to substitute :|");
780 *queryend = req.query.q[i];
785 /* Null-terminate the query */
789 if(PQsendQuery(sql, query))
798 return SQLerror(QSEND_FAIL, PQerrorMessage(sql));
802 return SQLerror(BAD_CONN, "Can't query until connection is complete");
805 SQLerror Query(const SQLrequest &req)
809 if(!qinprog && queue.totalsize())
811 /* There's no query currently in progress, and there's queries in the queue. */
812 SQLrequest& query = queue.front();
813 return DoQuery(query);
821 void OnUnloadModule(Module* mod)
823 queue.PurgeModule(mod);
826 const SQLhost GetConfHost()
832 if (!this->Instance->SE->DelFd(this))
834 if (sql && PQstatus(sql) == CONNECTION_BAD)
836 this->Instance->SE->DelFd(this, true);
840 Instance->Log(DEBUG, "BUG: PQsocket cant be removed from socket engine!");
853 class ModulePgSQL : public Module
857 unsigned long currid;
859 ReconnectTimer* retimer;
862 ModulePgSQL(InspIRCd* Me)
863 : Module::Module(Me), currid(0)
865 ServerInstance->UseInterface("SQLutils");
867 sqlsuccess = new char[strlen(SQLSUCCESS)+1];
869 strlcpy(sqlsuccess, SQLSUCCESS, strlen(SQLSUCCESS));
871 if (!ServerInstance->PublishFeature("SQL", this))
873 throw ModuleException("BUG: PgSQL Unable to publish feature 'SQL'");
878 ServerInstance->PublishInterface("SQL", this);
881 virtual ~ModulePgSQL()
884 ServerInstance->Timers->DelTimer(retimer);
885 ClearAllConnections();
887 ServerInstance->UnpublishInterface("SQL", this);
888 ServerInstance->UnpublishFeature("SQL");
889 ServerInstance->DoneWithInterface("SQLutils");
892 void Implements(char* List)
894 List[I_OnUnloadModule] = List[I_OnRequest] = List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
897 virtual void OnRehash(userrec* user, const std::string ¶meter)
902 bool HasHost(const SQLhost &host)
904 for (ConnMap::iterator iter = connections.begin(); iter != connections.end(); iter++)
906 if (host == iter->second->GetConfHost())
912 bool HostInConf(const SQLhost &h)
914 ConfigReader conf(ServerInstance);
915 for(int i = 0; i < conf.Enumerate("database"); i++)
918 host.id = conf.ReadValue("database", "id", i);
919 host.host = conf.ReadValue("database", "hostname", i);
920 host.port = conf.ReadInteger("database", "port", i, true);
921 host.name = conf.ReadValue("database", "name", i);
922 host.user = conf.ReadValue("database", "username", i);
923 host.pass = conf.ReadValue("database", "password", i);
924 host.ssl = conf.ReadFlag("database", "ssl", "0", i);
933 ClearOldConnections();
935 ConfigReader conf(ServerInstance);
936 for(int i = 0; i < conf.Enumerate("database"); i++)
942 host.id = conf.ReadValue("database", "id", i);
943 host.host = conf.ReadValue("database", "hostname", i);
944 host.port = conf.ReadInteger("database", "port", i, true);
945 host.name = conf.ReadValue("database", "name", i);
946 host.user = conf.ReadValue("database", "username", i);
947 host.pass = conf.ReadValue("database", "password", i);
948 host.ssl = conf.ReadFlag("database", "ssl", "0", i);
953 ipvalid = insp_aton(host.host.c_str(), &blargle);
957 /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */
961 else if(ipvalid == 0)
963 /* Conversion failed, assume it's a host */
964 SQLresolver* resolver;
969 resolver = new SQLresolver(this, ServerInstance, host, cached);
970 ServerInstance->AddResolver(resolver, cached);
974 /* THE WORLD IS COMING TO AN END! */
979 /* Invalid address family, die horribly. */
980 ServerInstance->Log(DEBUG, "BUG: insp_aton failed returning -1, oh noes.");
985 void ClearOldConnections()
987 ConnMap::iterator iter,safei;
988 for (iter = connections.begin(); iter != connections.end(); iter++)
990 if (!HostInConf(iter->second->GetConfHost()))
992 DELETE(iter->second);
995 connections.erase(safei);
1000 void ClearAllConnections()
1002 ConnMap::iterator i;
1003 while ((i = connections.begin()) != connections.end())
1005 connections.erase(i);
1010 void AddConn(const SQLhost& hi)
1014 ServerInstance->Log(DEFAULT, "WARNING: A pgsql connection with id: %s already exists, possibly due to DNS delay. Aborting connection attempt.", hi.id.c_str());
1020 /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */
1021 newconn = new SQLConn(ServerInstance, this, hi);
1023 connections.insert(std::make_pair(hi.id, newconn));
1026 void ReconnectConn(SQLConn* conn)
1028 for (ConnMap::iterator iter = connections.begin(); iter != connections.end(); iter++)
1030 if (conn == iter->second)
1032 DELETE(iter->second);
1033 connections.erase(iter);
1037 retimer = new ReconnectTimer(ServerInstance, this);
1038 ServerInstance->Timers->AddTimer(retimer);
1041 virtual char* OnRequest(Request* request)
1043 if(strcmp(SQLREQID, request->GetId()) == 0)
1045 SQLrequest* req = (SQLrequest*)request;
1046 ConnMap::iterator iter;
1047 if((iter = connections.find(req->dbid)) != connections.end())
1051 req->error = iter->second->Query(*req);
1053 return (req->error.Id() == NO_ERROR) ? sqlsuccess : NULL;
1057 req->error.Id(BAD_DBID);
1064 virtual void OnUnloadModule(Module* mod, const std::string& name)
1066 /* When a module unloads we have to check all the pending queries for all our connections
1067 * and set the Module* specifying where the query came from to NULL. If the query has already
1068 * been dispatched then when it is processed it will be dropped if the pointer is NULL.
1070 * If the queries we find are not already being executed then we can simply remove them immediately.
1072 for(ConnMap::iterator iter = connections.begin(); iter != connections.end(); iter++)
1074 iter->second->OnUnloadModule(mod);
1078 unsigned long NewID()
1086 virtual Version GetVersion()
1088 return Version(1, 1, 0, 0, VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
1092 /* move this here to use AddConn, rather that than having the whole
1093 * module above SQLConn, since this is buggin me right now :/
1095 void SQLresolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
1098 ((ModulePgSQL*)mod)->AddConn(host);
1099 ((ModulePgSQL*)mod)->ClearOldConnections();
1102 void ReconnectTimer::Tick(time_t time)
1104 ((ModulePgSQL*)mod)->ReadConf();
1107 void SQLConn::DelayReconnect()
1109 ((ModulePgSQL*)us)->ReconnectConn(this);
1113 class ModulePgSQLFactory : public ModuleFactory
1116 ModulePgSQLFactory()
1120 ~ModulePgSQLFactory()
1124 virtual Module * CreateModule(InspIRCd* Me)
1126 return new ModulePgSQL(Me);
1131 extern "C" void * init_module( void )
1133 return new ModulePgSQLFactory;