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

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

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


Revision 1.9 - (show annotations) (download) (as text)
Thu Jan 27 11:20:16 2011 UTC (9 years, 5 months ago) by pseudo
Branch: MAIN
Changes since 1.8: +2 -1 lines
File MIME type: text/x-chdr
Added missing message for incoming telnet connections.

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 *
7 * $Id: dcc.c,v 1.8 2010/11/26 13:20:29 pseudo Exp $
8 */
9 /*
10 * Copyright (C) 1997 Robey Pointer
11 * Copyright (C) 1999 - 2010 Eggheads Development Team
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 */
27
28 #include "main.h"
29 #include <ctype.h>
30 #include <errno.h>
31 #include "modules.h"
32 #include "tandem.h"
33
34 /* Includes for botnet md5 challenge/response code <cybah> */
35 #include "md5/md5.h"
36
37 extern struct userrec *userlist;
38 extern struct chanset_t *chanset;
39 extern Tcl_Interp *interp;
40 extern time_t now;
41 extern char botnetnick[], ver[], origbotname[], notify_new[];
42 extern int egg_numver, connect_timeout, conmask, backgrd, max_dcc,
43 make_userfile, default_flags, raw_log, ignore_time,
44 par_telnet_flood;
45
46 #ifdef TLS
47 extern int tls_vfybots;
48
49 int tls_vfyclients = 0; /* Certificate validation mode for clients */
50 int tls_vfydcc = 0; /* Verify DCC chat/send user certificates */
51 int tls_auth = 0; /* Allow certificate authentication */
52 #endif
53
54 struct dcc_t *dcc = NULL; /* DCC list */
55 int dcc_total = 0; /* Total dcc's */
56 int require_p = 0; /* Require 'p' access to get on the
57 * party line? */
58 int allow_new_telnets = 0; /* Allow people to introduce themselves
59 * via telnet */
60 int stealth_telnets = 0; /* Be paranoid? <cybah> */
61 int use_telnet_banner = 0; /* Display telnet banner? */
62 int password_timeout = 180; /* Time to wait for a password from a user */
63 int bot_timeout = 60; /* Bot timeout value */
64 int identtimeout = 5; /* Timeout value for ident lookups */
65 int dupwait_timeout = 5; /* Timeout for rejecting duplicate entries */
66 int protect_telnet = 1; /* Even bother with ident lookups :) */
67 int flood_telnet_thr = 5; /* Number of telnet connections to be
68 * considered a flood */
69 int flood_telnet_time = 60; /* In how many seconds? */
70 char tempdir[121] = ""; /* Temporary directory (default: current dir) */
71 char network[41] = "unknown-net"; /* Name of the IRC network you're on */
72 char bannerfile[121] = "text/banner"; /* File displayed on telnet login */
73
74 static void dcc_telnet_hostresolved(int);
75 static void dcc_telnet_got_ident(int, char *);
76 static void dcc_telnet_pass(int, int);
77
78
79 /* This is not a universal telnet detector. You need to send WILL STATUS to the
80 * other side and pass the reply to this function. A telnet client will respond
81 * to this with either DO or DONT STATUS.
82 */
83 static int detect_telnet(unsigned char *buf)
84 {
85 if (!buf || !buf[0] || !buf[1])
86 return 0;
87 while (buf[2]) {
88 if (buf[0] == TLN_IAC && (buf[1] == TLN_DO || buf[1] == TLN_DONT) &&
89 buf[2] == TLN_STATUS)
90 return 1;
91 buf++;
92 }
93 return 0;
94 }
95
96 /* Escape telnet IAC and prepend CR to LF */
97 static char *escape_telnet(char *s)
98 {
99 static char buf[1024];
100 char *p;
101
102 for (p = buf; *s && (p < (buf + sizeof(buf) - 2)); *p++ = *s++)
103 if ((unsigned char) *s == TLN_IAC)
104 *p++ = *s;
105 else if (*s == '\n')
106 *p++ = '\r';
107 *p = 0;
108
109 return buf;
110 }
111
112 static void strip_telnet(int sock, char *buf, int *len)
113 {
114 unsigned char *p = (unsigned char *) buf, *o = (unsigned char *) buf;
115 int mark;
116
117 while (*p != 0) {
118 while ((*p != TLN_IAC) && (*p != 0))
119 *o++ = *p++;
120 if (*p == TLN_IAC) {
121 p++;
122 mark = 2;
123 if (!*p)
124 mark = 1; /* bogus */
125 if ((*p >= TLN_WILL) && (*p <= TLN_DONT)) {
126 mark = 3;
127 if (!*(p + 1))
128 mark = 2; /* bogus */
129 } else if (*p == TLN_WILL) {
130 /* WILL X -> response: DONT X */
131 /* except WILL ECHO which we just smile and ignore */
132 if (*(p + 1) != TLN_ECHO) {
133 write(sock, TLN_IAC_C TLN_DONT_C, 2);
134 write(sock, p + 1, 1);
135 }
136 } else if (*p == TLN_DO) {
137 /* DO X -> response: WONT X */
138 /* except DO ECHO which we just smile and ignore */
139 if (*(p + 1) != TLN_ECHO) {
140 write(sock, TLN_IAC_C TLN_WONT_C, 2);
141 write(sock, p + 1, 1);
142 }
143 } else if (*p == TLN_AYT) {
144 /* "Are You There?" */
145 /* response is: "Hell, yes!" */
146 write(sock, "\r\nHell, yes!\r\n", 14);
147 } else if (*p == TLN_IAC) {
148 /* IAC character in data, escaped with another IAC */
149 *o++ = *p++;
150 mark = 1;
151 }
152 /* Anything else can probably be ignored */
153 p += mark - 1;
154 *len = *len - mark;
155 }
156 }
157 *o = *p;
158 }
159
160 static void greet_new_bot(int idx)
161 {
162 int bfl = bot_flags(dcc[idx].user);
163 int i;
164
165 dcc[idx].timeval = now;
166 dcc[idx].u.bot->version[0] = 0;
167 dcc[idx].u.bot->numver = 0;
168 if (bfl & BOT_REJECT) {
169 putlog(LOG_BOTS, "*", DCC_REJECT, dcc[idx].nick);
170 dprintf(idx, "bye %s\n", "rejected");
171 killsock(dcc[idx].sock);
172 lostdcc(idx);
173 return;
174 }
175 if (bfl & BOT_LEAF)
176 dcc[idx].status |= STAT_LEAF;
177 dcc[idx].status |= STAT_LINKING;
178 #ifndef NO_OLD_BOTNET
179 dprintf(idx, "version %d %d %s <%s>\n", egg_numver, HANDLEN, ver, network);
180 #else
181 dprintf(idx, "v %d %d %s <%s>\n", egg_numver, HANDLEN, ver, network);
182 #endif
183 for (i = 0; i < dcc_total; i++)
184 if (dcc[i].type == &DCC_FORK_BOT) {
185 killsock(dcc[i].sock);
186 lostdcc(i);
187 }
188 }
189
190 static void bot_version(int idx, char *par)
191 {
192 char x[1024];
193 int l;
194
195 dcc[idx].timeval = now;
196 if (in_chain(dcc[idx].nick)) {
197 dprintf(idx, "error Sorry, already connected.\n");
198 dprintf(idx, "bye\n");
199 killsock(dcc[idx].sock);
200 lostdcc(idx);
201 return;
202 }
203 if ((par[0] >= '0') && (par[0] <= '9')) {
204 char *work;
205
206 work = newsplit(&par);
207 dcc[idx].u.bot->numver = atoi(work);
208 } else
209 dcc[idx].u.bot->numver = 0;
210
211 #ifndef NO_OLD_BOTNET
212 if (b_numver(idx) < NEAT_BOTNET) {
213 #if HANDLEN != 9
214 putlog(LOG_BOTS, "*", "Non-matching handle lengths with %s, they use 9 "
215 "characters.", dcc[idx].nick);
216 dprintf(idx, "error Non-matching handle length: mine %d, yours 9\n",
217 HANDLEN);
218 dprintf(idx, "bye %s\n", "bad handlen");
219 killsock(dcc[idx].sock);
220 lostdcc(idx);
221 return;
222 #else
223 dprintf(idx, "thisbot %s\n", botnetnick);
224 #endif
225 } else {
226 #endif
227 dprintf(idx, "tb %s\n", botnetnick);
228 l = atoi(newsplit(&par));
229 if (l != HANDLEN) {
230 putlog(LOG_BOTS, "*", "Non-matching handle lengths with %s, they use %d "
231 "characters.", dcc[idx].nick, l);
232 dprintf(idx, "error Non-matching handle length: mine %d, yours %d\n",
233 HANDLEN, l);
234 dprintf(idx, "bye %s\n", "bad handlen");
235 killsock(dcc[idx].sock);
236 lostdcc(idx);
237 return;
238 }
239 #ifndef NO_OLD_BOTNET
240 }
241 #endif
242 strncpyz(dcc[idx].u.bot->version, par, 120);
243 putlog(LOG_BOTS, "*", DCC_LINKED, dcc[idx].nick);
244 chatout("*** Linked to %s\n", dcc[idx].nick);
245 botnet_send_nlinked(idx, dcc[idx].nick, botnetnick, '!',
246 dcc[idx].u.bot->numver);
247 touch_laston(dcc[idx].user, "linked", now);
248 dump_links(idx);
249 dcc[idx].type = &DCC_BOT;
250 addbot(dcc[idx].nick, dcc[idx].nick, botnetnick, '-', dcc[idx].u.bot->numver);
251 check_tcl_link(dcc[idx].nick, botnetnick);
252 egg_snprintf(x, sizeof x, "v %d", dcc[idx].u.bot->numver);
253 bot_share(idx, x);
254 dprintf(idx, "el\n");
255 }
256
257 void failed_link(int idx)
258 {
259 char s[81], s1[512];
260
261 if (dcc[idx].port >= dcc[idx].u.bot->port + 3) {
262 if (dcc[idx].u.bot->linker[0]) {
263 egg_snprintf(s, sizeof s, "Couldn't link to %s.", dcc[idx].nick);
264 strcpy(s1, dcc[idx].u.bot->linker);
265 add_note(s1, botnetnick, s, -2, 0);
266 }
267 if (dcc[idx].u.bot->numver >= -1)
268 putlog(LOG_BOTS, "*", DCC_LINKFAIL, dcc[idx].nick);
269 killsock(dcc[idx].sock);
270 strcpy(s, dcc[idx].nick);
271 lostdcc(idx);
272 autolink_cycle(s); /* Check for more auto-connections */
273 return;
274 }
275
276 /* Try next port */
277 killsock(dcc[idx].sock);
278 dcc[idx].timeval = now;
279 if (open_telnet(idx, dcc[idx].host, dcc[idx].port + 1) < 0)
280 failed_link(idx);
281 }
282
283 static void cont_link(int idx, char *buf, int i)
284 {
285 char x[1024];
286 int atr = bot_flags(dcc[idx].user);
287 int users, bots;
288
289 if (atr & BOT_HUB) {
290 /* Disconnect all +a bots because we just got a hub */
291 for (i = 0; i < dcc_total; i++) {
292 if ((i != idx) && (bot_flags(dcc[i].user) & BOT_ALT)) {
293 if ((dcc[i].type == &DCC_FORK_BOT) || (dcc[i].type == &DCC_BOT_NEW)) {
294 killsock(dcc[i].sock);
295 lostdcc(i);
296 }
297 }
298 }
299 /* Just those currently in the process of linking */
300 if (in_chain(dcc[idx].nick)) {
301 i = nextbot(dcc[idx].nick);
302 if (i > 0) {
303 bots = bots_in_subtree(findbot(dcc[idx].nick));
304 users = users_in_subtree(findbot(dcc[idx].nick));
305 egg_snprintf(x, sizeof x,
306 "Unlinked %s (restructure) (lost %d bot%s and %d user%s)",
307 dcc[i].nick, bots, (bots != 1) ? "s" : "",
308 users, (users != 1) ? "s" : "");
309 chatout("*** %s\n", x);
310 botnet_send_unlinked(i, dcc[i].nick, x);
311 dprintf(i, "bye %s\n", "restructure");
312 killsock(dcc[i].sock);
313 lostdcc(i);
314 }
315 }
316 }
317 dcc[idx].type = &DCC_BOT_NEW;
318 dcc[idx].u.bot->numver = 0;
319
320 /* Don't send our password here, just the username. The code later on
321 * will determine if the password needs to be sent in cleartext or if
322 * we can send an MD5 digest. <cybah>
323 */
324 dprintf(idx, "%s\n", botnetnick);
325 return;
326 }
327
328 /* This function generates a digest by combining 'challenge' with
329 * 'password' and then sends it to the other bot. <Cybah>
330 */
331 static void dcc_bot_digest(int idx, char *challenge, char *password)
332 {
333 MD5_CTX md5context;
334 char digest_string[33]; /* 32 for digest in hex + null */
335 unsigned char digest[16];
336 int i;
337
338 MD5_Init(&md5context);
339 MD5_Update(&md5context, (unsigned char *) challenge, strlen(challenge));
340 MD5_Update(&md5context, (unsigned char *) password, strlen(password));
341 MD5_Final(digest, &md5context);
342
343 for (i = 0; i < 16; i++)
344 sprintf(digest_string + (i * 2), "%.2x", digest[i]);
345 dprintf(idx, "digest %s\n", digest_string);
346 putlog(LOG_BOTS, "*", "Received challenge from %s... sending response ...",
347 dcc[idx].nick);
348 }
349
350 static void dcc_bot_new(int idx, char *buf, int x)
351 {
352 struct userrec *u = get_user_by_handle(userlist, dcc[idx].nick);
353 char *code;
354
355 code = newsplit(&buf);
356 if (!egg_strcasecmp(code, "*hello!"))
357 greet_new_bot(idx);
358 else if (!egg_strcasecmp(code, "version") || !egg_strcasecmp(code, "v"))
359 bot_version(idx, buf);
360 else if (!egg_strcasecmp(code, "badpass"))
361 /* We entered the wrong password */
362 putlog(LOG_BOTS, "*", DCC_BADPASS, dcc[idx].nick);
363 else if (!egg_strcasecmp(code, "passreq")) {
364 char *pass = get_user(&USERENTRY_PASS, u);
365
366 #ifdef TLS
367 /* We got a STARTTLS request earlier. Switch to ssl NOW. Doing this
368 * in two steps is necessary in order to synchronize the handshake.
369 */
370 if (dcc[idx].status & STAT_STARTTLS) {
371 dcc[idx].ssl = 1;
372 if (ssl_handshake(dcc[idx].sock, TLS_CONNECT, tls_vfybots, LOG_BOTS,
373 dcc[idx].host, NULL))
374 putlog(LOG_BOTS, "*", "STARTTLS failed while linking to %s",
375 dcc[idx].nick);
376 dcc[idx].status &= ~STAT_STARTTLS;
377 }
378 #endif
379 if (!pass || !strcmp(pass, "-")) {
380 putlog(LOG_BOTS, "*", DCC_PASSREQ, dcc[idx].nick);
381 dprintf(idx, "-\n");
382 } else {
383 /* Determine if the other end supports an MD5 digest instead of a
384 * cleartext password. <Cybah>
385 */
386 if (buf && buf[0] && strchr(buf, '<') && strchr(buf + 1, '>'))
387 dcc_bot_digest(idx, buf, pass);
388 else
389 dprintf(idx, "%s\n", pass);
390 }
391 #ifdef TLS
392 } else if (!egg_strcasecmp(code, "starttls") && !dcc[idx].ssl) {
393 /* Mark the connection for secure communication, but don't switch yet.
394 * The hub has to send a plaintext passreq right after the starttls command
395 * and if we switch now, we'll break the handshake. Instead, we'll only
396 * send a confirmation to the peer and wait for the passreq.
397 */
398 putlog(LOG_BOTS, "*", "Got STARTTLS from %s. Replying...", dcc[idx].nick);
399 dcc[idx].status |= STAT_STARTTLS;
400 /* needs to have space to be distinguished from a plaintext password */
401 dprintf(idx, "starttls -\n");
402 #endif
403 } else if (!egg_strcasecmp(code, "error"))
404 putlog(LOG_BOTS, "*", DCC_LINKERROR, dcc[idx].nick, buf);
405 /* Ignore otherwise */
406 }
407
408 static void eof_dcc_bot_new(int idx)
409 {
410 putlog(LOG_BOTS, "*", DCC_LOSTBOT, dcc[idx].nick, dcc[idx].port);
411 killsock(dcc[idx].sock);
412 lostdcc(idx);
413 }
414
415 static void timeout_dcc_bot_new(int idx)
416 {
417 putlog(LOG_BOTS, "*", DCC_TIMEOUT, dcc[idx].nick,
418 dcc[idx].host, dcc[idx].port);
419 killsock(dcc[idx].sock);
420 lostdcc(idx);
421 }
422
423 static void display_dcc_bot_new(int idx, char *buf)
424 {
425 long tv;
426
427 tv = now - dcc[idx].timeval;
428 sprintf(buf, "bot* waited %lis", tv);
429 }
430
431 static int expmem_dcc_bot_(void *x)
432 {
433 return sizeof(struct bot_info);
434 }
435
436 static void free_dcc_bot_(int n, void *x)
437 {
438 if (dcc[n].type == &DCC_BOT) {
439 unvia(n, findbot(dcc[n].nick));
440 rembot(dcc[n].nick);
441 }
442 nfree(x);
443 }
444
445 struct dcc_table DCC_BOT_NEW = {
446 "BOT_NEW",
447 0,
448 eof_dcc_bot_new,
449 dcc_bot_new,
450 &bot_timeout,
451 timeout_dcc_bot_new,
452 display_dcc_bot_new,
453 expmem_dcc_bot_,
454 free_dcc_bot_,
455 NULL
456 };
457
458 /* Hash function for tandem bot commands */
459 extern botcmd_t C_bot[];
460
461 static void dcc_bot(int idx, char *code, int i)
462 {
463 char *msg;
464 int f;
465
466 if (raw_log) {
467 if (!strcmp(code, "s"))
468 putlog(LOG_BOTSHARE, "*", "{%s} %s", dcc[idx].nick, code + 2);
469 else
470 putlog(LOG_BOTNET, "*", "[%s] %s", dcc[idx].nick, code);
471 }
472 msg = strchr(code, ' ');
473 if (msg) {
474 *msg = 0;
475 msg++;
476 } else
477 msg = "";
478 for (f = i = 0; C_bot[i].name && !f; i++) {
479 int y = egg_strcasecmp(code, C_bot[i].name);
480
481 if (!y) {
482 /* Found a match */
483 (C_bot[i].func) (idx, msg);
484 f = 1;
485 } else if (y < 0)
486 return;
487 }
488 }
489
490 static void eof_dcc_bot(int idx)
491 {
492 char x[1024];
493 int bots, users;
494
495 bots = bots_in_subtree(findbot(dcc[idx].nick));
496 users = users_in_subtree(findbot(dcc[idx].nick));
497 egg_snprintf(x, sizeof x,
498 "Lost bot: %s (lost %d bot%s and %d user%s)",
499 dcc[idx].nick, bots, (bots != 1) ? "s" : "", users,
500 (users != 1) ? "s" : "");
501 putlog(LOG_BOTS, "*", "%s.", x);
502 chatout("*** %s\n", x);
503 botnet_send_unlinked(idx, dcc[idx].nick, x);
504 killsock(dcc[idx].sock);
505 lostdcc(idx);
506 }
507
508 static void display_dcc_bot(int idx, char *buf)
509 {
510 int i = simple_sprintf(buf, "bot flags: ");
511
512 buf[i++] = b_status(idx) & STAT_PINGED ? 'P' : 'p';
513 buf[i++] = b_status(idx) & STAT_SHARE ? 'U' : 'u';
514 buf[i++] = b_status(idx) & STAT_CALLED ? 'C' : 'c';
515 buf[i++] = b_status(idx) & STAT_OFFERED ? 'O' : 'o';
516 buf[i++] = b_status(idx) & STAT_SENDING ? 'S' : 's';
517 buf[i++] = b_status(idx) & STAT_GETTING ? 'G' : 'g';
518 buf[i++] = b_status(idx) & STAT_WARNED ? 'W' : 'w';
519 buf[i++] = b_status(idx) & STAT_LEAF ? 'L' : 'l';
520 buf[i++] = b_status(idx) & STAT_LINKING ? 'I' : 'i';
521 buf[i++] = b_status(idx) & STAT_AGGRESSIVE ? 'a' : 'A';
522 buf[i++] = 0;
523 }
524
525 static void display_dcc_fork_bot(int idx, char *buf)
526 {
527 sprintf(buf, "conn bot");
528 }
529
530 struct dcc_table DCC_BOT = {
531 "BOT",
532 DCT_BOT | DCT_VALIDIDX,
533 eof_dcc_bot,
534 dcc_bot,
535 NULL,
536 NULL,
537 display_dcc_bot,
538 expmem_dcc_bot_,
539 free_dcc_bot_,
540 NULL
541 };
542
543 struct dcc_table DCC_FORK_BOT = {
544 "FORK_BOT",
545 0,
546 failed_link,
547 cont_link,
548 &connect_timeout,
549 failed_link,
550 display_dcc_fork_bot,
551 expmem_dcc_bot_,
552 free_dcc_bot_,
553 NULL
554 };
555
556 /* This function generates a digest by combining a challenge consisting
557 * of our process id + connection time + botnetnick. The digest is then
558 * compared to the one given by the remote bot.
559 *
560 * Returns 1 if the digest matches, otherwise returns 0.
561 * <Cybah>
562 */
563 static int dcc_bot_check_digest(int idx, char *remote_digest)
564 {
565 MD5_CTX md5context;
566 char digest_string[33]; /* 32 for digest in hex + null */
567 unsigned char digest[16];
568 int i;
569 char *password = get_user(&USERENTRY_PASS, dcc[idx].user);
570
571 if (!password)
572 return 1;
573
574 MD5_Init(&md5context);
575
576 egg_snprintf(digest_string, 33, "<%x%x@", getpid(),
577 (unsigned int) dcc[idx].timeval);
578 MD5_Update(&md5context, (unsigned char *) digest_string,
579 strlen(digest_string));
580 MD5_Update(&md5context, (unsigned char *) botnetnick, strlen(botnetnick));
581 MD5_Update(&md5context, (unsigned char *) ">", 1);
582 MD5_Update(&md5context, (unsigned char *) password, strlen(password));
583
584 MD5_Final(digest, &md5context);
585
586 for (i = 0; i < 16; i++)
587 sprintf(digest_string + (i * 2), "%.2x", digest[i]);
588
589 if (!strcmp(digest_string, remote_digest))
590 return 1;
591
592 putlog(LOG_BOTS, "*", "Response (password hash) from %s incorrect",
593 dcc[idx].nick);
594 return 0;
595 }
596
597 static void dcc_chat_pass(int idx, char *buf, int atr)
598 {
599 if (!atr)
600 return;
601 if (dcc[idx].status & STAT_TELNET)
602 strip_telnet(dcc[idx].sock, buf, &atr);
603 else if (detect_telnet((unsigned char *) buf))
604 buf += 3; /* 'IAC','DO(DONT)','STATUS' */
605 atr = dcc[idx].user ? dcc[idx].user->flags : 0;
606
607 #ifdef TLS
608 if (atr & USER_BOT) {
609 if (!egg_strncasecmp(buf, "starttls ", 9)) {
610 dcc[idx].ssl = 1;
611 if (ssl_handshake(dcc[idx].sock, TLS_LISTEN, tls_vfybots, LOG_BOTS,
612 dcc[idx].host, NULL)) {
613 killsock(dcc[idx].sock);
614 lostdcc(idx);
615 }
616 return;
617 }
618 /* No password set? */
619 if (u_pass_match(dcc[idx].user, "-")) {
620 char ps[20];
621
622 makepass(ps);
623 set_user(&USERENTRY_PASS, dcc[idx].user, ps);
624 changeover_dcc(idx, &DCC_BOT_NEW, sizeof(struct bot_info));
625
626 dcc[idx].status = STAT_CALLED;
627 dprintf(idx, "*hello!\n");
628 greet_new_bot(idx);
629 #ifdef NO_OLD_BOTNET
630 dprintf(idx, "h %s\n", ps);
631 #else
632 dprintf(idx, "handshake %s\n", ps);
633 #endif
634 return;
635 }
636 }
637 #endif
638 /* Check for MD5 digest from remote _bot_. <cybah> */
639 if ((atr & USER_BOT) && !egg_strncasecmp(buf, "digest ", 7)) {
640 if (dcc_bot_check_digest(idx, buf + 7)) {
641 nfree(dcc[idx].u.chat);
642 dcc[idx].type = &DCC_BOT_NEW;
643 dcc[idx].u.bot = get_data_ptr(sizeof(struct bot_info));
644 dcc[idx].status = STAT_CALLED;
645 dprintf(idx, "*hello!\n");
646 greet_new_bot(idx);
647 return;
648 } else {
649 /* Invalid password/digest */
650 dprintf(idx, "badpass\n");
651 putlog(LOG_MISC, "*", DCC_BADLOGIN, dcc[idx].nick, dcc[idx].host,
652 dcc[idx].port);
653 killsock(dcc[idx].sock);
654 lostdcc(idx);
655 return;
656 }
657 }
658
659 #ifdef TLS
660 /* Skip checking the password if the user is already identified by
661 * fingerprint.
662 */
663 if (dcc[idx].status & STAT_FPRINT || u_pass_match(dcc[idx].user, buf)) {
664 #else
665 if (u_pass_match(dcc[idx].user, buf)) {
666 #endif
667 if (atr & USER_BOT) {
668 nfree(dcc[idx].u.chat);
669 dcc[idx].type = &DCC_BOT_NEW;
670 dcc[idx].u.bot = get_data_ptr(sizeof(struct bot_info));
671
672 dcc[idx].status = STAT_CALLED;
673 dprintf(idx, "*hello!\n");
674 greet_new_bot(idx);
675 } else {
676 /* Log entry for successful login -slennox 3/28/1999 */
677 putlog(LOG_MISC, "*", DCC_LOGGEDIN, dcc[idx].nick,
678 dcc[idx].host, dcc[idx].port);
679 if (dcc[idx].u.chat->away) {
680 nfree(dcc[idx].u.chat->away);
681 dcc[idx].u.chat->away = NULL;
682 }
683 dcc[idx].type = &DCC_CHAT;
684 dcc[idx].status &= ~STAT_CHAT;
685 dcc[idx].u.chat->con_flags = (atr & USER_MASTER) ? conmask : 0;
686 dcc[idx].u.chat->channel = -2;
687 /* Turn echo back on for telnet sessions (send IAC WON'T ECHO). */
688 if (dcc[idx].status & STAT_TELNET)
689 tputs(dcc[idx].sock, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n", 4);
690 dcc_chatter(idx);
691 }
692 } else {
693 if (atr & USER_BOT)
694 dprintf(idx, "badpass\n");
695 else
696 dprintf(idx, DCC_HOUSTON);
697 putlog(LOG_MISC, "*", DCC_BADLOGIN, dcc[idx].nick,
698 dcc[idx].host, dcc[idx].port);
699 if (dcc[idx].u.chat->away) { /* su from a dumb user */
700 /* Turn echo back on for telnet sessions (send IAC WON'T ECHO). */
701 if (dcc[idx].status & STAT_TELNET)
702 tputs(dcc[idx].sock, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n", 4);
703 dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->away);
704 strcpy(dcc[idx].nick, dcc[idx].u.chat->away);
705 nfree(dcc[idx].u.chat->away);
706 nfree(dcc[idx].u.chat->su_nick);
707 dcc[idx].u.chat->away = NULL;
708 dcc[idx].u.chat->su_nick = NULL;
709 dcc[idx].type = &DCC_CHAT;
710 if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
711 botnet_send_join_idx(idx, -1);
712 chanout_but(-1, dcc[idx].u.chat->channel, DCC_JOIN, dcc[idx].nick);
713 } else {
714 killsock(dcc[idx].sock);
715 lostdcc(idx);
716 }
717 }
718 }
719
720 static void eof_dcc_general(int idx)
721 {
722 putlog(LOG_MISC, "*", DCC_LOSTDCC, dcc[idx].nick,
723 dcc[idx].host, dcc[idx].port);
724 killsock(dcc[idx].sock);
725 lostdcc(idx);
726 }
727
728 static void tout_dcc_chat_pass(int idx)
729 {
730 dprintf(idx, "Timeout.\n");
731 putlog(LOG_MISC, "*", DCC_PWDTIMEOUT, dcc[idx].nick, dcc[idx].host);
732 killsock(dcc[idx].sock);
733 lostdcc(idx);
734 }
735
736 static void display_dcc_chat_pass(int idx, char *buf)
737 {
738 long tv;
739
740 tv = now - dcc[idx].timeval;
741 sprintf(buf, "pass waited %lis", tv);
742 }
743
744 static int expmem_dcc_general(void *x)
745 {
746 register struct chat_info *p = (struct chat_info *) x;
747 int tot = sizeof(struct chat_info);
748
749 if (p->away)
750 tot += strlen(p->away) + 1;
751 if (p->buffer) {
752 struct msgq *q = p->buffer;
753
754 while (q) {
755 tot += sizeof(struct list_type);
756
757 tot += q->len + 1;
758 q = q->next;
759 }
760 }
761 if (p->su_nick)
762 tot += strlen(p->su_nick) + 1;
763 return tot;
764 }
765
766 static void kill_dcc_general(int idx, void *x)
767 {
768 register struct chat_info *p = (struct chat_info *) x;
769
770 if (p) {
771 if (p->buffer) {
772 struct msgq *r, *q;
773
774 for (r = dcc[idx].u.chat->buffer; r; r = q) {
775 q = r->next;
776 nfree(r->msg);
777 nfree(r);
778 }
779 }
780 if (p->away) {
781 nfree(p->away);
782 }
783 nfree(p);
784 }
785 }
786
787 /* Remove the color control codes that mIRC,pIRCh etc use to make
788 * their client seem so fecking cool! (Sorry, Khaled, you are a nice
789 * guy, but when you added this feature you forced people to either
790 * use your *SHAREWARE* client or face screenfulls of crap!)
791 */
792 void strip_mirc_codes(int flags, char *text)
793 {
794 char *dd = text;
795
796 while (*text) {
797 switch (*text) {
798 case 2: /* Bold text */
799 if (flags & STRIP_BOLD) {
800 text++;
801 continue;
802 }
803 break;
804 case 3: /* mIRC colors? */
805 if (flags & STRIP_COLOR) {
806 if (egg_isdigit(text[1])) { /* Is the first char a number? */
807 text += 2; /* Skip over the ^C and the first digit */
808 if (egg_isdigit(*text))
809 text++; /* Is this a double digit number? */
810 if (*text == ',') { /* Do we have a background color next? */
811 if (egg_isdigit(text[1]))
812 text += 2; /* Skip over the first background digit */
813 if (egg_isdigit(*text))
814 text++; /* Is it a double digit? */
815 }
816 } else
817 text++;
818 continue;
819 }
820 break;
821 case 7:
822 if (flags & STRIP_BELLS) {
823 text++;
824 continue;
825 }
826 break;
827 case 0x16: /* Reverse video */
828 if (flags & STRIP_REV) {
829 text++;
830 continue;
831 }
832 break;
833 case 0x1f: /* Underlined text */
834 if (flags & STRIP_UNDER) {
835 text++;
836 continue;
837 }
838 break;
839 case 033:
840 if (flags & STRIP_ANSI) {
841 text++;
842 if (*text == '[') {
843 text++;
844 while ((*text == ';') || egg_isdigit(*text))
845 text++;
846 if (*text)
847 text++; /* also kill the following char */
848 }
849 continue;
850 }
851 break;
852 }
853 *dd++ = *text++; /* Move on to the next char */
854 }
855 *dd = 0;
856 }
857
858 static void append_line(int idx, char *line)
859 {
860 int l = strlen(line);
861 struct msgq *p, *q;
862 struct chat_info *c = (dcc[idx].type == &DCC_CHAT) ? dcc[idx].u.chat :
863 dcc[idx].u.file->chat;
864
865 if (c->current_lines > 1000) {
866 /* They're probably trying to fill up the bot nuke the sods :) */
867 for (p = c->buffer; p; p = q) {
868 q = p->next;
869 nfree(p->msg);
870 nfree(p);
871 }
872 c->buffer = 0;
873 dcc[idx].status &= ~STAT_PAGE;
874 do_boot(idx, botnetnick, "too many pages - sendq full");
875 return;
876 }
877 if ((c->line_count < c->max_line) && (c->buffer == NULL)) {
878 c->line_count++;
879 tputs(dcc[idx].sock, line, l);
880 } else {
881 c->current_lines++;
882 if (c->buffer == NULL)
883 q = NULL;
884 else
885 for (q = c->buffer; q->next; q = q->next);
886
887 p = get_data_ptr(sizeof(struct msgq));
888
889 p->len = l;
890 p->msg = get_data_ptr(l + 1);
891 p->next = NULL;
892 strcpy(p->msg, line);
893 if (q == NULL)
894 c->buffer = p;
895 else
896 q->next = p;
897 }
898 }
899
900
901 static void out_dcc_general(int idx, char *buf, void *x)
902 {
903 register struct chat_info *p = (struct chat_info *) x;
904 char *y = buf;
905
906 strip_mirc_codes(p->strip_flags, buf);
907 if (dcc[idx].status & STAT_TELNET)
908 y = escape_telnet(buf);
909 if (dcc[idx].status & STAT_PAGE)
910 append_line(idx, y);
911 else
912 tputs(dcc[idx].sock, y, strlen(y));
913 }
914
915 struct dcc_table DCC_CHAT_PASS = {
916 "CHAT_PASS",
917 0,
918 eof_dcc_general,
919 dcc_chat_pass,
920 &password_timeout,
921 tout_dcc_chat_pass,
922 display_dcc_chat_pass,
923 expmem_dcc_general,
924 kill_dcc_general,
925 out_dcc_general
926 };
927
928 /* Make sure ANSI code is just for color-changing */
929 int check_ansi(char *v)
930 {
931 int count = 2;
932
933 if (*v++ != '\033')
934 return 1;
935 if (*v++ != '[')
936 return 1;
937 while (*v) {
938 if (*v == 'm')
939 return 0;
940 if ((*v != ';') && ((*v < '0') || (*v > '9')))
941 return count;
942 v++;
943 count++;
944 }
945 return count;
946 }
947
948 static void eof_dcc_chat(int idx)
949 {
950 putlog(LOG_MISC, "*", DCC_LOSTDCC, dcc[idx].nick,
951 dcc[idx].host, dcc[idx].port);
952 if (dcc[idx].u.chat->channel >= 0) {
953 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s lost dcc link.\n",
954 dcc[idx].nick);
955 if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
956 botnet_send_part_idx(idx, "lost dcc link");
957 check_tcl_chpt(botnetnick, dcc[idx].nick, dcc[idx].sock,
958 dcc[idx].u.chat->channel);
959 }
960 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
961 killsock(dcc[idx].sock);
962 lostdcc(idx);
963 }
964
965 static void dcc_chat(int idx, char *buf, int i)
966 {
967 int nathan = 0, doron = 0, fixed = 0;
968 char *v, *d, filtbuf[2048];
969
970 if (dcc[idx].status & STAT_TELNET)
971 strip_telnet(dcc[idx].sock, buf, &i);
972 if (buf[0] && (buf[0] != '.') &&
973 detect_dcc_flood(&dcc[idx].timeval, dcc[idx].u.chat, idx))
974 return;
975 dcc[idx].timeval = now;
976 if (buf[0]) {
977 const char *filt = check_tcl_filt(idx, buf);
978 if (filt != buf) {
979 strncpyz(filtbuf, filt, sizeof(filtbuf));
980 buf = filtbuf;
981 }
982 }
983 if (buf[0]) {
984 /* Check for beeps and cancel annoying ones */
985 v = buf;
986 d = buf;
987 while (*v)
988 switch (*v) {
989 case 7: /* Beep - no more than 3 */
990 nathan++;
991 if (nathan > 3)
992 v++;
993 else
994 *d++ = *v++;
995 break;
996 case 8: /* Backspace - for lame telnet's :) */
997 if (d > buf) {
998 d--;
999 }
1000 v++;
1001 break;
1002 case 27: /* ESC - ansi code? */
1003 doron = check_ansi(v);
1004 /* If it's valid, append a return-to-normal code at the end */
1005 if (!doron) {
1006 *d++ = *v++;
1007 fixed = 1;
1008 } else
1009 v += doron;
1010 break;
1011 case '\r': /* Weird pseudo-linefeed */
1012 v++;
1013 break;
1014 default:
1015 *d++ = *v++;
1016 }
1017 if (fixed)
1018 strcpy(d, "\033[0m");
1019 else
1020 *d = 0;
1021 if (buf[0]) { /* Nothing to say - maybe paging... */
1022 if ((buf[0] == '.') || (dcc[idx].u.chat->channel < 0)) {
1023 if (buf[0] == '.')
1024 buf++;
1025 v = newsplit(&buf);
1026 rmspace(buf);
1027 if (check_tcl_dcc(v, idx, buf)) {
1028 if (dcc[idx].u.chat->channel >= 0)
1029 check_tcl_chpt(botnetnick, dcc[idx].nick, dcc[idx].sock,
1030 dcc[idx].u.chat->channel);
1031 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
1032 dprintf(idx, "*** Ja mata!\n");
1033 flush_lines(idx, dcc[idx].u.chat);
1034 putlog(LOG_MISC, "*", DCC_CLOSED, dcc[idx].nick, dcc[idx].host);
1035 if (dcc[idx].u.chat->channel >= 0) {
1036 chanout_but(-1, dcc[idx].u.chat->channel,
1037 "*** %s left the party line%s%s\n",
1038 dcc[idx].nick, buf[0] ? ": " : ".", buf);
1039 if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
1040 botnet_send_part_idx(idx, buf);
1041 }
1042 if (dcc[idx].u.chat->su_nick) {
1043 dcc[idx].user = get_user_by_handle(userlist,
1044 dcc[idx].u.chat->su_nick);
1045 strcpy(dcc[idx].nick, dcc[idx].u.chat->su_nick);
1046 dcc[idx].type = &DCC_CHAT;
1047 dprintf(idx, "Returning to real nick %s!\n",
1048 dcc[idx].u.chat->su_nick);
1049 nfree(dcc[idx].u.chat->su_nick);
1050 dcc[idx].u.chat->su_nick = NULL;
1051 dcc_chatter(idx);
1052 if (dcc[idx].u.chat->channel < GLOBAL_CHANS &&
1053 dcc[idx].u.chat->channel >= 0)
1054 botnet_send_join_idx(idx, -1);
1055 return;
1056 } else if ((dcc[idx].sock != STDOUT) || backgrd) {
1057 killsock(dcc[idx].sock);
1058 lostdcc(idx);
1059 return;
1060 } else {
1061 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
1062 dcc_chatter(idx);
1063 return;
1064 }
1065 }
1066 } else if (buf[0] == ',') {
1067 int me = 0;
1068
1069 if ((buf[1] == 'm') && (buf[2] == 'e') && buf[3] == ' ')
1070 me = 1;
1071 for (i = 0; i < dcc_total; i++) {
1072 int ok = 0;
1073
1074 if ((dcc[i].type->flags & DCT_MASTER) &&
1075 ((dcc[i].type != &DCC_CHAT) || (dcc[i].u.chat->channel >= 0)) &&
1076 ((i != idx) || (dcc[idx].status & STAT_ECHO)))
1077 ok = 1;
1078 if (ok) {
1079 struct userrec *u = get_user_by_handle(userlist, dcc[i].nick);
1080
1081 if (u && (u->flags & USER_MASTER)) {
1082 if (me)
1083 dprintf(i, "-> %s%s\n", dcc[idx].nick, buf + 3);
1084 else
1085 dprintf(i, "-%s-> %s\n", dcc[idx].nick, buf + 1);
1086 }
1087 }
1088 }
1089 } else if (buf[0] == '\'') {
1090 int me = 0;
1091
1092 if ((buf[1] == 'm') && (buf[2] == 'e') &&
1093 ((buf[3] == ' ') || (buf[3] == '\'') || (buf[3] == ',')))
1094 me = 1;
1095 for (i = 0; i < dcc_total; i++) {
1096 if (dcc[i].type->flags & DCT_CHAT) {
1097 if (me)
1098 dprintf(i, "=> %s%s\n", dcc[idx].nick, buf + 3);
1099 else
1100 dprintf(i, "=%s=> %s\n", dcc[idx].nick, buf + 1);
1101 }
1102 }
1103 } else {
1104 if (dcc[idx].u.chat->away != NULL)
1105 not_away(idx);
1106 if (dcc[idx].status & STAT_ECHO)
1107 chanout_but(-1, dcc[idx].u.chat->channel,
1108 "<%s> %s\n", dcc[idx].nick, buf);
1109 else
1110 chanout_but(idx, dcc[idx].u.chat->channel, "<%s> %s\n",
1111 dcc[idx].nick, buf);
1112 botnet_send_chan(-1, botnetnick, dcc[idx].nick,
1113 dcc[idx].u.chat->channel, buf);
1114 check_tcl_chat(dcc[idx].nick, dcc[idx].u.chat->channel, buf);
1115 }
1116 }
1117 }
1118 if (dcc[idx].type == &DCC_CHAT) /* Could have change to files */
1119 if (dcc[idx].status & STAT_PAGE)
1120 flush_lines(idx, dcc[idx].u.chat);
1121 }
1122
1123 static void display_dcc_chat(int idx, char *buf)
1124 {
1125 int i = simple_sprintf(buf, "chat flags: ");
1126
1127 buf[i++] = dcc[idx].status & STAT_CHAT ? 'C' : 'c';
1128 buf[i++] = dcc[idx].status & STAT_PARTY ? 'P' : 'p';
1129 buf[i++] = dcc[idx].status & STAT_TELNET ? 'T' : 't';
1130 buf[i++] = dcc[idx].status & STAT_ECHO ? 'E' : 'e';
1131 buf[i++] = dcc[idx].status & STAT_PAGE ? 'P' : 'p';
1132 simple_sprintf(buf + i, "/%d", dcc[idx].u.chat->channel);
1133 }
1134
1135 struct dcc_table DCC_CHAT = {
1136 "CHAT",
1137 DCT_CHAT | DCT_MASTER | DCT_SHOWWHO | DCT_VALIDIDX | DCT_SIMUL |
1138 DCT_CANBOOT | DCT_REMOTEWHO,
1139 eof_dcc_chat,
1140 dcc_chat,
1141 NULL,
1142 NULL,
1143 display_dcc_chat,
1144 expmem_dcc_general,
1145 kill_dcc_general,
1146 out_dcc_general
1147 };
1148
1149 static int lasttelnets;
1150 static char lasttelnethost[81];
1151 static time_t lasttelnettime;
1152
1153 /* A modified detect_flood for incoming telnet flood protection.
1154 */
1155 static int detect_telnet_flood(char *floodhost)
1156 {
1157 struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
1158
1159 get_user_flagrec(get_user_by_host(floodhost), &fr, NULL);
1160 if (!flood_telnet_thr || (glob_friend(fr) && !par_telnet_flood))
1161 return 0; /* No flood protection */
1162 if (egg_strcasecmp(lasttelnethost, floodhost)) { /* New... */
1163 strcpy(lasttelnethost, floodhost);
1164 lasttelnettime = now;
1165 lasttelnets = 0;
1166 return 0;
1167 }
1168 if (lasttelnettime < now - flood_telnet_time) {
1169 /* Flood timer expired, reset it */
1170 lasttelnettime = now;
1171 lasttelnets = 0;
1172 return 0;
1173 }
1174 lasttelnets++;
1175 if (lasttelnets >= flood_telnet_thr) { /* FLOOD! */
1176 /* Reset counters */
1177 lasttelnets = 0;
1178 lasttelnettime = 0;
1179 lasttelnethost[0] = 0;
1180 putlog(LOG_MISC, "*", IRC_TELNETFLOOD, floodhost);
1181 addignore(floodhost, origbotname, "Telnet connection flood",
1182 now + (60 * ignore_time));
1183 return 1;
1184 }
1185 return 0;
1186 }
1187
1188 static void dcc_telnet(int idx, char *buf, int i)
1189 {
1190 unsigned short port;
1191 int j = 0, sock;
1192
1193 if (dcc_total + 1 > max_dcc && increase_socks_max()) {
1194 sockname_t name;
1195 j = answer(dcc[idx].sock, &name, &port, 0);
1196 if (j != -1) {
1197 dprintf(-j, "Sorry, too many connections already.\r\n");
1198 killsock(j);
1199 }
1200 return;
1201 }
1202 i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
1203 sock = answer(dcc[idx].sock, &dcc[i].sockname, &port, 0);
1204 while ((sock == -1) && (errno == EAGAIN))
1205 sock = answer(dcc[idx].sock, &dcc[i].sockname, &port, 0);
1206 if (sock < 0) {
1207 putlog(LOG_MISC, "*", DCC_FAILED, strerror(errno));
1208 return;
1209 }
1210 /* Buffer data received on this socket. */
1211 sockoptions(sock, EGG_OPTION_SET, SOCK_BUFFER);
1212
1213 #if (SIZEOF_SHORT == 2)
1214 if (port < 1024) {
1215 #else
1216 if (port < 1024 || port > 65535) {
1217 #endif
1218 putlog(LOG_BOTS, "*", DCC_BADSRC, iptostr(&dcc[i].sockname.addr.sa), port);
1219 killsock(sock);
1220 lostdcc(i);
1221 return;
1222 }
1223
1224 dcc[i].u.dns->ip = &dcc[i].sockname;
1225 dcc[i].sock = sock;
1226 dcc[i].port = port;
1227 #ifdef TLS
1228 if (dcc[idx].ssl && ssl_handshake(sock, TLS_LISTEN, tls_vfyclients,
1229 LOG_MISC, NULL, NULL)) {
1230 killsock(sock);
1231 lostdcc(i);
1232 return;
1233 }
1234 dcc[i].ssl = dcc[idx].ssl;
1235 #endif
1236 dcc[i].timeval = now;
1237 strcpy(dcc[i].nick, "*");
1238 dcc[i].u.dns->dns_success = dcc_telnet_hostresolved;
1239 dcc[i].u.dns->dns_failure = dcc_telnet_hostresolved;
1240 dcc[i].u.dns->dns_type = RES_HOSTBYIP;
1241 dcc[i].u.dns->ibuf = dcc[idx].sock;
1242 dcc[i].u.dns->type = &DCC_IDENTWAIT;
1243 dcc_dnshostbyip(&dcc[i].sockname);
1244 }
1245
1246 static void dcc_telnet_hostresolved(int i)
1247 {
1248 int idx;
1249 int j = 0, sock;
1250 char s[UHOSTLEN + 20];
1251
1252 strncpyz(dcc[i].host, dcc[i].u.dns->host, UHOSTLEN);
1253
1254 for (idx = 0; idx < dcc_total; idx++)
1255 if ((dcc[idx].type == &DCC_TELNET) &&
1256 (dcc[idx].sock == dcc[i].u.dns->ibuf)) {
1257 break;
1258 }
1259 if (dcc_total == idx) {
1260 putlog(LOG_BOTS, "*", "Lost listening socket while resolving %s",
1261 dcc[i].host);
1262 killsock(dcc[i].sock);
1263 lostdcc(i);
1264 return;
1265 }
1266 if (dcc[idx].host[0] == '@') {
1267 /* Restrict by hostname */
1268 if (!wild_match(dcc[idx].host + 1, dcc[i].host)) {
1269 putlog(LOG_BOTS, "*", DCC_BADHOST, dcc[i].host);
1270 killsock(dcc[i].sock);
1271 lostdcc(i);
1272 return;
1273 }
1274 }
1275 sprintf(s, "-telnet!telnet@%s", dcc[i].host);
1276 if (match_ignore(s) || detect_telnet_flood(s)) {
1277 killsock(dcc[i].sock);
1278 lostdcc(i);
1279 return;
1280 }
1281
1282 putlog(LOG_MISC, "*", DCC_TELCONN, dcc[i].host, dcc[i].port);
1283 changeover_dcc(i, &DCC_IDENTWAIT, 0);
1284 dcc[i].timeval = now;
1285 dcc[i].u.ident_sock = dcc[idx].sock;
1286 sock = -1;
1287 j = new_dcc(&DCC_IDENT, 0);
1288 if (j < 0)
1289 putlog(LOG_MISC, "*", DCC_IDENTFAIL, dcc[i].host, strerror(errno));
1290 else {
1291 egg_memcpy(&dcc[j].sockname, &dcc[i].sockname, sizeof(sockname_t));
1292 dcc[j].sock = getsock(dcc[j].sockname.family, 0);
1293 if (dcc[j].sock >= 0) {
1294 sockname_t name;
1295 name.addrlen = sizeof(name.addr);
1296 getsockname(dcc[i].sock, &name.addr.sa, &name.addrlen);
1297 bind(dcc[j].sock, &name.addr.sa, name.addrlen);
1298 setsnport(dcc[j].sockname, 113);
1299 if (connect(dcc[j].sock, &dcc[j].sockname.addr.sa,
1300 dcc[j].sockname.addrlen) < 0 && (errno != EINPROGRESS)) {
1301 killsock(dcc[j].sock);
1302 lostdcc(j);
1303 putlog(LOG_MISC, "*", DCC_IDENTFAIL, dcc[i].host, strerror(errno));
1304 j = 0;
1305 }
1306 sock = dcc[j].sock;
1307 }
1308 }
1309 if (j < 0) {
1310 sprintf(s, "telnet@%s", dcc[i].host);
1311 dcc_telnet_got_ident(i, s);
1312 return;
1313 }
1314 dcc[j].sock = sock;
1315 dcc[j].port = 113;
1316 dcc[j].addr = dcc[i].addr;
1317 strcpy(dcc[j].host, dcc[i].host);
1318 strcpy(dcc[j].nick, "*");
1319 dcc[j].u.ident_sock = dcc[i].sock;
1320 dcc[j].timeval = now;
1321 dprintf(j, "%d, %d\n", dcc[i].port, dcc[idx].port);
1322 }
1323
1324 static void eof_dcc_telnet(int idx)
1325 {
1326 putlog(LOG_MISC, "*", DCC_PORTDIE, dcc[idx].port);
1327 killsock(dcc[idx].sock);
1328 lostdcc(idx);
1329 }
1330
1331 static void display_telnet(int idx, char *buf)
1332 {
1333 sprintf(buf, "lstn %d%s", dcc[idx].port,
1334 (dcc[idx].status & LSTN_PUBLIC) ? " pub" : "");
1335 }
1336
1337 struct dcc_table DCC_TELNET = {
1338 "TELNET",
1339 DCT_LISTEN,
1340 eof_dcc_telnet,
1341 dcc_telnet,
1342 NULL,
1343 NULL,
1344 display_telnet,
1345 NULL,
1346 NULL,
1347 NULL
1348 };
1349
1350 static void eof_dcc_dupwait(int idx)
1351 {
1352 putlog(LOG_BOTS, "*", DCC_LOSTDUP, dcc[idx].host);
1353 killsock(dcc[idx].sock);
1354 lostdcc(idx);
1355 }
1356
1357 static void dcc_dupwait(int idx, char *buf, int i)
1358 {
1359 /* We just ignore any data at this point. */
1360 return;
1361 }
1362
1363 /* We now check again. If the bot is still marked as duplicate, there is no
1364 * botnet lag we could push it on, so we just drop the connection.
1365 */
1366 static void timeout_dupwait(int idx)
1367 {
1368 char x[100];
1369
1370 /* Still duplicate? */
1371 if (in_chain(dcc[idx].nick)) {
1372 egg_snprintf(x, sizeof x, "%s!%s", dcc[idx].nick, dcc[idx].host);
1373 dprintf(idx, "error Already connected.\n");
1374 putlog(LOG_BOTS, "*", DCC_DUPLICATE, x);
1375 killsock(dcc[idx].sock);
1376 lostdcc(idx);
1377 } else {
1378 /* Ha! Now it's gone and we can grant this bot access. */
1379 dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
1380 }
1381 }
1382
1383 static void display_dupwait(int idx, char *buf)
1384 {
1385 sprintf(buf, "wait duplicate?");
1386 }
1387
1388 static int expmem_dupwait(void *x)
1389 {
1390 register struct dupwait_info *p = (struct dupwait_info *) x;
1391 int tot = sizeof(struct dupwait_info);
1392
1393 if (p && p->chat && DCC_CHAT.expmem)
1394 tot += DCC_CHAT.expmem(p->chat);
1395 return tot;
1396 }
1397
1398 static void kill_dupwait(int idx, void *x)
1399 {
1400 register struct dupwait_info *p = (struct dupwait_info *) x;
1401
1402 if (p) {
1403 if (p->chat && DCC_CHAT.kill)
1404 DCC_CHAT.kill(idx, p->chat);
1405 nfree(p);
1406 }
1407 }
1408
1409 struct dcc_table DCC_DUPWAIT = {
1410 "DUPWAIT",
1411 DCT_VALIDIDX,
1412 eof_dcc_dupwait,
1413 dcc_dupwait,
1414 &dupwait_timeout,
1415 timeout_dupwait,
1416 display_dupwait,
1417 expmem_dupwait,
1418 kill_dupwait,
1419 NULL
1420 };
1421
1422 /* This function is called if a bot gets removed from the list. It checks
1423 * wether we have a pending duplicate connection for that bot and continues
1424 * with the login in that case.
1425 */
1426 void dupwait_notify(char *who)
1427 {
1428 register int idx;
1429
1430 Assert(who);
1431 for (idx = 0; idx < dcc_total; idx++)
1432 if ((dcc[idx].type == &DCC_DUPWAIT) &&
1433 !egg_strcasecmp(dcc[idx].nick, who)) {
1434 dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
1435 break;
1436 }
1437 }
1438
1439 static void dcc_telnet_id(int idx, char *buf, int atr)
1440 {
1441 int ok = 0;
1442 struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
1443
1444 if (detect_telnet((unsigned char *) buf)) {
1445 dcc[idx].status |= STAT_TELNET;
1446 strip_telnet(dcc[idx].sock, buf, &atr);
1447 } else
1448 dcc[idx].status &= ~STAT_TELNET;
1449 buf[HANDLEN] = 0;
1450 /* Toss out bad nicknames */
1451 if (dcc[idx].nick[0] != '@' && !wild_match(dcc[idx].nick, buf)) {
1452 dprintf(idx, "Sorry, that nickname format is invalid.\n");
1453 putlog(LOG_BOTS, "*", DCC_BADNICK, dcc[idx].host);
1454 killsock(dcc[idx].sock);
1455 lostdcc(idx);
1456 return;
1457 }
1458 dcc[idx].user = get_user_by_handle(userlist, buf);
1459 get_user_flagrec(dcc[idx].user, &fr, NULL);
1460 #ifdef TLS
1461 if (dcc[idx].ssl && (tls_auth == 2)) {
1462 char *uid = ssl_getuid(dcc[idx].sock);
1463
1464 if (!uid || strcasecmp(uid, buf)) {
1465 if (glob_bot(fr))
1466 dprintf(idx, "error Certificate UID doesn't match handle\n");
1467 else
1468 dprintf(idx, "Your certificate UID doesn't match your handle.\n");
1469 killsock(dcc[idx].sock);
1470 lostdcc(idx);
1471 return;
1472 }
1473 }
1474 #endif
1475 /* Make sure users-only/bots-only connects are honored */
1476 if ((dcc[idx].status & STAT_BOTONLY) && !glob_bot(fr)) {
1477 dprintf(idx, "This telnet port is for bots only.\n");
1478 putlog(LOG_BOTS, "*", DCC_NONBOT, dcc[idx].host);
1479 killsock(dcc[idx].sock);
1480 lostdcc(idx);
1481 return;
1482 }
1483 if ((dcc[idx].status & STAT_USRONLY) && glob_bot(fr)) {
1484 dprintf(idx, "error Only users may connect at this port.\n");
1485 putlog(LOG_BOTS, "*", DCC_NONUSER, dcc[idx].host);
1486 killsock(dcc[idx].sock);
1487 lostdcc(idx);
1488 return;
1489 }
1490 dcc[idx].status &= ~(STAT_BOTONLY | STAT_USRONLY);
1491 if (!egg_strcasecmp(buf, "NEW") && (allow_new_telnets || make_userfile)) {
1492 dcc[idx].type = &DCC_TELNET_NEW;
1493 dcc[idx].timeval = now;
1494 dprintf(idx, "\n");
1495 dprintf(idx, IRC_TELNET, botnetnick);
1496 dprintf(idx, IRC_TELNET1);
1497 dprintf(idx, "\nEnter the nickname you would like to use.\n");
1498 return;
1499 }
1500 if (chan_op(fr)) {
1501 if (!require_p)
1502 ok = 1;
1503 }
1504 if (!ok && (glob_party(fr) || glob_bot(fr)))
1505 ok = 1;
1506
1507 if (!ok) {
1508 dprintf(idx, "You don't have access.\n");
1509 putlog(LOG_BOTS, "*", DCC_INVHANDLE, dcc[idx].host, buf);
1510 killsock(dcc[idx].sock);
1511 lostdcc(idx);
1512 return;
1513 }
1514 correct_handle(buf);
1515 strcpy(dcc[idx].nick, buf);
1516 if (glob_bot(fr)) {
1517 if (!egg_strcasecmp(botnetnick, dcc[idx].nick)) {
1518 dprintf(idx, "error You cannot link using my botnetnick.\n");
1519 putlog(LOG_BOTS, "*", DCC_MYBOTNETNICK, dcc[idx].host);
1520 killsock(dcc[idx].sock);
1521 lostdcc(idx);
1522 return;
1523 } else if (in_chain(dcc[idx].nick)) {
1524 struct chat_info *ci;
1525
1526 ci = dcc[idx].u.chat;
1527 dcc[idx].type = &DCC_DUPWAIT;
1528 dcc[idx].u.dupwait = get_data_ptr(sizeof(struct dupwait_info));
1529 dcc[idx].u.dupwait->chat = ci;
1530 dcc[idx].u.dupwait->atr = atr;
1531 return;
1532 }
1533 }
1534 dcc_telnet_pass(idx, atr);
1535 }
1536
1537 #ifdef TLS
1538 int dcc_fingerprint(idx)
1539 {
1540 char *cf, *uf;
1541 struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
1542
1543 get_user_flagrec(dcc[idx].user, &fr, NULL);
1544 /* Check if fingerprint authentication is allowed or required. */
1545 if (dcc[idx].ssl && tls_auth) {
1546 /* Get the fingerprint of the current certificate */
1547 cf = ssl_getfp(dcc[idx].sock);
1548 /* Get the fingerprint of the user, if set */
1549 uf = get_user(&USERENTRY_FPRINT, dcc[idx].user);
1550 if (cf && uf && !strcasecmp(cf, uf)) {
1551 if (!glob_bot(fr))
1552 dprintf(idx, "Used your fingerprint for automatic authentication.\n");
1553 dcc[idx].status |= STAT_FPRINT;
1554 dcc_chat_pass(idx, "+", 1);
1555 /* Required? */
1556 } else if (tls_auth == 2) {
1557 if (glob_bot(fr))
1558 dprintf(idx, "error fingerprint required\n");
1559 else
1560 dprintf(idx, "Certificate authentication required. "
1561 "You need to set your fingerprint.\n");
1562 killsock(dcc[idx].sock);
1563 lostdcc(idx);
1564 }
1565 return 0;
1566 }
1567 return 1;
1568 }
1569 #endif
1570
1571 static void dcc_telnet_pass(int idx, int atr)
1572 {
1573 int ok = 0;
1574 struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
1575
1576 get_user_flagrec(dcc[idx].user, &fr, NULL);
1577 #ifdef TLS
1578 /* Check if fingerprint authentication is allowed or required. */
1579 if (dcc[idx].ssl && tls_auth) {
1580 char *cf, *uf;
1581
1582 /* Get the fingerprint of the current certificate */
1583 cf = ssl_getfp(dcc[idx].sock);
1584 /* Get the fingerprint of the user, if set */
1585 uf = get_user(&USERENTRY_FPRINT, dcc[idx].user);
1586 if (cf && uf && !strcasecmp(cf, uf)) {
1587 char fakepass[2] = "+";
1588
1589 if (!glob_bot(fr))
1590 dprintf(idx, "Used your fingerprint for automatic authentication.\n");
1591 dcc[idx].status |= STAT_FPRINT;
1592 dcc_chat_pass(idx, fakepass, 1);
1593 return;
1594 /* Required? */
1595 } else if (tls_auth == 2) {
1596 if (glob_bot(fr))
1597 dprintf(idx, "error fingerprint required\n");
1598 else
1599 dprintf(idx, "Certificate authentication required. "
1600 "You need to set your fingerprint.\n");
1601 killsock(dcc[idx].sock);
1602 lostdcc(idx);
1603 return;
1604 }
1605 }
1606 #endif
1607 /* No password set? */
1608 if (!glob_bot(fr) && u_pass_match(dcc[idx].user, "-")) {
1609 dprintf(idx, "Can't telnet until you have a password set.\n");
1610 putlog(LOG_MISC, "*", DCC_NOPASS, dcc[idx].nick, dcc[idx].host);
1611 killsock(dcc[idx].sock);
1612 lostdcc(idx);
1613 return;
1614 }
1615 ok = 0;
1616 if (dcc[idx].type == &DCC_DUPWAIT) {
1617 struct chat_info *ci;
1618
1619 ci = dcc[idx].u.dupwait->chat;
1620 nfree(dcc[idx].u.dupwait);
1621 dcc[idx].u.chat = ci;
1622 }
1623 dcc[idx].type = &DCC_CHAT_PASS;
1624 dcc[idx].timeval = now;
1625 if (glob_botmast(fr))
1626 ok = 1;
1627 else if (chan_op(fr)) {
1628 if (!require_p)
1629 ok = 1;
1630 else if (glob_party(fr))
1631 ok = 1;
1632 } else if (glob_party(fr)) {
1633 ok = 1;
1634 dcc[idx].status |= STAT_PARTY;
1635 }
1636 if (glob_bot(fr))
1637 ok = 1;
1638 if (!ok) {
1639 struct chat_info *ci;
1640
1641 ci = dcc[idx].u.chat;
1642 dcc[idx].u.file = get_data_ptr(sizeof(struct file_info));
1643 dcc[idx].u.file->chat = ci;
1644 }
1645
1646 if (glob_bot(fr)) {
1647 #ifdef TLS
1648 /* Ask the peer to switch to ssl communication. We'll continue using plain
1649 * text, until it replies with starttls itself. Bots which don't support ssl
1650 * will simply ignore the request and everything will go on as usual.
1651 */
1652 if (!dcc[idx].ssl) {
1653 dprintf(idx, "starttls\n");
1654 putlog(LOG_BOTS, "*", "Sent STARTTLS to %s...", dcc[idx].nick);
1655 }
1656 #endif
1657 /* Must generate a string consisting of our process ID and the current
1658 * time. The bot will add it's password to the end and use it to generate
1659 * an MD5 checksum (always 128bit). The checksum is sent back and this
1660 * end does the same. The remote bot is only allowed access if the
1661 * checksums match.
1662 *
1663 * Please don't fuck with 'timeval', or the digest we generate later for
1664 * authentication will not be correct - you've been warned ;)
1665 * <Cybah>
1666 */
1667 putlog(LOG_BOTS, "*", "Challenging %s...", dcc[idx].nick);
1668 dprintf(idx, "passreq <%x%x@%s>\n", getpid(), dcc[idx].timeval, botnetnick);
1669 } else {
1670 /* NOTE: The MD5 digest used above to prevent cleartext passwords being
1671 * sent across the net will _only_ work when we have the cleartext
1672 * password. User passwords are encrypted (with blowfish usually)
1673 * so the same thing cant be done. Botnet passwords are always
1674 * stored in cleartext, or at least something that can be reversed.
1675 * <Cybah>
1676 */
1677
1678 /* Turn off remote telnet echo (send IAC WILL ECHO). */
1679 if (dcc[idx].status & STAT_TELNET) {
1680 char buf[1030];
1681 snprintf(buf, sizeof buf, "\n%s%s\r\n", escape_telnet(DCC_ENTERPASS),
1682 TLN_IAC_C TLN_WILL_C TLN_ECHO_C);
1683 tputs(dcc[idx].sock, buf, strlen(buf));
1684 } else
1685 dprintf(idx, "\n%s\n", DCC_ENTERPASS);
1686 }
1687 }
1688
1689 static void eof_dcc_telnet_id(int idx)
1690 {
1691 putlog(LOG_MISC, "*", DCC_LOSTCON, dcc[idx].host, dcc[idx].port);
1692 killsock(dcc[idx].sock);
1693 lostdcc(idx);
1694 }
1695
1696 static void timeout_dcc_telnet_id(int idx)
1697 {
1698 dprintf(idx, "Timeout.\n");
1699 putlog(LOG_MISC, "*", DCC_TTIMEOUT, dcc[idx].host);
1700 killsock(dcc[idx].sock);
1701 lostdcc(idx);
1702 }
1703
1704 static void display_dcc_telnet_id(int idx, char *buf)
1705 {
1706 long tv;
1707
1708 tv = now - dcc[idx].timeval;
1709 sprintf(buf, "t-in waited %lis", tv);
1710 }
1711
1712 struct dcc_table DCC_TELNET_ID = {
1713 "TELNET_ID",
1714 0,
1715 eof_dcc_telnet_id,
1716 dcc_telnet_id,
1717 &password_timeout,
1718 timeout_dcc_telnet_id,
1719 display_dcc_telnet_id,
1720 expmem_dcc_general,
1721 kill_dcc_general,
1722 out_dcc_general
1723 };
1724
1725 static void dcc_telnet_new(int idx, char *buf, int x)
1726 {
1727 int ok = 1;
1728 char work[1024], *p, *q, *r;
1729
1730 buf[HANDLEN] = 0;
1731 if (dcc[idx].status & STAT_TELNET)
1732 strip_telnet(dcc[idx].sock, buf, &x);
1733 dcc[idx].timeval = now;
1734 for (x = 0; x < strlen(buf); x++)
1735 if (buf[x] <= 32)
1736 ok = 0;
1737 if (!ok) {
1738 dprintf(idx, "\nYou can't use weird symbols in your nick.\n");
1739 dprintf(idx, "Try another one please:\n");
1740 } else if (strchr(BADHANDCHARS, buf[0]) != NULL) {
1741 dprintf(idx, "\nYou can't start your nick with the character '%c'\n",
1742 buf[0]);
1743 dprintf(idx, "Try another one please:\n");
1744 } else if (get_user_by_handle(userlist, buf)) {
1745 dprintf(idx, "\nSorry, that nickname is taken already.\n");
1746 dprintf(idx, "Try another one please:\n");
1747 return;
1748 } else if (!egg_strcasecmp(buf, botnetnick))
1749 dprintf(idx, "Sorry, can't use my name for a nick.\n");
1750 else {
1751 strcpy(dcc[idx].nick, buf);
1752 if (make_userfile)
1753 userlist = adduser(userlist,
1754 buf, "-telnet!*@*", "-", sanity_check(default_flags |
1755 USER_PARTY | USER_MASTER | USER_OWNER));
1756 else {
1757 p = strchr(dcc[idx].host, '@');
1758 if (p) {
1759 q = p;
1760 *q = 0;
1761 p++;
1762 r = strchr(p, '.');
1763 if (!r)
1764 simple_sprintf(work, "-telnet!%s@%s", dcc[idx].host, p);
1765 else
1766 simple_sprintf(work, "-telnet!%s@*%s", dcc[idx].host, r);
1767 *q = '@';
1768 } else
1769 simple_sprintf(work, "-telnet!*@*%s", dcc[idx].host);
1770 userlist = adduser(userlist, buf, work, "-",
1771 sanity_check(USER_PARTY | default_flags));
1772 }
1773 reaffirm_owners();
1774 dcc[idx].status = STAT_ECHO | STAT_TELNET;
1775 dcc[idx].type = &DCC_CHAT; /* Just so next line will work */
1776 dcc[idx].user = get_user_by_handle(userlist, buf);
1777 check_dcc_attrs(dcc[idx].user, USER_PARTY | default_flags);
1778 dcc[idx].type = &DCC_TELNET_PW;
1779 if (make_userfile) {
1780 dprintf(idx, "\nYOU ARE THE MASTER/OWNER ON THIS BOT NOW\n");
1781 dprintf(idx, IRC_LIMBO);
1782 putlog(LOG_MISC, "*", DCC_INSTCOMPL, buf);
1783 make_userfile = 0;
1784 write_userfile(-1);
1785 add_note(buf, botnetnick, "Welcome to eggdrop! :)", -1, 0);
1786 }
1787 dprintf(idx, "\nOkay, now choose and enter a password:\n");
1788 dprintf(idx, "(Only the first 15 letters are significant.)\n");
1789 }
1790 }
1791
1792 static void dcc_telnet_pw(int idx, char *buf, int x)
1793 {
1794 char *newpass;
1795 int ok;
1796
1797 if (dcc[idx].status & STAT_TELNET)
1798 strip_telnet(dcc[idx].sock, buf, &x);
1799 buf[16] = 0;
1800 ok = 1;
1801 if (strlen(buf) < 4) {
1802 dprintf(idx, "\nTry to use at least 4 characters in your password.\n");
1803 dprintf(idx, "Choose and enter a password:\n");
1804 return;
1805 }
1806 for (x = 0; x < strlen(buf); x++)
1807 if ((buf[x] <= 32) || (buf[x] == 127))
1808 ok = 0;
1809 if (!ok) {
1810 dprintf(idx, "\nYou can't use weird symbols in your password.\n");
1811 dprintf(idx, "Try another one please:\n");
1812 return;
1813 }
1814 putlog(LOG_MISC, "*", DCC_NEWUSER, dcc[idx].nick, dcc[idx].host,
1815 dcc[idx].port);
1816 if (notify_new[0]) {
1817 char s[121], s1[121], s2[121];
1818
1819 sprintf(s, "Introduced to %s, %s", dcc[idx].nick, dcc[idx].host);
1820 strcpy(s1, notify_new);
1821 splitc(s2, s1, ',');
1822 while (s2[0]) {
1823 rmspace(s2);
1824 add_note(s2, botnetnick, s, -1, 0);
1825 splitc(s2, s1, ',');
1826 }
1827 rmspace(s1);
1828 add_note(s1, botnetnick, s, -1, 0);
1829 }
1830 newpass = newsplit(&buf);
1831 set_user(&USERENTRY_PASS, dcc[idx].user, newpass);
1832 dprintf(idx, "\nRemember that! You'll need it next time you log in.\n");
1833 dprintf(idx, "You now have an account on %s...\n\n\n", botnetnick);
1834 dcc[idx].type = &DCC_CHAT;
1835 dcc[idx].u.chat->channel = -2;
1836 dcc_chatter(idx);
1837 }
1838
1839 static void eof_dcc_telnet_new(int idx)
1840 {
1841 putlog(LOG_MISC, "*", DCC_LOSTNEWUSER, dcc[idx].host, dcc[idx].port);
1842 killsock(dcc[idx].sock);
1843 lostdcc(idx);
1844 }
1845
1846 static void eof_dcc_telnet_pw(int idx)
1847 {
1848 putlog(LOG_MISC, "*", DCC_LOSTNEWUSR2, dcc[idx].nick, dcc[idx].host,
1849 dcc[idx].port);
1850 deluser(dcc[idx].nick);
1851 killsock(dcc[idx].sock);
1852 lostdcc(idx);
1853 }
1854
1855 static void tout_dcc_telnet_new(int idx)
1856 {
1857 dprintf(idx, "Guess you're not there. Bye.\n");
1858 putlog(LOG_MISC, "*", DCC_TIMEOUTUSER, dcc[idx].host, dcc[idx].port);
1859 killsock(dcc[idx].sock);
1860 lostdcc(idx);
1861 }
1862
1863 static void tout_dcc_telnet_pw(int idx)
1864 {
1865 dprintf(idx, "Guess you're not there. Bye.\n");
1866 putlog(LOG_MISC, "*", DCC_TIMEOUTUSR2, dcc[idx].nick,
1867 dcc[idx].host, dcc[idx].port);
1868 killsock(dcc[idx].sock);
1869 lostdcc(idx);
1870 }
1871
1872 static void display_dcc_telnet_new(int idx, char *buf)
1873 {
1874 long tv;
1875
1876 tv = now - dcc[idx].timeval;
1877 sprintf(buf, "new waited %lis", tv);
1878 }
1879
1880 static void display_dcc_telnet_pw(int idx, char *buf)
1881 {
1882 long tv;
1883
1884 tv = now - dcc[idx].timeval;
1885 sprintf(buf, "newp waited %lis", tv);
1886 }
1887
1888 struct dcc_table DCC_TELNET_NEW = {
1889 "TELNET_NEW",
1890 0,
1891 eof_dcc_telnet_new,
1892 dcc_telnet_new,
1893 &password_timeout,
1894 tout_dcc_telnet_new,
1895 display_dcc_telnet_new,
1896 expmem_dcc_general,
1897 kill_dcc_general,
1898 out_dcc_general
1899 };
1900
1901 struct dcc_table DCC_TELNET_PW = {
1902 "TELNET_PW",
1903 0,
1904 eof_dcc_telnet_pw,
1905 dcc_telnet_pw,
1906 &password_timeout,
1907 tout_dcc_telnet_pw,
1908 display_dcc_telnet_pw,
1909 expmem_dcc_general,
1910 kill_dcc_general,
1911 out_dcc_general
1912 };
1913
1914 static int call_tcl_func(char *name, int idx, char *args)
1915 {
1916 char s[11];
1917
1918 sprintf(s, "%d", idx);
1919 Tcl_SetVar(interp, "_n", s, 0);
1920 Tcl_SetVar(interp, "_a", args, 0);
1921 if (Tcl_VarEval(interp, name, " $_n $_a", NULL) == TCL_ERROR) {
1922 putlog(LOG_MISC, "*", DCC_TCLERROR, name, tcl_resultstring());
1923 return -1;
1924 }
1925 return tcl_resultint();
1926 }
1927
1928 static void dcc_script(int idx, char *buf, int len)
1929 {
1930 long oldsock;
1931
1932 if (dcc[idx].status & STAT_TELNET)
1933 strip_telnet(dcc[idx].sock, buf, &len);
1934 if (!len)
1935 return;
1936
1937 dcc[idx].timeval = now;
1938 oldsock = dcc[idx].sock; /* Remember the socket number. */
1939 if (call_tcl_func(dcc[idx].u.script->command, dcc[idx].sock, buf)) {
1940 void *old_other = NULL;
1941
1942 /* Check whether the socket and dcc entry are still valid. They
1943 * might have been killed by `killdcc'. */
1944 if (dcc[idx].sock != oldsock || idx > max_dcc)
1945 return;
1946
1947 old_other = dcc[idx].u.script->u.other;
1948 dcc[idx].type = dcc[idx].u.script->type;
1949 nfree(dcc[idx].u.script);
1950 dcc[idx].u.other = old_other;
1951 if (dcc[idx].type == &DCC_SOCKET) {
1952 /* Kill the whole thing off */
1953 killsock(dcc[idx].sock);
1954 lostdcc(idx);
1955 return;
1956 }
1957 if (dcc[idx].type == &DCC_CHAT) {
1958 if (dcc[idx].u.chat->channel >= 0) {
1959 chanout_but(-1, dcc[idx].u.chat->channel, DCC_JOIN, dcc[idx].nick);
1960 if (dcc[idx].u.chat->channel < 10000)
1961 botnet_send_join_idx(idx, -1);
1962 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
1963 geticon(idx), dcc[idx].sock, dcc[idx].host);
1964 }
1965 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
1966 }
1967 }
1968 }
1969
1970 static void eof_dcc_script(int idx)
1971 {
1972 void *old;
1973 int oldflags;
1974
1975 /* This will stop a killdcc from working, incase the script tries
1976 * to kill it's controlling socket while handling an EOF <cybah>
1977 */
1978 oldflags = dcc[idx].type->flags;
1979 dcc[idx].type->flags &= ~(DCT_VALIDIDX);
1980 /* Tell the script they're gone: */
1981 call_tcl_func(dcc[idx].u.script->command, dcc[idx].sock, "");
1982 /* Restore the flags */
1983 dcc[idx].type->flags = oldflags;
1984 old = dcc[idx].u.script->u.other;
1985 dcc[idx].type = dcc[idx].u.script->type;
1986 nfree(dcc[idx].u.script);
1987 dcc[idx].u.other = old;
1988 /* Then let it fall thru to the real one */
1989 if (dcc[idx].type && dcc[idx].type->eof)
1990 dcc[idx].type->eof(idx);
1991 else {
1992 putlog(LOG_MISC, "*", DCC_DEADSOCKET, dcc[idx].sock, dcc[idx].type->name);
1993 killsock(dcc[idx].sock);
1994 lostdcc(idx);
1995 }
1996 }
1997
1998 static void display_dcc_script(int idx, char *buf)
1999 {
2000 sprintf(buf, "scri %s", dcc[idx].u.script->command);
2001 }
2002
2003 static int expmem_dcc_script(void *x)
2004 {
2005 register struct script_info *p = (struct script_info *) x;
2006 int tot = sizeof(struct script_info);
2007
2008 if (p->type && p->u.other)
2009 tot += p->type->expmem(p->u.other);
2010 return tot;
2011 }
2012
2013 static void kill_dcc_script(int idx, void *x)
2014 {
2015 register struct script_info *p = (struct script_info *) x;
2016
2017 if (p->type && p->u.other)
2018 p->type->kill(idx, p->u.other);
2019 nfree(p);
2020 }
2021
2022 static void out_dcc_script(int idx, char *buf, void *x)
2023 {
2024 register struct script_info *p = (struct script_info *) x;
2025
2026 if (p && p->type && p->u.other)
2027 p->type->output(idx, buf, p->u.other);
2028 else
2029 tputs(dcc[idx].sock, buf, strlen(buf));
2030 }
2031
2032 struct dcc_table DCC_SCRIPT = {
2033 "SCRIPT",
2034 DCT_VALIDIDX,
2035 eof_dcc_script,
2036 dcc_script,
2037 NULL,
2038 NULL,
2039 display_dcc_script,
2040 expmem_dcc_script,
2041 kill_dcc_script,
2042 out_dcc_script
2043 };
2044
2045 static void dcc_socket(int idx, char *buf, int len)
2046 {
2047 }
2048
2049 static void eof_dcc_socket(int idx)
2050 {
2051 killsock(dcc[idx].sock);
2052 lostdcc(idx);
2053 }
2054
2055 static void display_dcc_socket(int idx, char *buf)
2056 {
2057 strcpy(buf, "sock (stranded)");
2058 }
2059
2060 struct dcc_table DCC_SOCKET = {
2061 "SOCKET",
2062 DCT_VALIDIDX,
2063 eof_dcc_socket,
2064 dcc_socket,
2065 NULL,
2066 NULL,
2067 display_dcc_socket,
2068 NULL,
2069 NULL,
2070 NULL
2071 };
2072
2073 static void display_dcc_lost(int idx, char *buf)
2074 {
2075 strcpy(buf, "lost");
2076 }
2077
2078 struct dcc_table DCC_LOST = {
2079 "LOST",
2080 0,
2081 NULL,
2082 dcc_socket,
2083 NULL,
2084 NULL,
2085 display_dcc_lost,
2086 NULL,
2087 NULL,
2088 NULL
2089 };
2090
2091 void dcc_identwait(int idx, char *buf, int len)
2092 {
2093 /* Ignore anything now */
2094 }
2095
2096 void eof_dcc_identwait(int idx)
2097 {
2098 int i;
2099
2100 putlog(LOG_MISC, "*", DCC_LOSTCONN, dcc[idx].host, dcc[idx].port);
2101 for (i = 0; i < dcc_total; i++)
2102 if ((dcc[i].type == &DCC_IDENT) &&
2103 (dcc[i].u.ident_sock == dcc[idx].sock)) {
2104 killsock(dcc[i].sock); /* Cleanup ident socket */
2105 dcc[i].u.other = 0;
2106 lostdcc(i);
2107 break;
2108 }
2109 killsock(dcc[idx].sock); /* Cleanup waiting socket */
2110 dcc[idx].u.other = 0;
2111 lostdcc(idx);
2112 }
2113
2114 static void display_dcc_identwait(int idx, char *buf)
2115 {
2116 long tv;
2117
2118 tv = now - dcc[idx].timeval;
2119 sprintf(buf, "idtw waited %lis", tv);
2120 }
2121
2122 struct dcc_table DCC_IDENTWAIT = {
2123 "IDENTWAIT",
2124 0,
2125 eof_dcc_identwait,
2126 dcc_identwait,
2127 NULL,
2128 NULL,
2129 display_dcc_identwait,
2130 NULL,
2131 NULL,
2132 NULL
2133 };
2134
2135 void dcc_ident(int idx, char *buf, int len)
2136 {
2137 char response[512], uid[512], buf1[UHOSTLEN];
2138 int i;
2139
2140 *response = *uid = '\0';
2141 sscanf(buf, "%*[^:]:%[^:]:%*[^:]:%[^\n]\n", response, uid);
2142 rmspace(response);
2143 if (response[0] != 'U') {
2144 dcc[idx].timeval = now;
2145 return;
2146 }
2147 rmspace(uid);
2148 uid[20] = 0; /* 20 character ident max */
2149 for (i = 0; i < dcc_total; i++)
2150 if ((dcc[i].type == &DCC_IDENTWAIT) &&
2151 (dcc[i].sock == dcc[idx].u.ident_sock)) {
2152 simple_sprintf(buf1, "%s@%s", uid, dcc[idx].host);
2153 dcc_telnet_got_ident(i, buf1);
2154 }
2155 dcc[idx].u.other = 0;
2156 killsock(dcc[idx].sock);
2157 lostdcc(idx);
2158 }
2159
2160 void eof_dcc_ident(int idx)
2161 {
2162 char buf[UHOSTLEN];
2163 int i;
2164
2165 for (i = 0; i < dcc_total; i++)
2166 if ((dcc[i].type == &DCC_IDENTWAIT) &&
2167 (dcc[i].sock == dcc[idx].u.ident_sock)) {
2168 putlog(LOG_MISC, "*", DCC_EOFIDENT);
2169 simple_sprintf(buf, "telnet@%s", dcc[idx].host);
2170 dcc_telnet_got_ident(i, buf);
2171 }
2172 killsock(dcc[idx].sock);
2173 dcc[idx].u.other = 0;
2174 lostdcc(idx);
2175 }
2176
2177 static void display_dcc_ident(int idx, char *buf)
2178 {
2179 sprintf(buf, "idnt (sock %d)", dcc[idx].u.ident_sock);
2180 }
2181
2182 struct dcc_table DCC_IDENT = {
2183 "IDENT",
2184 0,
2185 eof_dcc_ident,
2186 dcc_ident,
2187 &identtimeout,
2188 eof_dcc_ident,
2189 display_dcc_ident,
2190 NULL,
2191 NULL,
2192 NULL
2193 };
2194
2195 static void dcc_telnet_got_ident(int i, char *host)
2196 {
2197 int idx;
2198 char x[1024];
2199
2200 for (idx = 0; idx < dcc_total; idx++)
2201 if ((dcc[idx].type == &DCC_TELNET) &&
2202 (dcc[idx].sock == dcc[i].u.ident_sock))
2203 break;
2204 dcc[i].u.other = 0;
2205 if (dcc_total == idx) {
2206 putlog(LOG_MISC, "*", DCC_LOSTIDENT);
2207 killsock(dcc[i].sock);
2208 lostdcc(i);
2209 return;
2210 }
2211 strncpyz(dcc[i].host, host, UHOSTLEN);
2212 egg_snprintf(x, sizeof x, "-telnet!%s", dcc[i].host);
2213 if (protect_telnet && !make_userfile) {
2214 struct userrec *u;
2215 int ok = 1;
2216
2217 u = get_user_by_host(x);
2218 /* Not a user or +p & require p OR +o */
2219 if (!u)
2220 ok = 0;
2221 else if (require_p && !(u->flags & USER_PARTY))
2222 ok = 0;
2223 else if (!require_p && !(u->flags & USER_OP))
2224 ok = 0;
2225 if (!ok && u && (u->flags & USER_BOT))
2226 ok = 1;
2227 if (!ok && (dcc[idx].status & LSTN_PUBLIC))
2228 ok = 1;
2229 if (!ok) {
2230 putlog(LOG_MISC, "*", DCC_NOACCESS, dcc[i].host);
2231 killsock(dcc[i].sock);
2232 lostdcc(i);
2233 return;
2234 }
2235 }
2236 if (match_ignore(x)) {
2237 killsock(dcc[i].sock);
2238 lostdcc(i);
2239 return;
2240 }
2241
2242 /* Script? */
2243 if (!strcmp(dcc[idx].nick, "(script)")) {
2244 dcc[i].type = &DCC_SOCKET;
2245 dcc[i].u.other = NULL;
2246 strcpy(dcc[i].nick, "*");
2247 check_tcl_listen(dcc[idx].host, dcc[i].sock);
2248 return;
2249 }
2250 /* Do not buffer data anymore. All received and stored data is passed
2251 * over to the dcc functions from now on. */
2252 sockoptions(dcc[i].sock, EGG_OPTION_UNSET, SOCK_BUFFER);
2253
2254 dcc[i].type = &DCC_TELNET_ID;
2255 dcc[i].u.chat = get_data_ptr(sizeof(struct chat_info));
2256 egg_bzero(dcc[i].u.chat, sizeof(struct chat_info));
2257
2258 /* Note: we don't really care about telnet status here. We use the
2259 * STATUS option as a hopefully harmless way to detect if the other
2260 * side is a telnet client or not. */
2261 dprintf(i, TLN_IAC_C TLN_WILL_C TLN_STATUS_C "\n");
2262
2263 /* Copy acceptable-nick/host mask */
2264 dcc[i].status = STAT_TELNET | STAT_ECHO;
2265 if (!strcmp(dcc[idx].nick, "(bots)"))
2266 dcc[i].status |= STAT_BOTONLY;
2267 if (!strcmp(dcc[idx].nick, "(users)"))
2268 dcc[i].status |= STAT_USRONLY;
2269 /* Copy acceptable-nick/host mask */
2270 strncpyz(dcc[i].nick, dcc[idx].host, HANDLEN);
2271 dcc[i].timeval = now;
2272 strcpy(dcc[i].u.chat->con_chan, chanset ? chanset->dname : "*");
2273 /* Displays a customizable banner. */
2274 if (use_telnet_banner)
2275 show_banner(i);
2276 /* This is so we dont tell someone doing a portscan anything
2277 * about ourselves. <cybah>
2278 */
2279 if (stealth_telnets)
2280 sub_lang(i, MISC_BANNER_STEALTH);
2281 else {
2282 dprintf(i, "\n\n");
2283 sub_lang(i, MISC_BANNER);
2284 }
2285