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