]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | |
2 | #include "proxyscan.h" | |
3 | ||
4 | #include <sys/poll.h> | |
5 | #include <sys/types.h> | |
6 | #include <sys/socket.h> | |
7 | #include <netdb.h> | |
8 | #include "../core/error.h" | |
9 | #include "../core/events.h" | |
10 | #include <stdlib.h> | |
11 | #include <stdio.h> | |
12 | #include <errno.h> | |
13 | #include "../nick/nick.h" | |
14 | #include "../core/hooks.h" | |
15 | #include "../lib/sstring.h" | |
16 | #include "../irc/irc_config.h" | |
17 | #include "../localuser/localuser.h" | |
18 | #include "../core/config.h" | |
19 | #include <unistd.h> | |
20 | #include "../core/schedule.h" | |
21 | #include <string.h> | |
22 | #include "../irc/irc.h" | |
23 | #include "../lib/irc_string.h" | |
24 | ||
25 | #define SCANTIMEOUT 60 | |
26 | ||
27 | #define SCANHOSTHASHSIZE 1000 | |
28 | #define SCANHASHSIZE 400 | |
29 | ||
30 | /* It's unlikely you'll get 100k of preamble before a connect... */ | |
31 | #define READ_SANITY_LIMIT 102400 | |
32 | ||
33 | scan *scantable[SCANHASHSIZE]; | |
34 | ||
35 | int listenfd; | |
36 | int activescans; | |
37 | int maxscans; | |
38 | int queuedhosts; | |
39 | int scansdone; | |
40 | int rescaninterval; | |
41 | int warningsent; | |
42 | int glinedhosts; | |
43 | time_t starttime; | |
44 | ||
45 | int numscans; /* number of scan types currently valid */ | |
46 | scantype thescans[PSCAN_MAXSCANS]; | |
47 | ||
48 | unsigned int hitsbyclass[10]; | |
49 | unsigned int scansbyclass[10]; | |
50 | ||
51 | unsigned int myip; | |
52 | sstring *myipstr; | |
53 | unsigned short listenport; | |
54 | int brokendb; | |
55 | ||
56 | unsigned int ps_mailip; | |
57 | unsigned int ps_mailport; | |
58 | sstring *ps_mailname; | |
59 | ||
60 | nick *proxyscannick; | |
61 | ||
62 | FILE *ps_logfile; | |
63 | ||
64 | /* Local functions */ | |
65 | void handlescansock(int fd, short events); | |
66 | void timeoutscansock(void *arg); | |
67 | void proxyscan_newnick(int hooknum, void *arg); | |
68 | void proxyscan_lostnick(int hooknum, void *arg); | |
69 | void proxyscanuserhandler(nick *target, int message, void **params); | |
70 | void registerproxyscannick(); | |
71 | void killsock(scan *sp, int outcome); | |
72 | void killallscans(); | |
73 | void proxyscanstats(int hooknum, void *arg); | |
74 | void sendlagwarning(); | |
75 | void proxyscandostatus(nick *np); | |
76 | void proxyscandebug(nick *np); | |
77 | void proxyscan_newip(nick *np, unsigned long ip); | |
78 | int proxyscan_addscantype(int type, int port); | |
79 | int proxyscan_delscantype(int type, int port); | |
80 | ||
81 | int proxyscan_addscantype(int type, int port) { | |
82 | /* Check we have a spare scan slot */ | |
83 | ||
84 | if (numscans>=PSCAN_MAXSCANS) | |
85 | return 1; | |
86 | ||
87 | thescans[numscans].type=type; | |
88 | thescans[numscans].port=port; | |
89 | thescans[numscans].hits=0; | |
90 | ||
91 | numscans++; | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
96 | int proxyscan_delscantype(int type, int port) { | |
97 | int i; | |
98 | ||
99 | for (i=0;i<numscans;i++) | |
100 | if (thescans[i].type==type && thescans[i].port==port) | |
101 | break; | |
102 | ||
103 | if (i>=numscans) | |
104 | return 1; | |
105 | ||
106 | memmove(thescans+i, thescans+(i+1), (PSCAN_MAXSCANS-(i+1)) * sizeof(scantype)); | |
107 | numscans--; | |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
112 | void _init(void) { | |
113 | sstring *cfgstr; | |
114 | int ipbits[4]; | |
115 | ||
116 | memset(scantable,0,sizeof(scantable)); | |
117 | maxscans=200; | |
118 | activescans=0; | |
119 | queuedhosts=0; | |
120 | scansdone=0; | |
121 | warningsent=0; | |
122 | starttime=time(NULL); | |
123 | glinedhosts=0; | |
124 | ||
125 | /* Listen port */ | |
126 | cfgstr=getcopyconfigitem("proxyscan","port","9999",6); | |
127 | listenport=strtol(cfgstr->content,NULL,10); | |
128 | freesstring(cfgstr); | |
129 | ||
130 | /* Max concurrent scans */ | |
131 | cfgstr=getcopyconfigitem("proxyscan","maxscans","200",5); | |
132 | maxscans=strtol(cfgstr->content,NULL,10); | |
133 | freesstring(cfgstr); | |
134 | ||
135 | /* Clean host timeout */ | |
136 | cfgstr=getcopyconfigitem("proxyscan","rescaninterval","3600",7); | |
137 | rescaninterval=strtol(cfgstr->content,NULL,10); | |
138 | cachehostinit(rescaninterval); | |
139 | freesstring(cfgstr); | |
140 | ||
141 | /* this default will NOT work well */ | |
142 | myipstr=getcopyconfigitem("proxyscan","ip","127.0.0.1",16); | |
143 | ||
144 | sscanf(myipstr->content,"%d.%d.%d.%d",&ipbits[0],&ipbits[1],&ipbits[2],&ipbits[3]); | |
145 | ||
146 | myip=((ipbits[0]&0xFF)<<24)+((ipbits[1]&0xFF)<<16)+ | |
147 | ((ipbits[2]&0xFF)<<8)+(ipbits[3]&0xFF); | |
148 | ||
149 | /* Mailer host */ | |
150 | cfgstr=getcopyconfigitem("proxyscan","mailerip","",16); | |
151 | ||
152 | #if defined(PROXYSCAN_MAIL) | |
153 | psm_mailerfd=-1; | |
154 | if (cfgstr) { | |
155 | sscanf(cfgstr->content,"%d.%d.%d.%d",&ipbits[0],&ipbits[1],&ipbits[2],&ipbits[3]); | |
156 | ps_mailip = ((ipbits[0]&0xFF)<<24)+((ipbits[1]&0xFF)<<16)+ | |
157 | ((ipbits[2]&0xFF)<<8)+(ipbits[3]&0xFF); | |
158 | ps_mailport=25; | |
159 | freesstring(cfgstr); | |
160 | ||
161 | ps_mailname=getcopyconfigitem("proxyscan","mailname","some.mail.server",HOSTLEN); | |
162 | Error("proxyscan",ERR_INFO,"Proxyscan mailer enabled; mailing to %s as %s.",IPtostr(ps_mailip),ps_mailname->content); | |
163 | } else { | |
164 | ps_mailport=0; | |
165 | ps_mailname=NULL; | |
166 | } | |
167 | #endif | |
168 | ||
169 | proxyscannick=NULL; | |
170 | /* Set up our nick on the network */ | |
171 | scheduleoneshot(time(NULL),®isterproxyscannick,NULL); | |
172 | ||
173 | registerhook(HOOK_NICK_NEWNICK,&proxyscan_newnick); | |
174 | ||
175 | registerhook(HOOK_CORE_STATSREQUEST,&proxyscanstats); | |
176 | ||
177 | /* Read in the clean hosts */ | |
178 | loadcachehosts(); | |
179 | ||
180 | /* Set up the database */ | |
181 | if ((proxyscandbinit())!=0) { | |
182 | brokendb=1; | |
183 | } else { | |
184 | brokendb=0; | |
185 | } | |
186 | ||
187 | /* Default scan types */ | |
188 | proxyscan_addscantype(STYPE_HTTP, 8080); | |
189 | proxyscan_addscantype(STYPE_HTTP, 80); | |
190 | proxyscan_addscantype(STYPE_HTTP, 6588); | |
191 | proxyscan_addscantype(STYPE_HTTP, 8000); | |
192 | proxyscan_addscantype(STYPE_HTTP, 3128); | |
193 | proxyscan_addscantype(STYPE_HTTP, 3802); | |
194 | proxyscan_addscantype(STYPE_HTTP, 5490); | |
195 | proxyscan_addscantype(STYPE_HTTP, 7441); | |
196 | proxyscan_addscantype(STYPE_HTTP, 808); | |
197 | proxyscan_addscantype(STYPE_HTTP, 3332); | |
198 | proxyscan_addscantype(STYPE_HTTP, 2282); | |
199 | proxyscan_addscantype(STYPE_SOCKS4, 1080); | |
200 | proxyscan_addscantype(STYPE_SOCKS5, 1080); | |
201 | proxyscan_addscantype(STYPE_SOCKS4, 1075); | |
202 | proxyscan_addscantype(STYPE_SOCKS5, 1075); | |
203 | proxyscan_addscantype(STYPE_SOCKS4, 2280); | |
204 | proxyscan_addscantype(STYPE_SOCKS5, 2280); | |
205 | proxyscan_addscantype(STYPE_SOCKS4, 1180); | |
206 | proxyscan_addscantype(STYPE_SOCKS5, 1180); | |
207 | proxyscan_addscantype(STYPE_WINGATE, 23); | |
208 | proxyscan_addscantype(STYPE_CISCO, 23); | |
209 | proxyscan_addscantype(STYPE_WINGATE, 1181); | |
210 | proxyscan_addscantype(STYPE_SOCKS5, 1978); | |
211 | proxyscan_addscantype(STYPE_SOCKS5, 1029); | |
212 | proxyscan_addscantype(STYPE_SOCKS5, 3801); | |
213 | proxyscan_addscantype(STYPE_SOCKS5, 3331); | |
214 | proxyscan_addscantype(STYPE_HTTP, 65506); | |
215 | proxyscan_addscantype(STYPE_HTTP, 63809); | |
216 | ||
217 | /* Schedule saves */ | |
218 | schedulerecurring(time(NULL)+3600,0,3600,&dumpcachehosts,NULL); | |
219 | ||
220 | ps_logfile=fopen("proxyscan.log","a"); | |
221 | } | |
222 | ||
223 | void registerproxyscannick(void *arg) { | |
224 | sstring *psnick,*psuser,*pshost,*psrealname; | |
225 | /* Set up our nick on the network */ | |
226 | ||
227 | psnick=getcopyconfigitem("proxyscan","nick","P",NICKLEN); | |
228 | psuser=getcopyconfigitem("proxyscan","user","proxyscan",USERLEN); | |
229 | pshost=getcopyconfigitem("proxyscan","host","some.host",HOSTLEN); | |
230 | psrealname=getcopyconfigitem("proxyscan","realname","Proxyscan",REALLEN); | |
231 | ||
232 | proxyscannick=registerlocaluser(psnick->content,psuser->content,pshost->content, | |
233 | psrealname->content, | |
234 | NULL,UMODE_OPER|UMODE_SERVICE|UMODE_DEAF, | |
235 | &proxyscanuserhandler); | |
236 | ||
237 | freesstring(psnick); | |
238 | freesstring(psuser); | |
239 | freesstring(pshost); | |
240 | freesstring(psrealname); | |
241 | } | |
242 | ||
243 | void _fini(void) { | |
244 | ||
245 | deregisterlocaluser(proxyscannick,NULL); | |
246 | ||
247 | deregisterhook(HOOK_NICK_NEWNICK,&proxyscan_newnick); | |
248 | ||
249 | deregisterhook(HOOK_CORE_STATSREQUEST,&proxyscanstats); | |
250 | ||
251 | deleteschedule(NULL,&dumpcachehosts,NULL); | |
252 | ||
253 | /* Kill any scans in progress */ | |
254 | killallscans(); | |
255 | ||
256 | /* Dump the database - AFTER killallscans() which prunes it */ | |
257 | dumpcachehosts(NULL); | |
258 | ||
259 | /* free() all our structures */ | |
260 | sfreeall(); | |
261 | ||
262 | freesstring(ps_mailname); | |
263 | #if defined(PROXYSCAN_MAIL) | |
264 | if (psm_mailerfd!=-1) | |
265 | deregisterhandler(psm_mailerfd,1); | |
266 | #endif | |
267 | ||
268 | if (ps_logfile) | |
269 | fclose(ps_logfile); | |
270 | } | |
271 | ||
272 | void proxyscanuserhandler(nick *target, int message, void **params) { | |
273 | nick *sender; | |
274 | char *msg; | |
275 | int i; | |
276 | ||
277 | switch(message) { | |
278 | case LU_KILLED: | |
279 | scheduleoneshot(time(NULL)+1,®isterproxyscannick,NULL); | |
280 | proxyscannick=NULL; | |
281 | break; | |
282 | ||
283 | case LU_PRIVMSG: | |
284 | case LU_SECUREMSG: | |
285 | sender=(nick *)params[0]; | |
286 | msg=(char *)params[1]; | |
287 | ||
288 | if (IsOper(sender)) { | |
289 | if (!ircd_strncmp(msg,"listopen",8)) { | |
290 | proxyscandolistopen(proxyscannick,sender,time(NULL)-rescaninterval); | |
291 | } | |
292 | ||
293 | if (!ircd_strncmp(msg,"status",6)) { | |
294 | proxyscandostatus(sender); | |
295 | } | |
296 | ||
297 | if (!ircd_strncmp(msg,"save",4)) { | |
298 | dumpcachehosts(NULL); | |
299 | sendnoticetouser(proxyscannick,sender,"Done."); | |
300 | } | |
301 | ||
302 | if (!ircd_strncmp(msg,"debug",5)) { | |
303 | proxyscandebug(sender); | |
304 | } | |
305 | ||
306 | if (!ircd_strncmp(msg,"scan ",5)) { | |
307 | unsigned long a,b,c,d; | |
308 | if (4 != sscanf(&msg[5],"%lu.%lu.%lu.%lu",&a,&b,&c,&d)) { | |
309 | sendnoticetouser(proxyscannick,sender,"Usage: scan a.b.c.d"); | |
310 | } else { | |
311 | sendnoticetouser(proxyscannick,sender,"Forcing scan of %lu.%lu.%lu.%lu",a,b,c,d); | |
312 | /* Just queue the scans directly here.. plonk them on the priority queue */ | |
313 | for(i=0;i<numscans;i++) { | |
314 | queuescan((a<<24)+(b<<16)+(c<<8)+d,thescans[i].type,thescans[i].port,SCLASS_NORMAL,time(NULL)); | |
315 | } | |
316 | } | |
317 | } | |
318 | ||
319 | if (!ircd_strncmp(msg,"addscan ",8)) { | |
320 | unsigned int a,b; | |
321 | if (sscanf(msg+8,"%u %u",&a,&b) != 2) { | |
322 | sendnoticetouser(proxyscannick,sender,"Usage: addscan <type> <port>"); | |
323 | } else { | |
324 | sendnoticetouser(proxyscannick,sender,"Added scan type %u port %u",a,b); | |
325 | proxyscan_addscantype(a,b); | |
326 | scanall(a,b); | |
327 | } | |
328 | } | |
329 | ||
330 | if (!ircd_strncmp(msg,"delscan ",8)) { | |
331 | unsigned int a,b; | |
332 | if (sscanf(msg+8,"%u %u",&a,&b) != 2) { | |
333 | sendnoticetouser(proxyscannick,sender,"Usage: delscan <type> <port>"); | |
334 | } else { | |
335 | sendnoticetouser(proxyscannick,sender,"Delete scan type %u port %u",a,b); | |
336 | proxyscan_delscantype(a,b); | |
337 | } | |
338 | } | |
339 | ||
340 | if (!ircd_strncmp(msg,"help",4)) { | |
341 | sendnoticetouser(proxyscannick,sender,"Proxyscan commands:"); | |
342 | sendnoticetouser(proxyscannick,sender,"-------------------------------------------"); | |
343 | sendnoticetouser(proxyscannick,sender,"help Shows this help"); | |
344 | sendnoticetouser(proxyscannick,sender,"status Prints status information"); | |
345 | sendnoticetouser(proxyscannick,sender,"listopen Shows open proxies found recently"); | |
346 | sendnoticetouser(proxyscannick,sender,"save Saves the clean host database"); | |
347 | } | |
348 | } | |
349 | ||
350 | default: | |
351 | break; | |
352 | } | |
353 | } | |
354 | ||
355 | void addscantohash(scan *sp) { | |
356 | int hash; | |
357 | hash=(sp->fd)%SCANHASHSIZE; | |
358 | ||
359 | sp->next=scantable[hash]; | |
360 | scantable[hash]=sp; | |
361 | ||
362 | activescans++; | |
363 | } | |
364 | ||
365 | void delscanfromhash(scan *sp) { | |
366 | int hash; | |
367 | scan **sh; | |
368 | ||
369 | hash=(sp->fd)%SCANHASHSIZE; | |
370 | ||
371 | for (sh=&(scantable[hash]);*sh;sh=&((*sh)->next)) { | |
372 | if (*sh==sp) { | |
373 | (*sh)=sp->next; | |
374 | break; | |
375 | } | |
376 | } | |
377 | ||
378 | activescans--; | |
379 | } | |
380 | ||
381 | scan *findscan(int fd) { | |
382 | int hash; | |
383 | scan *sp; | |
384 | ||
385 | hash=fd%SCANHASHSIZE; | |
386 | ||
387 | for (sp=scantable[hash];sp;sp=sp->next) | |
388 | if (sp->fd==fd) | |
389 | return sp; | |
390 | ||
391 | return NULL; | |
392 | } | |
393 | ||
394 | void startscan(unsigned int IP, int type, int port, int class) { | |
395 | scan *sp; | |
396 | ||
397 | sp=getscan(); | |
398 | ||
399 | sp->outcome=SOUTCOME_INPROGRESS; | |
400 | sp->port=port; | |
401 | sp->IP=IP; | |
402 | sp->type=type; | |
403 | sp->class=class; | |
404 | sp->bytesread=0; | |
405 | sp->totalbytesread=0; | |
406 | memset(sp->readbuf, '\0', PSCAN_READBUFSIZE); | |
407 | ||
408 | sp->fd=createconnectsocket(sp->IP,sp->port); | |
409 | sp->state=SSTATE_CONNECTING; | |
410 | if (sp->fd<0) { | |
411 | /* Couldn't set up the socket? */ | |
412 | freescan(sp); | |
413 | return; | |
414 | } | |
415 | /* Wait until it is writeable */ | |
416 | registerhandler(sp->fd,POLLERR|POLLHUP|POLLOUT,&handlescansock); | |
417 | /* And set a timeout */ | |
418 | sp->sch=scheduleoneshot(time(NULL)+SCANTIMEOUT,&timeoutscansock,(void *)sp); | |
419 | addscantohash(sp); | |
420 | } | |
421 | ||
422 | void timeoutscansock(void *arg) { | |
423 | scan *sp=(scan *)arg; | |
424 | ||
425 | killsock(sp, SOUTCOME_CLOSED); | |
426 | } | |
427 | ||
428 | void killsock(scan *sp, int outcome) { | |
429 | int i; | |
430 | cachehost *chp; | |
431 | foundproxy *fpp; | |
432 | ||
433 | scansdone++; | |
434 | scansbyclass[sp->class]++; | |
435 | ||
436 | /* Remove the socket from the schedule/event lists */ | |
437 | deregisterhandler(sp->fd,1); /* this will close the fd for us */ | |
438 | deleteschedule(sp->sch,&timeoutscansock,(void *)sp); | |
439 | ||
440 | sp->outcome=outcome; | |
441 | delscanfromhash(sp); | |
442 | ||
443 | /* See if we need to queue another scan.. */ | |
444 | if (sp->outcome==SOUTCOME_CLOSED && | |
445 | ((sp->class==SCLASS_CHECK) || | |
446 | (sp->class==SCLASS_NORMAL && (sp->state==SSTATE_SENTREQUEST || sp->state==SSTATE_GOTRESPONSE)))) | |
447 | queuescan(sp->IP, sp->type, sp->port, SCLASS_PASS2, time(NULL)+300); | |
448 | ||
449 | if (sp->outcome==SOUTCOME_CLOSED && sp->class==SCLASS_PASS2) | |
450 | queuescan(sp->IP, sp->type, sp->port, SCLASS_PASS3, time(NULL)+300); | |
451 | ||
452 | if (sp->outcome==SOUTCOME_CLOSED && sp->class==SCLASS_PASS3) | |
453 | queuescan(sp->IP, sp->type, sp->port, SCLASS_PASS4, time(NULL)+300); | |
454 | ||
455 | if (sp->outcome==SOUTCOME_OPEN) { | |
456 | hitsbyclass[sp->class]++; | |
457 | ||
458 | /* Lets try and get the cache record. If there isn't one, make a new one. */ | |
459 | if (!(chp=findcachehost(sp->IP))) | |
460 | chp=addcleanhost(sp->IP, time(NULL)); | |
461 | ||
462 | /* Stick it on the cache's list of proxies, if necessary */ | |
463 | for (fpp=chp->proxies;fpp;fpp=fpp->next) | |
464 | if (fpp->type==sp->type && fpp->port==sp->port) | |
465 | break; | |
466 | ||
467 | if (!fpp) { | |
468 | fpp=getfoundproxy(); | |
469 | fpp->type=sp->type; | |
470 | fpp->port=sp->port; | |
471 | fpp->next=chp->proxies; | |
472 | chp->proxies=fpp; | |
473 | } | |
474 | ||
475 | if (!chp->glineid) { | |
476 | glinedhosts++; | |
477 | loggline(chp); | |
478 | irc_send("%s GL * +*@%s 1800 :Open Proxy, see http://www.quakenet.org/openproxies.html - ID: %d", | |
479 | mynumeric->content,IPtostr(sp->IP),chp->glineid); | |
480 | Error("proxyscan",ERR_DEBUG,"Found open proxy on host %s",IPtostr(sp->IP)); | |
481 | } else { | |
482 | loggline(chp); /* Update log only */ | |
483 | } | |
484 | ||
485 | /* Update counter */ | |
486 | for(i=0;i<numscans;i++) { | |
487 | if (thescans[i].type==sp->type && thescans[i].port==sp->port) { | |
488 | thescans[i].hits++; | |
489 | break; | |
490 | } | |
491 | } | |
492 | } | |
493 | ||
494 | freescan(sp); | |
495 | ||
496 | /* kick the queue.. */ | |
497 | startqueuedscans(); | |
498 | } | |
499 | ||
500 | void handlescansock(int fd, short events) { | |
501 | scan *sp; | |
502 | char buf[512]; | |
503 | int res; | |
504 | int i; | |
505 | unsigned long netip; | |
506 | unsigned short netport; | |
507 | ||
508 | if ((sp=findscan(fd))==NULL) { | |
509 | /* Not found; return and hope it goes away */ | |
510 | Error("proxyscan",ERR_ERROR,"Unexpected message from fd %d",fd); | |
511 | return; | |
512 | } | |
513 | ||
514 | /* It woke up, delete the alarm call.. */ | |
515 | deleteschedule(sp->sch,&timeoutscansock,(void *)sp); | |
516 | ||
517 | if (events & (POLLERR|POLLHUP)) { | |
518 | /* Some kind of error; give up on this socket */ | |
519 | if (sp->state==SSTATE_GOTRESPONSE) { | |
520 | /* If the error occured while we were waiting for a response, we might have | |
521 | * received the "OPEN PROXY!" message and the EOF at the same time, so continue | |
522 | * processing */ | |
523 | /* Error("proxyscan",ERR_DEBUG,"Got error in GOTRESPONSE state for %s, continuing.",IPtostr(sp->host->IP)); */ | |
524 | } else { | |
525 | killsock(sp, SOUTCOME_CLOSED); | |
526 | return; | |
527 | } | |
528 | } | |
529 | ||
530 | /* Otherwise, we got what we wanted.. */ | |
531 | ||
532 | switch(sp->state) { | |
533 | case SSTATE_CONNECTING: | |
534 | /* OK, we got activity while connecting, so we're going to send some | |
535 | * request depending on scan type. However, we can reregister everything | |
536 | * here to save duplicate code: This code is common for all handlers */ | |
537 | ||
538 | /* Delete the old handler */ | |
539 | deregisterhandler(fd,0); | |
540 | /* Set the new one */ | |
541 | registerhandler(fd,POLLERR|POLLHUP|POLLIN,&handlescansock); | |
542 | sp->sch=scheduleoneshot(time(NULL)+SCANTIMEOUT,&timeoutscansock,(void *)sp); | |
543 | /* Update state */ | |
544 | sp->state=SSTATE_SENTREQUEST; | |
545 | ||
546 | switch(sp->type) { | |
547 | case STYPE_HTTP: | |
548 | sprintf(buf,"CONNECT %s:%d HTTP/1.0\r\n\r\n",myipstr->content,listenport); | |
549 | if ((write(fd,buf,strlen(buf)))<strlen(buf)) { | |
550 | /* We didn't write the full amount, DIE */ | |
551 | killsock(sp,SOUTCOME_CLOSED); | |
552 | return; | |
553 | } | |
554 | break; | |
555 | ||
556 | case STYPE_SOCKS4: | |
557 | /* set up the buffer */ | |
558 | netip=htonl(myip); | |
559 | netport=htons(listenport); | |
560 | memcpy(&buf[4],&netip,4); | |
561 | memcpy(&buf[2],&netport,2); | |
562 | buf[0]=4; | |
563 | buf[1]=1; | |
564 | buf[8]=0; | |
565 | if ((write(fd,buf,9))<9) { | |
566 | /* Didn't write enough, give up */ | |
567 | killsock(sp,SOUTCOME_CLOSED); | |
568 | return; | |
569 | } | |
570 | break; | |
571 | ||
572 | case STYPE_SOCKS5: | |
573 | /* Set up initial request buffer */ | |
574 | buf[0]=5; | |
575 | buf[1]=1; | |
576 | buf[2]=0; | |
577 | if ((write(fd,buf,3))>3) { | |
578 | /* Didn't write enough, give up */ | |
579 | killsock(sp,SOUTCOME_CLOSED); | |
580 | return; | |
581 | } | |
582 | ||
583 | /* Now the actual connect request */ | |
584 | buf[0]=5; | |
585 | buf[1]=1; | |
586 | buf[2]=0; | |
587 | buf[3]=1; | |
588 | netip=htonl(myip); | |
589 | netport=htons(listenport); | |
590 | memcpy(&buf[4],&netip,4); | |
591 | memcpy(&buf[8],&netport,2); | |
592 | res=write(fd,buf,10); | |
593 | if (res<10) { | |
594 | killsock(sp,SOUTCOME_CLOSED); | |
595 | return; | |
596 | } | |
597 | break; | |
598 | ||
599 | case STYPE_WINGATE: | |
600 | /* Send wingate request */ | |
601 | sprintf(buf,"%s:%d\r\n",myipstr->content,listenport); | |
602 | if((write(fd,buf,strlen(buf)))<strlen(buf)) { | |
603 | killsock(sp,SOUTCOME_CLOSED); | |
604 | return; | |
605 | } | |
606 | break; | |
607 | ||
608 | case STYPE_CISCO: | |
609 | /* Send cisco request */ | |
610 | sprintf(buf,"cisco\r\n"); | |
611 | if ((write(fd,buf,strlen(buf)))<strlen(buf)) { | |
612 | killsock(sp, SOUTCOME_CLOSED); | |
613 | return; | |
614 | } | |
615 | ||
616 | sprintf(buf,"telnet %s %d\r\n",myipstr->content,listenport); | |
617 | if ((write(fd,buf,strlen(buf)))<strlen(buf)) { | |
618 | killsock(sp, SOUTCOME_CLOSED); | |
619 | return; | |
620 | } | |
621 | ||
622 | break; | |
623 | } | |
624 | break; | |
625 | ||
626 | case SSTATE_SENTREQUEST: | |
627 | res=read(fd, sp->readbuf+sp->bytesread, PSCAN_READBUFSIZE-sp->bytesread); | |
628 | ||
629 | if (res<=0) { | |
630 | if ((errno!=EINTR && errno!=EWOULDBLOCK) || res==0) { | |
631 | /* EOF, forget it */ | |
632 | killsock(sp, SOUTCOME_CLOSED); | |
633 | return; | |
634 | } | |
635 | } | |
636 | ||
637 | sp->bytesread+=res; | |
638 | sp->totalbytesread+=res; | |
639 | for (i=0;i<sp->bytesread - MAGICSTRINGLENGTH;i++) { | |
640 | if (!strncmp(sp->readbuf+i, MAGICSTRING, MAGICSTRINGLENGTH)) { | |
641 | /* Found the magic string */ | |
642 | /* If the offset is 0, this means it was the first thing we got from the socket, | |
643 | * so it's an actual IRCD (sheesh). Note that when the buffer is full and moved, | |
644 | * the thing moved to offset 0 would previously have been tested as offset | |
645 | * PSCAN_READBUFSIZE/2. */ | |
646 | if (i==0) { | |
647 | killsock(sp, SOUTCOME_CLOSED); | |
648 | return; | |
649 | } | |
650 | ||
651 | killsock(sp, SOUTCOME_OPEN); | |
652 | return; | |
653 | } | |
654 | } | |
655 | ||
656 | /* If the buffer is full, move half of it along to make room */ | |
657 | if (sp->bytesread == PSCAN_READBUFSIZE) { | |
658 | memcpy(sp->readbuf, sp->readbuf + (PSCAN_READBUFSIZE)/2, PSCAN_READBUFSIZE/2); | |
659 | sp->bytesread = PSCAN_READBUFSIZE/2; | |
660 | } | |
661 | ||
662 | /* Don't read data forever.. */ | |
663 | if (sp->totalbytesread > READ_SANITY_LIMIT) { | |
664 | killsock(sp, SOUTCOME_CLOSED); | |
665 | return; | |
666 | } | |
667 | ||
668 | /* No magic string yet, we schedule another timeout in case it comes later. */ | |
669 | sp->sch=scheduleoneshot(time(NULL)+SCANTIMEOUT,&timeoutscansock,(void *)sp); | |
670 | return; | |
671 | } | |
672 | } | |
673 | ||
674 | void killallscans() { | |
675 | int i; | |
676 | scan *sp; | |
677 | cachehost *chp; | |
678 | ||
679 | for(i=0;i<SCANHASHSIZE;i++) { | |
680 | for(sp=scantable[i];sp;sp=sp->next) { | |
681 | /* If there is a pending scan, delete it's clean host record.. */ | |
682 | if ((chp=findcachehost(sp->IP)) && !chp->proxies) | |
683 | delcachehost(chp); | |
684 | ||
685 | if (sp->fd!=-1) { | |
686 | deregisterhandler(sp->fd,1); | |
687 | deleteschedule(sp->sch,&timeoutscansock,(void *)(sp)); | |
688 | } | |
689 | } | |
690 | } | |
691 | } | |
692 | ||
693 | void proxyscanstats(int hooknum, void *arg) { | |
694 | char buf[512]; | |
695 | ||
696 | sprintf(buf, "Proxyscn: %6d/%4d scans complete/in progress. %d hosts queued.", | |
697 | scansdone,activescans,queuedhosts); | |
698 | triggerhook(HOOK_CORE_STATSREPLY,buf); | |
699 | sprintf(buf, "Proxyscn: %6u known clean hosts",cleancount()); | |
700 | triggerhook(HOOK_CORE_STATSREPLY,buf); | |
701 | } | |
702 | ||
703 | void sendlagwarning() { | |
704 | int i,j; | |
705 | nick *np; | |
706 | ||
707 | for (i=0;i<MAXSERVERS;i++) { | |
708 | if (serverlist[i].maxusernum>0) { | |
709 | for(j=0;j<serverlist[i].maxusernum;j++) { | |
710 | np=servernicks[i][j]; | |
711 | if (np!=NULL && IsOper(np)) { | |
712 | sendnoticetouser(proxyscannick,np,"Warning: More than 20,000 hosts to scan - I'm lagging behind badly!"); | |
713 | } | |
714 | } | |
715 | } | |
716 | } | |
717 | } | |
718 | ||
719 | void proxyscandostatus(nick *np) { | |
720 | int i; | |
721 | int totaldetects=0; | |
722 | ||
723 | sendnoticetouser(proxyscannick,np,"Service uptime: %s",longtoduration(time(NULL)-starttime, 1)); | |
724 | sendnoticetouser(proxyscannick,np,"Total scans completed: %d",scansdone); | |
725 | sendnoticetouser(proxyscannick,np,"Total hosts glined: %d",glinedhosts); | |
726 | ||
727 | sendnoticetouser(proxyscannick,np,"Currently active scans: %d/%d",activescans,maxscans); | |
728 | sendnoticetouser(proxyscannick,np,"Normal queued scans: %d",normalqueuedscans); | |
729 | sendnoticetouser(proxyscannick,np,"Timed queued scans: %d",prioqueuedscans); | |
730 | sendnoticetouser(proxyscannick,np,"'Clean' cached hosts: %d",cleancount()); | |
731 | sendnoticetouser(proxyscannick,np,"'Dirty' cached hosts: %d",dirtycount()); | |
732 | ||
733 | for (i=0;i<5;i++) | |
734 | sendnoticetouser(proxyscannick,np,"Open proxies, class %1d: %d/%d (%.2f%%)",i,hitsbyclass[i],scansbyclass[i],((float)hitsbyclass[i]*100)/scansbyclass[i]); | |
735 | ||
736 | for (i=0;i<numscans;i++) | |
737 | totaldetects+=thescans[i].hits; | |
738 | ||
739 | sendnoticetouser(proxyscannick,np,"Scan type Port Detections"); | |
740 | for (i=0;i<numscans;i++) | |
741 | sendnoticetouser(proxyscannick,np,"%-9s %-5d %d (%.2f%%)", | |
742 | scantostr(thescans[i].type), thescans[i].port, thescans[i].hits, ((float)thescans[i].hits*100)/totaldetects); | |
743 | ||
744 | sendnoticetouser(proxyscannick,np,"End of list."); | |
745 | } | |
746 | ||
747 | void proxyscandebug(nick *np) { | |
748 | /* Dump all scans.. */ | |
749 | int i; | |
750 | int activescansfound=0; | |
751 | int totalscansfound=0; | |
752 | scan *sp; | |
753 | ||
754 | sendnoticetouser(proxyscannick,np,"Active scans : %d",activescans); | |
755 | ||
756 | for (i=0;i<SCANHASHSIZE;i++) { | |
757 | for (sp=scantable[i];sp;sp=sp->next) { | |
758 | if (sp->outcome==SOUTCOME_INPROGRESS) { | |
759 | activescansfound++; | |
760 | } | |
761 | totalscansfound++; | |
762 | sendnoticetouser(proxyscannick,np,"fd: %d type: %d port: %d state: %d outcome: %d IP: %s", | |
763 | sp->fd,sp->type,sp->port,sp->state,sp->outcome,IPtostr(sp->IP)); | |
764 | } | |
765 | } | |
766 | ||
767 | sendnoticetouser(proxyscannick,np,"Total %d scans actually found (%d active)",totalscansfound,activescansfound); | |
768 | } |