]>
Commit | Line | Data |
---|---|---|
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 | 20 | unsigned int lastid; |
96464e2c P |
21 | int sqlconnected = 0; |
22 | extern nick *proxyscannick; | |
23 | ||
ee8cd7d0 | 24 | void 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 | ||
31 | int 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 |
54 | void 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 | ||
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; | |
905c2ba2 | 98 | |
99 | case STYPE_DIRECT: | |
100 | reason="forward"; | |
101 | break; | |
c86edd1d Q |
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 | ||
557c8cb2 | 124 | void loggline(cachehost *chp, patricia_node_t *node) { |
c86edd1d | 125 | char reasonlist[100]; |
96464e2c | 126 | char reasonesc[200 + 1]; /* reasonlist*2+1 */ |
c86edd1d Q |
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 | reasonpos += sprintf(reasonlist+reasonpos, "%s:%d ",scantostr(fpp->type), fpp->port); | |
140 | } | |
141 | ||
142 | if (chp->glineid==0) { | |
143 | chp->glineid=++lastid; | |
144 | ||
ee8cd7d0 CP |
145 | dbescapestring(reasonesc,reasonlist,strlen(reasonlist)); |
146 | dbquery("INSERT INTO openproxies VALUES(%u,'%s',%d,%ld,'%s')",chp->glineid, | |
557c8cb2 | 147 | IPtostr(((patricia_node_t *)node)->prefix->sin),reasonmask,getnettime(),reasonesc); |
c86edd1d | 148 | } else { |
ee8cd7d0 CP |
149 | dbescapestring(reasonesc,reasonlist,strlen(reasonlist)); |
150 | dbquery("UPDATE openproxies SET PM=%d,RH='%s' where ID=%u", | |
c86edd1d | 151 | reasonmask,reasonesc,chp->glineid); |
c86edd1d Q |
152 | } |
153 | } | |
154 | ||
155 | /* | |
156 | * proxyscandbclose: | |
157 | * Closes the db socket when proxyscan is unloaded | |
158 | */ | |
159 | ||
160 | void proxyscandbclose() { | |
c86edd1d Q |
161 | } |
162 | ||
163 | /* | |
164 | * proxyscandolistopen: | |
165 | * Lists all the open proxies found since <since> to user usernick. | |
166 | */ | |
167 | ||
ee8cd7d0 | 168 | void proxyscandolistopen_real(DBConn *dbconn, void *arg) { |
c54295ef | 169 | nick *np=getnickbynumeric((unsigned long)arg); |
ee8cd7d0 | 170 | DBResult *pgres; |
c86edd1d | 171 | |
ee8cd7d0 CP |
172 | pgres=dbgetresult(dbconn); |
173 | if (!dbquerysuccessful(pgres)) { | |
96464e2c | 174 | Error("proxyscan", ERR_ERROR, "Error loading data."); |
c86edd1d Q |
175 | return; |
176 | } | |
96464e2c | 177 | |
ee8cd7d0 | 178 | if (dbnumfields(pgres) != 3) { |
96464e2c | 179 | Error("proxyscan", ERR_ERROR, "data format error."); |
ee8cd7d0 CP |
180 | dbclear(pgres); |
181 | return; | |
96464e2c | 182 | } |
c86edd1d | 183 | |
96464e2c | 184 | if (!np) { |
ee8cd7d0 | 185 | dbclear(pgres); |
c86edd1d Q |
186 | return; |
187 | } | |
188 | ||
96464e2c | 189 | sendnoticetouser(proxyscannick,np,"%-20s %-22s %s","IP","Found at","What was open"); |
ee8cd7d0 CP |
190 | while(dbfetchrow(pgres)) { |
191 | sendnoticetouser(proxyscannick,np, "%-20s %-22s %s",dbgetvalue(pgres, 0), | |
192 | dbgetvalue(pgres, 1), | |
193 | dbgetvalue(pgres, 2)); | |
c86edd1d | 194 | } |
ee8cd7d0 | 195 | dbclear(pgres); |
96464e2c P |
196 | sendnoticetouser(proxyscannick,np,"--- End of list ---"); |
197 | } | |
198 | ||
199 | void proxyscandolistopen(nick *mynick, nick *usernick, time_t snce) { | |
200 | ||
ee8cd7d0 | 201 | dbasyncquery(proxyscandolistopen_real,(void *)usernick->numeric, |
96464e2c | 202 | "SELECT IP,TS,RH FROM openproxies WHERE TS>'%lu' ORDER BY TS",snce); |
c86edd1d | 203 | } |
7d228b1d D |
204 | |
205 | /* | |
206 | * proxyscanspewip | |
207 | * Check db for open proxies matching the given IP, send to user usernick. | |
208 | */ | |
209 | ||
ee8cd7d0 | 210 | void proxyscanspewip_real(DBConn *dbconn, void *arg) { |
c54295ef | 211 | nick *np=getnickbynumeric((unsigned long)arg); |
ee8cd7d0 | 212 | DBResult *pgres; |
7d228b1d | 213 | |
ee8cd7d0 CP |
214 | pgres=dbgetresult(dbconn); |
215 | if (!dbquerysuccessful(pgres)) { | |
96464e2c | 216 | Error("proxyscan", ERR_ERROR, "Error loading data."); |
7d228b1d D |
217 | return; |
218 | } | |
219 | ||
ee8cd7d0 | 220 | if (dbnumfields(pgres) != 4) { |
96464e2c | 221 | Error("proxyscan", ERR_ERROR, "data format error."); |
ee8cd7d0 CP |
222 | dbclear(pgres); |
223 | return; | |
96464e2c P |
224 | } |
225 | ||
96464e2c | 226 | if (!np) { |
ee8cd7d0 | 227 | dbclear(pgres); |
7d228b1d D |
228 | return; |
229 | } | |
230 | ||
96464e2c | 231 | sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open"); |
ee8cd7d0 CP |
232 | while(dbfetchrow(pgres)) { |
233 | sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",dbgetvalue(pgres, 0), | |
234 | dbgetvalue(pgres, 1), | |
235 | dbgetvalue(pgres, 2), | |
236 | dbgetvalue(pgres, 3)); | |
7d228b1d | 237 | } |
ee8cd7d0 | 238 | dbclear(pgres); |
96464e2c P |
239 | sendnoticetouser(proxyscannick,np,"--- End of list ---"); |
240 | } | |
241 | ||
242 | void proxyscanspewip(nick *mynick, nick *usernick, unsigned long a, unsigned long b, unsigned long c, unsigned long d) { | |
ee8cd7d0 | 243 | dbasyncquery(proxyscanspewip_real,(void *)usernick->numeric, |
96464e2c P |
244 | "SELECT ID,IP,TS,RH FROM openproxies WHERE IP='%lu.%lu.%lu.%lu' ORDER BY TS DESC LIMIT 10",a,b,c,d); |
245 | ||
7d228b1d D |
246 | } |
247 | ||
248 | /* | |
249 | * proxyscanshowkill | |
250 | * Check db for open proxies matching the given kill/gline ID, send to user usernick. | |
251 | */ | |
252 | ||
ee8cd7d0 | 253 | void proxyscanshowkill_real(DBConn *dbconn, void *arg) { |
c54295ef | 254 | nick *np=getnickbynumeric((unsigned long)arg); |
ee8cd7d0 | 255 | DBResult *pgres; |
7d228b1d | 256 | |
ee8cd7d0 CP |
257 | pgres=dbgetresult(dbconn); |
258 | if (!dbquerysuccessful(pgres)) { | |
96464e2c | 259 | Error("proxyscan", ERR_ERROR, "Error loading data."); |
7d228b1d D |
260 | return; |
261 | } | |
262 | ||
ee8cd7d0 | 263 | if (dbnumfields(pgres) != 4) { |
96464e2c | 264 | Error("proxyscan", ERR_ERROR, "data format error."); |
ee8cd7d0 CP |
265 | dbclear(pgres); |
266 | return; | |
96464e2c P |
267 | } |
268 | ||
96464e2c | 269 | if (!np) { |
ee8cd7d0 | 270 | dbclear(pgres); |
7d228b1d D |
271 | return; |
272 | } | |
273 | ||
96464e2c | 274 | sendnoticetouser(proxyscannick,np,"%-5s %-20s %-22s %s","ID","IP","Found at","What was open"); |
ee8cd7d0 CP |
275 | while(dbfetchrow(pgres)) { |
276 | sendnoticetouser(proxyscannick,np, "%-5s %-20s %-22s %s",dbgetvalue(pgres, 0), | |
277 | dbgetvalue(pgres, 1), | |
278 | dbgetvalue(pgres, 2), | |
279 | dbgetvalue(pgres, 3)); | |
7d228b1d | 280 | } |
ee8cd7d0 | 281 | dbclear(pgres); |
96464e2c P |
282 | sendnoticetouser(proxyscannick,np,"--- End of list ---"); |
283 | } | |
284 | ||
285 | ||
286 | void proxyscanshowkill(nick *mynick, nick *usernick, unsigned long a) { | |
ee8cd7d0 | 287 | dbasyncquery(proxyscanspewip_real,(void *)usernick->numeric, |
96464e2c | 288 | "SELECT ID,IP,TS,RH FROM openproxies WHERE ID='%lu'",a); |
7d228b1d | 289 | } |