/[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.8 - (show annotations) (download) (as text)
Mon Aug 30 19:30:53 1999 UTC (20 years ago) by poptix
Branch: MAIN
Changes since 1.7: +1 -0 lines
File MIME type: text/x-chdr

quiet_reject2.patch, ignoremodes.patch, parafix.patch

: Modified Files:
: 	Makefile TODO doc/UPDATES1.3 src/dcc.c src/main.h src/misc.c
: 	src/patch.h src/tclegg.h src/mod/channels.mod/tclchan.c
: 	src/mod/filesys.mod/filesys.c src/mod/filesys.mod/filesys.h
: 	src/mod/server.mod/servmsg.c src/mod/transfer.mod/transfer.c
: 	src/mod/transfer.mod/transfer.h

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 (in_chain(buf)) {
1250 dprintf(idx, "error Already connected.\n");
1251 putlog(LOG_BOTS, "*", DCC_DUPLICATE,
1252 dcc[idx].host);
1253 killsock(dcc[idx].sock);
1254 lostdcc(idx);
1255 return;
1256 }
1257 }
1258 /* no password set? */
1259 if (u_pass_match(dcc[idx].user, "-")) {
1260 if (glob_bot(fr)) {
1261 char ps[20];
1262
1263 makepass(ps);
1264 set_user(&USERENTRY_PASS, dcc[idx].user, ps);
1265 correct_handle(buf);
1266 strcpy(dcc[idx].nick, buf);
1267 nfree(dcc[idx].u.chat);
1268 dcc[idx].u.bot = get_data_ptr(sizeof(struct bot_info));
1269
1270 dcc[idx].type = &DCC_BOT_NEW;
1271 dcc[idx].status = STAT_CALLED;
1272 dprintf(idx, "*hello!\n");
1273 greet_new_bot(idx);
1274 #ifdef NO_OLD_BOTNET
1275 dprintf(idx, "h %s\n", ps);
1276 #else
1277 dprintf(idx, "handshake %s\n", ps);
1278 #endif
1279 return;
1280 }
1281 dprintf(idx, "Can't telnet until you have a password set.\r\n");
1282 putlog(LOG_MISC, "*", DCC_NOPASS, buf, dcc[idx].host);
1283 killsock(dcc[idx].sock);
1284 lostdcc(idx);
1285 return;
1286 }
1287 ok = 0;
1288 dcc[idx].type = &DCC_CHAT_PASS;
1289 dcc[idx].timeval = now;
1290 if (glob_botmast(fr))
1291 ok = 1;
1292 else if (chan_op(fr)) {
1293 if (!require_p)
1294 ok = 1;
1295 else if (glob_party(fr))
1296 ok = 1;
1297 } else if (glob_party(fr)) {
1298 ok = 1;
1299 dcc[idx].status |= STAT_PARTY;
1300 }
1301 if (glob_bot(fr))
1302 ok = 1;
1303 if (!ok) {
1304 struct chat_info *ci;
1305
1306 ci = dcc[idx].u.chat;
1307 dcc[idx].u.file = get_data_ptr(sizeof(struct file_info));
1308
1309 dcc[idx].u.file->chat = ci;
1310 }
1311 correct_handle(buf);
1312 strcpy(dcc[idx].nick, buf);
1313
1314 if (glob_bot(fr)) {
1315 /* Must generate a string consisting of our process ID and the current
1316 * time. The bot will add it's password to the end and use it to generate
1317 * an MD5 checksum (always 128bit). The checksum is sent back and this
1318 * end does the same. The remote bot is only allowed access if the
1319 * checksums match. Please don't fuck with 'timeval', or the digest we
1320 * generate later for authentication will not be correct - you've been
1321 * warned ;) <Cybah>
1322 */
1323 putlog(LOG_BOTS, "*", "Challenging %s...", dcc[idx].nick);
1324 dprintf(idx, "passreq <%x%x@%s>\n", getpid(), dcc[idx].timeval, botnetnick);
1325 } else {
1326 /* *note* The MD5 digest used above to prevent cleartext passwords
1327 * being sent across the net will _only_ work when we have the cleartext
1328 * password. User passwords are encrypted (with blowfish usually) so the
1329 * same thing cant be done. Botnet passwords are always stored in
1330 * cleartext, or at least something that can be reversed. <Cybah>
1331 */
1332 dprintf(idx, "\nEnter your password.\377\373\001\n");
1333 /* turn off remote telnet echo: IAC WILL ECHO */
1334 }
1335 }
1336
1337 static void eof_dcc_telnet_id(int idx)
1338 {
1339 putlog(LOG_MISC, "*", DCC_LOSTCON, dcc[idx].host,
1340 dcc[idx].port);
1341 killsock(dcc[idx].sock);
1342 lostdcc(idx);
1343 }
1344
1345 static void timeout_dcc_telnet_id(int idx)
1346 {
1347 dprintf(idx, "Timeout.\n");
1348 putlog(LOG_MISC, "*", DCC_TTIMEOUT, dcc[idx].host);
1349 killsock(dcc[idx].sock);
1350 lostdcc(idx);
1351 }
1352
1353 static void display_dcc_telnet_id(int idx, char *buf)
1354 {
1355 sprintf(buf, "t-in waited %lus", now - dcc[idx].timeval);
1356 }
1357
1358 struct dcc_table DCC_TELNET_ID =
1359 {
1360 "TELNET_ID",
1361 0,
1362 eof_dcc_telnet_id,
1363 dcc_telnet_id,
1364 &password_timeout,
1365 timeout_dcc_telnet_id,
1366 display_dcc_telnet_id,
1367 expmem_dcc_general,
1368 kill_dcc_general,
1369 out_dcc_general
1370 };
1371
1372 static void dcc_telnet_new(int idx, char *buf, int x)
1373 {
1374 int ok = 1;
1375 char work[1024], *p, *q, *r;
1376
1377 buf[HANDLEN] = 0;
1378 strip_telnet(dcc[idx].sock, buf, &x);
1379 strcpy(dcc[idx].nick, buf);
1380 dcc[idx].timeval = now;
1381 for (x = 0; x < strlen(buf); x++)
1382 if ((buf[x] <= 32) || (buf[x] >= 127))
1383 ok = 0;
1384 if (!ok) {
1385 dprintf(idx, "\nYou can't use weird symbols in your nick.\n");
1386 dprintf(idx, "Try another one please:\n");
1387 } else if (strchr("-,+*=:!.@#;$", buf[0]) != NULL) {
1388 dprintf(idx, "\nYou can't start your nick with the character '%c'\n", buf[0]);
1389 dprintf(idx, "Try another one please:\n");
1390 } else if (get_user_by_handle(userlist, buf)) {
1391 dprintf(idx, "\nSorry, that nickname is taken already.\n");
1392 dprintf(idx, "Try another one please:\n");
1393 return;
1394 } else if (!strcasecmp(buf, origbotname) || !strcasecmp(buf, botnetnick)) {
1395 dprintf(idx, "Sorry, can't use my name for a nick.\n");
1396 } else {
1397 if (make_userfile)
1398 userlist = adduser(userlist, buf, "telnet!*@*", "-",
1399 sanity_check(default_flags | USER_PARTY |
1400 USER_MASTER | USER_OWNER));
1401 else {
1402 p = strchr(dcc[idx].host, '@');
1403 if (p) {
1404 q = p;
1405 *q = 0;
1406 p++;
1407 r = strchr(p, '.');
1408 if (!r)
1409 simple_sprintf(work, "telnet!%s@%s", dcc[idx].host, p);
1410 else
1411 simple_sprintf(work, "telnet!%s@*%s", dcc[idx].host, r);
1412 *q = '@';
1413 } else
1414 simple_sprintf(work, "telnet!*@*%s", dcc[idx].host);
1415 userlist = adduser(userlist, buf, work, "-",
1416 sanity_check(USER_PARTY | default_flags));
1417 }
1418 reaffirm_owners();
1419 dcc[idx].status = STAT_ECHO | STAT_TELNET;
1420 dcc[idx].type = &DCC_CHAT; /* just so next line will work */
1421 dcc[idx].user = get_user_by_handle(userlist, buf);
1422 check_dcc_attrs(dcc[idx].user, USER_PARTY | default_flags);
1423 dcc[idx].type = &DCC_TELNET_PW;
1424 if (make_userfile) {
1425 dprintf(idx, "\nYOU ARE THE MASTER/OWNER ON THIS BOT NOW\n");
1426 dprintf(idx, IRC_LIMBO);
1427 putlog(LOG_MISC, "*", DCC_INSTCOMPL,
1428 buf);
1429 make_userfile = 0;
1430 write_userfile(-1);
1431 add_note(buf, botnetnick, "Welcome to eggdrop! :)", -1, 0);
1432 }
1433 dprintf(idx, "\nOkay, now choose and enter a password:\n");
1434 dprintf(idx, "(Only the first 15 letters are significant.)\n");
1435 }
1436 }
1437
1438 static void dcc_telnet_pw(int idx, char *buf, int x)
1439 {
1440 char *newpass;
1441 int ok;
1442
1443 strip_telnet(dcc[idx].sock, buf, &x);
1444 buf[16] = 0;
1445 ok = 1;
1446 if (strlen(buf) < 4) {
1447 dprintf(idx, "\nTry to use at least 4 characters in your password.\n");
1448 dprintf(idx, "Choose and enter a password:\n");
1449 return;
1450 }
1451 for (x = 0; x < strlen(buf); x++)
1452 if ((buf[x] <= 32) || (buf[x] == 127))
1453 ok = 0;
1454 if (!ok) {
1455 dprintf(idx, "\nYou can't use weird symbols in your password.\n");
1456 dprintf(idx, "Try another one please:\n");
1457 return;
1458 }
1459 putlog(LOG_MISC, "*", DCC_NEWUSER, dcc[idx].nick,
1460 dcc[idx].host, dcc[idx].port);
1461 if (notify_new[0]) {
1462 char s[121], s1[121], s2[121];
1463
1464 sprintf(s, "Introduced to %s, %s", dcc[idx].nick, dcc[idx].host);
1465 strcpy(s1, notify_new);
1466 splitc(s2, s1, ',');
1467 while (s2[0]) {
1468 rmspace(s2);
1469 add_note(s2, botnetnick, s, -1, 0);
1470 splitc(s2, s1, ',');
1471 }
1472 rmspace(s1);
1473 add_note(s1, botnetnick, s, -1, 0);
1474 }
1475 newpass = newsplit(&buf);
1476 set_user(&USERENTRY_PASS, dcc[idx].user, newpass);
1477 dprintf(idx, "\nRemember that! You'll need it next time you log in.\n");
1478 dprintf(idx, "You now have an account on %s...\n\n\n", botnetnick);
1479 dcc[idx].type = &DCC_CHAT;
1480 dcc[idx].u.chat->channel = -2;
1481 dcc_chatter(idx);
1482 }
1483
1484 static void eof_dcc_telnet_new(int idx)
1485 {
1486 putlog(LOG_MISC, "*", DCC_LOSTNEWUSER, dcc[idx].host,
1487 dcc[idx].port);
1488 killsock(dcc[idx].sock);
1489 lostdcc(idx);
1490 }
1491
1492 static void eof_dcc_telnet_pw(int idx)
1493 {
1494 putlog(LOG_MISC, "*", DCC_LOSTNEWUSR2, dcc[idx].nick,
1495 dcc[idx].host, dcc[idx].port);
1496 deluser(dcc[idx].nick);
1497 killsock(dcc[idx].sock);
1498 lostdcc(idx);
1499 }
1500
1501 static void tout_dcc_telnet_new(int idx)
1502 {
1503 dprintf(idx, "Guess you're not there. Bye.\n");
1504 putlog(LOG_MISC, "*", DCC_TIMEOUTUSER, dcc[idx].host,
1505 dcc[idx].port);
1506 killsock(dcc[idx].sock);
1507 lostdcc(idx);
1508 }
1509
1510 static void tout_dcc_telnet_pw(int idx)
1511 {
1512 dprintf(idx, "Guess you're not there. Bye.\n");
1513 putlog(LOG_MISC, "*", DCC_TIMEOUTUSR2,
1514 dcc[idx].nick, dcc[idx].host, dcc[idx].port);
1515 killsock(dcc[idx].sock);
1516 lostdcc(idx);
1517 }
1518
1519 static void display_dcc_telnet_new(int idx, char *buf)
1520 {
1521 sprintf(buf, "new waited %lus", now - dcc[idx].timeval);
1522 }
1523
1524 static void display_dcc_telnet_pw(int idx, char *buf)
1525 {
1526 sprintf(buf, "newp waited %lus", now - dcc[idx].timeval);
1527 }
1528
1529 struct dcc_table DCC_TELNET_NEW =
1530 {
1531 "TELNET_NEW",
1532 0,
1533 eof_dcc_telnet_new,
1534 dcc_telnet_new,
1535 &password_timeout,
1536 tout_dcc_telnet_new,
1537 display_dcc_telnet_new,
1538 expmem_dcc_general,
1539 kill_dcc_general,
1540 out_dcc_general
1541 };
1542
1543 struct dcc_table DCC_TELNET_PW =
1544 {
1545 "TELNET_PW",
1546 0,
1547 eof_dcc_telnet_pw,
1548 dcc_telnet_pw,
1549 &password_timeout,
1550 tout_dcc_telnet_pw,
1551 display_dcc_telnet_pw,
1552 expmem_dcc_general,
1553 kill_dcc_general,
1554 out_dcc_general
1555 };
1556
1557 static int call_tcl_func(char *name, int idx, char *args)
1558 {
1559 char s[11];
1560
1561 set_tcl_vars();
1562 sprintf(s, "%d", idx);
1563 Tcl_SetVar(interp, "_n", s, 0);
1564 Tcl_SetVar(interp, "_a", args, 0);
1565 if (Tcl_VarEval(interp, name, " $_n $_a", NULL) == TCL_ERROR) {
1566 putlog(LOG_MISC, "*", DCC_TCLERROR, name, interp->result);
1567 return -1;
1568 }
1569 return (atoi(interp->result));
1570 }
1571
1572 static void dcc_script(int idx, char *buf, int len)
1573 {
1574 void *old = NULL;
1575 long oldsock = dcc[idx].sock;
1576
1577 strip_telnet(dcc[idx].sock, buf, &len);
1578 if (!len)
1579 return;
1580 dcc[idx].timeval = now;
1581 set_tcl_vars();
1582 if (call_tcl_func(dcc[idx].u.script->command, dcc[idx].sock, buf)) {
1583 context;
1584 if ((dcc[idx].sock != oldsock) || (idx>max_dcc))
1585 return; /* drummer: this happen after killdcc */
1586 old = dcc[idx].u.script->u.other;
1587 dcc[idx].type = dcc[idx].u.script->type;
1588 nfree(dcc[idx].u.script);
1589 dcc[idx].u.other = old;
1590 if (dcc[idx].type == &DCC_SOCKET) {
1591 /* kill the whole thing off */
1592 killsock(dcc[idx].sock);
1593 lostdcc(idx);
1594 return;
1595 }
1596 if (dcc[idx].type == &DCC_CHAT) {
1597 if (dcc[idx].u.chat->channel >= 0) {
1598 chanout_but(-1, dcc[idx].u.chat->channel,DCC_JOIN, dcc[idx].nick);
1599 context;
1600 if (dcc[idx].u.chat->channel < 10000)
1601 botnet_send_join_idx(idx, -1);
1602 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
1603 geticon(idx), dcc[idx].sock, dcc[idx].host);
1604 }
1605 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
1606 }
1607 }
1608 }
1609
1610 static void eof_dcc_script(int idx)
1611 {
1612 void *old;
1613 int oldflags;
1614
1615 context;
1616 /* This will stop a killdcc from working, incase the script tries
1617 * to kill it's controlling socket while handling an EOF <cybah> */
1618 oldflags = dcc[idx].type->flags;
1619 dcc[idx].type->flags &= ~(DCT_VALIDIDX);
1620 /* tell the script they're gone: */
1621 call_tcl_func(dcc[idx].u.script->command, dcc[idx].sock, "");
1622 /* restore the flags */
1623 dcc[idx].type->flags = oldflags;
1624 context;
1625 old = dcc[idx].u.script->u.other;
1626 dcc[idx].type = dcc[idx].u.script->type;
1627 nfree(dcc[idx].u.script);
1628 dcc[idx].u.other = old;
1629 /* then let it fall thru to the real one */
1630 if (dcc[idx].type && dcc[idx].type->eof)
1631 dcc[idx].type->eof(idx);
1632 else {
1633 putlog(LOG_MISC, "*",
1634 DCC_DEADSOCKET,
1635 dcc[idx].sock, dcc[idx].type->name);
1636 killsock(dcc[idx].sock);
1637 lostdcc(idx);
1638 }
1639 }
1640
1641 static void display_dcc_script(int idx, char *buf)
1642 {
1643 sprintf(buf, "scri %s", dcc[idx].u.script->command);
1644 }
1645
1646 static int expmem_dcc_script(void *x)
1647 {
1648 register struct script_info *p = (struct script_info *) x;
1649 int tot = sizeof(struct script_info);
1650
1651 if (p->type && p->u.other)
1652 tot += p->type->expmem(p->u.other);
1653 return tot;
1654 }
1655
1656 static void kill_dcc_script(int idx, void *x)
1657 {
1658 register struct script_info *p = (struct script_info *) x;
1659
1660 if (p->type && p->u.other)
1661 p->type->kill(idx, p->u.other);
1662 nfree(p);
1663 }
1664
1665 static void out_dcc_script(int idx, char *buf, void *x)
1666 {
1667 register struct script_info *p = (struct script_info *) x;
1668
1669 if (p && p->type && p->u.other)
1670 p->type->output(idx, buf, p->u.other);
1671 else
1672 tputs(dcc[idx].sock, buf, strlen(buf));
1673 }
1674
1675 struct dcc_table DCC_SCRIPT =
1676 {
1677 "SCRIPT",
1678 DCT_VALIDIDX,
1679 eof_dcc_script,
1680 dcc_script,
1681 0,
1682 0,
1683 display_dcc_script,
1684 expmem_dcc_script,
1685 kill_dcc_script,
1686 out_dcc_script
1687 };
1688
1689 static void dcc_socket(int idx, char *buf, int len)
1690 {
1691 }
1692
1693 static void eof_dcc_socket(int idx)
1694 {
1695 killsock(dcc[idx].sock);
1696 lostdcc(idx);
1697 }
1698
1699 static void display_dcc_socket(int idx, char *buf)
1700 {
1701 strcpy(buf, "sock (stranded)");
1702 }
1703
1704 struct dcc_table DCC_SOCKET =
1705 {
1706 "SOCKET",
1707 DCT_VALIDIDX,
1708 eof_dcc_socket,
1709 dcc_socket,
1710 0,
1711 0,
1712 display_dcc_socket,
1713 0,
1714 0,
1715 0
1716 };
1717
1718 static void display_dcc_lost(int idx, char *buf)
1719 {
1720 strcpy(buf, "lost");
1721 }
1722
1723 struct dcc_table DCC_LOST =
1724 {
1725 "LOST",
1726 0,
1727 0,
1728 dcc_socket,
1729 0,
1730 0,
1731 display_dcc_lost,
1732 0,
1733 0,
1734 0
1735 };
1736
1737 void dcc_identwait(int idx, char *buf, int len)
1738 {
1739 /* ignore anything now */
1740 context;
1741 }
1742
1743 void eof_dcc_identwait(int idx)
1744 {
1745 int i;
1746
1747 putlog(LOG_MISC, "*", DCC_LOSTCONN,
1748 dcc[idx].host, dcc[idx].port);
1749 for (i = 0; i < dcc_total; i++)
1750 if ((dcc[i].type == &DCC_IDENT) &&
1751 (dcc[i].u.ident_sock == dcc[idx].sock)) {
1752 dcc[i].u.other = 0;
1753 killsock(dcc[i].sock); /* cleanup ident socket */
1754 }
1755 killsock(dcc[idx].sock); /* cleanup waiting socket */
1756 dcc[idx].u.other = 0;
1757 lostdcc(idx);
1758 }
1759
1760 static void display_dcc_identwait(int idx, char *buf)
1761 {
1762 sprintf(buf, "idtw waited %lus", now - dcc[idx].timeval);
1763 }
1764
1765 struct dcc_table DCC_IDENTWAIT =
1766 {
1767 "IDENTWAIT",
1768 0,
1769 eof_dcc_identwait,
1770 dcc_identwait,
1771 0,
1772 0,
1773 display_dcc_identwait,
1774 0,
1775 0,
1776 0
1777 };
1778
1779 void dcc_ident(int idx, char *buf, int len)
1780 {
1781 char response[512], uid[512], buf1[UHOSTLEN];
1782 int i, sock = dcc[idx].sock;
1783
1784 context;
1785 sscanf(buf, "%*[^:]:%[^:]:%*[^:]:%[^\n]\n", response, uid);
1786 rmspace(response);
1787 if (response[0] != 'U') {
1788 dcc[idx].timeval = now;
1789 return;
1790 }
1791 rmspace(uid);
1792 uid[20] = 0; /* 20 character ident max */
1793 for (i = 0; i < dcc_total; i++)
1794 if ((dcc[i].type == &DCC_IDENTWAIT) &&
1795 (dcc[i].sock == dcc[idx].u.ident_sock)) {
1796 simple_sprintf(buf1, "%s@%s", uid, dcc[idx].host);
1797 dcc_telnet_got_ident(i, buf1);
1798 }
1799 idx = findanyidx(sock);
1800 dcc[idx].u.other = 0;
1801 killsock(dcc[idx].sock);
1802 lostdcc(idx);
1803 }
1804
1805 void eof_dcc_ident(int idx)
1806 {
1807 char buf[UHOSTLEN];
1808 int i, sock = dcc[idx].sock;
1809
1810 for (i = 0; i < dcc_total; i++)
1811 if ((dcc[i].type == &DCC_IDENTWAIT) &&
1812 (dcc[i].sock == dcc[idx].u.ident_sock)) {
1813 putlog(LOG_MISC, "*", DCC_EOFIDENT);
1814 simple_sprintf(buf, "telnet@%s", dcc[idx].host);
1815 dcc_telnet_got_ident(i, buf);
1816 }
1817 idx = findanyidx(sock); /* sanity */
1818 killsock(dcc[idx].sock);
1819 dcc[idx].u.other = 0;
1820 lostdcc(idx);
1821 }
1822
1823 static void display_dcc_ident(int idx, char *buf)
1824 {
1825 sprintf(buf, "idnt (sock %d)", dcc[idx].u.ident_sock);
1826 }
1827
1828 struct dcc_table DCC_IDENT =
1829 {
1830 "IDENT",
1831 0,
1832 eof_dcc_ident,
1833 dcc_ident,
1834 &identtimeout,
1835 eof_dcc_ident,
1836 display_dcc_ident,
1837 0,
1838 0,
1839 0
1840 };
1841
1842 void dcc_telnet_got_ident(int i, char *host)
1843 {
1844 int idx;
1845 char x[1024];
1846
1847 for (idx = 0; idx < dcc_total; idx++)
1848 if ((dcc[idx].type == &DCC_TELNET) &&
1849 (dcc[idx].sock == dcc[i].u.ident_sock))
1850 break;
1851 dcc[i].u.other = 0;
1852 if (dcc_total == idx)
1853 putlog(LOG_MISC, "*", DCC_LOSTIDENT);
1854 strncpy(dcc[i].host, host, UHOSTLEN);
1855 dcc[i].host[UHOSTLEN] = 0;
1856 simple_sprintf(x, "telnet!%s", dcc[i].host);
1857 if (protect_telnet && !make_userfile) {
1858 struct userrec *u;
1859 int ok = 1;
1860
1861 context;
1862 u = get_user_by_host(x);
1863 /* not a user or +p & require p OR +o */
1864 if (!u)
1865 ok = 0;
1866 else if (require_p && !(u->flags & USER_PARTY))
1867 ok = 0;
1868 else if (!require_p && !(u->flags & USER_OP))
1869 ok = 0;
1870 if (!ok && u && (u->flags & USER_BOT))
1871 ok = 1;
1872 if (!ok) {
1873 putlog(LOG_MISC, "*", DCC_NOACCESS, dcc[i].host);
1874 killsock(dcc[i].sock);
1875 lostdcc(i);
1876 return;
1877 }
1878 }
1879 context;
1880 if (match_ignore(x)) {
1881 killsock(dcc[i].sock);
1882 lostdcc(i);
1883 return;
1884 }
1885 /* script? */
1886 if (!strcmp(dcc[idx].nick, "(script)")) {
1887 dcc[i].type = &DCC_SOCKET;
1888 dcc[i].u.other = NULL;
1889 strcpy(dcc[i].nick, "*");
1890 check_tcl_listen(dcc[idx].host, dcc[i].sock);
1891 return;
1892 }
1893 dcc[i].type = &DCC_TELNET_ID;
1894 dcc[i].u.chat = get_data_ptr(sizeof(struct chat_info));
1895 bzero(dcc[i].u.chat, sizeof(struct chat_info));
1896
1897 /* copy acceptable-nick/host mask */
1898 dcc[i].status = STAT_TELNET | STAT_ECHO;
1899 if (!strcmp(dcc[idx].nick, "(bots)"))
1900 dcc[i].status |= STAT_BOTONLY;
1901 if (!strcmp(dcc[idx].nick, "(users)"))
1902 dcc[i].status |= STAT_USRONLY;
1903 /* copy acceptable-nick/host mask */
1904 strncpy(dcc[i].nick, dcc[idx].host, HANDLEN);
1905 dcc[i].nick[HANDLEN] = 0;
1906 dcc[i].timeval = now;
1907 strcpy(dcc[i].u.chat->con_chan, chanset ? chanset->name : "*");
1908 /* Displays a customizable banner. [seC] */
1909 if (use_telnet_banner)
1910 show_banner(i);
1911 /* This is so we dont tell someone doing a portscan anything
1912 * about ourselves. <cybah> */
1913 if (stealth_telnets)
1914 sub_lang(i, MISC_BANNER_STEALTH);
1915 else {
1916 dprintf(i, "\r\n\r\n");
1917 sub_lang(i, MISC_BANNER);
1918 }
1919 if (allow_new_telnets)
1920 dprintf(i, "(If you are new, enter 'NEW' here.)\r\n");
1921 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23