#include "proxyscan.h"
-#include <mysql/mysql.h>
+#include "../dbapi/dbapi.h"
#include "../core/config.h"
#include "../lib/sstring.h"
#include "../irc/irc_config.h"
#include <string.h>
#include <stdio.h>
-MYSQL proxyscansql;
unsigned int lastid;
-int sqlconnected;
+int sqlconnected = 0;
+extern nick *proxyscannick;
+
+void proxyscan_get_last_id(DBConn *dbconn, void *arg);
/*
* proxyscandbinit():
*/
int proxyscandbinit() {
- sstring *dbhost,*dbusername,*dbpassword,*dbdatabase,*dbport;
- MYSQL_RES *myres;
- MYSQL_ROW myrow;
-
- sqlconnected=0;
-
- dbhost=getcopyconfigitem("proxyscan","dbhost","localhost",HOSTLEN);
- dbusername=getcopyconfigitem("proxyscan","dbusername","proxyscan",20);
- dbpassword=getcopyconfigitem("proxyscan","dbpassword","moo",20);
- dbdatabase=getcopyconfigitem("proxyscan","dbdatabase","proxyscan",20);
- dbport=getcopyconfigitem("proxyscan","dbport","3306",8);
-
- mysql_init(&proxyscansql);
- if (!mysql_real_connect(&proxyscansql,dbhost->content,dbusername->content,
- dbpassword->content,dbdatabase->content,
- strtol(dbport->content,NULL,10), NULL, 0)) {
- Error("proxyscan",ERR_ERROR,"Unable to connect to database");
+ if(!dbconnected())
return 1;
- }
- freesstring(dbhost);
- freesstring(dbusername);
- freesstring(dbpassword);
- freesstring(dbdatabase);
- freesstring(dbport);
-
sqlconnected=1;
/* Set up the table */
- mysql_query(&proxyscansql,"CREATE TABLE openproxies (ID BIGINT not null, IP VARCHAR (20) not null, PM INT not null, TS DATETIME not null, RH TEXT not null, PRIMARY KEY (ID), INDEX (ID), UNIQUE (ID))");
+ dbcreatequery("CREATE TABLE openproxies ("
+ "ID int8 not null,"
+ "IP inet not null,"
+ "PM int4 not null,"
+ "TS int4 not null,"
+ "RH varchar not null,"
+ "PRIMARY KEY (ID))");
- /* Get max ID */
- if ((mysql_query(&proxyscansql,"SELECT max(ID) FROM openproxies"))!=0) {
- Error("proxyscan",ERR_ERROR,"Unable to retrieve max ID from database");
- return 1;
- }
-
- myres=mysql_store_result(&proxyscansql);
- if (mysql_num_fields(myres)!=1) {
- Error("proxyscan",ERR_ERROR,"Weird format retrieving max ID");
- mysql_free_result(myres);
- return 1;
- }
+ dbcreatequery("CREATE INDEX openproxies_id_index ON openproxies (ID)");
+
+ dbasyncquery(proxyscan_get_last_id, NULL,
+ "SELECT ID FROM openproxies ORDER BY id DESC LIMIT 1");
- lastid=0;
- if ((myrow=mysql_fetch_row(myres))) {
- if (myrow[0]==NULL) {
- lastid=0;
- } else {
- lastid=strtol(myrow[0],NULL,10);
- }
- Error("proxyscan",ERR_INFO,"Retrieved lastid %d from database.",lastid);
- }
-
- mysql_free_result(myres);
return 0;
}
+void proxyscan_get_last_id(DBConn *dbconn, void *arg) {
+ DBResult *pgres = dbgetresult(dbconn);
+
+ if(!dbquerysuccessful(pgres)) {
+ Error("proxyscan", ERR_STOP, "Error loading last id.");
+ return;
+ }
+
+ if (dbfetchrow(pgres))
+ lastid = atoi(dbgetvalue(pgres, 0));
+ else
+ lastid = 0;
+
+ dbclear(pgres);
+ Error("proxyscan",ERR_INFO,"Retrieved lastid %d from database.",lastid);
+}
/*
* scantostr:
* Given a scan number, returns the string associated.
case STYPE_DIRECT:
reason="forward";
break;
+
+ case STYPE_DIRECT_IRC:
+ reason="fwdirc";
+ break;
+
+ case STYPE_ROUTER:
+ reason="router";
+ break;
}
return reason;
* the unique ID assigned to this gline (for the gline message itself).
*/
-void loggline(cachehost *chp) {
- char reasonlist[100];
- char reasonesc[100];
- char sqlquery[4000];
+void loggline(cachehost *chp, patricia_node_t *node) {
+ 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;
- mysql_escape_string(reasonesc,reasonlist,strlen(reasonlist));
- sprintf(sqlquery,"INSERT INTO openproxies VALUES(%u,'%s',%d,NOW(),'%s')",chp->glineid,
- IPtostr(chp->IP),reasonmask,reasonesc);
- mysql_query(&proxyscansql,sqlquery);
+ 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 {
- mysql_escape_string(reasonesc,reasonlist,strlen(reasonlist));
- sprintf(sqlquery,"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);
- mysql_query(&proxyscansql,sqlquery);
}
}
*/
void proxyscandbclose() {
- if (sqlconnected==1) {
- mysql_close(&proxyscansql);
- }
}
/*
* Lists all the open proxies found since <since> to user usernick.
*/
-void proxyscandolistopen(nick *mynick, nick *usernick, time_t snce) {
- char mysqlquery[2000];
- char timestmp[30];
- MYSQL_RES *myres;
- MYSQL_ROW myrow;
+void proxyscandolistopen_real(DBConn *dbconn, void *arg) {
+ nick *np=getnickbynumeric((unsigned long)arg);
+ DBResult *pgres;
+
+ pgres=dbgetresult(dbconn);
+ if (!dbquerysuccessful(pgres)) {
+ Error("proxyscan", ERR_ERROR, "Error loading data.");
+ return;
+ }
+
+ if (dbnumfields(pgres) != 3) {
+ Error("proxyscan", ERR_ERROR, "data format error.");
+ dbclear(pgres);
+ return;
+ }
+
+ if (!np) {
+ dbclear(pgres);
+ return;
+ }
+
+ sendnoticetouser(proxyscannick,np,"%-20s %-22s %s","IP","Found at","What was open");
+ 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 ---");
+}
+
+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;
+}
+
+/*
+ * proxyscanspewip
+ * Check db for open proxies matching the given IP, send to user usernick.
+ */
+
+void proxyscanspewip_real(DBConn *dbconn, void *arg) {
+ nick *np=getnickbynumeric((unsigned long)arg);
+ DBResult *pgres;
+ char timebuf[30];
- strftime(timestmp,30,"%Y-%m-%d %H:%M:%S",localtime(&snce));
- sprintf(mysqlquery,"SELECT IP,TS,RH FROM openproxies WHERE TS>'%s' ORDER BY TS",timestmp);
+ pgres=dbgetresult(dbconn);
+ if (!dbquerysuccessful(pgres)) {
+ Error("proxyscan", ERR_ERROR, "Error loading data.");
+ return;
+ }
- if ((mysql_query(&proxyscansql,mysqlquery))!=0) {
- sendnoticetouser(mynick,usernick,"Error performing database query!");
- Error("proxyscan",ERR_ERROR,"Error performing listopen query");
+ if (dbnumfields(pgres) != 4) {
+ Error("proxyscan", ERR_ERROR, "data format error.");
+ dbclear(pgres);
return;
}
- myres=mysql_use_result(&proxyscansql);
- if (mysql_num_fields(myres)!=3) {
- sendnoticetouser(mynick,usernick,"Error performing database query!");
- Error("proxyscan",ERR_ERROR,"Error performing listopen query");
+ if (!np) {
+ dbclear(pgres);
return;
}
- sendnoticetouser(mynick,usernick,"%-20s %-22s %s","IP","Found at","What was open");
- while ((myrow=mysql_fetch_row(myres))) {
- sendnoticetouser(mynick,usernick,"%-20s %-22s %s",myrow[0],myrow[1],myrow[2]);
+ sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
+ 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));
}
- sendnoticetouser(mynick,usernick,"--- End of list ---");
- mysql_free_result(myres);
+ 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) {
+ 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);
+
+}
+
+/*
+ * proxyscanshowkill
+ * Check db for open proxies matching the given kill/gline ID, send to user usernick.
+ */
+
+void proxyscanshowkill_real(DBConn *dbconn, void *arg) {
+ nick *np=getnickbynumeric((unsigned long)arg);
+ DBResult *pgres;
+
+ pgres=dbgetresult(dbconn);
+ if (!dbquerysuccessful(pgres)) {
+ Error("proxyscan", ERR_ERROR, "Error loading data.");
+ return;
+ }
+
+ if (dbnumfields(pgres) != 4) {
+ Error("proxyscan", ERR_ERROR, "data format error.");
+ dbclear(pgres);
+ return;
+ }
+
+ if (!np) {
+ dbclear(pgres);
+ return;
+ }
+
+ sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
+ 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) {
+ dbasyncquery(proxyscanspewip_real,(void *)usernick->numeric,
+ "SELECT ID,IP,TS,RH FROM openproxies WHERE ID='%lu'",a);
}