#include <sys/cdefs.h>
#include <stdlib.h>
#include <err.h>
#ifdef __FBSDID
__FBSDID("$FreeBSD: src/crypto/telnet/telnet/sys_bsd.c,v 1.2.8.4 2002/04/13 10:59:08 markm Exp $");
#endif
#ifndef __unused
#define __unused __attribute__((__unused__))
#endif
#ifndef lint
static const char sccsid[] = "@(#)sys_bsd.c 8.4 (Berkeley) 5/30/95";
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <arpa/telnet.h>
#include "ring.h"
#include "fdset.h"
#include "defines.h"
#include "externs.h"
#include "types.h"
int
tout,
tin,
net;
#ifndef USE_TERMIO
struct tchars otc = { 0 }, ntc = { 0 };
struct ltchars oltc = { 0 }, nltc = { 0 };
struct sgttyb ottyb = { 0 }, nttyb = { 0 };
int olmode = 0;
# define cfgetispeed(ptr) (ptr)->sg_ispeed
# define cfgetospeed(ptr) (ptr)->sg_ospeed
# define old_tc ottyb
#else
struct termio old_tc = { 0, 0, 0, 0, {}, 0, 0 };
# ifndef TCSANOW
# ifdef TCSETS
# define TCSANOW TCSETS
# define TCSADRAIN TCSETSW
# define tcgetattr(f, t) ioctl(f, TCGETS, (char *)t)
# else
# ifdef TCSETA
# define TCSANOW TCSETA
# define TCSADRAIN TCSETAW
# define tcgetattr(f, t) ioctl(f, TCGETA, (char *)t)
# else
# define TCSANOW TIOCSETA
# define TCSADRAIN TIOCSETAW
# define tcgetattr(f, t) ioctl(f, TIOCGETA, (char *)t)
# endif
# endif
# define tcsetattr(f, a, t) ioctl(f, a, (char *)t)
# define cfgetospeed(ptr) ((ptr)->c_cflag&CBAUD)
# ifdef CIBAUD
# define cfgetispeed(ptr) (((ptr)->c_cflag&CIBAUD) >> IBSHIFT)
# else
# define cfgetispeed(ptr) cfgetospeed(ptr)
# endif
# endif
# ifdef sysV88
# define TIOCFLUSH TC_PX_DRAIN
# endif
#endif
static fd_set *ibitsp, *obitsp, *xbitsp;
int fdsn;
#ifdef SIGINT
static SIG_FUNC_RET intr(int);
#endif
#ifdef SIGQUIT
static SIG_FUNC_RET intr2(int);
#endif
#ifdef SIGTSTP
static SIG_FUNC_RET susp(int);
#endif
#ifdef SIGINFO
static SIG_FUNC_RET ayt(int);
#endif
void
init_sys(void)
{
tout = fileno(stdout);
tin = fileno(stdin);
errno = 0;
}
int
TerminalWrite(char *buf, int n)
{
return write(tout, buf, n);
}
int
TerminalRead(char *buf, int n)
{
return read(tin, buf, n);
}
int
TerminalAutoFlush(void)
{
#if defined(LNOFLSH)
int flush;
ioctl(0, TIOCLGET, (char *)&flush);
return !(flush&LNOFLSH);
#else
return 1;
#endif
}
#ifdef KLUDGELINEMODE
extern int kludgelinemode;
#endif
int
TerminalSpecialChars(int c)
{
if (c == termIntChar) {
intp();
return 0;
} else if (c == termQuitChar) {
#ifdef KLUDGELINEMODE
if (kludgelinemode)
sendbrk();
else
#endif
sendabort();
return 0;
} else if (c == termEofChar) {
if (my_want_state_is_will(TELOPT_LINEMODE)) {
sendeof();
return 0;
}
return 1;
} else if (c == termSuspChar) {
sendsusp();
return(0);
} else if (c == termFlushChar) {
xmitAO();
return 0;
} else if (!MODE_LOCAL_CHARS(globalmode)) {
if (c == termKillChar) {
xmitEL();
return 0;
} else if (c == termEraseChar) {
xmitEC();
return 0;
}
}
return 1;
}
void
TerminalFlushOutput(void)
{
#ifdef TIOCFLUSH
(void) ioctl(fileno(stdout), TIOCFLUSH, (char *) 0);
#else
(void) ioctl(fileno(stdout), TCFLSH, (char *) 0);
#endif
}
void
TerminalSaveState(void)
{
#ifndef USE_TERMIO
ioctl(0, TIOCGETP, (char *)&ottyb);
ioctl(0, TIOCGETC, (char *)&otc);
ioctl(0, TIOCGLTC, (char *)&oltc);
ioctl(0, TIOCLGET, (char *)&olmode);
ntc = otc;
nltc = oltc;
nttyb = ottyb;
#else
tcgetattr(0, &old_tc);
new_tc = old_tc;
#ifndef VDISCARD
termFlushChar = CONTROL('O');
#endif
#ifndef VWERASE
termWerasChar = CONTROL('W');
#endif
#ifndef VREPRINT
termRprntChar = CONTROL('R');
#endif
#ifndef VLNEXT
termLiteralNextChar = CONTROL('V');
#endif
#ifndef VSTART
termStartChar = CONTROL('Q');
#endif
#ifndef VSTOP
termStopChar = CONTROL('S');
#endif
#ifndef VSTATUS
termAytChar = CONTROL('T');
#endif
#endif
}
cc_t *
tcval(int func)
{
switch(func) {
case SLC_IP: return(&termIntChar);
case SLC_ABORT: return(&termQuitChar);
case SLC_EOF: return(&termEofChar);
case SLC_EC: return(&termEraseChar);
case SLC_EL: return(&termKillChar);
case SLC_XON: return(&termStartChar);
case SLC_XOFF: return(&termStopChar);
case SLC_FORW1: return(&termForw1Char);
#ifdef USE_TERMIO
case SLC_FORW2: return(&termForw2Char);
# ifdef VDISCARD
case SLC_AO: return(&termFlushChar);
# endif
# ifdef VSUSP
case SLC_SUSP: return(&termSuspChar);
# endif
# ifdef VWERASE
case SLC_EW: return(&termWerasChar);
# endif
# ifdef VREPRINT
case SLC_RP: return(&termRprntChar);
# endif
# ifdef VLNEXT
case SLC_LNEXT: return(&termLiteralNextChar);
# endif
# ifdef VSTATUS
case SLC_AYT: return(&termAytChar);
# endif
#endif
case SLC_SYNCH:
case SLC_BRK:
case SLC_EOR:
default:
return((cc_t *)0);
}
}
void
TerminalDefaultChars(void)
{
#ifndef USE_TERMIO
ntc = otc;
nltc = oltc;
nttyb.sg_kill = ottyb.sg_kill;
nttyb.sg_erase = ottyb.sg_erase;
#else
memcpy(new_tc.c_cc, old_tc.c_cc, sizeof(old_tc.c_cc));
# ifndef VDISCARD
termFlushChar = CONTROL('O');
# endif
# ifndef VWERASE
termWerasChar = CONTROL('W');
# endif
# ifndef VREPRINT
termRprntChar = CONTROL('R');
# endif
# ifndef VLNEXT
termLiteralNextChar = CONTROL('V');
# endif
# ifndef VSTART
termStartChar = CONTROL('Q');
# endif
# ifndef VSTOP
termStopChar = CONTROL('S');
# endif
# ifndef VSTATUS
termAytChar = CONTROL('T');
# endif
#endif
}
void
TerminalNewMode(int f)
{
static int prevmode = 0;
#ifndef USE_TERMIO
struct tchars tc;
struct ltchars ltc;
struct sgttyb sb;
int lmode;
#else
struct termio tmp_tc;
#endif
int onoff;
int old;
cc_t esc;
globalmode = f&~MODE_FORCE;
if (prevmode == f)
return;
old = ttyflush(SYNCHing|flushout);
if (old < 0 || old > 1) {
#ifdef USE_TERMIO
tcgetattr(tin, &tmp_tc);
#endif
do {
#ifdef USE_TERMIO
tcsetattr(tin, TCSADRAIN, &tmp_tc);
#endif
old = ttyflush(SYNCHing|flushout);
} while (old < 0 || old > 1);
}
old = prevmode;
prevmode = f&~MODE_FORCE;
#ifndef USE_TERMIO
sb = nttyb;
tc = ntc;
ltc = nltc;
lmode = olmode;
#else
tmp_tc = new_tc;
#endif
if (f&MODE_ECHO) {
#ifndef USE_TERMIO
sb.sg_flags |= ECHO;
#else
tmp_tc.c_lflag |= ECHO;
tmp_tc.c_oflag |= ONLCR;
if (crlf)
tmp_tc.c_iflag |= ICRNL;
#endif
} else {
#ifndef USE_TERMIO
sb.sg_flags &= ~ECHO;
#else
tmp_tc.c_lflag &= ~ECHO;
tmp_tc.c_oflag &= ~ONLCR;
#endif
}
if ((f&MODE_FLOW) == 0) {
#ifndef USE_TERMIO
tc.t_startc = _POSIX_VDISABLE;
tc.t_stopc = _POSIX_VDISABLE;
#else
tmp_tc.c_iflag &= ~(IXOFF|IXON);
} else {
if (restartany < 0) {
tmp_tc.c_iflag |= IXOFF|IXON;
} else if (restartany > 0) {
tmp_tc.c_iflag |= IXOFF|IXON|IXANY;
} else {
tmp_tc.c_iflag |= IXOFF|IXON;
tmp_tc.c_iflag &= ~IXANY;
}
#endif
}
if ((f&MODE_TRAPSIG) == 0) {
#ifndef USE_TERMIO
tc.t_intrc = _POSIX_VDISABLE;
tc.t_quitc = _POSIX_VDISABLE;
tc.t_eofc = _POSIX_VDISABLE;
ltc.t_suspc = _POSIX_VDISABLE;
ltc.t_dsuspc = _POSIX_VDISABLE;
#else
tmp_tc.c_lflag &= ~ISIG;
#endif
localchars = 0;
} else {
#ifdef USE_TERMIO
tmp_tc.c_lflag |= ISIG;
#endif
localchars = 1;
}
if (f&MODE_EDIT) {
#ifndef USE_TERMIO
sb.sg_flags &= ~CBREAK;
sb.sg_flags |= CRMOD;
#else
tmp_tc.c_lflag |= ICANON;
#endif
} else {
#ifndef USE_TERMIO
sb.sg_flags |= CBREAK;
if (f&MODE_ECHO)
sb.sg_flags |= CRMOD;
else
sb.sg_flags &= ~CRMOD;
#else
tmp_tc.c_lflag &= ~ICANON;
tmp_tc.c_iflag &= ~ICRNL;
tmp_tc.c_cc[VMIN] = 1;
tmp_tc.c_cc[VTIME] = 0;
#endif
}
if ((f&(MODE_EDIT|MODE_TRAPSIG)) == 0) {
#ifndef USE_TERMIO
ltc.t_lnextc = _POSIX_VDISABLE;
#else
# ifdef VLNEXT
tmp_tc.c_cc[VLNEXT] = (cc_t)(_POSIX_VDISABLE);
# endif
#endif
}
if (f&MODE_SOFT_TAB) {
#ifndef USE_TERMIO
sb.sg_flags |= XTABS;
#else
# ifdef OXTABS
tmp_tc.c_oflag |= OXTABS;
# endif
# ifdef TABDLY
tmp_tc.c_oflag &= ~TABDLY;
tmp_tc.c_oflag |= TAB3;
# endif
#endif
} else {
#ifndef USE_TERMIO
sb.sg_flags &= ~XTABS;
#else
# ifdef OXTABS
tmp_tc.c_oflag &= ~OXTABS;
# endif
# ifdef TABDLY
tmp_tc.c_oflag &= ~TABDLY;
# endif
#endif
}
if (f&MODE_LIT_ECHO) {
#ifndef USE_TERMIO
lmode &= ~LCTLECH;
#else
# ifdef ECHOCTL
tmp_tc.c_lflag &= ~ECHOCTL;
# endif
#endif
} else {
#ifndef USE_TERMIO
lmode |= LCTLECH;
#else
# ifdef ECHOCTL
tmp_tc.c_lflag |= ECHOCTL;
# endif
#endif
}
if (f == -1) {
onoff = 0;
} else {
#ifndef USE_TERMIO
if (f & MODE_OUTBIN)
lmode |= LLITOUT;
else
lmode &= ~LLITOUT;
if (f & MODE_INBIN)
lmode |= LPASS8;
else
lmode &= ~LPASS8;
#else
if (f & MODE_INBIN)
tmp_tc.c_iflag &= ~ISTRIP;
else
tmp_tc.c_iflag |= ISTRIP;
if (f & MODE_OUTBIN) {
tmp_tc.c_cflag &= ~(CSIZE|PARENB);
tmp_tc.c_cflag |= CS8;
tmp_tc.c_oflag &= ~OPOST;
} else {
tmp_tc.c_cflag &= ~(CSIZE|PARENB);
tmp_tc.c_cflag |= old_tc.c_cflag & (CSIZE|PARENB);
tmp_tc.c_oflag |= OPOST;
}
#endif
onoff = 1;
}
if (f != -1) {
#ifdef SIGINT
(void) signal(SIGINT, intr);
#endif
#ifdef SIGQUIT
(void) signal(SIGQUIT, intr2);
#endif
#ifdef SIGTSTP
(void) signal(SIGTSTP, susp);
#endif
#ifdef SIGINFO
(void) signal(SIGINFO, ayt);
#endif
#if defined(USE_TERMIO) && defined(NOKERNINFO)
tmp_tc.c_lflag |= NOKERNINFO;
#endif
#ifndef USE_TERMIO
ltc.t_dsuspc = _POSIX_VDISABLE;
#else
# ifdef VDSUSP
tmp_tc.c_cc[VDSUSP] = (cc_t)(_POSIX_VDISABLE);
# endif
#endif
#ifdef USE_TERMIO
esc = (rlogin != _POSIX_VDISABLE) ? rlogin : escape;
if ((tmp_tc.c_cc[VEOL] != esc)
# ifdef VEOL2
&& (tmp_tc.c_cc[VEOL2] != esc)
# endif
) {
if (tmp_tc.c_cc[VEOL] == (cc_t)(_POSIX_VDISABLE))
tmp_tc.c_cc[VEOL] = esc;
# ifdef VEOL2
else if (tmp_tc.c_cc[VEOL2] == (cc_t)(_POSIX_VDISABLE))
tmp_tc.c_cc[VEOL2] = esc;
# endif
}
#else
if (tc.t_brkc == (cc_t)(_POSIX_VDISABLE))
tc.t_brkc = esc;
#endif
} else {
#ifdef SIGINFO
(void) signal(SIGINFO, (void (*)(int))ayt_status);
#endif
#ifdef SIGINT
(void) signal(SIGINT, SIG_DFL);
#endif
#ifdef SIGQUIT
(void) signal(SIGQUIT, SIG_DFL);
#endif
#ifdef SIGTSTP
(void) signal(SIGTSTP, SIG_DFL);
# ifndef SOLARIS
(void) sigsetmask(sigblock(0) & ~(1<<(SIGTSTP-1)));
# else SOLARIS
(void) sigrelse(SIGTSTP);
# endif SOLARIS
#endif
#ifndef USE_TERMIO
ltc = oltc;
tc = otc;
sb = ottyb;
lmode = olmode;
#else
tmp_tc = old_tc;
#endif
}
#ifndef USE_TERMIO
ioctl(tin, TIOCLSET, (char *)&lmode);
ioctl(tin, TIOCSLTC, (char *)<c);
ioctl(tin, TIOCSETC, (char *)&tc);
ioctl(tin, TIOCSETN, (char *)&sb);
#else
if (tcsetattr(tin, TCSADRAIN, &tmp_tc) < 0)
tcsetattr(tin, TCSANOW, &tmp_tc);
#endif
ioctl(tin, FIONBIO, (char *)&onoff);
ioctl(tout, FIONBIO, (char *)&onoff);
}
#if B4800 != 4800
#define DECODE_BAUD
#endif
#ifdef DECODE_BAUD
#ifndef B7200
#define B7200 B4800
#endif
#ifndef B14400
#define B14400 B9600
#endif
#ifndef B19200
# define B19200 B14400
#endif
#ifndef B28800
#define B28800 B19200
#endif
#ifndef B38400
# define B38400 B28800
#endif
#ifndef B57600
#define B57600 B38400
#endif
#ifndef B76800
#define B76800 B57600
#endif
#ifndef B115200
#define B115200 B76800
#endif
#ifndef B230400
#define B230400 B115200
#endif
struct termspeeds {
long speed;
long value;
} termspeeds[] = {
{ 0, B0 }, { 50, B50 }, { 75, B75 },
{ 110, B110 }, { 134, B134 }, { 150, B150 },
{ 200, B200 }, { 300, B300 }, { 600, B600 },
{ 1200, B1200 }, { 1800, B1800 }, { 2400, B2400 },
{ 4800, B4800 }, { 7200, B7200 }, { 9600, B9600 },
{ 14400, B14400 }, { 19200, B19200 }, { 28800, B28800 },
{ 38400, B38400 }, { 57600, B57600 }, { 115200, B115200 },
{ 230400, B230400 }, { -1, B230400 }
};
#endif
void
TerminalSpeeds(long *ispeed, long *ospeed)
{
#ifdef DECODE_BAUD
struct termspeeds *tp;
#endif
long in, out;
out = cfgetospeed(&old_tc);
in = cfgetispeed(&old_tc);
if (in == 0)
in = out;
#ifdef DECODE_BAUD
tp = termspeeds;
while ((tp->speed != -1) && (tp->value < in))
tp++;
*ispeed = tp->speed;
tp = termspeeds;
while ((tp->speed != -1) && (tp->value < out))
tp++;
*ospeed = tp->speed;
#else
*ispeed = in;
*ospeed = out;
#endif
}
int
TerminalWindowSize(long *rows, long *cols)
{
#ifdef TIOCGWINSZ
struct winsize ws;
if (ioctl(fileno(stdin), TIOCGWINSZ, (char *)&ws) >= 0) {
*rows = ws.ws_row;
*cols = ws.ws_col;
return 1;
}
#endif
return 0;
}
int
NetClose(int fd)
{
return close(fd);
}
static void
NetNonblockingIO(int fd, int onoff)
{
ioctl(fd, FIONBIO, (char *)&onoff);
}
static SIG_FUNC_RET
deadpeer(int sig __unused)
{
setcommandmode();
longjmp(peerdied, -1);
}
SIG_FUNC_RET
intr(int sig __unused)
{
if (localchars) {
intp();
return;
}
setcommandmode();
longjmp(toplevel, -1);
}
SIG_FUNC_RET
intr2(int sig __unused)
{
if (localchars) {
#ifdef KLUDGELINEMODE
if (kludgelinemode)
sendbrk();
else
#endif
sendabort();
return;
}
}
#ifdef SIGTSTP
SIG_FUNC_RET
susp(int sig __unused)
{
if ((rlogin != _POSIX_VDISABLE) && rlogin_susp())
return;
if (localchars)
sendsusp();
}
#endif
#ifdef SIGWINCH
static SIG_FUNC_RET
sendwin(int sig __unused)
{
if (connected) {
sendnaws();
}
}
#endif
#ifdef SIGINFO
SIG_FUNC_RET
ayt(int sig __unused)
{
if (connected)
sendayt();
else
ayt_status();
}
#endif
void
sys_telnet_init(void)
{
(void) signal(SIGINT, intr);
(void) signal(SIGQUIT, intr2);
(void) signal(SIGPIPE, deadpeer);
#ifdef SIGWINCH
(void) signal(SIGWINCH, sendwin);
#endif
#ifdef SIGTSTP
(void) signal(SIGTSTP, susp);
#endif
#ifdef SIGINFO
(void) signal(SIGINFO, ayt);
#endif
setconnmode(0);
NetNonblockingIO(net, 1);
#if defined(SO_OOBINLINE)
if (SetSockOpt(net, SOL_SOCKET, SO_OOBINLINE, 1) == -1) {
perror("SetSockOpt");
}
#endif
}
int
process_rings(int netin, int netout, int netex, int ttyin, int ttyout, int poll)
{
int c;
int returnValue = 0;
static struct timeval TimeValue = { 0, 0 };
int maxfd = -1;
int tmp;
if ((netout || netin || netex) && net > maxfd)
maxfd = net;
if (ttyout && tout > maxfd)
maxfd = tout;
if (ttyin && tin > maxfd)
maxfd = tin;
tmp = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
if (tmp > fdsn) {
if (ibitsp)
free(ibitsp);
if (obitsp)
free(obitsp);
if (xbitsp)
free(xbitsp);
fdsn = tmp;
if ((ibitsp = (fd_set *)malloc(fdsn)) == NULL)
err(1, "malloc");
if ((obitsp = (fd_set *)malloc(fdsn)) == NULL)
err(1, "malloc");
if ((xbitsp = (fd_set *)malloc(fdsn)) == NULL)
err(1, "malloc");
memset(ibitsp, 0, fdsn);
memset(obitsp, 0, fdsn);
memset(xbitsp, 0, fdsn);
}
if (netout)
FD_SET(net, obitsp);
if (ttyout)
FD_SET(tout, obitsp);
if (ttyin)
FD_SET(tin, ibitsp);
if (netin)
FD_SET(net, ibitsp);
if (netex)
FD_SET(net, xbitsp);
if ((c = select(maxfd + 1, ibitsp, obitsp, xbitsp,
(poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) {
if (c == -1) {
if (errno == EINTR) {
return 0;
}
printf("sleep(5) from telnet, after select: %s\r\n", strerror(errno));
sleep(5);
}
return 0;
}
if (FD_ISSET(net, xbitsp)) {
FD_CLR(net, xbitsp);
SYNCHing = 1;
(void) ttyflush(1);
}
if (FD_ISSET(net, ibitsp)) {
int canread;
FD_CLR(net, ibitsp);
canread = ring_empty_consecutive(&netiring);
#if !defined(SO_OOBINLINE)
if (SYNCHing) {
int atmark;
static int bogus_oob = 0, first = 1;
ioctl(net, SIOCATMARK, (char *)&atmark);
if (atmark) {
c = recv(net, netiring.supply, canread, MSG_OOB);
if ((c == -1) && (errno == EINVAL)) {
c = recv(net, netiring.supply, canread, 0);
if (clocks.didnetreceive < clocks.gotDM) {
SYNCHing = stilloob(net);
}
} else if (first && c > 0) {
int i;
i = recv(net, netiring.supply + c, canread - c, MSG_OOB);
if (i == c &&
memcmp(netiring.supply, netiring.supply + c, i) == 0) {
bogus_oob = 1;
first = 0;
} else if (i < 0) {
bogus_oob = 0;
first = 0;
} else
c += i;
}
if (bogus_oob && c > 0) {
int i;
i = read(net, netiring.supply + c, canread - c);
if (i > 0)
c += i;
}
} else {
c = recv(net, netiring.supply, canread, 0);
}
} else {
c = recv(net, netiring.supply, canread, 0);
}
settimer(didnetreceive);
#else
c = recv(net, (char *)netiring.supply, canread, 0);
#endif
if (c < 0 && errno == EWOULDBLOCK) {
c = 0;
} else if (c <= 0) {
return -1;
}
if (netdata) {
Dump('<', netiring.supply, c);
}
if (c)
ring_supplied(&netiring, c);
returnValue = 1;
}
if (FD_ISSET(tin, ibitsp)) {
FD_CLR(tin, ibitsp);
c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring));
if (c < 0 && errno == EIO)
c = 0;
if (c < 0 && errno == EWOULDBLOCK) {
c = 0;
} else {
if ((c == 0) && MODE_LOCAL_CHARS(globalmode) && isatty(tin)) {
*ttyiring.supply = termEofChar;
c = 1;
}
if (c <= 0) {
return -1;
}
if (termdata) {
Dump('<', ttyiring.supply, c);
}
ring_supplied(&ttyiring, c);
}
returnValue = 1;
}
if (FD_ISSET(net, obitsp)) {
FD_CLR(net, obitsp);
returnValue |= netflush();
}
if (FD_ISSET(tout, obitsp)) {
FD_CLR(tout, obitsp);
returnValue |= (ttyflush(SYNCHing|flushout) > 0);
}
return returnValue;
}