/[cvs]/eggdrop1.4/src/dcc.c
ViewVC logotype

Contents of /eggdrop1.4/src/dcc.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.11 - (show annotations) (download) (as text)
Thu Sep 30 21:08:30 1999 UTC (20 years, 2 months ago) by guppy
Branch: MAIN
Changes since 1.10: +0 -13 lines
File MIME type: text/x-chdr
more patches

1 /*
2 * dcc.c -- handles:
3 * activity on a dcc socket
4 * disconnect on a dcc socket
5 * ...and that's it! (but it's a LOT)
6 * dprintf'ized, 27oct1995
7 */
8 /*
9 * This file is part of the eggdrop source code
10 * copyright (c) 1997 Robey Pointer
11 * and is distributed according to the GNU general public license.
12 * For full details, read the top of 'main.c' or the file called
13 * COPYING that was distributed with this code.
14 */
15
16 #include "main.h"
17 #include <ctype.h>
18 #include <errno.h>
19 #include "modules.h"
20 #include "tandem.h"
21
22 /* includes for botnet md5 challenge/response code <cybah> */
23 #include "md5/global.h"
24 #include "md5/md5.h"
25
26 extern struct userrec *userlist;
27 extern struct chanset_t *chanset;
28 extern Tcl_Interp *interp;
29 extern time_t now;
30 extern int egg_numver, connect_timeout, conmask, backgrd, max_dcc;
31 extern int make_userfile, default_flags, debug_output, ignore_time;
32 extern char botnetnick[], ver[], origbotname[], notify_new[];
33
34 struct dcc_t *dcc = 0; /* dcc list */
35 int dcc_total = 0; /* total dcc's */
36 char tempdir[121] = ""; /* temporary directory (default: current dir) */
37 int require_p = 0; /* require 'p' access to get on the party line? */
38 int allow_new_telnets = 0; /* allow people to introduce themselves via telnet */
39 int stealth_telnets = 0; /* be paranoid? <cybah> */
40 int use_telnet_banner = 0; /* display telnet banner? [seC] */
41 char network[41] = "unknown-net"; /* name of the IRC network you're on */
42 int password_timeout = 180; /* time to wait for a password from a user */
43 int bot_timeout = 60; /* bot timeout value */
44 int identtimeout = 5; /* timeout value for ident lookups */
45 int protect_telnet = 1; /* even bother with ident lookups :) */
46 int flood_telnet_thr = 5; /* number of telnet connections to be considered a flood */
47 int flood_telnet_time = 60; /* in how many seconds? */
48 extern int min_dcc_port, max_dcc_port; /* valid portrange for telnets */
49 extern int par_telnet_flood; /* trigger telnet flood for +f ppl? */
50 char bannerfile[121] = "telnet-banner"; /* file displayed on telnet login */
51
52 static void strip_telnet(int sock, char *buf, int *len)
53 {
54 unsigned char *p = (unsigned char *) buf, *o = (unsigned char *) buf;
55 int mark;
56
57 while (*p != 0) {
58 while ((*p != 255) && (*p != 0))
59 *o++ = *p++;
60 if (*p == 255) {
61 p++;
62 mark = 2;
63 if (!*p)
64 mark = 1; /* bogus */
65 if ((*p >= 251) && (*p <= 254)) {
66 mark = 3;
67 if (!*(p + 1))
68 mark = 2; /* bogus */
69 }
70 if (*p == 251) {
71 /* WILL X -> response: DONT X */
72 /* except WILL ECHO which we just smile and ignore */
73 if (!(*(p + 1) == 1)) {
74 write(sock, "\377\376", 2);
75 write(sock, p + 1, 1);
76 }
77 }
78 if (*p == 253) {
79 /* DO X -> response: WONT X */
80 /* except DO ECHO which we just smile and ignore */
81 if (!(*(p + 1) == 1)) {
82 write(sock, "\377\374", 2);
83 write(sock, p + 1, 1);
84 }
85 }
86 if (*p == 246) {
87 /* "are you there?" */
88 /* response is: "hell yes!" */
89 write(sock, "\r\nHell, yes!\r\n", 14);
90 }
91 /* anything else can probably be ignored */
92 p += mark - 1;
93 *len = *len - mark;
94 }
95 }
96 *o = *p;
97 }
98
99 static void greet_new_bot(int idx)
100 {
101 int bfl = bot_flags(dcc[idx].user);
102 int i;
103
104 dcc[idx].timeval = now;
105 dcc[idx].u.bot->version[0] = 0;
106 dcc[idx].u.bot->numver = 0;
107 if (bfl & BOT_REJECT) {
108 putlog(LOG_BOTS, "*", DCC_REJECT, dcc[idx].nick);
109 dprintf(idx, "error You are being rejected.\n");
110 dprintf(idx, "bye\n");
111 killsock(dcc[idx].sock);
112 lostdcc(idx);
113 return;
114 }
115 if (bfl & BOT_LEAF)
116 dcc[idx].status |= STAT_LEAF;
117 dcc[idx].status |= STAT_LINKING;
118 #ifndef NO_OLD_BOTNET
119 dprintf(idx, "version %d %d %s <%s>\n", egg_numver, HANDLEN, ver, network);
120 #else
121 dprintf(idx, "v %d %d %s <%s>\n", egg_numver, HANDLEN, ver, network);
122 #endif
123 for (i = 0; i < dcc_total; i++)
124 if (dcc[i].type == &DCC_FORK_BOT) {
125 killsock(dcc[i].sock);
126 lostdcc(i);
127 i--;
128 }
129 }
130
131 static void bot_version(int idx, char *par)
132 {
133 char x[1024];
134 int l;
135
136 context;
137 dcc[idx].timeval = now;
138 if ((par[0] >= '0') && (par[0] <= '9')) {
139 char *work;
140
141 work = newsplit(&par);
142 dcc[idx].u.bot->numver = atoi(work);
143 } else
144 dcc[idx].u.bot->numver = 0;
145
146 #ifndef NO_OLD_BOTNET
147 if (b_numver(idx) < NEAT_BOTNET) {
148 #if HANDLEN != 9
149 dprintf(idx, "error Non-matching handle length: mine %d, yours 9\n",
150 HANDLEN);
151 dprintf(idx, "bye\n");
152 killsock(dcc[idx].sock);
153 lostdcc(idx);
154 return;
155 #else
156 dprintf(idx, "thisbot %s\n", botnetnick);
157 #endif
158 } else {
159 #endif
160 dprintf(idx, "tb %s\n", botnetnick);
161 l = atoi(newsplit(&par));
162 if (l != HANDLEN) {
163 dprintf(idx, "error Non-matching handle length: mine %d, yours %d\n",
164 HANDLEN, l);
165 dprintf(idx, "bye\n");
166 killsock(dcc[idx].sock);
167 lostdcc(idx);
168 return;
169 }
170 #ifndef NO_OLD_BOTNET
171 }
172 #endif
173 strncpy(dcc[idx].u.bot->version, par, 120);
174 dcc[idx].u.bot->version[120] = 0;
175 putlog(LOG_BOTS, "*", DCC_LINKED, dcc[idx].nick);
176 chatout("*** Linked to %s\n", dcc[idx].nick);
177 botnet_send_nlinked(idx, dcc[idx].nick, botnetnick, '!',
178 dcc[idx].u.bot->numver);
179 dump_links(idx);
180 dcc[idx].type = &DCC_BOT;
181 addbot(dcc[idx].nick, dcc[idx].nick, botnetnick, '-',
182 dcc[idx].u.bot->numver);
183 check_tcl_link(dcc[idx].nick, botnetnick);
184 simple_sprintf(x, "v %d", dcc[idx].u.bot->numver);
185 bot_share(idx, x);
186 dprintf(idx, "el\n");
187 }
188
189 void failed_link(int idx)
190 {
191 char s[81], s1[512];
192
193 if (dcc[idx].port >= dcc[idx].u.bot->port + 3) {
194 if (dcc[idx].u.bot->linker[0]) {
195 sprintf(s, "Couldn't link to %s.", dcc[idx].nick);
196 strcpy(s1, dcc[idx].u.bot->linker);
197 add_note(s1, botnetnick, s, -2, 0);
198 }
199 if (dcc[idx].u.bot->numver >= (-1))
200 putlog(LOG_BOTS, "*", DCC_LINKFAIL, dcc[idx].nick);
201 killsock(dcc[idx].sock);
202 strcpy(s, dcc[idx].nick);
203 lostdcc(idx);
204 autolink_cycle(s); /* check for more auto-connections */
205 return;
206 }
207 /* try next port */
208 killsock(dcc[idx].sock);
209 dcc[idx].sock = getsock(SOCK_STRONGCONN);
210 dcc[idx].port++;
211 dcc[idx].timeval = now;
212 if (open_telnet_raw(dcc[idx].sock, dcc[idx].host, dcc[idx].port) < 0) {
213 failed_link(idx);
214 }
215 }
216
217 static void cont_link(int idx, char *buf, int i)
218 {
219 char x[1024];
220 int atr = bot_flags(dcc[idx].user);
221
222 context;
223 if (atr & BOT_HUB) {
224 /* disconnect all +a bots because we just got a hub */
225 for (i = 0; i < dcc_total; i++) {
226 if ((i != idx) && (bot_flags(dcc[i].user) & BOT_ALT)) {
227 if (dcc[i].type == &DCC_FORK_BOT) {
228 killsock(dcc[i].sock);
229 lostdcc(i);
230 } else if (dcc[i].type == &DCC_BOT_NEW) {
231 killsock(dcc[i].sock);
232 lostdcc(i);
233 }
234 }
235 }
236 /* just those currently in the process of linking */
237 if (in_chain(dcc[idx].nick)) {
238 i = nextbot(dcc[idx].nick);
239 if (i > 0) {
240 simple_sprintf(x, "Unlinked %s (restructure)", dcc[i].nick);
241 chatout("*** %s\n", x);
242 botnet_send_unlinked(i, dcc[i].nick, x);
243 dprintf(i, "bye\n");
244 killsock(dcc[i].sock);
245 lostdcc(i);
246 }
247 }
248 }
249 dcc[idx].type = &DCC_BOT_NEW;
250 dcc[idx].u.bot->numver = 0;
251
252 /* Don't send our password here, just the username. The code later on
253 * will determine if the password needs to be sent in cleartext or if we
254 * can send an MD5 digest. <cybah>
255 */
256
257 dprintf(idx, "%s\n", botnetnick);
258 return;
259 }
260
261 /* This function generates a digest by combining 'challenge' with
262 * 'password' and then sends it to the other bot. <Cybah>
263 */
264 static void dcc_bot_digest(int idx, char *challenge, char *password)
265 {
266 MD5_CTX Context;
267 char DigestString[33]; /* 32 for digest in hex + null */
268 unsigned char Digest[16];
269 int i;
270
271 MD5Init(&Context);
272 MD5Update(&Context, (unsigned char *)challenge, strlen(challenge));
273 MD5Update(&Context, (unsigned char *)password, strlen(password));
274 MD5Final(Digest, &Context);
275
276 for(i=0;i<16;i++)
277 sprintf(DigestString + (i*2), "%.2x", Digest[i]);
278
279 dprintf(idx, "digest %s\n", DigestString);
280 putlog(LOG_BOTS, "*", "Received challenge from %s... sending response ...", dcc[idx].nick);
281 }
282
283 static void dcc_bot_new(int idx, char *buf, int x)
284 {
285 struct userrec *u = get_user_by_handle(userlist, dcc[idx].nick);
286 char *code;
287
288 strip_telnet(dcc[idx].sock, buf, &x);
289 code = newsplit(&buf);
290 if (!strcasecmp(code, "*hello!")) {
291 greet_new_bot(idx);
292 } else if (!strcasecmp(code, "version") || !strcasecmp(code, "v")) {
293 bot_version(idx, buf);
294 } else if (!strcasecmp(code, "badpass")) {
295 /* we entered the wrong password */
296 putlog(LOG_BOTS, "*", DCC_BADPASS, dcc[idx].nick);
297 } else if (!strcasecmp(code, "passreq")) {
298 char *pass = get_user(&USERENTRY_PASS, u);
299
300 if (!pass || !strcmp(pass, "-")) {
301 putlog(LOG_BOTS, "*", DCC_PASSREQ, dcc[idx].nick);
302 dprintf(idx, "-\n");
303 } else {
304 /* Determine if the other end supports an MD5 digest instead of a
305 * cleartext password. <Cybah>
306 */
307 if(buf && buf[0] && strchr(buf, '<') && strchr(buf+1, '>')) {
308 dcc_bot_digest(idx, buf, pass);
309 } else {
310 dprintf(idx, "%s\n", pass);
311 }
312 }
313 } else if (!strcasecmp(code, "error")) {
314 putlog(LOG_BOTS, "*", DCC_LINKERROR, dcc[idx].nick, buf);
315 }
316 /* ignore otherwise */
317 }
318
319 static void eof_dcc_bot_new(int idx)
320 {
321 context;
322 putlog(LOG_BOTS, "*", DCC_LOSTBOT, dcc[idx].nick, dcc[idx].port);
323 killsock(dcc[idx].sock);
324 lostdcc(idx);
325 }
326
327 static void timeout_dcc_bot_new(int idx)
328 {
329 putlog(LOG_BOTS, "*", DCC_TIMEOUT, dcc[idx].nick,
330 dcc[idx].host, dcc[idx].port);
331 killsock(dcc[idx].sock);
332 lostdcc(idx);
333 }
334
335 static void display_dcc_bot_new(int idx, char *buf)
336 {
337 sprintf(buf, "bot* waited %lus", now - dcc[idx].timeval);
338 }
339
340 static int expmem_dcc_bot_(void *x)
341 {
342 return sizeof(struct bot_info);
343 }
344
345 static void free_dcc_bot_(int n, void *x)
346 {
347 if (dcc[n].type == &DCC_BOT) {
348 unvia(n, findbot(dcc[n].nick));
349 rembot(dcc[n].nick);
350 }
351 nfree(x);
352 }
353
354 struct dcc_table DCC_BOT_NEW =
355 {
356 "BOT_NEW",
357 0,
358 eof_dcc_bot_new,
359 dcc_bot_new,
360 &bot_timeout,
361 timeout_dcc_bot_new,
362 display_dcc_bot_new,
363 expmem_dcc_bot_,
364 free_dcc_bot_,
365 0
366 };
367
368 /* hash function for tandem bot commands */
369 extern botcmd_t C_bot[];
370
371 static void dcc_bot(int idx, char *code, int i)
372 {
373 char *msg;
374 int f;
375
376 context;
377 strip_telnet(dcc[idx].sock, code, &i);
378 if (debug_output) {
379 if (code[0] == 's')
380 putlog(LOG_BOTSHARE, "*", "{%s} %s", dcc[idx].nick, code + 2);
381 else
382 putlog(LOG_BOTNET, "*", "[%s] %s", dcc[idx].nick, code);
383 }
384 msg = strchr(code, ' ');
385 if (msg) {
386 *msg = 0;
387 msg++;
388 } else
389 msg = "";
390 f = 0;
391 i = 0;
392 while ((C_bot[i].name != NULL) && (!f)) {
393 int y = strcasecmp(code, C_bot[i].name);
394
395 if (y == 0) {
396 /* found a match */
397 (C_bot[i].func) (idx, msg);
398 f = 1;
399 } else if (y < 0)
400 return;
401 i++;
402 }
403 }
404
405 static void eof_dcc_bot(int idx)
406 {
407 char x[1024];
408
409 simple_sprintf(x, "Lost bot: %s", dcc[idx].nick);
410 putlog(LOG_BOTS, "*", "%s.", x);
411 chatout("*** %s\n", x);
412 botnet_send_unlinked(idx, dcc[idx].nick, x);
413 killsock(dcc[idx].sock);
414 lostdcc(idx);
415 }
416
417 static void display_dcc_bot(int idx, char *buf)
418 {
419 int i = simple_sprintf(buf, "bot flags: ");
420
421 buf[i++] = b_status(idx) & STAT_PINGED ? 'P' : 'p';
422 buf[i++] = b_status(idx) & STAT_SHARE ? 'U' : 'u';
423 buf[i++] = b_status(idx) & STAT_CALLED ? 'C' : 'c';
424 buf[i++] = b_status(idx) & STAT_OFFERED ? 'O' : 'o';
425 buf[i++] = b_status(idx) & STAT_SENDING ? 'S' : 's';
426 buf[i++] = b_status(idx) & STAT_GETTING ? 'G' : 'g';
427 buf[i++] = b_status(idx) & STAT_WARNED ? 'W' : 'w';
428 buf[i++] = b_status(idx) & STAT_LEAF ? 'L' : 'l';
429 buf[i++] = b_status(idx) & STAT_LINKING ? 'I' : 'i';
430 buf[i++] = b_status(idx) & STAT_AGGRESSIVE ? 'a' : 'A';
431 buf[i++] = 0;
432 }
433
434 static void display_dcc_fork_bot(int idx, char *buf)
435 {
436 sprintf(buf, "conn bot");
437 }
438
439 struct dcc_table DCC_BOT =
440 {
441 "BOT",
442 DCT_BOT,
443 eof_dcc_bot,
444 dcc_bot,
445 0,
446 0,
447 display_dcc_bot,
448 expmem_dcc_bot_,
449 free_dcc_bot_,
450 0
451 };
452
453 struct dcc_table DCC_FORK_BOT =
454 {
455 "FORK_BOT",
456 0,
457 failed_link,
458 cont_link,
459 &connect_timeout,
460 failed_link,
461 display_dcc_fork_bot,
462 expmem_dcc_bot_,
463 free_dcc_bot_,
464 0
465 };
466
467 /* This function generates a digest by combining a challenge consisting of
468 * our process id + connection time + botnetnick. The digest is then compared
469 * to the one given by the remote bot. Returns 1 if the digest matches,
470 * otherwise returns 0. <Cybah>
471 */
472 static int dcc_bot_check_digest(int idx, char *digest)
473 {
474 MD5_CTX Context;
475 char DigestString[33]; /* 32 for digest in hex + null */
476 unsigned char Digest[16];
477 int i;
478 char *password = get_user(&USERENTRY_PASS, dcc[idx].user);
479
480 MD5Init(&Context);
481
482 snprintf(DigestString, 33, "<%x%x@", getpid(), (unsigned int)dcc[idx].timeval);
483 MD5Update(&Context, (unsigned char *)DigestString, strlen(DigestString));
484 MD5Update(&Context, (unsigned char *)botnetnick, strlen(botnetnick));
485 MD5Update(&Context, (unsigned char *)">", 1);
486 MD5Update(&Context, (unsigned char *)password, strlen(password));
487
488 MD5Final(Digest, &Context);
489
490 for(i=0;i<16;i++)
491 sprintf(DigestString + (i*2), "%.2x", Digest[i]);
492
493 if(!strcmp(DigestString, digest)) {
494 return 1;
495 }
496 putlog(LOG_BOTS, "*", "Response (password hash) from %s incorrect", dcc[idx].nick);
497 return 0;
498 }
499
500 static void dcc_chat_pass(int idx, char *buf, int atr)
501 {
502 if (!atr)
503 return;
504 strip_telnet(dcc[idx].sock, buf, &atr);
505 atr = dcc[idx].user ? dcc[idx].user->flags : 0;
506
507 /* Check for MD5 digest from remote _bot_. <cybah> */
508 if ((atr & USER_BOT) && !strncasecmp(buf, "digest ", 7)) {
509 if(dcc_bot_check_digest(idx, buf+7)) {
510 nfree(dcc[idx].u.chat);
511 dcc[idx].type = &DCC_BOT_NEW;
512 dcc[idx].u.bot = get_data_ptr(sizeof(struct bot_info));
513 dcc[idx].status = STAT_CALLED;
514 dprintf(idx, "*hello!\n");
515 greet_new_bot(idx);
516 return;
517 } else {
518 /* invalid password/digest */
519 dprintf(idx, "badpass\n");
520 putlog(LOG_MISC, "*", DCC_BADLOGIN, dcc[idx].nick, dcc[idx].host,
521 dcc[idx].port);
522 killsock(dcc[idx].sock);
523 lostdcc(idx);
524 return;
525 }
526 }
527
528 if (u_pass_match(dcc[idx].user, buf)) {
529 if (atr & USER_BOT) {
530 nfree(dcc[idx].u.chat);
531 dcc[idx].type = &DCC_BOT_NEW;
532 dcc[idx].u.bot = get_data_ptr(sizeof(struct bot_info));
533
534 dcc[idx].status = STAT_CALLED;
535 dprintf(idx, "*hello!\n");
536 greet_new_bot(idx);
537 } else {
538 /* log entry for successful login -slennox 3/28/1999 */
539 putlog(LOG_MISC, "*", DCC_LOGGEDIN, dcc[idx].nick,
540 dcc[idx].host, dcc[idx].port);
541 if (dcc[idx].u.chat->away) {
542 nfree(dcc[idx].u.chat->away);
543 dcc[idx].u.chat->away = NULL;
544 }
545 dcc[idx].type = &DCC_CHAT;
546 dcc[idx].status &= ~STAT_CHAT;
547 dcc[idx].u.chat->con_flags = (atr & USER_MASTER) ? conmask : 0;
548 dcc[idx].u.chat->channel = -2;
549 if (dcc[idx].status & STAT_TELNET)
550 dprintf(idx, "\377\374\001\n"); /* turn echo back on */
551 dcc_chatter(idx);
552 }
553 } else {
554 if (atr & USER_BOT)
555 dprintf(idx, "badpass\n");
556 else
557 dprintf(idx, DCC_HOUSTON);
558 putlog(LOG_MISC, "*", DCC_BADLOGIN, dcc[idx].nick,
559 dcc[idx].host, dcc[idx].port);
560 if (dcc[idx].u.chat->away) { /* su from a dumb user */
561 if (dcc[idx].status & STAT_TELNET)
562 dprintf(idx, "\377\374\001\n"); /* turn echo back on */
563 dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->away);
564 strcpy(dcc[idx].nick, dcc[idx].u.chat->away);
565 nfree(dcc[idx].u.chat->away);
566 nfree(dcc[idx].u.chat->su_nick);
567 dcc[idx].u.chat->away = NULL;
568 dcc[idx].u.chat->su_nick = NULL;
569 dcc[idx].type = &DCC_CHAT;
570 if (dcc[idx].u.chat->channel < 100000)
571 botnet_send_join_idx(idx, -1);
572 chanout_but(-1, dcc[idx].u.chat->channel, DCC_JOIN, dcc[idx].nick);
573 } else {
574 killsock(dcc[idx].sock);
575 lostdcc(idx);
576 }
577 }
578 }
579
580 static void eof_dcc_general(int idx)
581 {
582 putlog(LOG_MISC, "*",DCC_LOSTDCC, dcc[idx].nick,
583 dcc[idx].host, dcc[idx].port);
584 killsock(dcc[idx].sock);
585 lostdcc(idx);
586 }
587
588 static void tout_dcc_chat_pass(int idx)
589 {
590 dprintf(idx, "Timeout.\n");
591 putlog(LOG_MISC, "*",DCC_PWDTIMEOUT, dcc[idx].nick,
592 dcc[idx].host);
593 killsock(dcc[idx].sock);
594 lostdcc(idx);
595 }
596
597 static void display_dcc_chat_pass(int idx, char *buf)
598 {
599 sprintf(buf, "pass waited %lus", now - dcc[idx].timeval);
600 }
601
602 static int expmem_dcc_general(void *x)
603 {
604 register struct chat_info *p = (struct chat_info *) x;
605 int tot = sizeof(struct chat_info);
606
607 if (p->away)
608 tot += strlen(p->away) + 1;
609 if (p->buffer) {
610 struct msgq *q = p->buffer;
611
612 while (q) {
613 tot += sizeof(struct list_type);
614
615 tot += q->len + 1;
616 q = q->next;
617 }
618 }
619 return tot;
620 }
621
622 static void kill_dcc_general(int idx, void *x)
623 {
624 register struct chat_info *p = (struct chat_info *) x;
625
626 if (p) {
627 if (p->buffer) {
628 struct msgq *r = dcc[idx].u.chat->buffer, *q;
629
630 while (r) {
631 q = r->next;
632 nfree(r->msg);
633 nfree(r);
634 r = q;
635 }
636 }
637 if (p->away) {
638 nfree(p->away);
639 }
640 nfree(p);
641 }
642 }
643
644 /* Remove the color control codes that mIRC,pIRCh etc use to make
645 * their client seem so fecking cool! (Sorry, Khaled, you are a nice
646 * guy, but when you added this feature you forced people to either
647 * use your *SHAREWARE* client or face screenfulls of crap!) */
648 static void strip_mirc_codes(int flags, char *text)
649 {
650 char *dd = text;
651
652 while (*text) {
653 switch (*text) {
654 case 2: /* Bold text */
655 if (flags & STRIP_BOLD) {
656 text++;
657 continue;
658 }
659 break;
660 case 3: /* mIRC colors? */
661 if (flags & STRIP_COLOR) {
662 if (isdigit(text[1])) { /* Is the first char a number? */
663 text += 2; /* Skip over the ^C and the first digit */
664 if (isdigit(*text))
665 text++; /* Is this a double digit number? */
666 if (*text == ',') { /* Do we have a background color next? */
667 if (isdigit(text[1]))
668 text += 2; /* Skip over the first background digit */
669 if (isdigit(*text))
670 text++; /* Is it a double digit? */
671 }
672 } else
673 text++;
674 continue;
675 }
676 break;
677 case 7:
678 if (flags & STRIP_BELLS) {
679 text++;
680 continue;
681 }
682 break;
683 case 0x16: /* Reverse video */
684 if (flags & STRIP_REV) {
685 text++;
686 continue;
687 }
688 break;
689 case 0x1f: /* Underlined text */
690 if (flags & STRIP_UNDER) {
691 text++;
692 continue;
693 }
694 break;
695 case 033:
696 if (flags & STRIP_ANSI) {
697 text++;
698 if (*text == '[') {
699 text++;
700 while ((*text == ';') || isdigit(*text))
701 text++;
702 if (*text)
703 text++; /* also kill the following char */
704 }
705 continue;
706 }
707 break;
708 }
709 *dd++ = *text++; /* Move on to the next char */
710 }
711 *dd = 0;
712 }
713
714 static void append_line(int idx, char *line)
715 {
716 int l = strlen(line);
717 struct msgq *p, *q;
718 struct chat_info *c = (dcc[idx].type == &DCC_CHAT) ? dcc[idx].u.chat :
719 dcc[idx].u.file->chat;
720
721 if (c->current_lines > 1000) {
722 p = c->buffer;
723 /* they're probably trying to fill up the bot nuke the sods :) */
724 while (p) { /* flush their queue */
725 q = p->next;
726 nfree(p->msg);
727 nfree(p);
728 p = q;
729 }
730 c->buffer = 0;
731 dcc[idx].status &= ~STAT_PAGE;
732 do_boot(idx, botnetnick, "too many pages - senq full");
733 return;
734 }
735 if ((c->line_count < c->max_line) && (c->buffer == NULL)) {
736 c->line_count++;
737 tputs(dcc[idx].sock, line, l);
738 } else {
739 c->current_lines++;
740 if (c->buffer == NULL)
741 q = NULL;
742 else {
743 q = c->buffer;
744 while (q->next != NULL)
745 q = q->next;
746 }
747 p = get_data_ptr(sizeof(struct msgq));
748
749 p->len = l;
750 p->msg = get_data_ptr(l + 1);
751 p->next = NULL;
752 strcpy(p->msg, line);
753 if (q == NULL)
754 c->buffer = p;
755 else
756 q->next = p;
757 }
758 }
759
760 static void out_dcc_general(int idx, char *buf, void *x)
761 {
762 register struct chat_info *p = (struct chat_info *) x;
763 char *y = buf;
764
765 strip_mirc_codes(p->strip_flags, buf);
766 if (dcc[idx].status & STAT_TELNET)
767 y = add_cr(buf);
768 if (dcc[idx].status & STAT_PAGE)
769 append_line(idx, y);
770 else
771 tputs(dcc[idx].sock, y, strlen(y));
772 }
773
774 struct dcc_table DCC_CHAT_PASS =
775 {
776 "CHAT_PASS",
777 0,
778 eof_dcc_general,
779 dcc_chat_pass,
780 &password_timeout,
781 tout_dcc_chat_pass,
782 display_dcc_chat_pass,
783 expmem_dcc_general,
784 kill_dcc_general,
785 out_dcc_general
786 };
787
788 /* make sure ansi code is just for color-changing */
789 static int check_ansi(char *v)
790 {
791 int count = 2;
792
793 if (*v++ != '\033')
794 return 1;
795 if (*v++ != '[')
796 return 1;
797 while (*v) {
798 if (*v == 'm')
799 return 0;
800 if ((*v != ';') && ((*v < '0') || (*v > '9')))
801 return count;
802 v++;
803 count++;
804 }
805 return count;
806 }
807
808 static void eof_dcc_chat(int idx)
809 {
810 context;
811 putlog(LOG_MISC, "*", DCC_LOSTDCC, dcc[idx].nick,
812 dcc[idx].host, dcc[idx].port);
813 context;
814 if (dcc[idx].u.chat->channel >= 0) {
815 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s lost dcc link.\n",
816 dcc[idx].nick);
817 context;
818 if (dcc[idx].u.chat->channel < 100000)
819 botnet_send_part_idx(idx, "lost dcc link");
820 check_tcl_chpt(botnetnick, dcc[idx].nick, dcc[idx].sock,
821 dcc[idx].u.chat->channel);
822 }
823 context;
824 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
825 context;
826 killsock(dcc[idx].sock);
827 context;
828 lostdcc(idx);
829 context;
830 }
831
832 static void dcc_chat(int idx, char *buf, int i)
833 {
834 int nathan = 0, doron = 0, fixed = 0;
835 char *v, *d;
836
837 context;
838 strip_telnet(dcc[idx].sock, buf, &i);
839 if (buf[0] && (buf[0] != '.') &&
840 detect_dcc_flood(&dcc[idx].timeval, dcc[idx].u.chat, idx))
841 return;
842 dcc[idx].timeval = now;
843 if (buf[0])
844 strcpy(buf, check_tcl_filt(idx, buf));
845 if (buf[0]) {
846 /* check for beeps and cancel annoying ones */
847 v = buf;
848 d = buf;
849 while (*v)
850 switch (*v) {
851 case 7: /* beep - no more than 3 */
852 nathan++;
853 if (nathan > 3)
854 v++;
855 else
856 *d++ = *v++;
857 break;
858 case 8: /* backspace - for lame telnet's :) */
859 if (d > buf) {
860 d--;
861 }
862 v++;
863 break;
864 case 27: /* ESC - ansi code? */
865 doron = check_ansi(v);
866 /* if it's valid, append a return-to-normal code at the end */
867 if (!doron) {
868 *d++ = *v++;
869 fixed = 1;
870 } else
871 v += doron;
872 break;
873 case '\r': /* weird pseudo-linefeed */
874 v++;
875 break;
876 default:
877 *d++ = *v++;
878 }
879 if (fixed)
880 strcpy(d, "\033[0m");
881 else
882 *d = 0;
883 if (buf[0]) { /* nothing to say - maybe paging... */
884 if ((buf[0] == '.') || (dcc[idx].u.chat->channel < 0)) {
885 if (buf[0] == '.')
886 buf++;
887 v = newsplit(&buf);
888 rmspace(buf);
889 if (check_tcl_dcc(v, idx, buf)) {
890 check_tcl_chpt(botnetnick, dcc[idx].nick, dcc[idx].sock,
891 dcc[idx].u.chat->channel);
892 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
893 dprintf(idx, "*** Ja mata!\n");
894 flush_lines(idx, dcc[idx].u.chat);
895 putlog(LOG_MISC, "*", DCC_CLOSED, dcc[idx].nick,
896 dcc[idx].host);
897 if (dcc[idx].u.chat->channel >= 0) {
898 chanout_but(-1, dcc[idx].u.chat->channel,
899 "*** %s left the party line%s%s\n",
900 dcc[idx].nick, buf[0] ? ": " : ".", buf);
901 if (dcc[idx].u.chat->channel < 100000)
902 botnet_send_part_idx(idx, buf);
903 }
904 if (dcc[idx].u.chat->su_nick) {
905 dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->su_nick);
906 strcpy(dcc[idx].nick, dcc[idx].u.chat->su_nick);
907 dcc[idx].type = &DCC_CHAT;
908 if (dcc[idx].u.chat->channel < 100000)
909 botnet_send_join_idx(idx, -1);
910 dprintf(idx, "Returning to real nick %s!\r\n", dcc[idx].u.chat->su_nick);
911 nfree(dcc[idx].u.chat->su_nick);
912 dcc[idx].u.chat->su_nick = NULL;
913 /* chanout_but(-1, dcc[idx].u.chat->channel, "*** %s has rejoined the party line.\n", dcc[idx].nick); */
914 dcc_chatter(idx);
915 return;
916 } else if ((dcc[idx].sock != STDOUT) || backgrd) {
917 killsock(dcc[idx].sock);
918 lostdcc(idx);
919 return;
920 } else {
921 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
922 dcc_chatter(idx);
923 return;
924 }
925 }
926 } else if (buf[0] == ',') {
927 int me = 0;
928
929 if ((buf[1] == 'm') && (buf[2] == 'e') && buf[3] == ' ')
930 me = 1;
931 for (i = 0; i < dcc_total; i++) {
932 int ok = 0;
933
934 if (dcc[i].type->flags & DCT_MASTER) {
935 if ((dcc[i].type != &DCC_CHAT) ||
936 (dcc[i].u.chat->channel >= 0))
937 if ((i != idx) || (dcc[idx].status & STAT_ECHO))
938 ok = 1;
939 }
940 if (ok) {
941 struct userrec *u = get_user_by_handle(userlist, dcc[i].nick);
942
943 if (u && (u->flags & USER_MASTER)) {
944 if (me)
945 dprintf(i, "-> %s%s\n", dcc[idx].nick, buf + 3);
946 else
947 dprintf(i, "-%s-> %s\n", dcc[idx].nick, buf + 1);
948 }
949 }
950 }
951 } else if (buf[0] == '\'') {
952 int me = 0;
953
954 if ((buf[1] == 'm') && (buf[2] == 'e') &&
955 ((buf[3] == ' ') || (buf[3] == '\'') || (buf[3] == ',')))
956 me = 1;
957 for (i = 0; i < dcc_total; i++) {
958 if (dcc[i].type->flags & DCT_CHAT) {
959 if (me)
960 dprintf(i, "=> %s%s\n", dcc[idx].nick, buf + 3);
961 else
962 dprintf(i, "=%s=> %s\n", dcc[idx].nick, buf + 1);
963 }
964 }
965 } else {
966 if (dcc[idx].u.chat->away != NULL)
967 not_away(idx);
968 if (dcc[idx].status & STAT_ECHO)
969 chanout_but(-1, dcc[idx].u.chat->channel,
970 "<%s> %s\n", dcc[idx].nick, buf);
971 else
972 chanout_but(idx, dcc[idx].u.chat->channel, "<%s> %s\n",
973 dcc[idx].nick, buf);
974 botnet_send_chan(-1, botnetnick, dcc[idx].nick,
975 dcc[idx].u.chat->channel, buf);
976 check_tcl_chat(dcc[idx].nick, dcc[idx].u.chat->channel, buf);
977 }
978 }
979 }
980 if (dcc[idx].type == &DCC_CHAT) /* could have change to files */
981 if (dcc[idx].status & STAT_PAGE)
982 flush_lines(idx, dcc[idx].u.chat);
983 }
984
985 static void display_dcc_chat(int idx, char *buf)
986 {
987 int i = simple_sprintf(buf, "chat flags: ");
988
989 buf[i++] = dcc[idx].status & STAT_CHAT ? 'C' : 'c';
990 buf[i++] = dcc[idx].status & STAT_PARTY ? 'P' : 'p';
991 buf[i++] = dcc[idx].status & STAT_TELNET ? 'T' : 't';
992 buf[i++] = dcc[idx].status & STAT_ECHO ? 'E' : 'e';
993 buf[i++] = dcc[idx].status & STAT_PAGE ? 'P' : 'p';
994 simple_sprintf(buf + i, "/%d", dcc[idx].u.chat->channel);
995 }
996
997 struct dcc_table DCC_CHAT =
998 {
999 "CHAT",
1000 DCT_CHAT | DCT_MASTER | DCT_SHOWWHO | DCT_VALIDIDX | DCT_SIMUL |
1001 DCT_CANBOOT | DCT_REMOTEWHO,
1002 eof_dcc_chat,
1003 dcc_chat,
1004 0,
1005 0,
1006 display_dcc_chat,
1007 expmem_dcc_general,
1008 kill_dcc_general,
1009 out_dcc_general
1010 };
1011
1012 static int lasttelnets;
1013 static char lasttelnethost[81];
1014 static time_t lasttelnettime;
1015
1016 /* a modified detect_flood for incoming telnet flood protection */
1017 static int detect_telnet_flood(char *floodhost)
1018 {
1019 struct flag_record fr =
1020 {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
1021 context;
1022 get_user_flagrec(get_user_by_host(floodhost), &fr, NULL);
1023 context;
1024 if (flood_telnet_thr == 0 || (glob_friend(fr) && !par_telnet_flood))
1025 return 0; /* no flood protection */
1026 if (strcasecmp(lasttelnethost, floodhost) != 0) { /* new */
1027 strcpy(lasttelnethost, floodhost);
1028 lasttelnettime = now;
1029 lasttelnets = 0;
1030 return 0;
1031 }
1032 if (lasttelnettime < now - flood_telnet_time) {
1033 /* flood timer expired, reset it */
1034 lasttelnettime = now;
1035 lasttelnets = 0;
1036 return 0;
1037 }
1038 lasttelnets++;
1039 if (lasttelnets >= flood_telnet_thr) { /* FLOOD */
1040 /* reset counters */
1041 lasttelnets = 0;
1042 lasttelnettime = 0;
1043 lasttelnethost[0] = 0;
1044 putlog(LOG_MISC, "*", IRC_TELNETFLOOD, floodhost);
1045 addignore(floodhost, origbotname, "Telnet connection flood",
1046 now + (60 * ignore_time));
1047 return 1;
1048 }
1049 return 0;
1050 }
1051
1052 static void dcc_telnet_got_ident(int, char *);
1053
1054 static void dcc_telnet(int idx, char *buf, int i)
1055 {
1056 unsigned long ip;
1057 unsigned short port;
1058 int j = 0, sock;
1059 char s[UHOSTLEN + 1], s2[UHOSTLEN + 20];
1060
1061 context;
1062 if (dcc_total + 1 > max_dcc) {
1063 j = answer(dcc[idx].sock, s, &ip, &port, 0);
1064 if (j != -1) {
1065 dprintf(-j, "Sorry, too many connections already.\r\n");
1066 killsock(j);
1067 }
1068 return;
1069 }
1070 context;
1071 sock = answer(dcc[idx].sock, s, &ip, &port, 0);
1072 while ((sock == -1) && (errno == EAGAIN))
1073 sock = answer(sock, s, &ip, &port, 0);
1074 if (sock < 0) {
1075 neterror(s);
1076 putlog(LOG_MISC, "*", DCC_FAILED, s);
1077 killsock(sock);
1078 return;
1079 }
1080 /* <bindle> [09:37] Telnet connection: 168.246.255.191/0
1081 * <bindle> [09:37] Lost connection while identing [168.246.255.191/0]
1082 */
1083 context;
1084 /* use dcc-portrange x:x on incoming telnets to ,dw */
1085 if ((port < min_dcc_port) || (port > max_dcc_port)) {
1086 putlog(LOG_BOTS, "*", DCC_BADSRC, s, port);
1087 killsock(sock);
1088 return;
1089 }
1090 context;
1091 /* deny ips that ends with 0 or 255, dw */
1092 if ((ip & 0xff) == 0 || (ip & 0xff) == 0xff) {
1093 putlog(LOG_BOTS, "*", DCC_BADIP, s, port);
1094 killsock(sock);
1095 return;
1096 }
1097 if (dcc[idx].host[0] == '@') {
1098 /* restrict by hostname */
1099 if (!wild_match(dcc[idx].host + 1, s)) {
1100 putlog(LOG_BOTS, "*", DCC_BADHOST, s);
1101 killsock(sock);
1102 return;
1103 }
1104 }
1105 context;
1106 sprintf(s2, "telnet!telnet@%s", s);
1107 if (match_ignore(s2) || detect_telnet_flood(s2)) {
1108 killsock(sock);
1109 return;
1110 }
1111 context;
1112 i = new_dcc(&DCC_IDENTWAIT, 0);
1113 dcc[i].sock = sock;
1114 dcc[i].addr = ip;
1115 dcc[i].port = port;
1116 dcc[i].timeval = now;
1117 dcc[i].u.ident_sock = dcc[idx].sock;
1118 strncpy(dcc[i].host, s, UHOSTLEN - 1);
1119 dcc[i].host[UHOSTLEN - 1] = 0;
1120 strcpy(dcc[i].nick, "*");
1121 sock = open_telnet(s, 113);
1122 putlog(LOG_MISC, "*", DCC_TELCONN, s, port);
1123 s[0] = 0;
1124 context;
1125 if (sock < 0) {
1126 if (sock == -2)
1127 strcpy(s, "DNS lookup failed for ident");
1128 else
1129 neterror(s);
1130 } else {
1131 j = new_dcc(&DCC_IDENT, 0);
1132 if (j < 0) {
1133 killsock(sock);
1134 strcpy(s, "No Free DCC's");
1135 }
1136 }
1137 context;
1138 if (s[0]) {
1139 putlog(LOG_MISC, "*", DCC_IDENTFAIL, dcc[i].host, s);
1140 sprintf(s, "telnet@%s", dcc[i].host);
1141 dcc_telnet_got_ident(i, s);
1142 return;
1143 }
1144 context;
1145 dcc[j].sock = sock;
1146 dcc[j].port = 113;
1147 dcc[j].addr = ip;
1148 strcpy(dcc[j].host, dcc[i].host);
1149 strcpy(dcc[j].nick, "*");
1150 dcc[j].u.ident_sock = dcc[i].sock;
1151 dcc[j].timeval = now;
1152 dprintf(j, "%d, %d\n", dcc[i].port, dcc[idx].port);
1153 }
1154
1155 static void eof_dcc_telnet(int idx)
1156 {
1157 putlog(LOG_MISC, "*", DCC_PORTDIE,
1158 dcc[idx].port);
1159 killsock(dcc[idx].sock);
1160 lostdcc(idx);
1161 }
1162
1163 static void display_telnet(int idx, char *buf)
1164 {
1165 strcpy(buf, "lstn");
1166 }
1167
1168 struct dcc_table DCC_TELNET =
1169 {
1170 "TELNET",
1171 DCT_LISTEN,
1172 eof_dcc_telnet,
1173 dcc_telnet,
1174 0,
1175 0,
1176 display_telnet,
1177 0,
1178 0,
1179 0
1180 };
1181
1182 static void dcc_telnet_id(int idx, char *buf, int atr)
1183 {
1184 int ok = 0;
1185 struct flag_record fr =
1186 {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
1187
1188 context;
1189 strip_telnet(dcc[idx].sock, buf, &atr);
1190 buf[HANDLEN] = 0;
1191 /* toss out bad nicknames */
1192 if ((dcc[idx].nick[0] != '@') && (!wild_match(dcc[idx].nick, buf))) {
1193 dprintf(idx, "Sorry, that nickname format is invalid.\r\n");
1194
1195 putlog(LOG_BOTS, "*", DCC_BADNICK, dcc[idx].host);
1196 killsock(dcc[idx].sock);
1197 lostdcc(idx);
1198 return;
1199 }
1200 dcc[idx].user = get_user_by_handle(userlist, buf);
1201 get_user_flagrec(dcc[idx].user, &fr, NULL);
1202 /* make sure users-only/bots-only connects are honored */
1203 if ((dcc[idx].status & STAT_BOTONLY) && !glob_bot(fr)) {
1204 dprintf(idx, "This telnet port is for bots only.\r\n");
1205 putlog(LOG_BOTS, "*", DCC_NONBOT, dcc[idx].host);
1206 killsock(dcc[idx].sock);
1207 lostdcc(idx);
1208 return;
1209 }
1210 if ((dcc[idx].status & STAT_USRONLY) && glob_bot(fr)) {
1211 dprintf(idx, "error Only users may connect at this port.\n");
1212 putlog(LOG_BOTS, "*", DCC_NONUSER, dcc[idx].host);
1213 killsock(dcc[idx].sock);
1214 lostdcc(idx);
1215 return;
1216 }
1217 dcc[idx].status &= ~(STAT_BOTONLY | STAT_USRONLY);
1218 if ((!strcasecmp(buf, "NEW")) &&
1219 ((allow_new_telnets) || (make_userfile))) {
1220 dcc[idx].type = &DCC_TELNET_NEW;
1221 dcc[idx].timeval = now;
1222 dprintf(idx, "\r\n");
1223 dprintf(idx, IRC_TELNET, IRC_TELNET_ARGS);
1224 dprintf(idx, IRC_TELNET1);
1225 dprintf(idx, "\r\nEnter the nickname you would like to use.\r\n");
1226 return;
1227 }
1228 if (chan_op(fr)) {
1229 if (!require_p)
1230 ok = 1;
1231 }
1232 if (glob_party(fr) || glob_bot(fr))
1233 ok = 1;
1234 if (glob_xfer(fr)) {
1235 module_entry *me = module_find("filesys", 0, 0);
1236
1237 if (me && me->funcs[FILESYS_ISVALID] && (me->funcs[FILESYS_ISVALID]) ())
1238 ok = 1;
1239 }
1240 if (!ok) {
1241 dprintf(idx, "You don't have access.\r\n");
1242 putlog(LOG_BOTS, "*", DCC_INVHANDLE,
1243 dcc[idx].host, buf);
1244 killsock(dcc[idx].sock);
1245 lostdcc(idx);
1246 return;
1247 }
1248 if (glob_bot(fr)) {
1249 if (!strcasecmp(botnetnick, buf)) {
1250 dprintf(idx, "error You cannot link using my botnetnick.\n");
1251 putlog(LOG_BOTS, "*", DCC_MYBOTNETNICK, dcc[idx].host);
1252 killsock(dcc[idx].sock);
1253 lostdcc(idx);
1254 return;
1255 } else if (in_chain(buf)) {
1256 dprintf(idx, "error Already connected.\n");
1257 putlog(LOG_BOTS, "*", DCC_DUPLICATE,
1258 dcc[idx].host);
1259 killsock(dcc[idx].sock);
1260 lostdcc(idx);
1261 return;
1262 }
1263 }
1264 /* no password set? */
1265 if (u_pass_match(dcc[idx].user, "-")) {
1266 if (glob_bot(fr)) {
1267 char ps[20];
1268
1269 makepass(ps);
1270 set_user(&USERENTRY_PASS, dcc[idx].user, ps);
1271 correct_handle(buf);
1272 strcpy(dcc[idx].nick, buf);
1273 nfree(dcc[idx].u.chat);
1274 dcc[idx].u.bot = get_data_ptr(sizeof(struct bot_info));
1275
1276 dcc[idx].type = &DCC_BOT_NEW;
1277 dcc[idx].status = STAT_CALLED;
1278 dprintf(idx, "*hello!\n");
1279 greet_new_bot(idx);
1280 #ifdef NO_OLD_BOTNET
1281 dprintf(idx, "h %s\n", ps);
1282 #else
1283 dprintf(idx, "handshake %s\n", ps);
1284 #endif
1285 return;
1286 }
1287 dprintf(idx, "Can't telnet until you have a password set.\r\n");
1288 putlog(LOG_MISC, "*", DCC_NOPASS, buf, dcc[idx].host);
1289 killsock(dcc[idx].sock);
1290 lostdcc(idx);
1291 return;
1292 }
1293 ok = 0;
1294 dcc[idx].type = &DCC_CHAT_PASS;
1295 dcc[idx].timeval = now;
1296 if (glob_botmast(fr))
1297 ok = 1;
1298 else if (chan_op(fr)) {
1299 if (!require_p)
1300 ok = 1;
1301 else if (glob_party(fr))
1302 ok = 1;
1303 } else if (glob_party(fr)) {
1304 ok = 1;
1305 dcc[idx].status |= STAT_PARTY;
1306 }
1307 if (glob_bot(fr))
1308 ok = 1;
1309 if (!ok) {
1310 struct chat_info *ci;
1311
1312 ci = dcc[idx].u.chat;
1313 dcc[idx].u.file = get_data_ptr(sizeof(struct file_info));
1314
1315 dcc[idx].u.file->chat = ci;
1316 }
1317 correct_handle(buf);
1318 strcpy(dcc[idx].nick, buf);
1319
1320 if (glob_bot(fr)) {
1321 /* Must generate a string consisting of our process ID and the current
1322 * time. The bot will add it's password to the end and use it to generate
1323 * an MD5 checksum (always 128bit). The checksum is sent back and this
1324 * end does the same. The remote bot is only allowed access if the
1325 * checksums match. Please don't fuck with 'timeval', or the digest we
1326 * generate later for authentication will not be correct - you've been
1327 * warned ;) <Cybah>
1328 */
1329 putlog(LOG_BOTS, "*", "Challenging %s...", dcc[idx].nick);
1330 dprintf(idx, "passreq <%x%x@%s>\n", getpid(), dcc[idx].timeval, botnetnick);
1331 } else {
1332 /* *note* The MD5 digest used above to prevent cleartext passwords
1333 * being sent across the net will _only_ work when we have the cleartext
1334 * password. User passwords are encrypted (with blowfish usually) so the
1335 * same thing cant be done. Botnet passwords are always stored in
1336 * cleartext, or at least something that can be reversed. <Cybah>
1337 */
1338 dprintf(idx, "\n%s\377\373\001\n",DCC_ENTERPASS);
1339 /* turn off remote telnet echo: IAC WILL ECHO */
1340 }
1341 }
1342
1343 static void eof_dcc_telnet_id(int idx)
1344 {
1345 putlog(LOG_MISC, "*", DCC_LOSTCON, dcc[idx].host,
1346 dcc[idx].port);
1347 killsock(dcc[idx].sock);
1348 lostdcc(idx);
1349 }
1350
1351 static void timeout_dcc_telnet_id(int idx)
1352 {
1353 dprintf(idx, "Timeout.\n");
1354 putlog(LOG_MISC, "*", DCC_TTIMEOUT, dcc[idx].host);
1355 killsock(dcc[idx].sock);
1356 lostdcc(idx);
1357 }
1358
1359 static void display_dcc_telnet_id(int idx, char *buf)
1360 {
1361 sprintf(buf, "t-in waited %lus", now - dcc[idx].timeval);
1362 }
1363
1364 struct dcc_table DCC_TELNET_ID =
1365 {
1366 "TELNET_ID",
1367 0,
1368 eof_dcc_telnet_id,
1369 dcc_telnet_id,
1370 &password_timeout,
1371 timeout_dcc_telnet_id,
1372 display_dcc_telnet_id,
1373 expmem_dcc_general,
1374 kill_dcc_general,
1375 out_dcc_general
1376 };
1377
1378 static void dcc_telnet_new(int idx, char *buf, int x)
1379 {
1380 int ok = 1;
1381 char work[1024], *p, *q, *r;
1382
1383 buf[HANDLEN] = 0;
1384 strip_telnet(dcc[idx].sock, buf, &x);
1385 strcpy(dcc[idx].nick, buf);
1386 dcc[idx].timeval = now;
1387 for (x = 0; x < strlen(buf); x++)
1388 if ((buf[x] <= 32) || (buf[x] >= 127))
1389 ok = 0;
1390 if (!ok) {
1391 dprintf(idx, "\nYou can't use weird symbols in your nick.\n");
1392 dprintf(idx, "Try another one please:\n");
1393 } else if (strchr("-,+*=:!.@#;$", buf[0]) != NULL) {
1394 dprintf(idx, "\nYou can't start your nick with the character '%c'\n", buf[0]);
1395 dprintf(idx, "Try another one please:\n");
1396 } else if (get_user_by_handle(userlist, buf)) {
1397 dprintf(idx, "\nSorry, that nickname is taken already.\n");
1398 dprintf(idx, "Try another one please:\n");
1399 return;
1400 } else if (!strcasecmp(buf, origbotname) || !strcasecmp(buf, botnetnick)) {
1401 dprintf(idx, "Sorry, can't use my name for a nick.\n");
1402 } else {
1403 if (make_userfile)
1404 userlist = adduser(userlist, buf, "telnet!*@*", "-",
1405 sanity_check(default_flags | USER_PARTY |
1406 USER_MASTER | USER_OWNER));
1407 else {
1408 p = strchr(dcc[idx].host, '@');
1409 if (p) {
1410 q = p;
1411 *q = 0;
1412 p++;
1413 r = strchr(p, '.');
1414 if (!r)
1415 simple_sprintf(work, "telnet!%s@%s", dcc[idx].host, p);
1416 else
1417 simple_sprintf(work, "telnet!%s@*%s", dcc[idx].host, r);
1418 *q = '@';
1419 } else
1420 simple_sprintf(work, "telnet!*@*%s", dcc[idx].host);
1421 userlist = adduser(userlist, buf, work, "-",
1422 sanity_check(USER_PARTY | default_flags));
1423 }
1424 reaffirm_owners();
1425 dcc[idx].status = STAT_ECHO | STAT_TELNET;
1426 dcc[idx].type = &DCC_CHAT; /* just so next line will work */
1427 dcc[idx].user = get_user_by_handle(userlist, buf);
1428 check_dcc_attrs(dcc[idx].user, USER_PARTY | default_flags);
1429 dcc[idx].type = &DCC_TELNET_PW;
1430 if (make_userfile) {
1431 dprintf(idx, "\nYOU ARE THE MASTER/OWNER ON THIS BOT NOW\n");
1432 dprintf(idx, IRC_LIMBO);
1433 putlog(LOG_MISC, "*", DCC_INSTCOMPL,
1434 buf);
1435 make_userfile = 0;
1436 write_userfile(-1);
1437 add_note(buf, botnetnick, "Welcome to eggdrop! :)", -1, 0);
1438 }
1439 dprintf(idx, "\nOkay, now choose and enter a password:\n");
1440 dprintf(idx, "(Only the first 15 letters are significant.)\n");
1441 }
1442 }
1443
1444 static void dcc_telnet_pw(int idx, char *buf, int x)
1445 {
1446 char *newpass;
1447 int ok;
1448
1449 strip_telnet(dcc[idx].sock, buf, &x);
1450 buf[16] = 0;
1451 ok = 1;
1452 if (strlen(buf) < 4) {
1453 dprintf(idx, "\nTry to use at least 4 characters in your password.\n");
1454 dprintf(idx, "Choose and enter a password:\n");
1455 return;
1456 }
1457 for (x = 0; x < strlen(buf); x++)
1458 if ((buf[x] <= 32) || (buf[x] == 127))
1459 ok = 0;
1460 if (!ok) {
1461 dprintf(idx, "\nYou can't use weird symbols in your password.\n");
1462 dprintf(idx, "Try another one please:\n");
1463 return;
1464 }
1465 putlog(LOG_MISC, "*", DCC_NEWUSER, dcc[idx].nick,
1466 dcc[idx].host, dcc[idx].port);
1467 if (notify_new[0]) {
1468 char s[121], s1[121], s2[121];
1469
1470 sprintf(s, "Introduced to %s, %s", dcc[idx].nick, dcc[idx].host);
1471 strcpy(s1, notify_new);
1472 splitc(s2, s1, ',');
1473 while (s2[0]) {
1474 rmspace(s2);
1475 add_note(s2, botnetnick, s, -1, 0);
1476 splitc(s2, s1, ',');
1477 }
1478 rmspace(s1);
1479 add_note(s1, botnetnick, s, -1, 0);
1480 }
1481 newpass = newsplit(&buf);
1482 set_user(&USERENTRY_PASS, dcc[idx].user, newpass);
1483 dprintf(idx, "\nRemember that! You'll need it next time you log in.\n");
1484 dprintf(idx, "You now have an account on %s...\n\n\n", botnetnick);
1485 dcc[idx].type = &DCC_CHAT;
1486 dcc[idx].u.chat->channel = -2;
1487 dcc_chatter(idx);
1488 }
1489
1490 static void eof_dcc_telnet_new(int idx)
1491 {
1492 putlog(LOG_MISC, "*", DCC_LOSTNEWUSER, dcc[idx].host,
1493 dcc[idx].port);
1494 killsock(dcc[idx].sock);
1495 lostdcc(idx);
1496 }
1497
1498 static void eof_dcc_telnet_pw(int idx)
1499 {
1500 putlog(LOG_MISC, "*", DCC_LOSTNEWUSR2, dcc[idx].nick,
1501 dcc[idx].host, dcc[idx].port);
1502 deluser(dcc[idx].nick);
1503 killsock(dcc[idx].sock);
1504 lostdcc(idx);
1505 }
1506
1507 static void tout_dcc_telnet_new(int idx)
1508 {
1509 dprintf(idx, "Guess you're not there. Bye.\n");
1510 putlog(LOG_MISC, "*", DCC_TIMEOUTUSER, dcc[idx].host,
1511 dcc[idx].port);
1512 killsock(dcc[idx].sock);
1513 lostdcc(idx);
1514 }
1515
1516 static void tout_dcc_telnet_pw(int idx)
1517 {
1518 dprintf(idx, "Guess you're not there. Bye.\n");
1519 putlog(LOG_MISC, "*", DCC_TIMEOUTUSR2,
1520 dcc[idx].nick, dcc[idx].host, dcc[idx].port);
1521 killsock(dcc[idx].sock);
1522 lostdcc(idx);
1523 }
1524
1525 static void display_dcc_telnet_new(int idx, char *buf)
1526 {
1527 sprintf(buf, "new waited %lus", now - dcc[idx].timeval);
1528 }
1529
1530 static void display_dcc_telnet_pw(int idx, char *buf)
1531 {
1532 sprintf(buf, "newp waited %lus", now - dcc[idx].timeval);
1533 }
1534
1535 struct dcc_table DCC_TELNET_NEW =
1536 {
1537 "TELNET_NEW",
1538 0,
1539 eof_dcc_telnet_new,
1540 dcc_telnet_new,
1541 &password_timeout,
1542 tout_dcc_telnet_new,
1543 display_dcc_telnet_new,
1544 expmem_dcc_general,
1545 kill_dcc_general,
1546 out_dcc_general
1547 };
1548
1549 struct dcc_table DCC_TELNET_PW =
1550 {
1551 "TELNET_PW",
1552 0,
1553 eof_dcc_telnet_pw,
1554 dcc_telnet_pw,
1555 &password_timeout,
1556 tout_dcc_telnet_pw,
1557 display_dcc_telnet_pw,
1558 expmem_dcc_general,
1559 kill_dcc_general,
1560 out_dcc_general
1561 };
1562
1563 static int call_tcl_func(char *name, int idx, char *args)
1564 {
1565 char s[11];
1566
1567 set_tcl_vars();
1568 sprintf(s, "%d", idx);
1569 Tcl_SetVar(interp, "_n", s, 0);
1570 Tcl_SetVar(interp, "_a", args, 0);
1571 if (Tcl_VarEval(interp, name, " $_n $_a", NULL) == TCL_ERROR) {
1572 putlog(LOG_MISC, "*", DCC_TCLERROR, name, interp->result);
1573 return -1;
1574 }
1575 return (atoi(interp->result));
1576 }
1577
1578 static void dcc_script(int idx, char *buf, int len)
1579 {
1580 void *old = NULL;
1581 long oldsock = dcc[idx].sock;
1582
1583 strip_telnet(dcc[idx].sock, buf, &len);
1584 if (!len)
1585 return;
1586 dcc[idx].timeval = now;
1587 set_tcl_vars();
1588 if (call_tcl_func(dcc[idx].u.script->command, dcc[idx].sock, buf)) {
1589 context;
1590 if ((dcc[idx].sock != oldsock) || (idx>max_dcc))
1591 return; /* drummer: this happen after killdcc */
1592 old = dcc[idx].u.script->u.other;
1593 dcc[idx].type = dcc[idx].u.script->type;
1594 nfree(dcc[idx].u.script);
1595 dcc[idx].u.other = old;
1596 if (dcc[idx].type == &DCC_SOCKET) {
1597 /* kill the whole thing off */
1598 killsock(dcc[idx].sock);
1599 lostdcc(idx);
1600 return;
1601 }
1602 if (dcc[idx].type == &DCC_CHAT) {
1603 if (dcc[idx].u.chat->channel >= 0) {
1604 chanout_but(-1, dcc[idx].u.chat->channel,DCC_JOIN, dcc[idx].nick);
1605 context;
1606 if (dcc[idx].u.chat->channel < 10000)
1607 botnet_send_join_idx(idx, -1);
1608 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
1609 geticon(idx), dcc[idx].sock, dcc[idx].host);
1610 }
1611 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
1612 }
1613 }
1614 }
1615
1616 static void eof_dcc_script(int idx)
1617 {
1618 void *old;
1619 int oldflags;
1620
1621 context;
1622 /* This will stop a killdcc from working, incase the script tries
1623 * to kill it's controlling socket while handling an EOF <cybah> */
1624 oldflags = dcc[idx].type->flags;
1625 dcc[idx].type->flags &= ~(DCT_VALIDIDX);
1626 /* tell the script they're gone: */
1627 call_tcl_func(dcc[idx].u.script->command, dcc[idx].sock, "");
1628 /* restore the flags */
1629 dcc[idx].type->flags = oldflags;
1630 context;
1631 old = dcc[idx].u.script->u.other;
1632 dcc[idx].type = dcc[idx].u.script->type;
1633 nfree(dcc[idx].u.script);
1634 dcc[idx].u.other = old;
1635 /* then let it fall thru to the real one */
1636 if (dcc[idx].type && dcc[idx].type->eof)
1637 dcc[idx].type->eof(idx);
1638 else {
1639 putlog(LOG_MISC, "*",
1640 DCC_DEADSOCKET,
1641 dcc[idx].sock, dcc[idx].type->name);
1642 killsock(dcc[idx].sock);
1643 lostdcc(idx);
1644 }
1645 }
1646
1647 static void display_dcc_script(int idx, char *buf)
1648 {
1649 sprintf(buf, "scri %s", dcc[idx].u.script->command);
1650 }
1651
1652 static int expmem_dcc_script(void *x)
1653 {
1654 register struct script_info *p = (struct script_info *) x;
1655 int tot = sizeof(struct script_info);
1656
1657 if (p->type && p->u.other)
1658 tot += p->type->expmem(p->u.other);
1659 return tot;
1660 }
1661
1662 static void kill_dcc_script(int idx, void *x)
1663 {
1664 register struct script_info *p = (struct script_info *) x;
1665
1666 if (p->type && p->u.other)
1667 p->type->kill(idx, p->u.other);
1668 nfree(p);
1669 }
1670
1671 static void out_dcc_script(int idx, char *buf, void *x)
1672 {
1673 register struct script_info *p = (struct script_info *) x;
1674
1675 if (p && p->type && p->u.other)
1676 p->type->output(idx, buf, p->u.other);
1677 else
1678 tputs(dcc[idx].sock, buf, strlen(buf));
1679 }
1680
1681 struct dcc_table DCC_SCRIPT =
1682 {
1683 "SCRIPT",
1684 DCT_VALIDIDX,
1685 eof_dcc_script,
1686 dcc_script,
1687 0,
1688 0,
1689 display_dcc_script,
1690 expmem_dcc_script,
1691 kill_dcc_script,
1692 out_dcc_script
1693 };
1694
1695 static void dcc_socket(int idx, char *buf, int len)
1696 {
1697 }
1698
1699 static void eof_dcc_socket(int idx)
1700 {
1701 killsock(dcc[idx].sock);
1702 lostdcc(idx);
1703 }
1704
1705 static void display_dcc_socket(int idx, char *buf)
1706 {
1707 strcpy(buf, "sock (stranded)");
1708 }
1709
1710 struct dcc_table DCC_SOCKET =
1711 {
1712 "SOCKET",
1713 DCT_VALIDIDX,
1714 eof_dcc_socket,
1715 dcc_socket,
1716 0,
1717 0,
1718 display_dcc_socket,
1719 0,
1720 0,
1721 0
1722 };
1723
1724 static void display_dcc_lost(int idx, char *buf)
1725 {
1726 strcpy(buf, "lost");
1727 }
1728
1729 struct dcc_table DCC_LOST =
1730 {
1731 "LOST",
1732 0,
1733 0,
1734 dcc_socket,
1735 0,
1736 0,
1737 display_dcc_lost,
1738 0,
1739 0,
1740 0
1741 };
1742
1743 void dcc_identwait(int idx, char *buf, int len)
1744 {
1745 /* ignore anything now */
1746 context;
1747 }
1748
1749 void eof_dcc_identwait(int idx)
1750 {
1751 int i;
1752
1753 putlog(LOG_MISC, "*", DCC_LOSTCONN,
1754 dcc[idx].host, dcc[idx].port);
1755 for (i = 0; i < dcc_total; i++)
1756 if ((dcc[i].type == &DCC_IDENT) &&
1757 (dcc[i].u.ident_sock == dcc[idx].sock)) {
1758 dcc[i].u.other = 0;
1759 killsock(dcc[i].sock); /* cleanup ident socket */
1760 }
1761 killsock(dcc[idx].sock); /* cleanup waiting socket */
1762 dcc[idx].u.other = 0;
1763 lostdcc(idx);
1764 }
1765
1766 static void display_dcc_identwait(int idx, char *buf)
1767 {
1768 sprintf(buf, "idtw waited %lus", now - dcc[idx].timeval);
1769 }
1770
1771 struct dcc_table DCC_IDENTWAIT =
1772 {
1773 "IDENTWAIT",
1774 0,
1775 eof_dcc_identwait,
1776 dcc_identwait,
1777 0,
1778 0,
1779 display_dcc_identwait,
1780 0,
1781 0,
1782 0
1783 };
1784
1785 void dcc_ident(int idx, char *buf, int len)
1786 {
1787 char response[512], uid[512], buf1[UHOSTLEN];
1788 int i, sock = dcc[idx].sock;
1789
1790 context;
1791 sscanf(buf, "%*[^:]:%[^:]:%*[^:]:%[^\n]\n", response, uid);
1792 rmspace(response);
1793 if (response[0] != 'U') {
1794 dcc[idx].timeval = now;
1795 return;
1796 }
1797 rmspace(uid);
1798 uid[20] = 0; /* 20 character ident max */
1799 for (i = 0; i < dcc_total; i++)
1800 if ((dcc[i].type == &DCC_IDENTWAIT) &&
1801 (dcc[i].sock == dcc[idx].u.ident_sock)) {
1802 simple_sprintf(buf1, "%s@%s", uid, dcc[idx].host);
1803 dcc_telnet_got_ident(i, buf1);
1804 }
1805 idx = findanyidx(sock);
1806 dcc[idx].u.other = 0;
1807 killsock(dcc[idx].sock);
1808 lostdcc(idx);
1809 }
1810
1811 void eof_dcc_ident(int idx)
1812 {
1813 char buf[UHOSTLEN];
1814 int i, sock = dcc[idx].sock;
1815
1816 for (i = 0; i < dcc_total; i++)
1817 if ((dcc[i].type == &DCC_IDENTWAIT) &&
1818 (dcc[i].sock == dcc[idx].u.ident_sock)) {
1819 putlog(LOG_MISC, "*", DCC_EOFIDENT);
1820 simple_sprintf(buf, "telnet@%s", dcc[idx].host);
1821 dcc_telnet_got_ident(i, buf);
1822 }
1823 idx = findanyidx(sock); /* sanity */
1824 killsock(dcc[idx].sock);
1825 dcc[idx].u.other = 0;
1826 lostdcc(idx);
1827 }
1828
1829 static void display_dcc_ident(int idx, char *buf)
1830 {
1831 sprintf(buf, "idnt (sock %d)", dcc[idx].u.ident_sock);
1832 }
1833
1834 struct dcc_table DCC_IDENT =
1835 {
1836 "IDENT",
1837 0,
1838 eof_dcc_ident,
1839 dcc_ident,
1840 &identtimeout,
1841 eof_dcc_ident,
1842 display_dcc_ident,
1843 0,
1844 0,
1845 0
1846 };
1847
1848 void dcc_telnet_got_ident(int i, char *host)
1849 {
1850 int idx;
1851 char x[1024];
1852
1853 for (idx = 0; idx < dcc_total; idx++)
1854 if ((dcc[idx].type == &DCC_TELNET) &&
1855 (dcc[idx].sock == dcc[i].u.ident_sock))
1856 break;
1857 dcc[i].u.other = 0;
1858 if (dcc_total == idx)
1859 putlog(LOG_MISC, "*", DCC_LOSTIDENT);
1860 strncpy(dcc[i].host, host, UHOSTLEN);
1861 dcc[i].host[UHOSTLEN] = 0;
1862 simple_sprintf(x, "telnet!%s", dcc[i].host);
1863 if (protect_telnet && !make_userfile) {
1864 struct userrec *u;
1865 int ok = 1;
1866
1867 context;
1868 u = get_user_by_host(x);
1869 /* not a user or +p & require p OR +o */
1870 if (!u)
1871 ok = 0;
1872 else if (require_p && !(u->flags & USER_PARTY))
1873 ok = 0;
1874 else if (!require_p && !(u->flags & USER_OP))
1875 ok = 0;
1876 if (!ok && u && (u->flags & USER_BOT))
1877 ok = 1;
1878 if (!ok) {
1879 putlog(LOG_MISC, "*", DCC_NOACCESS, dcc[i].host);
1880 killsock(dcc[i].sock);
1881 lostdcc(i);
1882 return;
1883 }
1884 }
1885 context;
1886 if (match_ignore(x)) {
1887 killsock(dcc[i].sock);
1888 lostdcc(i);
1889 return;
1890 }
1891 /* script? */
1892 if (!strcmp(dcc[idx].nick, "(script)")) {
1893 dcc[i].type = &DCC_SOCKET;
1894 dcc[i].u.other = NULL;
1895 strcpy(dcc[i].nick, "*");
1896 check_tcl_listen(dcc[idx].host, dcc[i].sock);
1897 return;
1898 }
1899 dcc[i].type = &DCC_TELNET_ID;
1900 dcc[i].u.chat = get_data_ptr(sizeof(struct chat_info));
1901 bzero(dcc[i].u.chat, sizeof(struct chat_info));
1902
1903 /* copy acceptable-nick/host mask */
1904 dcc[i].status = STAT_TELNET | STAT_ECHO;
1905 if (!strcmp(dcc[idx].nick, "(bots)"))
1906 dcc[i].status |= STAT_BOTONLY;
1907 if (!strcmp(dcc[idx].nick, "(users)"))
1908 dcc[i].status |= STAT_USRONLY;
1909 /* copy acceptable-nick/host mask */
1910 strncpy(dcc[i].nick, dcc[idx].host, HANDLEN);
1911 dcc[i].nick[HANDLEN] = 0;
1912 dcc[i].timeval = now;
1913 strcpy(dcc[i].u.chat->con_chan, chanset ? chanset->name : "*");
1914 /* Displays a customizable banner. [seC] */
1915 if (use_telnet_banner)
1916 show_banner(i);
1917 /* This is so we dont tell someone doing a portscan anything
1918 * about ourselves. <cybah> */
1919 if (stealth_telnets)
1920 sub_lang(i, MISC_BANNER_STEALTH);
1921 else {
1922 dprintf(i, "\r\n\r\n");
1923 sub_lang(i, MISC_BANNER);
1924 }
1925 if (allow_new_telnets)
1926 dprintf(i, "(If you are new, enter 'NEW' here.)\r\n");
1927 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23