diff --git a/libexec/telnetd/sys_term.c b/libexec/telnetd/sys_term.c index 72a1ff399f3f..8bbe3fe315cb 100644 --- a/libexec/telnetd/sys_term.c +++ b/libexec/telnetd/sys_term.c @@ -1,2203 +1,2201 @@ /* * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: sys_term.c,v 1.14 1996/09/22 21:55:42 wosch Exp $ */ #ifndef lint static char sccsid[] = "@(#)sys_term.c 8.2 (Berkeley) 12/15/93"; #endif /* not lint */ #include "telnetd.h" #include "pathnames.h" #if defined(AUTHENTICATION) #include #endif extern char *altlogin; #if defined(CRAY) || defined(__hpux) # define PARENT_DOES_UTMP #endif int utmp_len = MAXHOSTNAMELEN; #ifdef NEWINIT #include #else /* NEWINIT*/ # ifdef UTMPX # include struct utmpx wtmp; # else # include struct utmp wtmp; # endif /* UTMPX */ # ifndef PARENT_DOES_UTMP #ifdef _PATH_WTMP char wtmpf[] = _PATH_WTMP; #else char wtmpf[] = "/usr/adm/wtmp"; #endif #ifdef _PATH_UTMP char utmpf[] = _PATH_UTMP; #else char utmpf[] = "/etc/utmp"; #endif # else /* PARENT_DOES_UTMP */ char wtmpf[] = "/etc/wtmp"; # endif /* PARENT_DOES_UTMP */ # ifdef CRAY #include #include # if defined(_SC_CRAY_SECURE_SYS) && !defined(SCM_SECURITY) /* * UNICOS 6.0/6.1 do not have SCM_SECURITY defined, so we can * use it to tell us to turn off all the socket security code, * since that is only used in UNICOS 7.0 and later. */ # undef _SC_CRAY_SECURE_SYS # endif # if defined(_SC_CRAY_SECURE_SYS) #include #include extern int secflag; extern struct sysv sysv; # endif /* _SC_CRAY_SECURE_SYS */ # endif /* CRAY */ #endif /* NEWINIT */ #ifdef STREAMSPTY #include #include #endif #define SCPYN(a, b) (void) strncpy(a, b, sizeof(a)) #define SCMPN(a, b) strncmp(a, b, sizeof(a)) #ifdef STREAMS #include #endif #ifdef __hpux #include #include #endif #include #ifdef t_erase #undef t_erase #undef t_kill #undef t_intrc #undef t_quitc #undef t_startc #undef t_stopc #undef t_eofc #undef t_brkc #undef t_suspc #undef t_dsuspc #undef t_rprntc #undef t_flushc #undef t_werasc #undef t_lnextc #endif #if defined(UNICOS5) && defined(CRAY2) && !defined(EXTPROC) # define EXTPROC 0400 #endif #ifndef USE_TERMIO struct termbuf { struct sgttyb sg; struct tchars tc; struct ltchars ltc; int state; int lflags; } termbuf, termbuf2; # define cfsetospeed(tp, val) (tp)->sg.sg_ospeed = (val) # define cfsetispeed(tp, val) (tp)->sg.sg_ispeed = (val) # define cfgetospeed(tp) (tp)->sg.sg_ospeed # define cfgetispeed(tp) (tp)->sg.sg_ispeed #else /* USE_TERMIO */ # ifdef SYSV_TERMIO # define termios termio # endif # 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, t) # define cfsetospeed(tp, val) (tp)->c_cflag &= ~CBAUD; \ (tp)->c_cflag |= (val) # define cfgetospeed(tp) ((tp)->c_cflag & CBAUD) # ifdef CIBAUD # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CIBAUD; \ (tp)->c_cflag |= ((val)<c_cflag & CIBAUD)>>IBSHIFT) # else # define cfsetispeed(tp, val) (tp)->c_cflag &= ~CBAUD; \ (tp)->c_cflag |= (val) # define cfgetispeed(tp) ((tp)->c_cflag & CBAUD) # endif # endif /* TCSANOW */ struct termios termbuf, termbuf2; /* pty control structure */ # ifdef STREAMSPTY int ttyfd = -1; # endif #endif /* USE_TERMIO */ /* * init_termbuf() * copy_termbuf(cp) * set_termbuf() * * These three routines are used to get and set the "termbuf" structure * to and from the kernel. init_termbuf() gets the current settings. * copy_termbuf() hands in a new "termbuf" to write to the kernel, and * set_termbuf() writes the structure into the kernel. */ void init_termbuf() { #ifndef USE_TERMIO (void) ioctl(pty, TIOCGETP, (char *)&termbuf.sg); (void) ioctl(pty, TIOCGETC, (char *)&termbuf.tc); (void) ioctl(pty, TIOCGLTC, (char *)&termbuf.ltc); # ifdef TIOCGSTATE (void) ioctl(pty, TIOCGSTATE, (char *)&termbuf.state); # endif #else # ifdef STREAMSPTY (void) tcgetattr(ttyfd, &termbuf); # else (void) tcgetattr(pty, &termbuf); # endif #endif termbuf2 = termbuf; } #if defined(LINEMODE) && defined(TIOCPKT_IOCTL) void copy_termbuf(cp, len) char *cp; int len; { if (len > sizeof(termbuf)) len = sizeof(termbuf); bcopy(cp, (char *)&termbuf, len); termbuf2 = termbuf; } #endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */ void set_termbuf() { /* * Only make the necessary changes. */ #ifndef USE_TERMIO if (bcmp((char *)&termbuf.sg, (char *)&termbuf2.sg, sizeof(termbuf.sg))) (void) ioctl(pty, TIOCSETN, (char *)&termbuf.sg); if (bcmp((char *)&termbuf.tc, (char *)&termbuf2.tc, sizeof(termbuf.tc))) (void) ioctl(pty, TIOCSETC, (char *)&termbuf.tc); if (bcmp((char *)&termbuf.ltc, (char *)&termbuf2.ltc, sizeof(termbuf.ltc))) (void) ioctl(pty, TIOCSLTC, (char *)&termbuf.ltc); if (termbuf.lflags != termbuf2.lflags) (void) ioctl(pty, TIOCLSET, (char *)&termbuf.lflags); #else /* USE_TERMIO */ if (bcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf))) # ifdef STREAMSPTY (void) tcsetattr(ttyfd, TCSANOW, &termbuf); # else (void) tcsetattr(pty, TCSANOW, &termbuf); # endif # if defined(CRAY2) && defined(UNICOS5) needtermstat = 1; # endif #endif /* USE_TERMIO */ } /* * spcset(func, valp, valpp) * * This function takes various special characters (func), and * sets *valp to the current value of that character, and * *valpp to point to where in the "termbuf" structure that * value is kept. * * It returns the SLC_ level of support for this function. */ #ifndef USE_TERMIO int spcset(func, valp, valpp) int func; cc_t *valp; cc_t **valpp; { switch(func) { case SLC_EOF: *valp = termbuf.tc.t_eofc; *valpp = (cc_t *)&termbuf.tc.t_eofc; return(SLC_VARIABLE); case SLC_EC: *valp = termbuf.sg.sg_erase; *valpp = (cc_t *)&termbuf.sg.sg_erase; return(SLC_VARIABLE); case SLC_EL: *valp = termbuf.sg.sg_kill; *valpp = (cc_t *)&termbuf.sg.sg_kill; return(SLC_VARIABLE); case SLC_IP: *valp = termbuf.tc.t_intrc; *valpp = (cc_t *)&termbuf.tc.t_intrc; return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT); case SLC_ABORT: *valp = termbuf.tc.t_quitc; *valpp = (cc_t *)&termbuf.tc.t_quitc; return(SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT); case SLC_XON: *valp = termbuf.tc.t_startc; *valpp = (cc_t *)&termbuf.tc.t_startc; return(SLC_VARIABLE); case SLC_XOFF: *valp = termbuf.tc.t_stopc; *valpp = (cc_t *)&termbuf.tc.t_stopc; return(SLC_VARIABLE); case SLC_AO: *valp = termbuf.ltc.t_flushc; *valpp = (cc_t *)&termbuf.ltc.t_flushc; return(SLC_VARIABLE); case SLC_SUSP: *valp = termbuf.ltc.t_suspc; *valpp = (cc_t *)&termbuf.ltc.t_suspc; return(SLC_VARIABLE); case SLC_EW: *valp = termbuf.ltc.t_werasc; *valpp = (cc_t *)&termbuf.ltc.t_werasc; return(SLC_VARIABLE); case SLC_RP: *valp = termbuf.ltc.t_rprntc; *valpp = (cc_t *)&termbuf.ltc.t_rprntc; return(SLC_VARIABLE); case SLC_LNEXT: *valp = termbuf.ltc.t_lnextc; *valpp = (cc_t *)&termbuf.ltc.t_lnextc; return(SLC_VARIABLE); case SLC_FORW1: *valp = termbuf.tc.t_brkc; *valpp = (cc_t *)&termbuf.ltc.t_lnextc; return(SLC_VARIABLE); case SLC_BRK: case SLC_SYNCH: case SLC_AYT: case SLC_EOR: *valp = (cc_t)0; *valpp = (cc_t *)0; return(SLC_DEFAULT); default: *valp = (cc_t)0; *valpp = (cc_t *)0; return(SLC_NOSUPPORT); } } #else /* USE_TERMIO */ int spcset(func, valp, valpp) int func; cc_t *valp; cc_t **valpp; { #define setval(a, b) *valp = termbuf.c_cc[a]; \ *valpp = &termbuf.c_cc[a]; \ return(b); #define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT); switch(func) { case SLC_EOF: setval(VEOF, SLC_VARIABLE); case SLC_EC: setval(VERASE, SLC_VARIABLE); case SLC_EL: setval(VKILL, SLC_VARIABLE); case SLC_IP: setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT); case SLC_ABORT: setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT); case SLC_XON: #ifdef VSTART setval(VSTART, SLC_VARIABLE); #else defval(0x13); #endif case SLC_XOFF: #ifdef VSTOP setval(VSTOP, SLC_VARIABLE); #else defval(0x11); #endif case SLC_EW: #ifdef VWERASE setval(VWERASE, SLC_VARIABLE); #else defval(0); #endif case SLC_RP: #ifdef VREPRINT setval(VREPRINT, SLC_VARIABLE); #else defval(0); #endif case SLC_LNEXT: #ifdef VLNEXT setval(VLNEXT, SLC_VARIABLE); #else defval(0); #endif case SLC_AO: #if !defined(VDISCARD) && defined(VFLUSHO) # define VDISCARD VFLUSHO #endif #ifdef VDISCARD setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT); #else defval(0); #endif case SLC_SUSP: #ifdef VSUSP setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN); #else defval(0); #endif #ifdef VEOL case SLC_FORW1: setval(VEOL, SLC_VARIABLE); #endif #ifdef VEOL2 case SLC_FORW2: setval(VEOL2, SLC_VARIABLE); #endif case SLC_AYT: #ifdef VSTATUS setval(VSTATUS, SLC_VARIABLE); #else defval(0); #endif case SLC_BRK: case SLC_SYNCH: case SLC_EOR: defval(0); default: *valp = 0; *valpp = 0; return(SLC_NOSUPPORT); } } #endif /* USE_TERMIO */ #ifdef CRAY /* * getnpty() * * Return the number of pty's configured into the system. */ int getnpty() { #ifdef _SC_CRAY_NPTY int numptys; if ((numptys = sysconf(_SC_CRAY_NPTY)) != -1) return numptys; else #endif /* _SC_CRAY_NPTY */ return 128; } #endif /* CRAY */ #ifndef convex /* * getpty() * * Allocate a pty. As a side effect, the external character * array "line" contains the name of the slave side. * * Returns the file descriptor of the opened pty. */ #ifndef __GNUC__ char *line = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; #else static char Xline[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; char *line = Xline; #endif #ifdef CRAY char *myline = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; #endif /* CRAY */ int getpty(ptynum) int *ptynum; { register int p; #ifdef STREAMSPTY int t; char *ptsname(); p = open("/dev/ptmx", 2); if (p > 0) { grantpt(p); unlockpt(p); strcpy(line, ptsname(p)); return(p); } #else /* ! STREAMSPTY */ #ifndef CRAY register char *cp, *p1, *p2; register int i; #if defined(sun) && defined(TIOCGPGRP) && BSD < 199207 int dummy; #endif #ifndef __hpux (void) sprintf(line, "/dev/ptyXX"); p1 = &line[8]; p2 = &line[9]; #else (void) sprintf(line, "/dev/ptym/ptyXX"); p1 = &line[13]; p2 = &line[14]; #endif for (cp = "pqrsPQRS"; *cp; cp++) { struct stat stb; *p1 = *cp; *p2 = '0'; /* * This stat() check is just to keep us from * looping through all 256 combinations if there * aren't that many ptys available. */ if (stat(line, &stb) < 0) break; for (i = 0; i < 32; i++) { *p2 = "0123456789abcdefghijklmnopqrstuv"[i]; p = open(line, 2); if (p > 0) { #ifndef __hpux line[5] = 't'; #else for (p1 = &line[8]; *p1; p1++) *p1 = *(p1+1); line[9] = 't'; #endif chown(line, 0, 0); chmod(line, 0600); #if defined(sun) && defined(TIOCGPGRP) && BSD < 199207 if (ioctl(p, TIOCGPGRP, &dummy) == 0 || errno != EIO) { chmod(line, 0666); close(p); line[5] = 'p'; } else #endif /* defined(sun) && defined(TIOCGPGRP) && BSD < 199207 */ return(p); } } } #else /* CRAY */ extern lowpty, highpty; struct stat sb; for (*ptynum = lowpty; *ptynum <= highpty; (*ptynum)++) { (void) sprintf(myline, "/dev/pty/%03d", *ptynum); p = open(myline, 2); if (p < 0) continue; (void) sprintf(line, "/dev/ttyp%03d", *ptynum); /* * Here are some shenanigans to make sure that there * are no listeners lurking on the line. */ if(stat(line, &sb) < 0) { (void) close(p); continue; } if(sb.st_uid || sb.st_gid || sb.st_mode != 0600) { chown(line, 0, 0); chmod(line, 0600); (void)close(p); p = open(myline, 2); if (p < 0) continue; } /* * Now it should be safe...check for accessability. */ if (access(line, 6) == 0) return(p); else { /* no tty side to pty so skip it */ (void) close(p); } } #endif /* CRAY */ #endif /* STREAMSPTY */ return(-1); } #endif /* convex */ #ifdef LINEMODE /* * tty_flowmode() Find out if flow control is enabled or disabled. * tty_linemode() Find out if linemode (external processing) is enabled. * tty_setlinemod(on) Turn on/off linemode. * tty_isecho() Find out if echoing is turned on. * tty_setecho(on) Enable/disable character echoing. * tty_israw() Find out if terminal is in RAW mode. * tty_binaryin(on) Turn on/off BINARY on input. * tty_binaryout(on) Turn on/off BINARY on output. * tty_isediting() Find out if line editing is enabled. * tty_istrapsig() Find out if signal trapping is enabled. * tty_setedit(on) Turn on/off line editing. * tty_setsig(on) Turn on/off signal trapping. * tty_issofttab() Find out if tab expansion is enabled. * tty_setsofttab(on) Turn on/off soft tab expansion. * tty_islitecho() Find out if typed control chars are echoed literally * tty_setlitecho() Turn on/off literal echo of control chars * tty_tspeed(val) Set transmit speed to val. * tty_rspeed(val) Set receive speed to val. */ #ifdef convex static int linestate; #endif int tty_linemode() { #ifndef convex #ifndef USE_TERMIO return(termbuf.state & TS_EXTPROC); #else return(termbuf.c_lflag & EXTPROC); #endif #else return(linestate); #endif } void tty_setlinemode(on) int on; { #ifdef TIOCEXT # ifndef convex set_termbuf(); # else linestate = on; # endif (void) ioctl(pty, TIOCEXT, (char *)&on); # ifndef convex init_termbuf(); # endif #else /* !TIOCEXT */ # ifdef EXTPROC if (on) termbuf.c_lflag |= EXTPROC; else termbuf.c_lflag &= ~EXTPROC; # endif #endif /* TIOCEXT */ } #endif /* LINEMODE */ int tty_isecho() { #ifndef USE_TERMIO return (termbuf.sg.sg_flags & ECHO); #else return (termbuf.c_lflag & ECHO); #endif } int tty_flowmode() { #ifndef USE_TERMIO return(((termbuf.tc.t_startc) > 0 && (termbuf.tc.t_stopc) > 0) ? 1 : 0); #else return((termbuf.c_iflag & IXON) ? 1 : 0); #endif } int tty_restartany() { #ifndef USE_TERMIO # ifdef DECCTQ return((termbuf.lflags & DECCTQ) ? 0 : 1); # else return(-1); # endif #else return((termbuf.c_iflag & IXANY) ? 1 : 0); #endif } void tty_setecho(on) int on; { #ifndef USE_TERMIO if (on) termbuf.sg.sg_flags |= ECHO|CRMOD; else termbuf.sg.sg_flags &= ~(ECHO|CRMOD); #else if (on) termbuf.c_lflag |= ECHO; else termbuf.c_lflag &= ~ECHO; #endif } int tty_israw() { #ifndef USE_TERMIO return(termbuf.sg.sg_flags & RAW); #else return(!(termbuf.c_lflag & ICANON)); #endif } #if defined (AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R) int tty_setraw(on) { # ifndef USE_TERMIO if (on) termbuf.sg.sg_flags |= RAW; else termbuf.sg.sg_flags &= ~RAW; # else if (on) termbuf.c_lflag &= ~ICANON; else termbuf.c_lflag |= ICANON; # endif } #endif void tty_binaryin(on) int on; { #ifndef USE_TERMIO if (on) termbuf.lflags |= LPASS8; else termbuf.lflags &= ~LPASS8; #else if (on) { termbuf.c_iflag &= ~ISTRIP; } else { termbuf.c_iflag |= ISTRIP; } #endif } void tty_binaryout(on) int on; { #ifndef USE_TERMIO if (on) termbuf.lflags |= LLITOUT; else termbuf.lflags &= ~LLITOUT; #else if (on) { termbuf.c_cflag &= ~(CSIZE|PARENB); termbuf.c_cflag |= CS8; termbuf.c_oflag &= ~OPOST; } else { termbuf.c_cflag &= ~CSIZE; termbuf.c_cflag |= CS7|PARENB; termbuf.c_oflag |= OPOST; } #endif } int tty_isbinaryin() { #ifndef USE_TERMIO return(termbuf.lflags & LPASS8); #else return(!(termbuf.c_iflag & ISTRIP)); #endif } int tty_isbinaryout() { #ifndef USE_TERMIO return(termbuf.lflags & LLITOUT); #else return(!(termbuf.c_oflag&OPOST)); #endif } #ifdef LINEMODE int tty_isediting() { #ifndef USE_TERMIO return(!(termbuf.sg.sg_flags & (CBREAK|RAW))); #else return(termbuf.c_lflag & ICANON); #endif } int tty_istrapsig() { #ifndef USE_TERMIO return(!(termbuf.sg.sg_flags&RAW)); #else return(termbuf.c_lflag & ISIG); #endif } void tty_setedit(on) int on; { #ifndef USE_TERMIO if (on) termbuf.sg.sg_flags &= ~CBREAK; else termbuf.sg.sg_flags |= CBREAK; #else if (on) termbuf.c_lflag |= ICANON; else termbuf.c_lflag &= ~ICANON; #endif } void tty_setsig(on) int on; { #ifndef USE_TERMIO if (on) ; #else if (on) termbuf.c_lflag |= ISIG; else termbuf.c_lflag &= ~ISIG; #endif } #endif /* LINEMODE */ int tty_issofttab() { #ifndef USE_TERMIO return (termbuf.sg.sg_flags & XTABS); #else # ifdef OXTABS return (termbuf.c_oflag & OXTABS); # endif # ifdef TABDLY return ((termbuf.c_oflag & TABDLY) == TAB3); # endif #endif } void tty_setsofttab(on) int on; { #ifndef USE_TERMIO if (on) termbuf.sg.sg_flags |= XTABS; else termbuf.sg.sg_flags &= ~XTABS; #else if (on) { # ifdef OXTABS termbuf.c_oflag |= OXTABS; # endif # ifdef TABDLY termbuf.c_oflag &= ~TABDLY; termbuf.c_oflag |= TAB3; # endif } else { # ifdef OXTABS termbuf.c_oflag &= ~OXTABS; # endif # ifdef TABDLY termbuf.c_oflag &= ~TABDLY; termbuf.c_oflag |= TAB0; # endif } #endif } int tty_islitecho() { #ifndef USE_TERMIO return (!(termbuf.lflags & LCTLECH)); #else # ifdef ECHOCTL return (!(termbuf.c_lflag & ECHOCTL)); # endif # ifdef TCTLECH return (!(termbuf.c_lflag & TCTLECH)); # endif # if !defined(ECHOCTL) && !defined(TCTLECH) return (0); /* assumes ctl chars are echoed '^x' */ # endif #endif } void tty_setlitecho(on) int on; { #ifndef USE_TERMIO if (on) termbuf.lflags &= ~LCTLECH; else termbuf.lflags |= LCTLECH; #else # ifdef ECHOCTL if (on) termbuf.c_lflag &= ~ECHOCTL; else termbuf.c_lflag |= ECHOCTL; # endif # ifdef TCTLECH if (on) termbuf.c_lflag &= ~TCTLECH; else termbuf.c_lflag |= TCTLECH; # endif #endif } int tty_iscrnl() { #ifndef USE_TERMIO return (termbuf.sg.sg_flags & CRMOD); #else return (termbuf.c_iflag & ICRNL); #endif } /* * Try to guess whether speeds are "encoded" (4.2BSD) or just numeric (4.4BSD). */ #if B4800 != 4800 #define DECODE_BAUD #endif #ifdef DECODE_BAUD /* * A table of available terminal speeds */ struct termspeeds { int speed; int 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 }, { 9600, B9600 }, { 19200, B9600 }, { 38400, B9600 }, { -1, B9600 } }; #endif /* DECODE_BAUD */ void tty_tspeed(val) int val; { #ifdef DECODE_BAUD register struct termspeeds *tp; for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++) ; cfsetospeed(&termbuf, tp->value); #else /* DECODE_BAUD */ cfsetospeed(&termbuf, val); #endif /* DECODE_BAUD */ } void tty_rspeed(val) int val; { #ifdef DECODE_BAUD register struct termspeeds *tp; for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++) ; cfsetispeed(&termbuf, tp->value); #else /* DECODE_BAUD */ cfsetispeed(&termbuf, val); #endif /* DECODE_BAUD */ } #if defined(CRAY2) && defined(UNICOS5) int tty_isnewmap() { return((termbuf.c_oflag & OPOST) && (termbuf.c_oflag & ONLCR) && !(termbuf.c_oflag & ONLRET)); } #endif #ifdef PARENT_DOES_UTMP # ifndef NEWINIT extern struct utmp wtmp; extern char wtmpf[]; # else /* NEWINIT */ int gotalarm; /* ARGSUSED */ void nologinproc(sig) int sig; { gotalarm++; } # endif /* NEWINIT */ #endif /* PARENT_DOES_UTMP */ #ifndef NEWINIT # ifdef PARENT_DOES_UTMP extern void utmp_sig_init P((void)); extern void utmp_sig_reset P((void)); extern void utmp_sig_wait P((void)); extern void utmp_sig_notify P((int)); # endif /* PARENT_DOES_UTMP */ #endif /* * getptyslave() * * Open the slave side of the pty, and do any initialization * that is necessary. The return value is a file descriptor * for the slave side. */ int getptyslave() { register int t = -1; char erase; #if !defined(CRAY) || !defined(NEWINIT) # ifdef LINEMODE int waslm; # endif # ifdef TIOCGWINSZ struct winsize ws; extern int def_row, def_col; # endif extern int def_tspeed, def_rspeed; /* * Opening the slave side may cause initilization of the * kernel tty structure. We need remember the state of * if linemode was turned on * terminal window size * terminal speed * erase character * so that we can re-set them if we need to. */ # ifdef LINEMODE waslm = tty_linemode(); # endif erase = termbuf.c_cc[VERASE]; /* * Make sure that we don't have a controlling tty, and * that we are the session (process group) leader. */ # ifdef TIOCNOTTY t = open(_PATH_TTY, O_RDWR); if (t >= 0) { (void) ioctl(t, TIOCNOTTY, (char *)0); (void) close(t); } # endif # ifdef PARENT_DOES_UTMP /* * Wait for our parent to get the utmp stuff to get done. */ utmp_sig_wait(); # endif t = cleanopen(line); if (t < 0) fatalperror(net, line); #ifdef STREAMSPTY #ifdef USE_TERMIO ttyfd = t; #endif if (ioctl(t, I_PUSH, "ptem") < 0) fatal(net, "I_PUSH ptem"); if (ioctl(t, I_PUSH, "ldterm") < 0) fatal(net, "I_PUSH ldterm"); if (ioctl(t, I_PUSH, "ttcompat") < 0) fatal(net, "I_PUSH ttcompat"); if (ioctl(pty, I_PUSH, "pckt") < 0) fatal(net, "I_PUSH pckt"); #endif /* * set up the tty modes as we like them to be. */ init_termbuf(); # ifdef TIOCGWINSZ if (def_row || def_col) { bzero((char *)&ws, sizeof(ws)); ws.ws_col = def_col; ws.ws_row = def_row; (void)ioctl(t, TIOCSWINSZ, (char *)&ws); } # endif /* * Settings for sgtty based systems */ # ifndef USE_TERMIO termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS; # endif /* USE_TERMIO */ /* * Settings for UNICOS (and HPUX) */ # if defined(CRAY) || defined(__hpux) termbuf.c_oflag = OPOST|ONLCR|TAB3; termbuf.c_iflag = IGNPAR|ISTRIP|ICRNL|IXON; termbuf.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK; termbuf.c_cflag = EXTB|HUPCL|CS8; # endif /* * Settings for all other termios/termio based * systems, other than 4.4BSD. In 4.4BSD the * kernel does the initial terminal setup. */ # if defined(USE_TERMIO) && !(defined(CRAY) || defined(__hpux)) && (BSD <= 43) # ifndef OXTABS # define OXTABS 0 # endif termbuf.c_lflag |= ECHO; termbuf.c_oflag |= ONLCR|OXTABS; termbuf.c_iflag |= ICRNL; termbuf.c_iflag &= ~IXOFF; # endif /* defined(USE_TERMIO) && !defined(CRAY) && (BSD <= 43) */ tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600); tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600); if (erase) termbuf.c_cc[VERASE] = erase; # ifdef LINEMODE if (waslm) tty_setlinemode(1); # endif /* LINEMODE */ /* * Set the tty modes, and make this our controlling tty. */ set_termbuf(); if (login_tty(t) == -1) fatalperror(net, "login_tty"); #endif /* !defined(CRAY) || !defined(NEWINIT) */ if (net > 2) (void) close(net); #if defined(AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R) /* * Leave the pty open so that we can write out the rlogin * protocol for /bin/login, if the authentication works. */ #else if (pty > 2) { (void) close(pty); pty = -1; } #endif } #if !defined(CRAY) || !defined(NEWINIT) #ifndef O_NOCTTY #define O_NOCTTY 0 #endif /* * Open the specified slave side of the pty, * making sure that we have a clean tty. */ int cleanopen(line) char *line; { register int t; #if defined(_SC_CRAY_SECURE_SYS) struct secstat secbuf; #endif /* _SC_CRAY_SECURE_SYS */ #ifndef STREAMSPTY /* * Make sure that other people can't open the * slave side of the connection. */ (void) chown(line, 0, 0); (void) chmod(line, 0600); #endif # if !defined(CRAY) && (BSD > 43) (void) revoke(line); # endif #if defined(_SC_CRAY_SECURE_SYS) if (secflag) { if (secstat(line, &secbuf) < 0) return(-1); if (setulvl(secbuf.st_slevel) < 0) return(-1); if (setucmp(secbuf.st_compart) < 0) return(-1); } #endif /* _SC_CRAY_SECURE_SYS */ t = open(line, O_RDWR|O_NOCTTY); #if defined(_SC_CRAY_SECURE_SYS) if (secflag) { if (setulvl(sysv.sy_minlvl) < 0) return(-1); if (setucmp(0) < 0) return(-1); } #endif /* _SC_CRAY_SECURE_SYS */ if (t < 0) return(-1); /* * Hangup anybody else using this ttyp, then reopen it for * ourselves. */ # if !(defined(CRAY) || defined(__hpux)) && (BSD <= 43) && !defined(STREAMSPTY) (void) signal(SIGHUP, SIG_IGN); vhangup(); (void) signal(SIGHUP, SIG_DFL); t = open(line, O_RDWR|O_NOCTTY); if (t < 0) return(-1); # endif # if defined(CRAY) && defined(TCVHUP) { register int i; (void) signal(SIGHUP, SIG_IGN); (void) ioctl(t, TCVHUP, (char *)0); (void) signal(SIGHUP, SIG_DFL); setpgrp(); #if defined(_SC_CRAY_SECURE_SYS) if (secflag) { if (secstat(line, &secbuf) < 0) return(-1); if (setulvl(secbuf.st_slevel) < 0) return(-1); if (setucmp(secbuf.st_compart) < 0) return(-1); } #endif /* _SC_CRAY_SECURE_SYS */ i = open(line, O_RDWR); #if defined(_SC_CRAY_SECURE_SYS) if (secflag) { if (setulvl(sysv.sy_minlvl) < 0) return(-1); if (setucmp(0) < 0) return(-1); } #endif /* _SC_CRAY_SECURE_SYS */ if (i < 0) return(-1); (void) close(t); t = i; } # endif /* defined(CRAY) && defined(TCVHUP) */ return(t); } #endif /* !defined(CRAY) || !defined(NEWINIT) */ #if BSD <= 43 int login_tty(t) int t; { if (setsid() < 0) { #ifdef ultrix /* * The setsid() may have failed because we * already have a pgrp == pid. Zero out * our pgrp and try again... */ if ((setpgrp(0, 0) < 0) || (setsid() < 0)) #endif fatalperror(net, "setsid()"); } # ifdef TIOCSCTTY if (ioctl(t, TIOCSCTTY, (char *)0) < 0) fatalperror(net, "ioctl(sctty)"); # if defined(CRAY) /* * Close the hard fd to /dev/ttypXXX, and re-open through * the indirect /dev/tty interface. */ close(t); if ((t = open("/dev/tty", O_RDWR)) < 0) fatalperror(net, "open(/dev/tty)"); # endif # else /* * We get our controlling tty assigned as a side-effect * of opening up a tty device. But on BSD based systems, * this only happens if our process group is zero. The * setsid() call above may have set our pgrp, so clear * it out before opening the tty... */ (void) setpgrp(0, 0); close(open(line, O_RDWR)); # endif if (t != 0) (void) dup2(t, 0); if (t != 1) (void) dup2(t, 1); if (t != 2) (void) dup2(t, 2); if (t > 2) close(t); return(0); } #endif /* BSD <= 43 */ #ifdef NEWINIT char *gen_id = "fe"; #endif /* * startslave(host) * * Given a hostname, do whatever * is necessary to startup the login process on the slave side of the pty. */ /* ARGSUSED */ void startslave(host, autologin, autoname) char *host; int autologin; char *autoname; { register int i; long time(); char name[256]; #ifdef NEWINIT extern char *ptyip; struct init_request request; void nologinproc(); register int n; #endif /* NEWINIT */ #if defined(AUTHENTICATION) if (!autoname || !autoname[0]) autologin = 0; if (autologin < auth_level) { fatal(net, "Authorization failed"); exit(1); } #endif #ifndef NEWINIT # ifdef PARENT_DOES_UTMP utmp_sig_init(); # endif /* PARENT_DOES_UTMP */ if ((i = fork()) < 0) fatalperror(net, "fork"); if (i) { # ifdef PARENT_DOES_UTMP /* * Cray parent will create utmp entry for child and send * signal to child to tell when done. Child waits for signal * before doing anything important. */ register int pid = i; void sigjob P((int)); setpgrp(); utmp_sig_reset(); /* reset handler to default */ /* * Create utmp entry for child */ (void) time(&wtmp.ut_time); wtmp.ut_type = LOGIN_PROCESS; wtmp.ut_pid = pid; SCPYN(wtmp.ut_user, "LOGIN"); SCPYN(wtmp.ut_host, host); SCPYN(wtmp.ut_line, line + sizeof("/dev/") - 1); #ifndef __hpux SCPYN(wtmp.ut_id, wtmp.ut_line+3); #else SCPYN(wtmp.ut_id, wtmp.ut_line+7); #endif pututline(&wtmp); endutent(); if ((i = open(wtmpf, O_WRONLY|O_APPEND)) >= 0) { (void) write(i, (char *)&wtmp, sizeof(struct utmp)); (void) close(i); } #ifdef CRAY (void) signal(WJSIGNAL, sigjob); #endif utmp_sig_notify(pid); # endif /* PARENT_DOES_UTMP */ } else { getptyslave(autologin); start_login(host, autologin, autoname); /*NOTREACHED*/ } #else /* NEWINIT */ /* * Init will start up login process if we ask nicely. We only wait * for it to start up and begin normal telnet operation. */ if ((i = open(INIT_FIFO, O_WRONLY)) < 0) { char tbuf[128]; (void) sprintf(tbuf, "Can't open %s\n", INIT_FIFO); fatalperror(net, tbuf); } memset((char *)&request, 0, sizeof(request)); request.magic = INIT_MAGIC; SCPYN(request.gen_id, gen_id); SCPYN(request.tty_id, &line[8]); SCPYN(request.host, host); SCPYN(request.term_type, terminaltype ? terminaltype : "network"); #if !defined(UNICOS5) request.signal = SIGCLD; request.pid = getpid(); #endif #ifdef BFTPDAEMON /* * Are we working as the bftp daemon? */ if (bftpd) { SCPYN(request.exec_name, BFTPPATH); } #endif /* BFTPDAEMON */ if (write(i, (char *)&request, sizeof(request)) < 0) { char tbuf[128]; (void) sprintf(tbuf, "Can't write to %s\n", INIT_FIFO); fatalperror(net, tbuf); } (void) close(i); (void) signal(SIGALRM, nologinproc); for (i = 0; ; i++) { char tbuf[128]; alarm(15); n = read(pty, ptyip, BUFSIZ); if (i == 3 || n >= 0 || !gotalarm) break; gotalarm = 0; sprintf(tbuf, "telnetd: waiting for /etc/init to start login process on %s\r\n", line); (void) write(net, tbuf, strlen(tbuf)); } if (n < 0 && gotalarm) fatal(net, "/etc/init didn't start login process"); pcc += n; alarm(0); (void) signal(SIGALRM, SIG_DFL); return; #endif /* NEWINIT */ } char *envinit[3]; extern char **environ; void init_env() { extern char *getenv(); char **envp; envp = envinit; if (*envp = getenv("TZ")) *envp++ -= 3; #if defined(CRAY) || defined(__hpux) else *envp++ = "TZ=GMT0"; #endif *envp = 0; environ = envinit; } #ifndef NEWINIT /* * start_login(host) * * Assuming that we are now running as a child processes, this * function will turn us into the login process. */ void start_login(host, autologin, name) char *host; int autologin; char *name; { register char *cp; register char **argv; char **addarg(), *user; extern char *getenv(); #ifdef UTMPX register int pid = getpid(); struct utmpx utmpx; #endif #ifdef SOLARIS char *term; char termbuf[64]; #endif #ifdef UTMPX /* * Create utmp entry for child */ bzero(&utmpx, sizeof(utmpx)); SCPYN(utmpx.ut_user, ".telnet"); SCPYN(utmpx.ut_line, line + sizeof("/dev/") - 1); utmpx.ut_pid = pid; utmpx.ut_id[0] = 't'; utmpx.ut_id[1] = 'n'; utmpx.ut_id[2] = SC_WILDC; utmpx.ut_id[3] = SC_WILDC; utmpx.ut_type = LOGIN_PROCESS; (void) time(&utmpx.ut_tv.tv_sec); if (makeutx(&utmpx) == NULL) fatal(net, "makeutx failed"); #endif scrub_env(); /* * -h : pass on name of host. * WARNING: -h is accepted by login if and only if * getuid() == 0. * -p : don't clobber the environment (so terminal type stays set). * * -f : force this login, he has already been authenticated */ argv = addarg(0, "login"); #if !defined(NO_LOGIN_H) # if defined (AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R) /* * Don't add the "-h host" option if we are going * to be adding the "-r host" option down below... */ if ((auth_level < 0) || (autologin != AUTH_VALID)) # endif { argv = addarg(argv, "-h"); argv = addarg(argv, host); #ifdef SOLARIS /* * SVR4 version of -h takes TERM= as second arg, or - */ term = getenv("TERM"); if (term == NULL || term[0] == 0) { term = "-"; } else { strcpy(termbuf, "TERM="); strncat(termbuf, term, sizeof(termbuf) - 6); term = termbuf; } argv = addarg(argv, term); #endif } #endif #if !defined(NO_LOGIN_P) argv = addarg(argv, "-p"); #endif #ifdef BFTPDAEMON /* * Are we working as the bftp daemon? If so, then ask login * to start bftp instead of shell. */ if (bftpd) { argv = addarg(argv, "-e"); argv = addarg(argv, BFTPPATH); } else #endif #if defined (SecurID) /* * don't worry about the -f that might get sent. * A -s is supposed to override it anyhow. */ if (require_SecurID) argv = addarg(argv, "-s"); #endif #if defined (AUTHENTICATION) if (auth_level >= 0 && autologin == AUTH_VALID) { # if !defined(NO_LOGIN_F) argv = addarg(argv, "-f"); + argv = addarg(argv, "--"); argv = addarg(argv, name); # else # if defined(LOGIN_R) /* * We don't have support for "login -f", but we * can fool /bin/login into thinking that we are * rlogind, and allow us to log in without a * password. The rlogin protocol expects * local-user\0remote-user\0term/speed\0 */ if (pty > 2) { register char *cp; char speed[128]; int isecho, israw, xpty, len; extern int def_rspeed; # ifndef LOGIN_HOST /* * Tell login that we are coming from "localhost". * If we passed in the real host name, then the * user would have to allow .rhost access from * every machine that they want authenticated * access to work from, which sort of defeats * the purpose of an authenticated login... * So, we tell login that the session is coming * from "localhost", and the user will only have * to have "localhost" in their .rhost file. */ # define LOGIN_HOST "localhost" # endif argv = addarg(argv, "-r"); argv = addarg(argv, LOGIN_HOST); xpty = pty; # ifndef STREAMSPTY pty = 0; # else ttyfd = 0; # endif init_termbuf(); isecho = tty_isecho(); israw = tty_israw(); if (isecho || !israw) { tty_setecho(0); /* Turn off echo */ tty_setraw(1); /* Turn on raw */ set_termbuf(); } len = strlen(name)+1; write(xpty, name, len); write(xpty, name, len); sprintf(speed, "%s/%d", (cp = getenv("TERM")) ? cp : "", (def_rspeed > 0) ? def_rspeed : 9600); len = strlen(speed)+1; write(xpty, speed, len); if (isecho || !israw) { init_termbuf(); tty_setecho(isecho); tty_setraw(israw); set_termbuf(); if (!israw) { /* * Write a newline to ensure * that login will be able to * read the line... */ write(xpty, "\n", 1); } } pty = xpty; } # else + argv = addarg(argv, "--"); argv = addarg(argv, name); # endif # endif } else #endif - if (user = getenv("USER")) { - if (strchr(user, '-')) { - syslog(LOG_ERR, "tried to pass user \"%s\" to login", - user); - fatal(net, "invalid user"); - } + if (getenv("USER")) { + argv = addarg(argv, "--"); argv = addarg(argv, getenv("USER")); #if defined(LOGIN_ARGS) && defined(NO_LOGIN_P) { register char **cpp; for (cpp = environ; *cpp; cpp++) argv = addarg(argv, *cpp); } #endif /* * Assume that login will set the USER variable * correctly. For SysV systems, this means that * USER will no longer be set, just LOGNAME by * login. (The problem is that if the auto-login * fails, and the user then specifies a different * account name, he can get logged in with both * LOGNAME and USER in his environment, but the * USER value will be wrong. */ unsetenv("USER"); } #if defined(AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R) if (pty > 2) close(pty); #endif closelog(); if (altlogin == NULL) { altlogin = _PATH_LOGIN; } execv(altlogin, argv); syslog(LOG_ERR, "%s: %m\n", altlogin); fatalperror(net, altlogin); /*NOTREACHED*/ } char ** addarg(argv, val) register char **argv; register char *val; { register char **cpp; if (argv == NULL) { /* * 10 entries, a leading length, and a null */ argv = (char **)malloc(sizeof(*argv) * 12); if (argv == NULL) return(NULL); *argv++ = (char *)10; *argv = (char *)0; } for (cpp = argv; *cpp; cpp++) ; if (cpp == &argv[(int)argv[-1]]) { --argv; *argv = (char *)((int)(*argv) + 10); argv = (char **)realloc(argv, (int)(*argv) + 2); if (argv == NULL) return(NULL); argv++; cpp = &argv[(int)argv[-1] - 10]; } *cpp++ = val; *cpp = 0; return(argv); } #endif /* NEWINIT */ /* * scrub_env() * * Remove a few things from the environment that * don't need to be there. */ scrub_env() { register char **cpp, **cpp2; for (cpp2 = cpp = environ; *cpp; cpp++) { #ifdef __FreeBSD__ if (strncmp(*cpp, "LD_LIBRARY_PATH=", 16) && strncmp(*cpp, "LD_PRELOAD=", 11) && #else if (strncmp(*cpp, "LD_", 3) && strncmp(*cpp, "_RLD_", 5) && strncmp(*cpp, "LIBPATH=", 8) && #endif strncmp(*cpp, "IFS=", 4)) *cpp2++ = *cpp; } *cpp2 = 0; } /* * cleanup() * * This is the routine to call when we are all through, to * clean up anything that needs to be cleaned up. */ /* ARGSUSED */ void cleanup(sig) int sig; { #ifndef PARENT_DOES_UTMP # if (BSD > 43) || defined(convex) char *p; p = line + sizeof("/dev/") - 1; if (logout(p)) logwtmp(p, "", ""); (void)chmod(line, 0666); (void)chown(line, 0, 0); *p = 'p'; (void)chmod(line, 0666); (void)chown(line, 0, 0); (void) shutdown(net, 2); exit(1); # else void rmut(); rmut(); vhangup(); /* XXX */ (void) shutdown(net, 2); exit(1); # endif #else /* PARENT_DOES_UTMP */ # ifdef NEWINIT (void) shutdown(net, 2); exit(1); # else /* NEWINIT */ # ifdef CRAY static int incleanup = 0; register int t; /* * 1: Pick up the zombie, if we are being called * as the signal handler. * 2: If we are a nested cleanup(), return. * 3: Try to clean up TMPDIR. * 4: Fill in utmp with shutdown of process. * 5: Close down the network and pty connections. * 6: Finish up the TMPDIR cleanup, if needed. */ if (sig == SIGCHLD) while (waitpid(-1, 0, WNOHANG) > 0) ; /* VOID */ t = sigblock(sigmask(SIGCHLD)); if (incleanup) { sigsetmask(t); return; } incleanup = 1; sigsetmask(t); if (secflag) { /* * We need to set ourselves back to a null * label to clean up. */ setulvl(sysv.sy_minlvl); setucmp((long)0); } t = cleantmp(&wtmp); setutent(); /* just to make sure */ # endif /* CRAY */ rmut(line); close(pty); (void) shutdown(net, 2); # ifdef CRAY if (t == 0) cleantmp(&wtmp); # endif /* CRAY */ exit(1); # endif /* NEWINT */ #endif /* PARENT_DOES_UTMP */ } #if defined(PARENT_DOES_UTMP) && !defined(NEWINIT) /* * _utmp_sig_rcv * utmp_sig_init * utmp_sig_wait * These three functions are used to coordinate the handling of * the utmp file between the server and the soon-to-be-login shell. * The server actually creates the utmp structure, the child calls * utmp_sig_wait(), until the server calls utmp_sig_notify() and * signals the future-login shell to proceed. */ static int caught=0; /* NZ when signal intercepted */ static void (*func)(); /* address of previous handler */ void _utmp_sig_rcv(sig) int sig; { caught = 1; (void) signal(SIGUSR1, func); } void utmp_sig_init() { /* * register signal handler for UTMP creation */ if ((int)(func = signal(SIGUSR1, _utmp_sig_rcv)) == -1) fatalperror(net, "telnetd/signal"); } void utmp_sig_reset() { (void) signal(SIGUSR1, func); /* reset handler to default */ } # ifdef __hpux # define sigoff() /* do nothing */ # define sigon() /* do nothing */ # endif void utmp_sig_wait() { /* * Wait for parent to write our utmp entry. */ sigoff(); while (caught == 0) { pause(); /* wait until we get a signal (sigon) */ sigoff(); /* turn off signals while we check caught */ } sigon(); /* turn on signals again */ } void utmp_sig_notify(pid) { kill(pid, SIGUSR1); } # ifdef CRAY static int gotsigjob = 0; /*ARGSUSED*/ void sigjob(sig) int sig; { register int jid; register struct jobtemp *jp; while ((jid = waitjob(NULL)) != -1) { if (jid == 0) { return; } gotsigjob++; jobend(jid, NULL, NULL); } } /* * Clean up the TMPDIR that login created. * The first time this is called we pick up the info * from the utmp. If the job has already gone away, * then we'll clean up and be done. If not, then * when this is called the second time it will wait * for the signal that the job is done. */ int cleantmp(wtp) register struct utmp *wtp; { struct utmp *utp; static int first = 1; register int mask, omask, ret; extern struct utmp *getutid P((const struct utmp *_Id)); mask = sigmask(WJSIGNAL); if (first == 0) { omask = sigblock(mask); while (gotsigjob == 0) sigpause(omask); return(1); } first = 0; setutent(); /* just to make sure */ utp = getutid(wtp); if (utp == 0) { syslog(LOG_ERR, "Can't get /etc/utmp entry to clean TMPDIR"); return(-1); } /* * Nothing to clean up if the user shell was never started. */ if (utp->ut_type != USER_PROCESS || utp->ut_jid == 0) return(1); /* * Block the WJSIGNAL while we are in jobend(). */ omask = sigblock(mask); ret = jobend(utp->ut_jid, utp->ut_tpath, utp->ut_user); sigsetmask(omask); return(ret); } int jobend(jid, path, user) register int jid; register char *path; register char *user; { static int saved_jid = 0; static char saved_path[sizeof(wtmp.ut_tpath)+1]; static char saved_user[sizeof(wtmp.ut_user)+1]; if (path) { strncpy(saved_path, path, sizeof(wtmp.ut_tpath)); strncpy(saved_user, user, sizeof(wtmp.ut_user)); saved_path[sizeof(saved_path)] = '\0'; saved_user[sizeof(saved_user)] = '\0'; } if (saved_jid == 0) { saved_jid = jid; return(0); } cleantmpdir(jid, saved_path, saved_user); return(1); } /* * Fork a child process to clean up the TMPDIR */ cleantmpdir(jid, tpath, user) register int jid; register char *tpath; register char *user; { switch(fork()) { case -1: syslog(LOG_ERR, "TMPDIR cleanup(%s): fork() failed: %m\n", tpath); break; case 0: execl(CLEANTMPCMD, CLEANTMPCMD, user, tpath, 0); syslog(LOG_ERR, "TMPDIR cleanup(%s): execl(%s) failed: %m\n", tpath, CLEANTMPCMD); exit(1); default: /* * Forget about child. We will exit, and * /etc/init will pick it up. */ break; } } # endif /* CRAY */ #endif /* defined(PARENT_DOES_UTMP) && !defined(NEWINIT) */ /* * rmut() * * This is the function called by cleanup() to * remove the utmp entry for this person. */ #ifdef UTMPX void rmut() { register f; int found = 0; struct utmp *u, *utmp; int nutmp; struct stat statbf; struct utmpx *utxp, utmpx; /* * This updates the utmpx and utmp entries and make a wtmp/x entry */ SCPYN(utmpx.ut_line, line + sizeof("/dev/") - 1); utxp = getutxline(&utmpx); if (utxp) { utxp->ut_type = DEAD_PROCESS; utxp->ut_exit.e_termination = 0; utxp->ut_exit.e_exit = 0; (void) time(&utmpx.ut_tv.tv_sec); utmpx.ut_tv.tv_usec = 0; modutx(utxp); } endutxent(); } /* end of rmut */ #endif #if !defined(UTMPX) && !(defined(CRAY) || defined(__hpux)) && BSD <= 43 void rmut() { register f; int found = 0; struct utmp *u, *utmp; int nutmp; struct stat statbf; f = open(utmpf, O_RDWR); if (f >= 0) { (void) fstat(f, &statbf); utmp = (struct utmp *)malloc((unsigned)statbf.st_size); if (!utmp) syslog(LOG_ERR, "utmp malloc failed"); if (statbf.st_size && utmp) { nutmp = read(f, (char *)utmp, (int)statbf.st_size); nutmp /= sizeof(struct utmp); for (u = utmp ; u < &utmp[nutmp] ; u++) { if (SCMPN(u->ut_line, line+5) || u->ut_name[0]==0) continue; (void) lseek(f, ((long)u)-((long)utmp), L_SET); SCPYN(u->ut_name, ""); SCPYN(u->ut_host, ""); (void) time(&u->ut_time); (void) write(f, (char *)u, sizeof(wtmp)); found++; } } (void) close(f); } if (found) { f = open(wtmpf, O_WRONLY|O_APPEND); if (f >= 0) { SCPYN(wtmp.ut_line, line+5); SCPYN(wtmp.ut_name, ""); SCPYN(wtmp.ut_host, ""); (void) time(&wtmp.ut_time); (void) write(f, (char *)&wtmp, sizeof(wtmp)); (void) close(f); } } (void) chmod(line, 0666); (void) chown(line, 0, 0); line[strlen("/dev/")] = 'p'; (void) chmod(line, 0666); (void) chown(line, 0, 0); } /* end of rmut */ #endif /* CRAY */ #ifdef __hpux rmut (line) char *line; { struct utmp utmp; struct utmp *utptr; int fd; /* for /etc/wtmp */ utmp.ut_type = USER_PROCESS; (void) strncpy(utmp.ut_id, line+12, sizeof(utmp.ut_id)); (void) setutent(); utptr = getutid(&utmp); /* write it out only if it exists */ if (utptr) { utptr->ut_type = DEAD_PROCESS; utptr->ut_time = time((long *) 0); (void) pututline(utptr); /* set wtmp entry if wtmp file exists */ if ((fd = open(wtmpf, O_WRONLY | O_APPEND)) >= 0) { (void) write(fd, utptr, sizeof(utmp)); (void) close(fd); } } (void) endutent(); (void) chmod(line, 0666); (void) chown(line, 0, 0); line[14] = line[13]; line[13] = line[12]; line[8] = 'm'; line[9] = '/'; line[10] = 'p'; line[11] = 't'; line[12] = 'y'; (void) chmod(line, 0666); (void) chown(line, 0, 0); } #endif diff --git a/libexec/telnetd/telnetd.c b/libexec/telnetd/telnetd.c index afbced32c40a..1c57eb0ba84e 100644 --- a/libexec/telnetd/telnetd.c +++ b/libexec/telnetd/telnetd.c @@ -1,1542 +1,1546 @@ /* * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: telnetd.c,v 1.7 1996/09/22 21:55:46 wosch Exp $ */ #ifndef lint static char copyright[] = "@(#) Copyright (c) 1989, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)telnetd.c 8.2 (Berkeley) 12/15/93"; #endif /* not lint */ #include "telnetd.h" #include "pathnames.h" #if defined(_SC_CRAY_SECURE_SYS) && !defined(SCM_SECURITY) /* * UNICOS 6.0/6.1 do not have SCM_SECURITY defined, so we can * use it to tell us to turn off all the socket security code, * since that is only used in UNICOS 7.0 and later. */ # undef _SC_CRAY_SECURE_SYS #endif #if defined(_SC_CRAY_SECURE_SYS) #include #include # ifdef SO_SEC_MULTI /* 8.0 code */ #include #include # endif /* SO_SEC_MULTI */ int secflag; char tty_dev[16]; struct secdev dv; struct sysv sysv; # ifdef SO_SEC_MULTI /* 8.0 code */ struct socksec ss; # else /* SO_SEC_MULTI */ /* 7.0 code */ struct socket_security ss; # endif /* SO_SEC_MULTI */ #endif /* _SC_CRAY_SECURE_SYS */ #if defined(AUTHENTICATION) #include int auth_level = 0; #endif #if defined(SecurID) int require_SecurID = 0; #endif extern int utmp_len; int registerd_host_only = 0; #ifdef STREAMSPTY # include # include /* make sure we don't get the bsd version */ # include "/usr/include/sys/tty.h" # include /* * Because of the way ptyibuf is used with streams messages, we need * ptyibuf+1 to be on a full-word boundary. The following wierdness * is simply to make that happen. */ long ptyibufbuf[BUFSIZ/sizeof(long)+1]; char *ptyibuf = ((char *)&ptyibufbuf[1])-1; char *ptyip = ((char *)&ptyibufbuf[1])-1; char ptyibuf2[BUFSIZ]; unsigned char ctlbuf[BUFSIZ]; struct strbuf strbufc, strbufd; int readstream(); #else /* ! STREAMPTY */ /* * I/O data buffers, * pointers, and counters. */ char ptyibuf[BUFSIZ], *ptyip = ptyibuf; char ptyibuf2[BUFSIZ]; #endif /* ! STREAMPTY */ int hostinfo = 1; /* do we print login banner? */ #ifdef CRAY extern int newmap; /* nonzero if \n maps to ^M^J */ int lowpty = 0, highpty; /* low, high pty numbers */ #endif /* CRAY */ int debug = 0; int keepalive = 1; char *progname; char *altlogin; extern void usage P((void)); /* * The string to pass to getopt(). We do it this way so * that only the actual options that we support will be * passed off to getopt(). */ char valid_opts[] = { 'd', ':', 'h', 'k', 'n', 'p', ':', 'S', ':', 'u', ':', 'U', #ifdef AUTHENTICATION 'a', ':', 'X', ':', #endif #ifdef BFTPDAEMON 'B', #endif #ifdef DIAGNOSTICS 'D', ':', #endif #if defined(CRAY) && defined(NEWINIT) 'I', ':', #endif #ifdef LINEMODE 'l', #endif #ifdef CRAY 'r', ':', #endif #ifdef SecurID 's', #endif '\0' }; main(argc, argv) char *argv[]; { struct sockaddr_in from; int on = 1, fromlen; register int ch; extern char *optarg; extern int optind; #if defined(IPPROTO_IP) && defined(IP_TOS) int tos = -1; #endif pfrontp = pbackp = ptyobuf; netip = netibuf; nfrontp = nbackp = netobuf; progname = *argv; #ifdef CRAY /* * Get number of pty's before trying to process options, * which may include changing pty range. */ highpty = getnpty(); #endif /* CRAY */ while ((ch = getopt(argc, argv, valid_opts)) != EOF) { switch(ch) { #ifdef AUTHENTICATION case 'a': /* * Check for required authentication level */ if (strcmp(optarg, "debug") == 0) { extern int auth_debug_mode; auth_debug_mode = 1; } else if (strcasecmp(optarg, "none") == 0) { auth_level = 0; } else if (strcasecmp(optarg, "other") == 0) { auth_level = AUTH_OTHER; } else if (strcasecmp(optarg, "user") == 0) { auth_level = AUTH_USER; } else if (strcasecmp(optarg, "valid") == 0) { auth_level = AUTH_VALID; } else if (strcasecmp(optarg, "off") == 0) { /* * This hack turns off authentication */ auth_level = -1; } else { fprintf(stderr, "telnetd: unknown authorization level for -a\n"); } break; #endif /* AUTHENTICATION */ #ifdef BFTPDAEMON case 'B': bftpd++; break; #endif /* BFTPDAEMON */ case 'd': if (strcmp(optarg, "ebug") == 0) { debug++; break; } usage(); /* NOTREACHED */ break; #ifdef DIAGNOSTICS case 'D': /* * Check for desired diagnostics capabilities. */ if (!strcmp(optarg, "report")) { diagnostic |= TD_REPORT|TD_OPTIONS; } else if (!strcmp(optarg, "exercise")) { diagnostic |= TD_EXERCISE; } else if (!strcmp(optarg, "netdata")) { diagnostic |= TD_NETDATA; } else if (!strcmp(optarg, "ptydata")) { diagnostic |= TD_PTYDATA; } else if (!strcmp(optarg, "options")) { diagnostic |= TD_OPTIONS; } else { usage(); /* NOT REACHED */ } break; #endif /* DIAGNOSTICS */ case 'h': hostinfo = 0; break; #if defined(CRAY) && defined(NEWINIT) case 'I': { extern char *gen_id; gen_id = optarg; break; } #endif /* defined(CRAY) && defined(NEWINIT) */ #ifdef LINEMODE case 'l': alwayslinemode = 1; break; #endif /* LINEMODE */ case 'k': #if defined(LINEMODE) && defined(KLUDGELINEMODE) lmodetype = NO_AUTOKLUDGE; #else /* ignore -k option if built without kludge linemode */ #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */ break; case 'n': keepalive = 0; break; case 'p': altlogin = optarg; break; #ifdef CRAY case 'r': { char *strchr(); char *c; /* * Allow the specification of alterations * to the pty search range. It is legal to * specify only one, and not change the * other from its default. */ c = strchr(optarg, '-'); if (c) { *c++ = '\0'; highpty = atoi(c); } if (*optarg != '\0') lowpty = atoi(optarg); if ((lowpty > highpty) || (lowpty < 0) || (highpty > 32767)) { usage(); /* NOT REACHED */ } break; } #endif /* CRAY */ #ifdef SecurID case 's': /* SecurID required */ require_SecurID = 1; break; #endif /* SecurID */ case 'S': #ifdef HAS_GETTOS if ((tos = parsetos(optarg, "tcp")) < 0) fprintf(stderr, "%s%s%s\n", "telnetd: Bad TOS argument '", optarg, "'; will try to use default TOS"); #else fprintf(stderr, "%s%s\n", "TOS option unavailable; ", "-S flag not supported\n"); #endif break; case 'u': utmp_len = atoi(optarg); break; case 'U': registerd_host_only = 1; break; #ifdef AUTHENTICATION case 'X': /* * Check for invalid authentication types */ auth_disable_name(optarg); break; #endif /* AUTHENTICATION */ default: fprintf(stderr, "telnetd: %c: unknown option\n", ch); /* FALLTHROUGH */ case '?': usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; if (debug) { int s, ns, foo; struct servent *sp; static struct sockaddr_in sin = { AF_INET }; if (argc > 1) { usage(); /* NOT REACHED */ } else if (argc == 1) { if (sp = getservbyname(*argv, "tcp")) { sin.sin_port = sp->s_port; } else { sin.sin_port = atoi(*argv); if ((int)sin.sin_port <= 0) { fprintf(stderr, "telnetd: %s: bad port #\n", *argv); usage(); /* NOT REACHED */ } sin.sin_port = htons((u_short)sin.sin_port); } } else { sp = getservbyname("telnet", "tcp"); if (sp == 0) { fprintf(stderr, "telnetd: tcp/telnet: unknown service\n"); exit(1); } sin.sin_port = sp->s_port; } s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) { perror("telnetd: socket");; exit(1); } (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); if (bind(s, (struct sockaddr *)&sin, sizeof sin) < 0) { perror("bind"); exit(1); } if (listen(s, 1) < 0) { perror("listen"); exit(1); } foo = sizeof sin; ns = accept(s, (struct sockaddr *)&sin, &foo); if (ns < 0) { perror("accept"); exit(1); } (void) dup2(ns, 0); (void) close(ns); (void) close(s); #ifdef convex } else if (argc == 1) { ; /* VOID*/ /* Just ignore the host/port name */ #endif } else if (argc > 0) { usage(); /* NOT REACHED */ } #if defined(_SC_CRAY_SECURE_SYS) secflag = sysconf(_SC_CRAY_SECURE_SYS); /* * Get socket's security label */ if (secflag) { int szss = sizeof(ss); #ifdef SO_SEC_MULTI /* 8.0 code */ int sock_multi; int szi = sizeof(int); #endif /* SO_SEC_MULTI */ bzero((char *)&dv, sizeof(dv)); if (getsysv(&sysv, sizeof(struct sysv)) != 0) { perror("getsysv"); exit(1); } /* * Get socket security label and set device values * {security label to be set on ttyp device} */ #ifdef SO_SEC_MULTI /* 8.0 code */ if ((getsockopt(0, SOL_SOCKET, SO_SECURITY, (char *)&ss, &szss) < 0) || (getsockopt(0, SOL_SOCKET, SO_SEC_MULTI, (char *)&sock_multi, &szi) < 0)) { perror("getsockopt"); exit(1); } else { dv.dv_actlvl = ss.ss_actlabel.lt_level; dv.dv_actcmp = ss.ss_actlabel.lt_compart; if (!sock_multi) { dv.dv_minlvl = dv.dv_maxlvl = dv.dv_actlvl; dv.dv_valcmp = dv.dv_actcmp; } else { dv.dv_minlvl = ss.ss_minlabel.lt_level; dv.dv_maxlvl = ss.ss_maxlabel.lt_level; dv.dv_valcmp = ss.ss_maxlabel.lt_compart; } dv.dv_devflg = 0; } #else /* SO_SEC_MULTI */ /* 7.0 code */ if (getsockopt(0, SOL_SOCKET, SO_SECURITY, (char *)&ss, &szss) >= 0) { dv.dv_actlvl = ss.ss_slevel; dv.dv_actcmp = ss.ss_compart; dv.dv_minlvl = ss.ss_minlvl; dv.dv_maxlvl = ss.ss_maxlvl; dv.dv_valcmp = ss.ss_maxcmp; } #endif /* SO_SEC_MULTI */ } #endif /* _SC_CRAY_SECURE_SYS */ openlog("telnetd", LOG_PID | LOG_ODELAY, LOG_DAEMON); fromlen = sizeof (from); if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) { fprintf(stderr, "%s: ", progname); perror("getpeername"); _exit(1); } if (keepalive && setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof (on)) < 0) { syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); } #if defined(IPPROTO_IP) && defined(IP_TOS) { # if defined(HAS_GETTOS) struct tosent *tp; if (tos < 0 && (tp = gettosbyname("telnet", "tcp"))) tos = tp->t_tos; # endif if (tos < 0) tos = 020; /* Low Delay bit */ if (tos && (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(tos)) < 0) && (errno != ENOPROTOOPT) ) syslog(LOG_WARNING, "setsockopt (IP_TOS): %m"); } #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */ net = 0; doit(&from); /* NOTREACHED */ } /* end of main */ void usage() { fprintf(stderr, "Usage: telnetd"); #ifdef AUTHENTICATION fprintf(stderr, " [-a (debug|other|user|valid|off|none)]\n\t"); #endif #ifdef BFTPDAEMON fprintf(stderr, " [-B]"); #endif fprintf(stderr, " [-debug]"); #ifdef DIAGNOSTICS fprintf(stderr, " [-D (options|report|exercise|netdata|ptydata)]\n\t"); #endif #ifdef AUTHENTICATION fprintf(stderr, " [-edebug]"); #endif fprintf(stderr, " [-h]"); #if defined(CRAY) && defined(NEWINIT) fprintf(stderr, " [-Iinitid]"); #endif #if defined(LINEMODE) && defined(KLUDGELINEMODE) fprintf(stderr, " [-k]"); #endif #ifdef LINEMODE fprintf(stderr, " [-l]"); #endif fprintf(stderr, " [-n]"); #ifdef CRAY fprintf(stderr, " [-r[lowpty]-[highpty]]"); #endif fprintf(stderr, "\n\t"); #ifdef SecurID fprintf(stderr, " [-s]"); #endif #ifdef HAS_GETTOS fprintf(stderr, " [-S tos]"); #endif #ifdef AUTHENTICATION fprintf(stderr, " [-X auth-type]"); #endif fprintf(stderr, " [-u utmp_hostname_length] [-U]"); fprintf(stderr, " [port]\n"); exit(1); } /* * getterminaltype * * Ask the other end to send along its terminal type and speed. * Output is the variable terminaltype filled in. */ static unsigned char ttytype_sbbuf[] = { IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE }; int getterminaltype(name) char *name; { int retval = -1; void _gettermname(); settimer(baseline); #if defined(AUTHENTICATION) /* * Handle the Authentication option before we do anything else. */ send_do(TELOPT_AUTHENTICATION, 1); while (his_will_wont_is_changing(TELOPT_AUTHENTICATION)) ttloop(); if (his_state_is_will(TELOPT_AUTHENTICATION)) { retval = auth_wait(name); } #endif send_do(TELOPT_TTYPE, 1); send_do(TELOPT_TSPEED, 1); send_do(TELOPT_XDISPLOC, 1); send_do(TELOPT_NEW_ENVIRON, 1); send_do(TELOPT_OLD_ENVIRON, 1); while ( his_will_wont_is_changing(TELOPT_TTYPE) || his_will_wont_is_changing(TELOPT_TSPEED) || his_will_wont_is_changing(TELOPT_XDISPLOC) || his_will_wont_is_changing(TELOPT_NEW_ENVIRON) || his_will_wont_is_changing(TELOPT_OLD_ENVIRON)) { ttloop(); } if (his_state_is_will(TELOPT_TSPEED)) { static unsigned char sb[] = { IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } if (his_state_is_will(TELOPT_XDISPLOC)) { static unsigned char sb[] = { IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } if (his_state_is_will(TELOPT_NEW_ENVIRON)) { static unsigned char sb[] = { IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } else if (his_state_is_will(TELOPT_OLD_ENVIRON)) { static unsigned char sb[] = { IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE }; bcopy(sb, nfrontp, sizeof sb); nfrontp += sizeof sb; } if (his_state_is_will(TELOPT_TTYPE)) { bcopy(ttytype_sbbuf, nfrontp, sizeof ttytype_sbbuf); nfrontp += sizeof ttytype_sbbuf; } if (his_state_is_will(TELOPT_TSPEED)) { while (sequenceIs(tspeedsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_XDISPLOC)) { while (sequenceIs(xdisplocsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_NEW_ENVIRON)) { while (sequenceIs(environsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_OLD_ENVIRON)) { while (sequenceIs(oenvironsubopt, baseline)) ttloop(); } if (his_state_is_will(TELOPT_TTYPE)) { char first[256], last[256]; while (sequenceIs(ttypesubopt, baseline)) ttloop(); /* * If the other side has already disabled the option, then * we have to just go with what we (might) have already gotten. */ if (his_state_is_will(TELOPT_TTYPE) && !terminaltypeok(terminaltype)) { - (void) strncpy(first, terminaltype, sizeof(first)); + (void) strncpy(first, terminaltype, sizeof(first)-1); + first[sizeof(first)-1] = '\0'; for(;;) { /* * Save the unknown name, and request the next name. */ - (void) strncpy(last, terminaltype, sizeof(last)); + (void) strncpy(last, terminaltype, sizeof(last)-1); + last[sizeof(last)-1] = '\0'; _gettermname(); if (terminaltypeok(terminaltype)) break; if ((strncmp(last, terminaltype, sizeof(last)) == 0) || his_state_is_wont(TELOPT_TTYPE)) { /* * We've hit the end. If this is the same as * the first name, just go with it. */ if (strncmp(first, terminaltype, sizeof(first)) == 0) break; /* * Get the terminal name one more time, so that * RFC1091 compliant telnets will cycle back to * the start of the list. */ _gettermname(); - if (strncmp(first, terminaltype, sizeof(first)) != 0) - (void) strncpy(terminaltype, first, sizeof(first)); + if (strncmp(first, terminaltype, sizeof(first)) != 0) { + (void) strncpy(terminaltype, first, sizeof(terminaltype)-1); + terminaltype[sizeof(terminaltype)-1] = '\0'; + } break; } } } } return(retval); } /* end of getterminaltype */ void _gettermname() { /* * If the client turned off the option, * we can't send another request, so we * just return. */ if (his_state_is_wont(TELOPT_TTYPE)) return; settimer(baseline); bcopy(ttytype_sbbuf, nfrontp, sizeof ttytype_sbbuf); nfrontp += sizeof ttytype_sbbuf; while (sequenceIs(ttypesubopt, baseline)) ttloop(); } int terminaltypeok(s) char *s; { char buf[1024]; if (terminaltype == NULL) return(1); /* * tgetent() will return 1 if the type is known, and * 0 if it is not known. If it returns -1, it couldn't * open the database. But if we can't open the database, * it won't help to say we failed, because we won't be * able to verify anything else. So, we treat -1 like 1. */ if (tgetent(buf, s) == 0) return(0); return(1); } #ifndef MAXHOSTNAMELEN #define MAXHOSTNAMELEN 64 #endif /* MAXHOSTNAMELEN */ char *hostname; char host_name[MAXHOSTNAMELEN]; char remote_host_name[MAXHOSTNAMELEN]; extern void telnet P((int, int, char *)); int level; char user_name[256]; /* * Get a pty, scan input lines. */ doit(who) struct sockaddr_in *who; { char *host, *inet_ntoa(); int t; struct hostent *hp; int ptynum; /* * Find an available pty to use. */ #ifndef convex pty = getpty(&ptynum); if (pty < 0) fatal(net, "All network ports in use"); #else for (;;) { char *lp; extern char *line, *getpty(); if ((lp = getpty()) == NULL) fatal(net, "Out of ptys"); if ((pty = open(lp, 2)) >= 0) { strcpy(line,lp); line[5] = 't'; break; } } #endif #if defined(_SC_CRAY_SECURE_SYS) /* * set ttyp line security label */ if (secflag) { char slave_dev[16]; sprintf(tty_dev, "/dev/pty/%03d", ptynum); if (setdevs(tty_dev, &dv) < 0) fatal(net, "cannot set pty security"); sprintf(slave_dev, "/dev/ttyp%03d", ptynum); if (setdevs(slave_dev, &dv) < 0) fatal(net, "cannot set tty security"); } #endif /* _SC_CRAY_SECURE_SYS */ /* get name of connected client */ hp = gethostbyaddr((char *)&who->sin_addr, sizeof (struct in_addr), who->sin_family); if (hp == NULL && registerd_host_only) { fatal(net, "Couldn't resolve your address into a host name.\r\n\ Please contact your net administrator"); } else if (hp && (strlen(hp->h_name) <= ((utmp_len < 0) ? -utmp_len : utmp_len))) { host = hp->h_name; } else { host = inet_ntoa(who->sin_addr); } /* * We must make a copy because Kerberos is probably going * to also do a gethost* and overwrite the static data... */ strncpy(remote_host_name, host, sizeof(remote_host_name)-1); remote_host_name[sizeof(remote_host_name)-1] = 0; host = remote_host_name; (void) gethostname(host_name, sizeof (host_name)); hostname = host_name; #if defined(AUTHENTICATION) auth_encrypt_init(hostname, host, "TELNETD", 1); #endif init_env(); /* * get terminal type. */ *user_name = 0; level = getterminaltype(user_name); setenv("TERM", terminaltype ? terminaltype : "network", 1); #if defined(_SC_CRAY_SECURE_SYS) if (secflag) { if (setulvl(dv.dv_actlvl) < 0) fatal(net,"cannot setulvl()"); if (setucmp(dv.dv_actcmp) < 0) fatal(net, "cannot setucmp()"); } #endif /* _SC_CRAY_SECURE_SYS */ telnet(net, pty, host); /* begin server process */ /*NOTREACHED*/ } /* end of doit */ #if defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) int Xterm_output(ibufp, obuf, icountp, ocount) char **ibufp, *obuf; int *icountp, ocount; { int ret; ret = term_output(*ibufp, obuf, *icountp, ocount); *ibufp += *icountp; *icountp = 0; return(ret); } #define term_output Xterm_output #endif /* defined(CRAY2) && defined(UNICOS5) && defined(UNICOS50) */ /* * Main loop. Select from pty and network, and * hand data to telnet receiver finite state machine. */ void telnet(f, p, host) int f, p; char *host; { int on = 1; #define TABBUFSIZ 512 char defent[TABBUFSIZ]; char defstrs[TABBUFSIZ]; #undef TABBUFSIZ char *HE; char *HN; char *IM; void netflush(); /* * Initialize the slc mapping table. */ get_slc_defaults(); /* * Do some tests where it is desireable to wait for a response. * Rather than doing them slowly, one at a time, do them all * at once. */ if (my_state_is_wont(TELOPT_SGA)) send_will(TELOPT_SGA, 1); /* * Is the client side a 4.2 (NOT 4.3) system? We need to know this * because 4.2 clients are unable to deal with TCP urgent data. * * To find out, we send out a "DO ECHO". If the remote system * answers "WILL ECHO" it is probably a 4.2 client, and we note * that fact ("WILL ECHO" ==> that the client will echo what * WE, the server, sends it; it does NOT mean that the client will * echo the terminal input). */ send_do(TELOPT_ECHO, 1); #ifdef LINEMODE if (his_state_is_wont(TELOPT_LINEMODE)) { /* Query the peer for linemode support by trying to negotiate * the linemode option. */ linemode = 0; editmode = 0; send_do(TELOPT_LINEMODE, 1); /* send do linemode */ } #endif /* LINEMODE */ /* * Send along a couple of other options that we wish to negotiate. */ send_do(TELOPT_NAWS, 1); send_will(TELOPT_STATUS, 1); flowmode = 1; /* default flow control state */ restartany = -1; /* uninitialized... */ send_do(TELOPT_LFLOW, 1); /* * Spin, waiting for a response from the DO ECHO. However, * some REALLY DUMB telnets out there might not respond * to the DO ECHO. So, we spin looking for NAWS, (most dumb * telnets so far seem to respond with WONT for a DO that * they don't understand...) because by the time we get the * response, it will already have processed the DO ECHO. * Kludge upon kludge. */ while (his_will_wont_is_changing(TELOPT_NAWS)) ttloop(); /* * But... * The client might have sent a WILL NAWS as part of its * startup code; if so, we'll be here before we get the * response to the DO ECHO. We'll make the assumption * that any implementation that understands about NAWS * is a modern enough implementation that it will respond * to our DO ECHO request; hence we'll do another spin * waiting for the ECHO option to settle down, which is * what we wanted to do in the first place... */ if (his_want_state_is_will(TELOPT_ECHO) && his_state_is_will(TELOPT_NAWS)) { while (his_will_wont_is_changing(TELOPT_ECHO)) ttloop(); } /* * On the off chance that the telnet client is broken and does not * respond to the DO ECHO we sent, (after all, we did send the * DO NAWS negotiation after the DO ECHO, and we won't get here * until a response to the DO NAWS comes back) simulate the * receipt of a will echo. This will also send a WONT ECHO * to the client, since we assume that the client failed to * respond because it believes that it is already in DO ECHO * mode, which we do not want. */ if (his_want_state_is_will(TELOPT_ECHO)) { DIAG(TD_OPTIONS, {sprintf(nfrontp, "td: simulating recv\r\n"); nfrontp += strlen(nfrontp);}); willoption(TELOPT_ECHO); } /* * Finally, to clean things up, we turn on our echo. This * will break stupid 4.2 telnets out of local terminal echo. */ if (my_state_is_wont(TELOPT_ECHO)) send_will(TELOPT_ECHO, 1); #ifndef STREAMSPTY /* * Turn on packet mode */ (void) ioctl(p, TIOCPKT, (char *)&on); #endif #if defined(LINEMODE) && defined(KLUDGELINEMODE) /* * Continuing line mode support. If client does not support * real linemode, attempt to negotiate kludge linemode by sending * the do timing mark sequence. */ if (lmodetype < REAL_LINEMODE) send_do(TELOPT_TM, 1); #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */ /* * Call telrcv() once to pick up anything received during * terminal type negotiation, 4.2/4.3 determination, and * linemode negotiation. */ telrcv(); (void) ioctl(f, FIONBIO, (char *)&on); (void) ioctl(p, FIONBIO, (char *)&on); #if defined(CRAY2) && defined(UNICOS5) init_termdriver(f, p, interrupt, sendbrk); #endif #if defined(SO_OOBINLINE) (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof on); #endif /* defined(SO_OOBINLINE) */ #ifdef SIGTSTP (void) signal(SIGTSTP, SIG_IGN); #endif #ifdef SIGTTOU /* * Ignoring SIGTTOU keeps the kernel from blocking us * in ttioct() in /sys/tty.c. */ (void) signal(SIGTTOU, SIG_IGN); #endif (void) signal(SIGCHLD, cleanup); #if defined(CRAY2) && defined(UNICOS5) /* * Cray-2 will send a signal when pty modes are changed by slave * side. Set up signal handler now. */ if ((int)signal(SIGUSR1, termstat) < 0) perror("signal"); else if (ioctl(p, TCSIGME, (char *)SIGUSR1) < 0) perror("ioctl:TCSIGME"); /* * Make processing loop check terminal characteristics early on. */ termstat(); #endif #ifdef TIOCNOTTY { register int t; t = open(_PATH_TTY, O_RDWR); if (t >= 0) { (void) ioctl(t, TIOCNOTTY, (char *)0); (void) close(t); } } #endif #if defined(CRAY) && defined(NEWINIT) && defined(TIOCSCTTY) (void) setsid(); ioctl(p, TIOCSCTTY, 0); #endif /* * Show banner that getty never gave. * * We put the banner in the pty input buffer. This way, it * gets carriage return null processing, etc., just like all * other pty --> client data. */ #if !defined(CRAY) || !defined(NEWINIT) if (getenv("USER")) hostinfo = 0; #endif if (getent(defent, "default") == 1) { char *getstr(); char *cp=defstrs; HE = getstr("he", &cp); HN = getstr("hn", &cp); IM = getstr("im", &cp); if (HN && *HN) (void) strcpy(host_name, HN); if (IM == 0) IM = ""; } else { IM = DEFAULT_IM; HE = 0; } edithost(HE, host_name); if (hostinfo && *IM) putf(IM, ptyibuf2); if (pcc) (void) strncat(ptyibuf2, ptyip, pcc+1); ptyip = ptyibuf2; pcc = strlen(ptyip); #ifdef LINEMODE /* * Last check to make sure all our states are correct. */ init_termbuf(); localstat(); #endif /* LINEMODE */ DIAG(TD_REPORT, {sprintf(nfrontp, "td: Entering processing loop\r\n"); nfrontp += strlen(nfrontp);}); /* * Startup the login process on the slave side of the terminal * now. We delay this until here to insure option negotiation * is complete. */ startslave(host, level, user_name); for (;;) { fd_set ibits, obits, xbits; register int c; if (ncc < 0 && pcc < 0) break; #if defined(CRAY2) && defined(UNICOS5) if (needtermstat) _termstat(); #endif /* defined(CRAY2) && defined(UNICOS5) */ FD_ZERO(&ibits); FD_ZERO(&obits); FD_ZERO(&xbits); /* * Never look for input if there's still * stuff in the corresponding output buffer */ if (nfrontp - nbackp || pcc > 0) { FD_SET(f, &obits); } else { FD_SET(p, &ibits); } if (pfrontp - pbackp || ncc > 0) { FD_SET(p, &obits); } else { FD_SET(f, &ibits); } if (!SYNCHing) { FD_SET(f, &xbits); } if ((c = select(16, &ibits, &obits, &xbits, (struct timeval *)0)) < 1) { if (c == -1) { if (errno == EINTR) { continue; } } sleep(5); continue; } /* * Any urgent data? */ if (FD_ISSET(net, &xbits)) { SYNCHing = 1; } /* * Something to read from the network... */ if (FD_ISSET(net, &ibits)) { #if !defined(SO_OOBINLINE) /* * In 4.2 (and 4.3 beta) systems, the * OOB indication and data handling in the kernel * is such that if two separate TCP Urgent requests * come in, one byte of TCP data will be overlaid. * This is fatal for Telnet, but we try to live * with it. * * In addition, in 4.2 (and...), a special protocol * is needed to pick up the TCP Urgent data in * the correct sequence. * * What we do is: if we think we are in urgent * mode, we look to see if we are "at the mark". * If we are, we do an OOB receive. If we run * this twice, we will do the OOB receive twice, * but the second will fail, since the second * time we were "at the mark", but there wasn't * any data there (the kernel doesn't reset * "at the mark" until we do a normal read). * Once we've read the OOB data, we go ahead * and do normal reads. * * There is also another problem, which is that * since the OOB byte we read doesn't put us * out of OOB state, and since that byte is most * likely the TELNET DM (data mark), we would * stay in the TELNET SYNCH (SYNCHing) state. * So, clocks to the rescue. If we've "just" * received a DM, then we test for the * presence of OOB data when the receive OOB * fails (and AFTER we did the normal mode read * to clear "at the mark"). */ if (SYNCHing) { int atmark; (void) ioctl(net, SIOCATMARK, (char *)&atmark); if (atmark) { ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB); if ((ncc == -1) && (errno == EINVAL)) { ncc = read(net, netibuf, sizeof (netibuf)); if (sequenceIs(didnetreceive, gotDM)) { SYNCHing = stilloob(net); } } } else { ncc = read(net, netibuf, sizeof (netibuf)); } } else { ncc = read(net, netibuf, sizeof (netibuf)); } settimer(didnetreceive); #else /* !defined(SO_OOBINLINE)) */ ncc = read(net, netibuf, sizeof (netibuf)); #endif /* !defined(SO_OOBINLINE)) */ if (ncc < 0 && errno == EWOULDBLOCK) ncc = 0; else { if (ncc <= 0) { break; } netip = netibuf; } DIAG((TD_REPORT | TD_NETDATA), {sprintf(nfrontp, "td: netread %d chars\r\n", ncc); nfrontp += strlen(nfrontp);}); DIAG(TD_NETDATA, printdata("nd", netip, ncc)); } /* * Something to read from the pty... */ if (FD_ISSET(p, &ibits)) { #ifndef STREAMSPTY pcc = read(p, ptyibuf, BUFSIZ); #else pcc = readstream(p, ptyibuf, BUFSIZ); #endif /* * On some systems, if we try to read something * off the master side before the slave side is * opened, we get EIO. */ if (pcc < 0 && (errno == EWOULDBLOCK || #ifdef EAGAIN errno == EAGAIN || #endif errno == EIO)) { pcc = 0; } else { if (pcc <= 0) break; #if !defined(CRAY2) || !defined(UNICOS5) #ifdef LINEMODE /* * If ioctl from pty, pass it through net */ if (ptyibuf[0] & TIOCPKT_IOCTL) { copy_termbuf(ptyibuf+1, pcc-1); localstat(); pcc = 1; } #endif /* LINEMODE */ if (ptyibuf[0] & TIOCPKT_FLUSHWRITE) { netclear(); /* clear buffer back */ #ifndef NO_URGENT /* * There are client telnets on some * operating systems get screwed up * royally if we send them urgent * mode data. */ *nfrontp++ = IAC; *nfrontp++ = DM; neturg = nfrontp-1; /* off by one XXX */ #endif } if (his_state_is_will(TELOPT_LFLOW) && (ptyibuf[0] & (TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))) { int newflow = ptyibuf[0] & TIOCPKT_DOSTOP ? 1 : 0; if (newflow != flowmode) { flowmode = newflow; (void) sprintf(nfrontp, "%c%c%c%c%c%c", IAC, SB, TELOPT_LFLOW, flowmode ? LFLOW_ON : LFLOW_OFF, IAC, SE); nfrontp += 6; } } pcc--; ptyip = ptyibuf+1; #else /* defined(CRAY2) && defined(UNICOS5) */ if (!uselinemode) { unpcc = pcc; unptyip = ptyibuf; pcc = term_output(&unptyip, ptyibuf2, &unpcc, BUFSIZ); ptyip = ptyibuf2; } else ptyip = ptyibuf; #endif /* defined(CRAY2) && defined(UNICOS5) */ } } while (pcc > 0) { if ((&netobuf[BUFSIZ] - nfrontp) < 2) break; c = *ptyip++ & 0377, pcc--; if (c == IAC) *nfrontp++ = c; #if defined(CRAY2) && defined(UNICOS5) else if (c == '\n' && my_state_is_wont(TELOPT_BINARY) && newmap) *nfrontp++ = '\r'; #endif /* defined(CRAY2) && defined(UNICOS5) */ *nfrontp++ = c; if ((c == '\r') && (my_state_is_wont(TELOPT_BINARY))) { if (pcc > 0 && ((*ptyip & 0377) == '\n')) { *nfrontp++ = *ptyip++ & 0377; pcc--; } else *nfrontp++ = '\0'; } } #if defined(CRAY2) && defined(UNICOS5) /* * If chars were left over from the terminal driver, * note their existence. */ if (!uselinemode && unpcc) { pcc = unpcc; unpcc = 0; ptyip = unptyip; } #endif /* defined(CRAY2) && defined(UNICOS5) */ if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0) netflush(); if (ncc > 0) telrcv(); if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0) ptyflush(); } cleanup(0); } /* end of telnet */ #ifndef TCSIG # ifdef TIOCSIG # define TCSIG TIOCSIG # endif #endif #ifdef STREAMSPTY int flowison = -1; /* current state of flow: -1 is unknown */ int readstream(p, ibuf, bufsize) int p; char *ibuf; int bufsize; { int flags = 0; int ret = 0; struct termios *tsp; struct termio *tp; struct iocblk *ip; char vstop, vstart; int ixon; int newflow; strbufc.maxlen = BUFSIZ; strbufc.buf = (char *)ctlbuf; strbufd.maxlen = bufsize-1; strbufd.len = 0; strbufd.buf = ibuf+1; ibuf[0] = 0; ret = getmsg(p, &strbufc, &strbufd, &flags); if (ret < 0) /* error of some sort -- probably EAGAIN */ return(-1); if (strbufc.len <= 0 || ctlbuf[0] == M_DATA) { /* data message */ if (strbufd.len > 0) { /* real data */ return(strbufd.len + 1); /* count header char */ } else { /* nothing there */ errno = EAGAIN; return(-1); } } /* * It's a control message. Return 1, to look at the flag we set */ switch (ctlbuf[0]) { case M_FLUSH: if (ibuf[1] & FLUSHW) ibuf[0] = TIOCPKT_FLUSHWRITE; return(1); case M_IOCTL: ip = (struct iocblk *) (ibuf+1); switch (ip->ioc_cmd) { case TCSETS: case TCSETSW: case TCSETSF: tsp = (struct termios *) (ibuf+1 + sizeof(struct iocblk)); vstop = tsp->c_cc[VSTOP]; vstart = tsp->c_cc[VSTART]; ixon = tsp->c_iflag & IXON; break; case TCSETA: case TCSETAW: case TCSETAF: tp = (struct termio *) (ibuf+1 + sizeof(struct iocblk)); vstop = tp->c_cc[VSTOP]; vstart = tp->c_cc[VSTART]; ixon = tp->c_iflag & IXON; break; default: errno = EAGAIN; return(-1); } newflow = (ixon && (vstart == 021) && (vstop == 023)) ? 1 : 0; if (newflow != flowison) { /* it's a change */ flowison = newflow; ibuf[0] = newflow ? TIOCPKT_DOSTOP : TIOCPKT_NOSTOP; return(1); } } /* nothing worth doing anything about */ errno = EAGAIN; return(-1); } #endif /* STREAMSPTY */ /* * Send interrupt to process on other side of pty. * If it is in raw mode, just write NULL; * otherwise, write intr char. */ void interrupt() { ptyflush(); /* half-hearted */ #ifdef TCSIG (void) ioctl(pty, TCSIG, (char *)SIGINT); #else /* TCSIG */ init_termbuf(); *pfrontp++ = slctab[SLC_IP].sptr ? (unsigned char)*slctab[SLC_IP].sptr : '\177'; #endif /* TCSIG */ } /* * Send quit to process on other side of pty. * If it is in raw mode, just write NULL; * otherwise, write quit char. */ void sendbrk() { ptyflush(); /* half-hearted */ #ifdef TCSIG (void) ioctl(pty, TCSIG, (char *)SIGQUIT); #else /* TCSIG */ init_termbuf(); *pfrontp++ = slctab[SLC_ABORT].sptr ? (unsigned char)*slctab[SLC_ABORT].sptr : '\034'; #endif /* TCSIG */ } void sendsusp() { #ifdef SIGTSTP ptyflush(); /* half-hearted */ # ifdef TCSIG (void) ioctl(pty, TCSIG, (char *)SIGTSTP); # else /* TCSIG */ *pfrontp++ = slctab[SLC_SUSP].sptr ? (unsigned char)*slctab[SLC_SUSP].sptr : '\032'; # endif /* TCSIG */ #endif /* SIGTSTP */ } /* * When we get an AYT, if ^T is enabled, use that. Otherwise, * just send back "[Yes]". */ void recv_ayt() { #if defined(SIGINFO) && defined(TCSIG) if (slctab[SLC_AYT].sptr && *slctab[SLC_AYT].sptr != _POSIX_VDISABLE) { (void) ioctl(pty, TCSIG, (char *)SIGINFO); return; } #endif (void) strcpy(nfrontp, "\r\n[Yes]\r\n"); nfrontp += 9; } void doeof() { init_termbuf(); #if defined(LINEMODE) && defined(USE_TERMIO) && (VEOF == VMIN) if (!tty_isediting()) { extern char oldeofc; *pfrontp++ = oldeofc; return; } #endif *pfrontp++ = slctab[SLC_EOF].sptr ? (unsigned char)*slctab[SLC_EOF].sptr : '\004'; } diff --git a/libexec/telnetd/utility.c b/libexec/telnetd/utility.c index 3620813bd6c6..6786bba222f3 100644 --- a/libexec/telnetd/utility.c +++ b/libexec/telnetd/utility.c @@ -1,1083 +1,1084 @@ /* * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id$ + * $Id: utility.c,v 1.5 1996/09/22 21:55:52 wosch Exp $ */ #ifndef lint static char sccsid[] = "@(#)utility.c 8.2 (Berkeley) 12/15/93"; #endif /* not lint */ #ifdef __FreeBSD__ #include #endif #define PRINTOPTIONS #include "telnetd.h" /* * utility functions performing io related tasks */ /* * ttloop * * A small subroutine to flush the network output buffer, get some data * from the network, and pass it through the telnet state machine. We * also flush the pty input buffer (by dropping its data) if it becomes * too full. */ void ttloop() { void netflush(); DIAG(TD_REPORT, {sprintf(nfrontp, "td: ttloop\r\n"); nfrontp += strlen(nfrontp);}); if (nfrontp-nbackp) { netflush(); } ncc = read(net, netibuf, sizeof netibuf); if (ncc < 0) { syslog(LOG_INFO, "ttloop: read: %m\n"); exit(1); } else if (ncc == 0) { syslog(LOG_INFO, "ttloop: peer died: %m\n"); exit(1); } DIAG(TD_REPORT, {sprintf(nfrontp, "td: ttloop read %d chars\r\n", ncc); nfrontp += strlen(nfrontp);}); netip = netibuf; telrcv(); /* state machine */ if (ncc > 0) { pfrontp = pbackp = ptyobuf; telrcv(); } } /* end of ttloop */ /* * Check a descriptor to see if out of band data exists on it. */ int stilloob(s) int s; /* socket number */ { static struct timeval timeout = { 0 }; fd_set excepts; int value; do { FD_ZERO(&excepts); FD_SET(s, &excepts); + memset((char *)&timeout, 0, sizeof timeout); value = select(s+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout); } while ((value == -1) && (errno == EINTR)); if (value < 0) { fatalperror(pty, "select"); } if (FD_ISSET(s, &excepts)) { return 1; } else { return 0; } } void ptyflush() { int n; if ((n = pfrontp - pbackp) > 0) { DIAG((TD_REPORT | TD_PTYDATA), { sprintf(nfrontp, "td: ptyflush %d chars\r\n", n); nfrontp += strlen(nfrontp); }); DIAG(TD_PTYDATA, printdata("pd", pbackp, n)); n = write(pty, pbackp, n); } if (n < 0) { if (errno == EWOULDBLOCK || errno == EINTR) return; cleanup(0); } pbackp += n; if (pbackp == pfrontp) pbackp = pfrontp = ptyobuf; } /* * nextitem() * * Return the address of the next "item" in the TELNET data * stream. This will be the address of the next character if * the current address is a user data character, or it will * be the address of the character following the TELNET command * if the current address is a TELNET IAC ("I Am a Command") * character. */ char * nextitem(current) char *current; { if ((*current&0xff) != IAC) { return current+1; } switch (*(current+1)&0xff) { case DO: case DONT: case WILL: case WONT: return current+3; case SB: /* loop forever looking for the SE */ { register char *look = current+2; for (;;) { if ((*look++&0xff) == IAC) { if ((*look++&0xff) == SE) { return look; } } } } default: return current+2; } } /* end of nextitem */ /* * netclear() * * We are about to do a TELNET SYNCH operation. Clear * the path to the network. * * Things are a bit tricky since we may have sent the first * byte or so of a previous TELNET command into the network. * So, we have to scan the network buffer from the beginning * until we are up to where we want to be. * * A side effect of what we do, just to keep things * simple, is to clear the urgent data pointer. The principal * caller should be setting the urgent data pointer AFTER calling * us in any case. */ void netclear() { register char *thisitem, *next; char *good; #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \ ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL)) thisitem = netobuf; while ((next = nextitem(thisitem)) <= nbackp) { thisitem = next; } /* Now, thisitem is first before/at boundary. */ good = netobuf; /* where the good bytes go */ while (nfrontp > thisitem) { if (wewant(thisitem)) { int length; next = thisitem; do { next = nextitem(next); } while (wewant(next) && (nfrontp > next)); length = next-thisitem; bcopy(thisitem, good, length); good += length; thisitem = next; } else { thisitem = nextitem(thisitem); } } nbackp = netobuf; nfrontp = good; /* next byte to be sent */ neturg = 0; } /* end of netclear */ /* * netflush * Send as much data as possible to the network, * handling requests for urgent data. */ void netflush() { int n; extern int not42; if ((n = nfrontp - nbackp) > 0) { DIAG(TD_REPORT, { sprintf(nfrontp, "td: netflush %d chars\r\n", n); n += strlen(nfrontp); /* get count first */ nfrontp += strlen(nfrontp); /* then move pointer */ }); /* * if no urgent data, or if the other side appears to be an * old 4.2 client (and thus unable to survive TCP urgent data), * write the entire buffer in non-OOB mode. */ if ((neturg == 0) || (not42 == 0)) { n = write(net, nbackp, n); /* normal write */ } else { n = neturg - nbackp; /* * In 4.2 (and 4.3) systems, there is some question about * what byte in a sendOOB operation is the "OOB" data. * To make ourselves compatible, we only send ONE byte * out of band, the one WE THINK should be OOB (though * we really have more the TCP philosophy of urgent data * rather than the Unix philosophy of OOB data). */ if (n > 1) { n = send(net, nbackp, n-1, 0); /* send URGENT all by itself */ } else { n = send(net, nbackp, n, MSG_OOB); /* URGENT data */ } } } if (n < 0) { if (errno == EWOULDBLOCK || errno == EINTR) return; cleanup(0); } nbackp += n; if (nbackp >= neturg) { neturg = 0; } if (nbackp == nfrontp) { nbackp = nfrontp = netobuf; } return; } /* end of netflush */ /* * writenet * * Just a handy little function to write a bit of raw data to the net. * It will force a transmit of the buffer if necessary * * arguments * ptr - A pointer to a character string to write * len - How many bytes to write */ void writenet(ptr, len) register unsigned char *ptr; register int len; { /* flush buffer if no room for new data) */ if ((&netobuf[BUFSIZ] - nfrontp) < len) { /* if this fails, don't worry, buffer is a little big */ netflush(); } bcopy(ptr, nfrontp, len); nfrontp += len; } /* end of writenet */ /* * miscellaneous functions doing a variety of little jobs follow ... */ void fatal(f, msg) int f; char *msg; { char buf[BUFSIZ]; (void) sprintf(buf, "telnetd: %s.\r\n", msg); (void) write(f, buf, (int)strlen(buf)); sleep(1); /*XXX*/ exit(1); } void fatalperror(f, msg) int f; char *msg; { char buf[BUFSIZ], *strerror(); (void) sprintf(buf, "%s: %s\r\n", msg, strerror(errno)); fatal(f, buf); } char editedhost[32]; void edithost(pat, host) register char *pat; register char *host; { register char *res = editedhost; char *strncpy(); if (!pat) pat = ""; while (*pat) { switch (*pat) { case '#': if (*host) host++; break; case '@': if (*host) *res++ = *host++; break; default: *res++ = *pat; break; } if (res == &editedhost[sizeof editedhost - 1]) { *res = '\0'; return; } pat++; } if (*host) (void) strncpy(res, host, sizeof editedhost - (res - editedhost) -1); else *res = '\0'; editedhost[sizeof editedhost - 1] = '\0'; } static char *putlocation; void putstr(s) register char *s; { while (*s) putchr(*s++); } void putchr(cc) int cc; { *putlocation++ = cc; } #ifdef __FreeBSD__ static char fmtstr[] = { "%+" }; #else /* * This is split on two lines so that SCCS will not see the M * between two % signs and expand it... */ static char fmtstr[] = { "%l:%M\ %P on %A, %d %B %Y" }; #endif void putf(cp, where) register char *cp; char *where; { char *slash; time_t t; char db[100]; #ifdef STREAMSPTY extern char *index(); #else extern char *rindex(); #endif putlocation = where; while (*cp) { if (*cp != '%') { putchr(*cp++); continue; } switch (*++cp) { case 't': #ifdef STREAMSPTY /* names are like /dev/pts/2 -- we want pts/2 */ slash = index(line+1, '/'); #else slash = rindex(line, '/'); #endif if (slash == (char *) 0) putstr(line); else putstr(&slash[1]); break; case 'h': putstr(editedhost); break; case 'd': #ifdef __FreeBSD__ setlocale(LC_TIME, ""); #endif (void)time(&t); (void)strftime(db, sizeof(db), fmtstr, localtime(&t)); putstr(db); break; case '%': putchr('%'); break; } cp++; } } #ifdef DIAGNOSTICS /* * Print telnet options and commands in plain text, if possible. */ void printoption(fmt, option) register char *fmt; register int option; { if (TELOPT_OK(option)) sprintf(nfrontp, "%s %s\r\n", fmt, TELOPT(option)); else if (TELCMD_OK(option)) sprintf(nfrontp, "%s %s\r\n", fmt, TELCMD(option)); else sprintf(nfrontp, "%s %d\r\n", fmt, option); nfrontp += strlen(nfrontp); return; } void printsub(direction, pointer, length) char direction; /* '<' or '>' */ unsigned char *pointer; /* where suboption data sits */ int length; /* length of suboption data */ { register int i; char buf[512]; if (!(diagnostic & TD_OPTIONS)) return; if (direction) { sprintf(nfrontp, "td: %s suboption ", direction == '<' ? "recv" : "send"); nfrontp += strlen(nfrontp); if (length >= 3) { register int j; i = pointer[length-2]; j = pointer[length-1]; if (i != IAC || j != SE) { sprintf(nfrontp, "(terminated by "); nfrontp += strlen(nfrontp); if (TELOPT_OK(i)) sprintf(nfrontp, "%s ", TELOPT(i)); else if (TELCMD_OK(i)) sprintf(nfrontp, "%s ", TELCMD(i)); else sprintf(nfrontp, "%d ", i); nfrontp += strlen(nfrontp); if (TELOPT_OK(j)) sprintf(nfrontp, "%s", TELOPT(j)); else if (TELCMD_OK(j)) sprintf(nfrontp, "%s", TELCMD(j)); else sprintf(nfrontp, "%d", j); nfrontp += strlen(nfrontp); sprintf(nfrontp, ", not IAC SE!) "); nfrontp += strlen(nfrontp); } } length -= 2; } if (length < 1) { sprintf(nfrontp, "(Empty suboption??\?)"); nfrontp += strlen(nfrontp); return; } switch (pointer[0]) { case TELOPT_TTYPE: sprintf(nfrontp, "TERMINAL-TYPE "); nfrontp += strlen(nfrontp); switch (pointer[1]) { case TELQUAL_IS: sprintf(nfrontp, "IS \"%.*s\"", length-2, (char *)pointer+2); break; case TELQUAL_SEND: sprintf(nfrontp, "SEND"); break; default: sprintf(nfrontp, "- unknown qualifier %d (0x%x).", pointer[1], pointer[1]); } nfrontp += strlen(nfrontp); break; case TELOPT_TSPEED: sprintf(nfrontp, "TERMINAL-SPEED"); nfrontp += strlen(nfrontp); if (length < 2) { sprintf(nfrontp, " (empty suboption??\?)"); nfrontp += strlen(nfrontp); break; } switch (pointer[1]) { case TELQUAL_IS: sprintf(nfrontp, " IS %.*s", length-2, (char *)pointer+2); nfrontp += strlen(nfrontp); break; default: if (pointer[1] == 1) sprintf(nfrontp, " SEND"); else sprintf(nfrontp, " %d (unknown)", pointer[1]); nfrontp += strlen(nfrontp); for (i = 2; i < length; i++) { sprintf(nfrontp, " ?%d?", pointer[i]); nfrontp += strlen(nfrontp); } break; } break; case TELOPT_LFLOW: sprintf(nfrontp, "TOGGLE-FLOW-CONTROL"); nfrontp += strlen(nfrontp); if (length < 2) { sprintf(nfrontp, " (empty suboption??\?)"); nfrontp += strlen(nfrontp); break; } switch (pointer[1]) { case LFLOW_OFF: sprintf(nfrontp, " OFF"); break; case LFLOW_ON: sprintf(nfrontp, " ON"); break; case LFLOW_RESTART_ANY: sprintf(nfrontp, " RESTART-ANY"); break; case LFLOW_RESTART_XON: sprintf(nfrontp, " RESTART-XON"); break; default: sprintf(nfrontp, " %d (unknown)", pointer[1]); } nfrontp += strlen(nfrontp); for (i = 2; i < length; i++) { sprintf(nfrontp, " ?%d?", pointer[i]); nfrontp += strlen(nfrontp); } break; case TELOPT_NAWS: sprintf(nfrontp, "NAWS"); nfrontp += strlen(nfrontp); if (length < 2) { sprintf(nfrontp, " (empty suboption??\?)"); nfrontp += strlen(nfrontp); break; } if (length == 2) { sprintf(nfrontp, " ?%d?", pointer[1]); nfrontp += strlen(nfrontp); break; } sprintf(nfrontp, " %d %d (%d)", pointer[1], pointer[2], (int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2]))); nfrontp += strlen(nfrontp); if (length == 4) { sprintf(nfrontp, " ?%d?", pointer[3]); nfrontp += strlen(nfrontp); break; } sprintf(nfrontp, " %d %d (%d)", pointer[3], pointer[4], (int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4]))); nfrontp += strlen(nfrontp); for (i = 5; i < length; i++) { sprintf(nfrontp, " ?%d?", pointer[i]); nfrontp += strlen(nfrontp); } break; case TELOPT_LINEMODE: sprintf(nfrontp, "LINEMODE "); nfrontp += strlen(nfrontp); if (length < 2) { sprintf(nfrontp, " (empty suboption??\?)"); nfrontp += strlen(nfrontp); break; } switch (pointer[1]) { case WILL: sprintf(nfrontp, "WILL "); goto common; case WONT: sprintf(nfrontp, "WONT "); goto common; case DO: sprintf(nfrontp, "DO "); goto common; case DONT: sprintf(nfrontp, "DONT "); common: nfrontp += strlen(nfrontp); if (length < 3) { sprintf(nfrontp, "(no option??\?)"); nfrontp += strlen(nfrontp); break; } switch (pointer[2]) { case LM_FORWARDMASK: sprintf(nfrontp, "Forward Mask"); nfrontp += strlen(nfrontp); for (i = 3; i < length; i++) { sprintf(nfrontp, " %x", pointer[i]); nfrontp += strlen(nfrontp); } break; default: sprintf(nfrontp, "%d (unknown)", pointer[2]); nfrontp += strlen(nfrontp); for (i = 3; i < length; i++) { sprintf(nfrontp, " %d", pointer[i]); nfrontp += strlen(nfrontp); } break; } break; case LM_SLC: sprintf(nfrontp, "SLC"); nfrontp += strlen(nfrontp); for (i = 2; i < length - 2; i += 3) { if (SLC_NAME_OK(pointer[i+SLC_FUNC])) sprintf(nfrontp, " %s", SLC_NAME(pointer[i+SLC_FUNC])); else sprintf(nfrontp, " %d", pointer[i+SLC_FUNC]); nfrontp += strlen(nfrontp); switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) { case SLC_NOSUPPORT: sprintf(nfrontp, " NOSUPPORT"); break; case SLC_CANTCHANGE: sprintf(nfrontp, " CANTCHANGE"); break; case SLC_VARIABLE: sprintf(nfrontp, " VARIABLE"); break; case SLC_DEFAULT: sprintf(nfrontp, " DEFAULT"); break; } nfrontp += strlen(nfrontp); sprintf(nfrontp, "%s%s%s", pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "", pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "", pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : ""); nfrontp += strlen(nfrontp); if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN| SLC_FLUSHOUT| SLC_LEVELBITS)) { sprintf(nfrontp, "(0x%x)", pointer[i+SLC_FLAGS]); nfrontp += strlen(nfrontp); } sprintf(nfrontp, " %d;", pointer[i+SLC_VALUE]); nfrontp += strlen(nfrontp); if ((pointer[i+SLC_VALUE] == IAC) && (pointer[i+SLC_VALUE+1] == IAC)) i++; } for (; i < length; i++) { sprintf(nfrontp, " ?%d?", pointer[i]); nfrontp += strlen(nfrontp); } break; case LM_MODE: sprintf(nfrontp, "MODE "); nfrontp += strlen(nfrontp); if (length < 3) { sprintf(nfrontp, "(no mode??\?)"); nfrontp += strlen(nfrontp); break; } { char tbuf[32]; sprintf(tbuf, "%s%s%s%s%s", pointer[2]&MODE_EDIT ? "|EDIT" : "", pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "", pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "", pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "", pointer[2]&MODE_ACK ? "|ACK" : ""); sprintf(nfrontp, "%s", tbuf[1] ? &tbuf[1] : "0"); nfrontp += strlen(nfrontp); } if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) { sprintf(nfrontp, " (0x%x)", pointer[2]); nfrontp += strlen(nfrontp); } for (i = 3; i < length; i++) { sprintf(nfrontp, " ?0x%x?", pointer[i]); nfrontp += strlen(nfrontp); } break; default: sprintf(nfrontp, "%d (unknown)", pointer[1]); nfrontp += strlen(nfrontp); for (i = 2; i < length; i++) { sprintf(nfrontp, " %d", pointer[i]); nfrontp += strlen(nfrontp); } } break; case TELOPT_STATUS: { register char *cp; register int j, k; sprintf(nfrontp, "STATUS"); nfrontp += strlen(nfrontp); switch (pointer[1]) { default: if (pointer[1] == TELQUAL_SEND) sprintf(nfrontp, " SEND"); else sprintf(nfrontp, " %d (unknown)", pointer[1]); nfrontp += strlen(nfrontp); for (i = 2; i < length; i++) { sprintf(nfrontp, " ?%d?", pointer[i]); nfrontp += strlen(nfrontp); } break; case TELQUAL_IS: sprintf(nfrontp, " IS\r\n"); nfrontp += strlen(nfrontp); for (i = 2; i < length; i++) { switch(pointer[i]) { case DO: cp = "DO"; goto common2; case DONT: cp = "DONT"; goto common2; case WILL: cp = "WILL"; goto common2; case WONT: cp = "WONT"; goto common2; common2: i++; if (TELOPT_OK(pointer[i])) sprintf(nfrontp, " %s %s", cp, TELOPT(pointer[i])); else sprintf(nfrontp, " %s %d", cp, pointer[i]); nfrontp += strlen(nfrontp); sprintf(nfrontp, "\r\n"); nfrontp += strlen(nfrontp); break; case SB: sprintf(nfrontp, " SB "); nfrontp += strlen(nfrontp); i++; j = k = i; while (j < length) { if (pointer[j] == SE) { if (j+1 == length) break; if (pointer[j+1] == SE) j++; else break; } pointer[k++] = pointer[j++]; } printsub(0, &pointer[i], k - i); if (i < length) { sprintf(nfrontp, " SE"); nfrontp += strlen(nfrontp); i = j; } else i = j - 1; sprintf(nfrontp, "\r\n"); nfrontp += strlen(nfrontp); break; default: sprintf(nfrontp, " %d", pointer[i]); nfrontp += strlen(nfrontp); break; } } break; } break; } case TELOPT_XDISPLOC: sprintf(nfrontp, "X-DISPLAY-LOCATION "); nfrontp += strlen(nfrontp); switch (pointer[1]) { case TELQUAL_IS: sprintf(nfrontp, "IS \"%.*s\"", length-2, (char *)pointer+2); break; case TELQUAL_SEND: sprintf(nfrontp, "SEND"); break; default: sprintf(nfrontp, "- unknown qualifier %d (0x%x).", pointer[1], pointer[1]); } nfrontp += strlen(nfrontp); break; case TELOPT_NEW_ENVIRON: sprintf(nfrontp, "NEW-ENVIRON "); goto env_common1; case TELOPT_OLD_ENVIRON: sprintf(nfrontp, "OLD-ENVIRON"); env_common1: nfrontp += strlen(nfrontp); switch (pointer[1]) { case TELQUAL_IS: sprintf(nfrontp, "IS "); goto env_common; case TELQUAL_SEND: sprintf(nfrontp, "SEND "); goto env_common; case TELQUAL_INFO: sprintf(nfrontp, "INFO "); env_common: nfrontp += strlen(nfrontp); { register int noquote = 2; for (i = 2; i < length; i++ ) { switch (pointer[i]) { case NEW_ENV_VAR: sprintf(nfrontp, "\" VAR " + noquote); nfrontp += strlen(nfrontp); noquote = 2; break; case NEW_ENV_VALUE: sprintf(nfrontp, "\" VALUE " + noquote); nfrontp += strlen(nfrontp); noquote = 2; break; case ENV_ESC: sprintf(nfrontp, "\" ESC " + noquote); nfrontp += strlen(nfrontp); noquote = 2; break; case ENV_USERVAR: sprintf(nfrontp, "\" USERVAR " + noquote); nfrontp += strlen(nfrontp); noquote = 2; break; default: def_case: if (isprint(pointer[i]) && pointer[i] != '"') { if (noquote) { *nfrontp++ = '"'; noquote = 0; } *nfrontp++ = pointer[i]; } else { sprintf(nfrontp, "\" %03o " + noquote, pointer[i]); nfrontp += strlen(nfrontp); noquote = 2; } break; } } if (!noquote) *nfrontp++ = '"'; break; } } break; #if defined(AUTHENTICATION) case TELOPT_AUTHENTICATION: sprintf(nfrontp, "AUTHENTICATION"); nfrontp += strlen(nfrontp); if (length < 2) { sprintf(nfrontp, " (empty suboption??\?)"); nfrontp += strlen(nfrontp); break; } switch (pointer[1]) { case TELQUAL_REPLY: case TELQUAL_IS: sprintf(nfrontp, " %s ", (pointer[1] == TELQUAL_IS) ? "IS" : "REPLY"); nfrontp += strlen(nfrontp); if (AUTHTYPE_NAME_OK(pointer[2])) sprintf(nfrontp, "%s ", AUTHTYPE_NAME(pointer[2])); else sprintf(nfrontp, "%d ", pointer[2]); nfrontp += strlen(nfrontp); if (length < 3) { sprintf(nfrontp, "(partial suboption??\?)"); nfrontp += strlen(nfrontp); break; } sprintf(nfrontp, "%s|%s", ((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ? "CLIENT" : "SERVER", ((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ? "MUTUAL" : "ONE-WAY"); nfrontp += strlen(nfrontp); auth_printsub(&pointer[1], length - 1, buf, sizeof(buf)); sprintf(nfrontp, "%s", buf); nfrontp += strlen(nfrontp); break; case TELQUAL_SEND: i = 2; sprintf(nfrontp, " SEND "); nfrontp += strlen(nfrontp); while (i < length) { if (AUTHTYPE_NAME_OK(pointer[i])) sprintf(nfrontp, "%s ", AUTHTYPE_NAME(pointer[i])); else sprintf(nfrontp, "%d ", pointer[i]); nfrontp += strlen(nfrontp); if (++i >= length) { sprintf(nfrontp, "(partial suboption??\?)"); nfrontp += strlen(nfrontp); break; } sprintf(nfrontp, "%s|%s ", ((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ? "CLIENT" : "SERVER", ((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ? "MUTUAL" : "ONE-WAY"); nfrontp += strlen(nfrontp); ++i; } break; case TELQUAL_NAME: i = 2; sprintf(nfrontp, " NAME \""); nfrontp += strlen(nfrontp); while (i < length) *nfrontp += pointer[i++]; *nfrontp += '"'; break; default: for (i = 2; i < length; i++) { sprintf(nfrontp, " ?%d?", pointer[i]); nfrontp += strlen(nfrontp); } break; } break; #endif default: if (TELOPT_OK(pointer[0])) sprintf(nfrontp, "%s (unknown)", TELOPT(pointer[0])); else sprintf(nfrontp, "%d (unknown)", pointer[i]); nfrontp += strlen(nfrontp); for (i = 1; i < length; i++) { sprintf(nfrontp, " %d", pointer[i]); nfrontp += strlen(nfrontp); } break; } sprintf(nfrontp, "\r\n"); nfrontp += strlen(nfrontp); } /* * Dump a data buffer in hex and ascii to the output data stream. */ void printdata(tag, ptr, cnt) register char *tag; register char *ptr; register int cnt; { register int i; char xbuf[30]; while (cnt) { /* flush net output buffer if no room for new data) */ if ((&netobuf[BUFSIZ] - nfrontp) < 80) { netflush(); } /* add a line of output */ sprintf(nfrontp, "%s: ", tag); nfrontp += strlen(nfrontp); for (i = 0; i < 20 && cnt; i++) { sprintf(nfrontp, "%02x", *ptr); nfrontp += strlen(nfrontp); if (isprint(*ptr)) { xbuf[i] = *ptr; } else { xbuf[i] = '.'; } if (i % 2) { *nfrontp = ' '; nfrontp++; } cnt--; ptr++; } xbuf[i] = '\0'; sprintf(nfrontp, " %s\r\n", xbuf ); nfrontp += strlen(nfrontp); } } #endif /* DIAGNOSTICS */