/[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.6.2.4 - (show annotations) (download) (as text)
Sat Feb 26 19:26:53 2011 UTC (8 years, 4 months ago) by thommey
Branch: gettext
Changes since 1.6.2.3: +15 -4 lines
File MIME type: text/x-chdr
script listen sockets with the pub flag no longer perform ident lookups

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