/[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.7 - (show annotations) (download) (as text)
Tue Aug 24 07:55:51 1999 UTC (20 years, 3 months ago) by guppy
Branch: MAIN
Changes since 1.6: +120 -11 lines
File MIME type: text/x-chdr
blah

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 if (check_tcl_dcc(v, idx, buf)) {
889 check_tcl_chpt(botnetnick, dcc[idx].nick, dcc[idx].sock,
890 dcc[idx].u.chat->channel);
891 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
892 dprintf(idx, "*** Ja mata!\n");
893 flush_lines(idx, dcc[idx].u.chat);
894 putlog(LOG_MISC, "*", DCC_CLOSED, dcc[idx].nick,
895 dcc[idx].host);
896 if (dcc[idx].u.chat->channel >= 0) {
897 chanout_but(-1, dcc[idx].u.chat->channel,
898 "*** %s left the party line%s%s\n",
899 dcc[idx].nick, buf[0] ? ": " : ".", buf);
900 if (dcc[idx].u.chat->channel < 100000)
901 botnet_send_part_idx(idx, buf);
902 }
903 if (dcc[idx].u.chat->su_nick) {
904 dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->su_nick);
905 strcpy(dcc[idx].nick, dcc[idx].u.chat->su_nick);
906 dcc[idx].type = &DCC_CHAT;
907 if (dcc[idx].u.chat->channel < 100000)
908 botnet_send_join_idx(idx, -1);
909 dprintf(idx, "Returning to real nick %s!\r\n", dcc[idx].u.chat->su_nick);
910 nfree(dcc[idx].u.chat->su_nick);
911 dcc[idx].u.chat->su_nick = NULL;
912 /* chanout_but(-1, dcc[idx].u.chat->channel, "*** %s has rejoined the party line.\n", dcc[idx].nick); */
913 dcc_chatter(idx);
914 return;
915 } else if ((dcc[idx].sock != STDOUT) || backgrd) {
916 killsock(dcc[idx].sock);
917 lostdcc(idx);
918 return;
919 } else {
920 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
921 dcc_chatter(idx);
922 return;
923 }
924 }
925 } else if (buf[0] == ',') {
926 int me = 0;
927
928 if ((buf[1] == 'm') && (buf[2] == 'e') && buf[3] == ' ')
929 me = 1;
930 for (i = 0; i < dcc_total; i++) {
931 int ok = 0;
932
933 if (dcc[i].type->flags & DCT_MASTER) {
934 if ((dcc[i].type != &DCC_CHAT) ||
935 (dcc[i].u.chat->channel >= 0))
936 if ((i != idx) || (dcc[idx].status & STAT_ECHO))
937 ok = 1;
938 }
939 if (ok) {
940 struct userrec *u = get_user_by_handle(userlist, dcc[i].nick);
941
942 if (u && (u->flags & USER_MASTER)) {
943 if (me)
944 dprintf(i, "-> %s%s\n", dcc[idx].nick, buf + 3);
945 else
946 dprintf(i, "-%s-> %s\n", dcc[idx].nick, buf + 1);
947 }
948 }
949 }
950 } else if (buf[0] == '\'') {
951 int me = 0;
952
953 if ((buf[1] == 'm') && (buf[2] == 'e') &&
954 ((buf[3] == ' ') || (buf[3] == '\'') || (buf[3] == ',')))
955 me = 1;
956 for (i = 0; i < dcc_total; i++) {
957 if (dcc[i].type->flags & DCT_CHAT) {
958 if (me)
959 dprintf(i, "=> %s%s\n", dcc[idx].nick, buf + 3);
960 else
961 dprintf(i, "=%s=> %s\n", dcc[idx].nick, buf + 1);
962 }
963 }
964 } else {
965 if (dcc[idx].u.chat->away != NULL)
966 not_away(idx);
967 if (dcc[idx].status & STAT_ECHO)
968 chanout_but(-1, dcc[idx].u.chat->channel,
969 "<%s> %s\n", dcc[idx].nick, buf);
970 else
971 chanout_but(idx, dcc[idx].u.chat->channel, "<%s> %s\n",
972 dcc[idx].nick, buf);
973 botnet_send_chan(-1, botnetnick, dcc[idx].nick,
974 dcc[idx].u.chat->channel, buf);
975 check_tcl_chat(dcc[idx].nick, dcc[idx].u.chat->channel, buf);
976 }
977 }
978 }
979 if (dcc[idx].type == &DCC_CHAT) /* could have change to files */
980 if (dcc[idx].status & STAT_PAGE)
981 flush_lines(idx, dcc[idx].u.chat);
982 }
983
984 static void display_dcc_chat(int idx, char *buf)
985 {
986 int i = simple_sprintf(buf, "chat flags: ");
987
988 buf[i++] = dcc[idx].status & STAT_CHAT ? 'C' : 'c';
989 buf[i++] = dcc[idx].status & STAT_PARTY ? 'P' : 'p';
990 buf[i++] = dcc[idx].status & STAT_TELNET ? 'T' : 't';
991 buf[i++] = dcc[idx].status & STAT_ECHO ? 'E' : 'e';
992 buf[i++] = dcc[idx].status & STAT_PAGE ? 'P' : 'p';
993 simple_sprintf(buf + i, "/%d", dcc[idx].u.chat->channel);
994 }
995
996 struct dcc_table DCC_CHAT =
997 {
998 "CHAT",
999 DCT_CHAT | DCT_MASTER | DCT_SHOWWHO | DCT_VALIDIDX | DCT_SIMUL |
1000 DCT_CANBOOT | DCT_REMOTEWHO,
1001 eof_dcc_chat,
1002 dcc_chat,
1003 0,
1004 0,
1005 display_dcc_chat,
1006 expmem_dcc_general,
1007 kill_dcc_general,
1008 out_dcc_general
1009 };
1010
1011 static int lasttelnets;
1012 static char lasttelnethost[81];
1013 static time_t lasttelnettime;
1014
1015 /* a modified detect_flood for incoming telnet flood protection */
1016 static int detect_telnet_flood(char *floodhost)
1017 {
1018 struct flag_record fr =
1019 {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
1020 context;
1021 get_user_flagrec(get_user_by_host(floodhost), &fr, NULL);
1022 context;
1023 if (flood_telnet_thr == 0 || (glob_friend(fr) && !par_telnet_flood))
1024 return 0; /* no flood protection */
1025 if (strcasecmp(lasttelnethost, floodhost) != 0) { /* new */
1026 strcpy(lasttelnethost, floodhost);
1027 lasttelnettime = now;
1028 lasttelnets = 0;
1029 return 0;
1030 }
1031 if (lasttelnettime < now - flood_telnet_time) {
1032 /* flood timer expired, reset it */
1033 lasttelnettime = now;
1034 lasttelnets = 0;
1035 return 0;
1036 }
1037 lasttelnets++;
1038 if (lasttelnets >= flood_telnet_thr) { /* FLOOD */
1039 /* reset counters */
1040 lasttelnets = 0;
1041 lasttelnettime = 0;
1042 lasttelnethost[0] = 0;
1043 putlog(LOG_MISC, "*", IRC_TELNETFLOOD, floodhost);
1044 addignore(floodhost, origbotname, "Telnet connection flood",
1045 now + (60 * ignore_time));
1046 return 1;
1047 }
1048 return 0;
1049 }
1050
1051 static void dcc_telnet_got_ident(int, char *);
1052
1053 static void dcc_telnet(int idx, char *buf, int i)
1054 {
1055 unsigned long ip;
1056 unsigned short port;
1057 int j = 0, sock;
1058 char s[UHOSTLEN + 1], s2[UHOSTLEN + 20];
1059
1060 context;
1061 if (dcc_total + 1 > max_dcc) {
1062 j = answer(dcc[idx].sock, s, &ip, &port, 0);
1063 if (j != -1) {
1064 dprintf(-j, "Sorry, too many connections already.\r\n");
1065 killsock(j);
1066 }
1067 return;
1068 }
1069 context;
1070 sock = answer(dcc[idx].sock, s, &ip, &port, 0);
1071 while ((sock == -1) && (errno == EAGAIN))
1072 sock = answer(sock, s, &ip, &port, 0);
1073 if (sock < 0) {
1074 neterror(s);
1075 putlog(LOG_MISC, "*", DCC_FAILED, s);
1076 killsock(sock);
1077 return;
1078 }
1079 /* <bindle> [09:37] Telnet connection: 168.246.255.191/0
1080 * <bindle> [09:37] Lost connection while identing [168.246.255.191/0]
1081 */
1082 context;
1083 /* use dcc-portrange x:x on incoming telnets to ,dw */
1084 if ((port < min_dcc_port) || (port > max_dcc_port)) {
1085 putlog(LOG_BOTS, "*", DCC_BADSRC, s, port);
1086 killsock(sock);
1087 return;
1088 }
1089 context;
1090 /* deny ips that ends with 0 or 255, dw */
1091 if ((ip & 0xff) == 0 || (ip & 0xff) == 0xff) {
1092 putlog(LOG_BOTS, "*", DCC_BADIP, s, port);
1093 killsock(sock);
1094 return;
1095 }
1096 if (dcc[idx].host[0] == '@') {
1097 /* restrict by hostname */
1098 if (!wild_match(dcc[idx].host + 1, s)) {
1099 putlog(LOG_BOTS, "*", DCC_BADHOST, s);
1100 killsock(sock);
1101 return;
1102 }
1103 }
1104 context;
1105 sprintf(s2, "telnet!telnet@%s", s);
1106 if (match_ignore(s2) || detect_telnet_flood(s2)) {
1107 killsock(sock);
1108 return;
1109 }
1110 context;
1111 i = new_dcc(&DCC_IDENTWAIT, 0);
1112 dcc[i].sock = sock;
1113 dcc[i].addr = ip;
1114 dcc[i].port = port;
1115 dcc[i].timeval = now;
1116 dcc[i].u.ident_sock = dcc[idx].sock;
1117 strncpy(dcc[i].host, s, UHOSTLEN - 1);
1118 dcc[i].host[UHOSTLEN - 1] = 0;
1119 strcpy(dcc[i].nick, "*");
1120 sock = open_telnet(s, 113);
1121 putlog(LOG_MISC, "*", DCC_TELCONN, s, port);
1122 s[0] = 0;
1123 context;
1124 if (sock < 0) {
1125 if (sock == -2)
1126 strcpy(s, "DNS lookup failed for ident");
1127 else
1128 neterror(s);
1129 } else {
1130 j = new_dcc(&DCC_IDENT, 0);
1131 if (j < 0) {
1132 killsock(sock);
1133 strcpy(s, "No Free DCC's");
1134 }
1135 }
1136 context;
1137 if (s[0]) {
1138 putlog(LOG_MISC, "*", DCC_IDENTFAIL, dcc[i].host, s);
1139 sprintf(s, "telnet@%s", dcc[i].host);
1140 dcc_telnet_got_ident(i, s);
1141 return;
1142 }
1143 context;
1144 dcc[j].sock = sock;
1145 dcc[j].port = 113;
1146 dcc[j].addr = ip;
1147 strcpy(dcc[j].host, dcc[i].host);
1148 strcpy(dcc[j].nick, "*");
1149 dcc[j].u.ident_sock = dcc[i].sock;
1150 dcc[j].timeval = now;
1151 dprintf(j, "%d, %d\n", dcc[i].port, dcc[idx].port);
1152 }
1153
1154 static void eof_dcc_telnet(int idx)
1155 {
1156 putlog(LOG_MISC, "*", DCC_PORTDIE,
1157 dcc[idx].port);
1158 killsock(dcc[idx].sock);
1159 lostdcc(idx);
1160 }
1161
1162 static void display_telnet(int idx, char *buf)
1163 {
1164 strcpy(buf, "lstn");
1165 }
1166
1167 struct dcc_table DCC_TELNET =
1168 {
1169 "TELNET",
1170 DCT_LISTEN,
1171 eof_dcc_telnet,
1172 dcc_telnet,
1173 0,
1174 0,
1175 display_telnet,
1176 0,
1177 0,
1178 0
1179 };
1180
1181 static void dcc_telnet_id(int idx, char *buf, int atr)
1182 {
1183 int ok = 0;
1184 struct flag_record fr =
1185 {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
1186
1187 context;
1188 strip_telnet(dcc[idx].sock, buf, &atr);
1189 buf[HANDLEN] = 0;
1190 /* toss out bad nicknames */
1191 if ((dcc[idx].nick[0] != '@') && (!wild_match(dcc[idx].nick, buf))) {
1192 dprintf(idx, "Sorry, that nickname format is invalid.\r\n");
1193
1194 putlog(LOG_BOTS, "*", DCC_BADNICK, dcc[idx].host);
1195 killsock(dcc[idx].sock);
1196 lostdcc(idx);
1197 return;
1198 }
1199 dcc[idx].user = get_user_by_handle(userlist, buf);
1200 get_user_flagrec(dcc[idx].user, &fr, NULL);
1201 /* make sure users-only/bots-only connects are honored */
1202 if ((dcc[idx].status & STAT_BOTONLY) && !glob_bot(fr)) {
1203 dprintf(idx, "This telnet port is for bots only.\r\n");
1204 putlog(LOG_BOTS, "*", DCC_NONBOT, dcc[idx].host);
1205 killsock(dcc[idx].sock);
1206 lostdcc(idx);
1207 return;
1208 }
1209 if ((dcc[idx].status & STAT_USRONLY) && glob_bot(fr)) {
1210 dprintf(idx, "error Only users may connect at this port.\n");
1211 putlog(LOG_BOTS, "*", DCC_NONUSER, dcc[idx].host);
1212 killsock(dcc[idx].sock);
1213 lostdcc(idx);
1214 return;
1215 }
1216 dcc[idx].status &= ~(STAT_BOTONLY | STAT_USRONLY);
1217 if ((!strcasecmp(buf, "NEW")) &&
1218 ((allow_new_telnets) || (make_userfile))) {
1219 dcc[idx].type = &DCC_TELNET_NEW;
1220 dcc[idx].timeval = now;
1221 dprintf(idx, "\r\n");
1222 dprintf(idx, IRC_TELNET, IRC_TELNET_ARGS);
1223 dprintf(idx, IRC_TELNET1);
1224 dprintf(idx, "\r\nEnter the nickname you would like to use.\r\n");
1225 return;
1226 }
1227 if (chan_op(fr)) {
1228 if (!require_p)
1229 ok = 1;
1230 }
1231 if (glob_party(fr) || glob_bot(fr))
1232 ok = 1;
1233 if (glob_xfer(fr)) {
1234 module_entry *me = module_find("filesys", 0, 0);
1235
1236 if (me && me->funcs[FILESYS_ISVALID] && (me->funcs[FILESYS_ISVALID]) ())
1237 ok = 1;
1238 }
1239 if (!ok) {
1240 dprintf(idx, "You don't have access.\r\n");
1241 putlog(LOG_BOTS, "*", DCC_INVHANDLE,
1242 dcc[idx].host, buf);
1243 killsock(dcc[idx].sock);
1244 lostdcc(idx);
1245 return;
1246 }
1247 if (glob_bot(fr)) {
1248 if (in_chain(buf)) {
1249 dprintf(idx, "error Already connected.\n");
1250 putlog(LOG_BOTS, "*", DCC_DUPLICATE,
1251 dcc[idx].host);
1252 killsock(dcc[idx].sock);
1253 lostdcc(idx);
1254 return;
1255 }
1256 }
1257 /* no password set? */
1258 if (u_pass_match(dcc[idx].user, "-")) {
1259 if (glob_bot(fr)) {
1260 char ps[20];
1261
1262 makepass(ps);
1263 set_user(&USERENTRY_PASS, dcc[idx].user, ps);
1264 correct_handle(buf);
1265 strcpy(dcc[idx].nick, buf);
1266 nfree(dcc[idx].u.chat);
1267 dcc[idx].u.bot = get_data_ptr(sizeof(struct bot_info));
1268
1269 dcc[idx].type = &DCC_BOT_NEW;
1270 dcc[idx].status = STAT_CALLED;
1271 dprintf(idx, "*hello!\n");
1272 greet_new_bot(idx);
1273 #ifdef NO_OLD_BOTNET
1274 dprintf(idx, "h %s\n", ps);
1275 #else
1276 dprintf(idx, "handshake %s\n", ps);
1277 #endif
1278 return;
1279 }
1280 dprintf(idx, "Can't telnet until you have a password set.\r\n");
1281 putlog(LOG_MISC, "*", DCC_NOPASS, buf, dcc[idx].host);
1282 killsock(dcc[idx].sock);
1283 lostdcc(idx);
1284 return;
1285 }
1286 ok = 0;
1287 dcc[idx].type = &DCC_CHAT_PASS;
1288 dcc[idx].timeval = now;
1289 if (glob_botmast(fr))
1290 ok = 1;
1291 else if (chan_op(fr)) {
1292 if (!require_p)
1293 ok = 1;
1294 else if (glob_party(fr))
1295 ok = 1;
1296 } else if (glob_party(fr)) {
1297 ok = 1;
1298 dcc[idx].status |= STAT_PARTY;
1299 }
1300 if (glob_bot(fr))
1301 ok = 1;
1302 if (!ok) {
1303 struct chat_info *ci;
1304
1305 ci = dcc[idx].u.chat;
1306 dcc[idx].u.file = get_data_ptr(sizeof(struct file_info));
1307
1308 dcc[idx].u.file->chat = ci;
1309 }
1310 correct_handle(buf);
1311 strcpy(dcc[idx].nick, buf);
1312
1313 if (glob_bot(fr)) {
1314 /* Must generate a string consisting of our process ID and the current
1315 * time. The bot will add it's password to the end and use it to generate
1316 * an MD5 checksum (always 128bit). The checksum is sent back and this
1317 * end does the same. The remote bot is only allowed access if the
1318 * checksums match. Please don't fuck with 'timeval', or the digest we
1319 * generate later for authentication will not be correct - you've been
1320 * warned ;) <Cybah>
1321 */
1322 putlog(LOG_BOTS, "*", "Challenging %s...", dcc[idx].nick);
1323 dprintf(idx, "passreq <%x%x@%s>\n", getpid(), dcc[idx].timeval, botnetnick);
1324 } else {
1325 /* *note* The MD5 digest used above to prevent cleartext passwords
1326 * being sent across the net will _only_ work when we have the cleartext
1327 * password. User passwords are encrypted (with blowfish usually) so the
1328 * same thing cant be done. Botnet passwords are always stored in
1329 * cleartext, or at least something that can be reversed. <Cybah>
1330 */
1331 dprintf(idx, "\nEnter your password.\377\373\001\n");
1332 /* turn off remote telnet echo: IAC WILL ECHO */
1333 }
1334 }
1335
1336 static void eof_dcc_telnet_id(int idx)
1337 {
1338 putlog(LOG_MISC, "*", DCC_LOSTCON, dcc[idx].host,
1339 dcc[idx].port);
1340 killsock(dcc[idx].sock);
1341 lostdcc(idx);
1342 }
1343
1344 static void timeout_dcc_telnet_id(int idx)
1345 {
1346 dprintf(idx, "Timeout.\n");
1347 putlog(LOG_MISC, "*", DCC_TTIMEOUT, dcc[idx].host);
1348 killsock(dcc[idx].sock);
1349 lostdcc(idx);
1350 }
1351
1352 static void display_dcc_telnet_id(int idx, char *buf)
1353 {
1354 sprintf(buf, "t-in waited %lus", now - dcc[idx].timeval);
1355 }
1356
1357 struct dcc_table DCC_TELNET_ID =
1358 {
1359 "TELNET_ID",
1360 0,
1361 eof_dcc_telnet_id,
1362 dcc_telnet_id,
1363 &password_timeout,
1364 timeout_dcc_telnet_id,
1365 display_dcc_telnet_id,
1366 expmem_dcc_general,
1367 kill_dcc_general,
1368 out_dcc_general
1369 };
1370
1371 static void dcc_telnet_new(int idx, char *buf, int x)
1372 {
1373 int ok = 1;
1374 char work[1024], *p, *q, *r;
1375
1376 buf[HANDLEN] = 0;
1377 strip_telnet(dcc[idx].sock, buf, &x);
1378 strcpy(dcc[idx].nick, buf);
1379 dcc[idx].timeval = now;
1380 for (x = 0; x < strlen(buf); x++)
1381 if ((buf[x] <= 32) || (buf[x] >= 127))
1382 ok = 0;
1383 if (!ok) {
1384 dprintf(idx, "\nYou can't use weird symbols in your nick.\n");
1385 dprintf(idx, "Try another one please:\n");
1386 } else if (strchr("-,+*=:!.@#;$", buf[0]) != NULL) {
1387 dprintf(idx, "\nYou can't start your nick with the character '%c'\n", buf[0]);
1388 dprintf(idx, "Try another one please:\n");
1389 } else if (get_user_by_handle(userlist, buf)) {
1390 dprintf(idx, "\nSorry, that nickname is taken already.\n");
1391 dprintf(idx, "Try another one please:\n");
1392 return;
1393 } else if (!strcasecmp(buf, origbotname) || !strcasecmp(buf, botnetnick)) {
1394 dprintf(idx, "Sorry, can't use my name for a nick.\n");
1395 } else {
1396 if (make_userfile)
1397 userlist = adduser(userlist, buf, "telnet!*@*", "-",
1398 sanity_check(default_flags | USER_PARTY |
1399 USER_MASTER | USER_OWNER));
1400 else {
1401 p = strchr(dcc[idx].host, '@');
1402 if (p) {
1403 q = p;
1404 *q = 0;
1405 p++;
1406 r = strchr(p, '.');
1407 if (!r)
1408 simple_sprintf(work, "telnet!%s@%s", dcc[idx].host, p);
1409 else
1410 simple_sprintf(work, "telnet!%s@*%s", dcc[idx].host, r);
1411 *q = '@';
1412 } else
1413 simple_sprintf(work, "telnet!*@*%s", dcc[idx].host);
1414 userlist = adduser(userlist, buf, work, "-",
1415 sanity_check(USER_PARTY | default_flags));
1416 }
1417 reaffirm_owners();
1418 dcc[idx].status = STAT_ECHO | STAT_TELNET;
1419 dcc[idx].type = &DCC_CHAT; /* just so next line will work */
1420 dcc[idx].user = get_user_by_handle(userlist, buf);
1421 check_dcc_attrs(dcc[idx].user, USER_PARTY | default_flags);
1422 dcc[idx].type = &DCC_TELNET_PW;
1423 if (make_userfile) {
1424 dprintf(idx, "\nYOU ARE THE MASTER/OWNER ON THIS BOT NOW\n");
1425 dprintf(idx, IRC_LIMBO);
1426 putlog(LOG_MISC, "*", DCC_INSTCOMPL,
1427 buf);
1428 make_userfile = 0;
1429 write_userfile(-1);
1430 add_note(buf, botnetnick, "Welcome to eggdrop! :)", -1, 0);
1431 }
1432 dprintf(idx, "\nOkay, now choose and enter a password:\n");
1433 dprintf(idx, "(Only the first 15 letters are significant.)\n");
1434 }
1435 }
1436
1437 static void dcc_telnet_pw(int idx, char *buf, int x)
1438 {
1439 char *newpass;
1440 int ok;
1441
1442 strip_telnet(dcc[idx].sock, buf, &x);
1443 buf[16] = 0;
1444 ok = 1;
1445 if (strlen(buf) < 4) {
1446 dprintf(idx, "\nTry to use at least 4 characters in your password.\n");
1447 dprintf(idx, "Choose and enter a password:\n");
1448 return;
1449 }
1450 for (x = 0; x < strlen(buf); x++)
1451 if ((buf[x] <= 32) || (buf[x] == 127))
1452 ok = 0;
1453 if (!ok) {
1454 dprintf(idx, "\nYou can't use weird symbols in your password.\n");
1455 dprintf(idx, "Try another one please:\n");
1456 return;
1457 }
1458 putlog(LOG_MISC, "*", DCC_NEWUSER, dcc[idx].nick,
1459 dcc[idx].host, dcc[idx].port);
1460 if (notify_new[0]) {
1461 char s[121], s1[121], s2[121];
1462
1463 sprintf(s, "Introduced to %s, %s", dcc[idx].nick, dcc[idx].host);
1464 strcpy(s1, notify_new);
1465 splitc(s2, s1, ',');
1466 while (s2[0]) {
1467 rmspace(s2);
1468 add_note(s2, botnetnick, s, -1, 0);
1469 splitc(s2, s1, ',');
1470 }
1471 rmspace(s1);
1472 add_note(s1, botnetnick, s, -1, 0);
1473 }
1474 newpass = newsplit(&buf);
1475 set_user(&USERENTRY_PASS, dcc[idx].user, newpass);
1476 dprintf(idx, "\nRemember that! You'll need it next time you log in.\n");
1477 dprintf(idx, "You now have an account on %s...\n\n\n", botnetnick);
1478 dcc[idx].type = &DCC_CHAT;
1479 dcc[idx].u.chat->channel = -2;
1480 dcc_chatter(idx);
1481 }
1482
1483 static void eof_dcc_telnet_new(int idx)
1484 {
1485 putlog(LOG_MISC, "*", DCC_LOSTNEWUSER, dcc[idx].host,
1486 dcc[idx].port);
1487 killsock(dcc[idx].sock);
1488 lostdcc(idx);
1489 }
1490
1491 static void eof_dcc_telnet_pw(int idx)
1492 {
1493 putlog(LOG_MISC, "*", DCC_LOSTNEWUSR2, dcc[idx].nick,
1494 dcc[idx].host, dcc[idx].port);
1495 deluser(dcc[idx].nick);
1496 killsock(dcc[idx].sock);
1497 lostdcc(idx);
1498 }
1499
1500 static void tout_dcc_telnet_new(int idx)
1501 {
1502 dprintf(idx, "Guess you're not there. Bye.\n");
1503 putlog(LOG_MISC, "*", DCC_TIMEOUTUSER, dcc[idx].host,
1504 dcc[idx].port);
1505 killsock(dcc[idx].sock);
1506 lostdcc(idx);
1507 }
1508
1509 static void tout_dcc_telnet_pw(int idx)
1510 {
1511 dprintf(idx, "Guess you're not there. Bye.\n");
1512 putlog(LOG_MISC, "*", DCC_TIMEOUTUSR2,
1513 dcc[idx].nick, dcc[idx].host, dcc[idx].port);
1514 killsock(dcc[idx].sock);
1515 lostdcc(idx);
1516 }
1517
1518 static void display_dcc_telnet_new(int idx, char *buf)
1519 {
1520 sprintf(buf, "new waited %lus", now - dcc[idx].timeval);
1521 }
1522
1523 static void display_dcc_telnet_pw(int idx, char *buf)
1524 {
1525 sprintf(buf, "newp waited %lus", now - dcc[idx].timeval);
1526 }
1527
1528 struct dcc_table DCC_TELNET_NEW =
1529 {
1530 "TELNET_NEW",
1531 0,
1532 eof_dcc_telnet_new,
1533 dcc_telnet_new,
1534 &password_timeout,
1535 tout_dcc_telnet_new,
1536 display_dcc_telnet_new,
1537 expmem_dcc_general,
1538 kill_dcc_general,
1539 out_dcc_general
1540 };
1541
1542 struct dcc_table DCC_TELNET_PW =
1543 {
1544 "TELNET_PW",
1545 0,
1546 eof_dcc_telnet_pw,
1547 dcc_telnet_pw,
1548 &password_timeout,
1549 tout_dcc_telnet_pw,
1550 display_dcc_telnet_pw,
1551 expmem_dcc_general,
1552 kill_dcc_general,
1553 out_dcc_general
1554 };
1555
1556 static int call_tcl_func(char *name, int idx, char *args)
1557 {
1558 char s[11];
1559
1560 set_tcl_vars();
1561 sprintf(s, "%d", idx);
1562 Tcl_SetVar(interp, "_n", s, 0);
1563 Tcl_SetVar(interp, "_a", args, 0);
1564 if (Tcl_VarEval(interp, name, " $_n $_a", NULL) == TCL_ERROR) {
1565 putlog(LOG_MISC, "*", DCC_TCLERROR, name, interp->result);
1566 return -1;
1567 }
1568 return (atoi(interp->result));
1569 }
1570
1571 static void dcc_script(int idx, char *buf, int len)
1572 {
1573 void *old = NULL;
1574 long oldsock = dcc[idx].sock;
1575
1576 strip_telnet(dcc[idx].sock, buf, &len);
1577 if (!len)
1578 return;
1579 dcc[idx].timeval = now;
1580 set_tcl_vars();
1581 if (call_tcl_func(dcc[idx].u.script->command, dcc[idx].sock, buf)) {
1582 context;
1583 if ((dcc[idx].sock != oldsock) || (idx>max_dcc))
1584 return; /* drummer: this happen after killdcc */
1585 old = dcc[idx].u.script->u.other;
1586 dcc[idx].type = dcc[idx].u.script->type;
1587 nfree(dcc[idx].u.script);
1588 dcc[idx].u.other = old;
1589 if (dcc[idx].type == &DCC_SOCKET) {
1590 /* kill the whole thing off */
1591 killsock(dcc[idx].sock);
1592 lostdcc(idx);
1593 return;
1594 }
1595 if (dcc[idx].type == &DCC_CHAT) {
1596 if (dcc[idx].u.chat->channel >= 0) {
1597 chanout_but(-1, dcc[idx].u.chat->channel,DCC_JOIN, dcc[idx].nick);
1598 context;
1599 if (dcc[idx].u.chat->channel < 10000)
1600 botnet_send_join_idx(idx, -1);
1601 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
1602 geticon(idx), dcc[idx].sock, dcc[idx].host);
1603 }
1604 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
1605 }
1606 }
1607 }
1608
1609 static void eof_dcc_script(int idx)
1610 {
1611 void *old;
1612 int oldflags;
1613
1614 context;
1615 /* This will stop a killdcc from working, incase the script tries
1616 * to kill it's controlling socket while handling an EOF <cybah> */
1617 oldflags = dcc[idx].type->flags;
1618 dcc[idx].type->flags &= ~(DCT_VALIDIDX);
1619 /* tell the script they're gone: */
1620 call_tcl_func(dcc[idx].u.script->command, dcc[idx].sock, "");
1621 /* restore the flags */
1622 dcc[idx].type->flags = oldflags;
1623 context;
1624 old = dcc[idx].u.script->u.other;
1625 dcc[idx].type = dcc[idx].u.script->type;
1626 nfree(dcc[idx].u.script);
1627 dcc[idx].u.other = old;
1628 /* then let it fall thru to the real one */
1629 if (dcc[idx].type && dcc[idx].type->eof)
1630 dcc[idx].type->eof(idx);
1631 else {
1632 putlog(LOG_MISC, "*",
1633 DCC_DEADSOCKET,
1634 dcc[idx].sock, dcc[idx].type->name);
1635 killsock(dcc[idx].sock);
1636 lostdcc(idx);
1637 }
1638 }
1639
1640 static void display_dcc_script(int idx, char *buf)
1641 {
1642 sprintf(buf, "scri %s", dcc[idx].u.script->command);
1643 }
1644
1645 static int expmem_dcc_script(void *x)
1646 {
1647 register struct script_info *p = (struct script_info *) x;
1648 int tot = sizeof(struct script_info);
1649
1650 if (p->type && p->u.other)
1651 tot += p->type->expmem(p->u.other);
1652 return tot;
1653 }
1654
1655 static void kill_dcc_script(int idx, void *x)
1656 {
1657 register struct script_info *p = (struct script_info *) x;
1658
1659 if (p->type && p->u.other)
1660 p->type->kill(idx, p->u.other);
1661 nfree(p);
1662 }
1663
1664 static void out_dcc_script(int idx, char *buf, void *x)
1665 {
1666 register struct script_info *p = (struct script_info *) x;
1667
1668 if (p && p->type && p->u.other)
1669 p->type->output(idx, buf, p->u.other);
1670 else
1671 tputs(dcc[idx].sock, buf, strlen(buf));
1672 }
1673
1674 struct dcc_table DCC_SCRIPT =
1675 {
1676 "SCRIPT",
1677 DCT_VALIDIDX,
1678 eof_dcc_script,
1679 dcc_script,
1680 0,
1681 0,
1682 display_dcc_script,
1683 expmem_dcc_script,
1684 kill_dcc_script,
1685 out_dcc_script
1686 };
1687
1688 static void dcc_socket(int idx, char *buf, int len)
1689 {
1690 }
1691
1692 static void eof_dcc_socket(int idx)
1693 {
1694 killsock(dcc[idx].sock);
1695 lostdcc(idx);
1696 }
1697
1698 static void display_dcc_socket(int idx, char *buf)
1699 {
1700 strcpy(buf, "sock (stranded)");
1701 }
1702
1703 struct dcc_table DCC_SOCKET =
1704 {
1705 "SOCKET",
1706 DCT_VALIDIDX,
1707 eof_dcc_socket,
1708 dcc_socket,
1709 0,
1710 0,
1711 display_dcc_socket,
1712 0,
1713 0,
1714 0
1715 };
1716
1717 static void display_dcc_lost(int idx, char *buf)
1718 {
1719 strcpy(buf, "lost");
1720 }
1721
1722 struct dcc_table DCC_LOST =
1723 {
1724 "LOST",
1725 0,
1726 0,
1727 dcc_socket,
1728 0,
1729 0,
1730 display_dcc_lost,
1731 0,
1732 0,
1733 0
1734 };
1735
1736 void dcc_identwait(int idx, char *buf, int len)
1737 {
1738 /* ignore anything now */
1739 context;
1740 }
1741
1742 void eof_dcc_identwait(int idx)
1743 {
1744 int i;
1745
1746 putlog(LOG_MISC, "*", DCC_LOSTCONN,
1747 dcc[idx].host, dcc[idx].port);
1748 for (i = 0; i < dcc_total; i++)
1749 if ((dcc[i].type == &DCC_IDENT) &&
1750 (dcc[i].u.ident_sock == dcc[idx].sock)) {
1751 dcc[i].u.other = 0;
1752 killsock(dcc[i].sock); /* cleanup ident socket */
1753 }
1754 killsock(dcc[idx].sock); /* cleanup waiting socket */
1755 dcc[idx].u.other = 0;
1756 lostdcc(idx);
1757 }
1758
1759 static void display_dcc_identwait(int idx, char *buf)
1760 {
1761 sprintf(buf, "idtw waited %lus", now - dcc[idx].timeval);
1762 }
1763
1764 struct dcc_table DCC_IDENTWAIT =
1765 {
1766 "IDENTWAIT",
1767 0,
1768 eof_dcc_identwait,
1769 dcc_identwait,
1770 0,
1771 0,
1772 display_dcc_identwait,
1773 0,
1774 0,
1775 0
1776 };
1777
1778 void dcc_ident(int idx, char *buf, int len)
1779 {
1780 char response[512], uid[512], buf1[UHOSTLEN];
1781 int i, sock = dcc[idx].sock;
1782
1783 context;
1784 sscanf(buf, "%*[^:]:%[^:]:%*[^:]:%[^\n]\n", response, uid);
1785 rmspace(response);
1786 if (response[0] != 'U') {
1787 dcc[idx].timeval = now;
1788 return;
1789 }
1790 rmspace(uid);
1791 uid[20] = 0; /* 20 character ident max */
1792 for (i = 0; i < dcc_total; i++)
1793 if ((dcc[i].type == &DCC_IDENTWAIT) &&
1794 (dcc[i].sock == dcc[idx].u.ident_sock)) {
1795 simple_sprintf(buf1, "%s@%s", uid, dcc[idx].host);
1796 dcc_telnet_got_ident(i, buf1);
1797 }
1798 idx = findanyidx(sock);
1799 dcc[idx].u.other = 0;
1800 killsock(dcc[idx].sock);
1801 lostdcc(idx);
1802 }
1803
1804 void eof_dcc_ident(int idx)
1805 {
1806 char buf[UHOSTLEN];
1807 int i, sock = dcc[idx].sock;
1808
1809 for (i = 0; i < dcc_total; i++)
1810 if ((dcc[i].type == &DCC_IDENTWAIT) &&
1811 (dcc[i].sock == dcc[idx].u.ident_sock)) {
1812 putlog(LOG_MISC, "*", DCC_EOFIDENT);
1813 simple_sprintf(buf, "telnet@%s", dcc[idx].host);
1814 dcc_telnet_got_ident(i, buf);
1815 }
1816 idx = findanyidx(sock); /* sanity */
1817 killsock(dcc[idx].sock);
1818 dcc[idx].u.other = 0;
1819 lostdcc(idx);
1820 }
1821
1822 static void display_dcc_ident(int idx, char *buf)
1823 {
1824 sprintf(buf, "idnt (sock %d)", dcc[idx].u.ident_sock);
1825 }
1826
1827 struct dcc_table DCC_IDENT =
1828 {
1829 "IDENT",
1830 0,
1831 eof_dcc_ident,
1832 dcc_ident,
1833 &identtimeout,
1834 eof_dcc_ident,
1835 display_dcc_ident,
1836 0,
1837 0,
1838 0
1839 };
1840
1841 void dcc_telnet_got_ident(int i, char *host)
1842 {
1843 int idx;
1844 char x[1024];
1845
1846 for (idx = 0; idx < dcc_total; idx++)
1847 if ((dcc[idx].type == &DCC_TELNET) &&
1848 (dcc[idx].sock == dcc[i].u.ident_sock))
1849 break;
1850 dcc[i].u.other = 0;
1851 if (dcc_total == idx)
1852 putlog(LOG_MISC, "*", DCC_LOSTIDENT);
1853 strncpy(dcc[i].host, host, UHOSTLEN);
1854 dcc[i].host[UHOSTLEN] = 0;
1855 simple_sprintf(x, "telnet!%s", dcc[i].host);
1856 if (protect_telnet && !make_userfile) {
1857 struct userrec *u;
1858 int ok = 1;
1859
1860 context;
1861 u = get_user_by_host(x);
1862 /* not a user or +p & require p OR +o */
1863 if (!u)
1864 ok = 0;
1865 else if (require_p && !(u->flags & USER_PARTY))
1866 ok = 0;
1867 else if (!require_p && !(u->flags & USER_OP))
1868 ok = 0;
1869 if (!ok && u && (u->flags & USER_BOT))
1870 ok = 1;
1871 if (!ok) {
1872 putlog(LOG_MISC, "*", DCC_NOACCESS, dcc[i].host);
1873 killsock(dcc[i].sock);
1874 lostdcc(i);
1875 return;
1876 }
1877 }
1878 context;
1879 if (match_ignore(x)) {
1880 killsock(dcc[i].sock);
1881 lostdcc(i);
1882 return;
1883 }
1884 /* script? */
1885 if (!strcmp(dcc[idx].nick, "(script)")) {
1886 dcc[i].type = &DCC_SOCKET;
1887 dcc[i].u.other = NULL;
1888 strcpy(dcc[i].nick, "*");
1889 check_tcl_listen(dcc[idx].host, dcc[i].sock);
1890 return;
1891 }
1892 dcc[i].type = &DCC_TELNET_ID;
1893 dcc[i].u.chat = get_data_ptr(sizeof(struct chat_info));
1894 bzero(dcc[i].u.chat, sizeof(struct chat_info));
1895
1896 /* copy acceptable-nick/host mask */
1897 dcc[i].status = STAT_TELNET | STAT_ECHO;
1898 if (!strcmp(dcc[idx].nick, "(bots)"))
1899 dcc[i].status |= STAT_BOTONLY;
1900 if (!strcmp(dcc[idx].nick, "(users)"))
1901 dcc[i].status |= STAT_USRONLY;
1902 /* copy acceptable-nick/host mask */
1903 strncpy(dcc[i].nick, dcc[idx].host, HANDLEN);
1904 dcc[i].nick[HANDLEN] = 0;
1905 dcc[i].timeval = now;
1906 strcpy(dcc[i].u.chat->con_chan, chanset ? chanset->name : "*");
1907 /* Displays a customizable banner. [seC] */
1908 if (use_telnet_banner)
1909 show_banner(i);
1910 /* This is so we dont tell someone doing a portscan anything
1911 * about ourselves. <cybah> */
1912 if (stealth_telnets)
1913 sub_lang(i, MISC_BANNER_STEALTH);
1914 else {
1915 dprintf(i, "\r\n\r\n");
1916 sub_lang(i, MISC_BANNER);
1917 }
1918 if (allow_new_telnets)
1919 dprintf(i, "(If you are new, enter 'NEW' here.)\r\n");
1920 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23