]> jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscandb.c
Fix buffer overflow
[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
104 return reason;
105 }
106
107 /*
108 * scantobm:
109 * Given a scan number, returns the bitmask associated (as used in the
110 * previous version of P)
111 */
112
113 int scantodm(int scannum) {
114 return (1<<scannum);
115 }
116
117 /*
118 * loggline:
119 * This function takes the given scanhost (which is assumed to have
120 * at least one "OPEN" scan) and logs it to the database. It returns
121 * the unique ID assigned to this gline (for the gline message itself).
122 */
123
124 void loggline(cachehost *chp, patricia_node_t *node) {
125 char reasonlist[200];
126 char reasonesc[400 + 1]; /* reasonlist*2+1 */
127 int reasonmask=0;
128 int reasonpos=0;
129 foundproxy *fpp;
130
131 if (brokendb) {
132 chp->glineid=1;
133 return;
134 }
135
136 reasonlist[0]='\0';
137 reasonmask=0;
138 for (fpp=chp->proxies;fpp;fpp=fpp->next) {
139 if ((reasonpos + 20) > sizeof(reasonlist))
140 break;
141
142 reasonpos += sprintf(reasonlist+reasonpos, "%s:%d ",scantostr(fpp->type), fpp->port);
143 }
144
145 if (chp->glineid==0) {
146 chp->glineid=++lastid;
147
148 dbescapestring(reasonesc,reasonlist,strlen(reasonlist));
149 dbquery("INSERT INTO openproxies VALUES(%u,'%s',%d,%ld,'%s')",chp->glineid,
150 IPtostr(((patricia_node_t *)node)->prefix->sin),reasonmask,getnettime(),reasonesc);
151 } else {
152 dbescapestring(reasonesc,reasonlist,strlen(reasonlist));
153 dbquery("UPDATE openproxies SET PM=%d,RH='%s' where ID=%u",
154 reasonmask,reasonesc,chp->glineid);
155 }
156 }
157
158 /*
159 * proxyscandbclose:
160 * Closes the db socket when proxyscan is unloaded
161 */
162
163 void proxyscandbclose() {
164 }
165
166 /*
167 * proxyscandolistopen:
168 * Lists all the open proxies found since <since> to user usernick.
169 */
170
171 void proxyscandolistopen_real(DBConn *dbconn, void *arg) {
172 nick *np=getnickbynumeric((unsigned long)arg);
173 DBResult *pgres;
174
175 pgres=dbgetresult(dbconn);
176 if (!dbquerysuccessful(pgres)) {
177 Error("proxyscan", ERR_ERROR, "Error loading data.");
178 return;
179 }
180
181 if (dbnumfields(pgres) != 3) {
182 Error("proxyscan", ERR_ERROR, "data format error.");
183 dbclear(pgres);
184 return;
185 }
186
187 if (!np) {
188 dbclear(pgres);
189 return;
190 }
191
192 sendnoticetouser(proxyscannick,np,"%-20s %-22s %s","IP","Found at","What was open");
193 while(dbfetchrow(pgres)) {
194 sendnoticetouser(proxyscannick,np, "%-20s %-22s %s",dbgetvalue(pgres, 0),
195 dbgetvalue(pgres, 1),
196 dbgetvalue(pgres, 2));
197 }
198 dbclear(pgres);
199 sendnoticetouser(proxyscannick,np,"--- End of list ---");
200 }
201
202 void proxyscandolistopen(nick *mynick, nick *usernick, time_t snce) {
203
204 dbasyncquery(proxyscandolistopen_real,(void *)usernick->numeric,
205 "SELECT IP,TS,RH FROM openproxies WHERE TS>'%lu' ORDER BY TS",snce);
206 }
207
208 /*
209 * proxyscanspewip
210 * Check db for open proxies matching the given IP, send to user usernick.
211 */
212
213 void proxyscanspewip_real(DBConn *dbconn, void *arg) {
214 nick *np=getnickbynumeric((unsigned long)arg);
215 DBResult *pgres;
216
217 pgres=dbgetresult(dbconn);
218 if (!dbquerysuccessful(pgres)) {
219 Error("proxyscan", ERR_ERROR, "Error loading data.");
220 return;
221 }
222
223 if (dbnumfields(pgres) != 4) {
224 Error("proxyscan", ERR_ERROR, "data format error.");
225 dbclear(pgres);
226 return;
227 }
228
229 if (!np) {
230 dbclear(pgres);
231 return;
232 }
233
234 sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
235 while(dbfetchrow(pgres)) {
236 sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",dbgetvalue(pgres, 0),
237 dbgetvalue(pgres, 1),
238 dbgetvalue(pgres, 2),
239 dbgetvalue(pgres, 3));
240 }
241 dbclear(pgres);
242 sendnoticetouser(proxyscannick,np,"--- End of list ---");
243 }
244
245 void proxyscanspewip(nick *mynick, nick *usernick, unsigned long a, unsigned long b, unsigned long c, unsigned long d) {
246 dbasyncquery(proxyscanspewip_real,(void *)usernick->numeric,
247 "SELECT ID,IP,TS,RH FROM openproxies WHERE IP='%lu.%lu.%lu.%lu' ORDER BY TS DESC LIMIT 10",a,b,c,d);
248
249 }
250
251 /*
252 * proxyscanshowkill
253 * Check db for open proxies matching the given kill/gline ID, send to user usernick.
254 */
255
256 void proxyscanshowkill_real(DBConn *dbconn, void *arg) {
257 nick *np=getnickbynumeric((unsigned long)arg);
258 DBResult *pgres;
259
260 pgres=dbgetresult(dbconn);
261 if (!dbquerysuccessful(pgres)) {
262 Error("proxyscan", ERR_ERROR, "Error loading data.");
263 return;
264 }
265
266 if (dbnumfields(pgres) != 4) {
267 Error("proxyscan", ERR_ERROR, "data format error.");
268 dbclear(pgres);
269 return;
270 }
271
272 if (!np) {
273 dbclear(pgres);
274 return;
275 }
276
277 sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open");
278 while(dbfetchrow(pgres)) {
279 sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",dbgetvalue(pgres, 0),
280 dbgetvalue(pgres, 1),
281 dbgetvalue(pgres, 2),
282 dbgetvalue(pgres, 3));
283 }
284 dbclear(pgres);
285 sendnoticetouser(proxyscannick,np,"--- End of list ---");
286 }
287
288
289 void proxyscanshowkill(nick *mynick, nick *usernick, unsigned long a) {
290 dbasyncquery(proxyscanspewip_real,(void *)usernick->numeric,
291 "SELECT ID,IP,TS,RH FROM openproxies WHERE ID='%lu'",a);
292 }