X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fextra%2Fm_pgsql.cpp;h=b417fc0192b039185240dfcb263725789e0ed933;hb=0da6b3a13def40e8fd002b9fc60f955467f6372d;hp=2c586e1b376e5520b8dfd28489d50eee02fc4ba2;hpb=43847ec9c7e1a195163eb4c529f1c92fd1ace0a4;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/extra/m_pgsql.cpp b/src/modules/extra/m_pgsql.cpp index 2c586e1b3..b417fc019 100644 --- a/src/modules/extra/m_pgsql.cpp +++ b/src/modules/extra/m_pgsql.cpp @@ -3,7 +3,7 @@ * +------------------------------------+ * * InspIRCd: (C) 2002-2009 InspIRCd Development Team - * See: http://www.inspircd.org/wiki/index.php/Credits + * See: http://wiki.inspircd.org/Credits * * This program is free but copyrighted software; see * the file COPYING for details. @@ -44,14 +44,25 @@ typedef std::map ConnMap; */ enum SQLstatus { CREAD, CWRITE, WREAD, WWRITE, RREAD, RWRITE }; +unsigned long count(const char * const str, char a) +{ + unsigned long n = 0; + for (const char *p = reinterpret_cast(str); *p; ++p) + { + if (*p == '?') + ++n; + } + return n; +} + /** SQLhost::GetDSN() - Overload to return correct DSN for PostgreSQL */ std::string SQLhost::GetDSN() { - std::ostringstream conninfo("connect_timeout = '2'"); + std::ostringstream conninfo("connect_timeout = '5'"); - if (ip.length()) - conninfo << " hostaddr = '" << ip << "'"; + if (host.length()) + conninfo << " host = '" << host << "'"; if (port) conninfo << " port = '" << port << "'"; @@ -90,27 +101,6 @@ class ReconnectTimer : public Timer }; -/** Used to resolve sql server hostnames - */ -class SQLresolver : public Resolver -{ - private: - SQLhost host; - Module* mod; - public: - SQLresolver(Module* m, InspIRCd* Instance, const SQLhost& hi, bool &cached) - : Resolver(Instance, hi.host, DNS_QUERY_FORWARD, cached, (Module*)m), host(hi), mod(m) - { - } - - virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0); - - virtual void OnError(ResolverError e, const std::string &errormessage) - { - ServerInstance->Logs->Log("m_pgsql",DEBUG, "PgSQL: DNS lookup failed (%s), dying horribly", errormessage.c_str()); - } -}; - /** PgSQLresult is a subclass of the mostly-pure-virtual class SQLresult. * All SQL providers must create their own subclass and define it's methods using that * database library's data retriveal functions. The aim is to avoid a slow and inefficient process @@ -466,7 +456,8 @@ class SQLConn : public EventHandler case PGRES_FATAL_ERROR: reply.error.Id(SQL_QREPLY_FAIL); reply.error.Str(PQresultErrorMessage(result)); - default:; + default: + ; /* No action, other values are not errors */ } @@ -571,23 +562,32 @@ class SQLConn : public EventHandler char* query; /* Pointer to the current end of query, where we append new stuff */ char* queryend; + /* Total length of the unescaped parameters */ - unsigned int paramlen; + unsigned long maxparamlen, paramcount; - paramlen = 0; + /* The length of the longest parameter */ + maxparamlen = 0; for(ParamL::iterator i = req.query.p.begin(); i != req.query.p.end(); i++) { - paramlen += i->size(); + if (i->size() > maxparamlen) + maxparamlen = i->size(); } + /* How many params are there in the query? */ + paramcount = count(req.query.q.c_str(), '?'); + + /* This stores copy of params to be inserted with using numbered params 1;3B*/ + ParamL paramscopy(req.query.p); + /* To avoid a lot of allocations, allocate enough memory for the biggest the escaped query could possibly be. - * sizeofquery + (totalparamlength*2) + 1 + * sizeofquery + (maxtotalparamlength*2) + 1 * * The +1 is for null-terminating the string for PQsendQuery() */ - query = new char[req.query.q.length() + (paramlen*2) + 1]; + query = new char[req.query.q.length() + (maxparamlen*paramcount*2) + 1]; queryend = query; /* Okay, now we have a buffer large enough we need to start copying the query into it and escaping and substituting @@ -605,7 +605,53 @@ class SQLConn : public EventHandler * pointing at the right place. */ - if(req.query.p.size()) + /* Is it numbered parameter? + */ + + bool numbered; + numbered = false; + + /* Numbered parameter number :| + */ + unsigned int paramnum; + paramnum = 0; + + /* Let's check if it's a numbered param. And also calculate it's number. + */ + + while ((i < req.query.q.length() - 1) && (req.query.q[i+1] >= '0') && (req.query.q[i+1] <= '9')) + { + numbered = true; + ++i; + paramnum = paramnum * 10 + req.query.q[i] - '0'; + } + + if (paramnum > paramscopy.size() - 1) + { + /* index is out of range! + */ + numbered = false; + } + + if (numbered) + { + int error = 0; + size_t len = 0; + +#ifdef PGSQL_HAS_ESCAPECONN + len = PQescapeStringConn(sql, queryend, paramscopy[paramnum].c_str(), paramscopy[paramnum].length(), &error); +#else + len = PQescapeString (queryend, paramscopy[paramnum].c_str(), paramscopy[paramnum].length()); +#endif + if (error) + { + ServerInstance->Logs->Log("m_pgsql", DEBUG, "BUG: Apparently PQescapeStringConn() failed somehow...don't know how or what to do..."); + } + + /* Incremenet queryend to the end of the newly escaped parameter */ + queryend += len; + } + else if (req.query.p.size()) { int error = 0; size_t len = 0; @@ -685,7 +731,8 @@ class SQLConn : public EventHandler return confhost; } - void Close() { + void Close() + { if (!this->ServerInstance->SE->DelFd(this)) { if (sql && PQstatus(sql) == CONNECTION_BAD) @@ -733,8 +780,8 @@ class ModulePgSQL : public Module ReadConf(); ServerInstance->Modules->PublishInterface("SQL", this); - Implementation eventlist[] = { I_OnUnloadModule, I_OnRequest, I_OnRehash, I_OnUserRegister, I_OnCheckReady, I_OnUserDisconnect }; - ServerInstance->Modules->Attach(eventlist, this, 6); + Implementation eventlist[] = { I_OnUnloadModule, I_OnRequest, I_OnRehash }; + ServerInstance->Modules->Attach(eventlist, this, 3); } virtual ~ModulePgSQL() @@ -749,7 +796,7 @@ class ModulePgSQL : public Module } - virtual void OnRehash(User* user, const std::string ¶meter) + virtual void OnRehash(User* user) { ReadConf(); } @@ -791,7 +838,6 @@ class ModulePgSQL : public Module for(int i = 0; i < conf.Enumerate("database"); i++) { SQLhost host; - int ipvalid; host.id = conf.ReadValue("database", "id", i); host.host = conf.ReadValue("database", "hostname", i); @@ -804,46 +850,7 @@ class ModulePgSQL : public Module if (HasHost(host)) continue; -#ifdef IPV6 - if (strchr(host.host.c_str(),':')) - { - in6_addr blargle; - ipvalid = inet_pton(AF_INET6, host.host.c_str(), &blargle); - } - else -#endif - { - in_addr blargle; - ipvalid = inet_aton(host.host.c_str(), &blargle); - } - - if(ipvalid > 0) - { - /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */ - host.ip = host.host; - this->AddConn(host); - } - else if(ipvalid == 0) - { - /* Conversion failed, assume it's a host */ - SQLresolver* resolver; - - try - { - bool cached; - resolver = new SQLresolver(this, ServerInstance, host, cached); - ServerInstance->AddResolver(resolver, cached); - } - catch(...) - { - /* THE WORLD IS COMING TO AN END! */ - } - } - else - { - /* Invalid address family, die horribly. */ - ServerInstance->Logs->Log("m_pgsql",DEBUG, "BUG: insp_aton failed returning -1, oh noes."); - } + this->AddConn(host); } } @@ -876,13 +883,12 @@ class ModulePgSQL : public Module { if (HasHost(hi)) { - ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: A pgsql connection with id: %s already exists, possibly due to DNS delay. Aborting connection attempt.", hi.id.c_str()); + ServerInstance->Logs->Log("m_pgsql",DEFAULT, "WARNING: A pgsql connection with id: %s already exists. Aborting connection attempt.", hi.id.c_str()); return; } SQLConn* newconn; - /* The conversion succeeded, we were given an IP and we can give it straight to SQLConn */ newconn = new SQLConn(ServerInstance, this, hi); connections.insert(std::make_pair(hi.id, newconn)); @@ -954,19 +960,6 @@ class ModulePgSQL : public Module } }; -/* move this here to use AddConn, rather that than having the whole - * module above SQLConn, since this is buggin me right now :/ - */ -void SQLresolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum) -{ - if (!resultnum) - { - host.ip = result; - ((ModulePgSQL*)mod)->AddConn(host); - ((ModulePgSQL*)mod)->ClearOldConnections(); - } -} - void ReconnectTimer::Tick(time_t time) { ((ModulePgSQL*)mod)->ReadConf();