/[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.12 - (show annotations) (download) (as text)
Fri Jun 22 21:38:55 2012 UTC (8 years ago) by thommey
Branch: MAIN
Changes since 1.11: +7 -1 lines
File MIME type: text/x-chdr
new stripcode flag "o" for mirc ctrl+o and * for everything

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.11 2012/06/16 16:04:02 thommey 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 017: /* Reset all control chars (mIRC ctrl+o) */
828 if (flags & STRIP_RESET) {
829 text++;
830 continue;
831 }
832 break;
833 case 0x16: /* Reverse video */
834 if (flags & STRIP_REV) {
835 text++;
836 continue;
837 }
838 break;
839 case 0x1f: /* Underlined text */
840 if (flags & STRIP_UNDER) {
841 text++;
842 continue;
843 }
844 break;
845 case 033:
846 if (flags & STRIP_ANSI) {
847 text++;
848 if (*text == '[') {
849 text++;
850 while ((*text == ';') || egg_isdigit(*text))
851 text++;
852 if (*text)
853 text++; /* also kill the following char */
854 }
855 continue;
856 }
857 break;
858 }
859 *dd++ = *text++; /* Move on to the next char */
860 }
861 *dd = 0;
862 }
863
864 static void append_line(int idx, char *line)
865 {
866 int l = strlen(line);
867 struct msgq *p, *q;
868 struct chat_info *c = (dcc[idx].type == &DCC_CHAT) ? dcc[idx].u.chat :
869 dcc[idx].u.file->chat;
870
871 if (c->current_lines > 1000) {
872 /* They're probably trying to fill up the bot nuke the sods :) */
873 for (p = c->buffer; p; p = q) {
874 q = p->next;
875 nfree(p->msg);
876 nfree(p);
877 }
878 c->buffer = 0;
879 dcc[idx].status &= ~STAT_PAGE;
880 do_boot(idx, botnetnick, "too many pages - sendq full");
881 return;
882 }
883 if ((c->line_count < c->max_line) && (c->buffer == NULL)) {
884 c->line_count++;
885 tputs(dcc[idx].sock, line, l);
886 } else {
887 c->current_lines++;
888 if (c->buffer == NULL)
889 q = NULL;
890 else
891 for (q = c->buffer; q->next; q = q->next);
892
893 p = get_data_ptr(sizeof(struct msgq));
894
895 p->len = l;
896 p->msg = get_data_ptr(l + 1);
897 p->next = NULL;
898 strcpy(p->msg, line);
899 if (q == NULL)
900 c->buffer = p;
901 else
902 q->next = p;
903 }
904 }
905
906
907 static void out_dcc_general(int idx, char *buf, void *x)
908 {
909 register struct chat_info *p = (struct chat_info *) x;
910 char *y = buf;
911
912 strip_mirc_codes(p->strip_flags, buf);
913 if (dcc[idx].status & STAT_TELNET)
914 y = escape_telnet(buf);
915 if (dcc[idx].status & STAT_PAGE)
916 append_line(idx, y);
917 else
918 tputs(dcc[idx].sock, y, strlen(y));
919 }
920
921 struct dcc_table DCC_CHAT_PASS = {
922 "CHAT_PASS",
923 0,
924 eof_dcc_general,
925 dcc_chat_pass,
926 &password_timeout,
927 tout_dcc_chat_pass,
928 display_dcc_chat_pass,
929 expmem_dcc_general,
930 kill_dcc_general,
931 out_dcc_general
932 };
933
934 /* Make sure ANSI code is just for color-changing */
935 int check_ansi(char *v)
936 {
937 int count = 2;
938
939 if (*v++ != '\033')
940 return 1;
941 if (*v++ != '[')
942 return 1;
943 while (*v) {
944 if (*v == 'm')
945 return 0;
946 if ((*v != ';') && ((*v < '0') || (*v > '9')))
947 return count;
948 v++;
949 count++;
950 }
951 return count;
952 }
953
954 static void eof_dcc_chat(int idx)
955 {
956 putlog(LOG_MISC, "*", DCC_LOSTDCC, dcc[idx].nick,
957 dcc[idx].host, dcc[idx].port);
958 if (dcc[idx].u.chat->channel >= 0) {
959 chanout_but(idx, dcc[idx].u.chat->channel, "*** %s lost dcc link.\n",
960 dcc[idx].nick);
961 if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
962 botnet_send_part_idx(idx, "lost dcc link");
963 check_tcl_chpt(botnetnick, dcc[idx].nick, dcc[idx].sock,
964 dcc[idx].u.chat->channel);
965 }
966 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
967 killsock(dcc[idx].sock);
968 lostdcc(idx);
969 }
970
971 static void dcc_chat(int idx, char *buf, int i)
972 {
973 int nathan = 0, doron = 0, fixed = 0;
974 char *v, *d, filtbuf[2048];
975
976 if (dcc[idx].status & STAT_TELNET)
977 strip_telnet(dcc[idx].sock, buf, &i);
978 if (buf[0] && (buf[0] != '.') &&
979 detect_dcc_flood(&dcc[idx].timeval, dcc[idx].u.chat, idx))
980 return;
981 dcc[idx].timeval = now;
982 if (buf[0]) {
983 const char *filt = check_tcl_filt(idx, buf);
984 if (filt != buf) {
985 strncpyz(filtbuf, filt, sizeof(filtbuf));
986 buf = filtbuf;
987 }
988 }
989 if (buf[0]) {
990 /* Check for beeps and cancel annoying ones */
991 v = buf;
992 d = buf;
993 while (*v)
994 switch (*v) {
995 case 7: /* Beep - no more than 3 */
996 nathan++;
997 if (nathan > 3)
998 v++;
999 else
1000 *d++ = *v++;
1001 break;
1002 case 8: /* Backspace - for lame telnet's :) */
1003 if (d > buf) {
1004 d--;
1005 }
1006 v++;
1007 break;
1008 case 27: /* ESC - ansi code? */
1009 doron = check_ansi(v);
1010 /* If it's valid, append a return-to-normal code at the end */
1011 if (!doron) {
1012 *d++ = *v++;
1013 fixed = 1;
1014 } else
1015 v += doron;
1016 break;
1017 case '\r': /* Weird pseudo-linefeed */
1018 v++;
1019 break;
1020 default:
1021 *d++ = *v++;
1022 }
1023 if (fixed)
1024 strcpy(d, "\033[0m");
1025 else
1026 *d = 0;
1027 if (buf[0]) { /* Nothing to say - maybe paging... */
1028 if ((buf[0] == '.') || (dcc[idx].u.chat->channel < 0)) {
1029 if (buf[0] == '.')
1030 buf++;
1031 v = newsplit(&buf);
1032 rmspace(buf);
1033 if (check_tcl_dcc(v, idx, buf)) {
1034 if (dcc[idx].u.chat->channel >= 0)
1035 check_tcl_chpt(botnetnick, dcc[idx].nick, dcc[idx].sock,
1036 dcc[idx].u.chat->channel);
1037 check_tcl_chof(dcc[idx].nick, dcc[idx].sock);
1038 dprintf(idx, "*** Ja mata!\n");
1039 flush_lines(idx, dcc[idx].u.chat);
1040 putlog(LOG_MISC, "*", DCC_CLOSED, dcc[idx].nick, dcc[idx].host);
1041 if (dcc[idx].u.chat->channel >= 0) {
1042 chanout_but(-1, dcc[idx].u.chat->channel,
1043 "*** %s left the party line%s%s\n",
1044 dcc[idx].nick, buf[0] ? ": " : ".", buf);
1045 if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
1046 botnet_send_part_idx(idx, buf);
1047 }
1048 if (dcc[idx].u.chat->su_nick) {
1049 dcc[idx].user = get_user_by_handle(userlist,
1050 dcc[idx].u.chat->su_nick);
1051 strcpy(dcc[idx].nick, dcc[idx].u.chat->su_nick);
1052 dcc[idx].type = &DCC_CHAT;
1053 dprintf(idx, "Returning to real nick %s!\n",
1054 dcc[idx].u.chat->su_nick);
1055 nfree(dcc[idx].u.chat->su_nick);
1056 dcc[idx].u.chat->su_nick = NULL;
1057 dcc_chatter(idx);
1058 if (dcc[idx].u.chat->channel < GLOBAL_CHANS &&
1059 dcc[idx].u.chat->channel >= 0)
1060 botnet_send_join_idx(idx, -1);
1061 return;
1062 } else if ((dcc[idx].sock != STDOUT) || backgrd) {
1063 killsock(dcc[idx].sock);
1064 lostdcc(idx);
1065 return;
1066 } else {
1067 dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
1068 dcc_chatter(idx);
1069 return;
1070 }
1071 }
1072 } else if (buf[0] == ',') {
1073 int me = 0;
1074
1075 if ((buf[1] == 'm') && (buf[2] == 'e') && buf[3] == ' ')
1076 me = 1;
1077 for (i = 0; i < dcc_total; i++) {
1078 int ok = 0;
1079
1080 if ((dcc[i].type->flags & DCT_MASTER) &&
1081 ((dcc[i].type != &DCC_CHAT) || (dcc[i].u.chat->channel >= 0)) &&
1082 ((i != idx) || (dcc[idx].status & STAT_ECHO)))
1083 ok = 1;
1084 if (ok) {
1085 struct userrec *u = get_user_by_handle(userlist, dcc[i].nick);
1086
1087 if (u && (u->flags & USER_MASTER)) {
1088 if (me)
1089 dprintf(i, "-> %s%s\n", dcc[idx].nick, buf + 3);
1090 else
1091 dprintf(i, "-%s-> %s\n", dcc[idx].nick, buf + 1);
1092 }
1093 }
1094 }
1095 } else if (buf[0] == '\'') {
1096 int me = 0;
1097
1098 if ((buf[1] == 'm') && (buf[2] == 'e') &&
1099 ((buf[3] == ' ') || (buf[3] == '\'') || (buf[3] == ',')))
1100 me = 1;
1101 for (i = 0; i < dcc_total; i++) {
1102 if (dcc[i].type->flags & DCT_CHAT) {
1103 if (me)
1104 dprintf(i, "=> %s%s\n", dcc[idx].nick, buf + 3);
1105 else
1106 dprintf(i, "=%s=> %s\n", dcc[idx].nick, buf + 1);
1107 }
1108 }
1109 } else {
1110 if (dcc[idx].u.chat->away != NULL)
1111 not_away(idx);
1112 if (dcc[idx].status & STAT_ECHO)
1113 chanout_but(-1, dcc[idx].u.chat->channel,
1114 "<%s> %s\n", dcc[idx].nick, buf);
1115 else
1116 chanout_but(idx, dcc[idx].u.chat->channel, "<%s> %s\n",
1117 dcc[idx].nick, buf);
1118 botnet_send_chan(-1, botnetnick, dcc[idx].nick,
1119 dcc[idx].u.chat->channel, buf);
1120 check_tcl_chat(dcc[idx].nick, dcc[idx].u.chat->channel, buf);
1121 }
1122 }
1123 }
1124 if (dcc[idx].type == &DCC_CHAT) /* Could have change to files */
1125 if (dcc[idx].status & STAT_PAGE)
1126 flush_lines(idx, dcc[idx].u.chat);
1127 }
1128
1129 static void display_dcc_chat(int idx, char *buf)
1130 {
1131 int i = simple_sprintf(buf, "chat flags: ");
1132
1133 buf[i++] = dcc[idx].status & STAT_CHAT ? 'C' : 'c';
1134 buf[i++] = dcc[idx].status & STAT_PARTY ? 'P' : 'p';
1135 buf[i++] = dcc[idx].status & STAT_TELNET ? 'T' : 't';
1136 buf[i++] = dcc[idx].status & STAT_ECHO ? 'E' : 'e';
1137 buf[i++] = dcc[idx].status & STAT_PAGE ? 'P' : 'p';
1138 simple_sprintf(buf + i, "/%d", dcc[idx].u.chat->channel);
1139 }
1140
1141 struct dcc_table DCC_CHAT = {
1142 "CHAT",
1143 DCT_CHAT | DCT_MASTER | DCT_SHOWWHO | DCT_VALIDIDX | DCT_SIMUL |
1144 DCT_CANBOOT | DCT_REMOTEWHO,
1145 eof_dcc_chat,
1146 dcc_chat,
1147 NULL,
1148 NULL,
1149 display_dcc_chat,
1150 expmem_dcc_general,
1151 kill_dcc_general,
1152 out_dcc_general
1153 };
1154
1155 static int lasttelnets;
1156 static char lasttelnethost[81];
1157 static time_t lasttelnettime;
1158
1159 /* A modified detect_flood for incoming telnet flood protection.
1160 */
1161 static int detect_telnet_flood(char *floodhost)
1162 {
1163 struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
1164
1165 get_user_flagrec(get_user_by_host(floodhost), &fr, NULL);
1166 if (!flood_telnet_thr || (glob_friend(fr) && !par_telnet_flood))
1167 return 0; /* No flood protection */
1168 if (egg_strcasecmp(lasttelnethost, floodhost)) { /* New... */
1169 strcpy(lasttelnethost, floodhost);
1170 lasttelnettime = now;
1171 lasttelnets = 0;
1172 return 0;
1173 }
1174 if (lasttelnettime < now - flood_telnet_time) {
1175 /* Flood timer expired, reset it */
1176 lasttelnettime = now;
1177 lasttelnets = 0;
1178 return 0;
1179 }
1180 lasttelnets++;
1181 if (lasttelnets >= flood_telnet_thr) { /* FLOOD! */
1182 /* Reset counters */
1183 lasttelnets = 0;
1184 lasttelnettime = 0;
1185 lasttelnethost[0] = 0;
1186 putlog(LOG_MISC, "*", IRC_TELNETFLOOD, floodhost);
1187 addignore(floodhost, origbotname, "Telnet connection flood",
1188 now + (60 * ignore_time));
1189 return 1;
1190 }
1191 return 0;
1192 }
1193
1194 static void dcc_telnet(int idx, char *buf, int i)
1195 {
1196 unsigned short port;
1197 int j = 0, sock;
1198
1199 if (dcc_total + 1 > max_dcc && increase_socks_max()) {
1200 sockname_t name;
1201 j = answer(dcc[idx].sock, &name, &port, 0);
1202 if (j != -1) {
1203 dprintf(-j, "Sorry, too many connections already.\r\n");
1204 killsock(j);
1205 }
1206 return;
1207 }
1208 i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
1209 sock = answer(dcc[idx].sock, &dcc[i].sockname, &port, 0);
1210 while ((sock == -1) && (errno == EAGAIN))
1211 sock = answer(dcc[idx].sock, &dcc[i].sockname, &port, 0);
1212 if (sock < 0) {
1213 putlog(LOG_MISC, "*", DCC_FAILED, strerror(errno));
1214 return;
1215 }
1216 /* Buffer data received on this socket. */
1217 sockoptions(sock, EGG_OPTION_SET, SOCK_BUFFER);
1218
1219 #if (SIZEOF_SHORT == 2)
1220 if (port < 1024) {
1221 #else
1222 if (port < 1024 || port > 65535) {
1223 #endif
1224 putlog(LOG_BOTS, "*", DCC_BADSRC, iptostr(&dcc[i].sockname.addr.sa), port);
1225 killsock(sock);
1226 lostdcc(i);
1227 return;
1228 }
1229
1230 dcc[i].u.dns->ip = &dcc[i].sockname;
1231 dcc[i].sock = sock;
1232 dcc[i].port = port;
1233 #ifdef TLS
1234 if (dcc[idx].ssl && ssl_handshake(sock, TLS_LISTEN, tls_vfyclients,
1235 LOG_MISC, NULL, NULL)) {
1236 killsock(sock);
1237 lostdcc(i);
1238 return;
1239 }
1240 dcc[i].ssl = dcc[idx].ssl;
1241 #endif
1242 dcc[i].timeval = now;
1243 strcpy(dcc[i].nick, "*");
1244 dcc[i].u.dns->dns_success = dcc_telnet_hostresolved;
1245 dcc[i].u.dns->dns_failure = dcc_telnet_hostresolved;
1246 dcc[i].u.dns->dns_type = RES_HOSTBYIP;
1247 dcc[i].u.dns->ibuf = dcc[idx].sock;
1248 dcc[i].u.dns->type = &DCC_IDENTWAIT;
1249 dcc_dnshostbyip(&dcc[i].sockname);
1250 }
1251
1252 static void dcc_telnet_hostresolved(int i)
1253 {
1254 int idx;
1255 int j = 0, sock;
1256 char s[UHOSTLEN + 20], *userhost;
1257
1258 strncpyz(dcc[i].host, dcc[i].u.dns->host, UHOSTLEN);
1259
1260 for (idx = 0; idx < dcc_total; idx++)
1261 if ((dcc[idx].type == &DCC_TELNET) &&
1262 (dcc[idx].sock == dcc[i].u.dns->ibuf)) {
1263 break;
1264 }
1265 if (dcc_total == idx) {
1266 putlog(LOG_BOTS, "*", "Lost listening socket while resolving %s",
1267 dcc[i].host);
1268 killsock(dcc[i].sock);
1269 lostdcc(i);
1270 return;
1271 }
1272 if (dcc[idx].host[0] == '@') {
1273 /* Restrict by hostname */
1274 if (!wild_match(dcc[idx].host + 1, dcc[i].host)) {
1275 putlog(LOG_BOTS, "*", DCC_BADHOST, dcc[i].host);
1276 killsock(dcc[i].sock);
1277 lostdcc(i);
1278 return;
1279 }
1280 }
1281 sprintf(s, "-telnet!telnet@%s", dcc[i].host);
1282 userhost = s + strlen("-telnet!");
1283 if (match_ignore(s) || detect_telnet_flood(s)) {
1284 killsock(dcc[i].sock);
1285 lostdcc(i);
1286 return;
1287 }
1288
1289 putlog(LOG_MISC, "*", DCC_TELCONN, dcc[i].host, dcc[i].port);
1290
1291 /* Skip ident lookup for public script listeners */
1292 if ((dcc[idx].status & LSTN_PUBLIC) && !strcmp(dcc[idx].nick, "(script)")) {
1293 changeover_dcc(i, &DCC_SOCKET, 0);
1294 dcc[i].u.other = NULL;
1295 strcpy(dcc[i].nick, "*");
1296 strncpyz(dcc[i].host, userhost, UHOSTLEN);
1297 check_tcl_listen(dcc[idx].host, dcc[i].sock);
1298 return;
1299 }
1300
1301 changeover_dcc(i, &DCC_IDENTWAIT, 0);
1302 dcc[i].timeval = now;
1303 dcc[i].u.ident_sock = dcc[idx].sock;
1304 sock = -1;
1305 j = new_dcc(&DCC_IDENT, 0);
1306 if (j < 0)
1307 putlog(LOG_MISC, "*", DCC_IDENTFAIL, dcc[i].host, strerror(errno));
1308 else {
1309 egg_memcpy(&dcc[j].sockname, &dcc[i].sockname, sizeof(sockname_t));
1310 dcc[j].sock = getsock(dcc[j].sockname.family, 0);
1311 if (dcc[j].sock >= 0) {
1312 sockname_t name;
1313 name.addrlen = sizeof(name.addr);
1314 getsockname(dcc[i].sock, &name.addr.sa, &name.addrlen);
1315 bind(dcc[j].sock, &name.addr.sa, name.addrlen);
1316 setsnport(dcc[j].sockname, 113);
1317 if (connect(dcc[j].sock, &dcc[j].sockname.addr.sa,
1318 dcc[j].sockname.addrlen) < 0 && (errno != EINPROGRESS)) {
1319 killsock(dcc[j].sock);
1320 lostdcc(j);
1321 putlog(LOG_MISC, "*", DCC_IDENTFAIL, dcc[i].host, strerror(errno));
1322 j = 0;
1323 }
1324 sock = dcc[j].sock;
1325 }
1326 }
1327 if (j < 0) {
1328 dcc_telnet_got_ident(i, userhost);
1329 return;
1330 }
1331 dcc[j].sock = sock;
1332 dcc[j].port = 113;
1333 dcc[j].addr = dcc[i].addr;
1334 strcpy(dcc[j].host, dcc[i].host);
1335 strcpy(dcc[j].nick, "*");
1336 dcc[j].u.ident_sock = dcc[i].sock;
1337 dcc[j].timeval = now;
1338 dprintf(j, "%d, %d\n", dcc[i].port, dcc[idx].port);
1339 }
1340
1341 static void eof_dcc_telnet(int idx)
1342 {
1343 putlog(LOG_MISC, "*", DCC_PORTDIE, dcc[idx].port);
1344 killsock(dcc[idx].sock);
1345 lostdcc(idx);
1346 }
1347
1348 static void display_telnet(int idx, char *buf)
1349 {
1350 sprintf(buf, "lstn %d%s", dcc[idx].port,
1351 (dcc[idx].status & LSTN_PUBLIC) ? " pub" : "");
1352 }
1353
1354 struct dcc_table DCC_TELNET = {
1355 "TELNET",
1356 DCT_LISTEN,
1357 eof_dcc_telnet,
1358 dcc_telnet,
1359 NULL,
1360 NULL,
1361 display_telnet,
1362 NULL,
1363 NULL,
1364 NULL
1365 };
1366
1367 static void eof_dcc_dupwait(int idx)
1368 {
1369 putlog(LOG_BOTS, "*", DCC_LOSTDUP, dcc[idx].host);
1370 killsock(dcc[idx].sock);
1371 lostdcc(idx);
1372 }
1373
1374 static void dcc_dupwait(int idx, char *buf, int i)
1375 {
1376 /* We just ignore any data at this point. */
1377 return;
1378 }
1379
1380 /* We now check again. If the bot is still marked as duplicate, there is no
1381 * botnet lag we could push it on, so we just drop the connection.
1382 */
1383 static void timeout_dupwait(int idx)
1384 {
1385 char x[100];
1386
1387 /* Still duplicate? */
1388 if (in_chain(dcc[idx].nick)) {
1389 egg_snprintf(x, sizeof x, "%s!%s", dcc[idx].nick, dcc[idx].host);
1390 dprintf(idx, "error Already connected.\n");
1391 putlog(LOG_BOTS, "*", DCC_DUPLICATE, x);
1392 killsock(dcc[idx].sock);
1393 lostdcc(idx);
1394 } else {
1395 /* Ha! Now it's gone and we can grant this bot access. */
1396 dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
1397 }
1398 }
1399
1400 static void display_dupwait(int idx, char *buf)
1401 {
1402 sprintf(buf, "wait duplicate?");
1403 }
1404
1405 static int expmem_dupwait(void *x)
1406 {
1407 register struct dupwait_info *p = (struct dupwait_info *) x;
1408 int tot = sizeof(struct dupwait_info);
1409
1410 if (p && p->chat && DCC_CHAT.expmem)
1411 tot += DCC_CHAT.expmem(p->chat);
1412 return tot;
1413 }
1414
1415 static void kill_dupwait(int idx, void *x)
1416 {
1417 register struct dupwait_info *p = (struct dupwait_info *) x;
1418
1419 if (p) {
1420 if (p->chat && DCC_CHAT.kill)
1421 DCC_CHAT.kill(idx, p->chat);
1422 nfree(p);
1423 }
1424 }
1425
1426 struct dcc_table DCC_DUPWAIT = {
1427 "DUPWAIT",
1428 DCT_VALIDIDX,
1429 eof_dcc_dupwait,
1430 dcc_dupwait,
1431 &dupwait_timeout,
1432 timeout_dupwait,
1433 display_dupwait,
1434 expmem_dupwait,
1435 kill_dupwait,
1436 NULL
1437 };
1438
1439 /* This function is called if a bot gets removed from the list. It checks
1440 * wether we have a pending duplicate connection for that bot and continues
1441 * with the login in that case.
1442 */
1443 void dupwait_notify(char *who)
1444 {
1445 register int idx;
1446
1447 Assert(who);
1448 for (idx = 0; idx < dcc_total; idx++)
1449 if ((dcc[idx].type == &DCC_DUPWAIT) &&
1450 !egg_strcasecmp(dcc[idx].nick, who)) {
1451 dcc_telnet_pass(idx, dcc[idx].u.dupwait->atr);
1452 break;
1453 }
1454 }
1455
1456 static void dcc_telnet_id(int idx, char *buf, int atr)
1457 {
1458 int ok = 0;
1459 struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
1460
1461 if (detect_telnet((unsigned char *) buf)) {
1462 dcc[idx].status |= STAT_TELNET;
1463 strip_telnet(dcc[idx].sock, buf, &atr);
1464 } else
1465 dcc[idx].status &= ~STAT_TELNET;
1466 buf[HANDLEN] = 0;
1467 /* Toss out bad nicknames */
1468 if (dcc[idx].nick[0] != '@' && !wild_match(dcc[idx].nick, buf)) {
1469 dprintf(idx, "Sorry, that nickname format is invalid.\n");
1470 putlog(LOG_BOTS, "*", DCC_BADNICK, dcc[idx].host);
1471 killsock(dcc[idx].sock);
1472 lostdcc(idx);
1473 return;
1474 }
1475 dcc[idx].user = get_user_by_handle(userlist, buf);
1476 get_user_flagrec(dcc[idx].user, &fr, NULL);
1477 #ifdef TLS
1478 if (dcc[idx].ssl && (tls_auth == 2)) {
1479 char *uid = ssl_getuid(dcc[idx].sock);
1480
1481 if (!uid || strcasecmp(uid, buf)) {
1482 if (glob_bot(fr))
1483 dprintf(idx, "error Certificate UID doesn't match handle\n");
1484 else
1485 dprintf(idx, "Your certificate UID doesn't match your handle.\n");
1486 killsock(dcc[idx].sock);
1487 lostdcc(idx);
1488 return;
1489 }
1490 }
1491 #endif
1492 /* Make sure users-only/bots-only connects are honored */
1493 if ((dcc[idx].status & STAT_BOTONLY) && !glob_bot(fr)) {
1494 dprintf(idx, "This telnet port is for bots only.\n");
1495 putlog(LOG_BOTS, "*", DCC_NONBOT, dcc[idx].host);
1496 killsock(dcc[idx].sock);
1497 lostdcc(idx);
1498 return;
1499 }
1500 if ((dcc[idx].status & STAT_USRONLY) && glob_bot(fr)) {
1501 dprintf(idx, "error Only users may connect at this port.\n");
1502 putlog(LOG_BOTS, "*", DCC_NONUSER, dcc[idx].host);
1503 killsock(dcc[idx].sock);
1504 lostdcc(idx);
1505 return;
1506 }
1507 dcc[idx].status &= ~(STAT_BOTONLY | STAT_USRONLY);
1508 if (!egg_strcasecmp(buf, "NEW") && (allow_new_telnets || make_userfile)) {
1509 dcc[idx].type = &DCC_TELNET_NEW;
1510 dcc[idx].timeval = now;
1511 dprintf(idx, "\n");
1512 dprintf(idx, IRC_TELNET, botnetnick);
1513 dprintf(idx, IRC_TELNET1);
1514 dprintf(idx, "\nEnter the nickname you would like to use.\n");
1515 return;
1516 }
1517 if (chan_op(fr)) {
1518 if (!require_p)
1519 ok = 1;
1520 }
1521 if (!ok && (glob_party(fr) || glob_bot(fr)))
1522 ok = 1;
1523
1524 if (!ok) {
1525 dprintf(idx, "You don't have access.\n");
1526 putlog(LOG_BOTS, "*", DCC_INVHANDLE, dcc[idx].host, buf);
1527 killsock(dcc[idx].sock);
1528 lostdcc(idx);
1529 return;
1530 }
1531 correct_handle(buf);
1532 strcpy(dcc[idx].nick, buf);
1533 if (glob_bot(fr)) {
1534 if (!egg_strcasecmp(botnetnick, dcc[idx].nick)) {
1535 dprintf(idx, "error You cannot link using my botnetnick.\n");
1536 putlog(LOG_BOTS, "*", DCC_MYBOTNETNICK, dcc[idx].host);
1537 killsock(dcc[idx].sock);
1538 lostdcc(idx);
1539 return;
1540 } else if (in_chain(dcc[idx].nick)) {
1541 struct chat_info *ci;
1542
1543 ci = dcc[idx].u.chat;
1544 dcc[idx].type = &DCC_DUPWAIT;
1545 dcc[idx].u.dupwait = get_data_ptr(sizeof(struct dupwait_info));
1546 dcc[idx].u.dupwait->chat = ci;
1547 dcc[idx].u.dupwait->atr = atr;
1548 return;
1549 }
1550 }
1551 dcc_telnet_pass(idx, atr);
1552 }
1553
1554 #ifdef TLS
1555 int dcc_fingerprint(idx)
1556 {
1557 char *cf, *uf;
1558 struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
1559
1560 get_user_flagrec(dcc[idx].user, &fr, NULL);
1561 /* Check if fingerprint authentication is allowed or required. */
1562 if (dcc[idx].ssl && tls_auth) {
1563 /* Get the fingerprint of the current certificate */
1564 cf = ssl_getfp(dcc[idx].sock);
1565 /* Get the fingerprint of the user, if set */
1566 uf = get_user(&USERENTRY_FPRINT, dcc[idx].user);
1567 if (cf && uf && !strcasecmp(cf, uf)) {
1568 if (!glob_bot(fr))
1569 dprintf(idx, "Used your fingerprint for automatic authentication.\n");
1570 dcc[idx].status |= STAT_FPRINT;
1571 dcc_chat_pass(idx, "+", 1);
1572 /* Required? */
1573 } else if (tls_auth == 2) {
1574 if (glob_bot(fr))
1575 dprintf(idx, "error fingerprint required\n");
1576 else
1577 dprintf(idx, "Certificate authentication required. "
1578 "You need to set your fingerprint.\n");
1579 killsock(dcc[idx].sock);
1580 lostdcc(idx);
1581 }
1582 return 0;
1583 }
1584 return 1;
1585 }
1586 #endif
1587
1588 static void dcc_telnet_pass(int idx, int atr)
1589 {
1590 int ok = 0;
1591 struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
1592
1593 get_user_flagrec(dcc[idx].user, &fr, NULL);
1594 #ifdef TLS
1595 /* Check if fingerprint authentication is allowed or required. */
1596 if (dcc[idx].ssl && tls_auth) {
1597 char *cf, *uf;
1598
1599 /* Get the fingerprint of the current certificate */
1600 cf = ssl_getfp(dcc[idx].sock);
1601 /* Get the fingerprint of the user, if set */
1602 uf = get_user(&USERENTRY_FPRINT, dcc[idx].user);
1603 if (cf && uf && !strcasecmp(cf, uf)) {
1604 char fakepass[2] = "+";
1605
1606 if (!glob_bot(fr))
1607 dprintf(idx, "Used your fingerprint for automatic authentication.\n");
1608 dcc[idx].status |= STAT_FPRINT;
1609 dcc_chat_pass(idx, fakepass, 1);
1610 return;
1611 /* Required? */
1612 } else if (tls_auth == 2) {
1613 if (glob_bot(fr))
1614 dprintf(idx, "error fingerprint required\n");
1615 else
1616 dprintf(idx, "Certificate authentication required. "
1617 "You need to set your fingerprint.\n");
1618 killsock(dcc[idx].sock);
1619 lostdcc(idx);
1620 return;
1621 }
1622 }
1623 #endif
1624 /* No password set? */
1625 if (!glob_bot(fr) && u_pass_match(dcc[idx].user, "-")) {
1626 dprintf(idx, "Can't telnet until you have a password set.\n");
1627 putlog(LOG_MISC, "*", DCC_NOPASS, dcc[idx].nick, dcc[idx].host);
1628 killsock(dcc[idx].sock);
1629 lostdcc(idx);
1630 return;
1631 }
1632 ok = 0;
1633 if (dcc[idx].type == &DCC_DUPWAIT) {
1634 struct chat_info *ci;
1635
1636 ci = dcc[idx].u.dupwait->chat;
1637 nfree(dcc[idx].u.dupwait);
1638 dcc[idx].u.chat = ci;
1639 }
1640 dcc[idx].type = &DCC_CHAT_PASS;
1641 dcc[idx].timeval = now;
1642 if (glob_botmast(fr))
1643 ok = 1;
1644 else if (chan_op(fr)) {
1645 if (!require_p)
1646 ok = 1;
1647 else if (glob_party(fr))
1648 ok = 1;
1649 } else if (glob_party(fr)) {
1650 ok = 1;
1651 dcc[idx].status |= STAT_PARTY;
1652 }
1653 if (glob_bot(fr))
1654 ok = 1;
1655 if (!ok) {
1656 struct chat_info *ci;
1657
1658 ci = dcc[idx].u.chat;
1659 dcc[idx].u.file = get_data_ptr(sizeof(struct file_info));
1660 dcc[idx].u.file->chat = ci;
1661 }
1662
1663 if (glob_bot(fr)) {
1664 #ifdef TLS
1665 /* Ask the peer to switch to ssl communication. We'll continue using plain
1666 * text, until it replies with starttls itself. Bots which don't support ssl
1667 * will simply ignore the request and everything will go on as usual.
1668 */
1669 if (!dcc[idx].ssl) {
1670 dprintf(idx, "starttls\n");
1671 putlog(LOG_BOTS, "*", "Sent STARTTLS to %s...", dcc[idx].nick);
1672 }
1673 #endif
1674 /* Must generate a string consisting of our process ID and the current
1675 * time. The bot will add it's password to the end and use it to generate
1676 * an MD5 checksum (always 128bit). The checksum is sent back and this
1677 * end does the same. The remote bot is only allowed access if the
1678 * checksums match.
1679 *
1680 * Please don't fuck with 'timeval', or the digest we generate later for
1681 * authentication will not be correct - you've been warned ;)
1682 * <Cybah>
1683 */
1684 putlog(LOG_BOTS, "*", "Challenging %s...", dcc[idx].nick);
1685 dprintf(idx, "passreq <%x%x@%s>\n", getpid(), dcc[idx].timeval, botnetnick);
1686 } else {
1687 /* NOTE: The MD5 digest used above to prevent cleartext passwords being
1688 * sent across the net will _only_ work when we have the cleartext
1689 * password. User passwords are encrypted (with blowfish usually)
1690 * so the same thing cant be done. Botnet passwords are always
1691 * stored in cleartext, or at least something that can be reversed.
1692 * <Cybah>
1693 */
1694
1695 /* Turn off remote telnet echo (send IAC WILL ECHO). */
1696 if (dcc[idx].status & STAT_TELNET) {
1697 char buf[1030];
1698 snprintf(buf, sizeof buf, "\n%s%s\r\n", escape_telnet(DCC_ENTERPASS),
1699 TLN_IAC_C TLN_WILL_C TLN_ECHO_C);
1700 tputs(dcc[idx].sock, buf, strlen(buf));
1701 } else
1702 dprintf(idx, "\n%s\n", DCC_ENTERPASS);
1703 }
1704 }
1705
1706 static void eof_dcc_telnet_id(int idx)
1707 {
1708 putlog(LOG_MISC, "*", DCC_LOSTCON, dcc[idx].host, dcc[idx].port);
1709 killsock(dcc[idx].sock);
1710 lostdcc(idx);
1711 }
1712
1713 static void timeout_dcc_telnet_id(int idx)
1714 {
1715 dprintf(idx, "Timeout.\n");
1716 putlog(LOG_MISC, "*", DCC_TTIMEOUT, dcc[idx].host);
1717 killsock(dcc[idx].sock);
1718 lostdcc(idx);
1719 }
1720
1721 static void display_dcc_telnet_id(int idx, char *buf)
1722 {
1723 long tv;
1724
1725 tv = now - dcc[idx].timeval;
1726 sprintf(buf, "t-in waited %lis", tv);
1727 }
1728
1729 struct dcc_table DCC_TELNET_ID = {
1730 "TELNET_ID",
1731 0,
1732 eof_dcc_telnet_id,
1733 dcc_telnet_id,
1734 &password_timeout,
1735 timeout_dcc_telnet_id,
1736 display_dcc_telnet_id,
1737 expmem_dcc_general,
1738 kill_dcc_general,
1739 out_dcc_general
1740 };
1741
1742 static void dcc_telnet_new(int idx, char *buf, int x)
1743 {
1744 int ok = 1;
1745 char work[1024], *p, *q, *r;
1746
1747 buf[HANDLEN] = 0;
1748 if (dcc[idx].status & STAT_TELNET)
1749 strip_telnet(dcc[idx].sock, buf, &x);
1750 dcc[idx].timeval = now;
1751 for (x = 0; x < strlen(buf); x++)
1752 if (buf[x] <= 32)
1753 ok = 0;
1754 if (!ok) {
1755 dprintf(idx, "\nYou can't use weird symbols in your nick.\n");
1756 dprintf(idx, "Try another one please:\n");
1757 } else if (strchr(BADHANDCHARS, buf[0]) != NULL) {
1758 dprintf(idx, "\nYou can't start your nick with the character '%c'\n",
1759 buf[0]);
1760 dprintf(idx, "Try another one please:\n");
1761 } else if (get_user_by_handle(userlist, buf)) {
1762 dprintf(idx, "\nSorry, that nickname is taken already.\n");
1763 dprintf(idx, "Try another one please:\n");
1764 return;
1765 } else if (!egg_strcasecmp(buf, botnetnick))
1766 dprintf(idx, "Sorry, can't use my name for a nick.\n");
1767 else {
1768 strcpy(dcc[idx].nick, buf);
1769 if (make_userfile)
1770 userlist = adduser(userlist,
1771 buf, "-telnet!*@*", "-", sanity_check(default_flags |
1772 USER_PARTY | USER_MASTER | USER_OWNER));
1773 else {
1774 p = strchr(dcc[idx].host, '@');
1775 if (p) {
1776 q = p;
1777 *q = 0;
1778 p++;
1779 r = strchr(p, '.');
1780 if (!r)
1781 simple_sprintf(work, "-telnet!%s@%s", dcc[idx].host, p);
1782 else
1783 simple_sprintf(work, "-telnet!%s@*%s", dcc[idx].host, r);
1784 *q = '@';
1785 } else
1786 simple_sprintf(work, "-telnet!*@*%s", dcc[idx].host);
1787 userlist = adduser(userlist, buf, work, "-",
1788 sanity_check(USER_PARTY | default_flags));
1789 }
1790 reaffirm_owners();
1791 dcc[idx].status = STAT_ECHO | STAT_TELNET;
1792 dcc[idx].type = &DCC_CHAT; /* Just so next line will work */
1793 dcc[idx].user = get_user_by_handle(userlist, buf);
1794 check_dcc_attrs(dcc[idx].user, USER_PARTY | default_flags);
1795 dcc[idx].type = &DCC_TELNET_PW;
1796 if (make_userfile) {
1797 dprintf(idx, "\nYOU ARE THE MASTER/OWNER ON THIS BOT NOW\n");
1798 dprintf(idx, IRC_LIMBO);
1799 putlog(LOG_MISC, "*", DCC_INSTCOMPL, buf);
1800 make_userfile = 0;
1801 write_userfile(-1);
1802 add_note(buf, botnetnick, "Welcome to eggdrop! :)", -1, 0);
1803 }
1804 dprintf(idx, "\nOkay, now choose and enter a password:\n");
1805 dprintf(idx, "(Only the first 15 letters are significant.)\n");
1806 }
1807 }
1808
1809 static void dcc_telnet_pw(int idx, char *buf, int x)
1810 {
1811 char *newpass;
1812 int ok;
1813
1814 if (dcc[idx].status & STAT_TELNET)
1815 strip_telnet(dcc[idx].sock, buf, &x);
1816 buf[16] = 0;
1817 ok = 1;
1818 if (strlen(buf) < 4) {
1819 dprintf(idx, "\nTry to use at least 4 characters in your password.\n");
1820 dprintf(idx, "Choose and enter a password:\n");
1821 return;
1822 }
1823 for (x = 0; x < strlen(buf); x++)
1824 if ((buf[x] <= 32) || (buf[x] == 127))
1825 ok = 0;
1826 if (!ok) {
1827 dprintf(idx, "\nYou can't use weird symbols in your password.\n");
1828 dprintf(idx, "Try another one please:\n");
1829 return;
1830 }
1831 putlog(LOG_MISC, "*", DCC_NEWUSER, dcc[idx].nick, dcc[idx].host,
1832 dcc[idx].port);
1833 if (notify_new[0]) {
1834 char s[121], s1[121], s2[121];
1835
1836 sprintf(s, "Introduced to %s, %s", dcc[idx].nick, dcc[idx].host);
1837 strcpy(s1, notify_new);
1838 splitc(s2, s1, ',');
1839 while (s2[0]) {
1840 rmspace(s2);
1841 add_note(s2, botnetnick, s, -1, 0);
1842 splitc(s2, s1, ',');
1843 }
1844 rmspace(s1);
1845 add_note(s1, botnetnick, s, -1, 0);
1846 }
1847 newpass = newsplit(&buf);
1848 set_user(&USERENTRY_PASS, dcc[idx].user, newpass);
1849 dprintf(idx, "\nRemember that! You'll need it next time you log in.\n");
1850 dprintf(idx, "You now have an account on %s...\n\n\n", botnetnick);
1851 dcc[idx].type = &DCC_CHAT;
1852 dcc[idx].u.chat->channel = -2;
1853 dcc_chatter(idx);
1854 }
1855
1856 static void eof_dcc_telnet_new(int idx)
1857 {
1858 putlog(LOG_MISC, "*", DCC_LOSTNEWUSER, dcc[idx].host, dcc[idx].port);
1859 killsock(dcc[idx].sock);
1860 lostdcc(idx);
1861 }
1862
1863 static void eof_dcc_telnet_pw(int idx)
1864 {
1865 putlog(LOG_MISC, "*", DCC_LOSTNEWUSR2, dcc[idx].nick, dcc[idx].host,
1866 dcc[idx].port);
1867 deluser(dcc[idx].nick);
1868 killsock(dcc[idx].sock);
1869 lostdcc(idx);
1870 }
1871
1872 static void tout_dcc_telnet_new(int idx)
1873 {
1874 dprintf(idx, "Guess you're not there. Bye.\n");
1875 putlog(LOG_MISC, "*", DCC_TIMEOUTUSER, dcc[idx].host, dcc[idx].port);
1876 killsock(dcc[idx].sock);
1877 lostdcc(idx);
1878 }
1879
1880 static void tout_dcc_telnet_pw(int idx)
1881 {
1882 dprintf(idx, "Guess you're not there. Bye.\n");
1883 putlog(LOG_MISC, "*", DCC_TIMEOUTUSR2, dcc[idx].nick,
1884 dcc[idx].host, dcc[idx].port);
1885 killsock(dcc[idx].sock);
1886 lostdcc(idx);
1887 }
1888
1889 static void display_dcc_telnet_new(int idx, char *buf)
1890 {
1891 long tv;
1892
1893 tv = now - dcc[idx].timeval;
1894 sprintf(buf, "new waited %lis", tv);
1895 }
1896
1897 static void display_dcc_telnet_pw(int idx, char *buf)
1898 {
1899 long tv;
1900
1901 tv = now - dcc[idx].timeval;
1902 sprintf(buf, "newp waited %lis", tv);
1903 }
1904
1905 struct dcc_table DCC_TELNET_NEW = {
1906 "TELNET_NEW",
1907 0,
1908 eof_dcc_telnet_new,
1909 dcc_telnet_new,
1910 &password_timeout,
1911 tout_dcc_telnet_new,
1912 display_dcc_telnet_new,
1913 expmem_dcc_general,
1914 kill_dcc_general,
1915 out_dcc_general
1916 };
1917
1918 struct dcc_table DCC_TELNET_PW = {
1919 "TELNET_PW",
1920 0,
1921 eof_dcc_telnet_pw,
1922 dcc_telnet_pw,
1923 &password_timeout,
1924 tout_dcc_telnet_pw,
1925 display_dcc_telnet_pw,
1926 expmem_dcc_general,
1927 kill_dcc_general,
1928 out_dcc_general
1929 };
1930
1931 static int call_tcl_func(char *name, int idx, char *args)
1932 {
1933 char s[11];
1934
1935 sprintf(s, "%d", idx);
1936 Tcl_SetVar(interp, "_n", s, 0);
1937 Tcl_SetVar(interp, "_a", args, 0);
1938 if (Tcl_VarEval(interp, name, " $_n $_a", NULL) == TCL_ERROR) {
1939 putlog(LOG_MISC, "*", DCC_TCLERROR, name, tcl_resultstring());
1940 Tcl_BackgroundError(interp);
1941 return -1;
1942 }
1943 return tcl_resultint();
1944 }
1945
1946 static void dcc_script(int idx, char *buf, int len)
1947 {
1948 long oldsock;
1949
1950 if (dcc[idx].status & STAT_TELNET)
1951 strip_telnet(dcc[idx].sock, buf, &len);
1952 if (!len)
1953 return;
1954
1955 dcc[idx].timeval = now;
1956 oldsock = dcc[idx].sock; /* Remember the socket number. */
1957 if (call_tcl_func(dcc[idx].u.script->command, dcc[idx].sock, buf)) {
1958 void *old_other = NULL;
1959
1960 /* Check whether the socket and dcc entry are still valid. They
1961 * might have been killed by `killdcc'. */
1962 if (dcc[idx].sock != oldsock || idx > max_dcc)
1963 return;
1964
1965 old_other = dcc[idx].u.script->u.other;
1966 dcc[idx].type = dcc[idx].u.script->type;
1967 nfree(dcc[idx].u.script);
1968 dcc[idx].u.other = old_other;
1969 if (dcc[idx].type == &DCC_SOCKET) {
1970 /* Kill the whole thing off */
1971 killsock(dcc[idx].sock);
1972 lostdcc(idx);
1973 return;
1974 }
1975 if (dcc[idx].type == &DCC_CHAT) {
1976 if (dcc[idx].u.chat->channel >= 0) {
1977 chanout_but(-1, dcc[idx].u.chat->channel, DCC_JOIN, dcc[idx].nick);
1978 if (dcc[idx].u.chat->channel < 10000)
1979 botnet_send_join_idx(idx, -1);
1980 check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
1981 geticon(idx), dcc[idx].sock, dcc[idx].host);
1982 }
1983 check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
1984 }
1985 }
1986 }
1987
1988 static void eof_dcc_script(int idx)
1989 {
1990 void *old;
1991 int oldflags;
1992
1993 /* This will stop a killdcc from working, incase the script tries
1994 * to kill it's controlling socket while handling an EOF <cybah>
1995 */
1996 oldflags = dcc[idx].type->flags;
1997 dcc[idx].type->flags &= ~(DCT_VALIDIDX);
1998 /* Tell the script they're gone: */
1999 call_tcl_func(dcc[idx].u.script->command, dcc[idx].sock, "");
2000 /* Restore the flags */
2001 dcc[idx].type->flags = oldflags;
2002 old = dcc[idx].u.script->u.other;
2003 dcc[idx].type = dcc[idx].u.script->type;
2004 nfree(dcc[idx].u.script);
2005 dcc[idx].u.other = old;
2006 /* Then let it fall thru to the real one */
2007 if (dcc[idx].type && dcc[idx].type->eof)
2008 dcc[idx].type->eof(idx);
2009 else {
2010 putlog(LOG_MISC, "*", DCC_DEADSOCKET, dcc[idx].sock, dcc[idx].type->name);
2011 killsock(dcc[idx].sock);
2012 lostdcc(idx);
2013 }
2014 }
2015
2016 static void display_dcc_script(int idx, char *buf)
2017 {
2018 sprintf(buf, "scri %s", dcc[idx].u.script->command);
2019 }
2020
2021 static int expmem_dcc_script(void *x)
2022 {
2023 register struct script_info *p = (struct script_info *) x;
2024 int tot = sizeof(struct script_info);
2025
2026 if (p->type && p->u.other)
2027 tot += p->type->expmem(p->u.other);
2028 return tot;
2029 }
2030
2031 static void kill_dcc_script(int idx, void *x)
2032 {
2033 register struct script_info *p = (struct script_info *) x;
2034
2035 if (p->type && p->u.other)
2036 p->type->kill(idx, p->u.other);
2037 nfree(p);
2038 }
2039
2040 static void out_dcc_script(int idx, char *buf, void *x)
2041 {
2042 register struct script_info *p = (struct script_info *) x;
2043
2044 if (p && p->type && p->u.other)
2045 p->type->output(idx, buf, p->u.other);
2046 else
2047 tputs(dcc[idx].sock, buf, strlen(buf));
2048 }
2049
2050 struct dcc_table DCC_SCRIPT = {
2051 "SCRIPT",
2052 DCT_VALIDIDX,
2053 eof_dcc_script,
2054 dcc_script,
2055 NULL,
2056 NULL,
2057 display_dcc_script,
2058 expmem_dcc_script,
2059 kill_dcc_script,
2060 out_dcc_script
2061 };
2062
2063 static void dcc_socket(int idx, char *buf, int len)
2064 {
2065 }
2066
2067 static void eof_dcc_socket(int idx)
2068 {
2069 killsock(dcc[idx].sock);
2070 lostdcc(idx);
2071 }
2072
2073 static void display_dcc_socket(int idx, char *buf)
2074 {
2075 strcpy(buf, "sock (stranded)");
2076 }
2077
2078 struct dcc_table DCC_SOCKET = {
2079 "SOCKET",
2080 DCT_VALIDIDX,
2081 eof_dcc_socket,
2082 dcc_socket,
2083 NULL,
2084 NULL,
2085 display_dcc_socket,
2086 NULL,
2087 NULL,
2088 NULL
2089 };
2090
2091 static void display_dcc_lost(int idx, char *buf)
2092 {
2093 strcpy(buf, "lost");
2094 }
2095
2096 struct dcc_table DCC_LOST = {
2097 "LOST",
2098 0,
2099 NULL,
2100 dcc_socket,
2101 NULL,
2102 NULL,
2103 display_dcc_lost,
2104 NULL,
2105 NULL,
2106 NULL
2107 };
2108
2109 void dcc_identwait(int idx, char *buf, int len)
2110 {
2111 /* Ignore anything now */
2112 }
2113
2114 void eof_dcc_identwait(int idx)
2115 {
2116 int i;
2117
2118 putlog(LOG_MISC, "*", DCC_LOSTCONN, dcc[idx].host, dcc[idx].port);
2119 for (i = 0; i < dcc_total; i++)
2120 if ((dcc[i].type == &DCC_IDENT) &&
2121 (dcc[i].u.ident_sock == dcc[idx].sock)) {
2122 killsock(dcc[i].sock); /* Cleanup ident socket */
2123 dcc[i].u.other = 0;
2124 lostdcc(i);
2125 break;
2126 }
2127 killsock(dcc[idx].sock); /* Cleanup waiting socket */
2128 dcc[idx].u.other = 0;
2129 lostdcc(idx);
2130 }
2131
2132 static void display_dcc_identwait(int idx, char *buf)
2133 {
2134 long tv;
2135
2136 tv = now - dcc[idx].timeval;
2137 sprintf(buf, "idtw waited %lis", tv);
2138 }
2139
2140 struct dcc_table DCC_IDENTWAIT = {
2141 "IDENTWAIT",
2142 0,
2143 eof_dcc_identwait,
2144 dcc_identwait,
2145 NULL,
2146 NULL,
2147 display_dcc_identwait,
2148 NULL,
2149 NULL,
2150 NULL
2151 };
2152
2153 void dcc_ident(int idx, char *buf, int len)
2154 {
2155 char response[512], uid[512], buf1[UHOSTLEN];
2156 int i;
2157
2158 *response = *uid = '\0';
2159 sscanf(buf, "%*[^:]:%[^:]:%*[^:]:%[^\n]\n", response, uid);
2160 rmspace(response);
2161 if (response[0] != 'U') {
2162 dcc[idx].timeval = now;
2163 return;
2164 }
2165 rmspace(uid);
2166 uid[20] = 0; /* 20 character ident max */
2167 for (i = 0; i < dcc_total; i++)
2168 if ((dcc[i].type == &DCC_IDENTWAIT) &&
2169 (dcc[i].sock == dcc[idx].u.ident_sock)) {
2170 simple_sprintf(buf1, "%s@%s", uid, dcc[idx].host);
2171 dcc_telnet_got_ident(i, buf1);
2172 }
2173 dcc[idx].u.other = 0;
2174 killsock(dcc[idx].sock);
2175 lostdcc(idx);
2176 }
2177
2178 void eof_dcc_ident(int idx)
2179 {
2180 char buf[UHOSTLEN];
2181 int i;
2182
2183 for (i = 0; i < dcc_total; i++)
2184 if ((dcc[i].type == &DCC_IDENTWAIT) &&
2185 (dcc[i].sock == dcc[idx].u.ident_sock)) {
2186 putlog(LOG_MISC, "*", DCC_EOFIDENT);
2187 simple_sprintf(buf, "telnet@%s", dcc[idx].host);
2188 dcc_telnet_got_ident(i, buf);
2189 }
2190 killsock(dcc[idx].sock);
2191 dcc[idx].u.other = 0;
2192 lostdcc(idx);
2193 }
2194
2195 static void display_dcc_ident(int idx, char *buf)
2196 {
2197 sprintf(buf, "idnt (sock %d)", dcc[idx].u.ident_sock);
2198 }
2199
2200 struct dcc_table DCC_IDENT = {
2201 "IDENT",
2202 0,
2203 eof_dcc_ident,
2204 dcc_ident,
2205 &identtimeout,
2206 eof_dcc_ident,
2207 display_dcc_ident,
2208 NULL,
2209 NULL,
2210 NULL
2211 };
2212
2213 static void dcc_telnet_got_ident(int i, char *host)
2214 {
2215 int idx;
2216 char x[1024];
2217
2218 for (idx = 0; idx < dcc_total; idx++)
2219 if ((dcc[idx].type == &DCC_TELNET) &&
2220 (dcc[idx].sock == dcc[i].u.ident_sock))
2221 break;
2222 dcc[i].u.other = 0;
2223 if (dcc_total == idx) {
2224 putlog(LOG_MISC, "*", DCC_LOSTIDENT);
2225 killsock(dcc[i].sock);
2226 lostdcc(i);
2227 return;
2228 }
2229 strncpyz(dcc[i].host, host, UHOSTLEN);
2230 egg_snprintf(x, sizeof x, "-telnet!%s", dcc[i].host);
2231 if (protect_telnet && !make_userfile) {
2232 struct userrec *u;
2233 int ok = 1;
2234
2235 u = get_user_by_host(x);
2236 /* Not a user or +p & require p OR +o */
2237 if (!u)
2238 ok = 0;
2239 else if (require_p && !(u->flags & USER_PARTY))
2240 ok = 0;
2241 else if (!require_p && !(u->flags & USER_OP))
2242 ok = 0;
2243 if (!ok && u && (u->flags & USER_BOT))
2244 ok = 1;
2245 if (!ok && (dcc[idx].status & LSTN_PUBLIC))
2246 ok = 1;
2247 if (!ok) {
2248 putlog(LOG_MISC, "*", DCC_NOACCESS, dcc[i].host);
2249 killsock(dcc[i].sock);
2250 lostdcc(i);
2251 return;
2252 }
2253 }
2254 if (match_ignore(x)) {
2255 killsock(dcc[i].sock);
2256 lostdcc(i);
2257 return;
2258 }
2259
2260 /* Script? */
2261 if (!strcmp(dcc[idx].nick, "(script)")) {
2262 dcc[i].type = &DCC_SOCKET;
2263 dcc[i].u.other = NULL;
2264 strcpy(dcc[i].nick, "*");
2265 check_tcl_listen(dcc[idx].host, dcc[i].sock);
2266 return;
2267 }
2268 /* Do not buffer data anymore. All received and stored data is passed
2269 * over to the dcc functions from now on. */
2270 sockoptions(dcc[i].sock, EGG_OPTION_UNSET, SOCK_BUFFER);
2271
2272 dcc[i].type = &DCC_TELNET_ID;
2273 dcc[i].u.chat = get_data_ptr(sizeof(struct chat_info));
2274 egg_bzero(dcc[i].u.chat, sizeof(struct chat_info));
2275
2276 /* Note: we don't really care about telnet status here. We use the
2277 * STATUS option as a hopefully harmless way to detect if the other
2278 * side is a telnet client or not. */
2279 dprintf(i, TLN_IAC_C TLN_WILL_C TLN_STATUS_C "\n");
2280
2281 /* Copy acceptable-nick/host mask */
2282 dcc[i].status = STAT_TELNET | STAT_ECHO;
2283 if (!strcmp(dcc[idx].nick, "(bots)"))
2284 dcc[i].status |= STAT_BOTONLY;