]> jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscandb.c
PROXYSCAN: format time in spew output
[irc/quakenet/newserv.git] / proxyscan / proxyscandb.c
1
2 /*
3 * proxyscandb:
4 * Handles the database interface routines for proxyscan.
5 */
6
7 #include "proxyscan.h"
8
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"
17 #include <string.h>
18 #include <stdio.h>
19
20 unsigned int lastid;
21 int sqlconnected = 0;
22 extern nick *proxyscannick;
23
24 void proxyscan_get_last_id(DBConn *dbconn, void *arg);
25
26 /*
27 * proxyscandbinit():
28 * Connects to the database and recovers the last gline ID
29 */
30
31 int proxyscandbinit() {
32 if(!dbconnected())
33 return 1;
34
35 sqlconnected=1;
36
37 /* Set up the table */
38 dbcreatequery("CREATE TABLE openproxies ("
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,"
44 "PRIMARY KEY (ID))");
45
46 dbcreatequery("CREATE INDEX openproxies_id_index ON openproxies (ID)");
47
48 dbasyncquery(proxyscan_get_last_id, NULL,
49 "SELECT ID FROM openproxies ORDER BY id DESC LIMIT 1");
50
51 return 0;
52 }
53
54 void proxyscan_get_last_id(DBConn *dbconn, void *arg) {
55 DBResult *pgres = dbgetresult(dbconn);
56
57 if(!dbquerysuccessful(pgres)) {
58 Error("proxyscan", ERR_STOP, "Error loading last id.");
59 return;
60 }
61
62 if (dbfetchrow(pgres))
63 lastid = atoi(dbgetvalue(pgres, 0));
64 else
65 lastid = 0;
66
67 dbclear(pgres);
68 Error("proxyscan",ERR_INFO,"Retrieved lastid %d from database.",lastid);
69 }
70 /*
71 * scantostr:
72 * Given a scan number, returns the string associated.
73 */
74
75 const 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;
98
99 case STYPE_DIRECT:
100 reason="forward";
101 break;
102
103 case STYPE_DIRECT_IRC:
104 reason="fwdirc";
105 break;
106
107 case STYPE_ROUTER:
108 reason="router";
109 break;
110 }
111
112 return reason;
113 }
114
115 /*
116 * scantobm:
117 * Given a scan number, returns the bitmask associated (as used in the
118 * previous version of P)
119 */
120
121 int scantodm(int scannum) {
122 return (1<<scannum);
123 }
124
125 /*
126 * loggline:
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).
130 */
131
132 void loggline(cachehost *chp, patricia_node_t *node) {
133 char reasonlist[200];
134 char reasonesc[400 + 1]; /* reasonlist*2+1 */
135 int reasonmask=0;
136 int reasonpos=0;
137 foundproxy *fpp;
138
139 if (brokendb) {
140 chp->glineid=1;
141 return;
142 }
143
144 reasonlist[0]='\0';
145 reasonmask=0;
146 for (fpp=chp->proxies;fpp;fpp=fpp->next) {
147 if ((reasonpos + 20) > sizeof(reasonlist))
148 break;
149
150 reasonpos += sprintf(reasonlist+reasonpos, "%s:%d ",scantostr(fpp->type), fpp->port);
151 }
152
153 if (chp->glineid==0) {
154 chp->glineid=++lastid;
155
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);
159 } else {
160 dbescapestring(reasonesc,reasonlist,strlen(reasonlist));
161 dbquery("UPDATE openproxies SET PM=%d,RH='%s' where ID=%u",
162 reasonmask,reasonesc,chp->glineid);
163 }
164 }
165
166 /*
167 * proxyscandbclose:
168 * Closes the db socket when proxyscan is unloaded
169 */
170
171 void proxyscandbclose() {
172 }
173
174 /*
175 * proxyscandolistopen:
176 * Lists all the open proxies found since <since> to user usernick.
177 */
178
179 void proxyscandolistopen_real(DBConn *dbconn, void *arg) {
180 nick *np=getnickbynumeric((unsigned long)arg);
181 DBResult *pgres;
182
183 pgres=dbgetresult(dbconn);
184 if (!dbquerysuccessful(pgres)) {
185 Error("proxyscan", ERR_ERROR, "Error loading data.");
186 return;
187 }
188
189 if (dbnumfields(pgres) != 3) {
190 Error("proxyscan", ERR_ERROR, "data format error.");
191 dbclear(pgres);
192 return;
193 }
194
195 if (!np) {
196 dbclear(pgres);
197 return;
198 }
199
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));
205 }
206 dbclear(pgres);
207 sendnoticetouser(proxyscannick,np,"--- End of list ---");
208 }
209
210 int proxyscandolistopen(void *sender, int cargc, char **cargv) {
211 nick *usernick = (nick *)sender;
212
213 dbasyncquery(proxyscandolistopen_real,(void *)usernick->numeric,
214 "SELECT IP,TS,RH FROM openproxies WHERE TS>'%lu' ORDER BY TS",time(NULL)-rescaninterval);
215 return CMD_OK;
216 }
217
218 /*
219 * proxyscanspewip
220 * Check db for open proxies matching the given IP, send to user usernick.
221 */
222
223 void proxyscanspewip_real(DBConn *dbconn, void *arg) {
224 nick *np=getnickbynumeric((unsigned long)arg);
225 DBResult *pgres;
226 char timebuf[30];
227
228 pgres=dbgetresult(dbconn);
229 if (!dbquerysuccessful(pgres)) {
230 Error("proxyscan", ERR_ERROR, "Error loading data.");
231 return;
232 }
233
234 if (dbnumfields(pgres) != 4) {
235 Error("proxyscan", ERR_ERROR, "data format error.");
236 dbclear(pgres);
237 return;
238 }
239
240 if (!np) {
241 dbclear(pgres);
242 return;
243 }
244
245 sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
246 while(dbfetchrow(pgres)) {
247 time_t t = strtoul(dbgetvalue(pgres, 2), NULL, 10);
248 strftime(timebuf, sizeof(timebuf), "%d/%m/%y %H:%M GMT", gmtime(&t));
249
250 sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",dbgetvalue(pgres, 0),
251 dbgetvalue(pgres, 1),
252 timebuf,
253 dbgetvalue(pgres, 3));
254 }
255 dbclear(pgres);
256 sendnoticetouser(proxyscannick,np,"--- End of list ---");
257 }
258
259 void proxyscanspewip(nick *mynick, nick *usernick, unsigned long a, unsigned long b, unsigned long c, unsigned long d) {
260 dbasyncquery(proxyscanspewip_real,(void *)usernick->numeric,
261 "SELECT ID,IP,TS,RH FROM openproxies WHERE IP='%lu.%lu.%lu.%lu' ORDER BY TS DESC LIMIT 10",a,b,c,d);
262
263 }
264
265 /*
266 * proxyscanshowkill
267 * Check db for open proxies matching the given kill/gline ID, send to user usernick.
268 */
269
270 void proxyscanshowkill_real(DBConn *dbconn, void *arg) {
271 nick *np=getnickbynumeric((unsigned long)arg);
272 DBResult *pgres;
273
274 pgres=dbgetresult(dbconn);
275 if (!dbquerysuccessful(pgres)) {
276 Error("proxyscan", ERR_ERROR, "Error loading data.");
277 return;
278 }
279
280 if (dbnumfields(pgres) != 4) {
281 Error("proxyscan", ERR_ERROR, "data format error.");
282 dbclear(pgres);
283 return;
284 }
285
286 if (!np) {
287 dbclear(pgres);
288 return;
289 }
290
291 sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
292 while(dbfetchrow(pgres)) {
293 sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",dbgetvalue(pgres, 0),
294 dbgetvalue(pgres, 1),
295 dbgetvalue(pgres, 2),
296 dbgetvalue(pgres, 3));
297 }
298 dbclear(pgres);
299 sendnoticetouser(proxyscannick,np,"--- End of list ---");
300 }
301
302 void proxyscanshowkill(nick *mynick, nick *usernick, unsigned long a) {
303 dbasyncquery(proxyscanspewip_real,(void *)usernick->numeric,
304 "SELECT ID,IP,TS,RH FROM openproxies WHERE ID='%lu'",a);
305 }