#include "proxyscan.h"
-#include "../pqsql/pqsql.h"
+#include "../dbapi/dbapi.h"
#include "../core/config.h"
#include "../lib/sstring.h"
#include "../irc/irc_config.h"
#include "../localuser/localuser.h"
#include <string.h>
#include <stdio.h>
-#include <libpq-fe.h>
unsigned int lastid;
int sqlconnected = 0;
extern nick *proxyscannick;
-void proxyscan_get_last_id(PGconn *dbconn, void *arg);
+void proxyscan_get_last_id(DBConn *dbconn, void *arg);
/*
* proxyscandbinit():
*/
int proxyscandbinit() {
- if(!pqconnected())
+ if(!dbconnected())
return 1;
sqlconnected=1;
/* Set up the table */
- pqcreatequery("CREATE TABLE openproxies ("
+ dbcreatequery("CREATE TABLE openproxies ("
"ID int8 not null,"
"IP inet not null,"
"PM int4 not null,"
"RH varchar not null,"
"PRIMARY KEY (ID))");
- pqcreatequery("CREATE INDEX openproxies_id_index ON openproxies (ID)");
+ dbcreatequery("CREATE INDEX openproxies_id_index ON openproxies (ID)");
- pqasyncquery(proxyscan_get_last_id, NULL,
+ dbasyncquery(proxyscan_get_last_id, NULL,
"SELECT ID FROM openproxies ORDER BY id DESC LIMIT 1");
return 0;
}
-void proxyscan_get_last_id(PGconn *dbconn, void *arg) {
- PGresult *pgres = PQgetResult(dbconn);
- unsigned int numrows;
+void proxyscan_get_last_id(DBConn *dbconn, void *arg) {
+ DBResult *pgres = dbgetresult(dbconn);
- if(PQresultStatus(pgres) != PGRES_TUPLES_OK) {
- Error("proxyscan", ERR_ERROR, "Error loading last id.");
+ if(!dbquerysuccessful(pgres)) {
+ Error("proxyscan", ERR_STOP, "Error loading last id.");
+ return;
}
- numrows = PQntuples(pgres);
- if ( numrows )
- lastid = atoi(PQgetvalue(pgres, 0, 0));
+ if (dbfetchrow(pgres))
+ lastid = atoi(dbgetvalue(pgres, 0));
else
lastid = 0;
- PQclear(pgres);
- Error("proxyscan",ERR_INFO,"Retrieved lastid %d from database.",lastid);
+ dbclear(pgres);
+ Error("proxyscan",ERR_INFO,"Retrieved lastid %d from database.",lastid);
}
/*
* scantostr:
case STYPE_DIRECT:
reason="forward";
break;
+
+ case STYPE_DIRECT_IRC:
+ reason="fwdirc";
+ break;
+
+ case STYPE_ROUTER:
+ reason="router";
+ break;
}
return reason;
*/
void loggline(cachehost *chp, patricia_node_t *node) {
- char reasonlist[100];
- char reasonesc[200 + 1]; /* reasonlist*2+1 */
+ char reasonlist[200];
+ char reasonesc[400 + 1]; /* reasonlist*2+1 */
int reasonmask=0;
int reasonpos=0;
foundproxy *fpp;
reasonlist[0]='\0';
reasonmask=0;
for (fpp=chp->proxies;fpp;fpp=fpp->next) {
+ if ((reasonpos + 20) > sizeof(reasonlist))
+ break;
+
reasonpos += sprintf(reasonlist+reasonpos, "%s:%d ",scantostr(fpp->type), fpp->port);
}
if (chp->glineid==0) {
chp->glineid=++lastid;
- PQescapeString(reasonesc,reasonlist,strlen(reasonlist));
- pqquery("INSERT INTO openproxies VALUES(%u,'%s',%d,%ld,'%s')",chp->glineid,
+ dbescapestring(reasonesc,reasonlist,strlen(reasonlist));
+ dbquery("INSERT INTO openproxies VALUES(%u,'%s',%d,%ld,'%s')",chp->glineid,
IPtostr(((patricia_node_t *)node)->prefix->sin),reasonmask,getnettime(),reasonesc);
} else {
- PQescapeString(reasonesc,reasonlist,strlen(reasonlist));
- pqquery("UPDATE openproxies SET PM=%d,RH='%s' where ID=%u",
+ dbescapestring(reasonesc,reasonlist,strlen(reasonlist));
+ dbquery("UPDATE openproxies SET PM=%d,RH='%s' where ID=%u",
reasonmask,reasonesc,chp->glineid);
}
}
* Lists all the open proxies found since <since> to user usernick.
*/
-void proxyscandolistopen_real(PGconn *dbconn, void *arg) {
- nick *np=getnickbynumeric((unsigned int)arg);
- PGresult *pgres;
- int i, num;
+void proxyscandolistopen_real(DBConn *dbconn, void *arg) {
+ nick *np=getnickbynumeric((unsigned long)arg);
+ DBResult *pgres;
- pgres=PQgetResult(dbconn);
- if (PQresultStatus(pgres) != PGRES_TUPLES_OK) {
+ pgres=dbgetresult(dbconn);
+ if (!dbquerysuccessful(pgres)) {
Error("proxyscan", ERR_ERROR, "Error loading data.");
return;
}
- if (PQnfields(pgres) != 3) {
+ if (dbnumfields(pgres) != 3) {
Error("proxyscan", ERR_ERROR, "data format error.");
+ dbclear(pgres);
+ return;
}
- num=PQntuples(pgres);
-
if (!np) {
- PQclear(pgres);
+ dbclear(pgres);
return;
}
sendnoticetouser(proxyscannick,np,"%-20s %-22s %s","IP","Found at","What was open");
- for (i=0; i<num; i++) {
- sendnoticetouser(proxyscannick,np, "%-20s %-22s %s",PQgetvalue(pgres, i, 0),
- PQgetvalue(pgres, i, 1),
- PQgetvalue(pgres, i, 2));
+ while(dbfetchrow(pgres)) {
+ sendnoticetouser(proxyscannick,np, "%-20s %-22s %s",dbgetvalue(pgres, 0),
+ dbgetvalue(pgres, 1),
+ dbgetvalue(pgres, 2));
}
+ dbclear(pgres);
sendnoticetouser(proxyscannick,np,"--- End of list ---");
}
-void proxyscandolistopen(nick *mynick, nick *usernick, time_t snce) {
-
- pqasyncquery(proxyscandolistopen_real,(void *)usernick->numeric,
- "SELECT IP,TS,RH FROM openproxies WHERE TS>'%lu' ORDER BY TS",snce);
+int proxyscandolistopen(void *sender, int cargc, char **cargv) {
+ nick *usernick = (nick *)sender;
+
+ dbasyncquery(proxyscandolistopen_real,(void *)usernick->numeric,
+ "SELECT IP,TS,RH FROM openproxies WHERE TS>'%lu' ORDER BY TS",time(NULL)-rescaninterval);
+ return CMD_OK;
}
/*
* Check db for open proxies matching the given IP, send to user usernick.
*/
-void proxyscanspewip_real(PGconn *dbconn, void *arg) {
- nick *np=getnickbynumeric((unsigned int)arg);
- PGresult *pgres;
- int i, num;
+void proxyscanspewip_real(DBConn *dbconn, void *arg) {
+ nick *np=getnickbynumeric((unsigned long)arg);
+ DBResult *pgres;
+ char timebuf[30];
- pgres=PQgetResult(dbconn);
- if (PQresultStatus(pgres) != PGRES_TUPLES_OK) {
+ pgres=dbgetresult(dbconn);
+ if (!dbquerysuccessful(pgres)) {
Error("proxyscan", ERR_ERROR, "Error loading data.");
return;
}
- if (PQnfields(pgres) != 4) {
+ if (dbnumfields(pgres) != 4) {
Error("proxyscan", ERR_ERROR, "data format error.");
+ dbclear(pgres);
+ return;
}
- num=PQntuples(pgres);
-
if (!np) {
- PQclear(pgres);
+ dbclear(pgres);
return;
}
sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
- for (i=0; i<num; i++) {
- sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",PQgetvalue(pgres, i, 0),
- PQgetvalue(pgres, i, 1),
- PQgetvalue(pgres, i, 2),
- PQgetvalue(pgres, i, 3));
+ while(dbfetchrow(pgres)) {
+ time_t t = strtoul(dbgetvalue(pgres, 2), NULL, 10);
+ strftime(timebuf, sizeof(timebuf), "%d/%m/%y %H:%M GMT", gmtime(&t));
+
+ sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",dbgetvalue(pgres, 0),
+ dbgetvalue(pgres, 1),
+ timebuf,
+ dbgetvalue(pgres, 3));
}
+ dbclear(pgres);
sendnoticetouser(proxyscannick,np,"--- End of list ---");
}
void proxyscanspewip(nick *mynick, nick *usernick, unsigned long a, unsigned long b, unsigned long c, unsigned long d) {
- pqasyncquery(proxyscanspewip_real,(void *)usernick->numeric,
+ dbasyncquery(proxyscanspewip_real,(void *)usernick->numeric,
"SELECT ID,IP,TS,RH FROM openproxies WHERE IP='%lu.%lu.%lu.%lu' ORDER BY TS DESC LIMIT 10",a,b,c,d);
}
* Check db for open proxies matching the given kill/gline ID, send to user usernick.
*/
-void proxyscanshowkill_real(PGconn *dbconn, void *arg) {
- nick *np=getnickbynumeric((unsigned int)arg);
- PGresult *pgres;
- int i, num;
+void proxyscanshowkill_real(DBConn *dbconn, void *arg) {
+ nick *np=getnickbynumeric((unsigned long)arg);
+ DBResult *pgres;
- pgres=PQgetResult(dbconn);
- if (PQresultStatus(pgres) != PGRES_TUPLES_OK) {
+ pgres=dbgetresult(dbconn);
+ if (!dbquerysuccessful(pgres)) {
Error("proxyscan", ERR_ERROR, "Error loading data.");
return;
}
- if (PQnfields(pgres) != 4) {
+ if (dbnumfields(pgres) != 4) {
Error("proxyscan", ERR_ERROR, "data format error.");
+ dbclear(pgres);
+ return;
}
- num=PQntuples(pgres);
-
if (!np) {
- PQclear(pgres);
+ dbclear(pgres);
return;
}
sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
- for (i=0; i<num; i++) {
- sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",PQgetvalue(pgres, i, 0),
- PQgetvalue(pgres, i, 1),
- PQgetvalue(pgres, i, 2),
- PQgetvalue(pgres, i, 3));
+ while(dbfetchrow(pgres)) {
+ sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",dbgetvalue(pgres, 0),
+ dbgetvalue(pgres, 1),
+ dbgetvalue(pgres, 2),
+ dbgetvalue(pgres, 3));
}
+ dbclear(pgres);
sendnoticetouser(proxyscannick,np,"--- End of list ---");
}
-
void proxyscanshowkill(nick *mynick, nick *usernick, unsigned long a) {
- pqasyncquery(proxyscanspewip_real,(void *)usernick->numeric,
+ dbasyncquery(proxyscanspewip_real,(void *)usernick->numeric,
"SELECT ID,IP,TS,RH FROM openproxies WHERE ID='%lu'",a);
}