From 54d9afdd1747ece71597c50a4719a676151c82da Mon Sep 17 00:00:00 2001 From: Michiel Broek Date: Mon, 24 Nov 2003 22:19:50 +0000 Subject: [PATCH] Made outgoing telnet protocol work --- ChangeLog | 12 ++++-- lib/clcomm.c | 8 +++- mbcico/Makefile | 4 +- mbcico/call.c | 2 + mbcico/session.c | 1 + mbcico/telnio.c | 106 ++++++++++++++++++++++++++++++++--------------- mbcico/ttyio.c | 1 + mbcico/ttyio.h | 4 ++ 8 files changed, 97 insertions(+), 41 deletions(-) diff --git a/ChangeLog b/ChangeLog index 170b5fe6..1d0ab2a2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,22 +4,26 @@ $Id$ v0.39.2 21-Nov-2003 NOTE: Don't use this version yet, mbtelnetd might be removed again! + Well, almost certain that it will be removed. This version is + in cvs because I need to test it on several machines at once. general: With the use of a proxy based on the iftelnetd from the ifmail - package incoming telnet sessions are now supported. Outgoing - telnet still doesn't work. + package incoming telnet sessions are now supported. + Outgoing telnet finally works. upgrade: See mbtelnetd.html how to install mbtelnetd. common.a: - A small fix in printable function. + A small fix in printable log function. + Added a safety check for the TCP registrate function to prevent + stale TCP/IP mailer counts. mbcico: If called by inetd with the -t itn parameters, mbcico aborts and writes a short instruction in the log to install mbtelnetd. - Outgoing telnet works almost. + Outgoing telnet works. mbtelnetd: New program, a proxy to handle incoming telnet/vmodem sessions. diff --git a/lib/clcomm.c b/lib/clcomm.c index 26de8cd8..35b5079a 100644 --- a/lib/clcomm.c +++ b/lib/clcomm.c @@ -4,7 +4,7 @@ * Purpose ...............: Client/Server communications * ***************************************************************************** - * Copyright (C) 1997-2002 + * Copyright (C) 1997-2003 * * Michiel Broek FIDO: 2:280/2802 * Beekmansbos 10 @@ -37,6 +37,7 @@ int do_quiet = FALSE; /* Quiet flag */ int show_log = FALSE; /* Show loglines on screen */ int most_debug = FALSE; /* Toggle normal/most debugging */ +int tcp_regged = FALSE; /* Safety TCP registration flag */ char progname[21]; /* Program name */ char logdebug[PATH_MAX]; /* Debug logfile */ char logfile[PATH_MAX]; /* Normal logfile */ @@ -383,7 +384,10 @@ void IsDoing(const char *format, ...) void RegTCP(void) { - SockS("ATCP:1,%d;", mypid); + if (!tcp_regged) + SockS("ATCP:1,%d;", mypid); + + tcp_regged = TRUE; } diff --git a/mbcico/Makefile b/mbcico/Makefile index 6d62bef8..6ba410fc 100644 --- a/mbcico/Makefile +++ b/mbcico/Makefile @@ -126,13 +126,13 @@ filetime.o: ../config.h ../lib/libs.h filetime.h ftsc.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/nodelist.h ../lib/clcomm.h ../lib/mberrors.h session.h ttyio.h statetbl.h config.h ftsc.h rdoptions.h recvbark.h filelist.h sendbark.h respfreq.h xmrecv.h xmsend.h inbound.h atoul.o: ../config.h ../lib/libs.h atoul.h portsel.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/clcomm.h portsel.h -ttyio.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/clcomm.h telnio.h ttyio.h lutil.h +ttyio.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/clcomm.h telnio.h hydra.h ttyio.h lutil.h lutil.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/common.h ../lib/clcomm.h lutil.h scanout.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/clcomm.h ../lib/dbftn.h config.h scanout.h lutil.h emsi.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/nodelist.h ../lib/dbnode.h ../lib/clcomm.h ../lib/mberrors.h ttyio.h session.h statetbl.h config.h emsi.h emsidat.h hydra.h rdoptions.h tcp.h wazoo.h inbound.h ulock.o: ../config.h ../lib/libs.h ../lib/clcomm.h callstat.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/clcomm.h ../lib/common.h callstat.h -session.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/clcomm.h ../lib/nodelist.h ../lib/mberrors.h ttyio.h statetbl.h emsi.h ftsc.h session.h yoohoo.h mbcico.h binkp.h callstat.h inbound.h opentcp.h +session.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/clcomm.h ../lib/nodelist.h ../lib/mberrors.h ttyio.h statetbl.h emsi.h ftsc.h session.h yoohoo.h mbcico.h binkp.h callstat.h inbound.h opentcp.h telnio.h call.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/nodelist.h ../lib/clcomm.h ../lib/dbnode.h ../lib/mberrors.h session.h callstat.h call.h config.h dial.h lutil.h portsel.h openport.h opentcp.h rdoptions.h inbound.h mbcico.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/nodelist.h ../lib/clcomm.h ../lib/dbcfg.h ../lib/dbnode.h ../lib/dbftn.h ../lib/mberrors.h config.h answer.h call.h lutil.h mbcico.h session.h outstat.o: ../config.h ../lib/libs.h ../lib/structs.h ../lib/users.h ../lib/records.h ../lib/common.h ../lib/nodelist.h ../lib/clcomm.h ../lib/dbcfg.h ../lib/dbnode.h ../lib/dbftn.h ../lib/mberrors.h scanout.h callstat.h outstat.h diff --git a/mbcico/call.c b/mbcico/call.c index 0dd4db11..29cf7cac 100644 --- a/mbcico/call.c +++ b/mbcico/call.c @@ -53,6 +53,7 @@ extern int tcp_mode; +extern int telnet; extern int immediatecall; extern char *forcedphone; extern char *forcedline; @@ -178,6 +179,7 @@ int call(faddr *addr) #ifdef USE_TELNET } else if (strcmp(protocol, "telnet") == 0) { tcp_mode = TCPMODE_ITN; + telnet = TRUE; #endif } else { Syslog('+', "No common TCP/IP protocols for node %s", nlent->name); diff --git a/mbcico/session.c b/mbcico/session.c index a43c76f4..737146a3 100644 --- a/mbcico/session.c +++ b/mbcico/session.c @@ -48,6 +48,7 @@ #include "callstat.h" #include "inbound.h" #include "opentcp.h" +#include "telnio.h" extern int tcp_mode; diff --git a/mbcico/telnio.c b/mbcico/telnio.c index 1a5f93a1..5026983f 100644 --- a/mbcico/telnio.c +++ b/mbcico/telnio.c @@ -1,6 +1,6 @@ /***************************************************************************** * - * $id$ + * $Id$ * Purpose ...............: Telnet IO filter * ***************************************************************************** @@ -37,7 +37,7 @@ static int tellen; - +static int buflen = 0; /* --- This is an artwork of serge terekhov, 2:5000/13@fidonet :) --- */ @@ -96,24 +96,19 @@ int telnet_read(char *buf, int len) char *q, *p; static char telbuf[4]; - Syslog('s', "telnet_read(buf, %d tellen=%d)", len, tellen); while ((n == 0) && (n = read (0, buf + tellen, H_ZIPBUFLEN - tellen)) > 0) { - Syslog('s', " n=%d tellen=%d", n, tellen); if (n < 0) { - Syslog('s', "telnet_read n=%d", n); return n; } if (tellen) { - Syslog('s', " memcpy"); memcpy(buf, telbuf, tellen); n += tellen; tellen = 0; } if (memchr (buf, IAC, n)) { - Syslog('s', " IAC detected"); for (p = q = buf; n--; ) if ((m = (unsigned char)*q++) != IAC) *p++ = m; @@ -154,7 +149,6 @@ int telnet_read(char *buf, int len) } } - Syslog('s', " return n=%d", n); return n; } @@ -168,7 +162,6 @@ int telnet_write(char *buf, int len) char *q; int k, l; - Syslog('s', "telnet_write(buf, %d)", len); l = len; while ((len > 0) && (q = memchr(buf, IAC, len))) { k = (q - buf) + 1; @@ -187,46 +180,95 @@ int telnet_write(char *buf, int len) +/* + * Process a passed buffer for telnet escapes sequences. Use a temp + * buffer for sequences at the begin and end of a data block so that + * series of buffers are processed as if it is one stream. + */ int telnet_buffer(char *buf, int len) { - int i, j, m = 0, rc; - + int i, j, m = 0, rc; + static char telbuf[4]; + rc = len; + if (buflen > 2) { + WriteError("buflen=%d"); + buflen = 0; + } + + /* + * If from a previous call there are some telnet escape characters left in + * telbuf, insert these into the buffer array. + */ + if (buflen) { + memmove(buf+buflen, buf, (size_t)len); + memcpy(buf, telbuf, (size_t)buflen); + rc += buflen; + buflen = 0; + } + if (memchr (buf, IAC, rc)) { - Syslog('s', "telnet_buffer: IAC in input stream rc=%d", rc); -// Syslogp('s', printable(buf, rc)); j = 0; for (i = 0; i < rc; i++) { if ((buf[i] & 0xff) == IAC) { + telbuf[buflen] = buf[i] & 0xff; + buflen++; i++; + if (i >= rc) + break; switch (buf[i] & 0xff) { - case WILL: i++; - m = buf[i] & 0xff; - Syslog('s', "Telnet recv WILL %d", m); - if (m != TOPT_BIN && m != TOPT_SUPP && m != TOPT_ECHO) - telnet_answer(DONT, m); + case WILL: if ((i+1) < rc) { + buflen = 0; + i++; + m = buf[i] & 0xff; + Syslog('s', "Telnet recv WILL %d", m); + if (m != TOPT_BIN && m != TOPT_SUPP && m != TOPT_ECHO) + telnet_answer(DONT, m); + } else { + telbuf[buflen] = WILL; + buflen++; + } break; - case WONT: i++; - m = buf[i] & 0xff; - Syslog('s', "Telnet recv WONT %d", m); + case WONT: if ((i+1) < rc) { + buflen = 0; + i++; + m = buf[i] & 0xff; + Syslog('s', "Telnet recv WONT %d", m); + } else { + telbuf[buflen] = WONT; + buflen++; + } break; - case DO: i++; - m = buf[i] & 0xff; - Syslog('s', "Telnet recv DO %d", m); - if (m != TOPT_BIN && m != TOPT_SUPP && m != TOPT_ECHO) - telnet_answer(WONT, m); + case DO: if ((i+1) < rc) { + buflen = 0; + i++; + m = buf[i] & 0xff; + Syslog('s', "Telnet recv DO %d", m); + if (m != TOPT_BIN && m != TOPT_SUPP && m != TOPT_ECHO) + telnet_answer(WONT, m); + } else { + telbuf[buflen] = DO; + buflen++; + } break; - case DONT: i++; - m = buf[i] & 0xff; - Syslog('s', "Telnet recv DONT %d", m); + case DONT: if ((i+1) < rc) { + buflen = 0; + i++; + m = buf[i] & 0xff; + Syslog('s', "Telnet recv DONT %d", m); + } else { + telbuf[buflen] = DONT; + buflen++; + } break; case IAC: buf[j] = buf[i]; j++; - Syslog('s', "Telnet recv escaped IAC"); + buflen = 0; break; default: m = buf[i] & 0xff; - Syslog('s', "TELNET: recv IAC %d, this is not good", m); + Syslog('s', "TELNET: recv IAC %d, this is not good, i=%d j=%d", m, i, j); + buflen = 0; buf[j] = IAC; j++; buf[j] = m; @@ -239,8 +281,6 @@ int telnet_buffer(char *buf, int len) } } rc = j; -// Syslog('s', "new rc=%d", rc); -// Syslogp('s', printable(buf, rc)); } return rc; diff --git a/mbcico/ttyio.c b/mbcico/ttyio.c index a1c3c7dc..7aa03c78 100644 --- a/mbcico/ttyio.c +++ b/mbcico/ttyio.c @@ -39,6 +39,7 @@ #include "../lib/common.h" #include "../lib/clcomm.h" #include "telnio.h" +#include "hydra.h" #include "ttyio.h" #include "lutil.h" diff --git a/mbcico/ttyio.h b/mbcico/ttyio.h index f35c1e33..26d52147 100644 --- a/mbcico/ttyio.h +++ b/mbcico/ttyio.h @@ -67,10 +67,14 @@ #define SO 0x0e #define SI 0x0f #define DLE 0x10 +#ifndef XON #define XON 0x11 +#endif #define DC1 0x11 #define DC2 0x12 +#ifndef XOFF #define XOFF 0x13 +#endif #define DC3 0x13 #define DC4 0x14 #define NAK 0x15