]>
jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscandb.c
4 * Handles the database interface routines for proxyscan.
9 #include "../pqsql/pqsql.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"
23 extern nick
*proxyscannick
;
25 void proxyscan_get_last_id(PGconn
*dbconn
, void *arg
);
29 * Connects to the database and recovers the last gline ID
32 int proxyscandbinit() {
38 /* Set up the table */
39 pqcreatequery("CREATE TABLE openproxies ("
44 "RH varchar not null,"
47 pqcreatequery("CREATE INDEX openproxies_id_index ON openproxies (ID)");
49 pqasyncquery(proxyscan_get_last_id
, NULL
,
50 "SELECT ID FROM openproxies ORDER BY id DESC LIMIT 1");
55 void proxyscan_get_last_id(PGconn
*dbconn
, void *arg
) {
56 PGresult
*pgres
= PQgetResult(dbconn
);
59 if(PQresultStatus(pgres
) != PGRES_TUPLES_OK
) {
60 Error("proxyscan", ERR_ERROR
, "Error loading last id.");
63 numrows
= PQntuples(pgres
);
65 lastid
= atoi(PQgetvalue(pgres
, 0, 0));
70 Error("proxyscan",ERR_INFO
,"Retrieved lastid %d from database.",lastid
);
74 * Given a scan number, returns the string associated.
77 const char *scantostr(int type
) {
78 char *reason
="UNKNOWN";
111 * Given a scan number, returns the bitmask associated (as used in the
112 * previous version of P)
115 int scantodm(int scannum
) {
121 * This function takes the given scanhost (which is assumed to have
122 * at least one "OPEN" scan) and logs it to the database. It returns
123 * the unique ID assigned to this gline (for the gline message itself).
126 void loggline(cachehost
*chp
, patricia_node_t
*node
) {
127 char reasonlist
[100];
128 char reasonesc
[200 + 1]; /* reasonlist*2+1 */
140 for (fpp
=chp
->proxies
;fpp
;fpp
=fpp
->next
) {
141 reasonpos
+= sprintf(reasonlist
+reasonpos
, "%s:%d ",scantostr(fpp
->type
), fpp
->port
);
144 if (chp
->glineid
==0) {
145 chp
->glineid
=++lastid
;
147 PQescapeString(reasonesc
,reasonlist
,strlen(reasonlist
));
148 pqquery("INSERT INTO openproxies VALUES(%u,'%s',%d,%ld,'%s')",chp
->glineid
,
149 IPtostr(((patricia_node_t
*)node
)->prefix
->sin
),reasonmask
,getnettime(),reasonesc
);
151 PQescapeString(reasonesc
,reasonlist
,strlen(reasonlist
));
152 pqquery("UPDATE openproxies SET PM=%d,RH='%s' where ID=%u",
153 reasonmask
,reasonesc
,chp
->glineid
);
159 * Closes the db socket when proxyscan is unloaded
162 void proxyscandbclose() {
166 * proxyscandolistopen:
167 * Lists all the open proxies found since <since> to user usernick.
170 void proxyscandolistopen_real(PGconn
*dbconn
, void *arg
) {
171 nick
*np
=getnickbynumeric((unsigned long)arg
);
175 pgres
=PQgetResult(dbconn
);
176 if (PQresultStatus(pgres
) != PGRES_TUPLES_OK
) {
177 Error("proxyscan", ERR_ERROR
, "Error loading data.");
181 if (PQnfields(pgres
) != 3) {
182 Error("proxyscan", ERR_ERROR
, "data format error.");
185 num
=PQntuples(pgres
);
192 sendnoticetouser(proxyscannick
,np
,"%-20s %-22s %s","IP","Found at","What was open");
193 for (i
=0; i
<num
; i
++) {
194 sendnoticetouser(proxyscannick
,np
, "%-20s %-22s %s",PQgetvalue(pgres
, i
, 0),
195 PQgetvalue(pgres
, i
, 1),
196 PQgetvalue(pgres
, i
, 2));
198 sendnoticetouser(proxyscannick
,np
,"--- End of list ---");
201 void proxyscandolistopen(nick
*mynick
, nick
*usernick
, time_t snce
) {
203 pqasyncquery(proxyscandolistopen_real
,(void *)usernick
->numeric
,
204 "SELECT IP,TS,RH FROM openproxies WHERE TS>'%lu' ORDER BY TS",snce
);
209 * Check db for open proxies matching the given IP, send to user usernick.
212 void proxyscanspewip_real(PGconn
*dbconn
, void *arg
) {
213 nick
*np
=getnickbynumeric((unsigned long)arg
);
217 pgres
=PQgetResult(dbconn
);
218 if (PQresultStatus(pgres
) != PGRES_TUPLES_OK
) {
219 Error("proxyscan", ERR_ERROR
, "Error loading data.");
223 if (PQnfields(pgres
) != 4) {
224 Error("proxyscan", ERR_ERROR
, "data format error.");
227 num
=PQntuples(pgres
);
234 sendnoticetouser(proxyscannick
,np
,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
235 for (i
=0; i
<num
; i
++) {
236 sendnoticetouser(proxyscannick
,np
, "%-5s %-20s %-22s %s",PQgetvalue(pgres
, i
, 0),
237 PQgetvalue(pgres
, i
, 1),
238 PQgetvalue(pgres
, i
, 2),
239 PQgetvalue(pgres
, i
, 3));
241 sendnoticetouser(proxyscannick
,np
,"--- End of list ---");
244 void proxyscanspewip(nick
*mynick
, nick
*usernick
, unsigned long a
, unsigned long b
, unsigned long c
, unsigned long d
) {
245 pqasyncquery(proxyscanspewip_real
,(void *)usernick
->numeric
,
246 "SELECT ID,IP,TS,RH FROM openproxies WHERE IP='%lu.%lu.%lu.%lu' ORDER BY TS DESC LIMIT 10",a
,b
,c
,d
);
252 * Check db for open proxies matching the given kill/gline ID, send to user usernick.
255 void proxyscanshowkill_real(PGconn
*dbconn
, void *arg
) {
256 nick
*np
=getnickbynumeric((unsigned long)arg
);
260 pgres
=PQgetResult(dbconn
);
261 if (PQresultStatus(pgres
) != PGRES_TUPLES_OK
) {
262 Error("proxyscan", ERR_ERROR
, "Error loading data.");
266 if (PQnfields(pgres
) != 4) {
267 Error("proxyscan", ERR_ERROR
, "data format error.");
270 num
=PQntuples(pgres
);
277 sendnoticetouser(proxyscannick
,np
,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
278 for (i
=0; i
<num
; i
++) {
279 sendnoticetouser(proxyscannick
,np
, "%-5s %-20s %-22s %s",PQgetvalue(pgres
, i
, 0),
280 PQgetvalue(pgres
, i
, 1),
281 PQgetvalue(pgres
, i
, 2),
282 PQgetvalue(pgres
, i
, 3));
284 sendnoticetouser(proxyscannick
,np
,"--- End of list ---");
288 void proxyscanshowkill(nick
*mynick
, nick
*usernick
, unsigned long a
) {
289 pqasyncquery(proxyscanspewip_real
,(void *)usernick
->numeric
,
290 "SELECT ID,IP,TS,RH FROM openproxies WHERE ID='%lu'",a
);