Home | History | Annotate | Line # | Download | only in telnetd
utility.c revision 1.2
      1 /*
      2  * Copyright (c) 1989 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /*static char sccsid[] = "from: @(#)utility.c	5.8 (Berkeley) 3/22/91";*/
     36 static char rcsid[] = "$Id: utility.c,v 1.2 1993/08/01 18:29:02 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 #define PRINTOPTIONS
     40 #include "telnetd.h"
     41 
     42 /*
     43  * utility functions performing io related tasks
     44  */
     45 
     46 /*
     47  * ttloop
     48  *
     49  *	A small subroutine to flush the network output buffer, get some data
     50  * from the network, and pass it through the telnet state machine.  We
     51  * also flush the pty input buffer (by dropping its data) if it becomes
     52  * too full.
     53  */
     54 
     55     void
     56 ttloop()
     57 {
     58     void netflush();
     59 
     60     DIAG(TD_REPORT, {sprintf(nfrontp, "td: ttloop\r\n");
     61 		     nfrontp += strlen(nfrontp);});
     62     if (nfrontp-nbackp) {
     63 	netflush();
     64     }
     65     ncc = read(net, netibuf, sizeof netibuf);
     66     if (ncc < 0) {
     67 	syslog(LOG_INFO, "ttloop:  read: %m\n");
     68 	exit(1);
     69     } else if (ncc == 0) {
     70 	syslog(LOG_INFO, "ttloop:  peer died: %m\n");
     71 	exit(1);
     72     }
     73     DIAG(TD_REPORT, {sprintf(nfrontp, "td: ttloop read %d chars\r\n", ncc);
     74 		     nfrontp += strlen(nfrontp);});
     75     netip = netibuf;
     76     telrcv();			/* state machine */
     77     if (ncc > 0) {
     78 	pfrontp = pbackp = ptyobuf;
     79 	telrcv();
     80     }
     81 }  /* end of ttloop */
     82 
     83 /*
     84  * Check a descriptor to see if out of band data exists on it.
     85  */
     86     int
     87 stilloob(s)
     88     int	s;		/* socket number */
     89 {
     90     static struct timeval timeout = { 0 };
     91     fd_set	excepts;
     92     int value;
     93 
     94     do {
     95 	FD_ZERO(&excepts);
     96 	FD_SET(s, &excepts);
     97 	value = select(s+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout);
     98     } while ((value == -1) && (errno == EINTR));
     99 
    100     if (value < 0) {
    101 	fatalperror(pty, "select");
    102     }
    103     if (FD_ISSET(s, &excepts)) {
    104 	return 1;
    105     } else {
    106 	return 0;
    107     }
    108 }
    109 
    110 	void
    111 ptyflush()
    112 {
    113 	int n;
    114 
    115 	if ((n = pfrontp - pbackp) > 0) {
    116 		DIAG((TD_REPORT | TD_PTYDATA),
    117 			{ sprintf(nfrontp, "td: ptyflush %d chars\r\n", n);
    118 			  nfrontp += strlen(nfrontp); });
    119 		DIAG(TD_PTYDATA, printdata("pd", pbackp, n));
    120 		n = write(pty, pbackp, n);
    121 	}
    122 	if (n < 0) {
    123 		if (errno == EWOULDBLOCK || errno == EINTR)
    124 			return;
    125 		cleanup(0);
    126 	}
    127 	pbackp += n;
    128 	if (pbackp == pfrontp)
    129 		pbackp = pfrontp = ptyobuf;
    130 }
    131 
    132 /*
    133  * nextitem()
    134  *
    135  *	Return the address of the next "item" in the TELNET data
    136  * stream.  This will be the address of the next character if
    137  * the current address is a user data character, or it will
    138  * be the address of the character following the TELNET command
    139  * if the current address is a TELNET IAC ("I Am a Command")
    140  * character.
    141  */
    142     char *
    143 nextitem(current)
    144     char	*current;
    145 {
    146     if ((*current&0xff) != IAC) {
    147 	return current+1;
    148     }
    149     switch (*(current+1)&0xff) {
    150     case DO:
    151     case DONT:
    152     case WILL:
    153     case WONT:
    154 	return current+3;
    155     case SB:		/* loop forever looking for the SE */
    156 	{
    157 	    register char *look = current+2;
    158 
    159 	    for (;;) {
    160 		if ((*look++&0xff) == IAC) {
    161 		    if ((*look++&0xff) == SE) {
    162 			return look;
    163 		    }
    164 		}
    165 	    }
    166 	}
    167     default:
    168 	return current+2;
    169     }
    170 }  /* end of nextitem */
    171 
    172 
    173 /*
    174  * netclear()
    175  *
    176  *	We are about to do a TELNET SYNCH operation.  Clear
    177  * the path to the network.
    178  *
    179  *	Things are a bit tricky since we may have sent the first
    180  * byte or so of a previous TELNET command into the network.
    181  * So, we have to scan the network buffer from the beginning
    182  * until we are up to where we want to be.
    183  *
    184  *	A side effect of what we do, just to keep things
    185  * simple, is to clear the urgent data pointer.  The principal
    186  * caller should be setting the urgent data pointer AFTER calling
    187  * us in any case.
    188  */
    189     void
    190 netclear()
    191 {
    192     register char *thisitem, *next;
    193     char *good;
    194 #define	wewant(p)	((nfrontp > p) && ((*p&0xff) == IAC) && \
    195 				((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
    196 
    197 #if	defined(ENCRYPT)
    198     thisitem = nclearto > netobuf ? nclearto : netobuf;
    199 #else
    200     thisitem = netobuf;
    201 #endif
    202 
    203     while ((next = nextitem(thisitem)) <= nbackp) {
    204 	thisitem = next;
    205     }
    206 
    207     /* Now, thisitem is first before/at boundary. */
    208 
    209 #if	defined(ENCRYPT)
    210     good = nclearto > netobuf ? nclearto : netobuf;
    211 #else
    212     good = netobuf;	/* where the good bytes go */
    213 #endif
    214 
    215     while (nfrontp > thisitem) {
    216 	if (wewant(thisitem)) {
    217 	    int length;
    218 
    219 	    next = thisitem;
    220 	    do {
    221 		next = nextitem(next);
    222 	    } while (wewant(next) && (nfrontp > next));
    223 	    length = next-thisitem;
    224 	    bcopy(thisitem, good, length);
    225 	    good += length;
    226 	    thisitem = next;
    227 	} else {
    228 	    thisitem = nextitem(thisitem);
    229 	}
    230     }
    231 
    232     nbackp = netobuf;
    233     nfrontp = good;		/* next byte to be sent */
    234     neturg = 0;
    235 }  /* end of netclear */
    236 
    237 /*
    238  *  netflush
    239  *		Send as much data as possible to the network,
    240  *	handling requests for urgent data.
    241  */
    242     void
    243 netflush()
    244 {
    245     int n;
    246     extern int not42;
    247 
    248     if ((n = nfrontp - nbackp) > 0) {
    249 	DIAG(TD_REPORT,
    250 	    { sprintf(nfrontp, "td: netflush %d chars\r\n", n);
    251 	      n += strlen(nfrontp);  /* get count first */
    252 	      nfrontp += strlen(nfrontp);  /* then move pointer */
    253 	    });
    254 #if	defined(ENCRYPT)
    255 	if (encrypt_output) {
    256 		char *s = nclearto ? nclearto : nbackp;
    257 		if (nfrontp - s > 0) {
    258 			(*encrypt_output)((unsigned char *)s, nfrontp-s);
    259 			nclearto = nfrontp;
    260 		}
    261 	}
    262 #endif
    263 	/*
    264 	 * if no urgent data, or if the other side appears to be an
    265 	 * old 4.2 client (and thus unable to survive TCP urgent data),
    266 	 * write the entire buffer in non-OOB mode.
    267 	 */
    268 	if ((neturg == 0) || (not42 == 0)) {
    269 	    n = write(net, nbackp, n);	/* normal write */
    270 	} else {
    271 	    n = neturg - nbackp;
    272 	    /*
    273 	     * In 4.2 (and 4.3) systems, there is some question about
    274 	     * what byte in a sendOOB operation is the "OOB" data.
    275 	     * To make ourselves compatible, we only send ONE byte
    276 	     * out of band, the one WE THINK should be OOB (though
    277 	     * we really have more the TCP philosophy of urgent data
    278 	     * rather than the Unix philosophy of OOB data).
    279 	     */
    280 	    if (n > 1) {
    281 		n = send(net, nbackp, n-1, 0);	/* send URGENT all by itself */
    282 	    } else {
    283 		n = send(net, nbackp, n, MSG_OOB);	/* URGENT data */
    284 	    }
    285 	}
    286     }
    287     if (n < 0) {
    288 	if (errno == EWOULDBLOCK || errno == EINTR)
    289 		return;
    290 	cleanup(0);
    291     }
    292     nbackp += n;
    293 #if	defined(ENCRYPT)
    294     if (nbackp > nclearto)
    295 	nclearto = 0;
    296 #endif
    297     if (nbackp >= neturg) {
    298 	neturg = 0;
    299     }
    300     if (nbackp == nfrontp) {
    301 	nbackp = nfrontp = netobuf;
    302 #if	defined(ENCRYPT)
    303 	nclearto = 0;
    304 #endif
    305     }
    306     return;
    307 }  /* end of netflush */
    308 
    309 
    310 /*
    311  * writenet
    312  *
    313  * Just a handy little function to write a bit of raw data to the net.
    314  * It will force a transmit of the buffer if necessary
    315  *
    316  * arguments
    317  *    ptr - A pointer to a character string to write
    318  *    len - How many bytes to write
    319  */
    320 	void
    321 writenet(ptr, len)
    322 	register unsigned char *ptr;
    323 	register int len;
    324 {
    325 	/* flush buffer if no room for new data) */
    326 	if ((&netobuf[BUFSIZ] - nfrontp) < len) {
    327 		/* if this fails, don't worry, buffer is a little big */
    328 		netflush();
    329 	}
    330 
    331 	bcopy(ptr, nfrontp, len);
    332 	nfrontp += len;
    333 
    334 }  /* end of writenet */
    335 
    336 
    337 /*
    338  * miscellaneous functions doing a variety of little jobs follow ...
    339  */
    340 
    341 
    342 	void
    343 fatal(f, msg)
    344 	int f;
    345 	char *msg;
    346 {
    347 	char buf[BUFSIZ];
    348 
    349 	(void) sprintf(buf, "telnetd: %s.\r\n", msg);
    350 #if	defined(ENCRYPT)
    351 	if (encrypt_output) {
    352 		/*
    353 		 * Better turn off encryption first....
    354 		 * Hope it flushes...
    355 		 */
    356 		encrypt_send_end();
    357 		netflush();
    358 	}
    359 #endif
    360 	(void) write(f, buf, (int)strlen(buf));
    361 	sleep(1);	/*XXX*/
    362 	exit(1);
    363 }
    364 
    365 	void
    366 fatalperror(f, msg)
    367 	int f;
    368 	char *msg;
    369 {
    370 	char buf[BUFSIZ], *strerror();
    371 
    372 	(void) sprintf(buf, "%s: %s\r\n", msg, strerror(errno));
    373 	fatal(f, buf);
    374 }
    375 
    376 char editedhost[32];
    377 
    378 	void
    379 edithost(pat, host)
    380 	register char *pat;
    381 	register char *host;
    382 {
    383 	register char *res = editedhost;
    384 	char *strncpy();
    385 
    386 	if (!pat)
    387 		pat = "";
    388 	while (*pat) {
    389 		switch (*pat) {
    390 
    391 		case '#':
    392 			if (*host)
    393 				host++;
    394 			break;
    395 
    396 		case '@':
    397 			if (*host)
    398 				*res++ = *host++;
    399 			break;
    400 
    401 		default:
    402 			*res++ = *pat;
    403 			break;
    404 		}
    405 		if (res == &editedhost[sizeof editedhost - 1]) {
    406 			*res = '\0';
    407 			return;
    408 		}
    409 		pat++;
    410 	}
    411 	if (*host)
    412 		(void) strncpy(res, host,
    413 				sizeof editedhost - (res - editedhost) -1);
    414 	else
    415 		*res = '\0';
    416 	editedhost[sizeof editedhost - 1] = '\0';
    417 }
    418 
    419 static char *putlocation;
    420 
    421 	void
    422 putstr(s)
    423 	register char *s;
    424 {
    425 
    426 	while (*s)
    427 		putchr(*s++);
    428 }
    429 
    430 	void
    431 putchr(cc)
    432 	int cc;
    433 {
    434 	*putlocation++ = cc;
    435 }
    436 
    437 /*
    438  * This is split on two lines so that SCCS will not see the M
    439  * between two % signs and expand it...
    440  */
    441 static char fmtstr[] = { "%l:%M\
    442 %P on %A, %d %B %Y" };
    443 
    444 	void
    445 putf(cp, where)
    446 	register char *cp;
    447 	char *where;
    448 {
    449 	char *slash;
    450 	time_t t;
    451 	char db[100];
    452 	extern char *rindex();
    453 
    454 	putlocation = where;
    455 
    456 	while (*cp) {
    457 		if (*cp != '%') {
    458 			putchr(*cp++);
    459 			continue;
    460 		}
    461 		switch (*++cp) {
    462 
    463 		case 't':
    464 			slash = rindex(line, '/');
    465 			if (slash == (char *) 0)
    466 				putstr(line);
    467 			else
    468 				putstr(&slash[1]);
    469 			break;
    470 
    471 		case 'h':
    472 			putstr(editedhost);
    473 			break;
    474 
    475 		case 'd':
    476 			(void)time(&t);
    477 			(void)strftime(db, sizeof(db), fmtstr, localtime(&t));
    478 			putstr(db);
    479 			break;
    480 
    481 		case '%':
    482 			putchr('%');
    483 			break;
    484 		}
    485 		cp++;
    486 	}
    487 }
    488 
    489 #ifdef DIAGNOSTICS
    490 /*
    491  * Print telnet options and commands in plain text, if possible.
    492  */
    493 	void
    494 printoption(fmt, option)
    495 	register char *fmt;
    496 	register int option;
    497 {
    498 	if (TELOPT_OK(option))
    499 		sprintf(nfrontp, "%s %s\r\n", fmt, TELOPT(option));
    500 	else if (TELCMD_OK(option))
    501 		sprintf(nfrontp, "%s %s\r\n", fmt, TELCMD(option));
    502 	else
    503 		sprintf(nfrontp, "%s %d\r\n", fmt, option);
    504 	nfrontp += strlen(nfrontp);
    505 	return;
    506 }
    507 
    508     void
    509 printsub(direction, pointer, length)
    510     char		direction;	/* '<' or '>' */
    511     unsigned char	*pointer;	/* where suboption data sits */
    512     int			length;		/* length of suboption data */
    513 {
    514     register int i;
    515     char buf[512];
    516 
    517         if (!(diagnostic & TD_OPTIONS))
    518 		return;
    519 
    520 	if (direction) {
    521 	    sprintf(nfrontp, "td: %s suboption ",
    522 					direction == '<' ? "recv" : "send");
    523 	    nfrontp += strlen(nfrontp);
    524 	    if (length >= 3) {
    525 		register int j;
    526 
    527 		i = pointer[length-2];
    528 		j = pointer[length-1];
    529 
    530 		if (i != IAC || j != SE) {
    531 		    sprintf(nfrontp, "(terminated by ");
    532 		    nfrontp += strlen(nfrontp);
    533 		    if (TELOPT_OK(i))
    534 			sprintf(nfrontp, "%s ", TELOPT(i));
    535 		    else if (TELCMD_OK(i))
    536 			sprintf(nfrontp, "%s ", TELCMD(i));
    537 		    else
    538 			sprintf(nfrontp, "%d ", i);
    539 		    nfrontp += strlen(nfrontp);
    540 		    if (TELOPT_OK(j))
    541 			sprintf(nfrontp, "%s", TELOPT(j));
    542 		    else if (TELCMD_OK(j))
    543 			sprintf(nfrontp, "%s", TELCMD(j));
    544 		    else
    545 			sprintf(nfrontp, "%d", j);
    546 		    nfrontp += strlen(nfrontp);
    547 		    sprintf(nfrontp, ", not IAC SE!) ");
    548 		    nfrontp += strlen(nfrontp);
    549 		}
    550 	    }
    551 	    length -= 2;
    552 	}
    553 	if (length < 1) {
    554 	    sprintf(nfrontp, "(Empty suboption???)");
    555 	    nfrontp += strlen(nfrontp);
    556 	    return;
    557 	}
    558 	switch (pointer[0]) {
    559 	case TELOPT_TTYPE:
    560 	    sprintf(nfrontp, "TERMINAL-TYPE ");
    561 	    nfrontp += strlen(nfrontp);
    562 	    switch (pointer[1]) {
    563 	    case TELQUAL_IS:
    564 		sprintf(nfrontp, "IS \"%.*s\"", length-2, (char *)pointer+2);
    565 		break;
    566 	    case TELQUAL_SEND:
    567 		sprintf(nfrontp, "SEND");
    568 		break;
    569 	    default:
    570 		sprintf(nfrontp,
    571 				"- unknown qualifier %d (0x%x).",
    572 				pointer[1], pointer[1]);
    573 	    }
    574 	    nfrontp += strlen(nfrontp);
    575 	    break;
    576 	case TELOPT_TSPEED:
    577 	    sprintf(nfrontp, "TERMINAL-SPEED");
    578 	    nfrontp += strlen(nfrontp);
    579 	    if (length < 2) {
    580 		sprintf(nfrontp, " (empty suboption???)");
    581 		nfrontp += strlen(nfrontp);
    582 		break;
    583 	    }
    584 	    switch (pointer[1]) {
    585 	    case TELQUAL_IS:
    586 		sprintf(nfrontp, " IS %.*s", length-2, (char *)pointer+2);
    587 		nfrontp += strlen(nfrontp);
    588 		break;
    589 	    default:
    590 		if (pointer[1] == 1)
    591 		    sprintf(nfrontp, " SEND");
    592 		else
    593 		    sprintf(nfrontp, " %d (unknown)", pointer[1]);
    594 		nfrontp += strlen(nfrontp);
    595 		for (i = 2; i < length; i++) {
    596 		    sprintf(nfrontp, " ?%d?", pointer[i]);
    597 		    nfrontp += strlen(nfrontp);
    598 		}
    599 		break;
    600 	    }
    601 	    break;
    602 
    603 	case TELOPT_LFLOW:
    604 	    sprintf(nfrontp, "TOGGLE-FLOW-CONTROL");
    605 	    nfrontp += strlen(nfrontp);
    606 	    if (length < 2) {
    607 		sprintf(nfrontp, " (empty suboption???)");
    608 		nfrontp += strlen(nfrontp);
    609 		break;
    610 	    }
    611 	    switch (pointer[1]) {
    612 	    case 0:
    613 		sprintf(nfrontp, " OFF"); break;
    614 	    case 1:
    615 		sprintf(nfrontp, " ON"); break;
    616 	    default:
    617 		sprintf(nfrontp, " %d (unknown)", pointer[1]);
    618 	    }
    619 	    nfrontp += strlen(nfrontp);
    620 	    for (i = 2; i < length; i++) {
    621 		sprintf(nfrontp, " ?%d?", pointer[i]);
    622 		nfrontp += strlen(nfrontp);
    623 	    }
    624 	    break;
    625 
    626 	case TELOPT_NAWS:
    627 	    sprintf(nfrontp, "NAWS");
    628 	    nfrontp += strlen(nfrontp);
    629 	    if (length < 2) {
    630 		sprintf(nfrontp, " (empty suboption???)");
    631 		nfrontp += strlen(nfrontp);
    632 		break;
    633 	    }
    634 	    if (length == 2) {
    635 		sprintf(nfrontp, " ?%d?", pointer[1]);
    636 		nfrontp += strlen(nfrontp);
    637 		break;
    638 	    }
    639 	    sprintf(nfrontp, " %d %d (%d)",
    640 		pointer[1], pointer[2],
    641 		(int)((((unsigned int)pointer[1])<<8)|((unsigned int)pointer[2])));
    642 	    nfrontp += strlen(nfrontp);
    643 	    if (length == 4) {
    644 		sprintf(nfrontp, " ?%d?", pointer[3]);
    645 		nfrontp += strlen(nfrontp);
    646 		break;
    647 	    }
    648 	    sprintf(nfrontp, " %d %d (%d)",
    649 		pointer[3], pointer[4],
    650 		(int)((((unsigned int)pointer[3])<<8)|((unsigned int)pointer[4])));
    651 	    nfrontp += strlen(nfrontp);
    652 	    for (i = 5; i < length; i++) {
    653 		sprintf(nfrontp, " ?%d?", pointer[i]);
    654 		nfrontp += strlen(nfrontp);
    655 	    }
    656 	    break;
    657 
    658 	case TELOPT_LINEMODE:
    659 	    sprintf(nfrontp, "LINEMODE ");
    660 	    nfrontp += strlen(nfrontp);
    661 	    if (length < 2) {
    662 		sprintf(nfrontp, " (empty suboption???)");
    663 		nfrontp += strlen(nfrontp);
    664 		break;
    665 	    }
    666 	    switch (pointer[1]) {
    667 	    case WILL:
    668 		sprintf(nfrontp, "WILL ");
    669 		goto common;
    670 	    case WONT:
    671 		sprintf(nfrontp, "WONT ");
    672 		goto common;
    673 	    case DO:
    674 		sprintf(nfrontp, "DO ");
    675 		goto common;
    676 	    case DONT:
    677 		sprintf(nfrontp, "DONT ");
    678 	    common:
    679 		nfrontp += strlen(nfrontp);
    680 		if (length < 3) {
    681 		    sprintf(nfrontp, "(no option???)");
    682 		    nfrontp += strlen(nfrontp);
    683 		    break;
    684 		}
    685 		switch (pointer[2]) {
    686 		case LM_FORWARDMASK:
    687 		    sprintf(nfrontp, "Forward Mask");
    688 		    nfrontp += strlen(nfrontp);
    689 		    for (i = 3; i < length; i++) {
    690 			sprintf(nfrontp, " %x", pointer[i]);
    691 			nfrontp += strlen(nfrontp);
    692 		    }
    693 		    break;
    694 		default:
    695 		    sprintf(nfrontp, "%d (unknown)", pointer[2]);
    696 		    nfrontp += strlen(nfrontp);
    697 		    for (i = 3; i < length; i++) {
    698 			sprintf(nfrontp, " %d", pointer[i]);
    699 			nfrontp += strlen(nfrontp);
    700 		    }
    701 		    break;
    702 		}
    703 		break;
    704 
    705 	    case LM_SLC:
    706 		sprintf(nfrontp, "SLC");
    707 		nfrontp += strlen(nfrontp);
    708 		for (i = 2; i < length - 2; i += 3) {
    709 		    if (SLC_NAME_OK(pointer[i+SLC_FUNC]))
    710 			sprintf(nfrontp, " %s", SLC_NAME(pointer[i+SLC_FUNC]));
    711 		    else
    712 			sprintf(nfrontp, " %d", pointer[i+SLC_FUNC]);
    713 		    nfrontp += strlen(nfrontp);
    714 		    switch (pointer[i+SLC_FLAGS]&SLC_LEVELBITS) {
    715 		    case SLC_NOSUPPORT:
    716 			sprintf(nfrontp, " NOSUPPORT"); break;
    717 		    case SLC_CANTCHANGE:
    718 			sprintf(nfrontp, " CANTCHANGE"); break;
    719 		    case SLC_VARIABLE:
    720 			sprintf(nfrontp, " VARIABLE"); break;
    721 		    case SLC_DEFAULT:
    722 			sprintf(nfrontp, " DEFAULT"); break;
    723 		    }
    724 		    nfrontp += strlen(nfrontp);
    725 		    sprintf(nfrontp, "%s%s%s",
    726 			pointer[i+SLC_FLAGS]&SLC_ACK ? "|ACK" : "",
    727 			pointer[i+SLC_FLAGS]&SLC_FLUSHIN ? "|FLUSHIN" : "",
    728 			pointer[i+SLC_FLAGS]&SLC_FLUSHOUT ? "|FLUSHOUT" : "");
    729 		    nfrontp += strlen(nfrontp);
    730 		    if (pointer[i+SLC_FLAGS]& ~(SLC_ACK|SLC_FLUSHIN|
    731 						SLC_FLUSHOUT| SLC_LEVELBITS)) {
    732 			sprintf(nfrontp, "(0x%x)", pointer[i+SLC_FLAGS]);
    733 			nfrontp += strlen(nfrontp);
    734 		    }
    735 		    sprintf(nfrontp, " %d;", pointer[i+SLC_VALUE]);
    736 		    nfrontp += strlen(nfrontp);
    737 		    if ((pointer[i+SLC_VALUE] == IAC) &&
    738 			(pointer[i+SLC_VALUE+1] == IAC))
    739 				i++;
    740 		}
    741 		for (; i < length; i++) {
    742 		    sprintf(nfrontp, " ?%d?", pointer[i]);
    743 		    nfrontp += strlen(nfrontp);
    744 		}
    745 		break;
    746 
    747 	    case LM_MODE:
    748 		sprintf(nfrontp, "MODE ");
    749 		nfrontp += strlen(nfrontp);
    750 		if (length < 3) {
    751 		    sprintf(nfrontp, "(no mode???)");
    752 		    nfrontp += strlen(nfrontp);
    753 		    break;
    754 		}
    755 		{
    756 		    char tbuf[32];
    757 		    sprintf(tbuf, "%s%s%s%s%s",
    758 			pointer[2]&MODE_EDIT ? "|EDIT" : "",
    759 			pointer[2]&MODE_TRAPSIG ? "|TRAPSIG" : "",
    760 			pointer[2]&MODE_SOFT_TAB ? "|SOFT_TAB" : "",
    761 			pointer[2]&MODE_LIT_ECHO ? "|LIT_ECHO" : "",
    762 			pointer[2]&MODE_ACK ? "|ACK" : "");
    763 		    sprintf(nfrontp, "%s", tbuf[1] ? &tbuf[1] : "0");
    764 		    nfrontp += strlen(nfrontp);
    765 		}
    766 		if (pointer[2]&~(MODE_EDIT|MODE_TRAPSIG|MODE_ACK)) {
    767 		    sprintf(nfrontp, " (0x%x)", pointer[2]);
    768 		    nfrontp += strlen(nfrontp);
    769 		}
    770 		for (i = 3; i < length; i++) {
    771 		    sprintf(nfrontp, " ?0x%x?", pointer[i]);
    772 		    nfrontp += strlen(nfrontp);
    773 		}
    774 		break;
    775 	    default:
    776 		sprintf(nfrontp, "%d (unknown)", pointer[1]);
    777 		nfrontp += strlen(nfrontp);
    778 		for (i = 2; i < length; i++) {
    779 		    sprintf(nfrontp, " %d", pointer[i]);
    780 		    nfrontp += strlen(nfrontp);
    781 		}
    782 	    }
    783 	    break;
    784 
    785 	case TELOPT_STATUS: {
    786 	    register char *cp;
    787 	    register int j, k;
    788 
    789 	    sprintf(nfrontp, "STATUS");
    790 	    nfrontp += strlen(nfrontp);
    791 
    792 	    switch (pointer[1]) {
    793 	    default:
    794 		if (pointer[1] == TELQUAL_SEND)
    795 		    sprintf(nfrontp, " SEND");
    796 		else
    797 		    sprintf(nfrontp, " %d (unknown)", pointer[1]);
    798 		nfrontp += strlen(nfrontp);
    799 		for (i = 2; i < length; i++) {
    800 		    sprintf(nfrontp, " ?%d?", pointer[i]);
    801 		    nfrontp += strlen(nfrontp);
    802 		}
    803 		break;
    804 	    case TELQUAL_IS:
    805 		sprintf(nfrontp, " IS\r\n");
    806 		nfrontp += strlen(nfrontp);
    807 
    808 		for (i = 2; i < length; i++) {
    809 		    switch(pointer[i]) {
    810 		    case DO:	cp = "DO"; goto common2;
    811 		    case DONT:	cp = "DONT"; goto common2;
    812 		    case WILL:	cp = "WILL"; goto common2;
    813 		    case WONT:	cp = "WONT"; goto common2;
    814 		    common2:
    815 			i++;
    816 			if (TELOPT_OK((int)pointer[i]))
    817 			    sprintf(nfrontp, " %s %s", cp, TELOPT(pointer[i]));
    818 			else
    819 			    sprintf(nfrontp, " %s %d", cp, pointer[i]);
    820 			nfrontp += strlen(nfrontp);
    821 
    822 			sprintf(nfrontp, "\r\n");
    823 			nfrontp += strlen(nfrontp);
    824 			break;
    825 
    826 		    case SB:
    827 			sprintf(nfrontp, " SB ");
    828 			nfrontp += strlen(nfrontp);
    829 			i++;
    830 			j = k = i;
    831 			while (j < length) {
    832 			    if (pointer[j] == SE) {
    833 				if (j+1 == length)
    834 				    break;
    835 				if (pointer[j+1] == SE)
    836 				    j++;
    837 				else
    838 				    break;
    839 			    }
    840 			    pointer[k++] = pointer[j++];
    841 			}
    842 			printsub(0, &pointer[i], k - i);
    843 			if (i < length) {
    844 			    sprintf(nfrontp, " SE");
    845 			    nfrontp += strlen(nfrontp);
    846 			    i = j;
    847 			} else
    848 			    i = j - 1;
    849 
    850 			sprintf(nfrontp, "\r\n");
    851 			nfrontp += strlen(nfrontp);
    852 
    853 			break;
    854 
    855 		    default:
    856 			sprintf(nfrontp, " %d", pointer[i]);
    857 			nfrontp += strlen(nfrontp);
    858 			break;
    859 		    }
    860 		}
    861 		break;
    862 	    }
    863 	    break;
    864 	  }
    865 
    866 	case TELOPT_XDISPLOC:
    867 	    sprintf(nfrontp, "X-DISPLAY-LOCATION ");
    868 	    nfrontp += strlen(nfrontp);
    869 	    switch (pointer[1]) {
    870 	    case TELQUAL_IS:
    871 		sprintf(nfrontp, "IS \"%.*s\"", length-2, (char *)pointer+2);
    872 		break;
    873 	    case TELQUAL_SEND:
    874 		sprintf(nfrontp, "SEND");
    875 		break;
    876 	    default:
    877 		sprintf(nfrontp, "- unknown qualifier %d (0x%x).",
    878 				pointer[1], pointer[1]);
    879 	    }
    880 	    nfrontp += strlen(nfrontp);
    881 	    break;
    882 
    883 	case TELOPT_ENVIRON:
    884 	    sprintf(nfrontp, "ENVIRON ");
    885 	    nfrontp += strlen(nfrontp);
    886 	    switch (pointer[1]) {
    887 	    case TELQUAL_IS:
    888 		sprintf(nfrontp, "IS ");
    889 		goto env_common;
    890 	    case TELQUAL_SEND:
    891 		sprintf(nfrontp, "SEND ");
    892 		goto env_common;
    893 	    case TELQUAL_INFO:
    894 		sprintf(nfrontp, "INFO ");
    895 	    env_common:
    896 	    nfrontp += strlen(nfrontp);
    897 		{
    898 		    register int noquote = 2;
    899 		    for (i = 2; i < length; i++ ) {
    900 			switch (pointer[i]) {
    901 			case ENV_VAR:
    902 			    if (pointer[1] == TELQUAL_SEND)
    903 				goto def_case;
    904 			    sprintf(nfrontp, "\" VAR " + noquote);
    905 			    nfrontp += strlen(nfrontp);
    906 			    noquote = 2;
    907 			    break;
    908 
    909 			case ENV_VALUE:
    910 			    sprintf(nfrontp, "\" VALUE " + noquote);
    911 			    nfrontp += strlen(nfrontp);
    912 			    noquote = 2;
    913 			    break;
    914 
    915 			case ENV_ESC:
    916 			    sprintf(nfrontp, "\" ESC " + noquote);
    917 			    nfrontp += strlen(nfrontp);
    918 			    noquote = 2;
    919 			    break;
    920 
    921 			default:
    922 			def_case:
    923 			    if (isprint(pointer[i]) && pointer[i] != '"') {
    924 				if (noquote) {
    925 				    *nfrontp++ = '"';
    926 				    noquote = 0;
    927 				}
    928 				*nfrontp++ = pointer[i];
    929 			    } else {
    930 				sprintf(nfrontp, "\" %03o " + noquote,
    931 							pointer[i]);
    932 				nfrontp += strlen(nfrontp);
    933 				noquote = 2;
    934 			    }
    935 			    break;
    936 			}
    937 		    }
    938 		    if (!noquote)
    939 			*nfrontp++ = '"';
    940 		    break;
    941 		}
    942 	    }
    943 	    break;
    944 
    945 #if	defined(AUTHENTICATE)
    946 	case TELOPT_AUTHENTICATION:
    947 	    sprintf(nfrontp, "AUTHENTICATION");
    948 	    nfrontp += strlen(nfrontp);
    949 
    950 	    if (length < 2) {
    951 		sprintf(nfrontp, " (empty suboption???)");
    952 		nfrontp += strlen(nfrontp);
    953 		break;
    954 	    }
    955 	    switch (pointer[1]) {
    956 	    case TELQUAL_REPLY:
    957 	    case TELQUAL_IS:
    958 		sprintf(nfrontp, " %s ", (pointer[1] == TELQUAL_IS) ?
    959 							"IS" : "REPLY");
    960 		nfrontp += strlen(nfrontp);
    961 		if (AUTHTYPE_NAME_OK(pointer[2]))
    962 		    sprintf(nfrontp, "%s ", AUTHTYPE_NAME(pointer[2]));
    963 		else
    964 		    sprintf(nfrontp, "%d ", pointer[2]);
    965 		nfrontp += strlen(nfrontp);
    966 		if (length < 3) {
    967 		    sprintf(nfrontp, "(partial suboption???)");
    968 		    nfrontp += strlen(nfrontp);
    969 		    break;
    970 		}
    971 		sprintf(nfrontp, "%s|%s",
    972 			((pointer[3] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
    973 			"CLIENT" : "SERVER",
    974 			((pointer[3] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
    975 			"MUTUAL" : "ONE-WAY");
    976 		nfrontp += strlen(nfrontp);
    977 
    978 		auth_printsub(&pointer[1], length - 1, buf, sizeof(buf));
    979 		sprintf(nfrontp, "%s", buf);
    980 		nfrontp += strlen(nfrontp);
    981 		break;
    982 
    983 	    case TELQUAL_SEND:
    984 		i = 2;
    985 		sprintf(nfrontp, " SEND ");
    986 		nfrontp += strlen(nfrontp);
    987 		while (i < length) {
    988 		    if (AUTHTYPE_NAME_OK(pointer[i]))
    989 			sprintf(nfrontp, "%s ", AUTHTYPE_NAME(pointer[i]));
    990 		    else
    991 			sprintf(nfrontp, "%d ", pointer[i]);
    992 		    nfrontp += strlen(nfrontp);
    993 		    if (++i >= length) {
    994 			sprintf(nfrontp, "(partial suboption???)");
    995 			nfrontp += strlen(nfrontp);
    996 			break;
    997 		    }
    998 		    sprintf(nfrontp, "%s|%s ",
    999 			((pointer[i] & AUTH_WHO_MASK) == AUTH_WHO_CLIENT) ?
   1000 							"CLIENT" : "SERVER",
   1001 			((pointer[i] & AUTH_HOW_MASK) == AUTH_HOW_MUTUAL) ?
   1002 							"MUTUAL" : "ONE-WAY");
   1003 		    nfrontp += strlen(nfrontp);
   1004 		    ++i;
   1005 		}
   1006 		break;
   1007 
   1008 	    case TELQUAL_NAME:
   1009 		i = 2;
   1010 		sprintf(nfrontp, " NAME \"");
   1011 		nfrontp += strlen(nfrontp);
   1012 		while (i < length)
   1013 		    *nfrontp += pointer[i++];
   1014 		*nfrontp += '"';
   1015 		break;
   1016 
   1017 	    default:
   1018 		    for (i = 2; i < length; i++) {
   1019 			sprintf(nfrontp, " ?%d?", pointer[i]);
   1020 			nfrontp += strlen(nfrontp);
   1021 		    }
   1022 		    break;
   1023 	    }
   1024 	    break;
   1025 #endif
   1026 
   1027 #if	defined(ENCRYPT)
   1028 	case TELOPT_ENCRYPT:
   1029 	    sprintf(nfrontp, "ENCRYPT");
   1030 	    nfrontp += strlen(nfrontp);
   1031 	    if (length < 2) {
   1032 		sprintf(nfrontp, " (empty suboption???)");
   1033 		nfrontp += strlen(nfrontp);
   1034 		break;
   1035 	    }
   1036 	    switch (pointer[1]) {
   1037 	    case ENCRYPT_START:
   1038 		sprintf(nfrontp, " START");
   1039 		nfrontp += strlen(nfrontp);
   1040 		break;
   1041 
   1042 	    case ENCRYPT_END:
   1043 		sprintf(nfrontp, " END");
   1044 		nfrontp += strlen(nfrontp);
   1045 		break;
   1046 
   1047 	    case ENCRYPT_REQSTART:
   1048 		sprintf(nfrontp, " REQUEST-START");
   1049 		nfrontp += strlen(nfrontp);
   1050 		break;
   1051 
   1052 	    case ENCRYPT_REQEND:
   1053 		sprintf(nfrontp, " REQUEST-END");
   1054 		nfrontp += strlen(nfrontp);
   1055 		break;
   1056 
   1057 	    case ENCRYPT_IS:
   1058 	    case ENCRYPT_REPLY:
   1059 		sprintf(nfrontp, " %s ", (pointer[1] == ENCRYPT_IS) ?
   1060 							"IS" : "REPLY");
   1061 		nfrontp += strlen(nfrontp);
   1062 		if (length < 3) {
   1063 		    sprintf(nfrontp, " (partial suboption???)");
   1064 		    nfrontp += strlen(nfrontp);
   1065 		    break;
   1066 		}
   1067 		if (ENCTYPE_NAME_OK(pointer[2]))
   1068 		    sprintf(nfrontp, "%s ", ENCTYPE_NAME(pointer[2]));
   1069 		else
   1070 		    sprintf(nfrontp, " %d (unknown)", pointer[2]);
   1071 		nfrontp += strlen(nfrontp);
   1072 
   1073 		encrypt_printsub(&pointer[1], length - 1, buf, sizeof(buf));
   1074 		sprintf(nfrontp, "%s", buf);
   1075 		nfrontp += strlen(nfrontp);
   1076 		break;
   1077 
   1078 	    case ENCRYPT_SUPPORT:
   1079 		i = 2;
   1080 		sprintf(nfrontp, " SUPPORT ");
   1081 		nfrontp += strlen(nfrontp);
   1082 		while (i < length) {
   1083 		    if (ENCTYPE_NAME_OK(pointer[i]))
   1084 			sprintf(nfrontp, "%s ", ENCTYPE_NAME(pointer[i]));
   1085 		    else
   1086 			sprintf(nfrontp, "%d ", pointer[i]);
   1087 		    nfrontp += strlen(nfrontp);
   1088 		    i++;
   1089 		}
   1090 		break;
   1091 
   1092 	    case ENCRYPT_ENC_KEYID:
   1093 		sprintf(nfrontp, " ENC_KEYID", pointer[1]);
   1094 		nfrontp += strlen(nfrontp);
   1095 		goto encommon;
   1096 
   1097 	    case ENCRYPT_DEC_KEYID:
   1098 		sprintf(nfrontp, " DEC_KEYID", pointer[1]);
   1099 		nfrontp += strlen(nfrontp);
   1100 		goto encommon;
   1101 
   1102 	    default:
   1103 		sprintf(nfrontp, " %d (unknown)", pointer[1]);
   1104 		nfrontp += strlen(nfrontp);
   1105 	    encommon:
   1106 		for (i = 2; i < length; i++) {
   1107 		    sprintf(nfrontp, " %d", pointer[i]);
   1108 		    nfrontp += strlen(nfrontp);
   1109 		}
   1110 		break;
   1111 	    }
   1112 	    break;
   1113 #endif
   1114 
   1115 	default:
   1116 	    if (TELOPT_OK(pointer[0]))
   1117 	        sprintf(nfrontp, "%s (unknown)", TELOPT(pointer[0]));
   1118 	    else
   1119 	        sprintf(nfrontp, "%d (unknown)", pointer[i]);
   1120 	    nfrontp += strlen(nfrontp);
   1121 	    for (i = 1; i < length; i++) {
   1122 		sprintf(nfrontp, " %d", pointer[i]);
   1123 		nfrontp += strlen(nfrontp);
   1124 	    }
   1125 	    break;
   1126 	}
   1127 	sprintf(nfrontp, "\r\n");
   1128 	nfrontp += strlen(nfrontp);
   1129 }
   1130 
   1131 /*
   1132  * Dump a data buffer in hex and ascii to the output data stream.
   1133  */
   1134 	void
   1135 printdata(tag, ptr, cnt)
   1136 	register char *tag;
   1137 	register char *ptr;
   1138 	register int cnt;
   1139 {
   1140 	register int i;
   1141 	char xbuf[30];
   1142 
   1143 	while (cnt) {
   1144 		/* flush net output buffer if no room for new data) */
   1145 		if ((&netobuf[BUFSIZ] - nfrontp) < 80) {
   1146 			netflush();
   1147 		}
   1148 
   1149 		/* add a line of output */
   1150 		sprintf(nfrontp, "%s: ", tag);
   1151 		nfrontp += strlen(nfrontp);
   1152 		for (i = 0; i < 20 && cnt; i++) {
   1153 			sprintf(nfrontp, "%02x", *ptr);
   1154 			nfrontp += strlen(nfrontp);
   1155 			if (isprint(*ptr)) {
   1156 				xbuf[i] = *ptr;
   1157 			} else {
   1158 				xbuf[i] = '.';
   1159 			}
   1160 			if (i % 2) {
   1161 				*nfrontp = ' ';
   1162 				nfrontp++;
   1163 			}
   1164 			cnt--;
   1165 			ptr++;
   1166 		}
   1167 		xbuf[i] = '\0';
   1168 		sprintf(nfrontp, " %s\r\n", xbuf );
   1169 		nfrontp += strlen(nfrontp);
   1170 	}
   1171 }
   1172 #endif /* DIAGNOSTICS */
   1173