]> jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscandb.c
Rearchitecture makefiles, stage 1.
[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 "../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"
17 #include <string.h>
18 #include <stdio.h>
19 #include <libpq-fe.h>
20
21 unsigned int lastid;
22 int sqlconnected = 0;
23 extern nick *proxyscannick;
24
25 void proxyscan_get_last_id(PGconn *dbconn, void *arg);
26
27 /*
28 * proxyscandbinit():
29 * Connects to the database and recovers the last gline ID
30 */
31
32 int proxyscandbinit() {
33 if(!pqconnected())
34 return 1;
35
36 sqlconnected=1;
37
38 /* Set up the table */
39 pqcreatequery("CREATE TABLE openproxies ("
40 "ID int8 not null,"
41 "IP inet not null,"
42 "PM int4 not null,"
43 "TS int4 not null,"
44 "RH varchar not null,"
45 "PRIMARY KEY (ID))");
46
47 pqcreatequery("CREATE INDEX openproxies_id_index ON openproxies (ID)");
48
49 pqasyncquery(proxyscan_get_last_id, NULL,
50 "SELECT ID FROM openproxies ORDER BY id DESC LIMIT 1");
51
52 return 0;
53 }
54
55 void proxyscan_get_last_id(PGconn *dbconn, void *arg) {
56 PGresult *pgres = PQgetResult(dbconn);
57 unsigned int numrows;
58
59 if(PQresultStatus(pgres) != PGRES_TUPLES_OK) {
60 Error("proxyscan", ERR_ERROR, "Error loading last id.");
61 }
62
63 numrows = PQntuples(pgres);
64 if ( numrows )
65 lastid = atoi(PQgetvalue(pgres, 0, 0));
66 else
67 lastid = 0;
68
69 PQclear(pgres);
70 Error("proxyscan",ERR_INFO,"Retrieved lastid %d from database.",lastid);
71 }
72 /*
73 * scantostr:
74 * Given a scan number, returns the string associated.
75 */
76
77 const char *scantostr(int type) {
78 char *reason="UNKNOWN";
79
80 switch (type) {
81 case STYPE_SOCKS4:
82 reason="socks4";
83 break;
84
85 case STYPE_SOCKS5:
86 reason="socks5";
87 break;
88
89 case STYPE_HTTP:
90 reason="http";
91 break;
92
93 case STYPE_WINGATE:
94 reason="wingate";
95 break;
96
97 case STYPE_CISCO:
98 reason="router";
99 break;
100
101 case STYPE_DIRECT:
102 reason="forward";
103 break;
104 }
105
106 return reason;
107 }
108
109 /*
110 * scantobm:
111 * Given a scan number, returns the bitmask associated (as used in the
112 * previous version of P)
113 */
114
115 int scantodm(int scannum) {
116 return (1<<scannum);
117 }
118
119 /*
120 * loggline:
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).
124 */
125
126 void loggline(cachehost *chp, patricia_node_t *node) {
127 char reasonlist[100];
128 char reasonesc[200 + 1]; /* reasonlist*2+1 */
129 int reasonmask=0;
130 int reasonpos=0;
131 foundproxy *fpp;
132
133 if (brokendb) {
134 chp->glineid=1;
135 return;
136 }
137
138 reasonlist[0]='\0';
139 reasonmask=0;
140 for (fpp=chp->proxies;fpp;fpp=fpp->next) {
141 reasonpos += sprintf(reasonlist+reasonpos, "%s:%d ",scantostr(fpp->type), fpp->port);
142 }
143
144 if (chp->glineid==0) {
145 chp->glineid=++lastid;
146
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);
150 } else {
151 PQescapeString(reasonesc,reasonlist,strlen(reasonlist));
152 pqquery("UPDATE openproxies SET PM=%d,RH='%s' where ID=%u",
153 reasonmask,reasonesc,chp->glineid);
154 }
155 }
156
157 /*
158 * proxyscandbclose:
159 * Closes the db socket when proxyscan is unloaded
160 */
161
162 void proxyscandbclose() {
163 }
164
165 /*
166 * proxyscandolistopen:
167 * Lists all the open proxies found since <since> to user usernick.
168 */
169
170 void proxyscandolistopen_real(PGconn *dbconn, void *arg) {
171 nick *np=getnickbynumeric((unsigned long)arg);
172 PGresult *pgres;
173 int i, num;
174
175 pgres=PQgetResult(dbconn);
176 if (PQresultStatus(pgres) != PGRES_TUPLES_OK) {
177 Error("proxyscan", ERR_ERROR, "Error loading data.");
178 return;
179 }
180
181 if (PQnfields(pgres) != 3) {
182 Error("proxyscan", ERR_ERROR, "data format error.");
183 }
184
185 num=PQntuples(pgres);
186
187 if (!np) {
188 PQclear(pgres);
189 return;
190 }
191
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));
197 }
198 sendnoticetouser(proxyscannick,np,"--- End of list ---");
199 }
200
201 void proxyscandolistopen(nick *mynick, nick *usernick, time_t snce) {
202
203 pqasyncquery(proxyscandolistopen_real,(void *)usernick->numeric,
204 "SELECT IP,TS,RH FROM openproxies WHERE TS>'%lu' ORDER BY TS",snce);
205 }
206
207 /*
208 * proxyscanspewip
209 * Check db for open proxies matching the given IP, send to user usernick.
210 */
211
212 void proxyscanspewip_real(PGconn *dbconn, void *arg) {
213 nick *np=getnickbynumeric((unsigned long)arg);
214 PGresult *pgres;
215 int i, num;
216
217 pgres=PQgetResult(dbconn);
218 if (PQresultStatus(pgres) != PGRES_TUPLES_OK) {
219 Error("proxyscan", ERR_ERROR, "Error loading data.");
220 return;
221 }
222
223 if (PQnfields(pgres) != 4) {
224 Error("proxyscan", ERR_ERROR, "data format error.");
225 }
226
227 num=PQntuples(pgres);
228
229 if (!np) {
230 PQclear(pgres);
231 return;
232 }
233
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));
240 }
241 sendnoticetouser(proxyscannick,np,"--- End of list ---");
242 }
243
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);
247
248 }
249
250 /*
251 * proxyscanshowkill
252 * Check db for open proxies matching the given kill/gline ID, send to user usernick.
253 */
254
255 void proxyscanshowkill_real(PGconn *dbconn, void *arg) {
256 nick *np=getnickbynumeric((unsigned long)arg);
257 PGresult *pgres;
258 int i, num;
259
260 pgres=PQgetResult(dbconn);
261 if (PQresultStatus(pgres) != PGRES_TUPLES_OK) {
262 Error("proxyscan", ERR_ERROR, "Error loading data.");
263 return;
264 }
265
266 if (PQnfields(pgres) != 4) {
267 Error("proxyscan", ERR_ERROR, "data format error.");
268 }
269
270 num=PQntuples(pgres);
271
272 if (!np) {
273 PQclear(pgres);
274 return;
275 }
276
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));
283 }
284 sendnoticetouser(proxyscannick,np,"--- End of list ---");
285 }
286
287
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);
291 }