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