]>
jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscandb.c
4 * Handles the database interface routines for proxyscan.
9 #include "../dbapi/dbapi.h"
10 #include "../core/config.h"
11 #include "../lib/sstring.h"
12 #include "../irc/irc_config.h"
13 #include "../lib/irc_string.h"
14 #include "../core/error.h"
15 #include "../nick/nick.h"
16 #include "../localuser/localuser.h"
22 extern nick
*proxyscannick
;
24 void proxyscan_get_last_id(DBConn
*dbconn
, void *arg
);
28 * Connects to the database and recovers the last gline ID
31 int proxyscandbinit() {
37 /* Set up the table */
38 dbcreatequery("CREATE TABLE openproxies ("
43 "RH varchar not null,"
46 dbcreatequery("CREATE INDEX openproxies_id_index ON openproxies (ID)");
48 dbasyncquery(proxyscan_get_last_id
, NULL
,
49 "SELECT ID FROM openproxies ORDER BY id DESC LIMIT 1");
54 void proxyscan_get_last_id(DBConn
*dbconn
, void *arg
) {
55 DBResult
*pgres
= dbgetresult(dbconn
);
57 if(!dbquerysuccessful(pgres
)) {
58 Error("proxyscan", ERR_STOP
, "Error loading last id.");
62 if (dbfetchrow(pgres
))
63 lastid
= atoi(dbgetvalue(pgres
, 0));
68 Error("proxyscan",ERR_INFO
,"Retrieved lastid %d from database.",lastid
);
72 * Given a scan number, returns the string associated.
75 const char *scantostr(int type
) {
76 char *reason
="UNKNOWN";
103 case STYPE_DIRECT_IRC
:
117 * Given a scan number, returns the bitmask associated (as used in the
118 * previous version of P)
121 int scantodm(int scannum
) {
127 * This function takes the given scanhost (which is assumed to have
128 * at least one "OPEN" scan) and logs it to the database. It returns
129 * the unique ID assigned to this gline (for the gline message itself).
132 void loggline(cachehost
*chp
, patricia_node_t
*node
) {
133 char reasonlist
[200];
134 char reasonesc
[400 + 1]; /* reasonlist*2+1 */
146 for (fpp
=chp
->proxies
;fpp
;fpp
=fpp
->next
) {
147 if ((reasonpos
+ 20) > sizeof(reasonlist
))
150 reasonpos
+= sprintf(reasonlist
+reasonpos
, "%s:%d ",scantostr(fpp
->type
), fpp
->port
);
153 if (chp
->glineid
==0) {
154 chp
->glineid
=++lastid
;
156 dbescapestring(reasonesc
,reasonlist
,strlen(reasonlist
));
157 dbquery("INSERT INTO openproxies VALUES(%u,'%s',%d,%ld,'%s')",chp
->glineid
,
158 IPtostr(((patricia_node_t
*)node
)->prefix
->sin
),reasonmask
,getnettime(),reasonesc
);
160 dbescapestring(reasonesc
,reasonlist
,strlen(reasonlist
));
161 dbquery("UPDATE openproxies SET PM=%d,RH='%s' where ID=%u",
162 reasonmask
,reasonesc
,chp
->glineid
);
168 * Closes the db socket when proxyscan is unloaded
171 void proxyscandbclose() {
175 * proxyscandolistopen:
176 * Lists all the open proxies found since <since> to user usernick.
179 void proxyscandolistopen_real(DBConn
*dbconn
, void *arg
) {
180 nick
*np
=getnickbynumeric((unsigned long)arg
);
183 pgres
=dbgetresult(dbconn
);
184 if (!dbquerysuccessful(pgres
)) {
185 Error("proxyscan", ERR_ERROR
, "Error loading data.");
189 if (dbnumfields(pgres
) != 3) {
190 Error("proxyscan", ERR_ERROR
, "data format error.");
200 sendnoticetouser(proxyscannick
,np
,"%-20s %-22s %s","IP","Found at","What was open");
201 while(dbfetchrow(pgres
)) {
202 sendnoticetouser(proxyscannick
,np
, "%-20s %-22s %s",dbgetvalue(pgres
, 0),
203 dbgetvalue(pgres
, 1),
204 dbgetvalue(pgres
, 2));
207 sendnoticetouser(proxyscannick
,np
,"--- End of list ---");
210 int proxyscandolistopen(void *sender
, int cargc
, char **cargv
) {
211 nick
*usernick
= (nick
*)sender
;
213 dbasyncquery(proxyscandolistopen_real
,(void *)usernick
->numeric
,
214 "SELECT IP,TS,RH FROM openproxies WHERE TS>'%lu' ORDER BY TS",time(NULL
)-rescaninterval
);
220 * Check db for open proxies matching the given IP, send to user usernick.
223 void proxyscanspewip_real(DBConn
*dbconn
, void *arg
) {
224 nick
*np
=getnickbynumeric((unsigned long)arg
);
227 pgres
=dbgetresult(dbconn
);
228 if (!dbquerysuccessful(pgres
)) {
229 Error("proxyscan", ERR_ERROR
, "Error loading data.");
233 if (dbnumfields(pgres
) != 4) {
234 Error("proxyscan", ERR_ERROR
, "data format error.");
244 sendnoticetouser(proxyscannick
,np
,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
245 while(dbfetchrow(pgres
)) {
246 sendnoticetouser(proxyscannick
,np
, "%-5s %-20s %-22s %s",dbgetvalue(pgres
, 0),
247 dbgetvalue(pgres
, 1),
248 dbgetvalue(pgres
, 2),
249 dbgetvalue(pgres
, 3));
252 sendnoticetouser(proxyscannick
,np
,"--- End of list ---");
255 void proxyscanspewip(nick
*mynick
, nick
*usernick
, unsigned long a
, unsigned long b
, unsigned long c
, unsigned long d
) {
256 dbasyncquery(proxyscanspewip_real
,(void *)usernick
->numeric
,
257 "SELECT ID,IP,TS,RH FROM openproxies WHERE IP='%lu.%lu.%lu.%lu' ORDER BY TS DESC LIMIT 10",a
,b
,c
,d
);
263 * Check db for open proxies matching the given kill/gline ID, send to user usernick.
266 void proxyscanshowkill_real(DBConn
*dbconn
, void *arg
) {
267 nick
*np
=getnickbynumeric((unsigned long)arg
);
270 pgres
=dbgetresult(dbconn
);
271 if (!dbquerysuccessful(pgres
)) {
272 Error("proxyscan", ERR_ERROR
, "Error loading data.");
276 if (dbnumfields(pgres
) != 4) {
277 Error("proxyscan", ERR_ERROR
, "data format error.");
287 sendnoticetouser(proxyscannick
,np
,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
288 while(dbfetchrow(pgres
)) {
289 sendnoticetouser(proxyscannick
,np
, "%-5s %-20s %-22s %s",dbgetvalue(pgres
, 0),
290 dbgetvalue(pgres
, 1),
291 dbgetvalue(pgres
, 2),
292 dbgetvalue(pgres
, 3));
295 sendnoticetouser(proxyscannick
,np
,"--- End of list ---");
298 void proxyscanshowkill(nick
*mynick
, nick
*usernick
, unsigned long a
) {
299 dbasyncquery(proxyscanspewip_real
,(void *)usernick
->numeric
,
300 "SELECT ID,IP,TS,RH FROM openproxies WHERE ID='%lu'",a
);