]> jfr.im git - irc/quakenet/newserv.git/blame - proxyscan/proxyscandb.c
PROXYSCAN: Add port 6666 and change scan type displayed reason.
[irc/quakenet/newserv.git] / proxyscan / proxyscandb.c
CommitLineData
c86edd1d
Q
1
2/*
3 * proxyscandb:
4 * Handles the database interface routines for proxyscan.
5 */
6
7#include "proxyscan.h"
8
ee8cd7d0 9#include "../dbapi/dbapi.h"
c86edd1d
Q
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"
17#include <string.h>
18#include <stdio.h>
19
c86edd1d 20unsigned int lastid;
96464e2c
P
21int sqlconnected = 0;
22extern nick *proxyscannick;
23
ee8cd7d0 24void proxyscan_get_last_id(DBConn *dbconn, void *arg);
c86edd1d
Q
25
26/*
27 * proxyscandbinit():
28 * Connects to the database and recovers the last gline ID
29 */
30
31int proxyscandbinit() {
ee8cd7d0 32 if(!dbconnected())
c86edd1d 33 return 1;
c86edd1d 34
c86edd1d
Q
35 sqlconnected=1;
36
37 /* Set up the table */
ee8cd7d0 38 dbcreatequery("CREATE TABLE openproxies ("
96464e2c
P
39 "ID int8 not null,"
40 "IP inet not null,"
41 "PM int4 not null,"
42 "TS int4 not null,"
43 "RH varchar not null,"
0a85c6ba 44 "PRIMARY KEY (ID))");
c86edd1d 45
ee8cd7d0 46 dbcreatequery("CREATE INDEX openproxies_id_index ON openproxies (ID)");
96464e2c 47
ee8cd7d0 48 dbasyncquery(proxyscan_get_last_id, NULL,
96464e2c 49 "SELECT ID FROM openproxies ORDER BY id DESC LIMIT 1");
c86edd1d 50
c86edd1d
Q
51 return 0;
52}
53
ee8cd7d0
CP
54void proxyscan_get_last_id(DBConn *dbconn, void *arg) {
55 DBResult *pgres = dbgetresult(dbconn);
96464e2c 56
ee8cd7d0
CP
57 if(!dbquerysuccessful(pgres)) {
58 Error("proxyscan", ERR_STOP, "Error loading last id.");
59 return;
96464e2c
P
60 }
61
ee8cd7d0
CP
62 if (dbfetchrow(pgres))
63 lastid = atoi(dbgetvalue(pgres, 0));
96464e2c
P
64 else
65 lastid = 0;
66
ee8cd7d0
CP
67 dbclear(pgres);
68 Error("proxyscan",ERR_INFO,"Retrieved lastid %d from database.",lastid);
96464e2c 69}
c86edd1d
Q
70/*
71 * scantostr:
72 * Given a scan number, returns the string associated.
73 */
74
75const char *scantostr(int type) {
76 char *reason="UNKNOWN";
77
78 switch (type) {
79 case STYPE_SOCKS4:
80 reason="socks4";
81 break;
82
83 case STYPE_SOCKS5:
84 reason="socks5";
85 break;
86
87 case STYPE_HTTP:
88 reason="http";
89 break;
90
91 case STYPE_WINGATE:
92 reason="wingate";
93 break;
94
95 case STYPE_CISCO:
96 reason="router";
97 break;
905c2ba2 98
99 case STYPE_DIRECT:
100 reason="forward";
101 break;
c7be40f8
CP
102
103 case STYPE_DIRECT_IRC:
468b83b2 104 reason="fwdirc";
c7be40f8 105 break;
c86edd1d
Q
106 }
107
108 return reason;
109}
110
111/*
112 * scantobm:
113 * Given a scan number, returns the bitmask associated (as used in the
114 * previous version of P)
115 */
116
117int scantodm(int scannum) {
118 return (1<<scannum);
119}
120
121/*
122 * loggline:
123 * This function takes the given scanhost (which is assumed to have
124 * at least one "OPEN" scan) and logs it to the database. It returns
125 * the unique ID assigned to this gline (for the gline message itself).
126 */
127
557c8cb2 128void loggline(cachehost *chp, patricia_node_t *node) {
3f913369
C
129 char reasonlist[200];
130 char reasonesc[400 + 1]; /* reasonlist*2+1 */
c86edd1d
Q
131 int reasonmask=0;
132 int reasonpos=0;
133 foundproxy *fpp;
134
135 if (brokendb) {
136 chp->glineid=1;
137 return;
138 }
139
140 reasonlist[0]='\0';
141 reasonmask=0;
142 for (fpp=chp->proxies;fpp;fpp=fpp->next) {
3f913369
C
143 if ((reasonpos + 20) > sizeof(reasonlist))
144 break;
145
c86edd1d
Q
146 reasonpos += sprintf(reasonlist+reasonpos, "%s:%d ",scantostr(fpp->type), fpp->port);
147 }
148
149 if (chp->glineid==0) {
150 chp->glineid=++lastid;
151
ee8cd7d0
CP
152 dbescapestring(reasonesc,reasonlist,strlen(reasonlist));
153 dbquery("INSERT INTO openproxies VALUES(%u,'%s',%d,%ld,'%s')",chp->glineid,
557c8cb2 154 IPtostr(((patricia_node_t *)node)->prefix->sin),reasonmask,getnettime(),reasonesc);
c86edd1d 155 } else {
ee8cd7d0
CP
156 dbescapestring(reasonesc,reasonlist,strlen(reasonlist));
157 dbquery("UPDATE openproxies SET PM=%d,RH='%s' where ID=%u",
c86edd1d 158 reasonmask,reasonesc,chp->glineid);
c86edd1d
Q
159 }
160}
161
162/*
163 * proxyscandbclose:
164 * Closes the db socket when proxyscan is unloaded
165 */
166
167void proxyscandbclose() {
c86edd1d
Q
168}
169
170/*
171 * proxyscandolistopen:
172 * Lists all the open proxies found since <since> to user usernick.
173 */
174
ee8cd7d0 175void proxyscandolistopen_real(DBConn *dbconn, void *arg) {
c54295ef 176 nick *np=getnickbynumeric((unsigned long)arg);
ee8cd7d0 177 DBResult *pgres;
c86edd1d 178
ee8cd7d0
CP
179 pgres=dbgetresult(dbconn);
180 if (!dbquerysuccessful(pgres)) {
96464e2c 181 Error("proxyscan", ERR_ERROR, "Error loading data.");
c86edd1d
Q
182 return;
183 }
96464e2c 184
ee8cd7d0 185 if (dbnumfields(pgres) != 3) {
96464e2c 186 Error("proxyscan", ERR_ERROR, "data format error.");
ee8cd7d0
CP
187 dbclear(pgres);
188 return;
96464e2c 189 }
c86edd1d 190
96464e2c 191 if (!np) {
ee8cd7d0 192 dbclear(pgres);
c86edd1d
Q
193 return;
194 }
195
96464e2c 196 sendnoticetouser(proxyscannick,np,"%-20s %-22s %s","IP","Found at","What was open");
ee8cd7d0
CP
197 while(dbfetchrow(pgres)) {
198 sendnoticetouser(proxyscannick,np, "%-20s %-22s %s",dbgetvalue(pgres, 0),
199 dbgetvalue(pgres, 1),
200 dbgetvalue(pgres, 2));
c86edd1d 201 }
ee8cd7d0 202 dbclear(pgres);
96464e2c
P
203 sendnoticetouser(proxyscannick,np,"--- End of list ---");
204}
205
7ab80d0c
P
206int proxyscandolistopen(void *sender, int cargc, char **cargv) {
207 nick *usernick = (nick *)sender;
208
ee8cd7d0 209 dbasyncquery(proxyscandolistopen_real,(void *)usernick->numeric,
7ab80d0c
P
210 "SELECT IP,TS,RH FROM openproxies WHERE TS>'%lu' ORDER BY TS",time(NULL)-rescaninterval);
211 return CMD_OK;
c86edd1d 212}
7d228b1d
D
213
214/*
215 * proxyscanspewip
216 * Check db for open proxies matching the given IP, send to user usernick.
217 */
218
ee8cd7d0 219void proxyscanspewip_real(DBConn *dbconn, void *arg) {
c54295ef 220 nick *np=getnickbynumeric((unsigned long)arg);
ee8cd7d0 221 DBResult *pgres;
7d228b1d 222
ee8cd7d0
CP
223 pgres=dbgetresult(dbconn);
224 if (!dbquerysuccessful(pgres)) {
96464e2c 225 Error("proxyscan", ERR_ERROR, "Error loading data.");
7d228b1d
D
226 return;
227 }
228
ee8cd7d0 229 if (dbnumfields(pgres) != 4) {
96464e2c 230 Error("proxyscan", ERR_ERROR, "data format error.");
ee8cd7d0
CP
231 dbclear(pgres);
232 return;
96464e2c
P
233 }
234
96464e2c 235 if (!np) {
ee8cd7d0 236 dbclear(pgres);
7d228b1d
D
237 return;
238 }
239
96464e2c 240 sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
ee8cd7d0
CP
241 while(dbfetchrow(pgres)) {
242 sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",dbgetvalue(pgres, 0),
243 dbgetvalue(pgres, 1),
244 dbgetvalue(pgres, 2),
245 dbgetvalue(pgres, 3));
7d228b1d 246 }
ee8cd7d0 247 dbclear(pgres);
96464e2c
P
248 sendnoticetouser(proxyscannick,np,"--- End of list ---");
249}
250
251void proxyscanspewip(nick *mynick, nick *usernick, unsigned long a, unsigned long b, unsigned long c, unsigned long d) {
ee8cd7d0 252 dbasyncquery(proxyscanspewip_real,(void *)usernick->numeric,
96464e2c
P
253 "SELECT ID,IP,TS,RH FROM openproxies WHERE IP='%lu.%lu.%lu.%lu' ORDER BY TS DESC LIMIT 10",a,b,c,d);
254
7d228b1d
D
255}
256
257/*
258 * proxyscanshowkill
259 * Check db for open proxies matching the given kill/gline ID, send to user usernick.
260 */
261
ee8cd7d0 262void proxyscanshowkill_real(DBConn *dbconn, void *arg) {
c54295ef 263 nick *np=getnickbynumeric((unsigned long)arg);
ee8cd7d0 264 DBResult *pgres;
7d228b1d 265
ee8cd7d0
CP
266 pgres=dbgetresult(dbconn);
267 if (!dbquerysuccessful(pgres)) {
96464e2c 268 Error("proxyscan", ERR_ERROR, "Error loading data.");
7d228b1d
D
269 return;
270 }
271
ee8cd7d0 272 if (dbnumfields(pgres) != 4) {
96464e2c 273 Error("proxyscan", ERR_ERROR, "data format error.");
ee8cd7d0
CP
274 dbclear(pgres);
275 return;
96464e2c
P
276 }
277
96464e2c 278 if (!np) {
ee8cd7d0 279 dbclear(pgres);
7d228b1d
D
280 return;
281 }
282
96464e2c 283 sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
ee8cd7d0
CP
284 while(dbfetchrow(pgres)) {
285 sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",dbgetvalue(pgres, 0),
286 dbgetvalue(pgres, 1),
287 dbgetvalue(pgres, 2),
288 dbgetvalue(pgres, 3));
7d228b1d 289 }
ee8cd7d0 290 dbclear(pgres);
96464e2c
P
291 sendnoticetouser(proxyscannick,np,"--- End of list ---");
292}
293
96464e2c 294void proxyscanshowkill(nick *mynick, nick *usernick, unsigned long a) {
ee8cd7d0 295 dbasyncquery(proxyscanspewip_real,(void *)usernick->numeric,
96464e2c 296 "SELECT ID,IP,TS,RH FROM openproxies WHERE ID='%lu'",a);
7d228b1d 297}